SDL  2.0
testoffscreen.c
Go to the documentation of this file.
1 /*
2  Copyright (C) 1997-2020 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 <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 
29 static int done = SDL_FALSE;
30 static int frame_number = 0;
31 static int width = 640;
32 static int height = 480;
33 static int max_frames = 200;
34 
35 void
37 {
38  SDL_Rect Rect;
39 
40  SDL_SetRenderDrawColor(renderer, 0x10, 0x9A, 0xCE, 0xFF);
42 
43  /* Grow based on the frame just to show a difference per frame of the region */
44  Rect.x = 0;
45  Rect.y = 0;
46  Rect.w = (frame_number * 2) % width;
47  Rect.h = (frame_number * 2) % height;
48  SDL_SetRenderDrawColor(renderer, 0xFF, 0x10, 0x21, 0xFF);
50 
52 }
53 
54 void
56 {
58  Uint32 r_mask, g_mask, b_mask, a_mask;
59  Uint32 pixel_format;
60  char file[128];
61  int bbp;
62 
63  pixel_format = SDL_GetWindowPixelFormat(window);
64  SDL_PixelFormatEnumToMasks(pixel_format, &bbp, &r_mask, &g_mask, &b_mask, &a_mask);
65 
66  surface = SDL_CreateRGBSurface(0, width, height, bbp, r_mask, g_mask, b_mask, a_mask);
67  SDL_RenderReadPixels(renderer, NULL, pixel_format, (void*)surface->pixels, surface->pitch);
68 
69  SDL_snprintf(file, sizeof(file), "SDL_window%d-%8.8d.bmp",
71 
72  SDL_SaveBMP(surface, file);
74 }
75 
76 void
78 {
80 
81  /* Check for events */
82  while (SDL_PollEvent(&event)) {
83  switch (event.type) {
84  case SDL_QUIT:
85  done = SDL_TRUE;
86  break;
87  }
88  }
89 
90  draw();
92 
93 #ifdef __EMSCRIPTEN__
94  if (done) {
95  emscripten_cancel_main_loop();
96  }
97 #endif
98 }
99 
100 int
101 main(int argc, char *argv[])
102 {
103  Uint32 then, now, frames;
104 
105  /* Enable standard application logging */
107 
108  /* Force the offscreen renderer, if it cannot be created then fail out */
109  if (SDL_VideoInit("offscreen") < 0) {
110  SDL_Log("Couldn't initialize the offscreen video driver: %s\n",
111  SDL_GetError());
112  return SDL_FALSE;
113  }
114 
115  /* If OPENGL fails to init it will fallback to using a framebuffer for rendering */
116  window = SDL_CreateWindow("Offscreen Test",
118  width, height, 0);
119 
120  if (!window) {
121  SDL_Log("Couldn't create window: %s\n",
122  SDL_GetError());
123  return SDL_FALSE;
124  }
125 
127 
128  if (!renderer) {
129  SDL_Log("Couldn't create renderer: %s\n",
130  SDL_GetError());
131  return SDL_FALSE;
132  }
133 
135 
136  srand((unsigned int)time(NULL));
137 
138  /* Main render loop */
139  frames = 0;
140  then = SDL_GetTicks();
141  done = 0;
142 
143  SDL_Log("Rendering %i frames offscreen\n", max_frames);
144 
145 #ifdef __EMSCRIPTEN__
146  emscripten_set_main_loop(loop, 0, 1);
147 #else
148  while (!done && frames < max_frames) {
149  ++frames;
150  loop();
151 
152  /* Print out some timing information, along with remaining frames */
153  if (frames % (max_frames / 10) == 0) {
154  now = SDL_GetTicks();
155  if (now > then) {
156  double fps = ((double) frames * 1000) / (now - then);
157  SDL_Log("Frames remaining: %i rendering at %2.2f frames per second\n", max_frames - frames, fps);
158  }
159  }
160  }
161 #endif
162 
165  SDL_Quit();
166 
167  return 0;
168 }
169 
170 /* vi: set ts=4 sw=4 expandtab: */
SDL.h
SDL_GetError
#define SDL_GetError
Definition: SDL_dynapi_overrides.h:113
SDL_RenderPresent
#define SDL_RenderPresent
Definition: SDL_dynapi_overrides.h:346
SDL_Surface
A collection of pixels used in software blitting.
Definition: SDL_surface.h:71
time
EGLSurface EGLnsecsANDROID time
Definition: eglext.h:518
SDL_PollEvent
#define SDL_PollEvent
Definition: SDL_dynapi_overrides.h:122
NULL
#define NULL
Definition: begin_code.h:167
surface
EGLSurface surface
Definition: eglext.h:248
SDL_opengl.h
width
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
SDL_GetWindowID
#define SDL_GetWindowID
Definition: SDL_dynapi_overrides.h:516
save_surface_to_bmp
void save_surface_to_bmp()
Definition: testoffscreen.c:55
SDL_RenderFillRect
#define SDL_RenderFillRect
Definition: SDL_dynapi_overrides.h:341
SDL_WINDOWPOS_UNDEFINED
#define SDL_WINDOWPOS_UNDEFINED
Definition: SDL_video.h:129
loop
void loop()
Definition: testoffscreen.c:77
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:203
SDL_CreateWindow
#define SDL_CreateWindow
Definition: SDL_dynapi_overrides.h:514
SDL_Rect::x
int x
Definition: SDL_rect.h:79
SDL_Rect::w
int w
Definition: SDL_rect.h:80
SDL_Window
The type used to identify a window.
Definition: SDL_sysvideo.h:75
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2652
SDL_Renderer
Definition: SDL_sysrender.h:110
main
int main(int argc, char *argv[])
Definition: testoffscreen.c:101
window
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
SDL_Rect::y
int y
Definition: SDL_rect.h:79
SDL_Rect::h
int h
Definition: SDL_rect.h:80
SDL_LOG_CATEGORY_APPLICATION
@ SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
height
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572
SDL_QUIT
@ SDL_QUIT
Definition: SDL_events.h:60
SDL_FreeSurface
#define SDL_FreeSurface
Definition: SDL_dynapi_overrides.h:446
SDL_PixelFormatEnumToMasks
#define SDL_PixelFormatEnumToMasks
Definition: SDL_dynapi_overrides.h:278
SDL_VideoInit
#define SDL_VideoInit
Definition: SDL_dynapi_overrides.h:498
SDL_GetTicks
Uint32 SDL_GetTicks(void)
Get the number of milliseconds since the SDL library initialization.
SDL_Quit
#define SDL_Quit
Definition: SDL_dynapi_overrides.h:58
SDL_TRUE
@ SDL_TRUE
Definition: SDL_stdinc.h:164
done
static int done
Definition: testoffscreen.c:29
draw
void draw()
Definition: testoffscreen.c:36
SDL_CreateRGBSurface
#define SDL_CreateRGBSurface
Definition: SDL_dynapi_overrides.h:444
SDL_LOG_PRIORITY_INFO
@ SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
frames
static Uint32 frames
Definition: testsprite2.c:40
SDL_LogSetPriority
#define SDL_LogSetPriority
Definition: SDL_dynapi_overrides.h:236
SDL_GetWindowPixelFormat
#define SDL_GetWindowPixelFormat
Definition: SDL_dynapi_overrides.h:513
max_frames
static int max_frames
Definition: testoffscreen.c:33
SDL_snprintf
#define SDL_snprintf
Definition: SDL_dynapi_overrides.h:40
SDL_Rect
A rectangle, with the origin at the upper left (integer).
Definition: SDL_rect.h:78
SDL_stdinc.h
renderer
static SDL_Renderer * renderer
Definition: testoffscreen.c:27
SDL_RenderClear
#define SDL_RenderClear
Definition: SDL_dynapi_overrides.h:334
SDL_SetRenderDrawColor
#define SDL_SetRenderDrawColor
Definition: SDL_dynapi_overrides.h:330
frame_number
static int frame_number
Definition: testoffscreen.c:30
SDL_Event
General event structure.
Definition: SDL_events.h:559
SDL_FALSE
@ SDL_FALSE
Definition: SDL_stdinc.h:163
SDL_CreateRenderer
#define SDL_CreateRenderer
Definition: SDL_dynapi_overrides.h:301
SDL_DestroyRenderer
#define SDL_DestroyRenderer
Definition: SDL_dynapi_overrides.h:348
SDL_SaveBMP
#define SDL_SaveBMP(surface, file)
Definition: SDL_surface.h:224
SDL_RenderReadPixels
#define SDL_RenderReadPixels
Definition: SDL_dynapi_overrides.h:345
SDL_DestroyWindow
#define SDL_DestroyWindow
Definition: SDL_dynapi_overrides.h:549