SDL  2.0
testdraw2.c File Reference
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "SDL_test_common.h"
+ Include dependency graph for testdraw2.c:

Go to the source code of this file.

Macros

#define NUM_OBJECTS   100
 

Functions

void DrawPoints (SDL_Renderer *renderer)
 
void DrawLines (SDL_Renderer *renderer)
 
void DrawRects (SDL_Renderer *renderer)
 
void loop ()
 
int main (int argc, char *argv[])
 

Variables

static SDLTest_CommonStatestate
 
static int num_objects
 
static SDL_bool cycle_color
 
static SDL_bool cycle_alpha
 
static int cycle_direction = 1
 
static int current_alpha = 255
 
static int current_color = 255
 
static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE
 
int done
 

Macro Definition Documentation

◆ NUM_OBJECTS

#define NUM_OBJECTS   100

Definition at line 25 of file testdraw2.c.

Referenced by main().

Function Documentation

◆ DrawLines()

void DrawLines ( SDL_Renderer renderer)

Definition at line 82 of file testdraw2.c.

References current_alpha, current_color, cycle_alpha, cycle_color, cycle_direction, SDL_Rect::h, i, num_objects, SDL_RenderDrawLine, SDL_RenderGetViewport, SDL_SetRenderDrawColor, viewport, and SDL_Rect::w.

Referenced by loop().

83 {
84  int i;
85  int x1, y1, x2, y2;
87 
88  /* Query the sizes */
89  SDL_RenderGetViewport(renderer, &viewport);
90 
91  for (i = 0; i < num_objects; ++i) {
92  /* Cycle the color and alpha, if desired */
93  if (cycle_color) {
95  if (current_color < 0) {
96  current_color = 0;
98  }
99  if (current_color > 255) {
100  current_color = 255;
102  }
103  }
104  if (cycle_alpha) {
106  if (current_alpha < 0) {
107  current_alpha = 0;
109  }
110  if (current_alpha > 255) {
111  current_alpha = 255;
113  }
114  }
115  SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
116  (Uint8) current_color, (Uint8) current_alpha);
117 
118  if (i == 0) {
119  SDL_RenderDrawLine(renderer, 0, 0, viewport.w - 1, viewport.h - 1);
120  SDL_RenderDrawLine(renderer, 0, viewport.h - 1, viewport.w - 1, 0);
121  SDL_RenderDrawLine(renderer, 0, viewport.h / 2, viewport.w - 1, viewport.h / 2);
122  SDL_RenderDrawLine(renderer, viewport.w / 2, 0, viewport.w / 2, viewport.h - 1);
123  } else {
124  x1 = (rand() % (viewport.w*2)) - viewport.w;
125  x2 = (rand() % (viewport.w*2)) - viewport.w;
126  y1 = (rand() % (viewport.h*2)) - viewport.h;
127  y2 = (rand() % (viewport.h*2)) - viewport.h;
128  SDL_RenderDrawLine(renderer, x1, y1, x2, y2);
129  }
130  }
131 }
GLuint GLfloat GLfloat GLfloat x1
GLfixed GLfixed GLfixed y2
GLfixed GLfixed x2
static SDL_bool cycle_color
Definition: testdraw2.c:29
GLfixed y1
static int current_alpha
Definition: testdraw2.c:32
static int num_objects
Definition: testdraw2.c:28
static int current_color
Definition: testdraw2.c:33
uint8_t Uint8
Definition: SDL_stdinc.h:157
#define SDL_RenderGetViewport
int w
Definition: SDL_rect.h:67
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
static SDL_bool cycle_alpha
Definition: testdraw2.c:30
int h
Definition: SDL_rect.h:67
SDL_Rect viewport
Definition: testviewport.c:28
#define SDL_SetRenderDrawColor
static int cycle_direction
Definition: testdraw2.c:31
#define SDL_RenderDrawLine
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64

◆ DrawPoints()

void DrawPoints ( SDL_Renderer renderer)

Definition at line 39 of file testdraw2.c.

References current_alpha, current_color, cycle_alpha, cycle_color, cycle_direction, SDL_Rect::h, i, num_objects, SDL_RenderDrawPoint, SDL_RenderGetViewport, SDL_SetRenderDrawColor, viewport, and SDL_Rect::w.

Referenced by loop().

40 {
41  int i;
42  int x, y;
44 
45  /* Query the sizes */
46  SDL_RenderGetViewport(renderer, &viewport);
47 
48  for (i = 0; i < num_objects * 4; ++i) {
49  /* Cycle the color and alpha, if desired */
50  if (cycle_color) {
52  if (current_color < 0) {
53  current_color = 0;
55  }
56  if (current_color > 255) {
57  current_color = 255;
59  }
60  }
61  if (cycle_alpha) {
63  if (current_alpha < 0) {
64  current_alpha = 0;
66  }
67  if (current_alpha > 255) {
68  current_alpha = 255;
70  }
71  }
73  (Uint8) current_color, (Uint8) current_alpha);
74 
75  x = rand() % viewport.w;
76  y = rand() % viewport.h;
77  SDL_RenderDrawPoint(renderer, x, y);
78  }
79 }
#define SDL_RenderDrawPoint
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
static SDL_bool cycle_color
Definition: testdraw2.c:29
static int current_alpha
Definition: testdraw2.c:32
static int num_objects
Definition: testdraw2.c:28
static int current_color
Definition: testdraw2.c:33
uint8_t Uint8
Definition: SDL_stdinc.h:157
#define SDL_RenderGetViewport
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
int w
Definition: SDL_rect.h:67
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
static SDL_bool cycle_alpha
Definition: testdraw2.c:30
int h
Definition: SDL_rect.h:67
SDL_Rect viewport
Definition: testviewport.c:28
#define SDL_SetRenderDrawColor
static int cycle_direction
Definition: testdraw2.c:31
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64

◆ DrawRects()

void DrawRects ( SDL_Renderer renderer)

Definition at line 134 of file testdraw2.c.

References current_alpha, current_color, cycle_alpha, cycle_color, cycle_direction, SDL_Rect::h, i, num_objects, rect, SDL_RenderFillRect, SDL_RenderGetViewport, SDL_SetRenderDrawColor, viewport, SDL_Rect::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by loop().

135 {
136  int i;
137  SDL_Rect rect;
139 
140  /* Query the sizes */
141  SDL_RenderGetViewport(renderer, &viewport);
142 
143  for (i = 0; i < num_objects / 4; ++i) {
144  /* Cycle the color and alpha, if desired */
145  if (cycle_color) {
147  if (current_color < 0) {
148  current_color = 0;
150  }
151  if (current_color > 255) {
152  current_color = 255;
154  }
155  }
156  if (cycle_alpha) {
158  if (current_alpha < 0) {
159  current_alpha = 0;
161  }
162  if (current_alpha > 255) {
163  current_alpha = 255;
165  }
166  }
167  SDL_SetRenderDrawColor(renderer, 255, (Uint8) current_color,
168  (Uint8) current_color, (Uint8) current_alpha);
169 
170  rect.w = rand() % (viewport.h / 2);
171  rect.h = rand() % (viewport.h / 2);
172  rect.x = (rand() % (viewport.w*2) - viewport.w) - (rect.w / 2);
173  rect.y = (rand() % (viewport.h*2) - viewport.h) - (rect.h / 2);
174  SDL_RenderFillRect(renderer, &rect);
175  }
176 }
SDL_Rect rect
Definition: testrelative.c:27
#define SDL_RenderFillRect
static SDL_bool cycle_color
Definition: testdraw2.c:29
static int current_alpha
Definition: testdraw2.c:32
static int num_objects
Definition: testdraw2.c:28
static int current_color
Definition: testdraw2.c:33
uint8_t Uint8
Definition: SDL_stdinc.h:157
#define SDL_RenderGetViewport
int x
Definition: SDL_rect.h:66
int w
Definition: SDL_rect.h:67
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
static SDL_bool cycle_alpha
Definition: testdraw2.c:30
int h
Definition: SDL_rect.h:67
SDL_Rect viewport
Definition: testviewport.c:28
#define SDL_SetRenderDrawColor
static int cycle_direction
Definition: testdraw2.c:31
int y
Definition: SDL_rect.h:66
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64

◆ loop()

void loop ( )

Definition at line 179 of file testdraw2.c.

References done, DrawLines(), DrawPoints(), DrawRects(), i, NULL, SDLTest_CommonState::num_windows, renderer, SDLTest_CommonState::renderers, SDL_PollEvent, SDL_RenderClear, SDL_RenderPresent, SDL_SetRenderDrawColor, SDLTest_CommonEvent(), and SDLTest_CommonState::windows.

Referenced by main().

180 {
181  int i;
183 
184  /* Check for events */
185  while (SDL_PollEvent(&event)) {
186  SDLTest_CommonEvent(state, &event, &done);
187  }
188  for (i = 0; i < state->num_windows; ++i) {
190  if (state->windows[i] == NULL)
191  continue;
192  SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
193  SDL_RenderClear(renderer);
194 
195  DrawRects(renderer);
196  DrawLines(renderer);
197  DrawPoints(renderer);
198 
199  SDL_RenderPresent(renderer);
200  }
201 #ifdef __EMSCRIPTEN__
202  if (done) {
203  emscripten_cancel_main_loop();
204  }
205 #endif
206 }
static SDLTest_CommonState * state
Definition: testdraw2.c:27
#define SDL_PollEvent
SDL_Window ** windows
void SDLTest_CommonEvent(SDLTest_CommonState *state, SDL_Event *event, int *done)
Common event handler for test windows.
static SDL_Renderer * renderer
struct _cl_event * event
void DrawLines(SDL_Renderer *renderer)
Definition: testdraw2.c:82
int done
Definition: testdraw2.c:36
SDL_Renderer ** renderers
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:164
#define SDL_RenderClear
void DrawPoints(SDL_Renderer *renderer)
Definition: testdraw2.c:39
General event structure.
Definition: SDL_events.h:525
#define SDL_SetRenderDrawColor
void DrawRects(SDL_Renderer *renderer)
Definition: testdraw2.c:134
#define SDL_RenderPresent

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 209 of file testdraw2.c.

References blendMode, cycle_alpha, cycle_color, done, i, loop(), NULL, NUM_OBJECTS, num_objects, SDLTest_CommonState::num_windows, renderer, SDLTest_CommonState::renderers, SDL_atoi, SDL_BLENDMODE_ADD, SDL_BLENDMODE_BLEND, SDL_BLENDMODE_MOD, SDL_BLENDMODE_NONE, SDL_GetTicks(), SDL_INIT_VIDEO, SDL_isdigit, SDL_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, SDL_LogSetPriority, SDL_RenderClear, SDL_SetRenderDrawBlendMode, SDL_SetRenderDrawColor, SDL_strcasecmp, SDL_TRUE, SDLTest_CommonArg(), SDLTest_CommonCreateState(), SDLTest_CommonInit(), SDLTest_CommonQuit(), and SDLTest_CommonUsage().

210 {
211  int i;
212  Uint32 then, now, frames;
213 
214  /* Enable standard application logging */
216 
217  /* Initialize parameters */
219 
220  /* Initialize test framework */
222  if (!state) {
223  return 1;
224  }
225  for (i = 1; i < argc;) {
226  int consumed;
227 
228  consumed = SDLTest_CommonArg(state, i);
229  if (consumed == 0) {
230  consumed = -1;
231  if (SDL_strcasecmp(argv[i], "--blend") == 0) {
232  if (argv[i + 1]) {
233  if (SDL_strcasecmp(argv[i + 1], "none") == 0) {
235  consumed = 2;
236  } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) {
238  consumed = 2;
239  } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) {
241  consumed = 2;
242  } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) {
244  consumed = 2;
245  }
246  }
247  } else if (SDL_strcasecmp(argv[i], "--cyclecolor") == 0) {
249  consumed = 1;
250  } else if (SDL_strcasecmp(argv[i], "--cyclealpha") == 0) {
252  consumed = 1;
253  } else if (SDL_isdigit(*argv[i])) {
254  num_objects = SDL_atoi(argv[i]);
255  consumed = 1;
256  }
257  }
258  if (consumed < 0) {
259  SDL_Log("Usage: %s %s [--blend none|blend|add|mod] [--cyclecolor] [--cyclealpha]\n",
260  argv[0], SDLTest_CommonUsage(state));
261  return 1;
262  }
263  i += consumed;
264  }
265  if (!SDLTest_CommonInit(state)) {
266  return 2;
267  }
268 
269  /* Create the windows and initialize the renderers */
270  for (i = 0; i < state->num_windows; ++i) {
273  SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
274  SDL_RenderClear(renderer);
275  }
276 
277  srand((unsigned int)time(NULL));
278 
279  /* Main render loop */
280  frames = 0;
281  then = SDL_GetTicks();
282  done = 0;
283 
284 #ifdef __EMSCRIPTEN__
285  emscripten_set_main_loop(loop, 0, 1);
286 #else
287  while (!done) {
288  ++frames;
289  loop();
290  }
291 #endif
292 
293 
295 
296  /* Print out some timing information */
297  now = SDL_GetTicks();
298  if (now > then) {
299  double fps = ((double) frames * 1000) / (now - then);
300  SDL_Log("%2.2f frames per second\n", fps);
301  }
302  return 0;
303 }
static SDLTest_CommonState * state
Definition: testdraw2.c:27
#define NUM_OBJECTS
Definition: testdraw2.c:25
EGLSurface EGLnsecsANDROID time
Definition: eglext.h:518
SDLTest_CommonState * SDLTest_CommonCreateState(char **argv, Uint32 flags)
Parse command line parameters and create common state.
int SDLTest_CommonArg(SDLTest_CommonState *state, int index)
Process one common argument.
#define SDL_SetRenderDrawBlendMode
uint32_t Uint32
Definition: SDL_stdinc.h:181
#define SDL_strcasecmp
void loop()
Definition: testdraw2.c:179
SDL_bool SDLTest_CommonInit(SDLTest_CommonState *state)
Open test window.
static SDL_bool cycle_color
Definition: testdraw2.c:29
static SDL_BlendMode blendMode
Definition: testdraw2.c:34
#define SDL_Log
static int num_objects
Definition: testdraw2.c:28
#define SDL_isdigit
Uint32 SDL_GetTicks(void)
Get the number of milliseconds since the SDL library initialization.
static SDL_Renderer * renderer
int done
Definition: testdraw2.c:36
const char * SDLTest_CommonUsage(SDLTest_CommonState *state)
Returns common usage information.
#define SDL_atoi
SDL_Renderer ** renderers
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define SDL_LogSetPriority
#define NULL
Definition: begin_code.h:164
static SDL_bool cycle_alpha
Definition: testdraw2.c:30
#define SDL_RenderClear
#define SDL_SetRenderDrawColor
void SDLTest_CommonQuit(SDLTest_CommonState *state)
Close test window.
#define SDL_INIT_VIDEO
Definition: SDL.h:78

Variable Documentation

◆ blendMode

◆ current_alpha

int current_alpha = 255
static

Definition at line 32 of file testdraw2.c.

Referenced by DrawLines(), DrawPoints(), and DrawRects().

◆ current_color

int current_color = 255
static

Definition at line 33 of file testdraw2.c.

Referenced by DrawLines(), DrawPoints(), and DrawRects().

◆ cycle_alpha

SDL_bool cycle_alpha
static

Definition at line 30 of file testdraw2.c.

Referenced by DrawLines(), DrawPoints(), DrawRects(), and main().

◆ cycle_color

SDL_bool cycle_color
static

Definition at line 29 of file testdraw2.c.

Referenced by DrawLines(), DrawPoints(), DrawRects(), and main().

◆ cycle_direction

int cycle_direction = 1
static

Definition at line 31 of file testdraw2.c.

Referenced by DrawLines(), DrawPoints(), and DrawRects().

◆ done

int done

Definition at line 36 of file testdraw2.c.

Referenced by loop(), and main().

◆ num_objects

int num_objects
static

Definition at line 28 of file testdraw2.c.

Referenced by DrawLines(), DrawPoints(), DrawRects(), and main().

◆ state

SDLTest_CommonState* state
static

Definition at line 27 of file testdraw2.c.