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
#define SDL_MAIN_USE_CALLBACKS 1 #include
#include
#include
#include
struct FGameObject { double t{}; double v{}; float red{}; float green{}; float blue{}; std::string Text{ '\0', 255 }; } 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.MyGame"); 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_LogError(SDL_LOG_CATEGORY_APPLICATION, "TTF_Init failed (%s)", SDL_GetError()); return SDL_APP_FAILURE; } if (!SDL_CreateWindowAndRenderer("examples/MyGame", 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()) + "allerrg.ttf"; Font = TTF_OpenFont(FontPath.c_str(), 140.0f); if (!Font) { SDL_Log("Couldn't open font [%s]: %s", FontPath.c_str(), SDL_GetError()); return SDL_APP_FAILURE; } 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; double PerSeconds = 10.0f; GameObject.v = 2 * SDL_PI_D / PerSeconds; //v * t = 2PI => v * 1 = 2PI => v = 2PI //v * t = 2PI => v * 2 = 2PI => v = 2PI / 2 //v * t = 2PI => v * 3 = 2PI => v = 2PI / 3 GameObject.red = (float)(0.5 + 0.5 * SDL_sin(GameObject.v * GameObject.t + SDL_PI_D / 2.0f)); GameObject.green = (float)(0.5 + 0.5 * SDL_sin(GameObject.v * GameObject.t + SDL_PI_D * 3.0f / 2.0f)); GameObject.blue = (float)(0.5 + 0.5 * SDL_sin(GameObject.v * GameObject.t + SDL_PI_D * 3.0f / 2.0f)); } 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(), 254, "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 = { 255, 255, 0, 255 }; SDL_Surface* TextSurface = TTF_RenderText_Blended(Font, String, 0, Color); if (TextSurface) { SDL_Texture* TextTexture = SDL_CreateTextureFromSurface(Renderer, TextSurface); if (TextTexture) { SDL_FRect DestRect; DestRect.x = x; DestRect.y = y; DestRect.w = TextTexture->w; DestRect.h = TextTexture->h; SDL_RenderTexture(Renderer, TextTexture, nullptr, &DestRect); SDL_DestroyTexture(TextTexture); } SDL_DestroySurface(TextSurface); } }