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;
60  char file[128];
61  int bbp;
62 
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: */
#define SDL_RenderPresent
#define SDL_DestroyWindow
#define SDL_GetError
#define SDL_PollEvent
#define SDL_RenderFillRect
#define SDL_DestroyRenderer
#define SDL_SetRenderDrawColor
#define SDL_VideoInit
#define SDL_CreateWindow
#define SDL_GetWindowPixelFormat
#define SDL_GetWindowID
#define SDL_CreateRenderer
#define SDL_LogSetPriority
#define SDL_RenderClear
#define SDL_PixelFormatEnumToMasks
#define SDL_CreateRGBSurface
#define SDL_Quit
#define SDL_FreeSurface
#define SDL_Log
#define SDL_snprintf
#define SDL_RenderReadPixels
@ SDL_QUIT
Definition: SDL_events.h:60
int uint32_t uint32_t uint32_t pixel_format
Definition: SDL_kmsdrmsym.h:52
@ SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
@ SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
GLint GLint GLsizei width
Definition: SDL_opengl.h:1572
GLint GLint GLsizei GLsizei height
Definition: SDL_opengl.h:1572
struct _cl_event * event
@ SDL_TRUE
Definition: SDL_stdinc.h:170
@ SDL_FALSE
Definition: SDL_stdinc.h:169
uint32_t Uint32
Definition: SDL_stdinc.h:209
#define SDL_SaveBMP(surface, file)
Definition: SDL_surface.h:226
Uint32 SDL_GetTicks(void)
Get the number of milliseconds since the SDL library initialization.
#define SDL_WINDOWPOS_UNDEFINED
Definition: SDL_video.h:130
#define NULL
Definition: begin_code.h:163
EGLSurface surface
Definition: eglext.h:248
EGLSurface EGLnsecsANDROID time
Definition: eglext.h:518
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
A rectangle, with the origin at the upper left (integer).
Definition: SDL_rect.h:78
int h
Definition: SDL_rect.h:80
int w
Definition: SDL_rect.h:80
int y
Definition: SDL_rect.h:79
int x
Definition: SDL_rect.h:79
A collection of pixels used in software blitting.
Definition: SDL_surface.h:71
The type used to identify a window.
Definition: SDL_sysvideo.h:75
static int frame_number
Definition: testoffscreen.c:30
int main(int argc, char *argv[])
void draw()
Definition: testoffscreen.c:36
static int done
Definition: testoffscreen.c:29
void save_surface_to_bmp()
Definition: testoffscreen.c:55
static SDL_Renderer * renderer
Definition: testoffscreen.c:27
static int max_frames
Definition: testoffscreen.c:33
void loop()
Definition: testoffscreen.c:77
static Uint32 frames
Definition: testsprite2.c:40
General event structure.
Definition: SDL_events.h:592