Debian Codegraph @

test/testoffscreen.c

raw
1 /*
2 Copyright (C) 1997-2026 Sam Lantinga <[email protected]>
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 <stdlib.h>
16 #include <stdio.h>
17 #include <time.h>
18
19 #ifdef __EMSCRIPTEN__
20 #include <emscripten/emscripten.h>
21 #endif
22
23 #include "SDL.h"
24 #include "SDL_stdinc.h"
25 #include "SDL_opengl.h"
26
27 static SDL_Renderer *renderer = NULL;
28 static SDL_Window *window = NULL;
29 static int done = SDL_FALSE;
30 static int frame_number = 0;
31 static int width = 640;
32 static int height = 480;
33 static unsigned int max_frames = 200;
34
35 void draw(void)
36 {
37 SDL_Rect Rect;
38
39 SDL_SetRenderDrawColor(renderer, 0x10, 0x9A, 0xCE, 0xFF);
40 SDL_RenderClear(renderer);
41
42 /* Grow based on the frame just to show a difference per frame of the region */
43 Rect.x = 0;
44 Rect.y = 0;
45 Rect.w = (frame_number * 2) % width;
46 Rect.h = (frame_number * 2) % height;
47 SDL_SetRenderDrawColor(renderer, 0xFF, 0x10, 0x21, 0xFF);
48 SDL_RenderFillRect(renderer, &Rect);
49
50 SDL_RenderPresent(renderer);
51 }
52
53 void save_surface_to_bmp(void)
54 {
55 SDL_Surface *surface;
56 Uint32 r_mask, g_mask, b_mask, a_mask;
57 Uint32 pixel_format;
58 char file[128];
59 int bbp;
60
61 pixel_format = SDL_GetWindowPixelFormat(window);
62 SDL_PixelFormatEnumToMasks(pixel_format, &bbp, &r_mask, &g_mask, &b_mask, &a_mask);
63
64 surface = SDL_CreateRGBSurface(0, width, height, bbp, r_mask, g_mask, b_mask, a_mask);
65 SDL_RenderReadPixels(renderer, NULL, pixel_format, (void*)surface->pixels, surface->pitch);
66
67 SDL_snprintf(file, sizeof(file), "SDL_window%" SDL_PRIs32 "-%8.8d.bmp",
68 SDL_GetWindowID(window), ++frame_number);
69
70 SDL_SaveBMP(surface, file);
71 SDL_FreeSurface(surface);
72 }
73
74 void loop(void)
75 {
76 SDL_Event event;
77
78 /* Check for events */
79 while (SDL_PollEvent(&event)) {
80 switch (event.type) {
81 case SDL_QUIT:
82 done = SDL_TRUE;
83 break;
84 }
85 }
86
87 draw();
88 save_surface_to_bmp();
89
90 #ifdef __EMSCRIPTEN__
91 if (done) {
92 emscripten_cancel_main_loop();
93 }
94 #endif
95 }
96
97 int main(int argc, char *argv[])
98 {
99 #ifndef __EMSCRIPTEN__
100 Uint32 then, now, frames;
101 #endif
102
103 /* Enable standard application logging */
104 SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
105
106 /* Force the offscreen renderer, if it cannot be created then fail out */
107 if (SDL_VideoInit("offscreen") < 0) {
108 SDL_Log("Couldn't initialize the offscreen video driver: %s\n",
109 SDL_GetError());
110 return SDL_FALSE;
111 }
112
113 /* If OPENGL fails to init it will fallback to using a framebuffer for rendering */
114 window = SDL_CreateWindow("Offscreen Test",
115 SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
116 width, height, 0);
117
118 if (!window) {
119 SDL_Log("Couldn't create window: %s\n", SDL_GetError());
120 return SDL_FALSE;
121 }
122
123 renderer = SDL_CreateRenderer(window, -1, 0);
124
125 if (!renderer) {
126 SDL_Log("Couldn't create renderer: %s\n",
127 SDL_GetError());
128 return SDL_FALSE;
129 }
130
131 SDL_RenderClear(renderer);
132
133 srand((unsigned int)time(NULL));
134
135 #ifndef __EMSCRIPTEN__
136 /* Main render loop */
137 frames = 0;
138 then = SDL_GetTicks();
139 done = 0;
140 #endif
141
142 SDL_Log("Rendering %u frames offscreen\n", max_frames);
143
144 #ifdef __EMSCRIPTEN__
145 emscripten_set_main_loop(loop, 0, 1);
146 #else
147 while (!done && frames < max_frames) {
148 ++frames;
149 loop();
150
151 /* Print out some timing information, along with remaining frames */
152 if (frames % (max_frames / 10) == 0) {
153 now = SDL_GetTicks();
154 if (now > then) {
155 double fps = ((double)frames * 1000) / (now - then);
156 SDL_Log("Frames remaining: %" SDL_PRIu32 " rendering at %2.2f frames per second\n", max_frames - frames, fps);
157 }
158 }
159 }
160 #endif
161
162 SDL_DestroyRenderer(renderer);
163 SDL_DestroyWindow(window);
164 SDL_Quit();
165
166 return 0;
167 }
168
169 /* vi: set ts=4 sw=4 expandtab: */