Created
November 16, 2025 16:37
-
-
Save ferennag/c0cdfaf8a9fd90f6de06cb43ee048c3b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include "SDL3/SDL_error.h" | |
| #include "SDL3/SDL_render.h" | |
| #include <SDL3/SDL.h> | |
| #include <SDL3_ttf/SDL_ttf.h> | |
| #include <stdio.h> | |
| #include <string.h> | |
| int main(int argc, char **argv) { | |
| SDL_Init(SDL_INIT_VIDEO); | |
| SDL_Window *window = SDL_CreateWindow("platformer", 1024, 768, SDL_WINDOW_RESIZABLE); | |
| SDL_Renderer *renderer = SDL_CreateRenderer(window, NULL); | |
| if (!TTF_Init()) { | |
| printf("Error initializing TTF: %s\n", SDL_GetError()); | |
| return -1; | |
| } | |
| TTF_Font *font = TTF_OpenFont("assets/fonts/Roboto-Regular.ttf", 18.0f); | |
| if (!font) { | |
| printf("Error loading font: %s\n", SDL_GetError()); | |
| return -1; | |
| } | |
| bool running = true; | |
| while (running) { | |
| SDL_Event event; | |
| while (SDL_PollEvent(&event)) { | |
| switch (event.type) { | |
| case SDL_EVENT_QUIT: | |
| running = false; | |
| break; | |
| case SDL_EVENT_KEY_DOWN: | |
| switch (event.key.key) { | |
| case SDLK_ESCAPE: | |
| running = false; | |
| break; | |
| } | |
| break; | |
| } | |
| } | |
| SDL_FRect rect = {200.0f, 200.0f, 50.0f, 50.0f}; | |
| int width, height; | |
| SDL_GetRenderOutputSize(renderer, &width, &height); | |
| char buf[1024]; | |
| memset(buf, 0, 1024); | |
| sprintf(buf, "Render Size: %d %d", width, height); | |
| SDL_Color textColor = {255, 255, 255, 255}; | |
| SDL_Surface *text = TTF_RenderText_Blended(font, buf, 0, textColor); | |
| SDL_Texture *textTexture = SDL_CreateTextureFromSurface(renderer, text); | |
| float text_w = 0.0f, text_h = 0.0f; | |
| SDL_GetTextureSize(textTexture, &text_w, &text_h); | |
| SDL_FRect text_dst = {20.0f, 20.0f, text_w, text_h}; | |
| SDL_SetRenderDrawColorFloat(renderer, 0.1f, 0.1f, 0.1f, 0.1f); | |
| SDL_RenderClear(renderer); | |
| SDL_SetRenderDrawColorFloat(renderer, 0.8f, 0.1f, 0.1f, 0.1f); | |
| SDL_RenderFillRect(renderer, &rect); | |
| SDL_RenderTexture(renderer, textTexture, NULL, &text_dst); | |
| SDL_RenderPresent(renderer); | |
| } | |
| TTF_Quit(); | |
| SDL_Quit(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment