Laboratorium Komputerowe Progmar
Marcin Załęczny
Na stronie używamy cookies. Korzystając z witryny wyrażasz zgodę na ich wykorzystywanie.
Zamknij
Oferta
Kontakt
0004_RegulatedSpeedAnimation.cpp
Do pobrania:
Font
Aller_Rg.ttf
Link do kodu utworzonego na kursie:
0004_RegulatedSpeedAnimationCourseOutput
#define SDL_MAIN_USE_CALLBACKS 1 #include
#include
#include
#include
struct FGameObject { double t{}; float red{}; float green{}; float blue{}; std::string Text; } GameObject; SDL_Window *Window{}; SDL_Renderer *Renderer{}; TTF_Font* Font{}; void DrawString(SDL_Renderer* Renderer, const char* String, float x, float y); void Update(); SDL_AppResult SDL_AppInit(void** appstate, int argc, char* argv[]) { SDL_SetAppMetadata("Basic SDL3 App", "1.0", "com.example.001_BasicSDL3App"); if (!SDL_Init(SDL_INIT_EVENTS | SDL_INIT_VIDEO)) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init failed (%s)", SDL_GetError()); return SDL_APP_FAILURE; } if (!TTF_Init()) { SDL_Log("Couldn't initialize TTF: %s", SDL_GetError()); return SDL_APP_FAILURE; } if (!SDL_CreateWindowAndRenderer("examples/001_BasicOldFashionedApp", 640, 480, 0, &Window, &Renderer)) { SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); return SDL_APP_FAILURE; } std::string FontPath = std::string(SDL_GetBasePath()) + "Aller_Rg.ttf"; Font = TTF_OpenFont(FontPath.c_str(), 140.0); if (!Font) { SDL_Log("Couldn't open Font [%s]: %s", FontPath.c_str(), SDL_GetError()); return SDL_APP_FAILURE; } SDL_Log("SDL_PI_D=%f", SDL_PI_D); return SDL_APP_CONTINUE; } SDL_AppResult SDL_AppEvent(void* appstate, SDL_Event* event) { switch (event->type) { case SDL_EVENT_QUIT: case SDL_EVENT_FINGER_UP: SDL_Log("0003_BasicAnimation: Exit requested by user"); return SDL_APP_SUCCESS; } return SDL_APP_CONTINUE; } void Update() { GameObject.t = ((double)SDL_GetTicks()) / 1000.0; // in seconds const double PerSeconds = 3.0; const double v = 2 * SDL_PI_D / PerSeconds; GameObject.red = (float)(0.5 + 0.5 * SDL_sin(v * GameObject.t + SDL_PI_D / 2)); GameObject.green = (float)(0.5 + 0.5 * SDL_sin(v * GameObject.t - SDL_PI_D)); GameObject.blue = (float)(0.5 + 0.5 * SDL_sin(v * GameObject.t + SDL_PI_D)); } SDL_AppResult SDL_AppIterate(void* appstate) { Update(); SDL_SetRenderDrawColorFloat(Renderer, GameObject.red, GameObject.green, GameObject.blue, SDL_ALPHA_OPAQUE_FLOAT); SDL_RenderClear(Renderer); SDL_snprintf(GameObject.Text.data(), 255, "Aktualny czas: %.3f", GameObject.t); DrawString(Renderer, GameObject.Text.c_str(), 10.0f, 10.0f); SDL_RenderPresent(Renderer); return SDL_APP_CONTINUE; } void SDL_AppQuit(void* appstate, SDL_AppResult result) { if (Font) { TTF_CloseFont(Font); } if (Window) { SDL_DestroyWindow(Window); } if (Renderer) { SDL_DestroyRenderer(Renderer); } TTF_Quit(); SDL_Quit(); } void DrawString(SDL_Renderer* Renderer, const char* String, float x, float y) { SDL_Color Color = { 0xff, 0xff, 0x00, 0xff }; SDL_Surface* TextSurface = TTF_RenderText_Blended(Font, String, 0, Color); //SDL_Surface* TextSurface = TTF_RenderText_Solid(m_Font, String, 0, m_Color); // TTF_RenderText_Blended(m_Font, String, 0, m_Color); if (TextSurface) { SDL_Texture* TextTexture = SDL_CreateTextureFromSurface(Renderer, TextSurface); if (TextTexture != nullptr) { SDL_FRect destRect; destRect.x = x; destRect.y = y; destRect.w = static_cast
(TextSurface->w); destRect.h = static_cast
(TextSurface->h); SDL_RenderTexture(Renderer, TextTexture, nullptr, &destRect); SDL_DestroyTexture(TextTexture); } SDL_DestroySurface(TextSurface); } }