SDL 3.0
hello.c
Go to the documentation of this file.
1/*
2 Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
3
4 This software is provided 'as-is', without any express or implied
5 warranty. In no event will the authors be held liable for any damages
6 arising from the use of this software.
7
8 Permission is granted to anyone to use this software for any purpose,
9 including commercial applications, and to alter it and redistribute it
10 freely.
11*/
12#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
13#include <SDL3/SDL.h>
14#include <SDL3/SDL_main.h>
15
18
19/* This function runs once at startup. */
20SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
21{
22 /* Create the window */
23 if (!SDL_CreateWindowAndRenderer("Hello World", 800, 600, SDL_WINDOW_FULLSCREEN, &window, &renderer)) {
24 SDL_Log("Couldn't create window and renderer: %s", SDL_GetError());
25 return SDL_APP_FAILURE;
26 }
27 return SDL_APP_CONTINUE;
28}
29
30/* This function runs when a new event (mouse input, keypresses, etc) occurs. */
31SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
32{
33 if (event->type == SDL_EVENT_KEY_DOWN ||
34 event->type == SDL_EVENT_QUIT) {
35 return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
36 }
37 return SDL_APP_CONTINUE;
38}
39
40/* This function runs once per frame, and is the heart of the program. */
42{
43 const char *message = "Hello World!";
44 int w = 0, h = 0;
45 float x, y;
46 const float scale = 4.0f;
47
48 /* Center the message and scale it up */
50 SDL_SetRenderScale(renderer, scale, scale);
51 x = ((w / scale) - SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(message)) / 2;
52 y = ((h / scale) - SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE) / 2;
53
54 /* Draw the message */
55 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
57 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
58 SDL_RenderDebugText(renderer, x, y, message);
60
61 return SDL_APP_CONTINUE;
62}
63
64/* This function runs once at shutdown. */
65void SDL_AppQuit(void *appstate, SDL_AppResult result)
66{
67}
68
#define NULL
const char * SDL_GetError(void)
@ SDL_EVENT_KEY_DOWN
Definition SDL_events.h:168
@ SDL_EVENT_QUIT
Definition SDL_events.h:89
SDL_AppResult
Definition SDL_init.h:110
@ SDL_APP_CONTINUE
Definition SDL_init.h:111
@ SDL_APP_SUCCESS
Definition SDL_init.h:112
@ SDL_APP_FAILURE
Definition SDL_init.h:113
void SDL_Log(SDL_PRINTF_FORMAT_STRING const char *fmt,...) SDL_PRINTF_VARARG_FUNC(1)
bool SDL_RenderPresent(SDL_Renderer *renderer)
bool SDL_SetRenderScale(SDL_Renderer *renderer, float scaleX, float scaleY)
bool SDL_RenderDebugText(SDL_Renderer *renderer, float x, float y, const char *str)
bool SDL_CreateWindowAndRenderer(const char *title, int width, int height, SDL_WindowFlags window_flags, SDL_Window **window, SDL_Renderer **renderer)
#define SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE
bool SDL_GetRenderOutputSize(SDL_Renderer *renderer, int *w, int *h)
bool SDL_RenderClear(SDL_Renderer *renderer)
struct SDL_Renderer SDL_Renderer
Definition SDL_render.h:119
bool SDL_SetRenderDrawColor(SDL_Renderer *renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a)
size_t SDL_strlen(const char *str)
struct SDL_Window SDL_Window
Definition SDL_video.h:173
#define SDL_WINDOW_FULLSCREEN
Definition SDL_video.h:189
void SDL_AppQuit(void *appstate, SDL_AppResult result)
Definition hello.c:65
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
Definition hello.c:20
SDL_AppResult SDL_AppIterate(void *appstate)
Definition hello.c:41
static SDL_Renderer * renderer
Definition hello.c:17
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
Definition hello.c:31
static SDL_Window * window
Definition hello.c:16
Uint32 type
Definition SDL_events.h:990