#include #include #include #include //Screen dimension constants const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; void renderText(SDL_Renderer *renderer, TTF_Font *font, char *string, uint16_t x, uint16_t y, uint8_t r, uint8_t g, uint8_t b, uint8_t a) { SDL_Texture *fontTempTex; SDL_Surface *fontTempSurf; SDL_Color color = {r, g, b, a}; fontTempSurf = TTF_RenderText_Blended(font, string, color); fontTempTex = SDL_CreateTextureFromSurface(renderer, fontTempSurf); int iW, iH; SDL_QueryTexture(fontTempTex, NULL, NULL, &iW, &iH); SDL_Rect srcRect; srcRect.x = 0; srcRect.y = 0; srcRect.w = iW; srcRect.h = iH; SDL_Rect dstRect; dstRect.x = x; dstRect.y = y; dstRect.w = iW; dstRect.h = iH; SDL_RenderCopy(renderer, fontTempTex, &srcRect, &dstRect); SDL_FreeSurface(fontTempSurf); SDL_DestroyTexture(fontTempTex); } int main(int argc, char *args[]) { //The window we'll be rendering to SDL_Window *window = NULL; //The surface contained by the window SDL_Renderer *renderer = NULL; SDL_Rect rect1; TTF_Font *gFont; //Initialize SDL if (SDL_Init(SDL_INIT_VIDEO) < 0) { printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); return 1; } //Initialize SDL_ttf if (TTF_Init() == -1) { printf("SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError()); return 1; } gFont = TTF_OpenFont("../rasterthingy.ttf", 50); if (gFont == NULL) { printf("Failed to load lazy font! SDL_ttf Error: %s\n", TTF_GetError()); return 1; } //Create window window = SDL_CreateWindow("SDLko", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); if (window == NULL) { printf("Window could not be created! SDL_Error: %s\n", SDL_GetError()); return 1; } //Get window surface renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); //Hack to get window to stay up SDL_Event e; bool quit = false; Uint64 start; Uint64 end; while (!quit) { start = SDL_GetTicks64(); while (SDL_PollEvent(&e)) { if (e.type == SDL_QUIT) quit = true; } SDL_SetRenderDrawColor(renderer, 128, 0, 0, 255); SDL_RenderClear(renderer); SDL_SetRenderDrawColor(renderer, 0, 128, 0, 255); rect1.x = (rect1.x + 1) % 400; rect1.y = 10; rect1.w = 50; rect1.h = 10; SDL_RenderFillRect(renderer, &rect1); char textTemp[12]; sprintf(textTemp, "%d", rect1.x); renderText(renderer, gFont, textTemp, 100, 100, 255, 255, 255, 255); SDL_RenderPresent(renderer); end = SDL_GetTicks64(); SDL_Delay((1000 / 60) - (end - start)); } TTF_CloseFont(gFont); //Destroy window SDL_DestroyWindow(window); //Quit SDL subsystems SDL_Quit(); return 0; }