|
9 | 9 | including commercial applications, and to alter it and redistribute it
|
10 | 10 | freely.
|
11 | 11 | */
|
| 12 | +#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */ |
12 | 13 | #include <SDL3/SDL.h>
|
13 | 14 | #include <SDL3/SDL_main.h>
|
14 | 15 |
|
15 |
| -int main(int argc, char *argv[]) |
| 16 | +static SDL_Window *window = NULL; |
| 17 | +static SDL_Renderer *renderer = NULL; |
| 18 | + |
| 19 | +/* This function runs once at startup. */ |
| 20 | +SDL_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\n", 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. */ |
| 31 | +SDL_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. */ |
| 41 | +SDL_AppResult SDL_AppIterate(void *appstate) |
16 | 42 | {
|
17 |
| - SDL_Window *window = NULL; |
18 |
| - SDL_Renderer *renderer = NULL; |
19 | 43 | const char *message = "Hello World!";
|
20 | 44 | int w = 0, h = 0;
|
21 | 45 | float x, y;
|
22 | 46 | const float scale = 4.0f;
|
23 |
| - bool done = false; |
24 | 47 |
|
25 |
| - /* Create the window */ |
26 |
| - if (!SDL_CreateWindowAndRenderer("Hello World", 0, 0, SDL_WINDOW_FULLSCREEN, &window, &renderer)) { |
27 |
| - SDL_Log("Couldn't create window and renderer: %s\n", SDL_GetError()); |
28 |
| - return 1; |
29 |
| - } |
| 48 | + /* Center the message and scale it up */ |
| 49 | + SDL_GetRenderOutputSize(renderer, &w, &h); |
| 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; |
30 | 53 |
|
31 |
| - while (!done) { |
32 |
| - SDL_Event event; |
33 |
| - |
34 |
| - /* Handle events */ |
35 |
| - while (SDL_PollEvent(&event)) { |
36 |
| - if (event.type == SDL_EVENT_KEY_DOWN || |
37 |
| - event.type == SDL_EVENT_MOUSE_BUTTON_DOWN || |
38 |
| - event.type == SDL_EVENT_QUIT) { |
39 |
| - done = true; |
40 |
| - } |
41 |
| - } |
42 |
| - |
43 |
| - /* Center the message and scale it up */ |
44 |
| - SDL_GetRenderOutputSize(renderer, &w, &h); |
45 |
| - SDL_SetRenderScale(renderer, scale, scale); |
46 |
| - x = ((w / scale) - SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE * SDL_strlen(message)) / 2; |
47 |
| - y = ((h / scale) - SDL_DEBUG_TEXT_FONT_CHARACTER_SIZE) / 2; |
48 |
| - |
49 |
| - /* Draw the message */ |
50 |
| - SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); |
51 |
| - SDL_RenderClear(renderer); |
52 |
| - SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); |
53 |
| - SDL_RenderDebugText(renderer, x, y, message); |
54 |
| - SDL_RenderPresent(renderer); |
55 |
| - } |
56 |
| - SDL_Quit(); |
| 54 | + /* Draw the message */ |
| 55 | + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); |
| 56 | + SDL_RenderClear(renderer); |
| 57 | + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); |
| 58 | + SDL_RenderDebugText(renderer, x, y, message); |
| 59 | + SDL_RenderPresent(renderer); |
| 60 | + |
| 61 | + return SDL_APP_CONTINUE; |
| 62 | +} |
57 | 63 |
|
58 |
| - return 0; |
| 64 | +/* This function runs once at shutdown. */ |
| 65 | +void SDL_AppQuit(void *appstate, SDL_AppResult result) |
| 66 | +{ |
59 | 67 | }
|
60 | 68 |
|
0 commit comments