summaryrefslogtreecommitdiff
path: root/src/contrib/SDL-3.2.20/test/testoffscreen.c
diff options
context:
space:
mode:
author3gg <3gg@shellblade.net>2025-08-30 16:53:58 -0700
committer3gg <3gg@shellblade.net>2025-08-30 16:53:58 -0700
commit6aaedb813fa11ba0679c3051bc2eb28646b9506c (patch)
tree34acbfc9840e02cb4753e6306ea7ce978bf8b58e /src/contrib/SDL-3.2.20/test/testoffscreen.c
parent8f228ade99dd3d4c8da9b78ade1815c9adf85c8f (diff)
Update to SDL3
Diffstat (limited to 'src/contrib/SDL-3.2.20/test/testoffscreen.c')
-rw-r--r--src/contrib/SDL-3.2.20/test/testoffscreen.c167
1 files changed, 167 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/test/testoffscreen.c b/src/contrib/SDL-3.2.20/test/testoffscreen.c
new file mode 100644
index 0000000..8100bee
--- /dev/null
+++ b/src/contrib/SDL-3.2.20/test/testoffscreen.c
@@ -0,0 +1,167 @@
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
13/* Simple program: picks the offscreen backend and renders each frame to a bmp */
14
15#include <SDL3/SDL.h>
16#include <SDL3/SDL_main.h>
17#include <SDL3/SDL_test.h>
18#include <SDL3/SDL_opengl.h>
19
20#ifdef SDL_PLATFORM_EMSCRIPTEN
21#include <emscripten/emscripten.h>
22#endif
23
24static SDL_Renderer *renderer = NULL;
25static SDL_Window *window = NULL;
26static int done = false;
27static int frame_number = 0;
28static int width = 640;
29static int height = 480;
30static unsigned int max_frames = 200;
31
32static void draw(void)
33{
34 SDL_FRect rect;
35
36 SDL_SetRenderDrawColor(renderer, 0x10, 0x9A, 0xCE, 0xFF);
37 SDL_RenderClear(renderer);
38
39 /* Grow based on the frame just to show a difference per frame of the region */
40 rect.x = 0.0f;
41 rect.y = 0.0f;
42 rect.w = (float)((frame_number * 2) % width);
43 rect.h = (float)((frame_number * 2) % height);
44 SDL_SetRenderDrawColor(renderer, 0xFF, 0x10, 0x21, 0xFF);
45 SDL_RenderFillRect(renderer, &rect);
46
47 SDL_RenderPresent(renderer);
48}
49
50static void save_surface_to_bmp(void)
51{
52 SDL_Surface *surface;
53 char file[128];
54
55 surface = SDL_RenderReadPixels(renderer, NULL);
56
57 (void)SDL_snprintf(file, sizeof(file), "SDL_window%" SDL_PRIs32 "-%8.8d.bmp",
58 SDL_GetWindowID(window), ++frame_number);
59
60 SDL_SaveBMP(surface, file);
61 SDL_DestroySurface(surface);
62}
63
64static void loop(void)
65{
66 SDL_Event event;
67
68 /* Check for events */
69 while (SDL_PollEvent(&event)) {
70 switch (event.type) {
71 case SDL_EVENT_QUIT:
72 done = true;
73 break;
74 default:
75 break;
76 }
77 }
78
79 draw();
80 save_surface_to_bmp();
81
82#ifdef SDL_PLATFORM_EMSCRIPTEN
83 if (done) {
84 emscripten_cancel_main_loop();
85 }
86#endif
87}
88
89int main(int argc, char *argv[])
90{
91#ifndef SDL_PLATFORM_EMSCRIPTEN
92 Uint64 then, now;
93 Uint32 frames;
94#endif
95 SDLTest_CommonState *state;
96
97 /* Initialize test framework */
98 state = SDLTest_CommonCreateState(argv, 0);
99 if (!state) {
100 return 1;
101 }
102
103 /* Parse commandline */
104 if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
105 return 1;
106 }
107
108 /* Force the offscreen renderer, if it cannot be created then fail out */
109 SDL_SetHint(SDL_HINT_VIDEO_DRIVER, "offscreen");
110 if (!SDL_InitSubSystem(SDL_INIT_VIDEO)) {
111 SDL_Log("Couldn't initialize the offscreen video driver: %s",
112 SDL_GetError());
113 return 1;
114 }
115
116 /* If OPENGL fails to init it will fallback to using a framebuffer for rendering */
117 window = SDL_CreateWindow("Offscreen Test", width, height, 0);
118
119 if (!window) {
120 SDL_Log("Couldn't create window: %s", SDL_GetError());
121 return 1;
122 }
123
124 renderer = SDL_CreateRenderer(window, NULL);
125
126 if (!renderer) {
127 SDL_Log("Couldn't create renderer: %s",
128 SDL_GetError());
129 return 1;
130 }
131
132 SDL_RenderClear(renderer);
133
134#ifndef SDL_PLATFORM_EMSCRIPTEN
135 /* Main render loop */
136 frames = 0;
137 then = SDL_GetTicks();
138 done = 0;
139#endif
140
141 SDL_Log("Rendering %u frames offscreen", max_frames);
142
143#ifdef SDL_PLATFORM_EMSCRIPTEN
144 emscripten_set_main_loop(loop, 0, 1);
145#else
146 while (!done && frames < max_frames) {
147 ++frames;
148 loop();
149
150 /* Print out some timing information, along with remaining frames */
151 if (frames % (max_frames / 10) == 0) {
152 now = SDL_GetTicks();
153 if (now > then) {
154 double fps = ((double)frames * 1000) / (now - then);
155 SDL_Log("Frames remaining: %" SDL_PRIu32 " rendering at %2.2f frames per second", max_frames - frames, fps);
156 }
157 }
158 }
159#endif
160
161 SDL_DestroyRenderer(renderer);
162 SDL_DestroyWindow(window);
163 SDL_Quit();
164 SDLTest_CommonDestroyState(state);
165
166 return 0;
167}