rendering rectangles

This commit is contained in:
Josh Lyon
2025-08-12 20:02:48 -06:00
parent 256781587b
commit 4e3e4f9aab

View File

@@ -9,8 +9,11 @@
including commercial applications, and to alter it and redistribute it including commercial applications, and to alter it and redistribute it
freely. freely.
*/ */
#include <SDL3/SDL_rect.h>
#include <SDL3/SDL_render.h>
#include <string> #include <string>
#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */ #define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <SDL3/SDL_main.h> #include <SDL3/SDL_main.h>
@@ -20,46 +23,56 @@ static SDL_Renderer *renderer = nullptr;
/* This function runs once at startup. */ /* This function runs once at startup. */
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{ {
/* Create the window */ /* Create the window */
if (!SDL_CreateWindowAndRenderer("Hello World", 800, 600, SDL_WINDOW_FULLSCREEN, &window, &renderer)) { if (!SDL_CreateWindowAndRenderer("Hello World", 800, 600, SDL_WINDOW_FULLSCREEN, &window, &renderer)) {
SDL_Log("Couldn't create window and renderer: %s", SDL_GetError()); SDL_Log("Couldn't create window and renderer: %s", SDL_GetError());
return SDL_APP_FAILURE; return SDL_APP_FAILURE;
} }
return SDL_APP_CONTINUE; return SDL_APP_CONTINUE;
} }
/* This function runs when a new event (mouse input, keypresses, etc) occurs. */ /* This function runs when a new event (mouse input, keypresses, etc) occurs. */
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
{ {
if (event->type == SDL_EVENT_KEY_DOWN || if (event->type == SDL_EVENT_KEY_DOWN ||
event->type == SDL_EVENT_QUIT) { event->type == SDL_EVENT_QUIT) {
return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
} }
return SDL_APP_CONTINUE; return SDL_APP_CONTINUE;
} }
/* This function runs once per frame, and is the heart of the program. */ /* This function runs once per frame, and is the heart of the program. */
SDL_AppResult SDL_AppIterate(void *appstate) SDL_AppResult SDL_AppIterate(void *appstate)
{ {
std::string message("Hello World!"); std::string message("Hello World!");
int w = 0, h = 0; int w = 0, h = 0;
float x, y; float x, y;
const float scale = 4.0f; const float scale = 4.0f;
/* Center the message and scale it up */ /* Center the message and scale it up */
SDL_GetRenderOutputSize(renderer, &w, &h); SDL_GetRenderOutputSize(renderer, &w, &h);
SDL_SetRenderScale(renderer, scale, scale); SDL_SetRenderScale(renderer, scale, scale);
x = ((w / scale) - SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * message.length()) / 2; x = ((w / scale) - SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * message.length()) / 2;
y = ((h / scale) - SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE) / 2; y = ((h / scale) - SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE) / 2;
/* Draw the message */ SDL_FRect rect {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); .x = 30,
SDL_RenderClear(renderer); .y = 30,
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); .w = 100,
SDL_RenderDebugText(renderer, x, y, message.c_str()); .h = 100
SDL_RenderPresent(renderer); };
return SDL_APP_CONTINUE;
/* Draw the message */
SDL_SetRenderDrawColor(renderer, 0, 0, 200, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderFillRect(renderer, &rect);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderDebugText(renderer, x, y, message.c_str());
SDL_RenderPresent(renderer);
return SDL_APP_CONTINUE;
} }
/* This function runs once at shutdown. */ /* This function runs once at shutdown. */