hello.c
Go to the documentation of this file.
/*
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;
/* This function runs once at startup. */
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
/* Create the window */
if (!SDL_CreateWindowAndRenderer("Hello World", 800, 600, SDL_WINDOW_FULLSCREEN, &window, &renderer)) {
SDL_Log("Couldn't create window and renderer: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
return SDL_APP_CONTINUE;
}
/* This function runs when a new event (mouse input, keypresses, etc) occurs. */
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
{
if (event->type == SDL_EVENT_KEY_DOWN ||
event->type == SDL_EVENT_QUIT) {
return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
}
return SDL_APP_CONTINUE;
}
/* This function runs once per frame, and is the heart of the program. */
SDL_AppResult SDL_AppIterate(void *appstate)
{
const char *message = "Hello World!";
int w = 0, h = 0;
float x, y;
const float scale = 4.0f;
/* Center the message and scale it up */
SDL_GetRenderOutputSize(renderer, &w, &h);
SDL_SetRenderScale(renderer, scale, scale);
x = ((w / scale) - SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(message)) / 2;
y = ((h / scale) - SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE) / 2;
/* Draw the message */
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderDebugText(renderer, x, y, message);
SDL_RenderPresent(renderer);
return SDL_APP_CONTINUE;
}
/* This function runs once at shutdown. */
void SDL_AppQuit(void *appstate, SDL_AppResult result)
{
}
SDL.h
NULL
#define NULL
Definition SDL_begin_code.h:417
SDL_GetError
const char * SDL_GetError(void)
SDL_EVENT_KEY_DOWN
@ SDL_EVENT_KEY_DOWN
Definition SDL_events.h:168
SDL_EVENT_QUIT
@ SDL_EVENT_QUIT
Definition SDL_events.h:89
SDL_AppResult
SDL_AppResult
Definition SDL_init.h:110
SDL_APP_CONTINUE
@ SDL_APP_CONTINUE
Definition SDL_init.h:111
SDL_APP_SUCCESS
@ SDL_APP_SUCCESS
Definition SDL_init.h:112
SDL_APP_FAILURE
@ SDL_APP_FAILURE
Definition SDL_init.h:113
SDL_Log
void SDL_Log(SDL_PRINTF_FORMAT_STRING const char *fmt,...) SDL_PRINTF_VARARG_FUNC(1)
SDL_main.h
SDL_RenderPresent
bool SDL_RenderPresent(SDL_Renderer *renderer)
SDL_SetRenderScale
bool SDL_SetRenderScale(SDL_Renderer *renderer, float scaleX, float scaleY)
SDL_RenderDebugText
bool SDL_RenderDebugText(SDL_Renderer *renderer, float x, float y, const char *str)
SDL_CreateWindowAndRenderer
bool SDL_CreateWindowAndRenderer(const char *title, int width, int height, SDL_WindowFlags window_flags, SDL_Window **window, SDL_Renderer **renderer)
SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE
#define SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE
Definition SDL_render.h:2569
SDL_GetRenderOutputSize
bool SDL_GetRenderOutputSize(SDL_Renderer *renderer, int *w, int *h)
SDL_RenderClear
bool SDL_RenderClear(SDL_Renderer *renderer)
SDL_Renderer
struct SDL_Renderer SDL_Renderer
Definition SDL_render.h:119
SDL_SetRenderDrawColor
bool SDL_SetRenderDrawColor(SDL_Renderer *renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
SDL_strlen
size_t SDL_strlen(const char *str)
SDL_Window
struct SDL_Window SDL_Window
Definition SDL_video.h:173
SDL_WINDOW_FULLSCREEN
#define SDL_WINDOW_FULLSCREEN
Definition SDL_video.h:189
SDL_AppQuit
void SDL_AppQuit(void *appstate, SDL_AppResult result)
Definition hello.c:65
SDL_AppInit
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
Definition hello.c:20
SDL_AppIterate
SDL_AppResult SDL_AppIterate(void *appstate)
Definition hello.c:41
renderer
static SDL_Renderer * renderer
Definition hello.c:17
SDL_AppEvent
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
Definition hello.c:31
window
static SDL_Window * window
Definition hello.c:16
SDL_Event
Definition SDL_events.h:987
SDL_Event::type
Uint32 type
Definition SDL_events.h:988