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

Go to the source code of this file.

Functions

static SDL_Cursorinit_color_cursor (const char *file)
 
static SDL_Cursorinit_system_cursor (const char *image[])
 
static void quit (int rc)
 
void loop ()
 
int main (int argc, char *argv[])
 

Variables

static const char * arrow []
 
static SDLTest_CommonStatestate
 
int done
 
static SDL_Cursorcursors [1+SDL_NUM_SYSTEM_CURSORS]
 
static int current_cursor
 
static int show_cursor
 

Function Documentation

◆ init_color_cursor()

static SDL_Cursor* init_color_cursor ( const char *  file)
static

Definition at line 71 of file testcustomcursor.c.

72 {
75  if (surface) {
76  if (surface->format->palette) {
77  SDL_SetColorKey(surface, 1, *(Uint8 *) surface->pixels);
78  } else {
79  switch (surface->format->BitsPerPixel) {
80  case 15:
81  SDL_SetColorKey(surface, 1, (*(Uint16 *)surface->pixels) & 0x00007FFF);
82  break;
83  case 16:
84  SDL_SetColorKey(surface, 1, *(Uint16 *)surface->pixels);
85  break;
86  case 24:
87  SDL_SetColorKey(surface, 1, (*(Uint32 *)surface->pixels) & 0x00FFFFFF);
88  break;
89  case 32:
90  SDL_SetColorKey(surface, 1, *(Uint32 *)surface->pixels);
91  break;
92  }
93  }
96  }
97  return cursor;
98 }

References cursor, NULL, SDL_CreateColorCursor, SDL_FreeSurface, SDL_LoadBMP, and SDL_SetColorKey.

Referenced by main().

◆ init_system_cursor()

static SDL_Cursor* init_system_cursor ( const char *  image[])
static

Definition at line 101 of file testcustomcursor.c.

102 {
103  int i, row, col;
104  Uint8 data[4*32];
105  Uint8 mask[4*32];
106  int hot_x, hot_y;
107 
108  i = -1;
109  for (row=0; row<32; ++row) {
110  for (col=0; col<32; ++col) {
111  if (col % 8) {
112  data[i] <<= 1;
113  mask[i] <<= 1;
114  } else {
115  ++i;
116  data[i] = mask[i] = 0;
117  }
118  switch (image[4+row][col]) {
119  case 'X':
120  data[i] |= 0x01;
121  mask[i] |= 0x01;
122  break;
123  case '.':
124  mask[i] |= 0x01;
125  break;
126  case ' ':
127  break;
128  }
129  }
130  }
131  sscanf(image[4+row], "%d,%d", &hot_x, &hot_y);
132  return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y);
133 }

References hot_x, i, and SDL_CreateCursor.

Referenced by main().

◆ loop()

void loop ( )

Definition at line 150 of file testcustomcursor.c.

151 {
152  int i;
154  /* Check for events */
155  while (SDL_PollEvent(&event)) {
157  if (event.type == SDL_MOUSEBUTTONDOWN) {
158  if (event.button.button == SDL_BUTTON_LEFT) {
159  ++current_cursor;
161  current_cursor = 0;
162  }
164  } else {
167  }
168  }
169  }
170 
171  for (i = 0; i < state->num_windows; ++i) {
175  }
176 #ifdef __EMSCRIPTEN__
177  if (done) {
178  emscripten_cancel_main_loop();
179  }
180 #endif
181 }

References current_cursor, cursors, done, i, SDLTest_CommonState::num_windows, renderer, SDLTest_CommonState::renderers, SDL_arraysize, SDL_BUTTON_LEFT, SDL_MOUSEBUTTONDOWN, SDL_PollEvent, SDL_RenderClear, SDL_RenderPresent, SDL_SetCursor, SDL_ShowCursor, SDLTest_CommonEvent(), show_cursor, and state.

Referenced by main().

◆ main()

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

Definition at line 184 of file testcustomcursor.c.

185 {
186  int i;
187  const char *color_cursor = NULL;
188 
189  /* Enable standard application logging */
191 
192  /* Initialize test framework */
194  if (!state) {
195  return 1;
196  }
197  for (i = 1; i < argc;) {
198  int consumed;
199 
200  consumed = SDLTest_CommonArg(state, i);
201  if (consumed == 0) {
202  color_cursor = argv[i];
203  break;
204  }
205  if (consumed < 0) {
206  SDLTest_CommonLogUsage(state, argv[0], NULL);
207  quit(1);
208  }
209  i += consumed;
210  }
211 
212  if (!SDLTest_CommonInit(state)) {
213  quit(2);
214  }
215 
216  for (i = 0; i < state->num_windows; ++i) {
218  SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
220  }
221 
222  if (color_cursor) {
223  cursors[0] = init_color_cursor(color_cursor);
224  } else {
226  }
227  if (!cursors[0]) {
228  SDL_Log("Error, couldn't create cursor\n");
229  quit(2);
230  }
231  for (i = 0; i < SDL_NUM_SYSTEM_CURSORS; ++i) {
233  if (!cursors[1+i]) {
234  SDL_Log("Error, couldn't create system cursor %d\n", i);
235  quit(2);
236  }
237  }
239 
240  /* Main render loop */
241  done = 0;
242 #ifdef __EMSCRIPTEN__
243  emscripten_set_main_loop(loop, 0, 1);
244 #else
245  while (!done) {
246  loop();
247  }
248 #endif
249 
250  for (i = 0; i < SDL_arraysize(cursors); ++i) {
252  }
253  quit(0);
254 
255  /* keep the compiler happy ... */
256  return(0);
257 }

References arrow, cursors, done, i, init_color_cursor(), init_system_cursor(), loop(), NULL, SDLTest_CommonState::num_windows, quit(), renderer, SDLTest_CommonState::renderers, SDL_arraysize, SDL_CreateSystemCursor, SDL_FreeCursor, SDL_INIT_VIDEO, SDL_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, SDL_LogSetPriority, SDL_NUM_SYSTEM_CURSORS, SDL_RenderClear, SDL_SetCursor, SDL_SetRenderDrawColor, SDLTest_CommonArg(), SDLTest_CommonCreateState(), SDLTest_CommonInit(), SDLTest_CommonLogUsage(), and state.

◆ quit()

static void quit ( int  rc)
static

Definition at line 143 of file testcustomcursor.c.

144 {
146  exit(rc);
147 }

References SDLTest_CommonQuit(), and state.

Referenced by main().

Variable Documentation

◆ arrow

const char* arrow[]
static

Definition at line 27 of file testcustomcursor.c.

Referenced by main().

◆ current_cursor

int current_cursor
static

Definition at line 138 of file testcustomcursor.c.

Referenced by loop().

◆ cursors

SDL_Cursor* cursors[1+SDL_NUM_SYSTEM_CURSORS]
static

Definition at line 137 of file testcustomcursor.c.

Referenced by loop(), and main().

◆ done

int done

Definition at line 136 of file testcustomcursor.c.

Referenced by loop(), and main().

◆ show_cursor

int show_cursor
static

Definition at line 139 of file testcustomcursor.c.

Referenced by loop().

◆ state

SDLTest_CommonState* state
static

Definition at line 135 of file testcustomcursor.c.

Referenced by loop(), main(), and quit().

Uint8
uint8_t Uint8
Definition: SDL_stdinc.h:179
image
GLeglImageOES image
Definition: SDL_opengl.h:2148
SDL_RenderPresent
#define SDL_RenderPresent
Definition: SDL_dynapi_overrides.h:346
SDL_NUM_SYSTEM_CURSORS
@ SDL_NUM_SYSTEM_CURSORS
Definition: SDL_mouse.h:60
mask
GLenum GLint GLuint mask
Definition: SDL_opengl_glext.h:660
Uint16
uint16_t Uint16
Definition: SDL_stdinc.h:191
SDL_Surface
A collection of pixels used in software blitting.
Definition: SDL_surface.h:71
SDL_Cursor
Definition: SDL_mouse_c.h:31
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
state
static SDLTest_CommonState * state
Definition: testcustomcursor.c:135
done
int done
Definition: testcustomcursor.c:136
cursors
static SDL_Cursor * cursors[1+SDL_NUM_SYSTEM_CURSORS]
Definition: testcustomcursor.c:137
SDL_CreateSystemCursor
#define SDL_CreateSystemCursor
Definition: SDL_dynapi_overrides.h:253
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:203
quit
static void quit(int rc)
Definition: testcustomcursor.c:143
SDL_SetCursor
#define SDL_SetCursor
Definition: SDL_dynapi_overrides.h:254
SDLTest_CommonState::renderers
SDL_Renderer ** renderers
Definition: SDL_test_common.h:84
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
SDLTest_CommonCreateState
SDLTest_CommonState * SDLTest_CommonCreateState(char **argv, Uint32 flags)
Parse command line parameters and create common state.
Definition: SDL_test_common.c:59
row
GLenum GLenum void * row
Definition: SDL_opengl_glext.h:3141
init_color_cursor
static SDL_Cursor * init_color_cursor(const char *file)
Definition: testcustomcursor.c:71
SDLTest_CommonQuit
void SDLTest_CommonQuit(SDLTest_CommonState *state)
Close test window.
Definition: SDL_test_common.c:1910
SDLTest_CommonState::num_windows
int num_windows
Definition: SDL_test_common.h:77
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2652
SDL_Renderer
Definition: SDL_sysrender.h:110
current_cursor
static int current_cursor
Definition: testcustomcursor.c:138
SDL_BUTTON_LEFT
#define SDL_BUTTON_LEFT
Definition: SDL_mouse.h:282
init_system_cursor
static SDL_Cursor * init_system_cursor(const char *image[])
Definition: testcustomcursor.c:101
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
SDL_LOG_CATEGORY_APPLICATION
@ SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
loop
void loop()
Definition: testcustomcursor.c:150
SDLTest_CommonArg
int SDLTest_CommonArg(SDLTest_CommonState *state, int index)
Process one common argument.
Definition: SDL_test_common.c:117
SDL_FreeSurface
#define SDL_FreeSurface
Definition: SDL_dynapi_overrides.h:446
SDL_SetColorKey
#define SDL_SetColorKey
Definition: SDL_dynapi_overrides.h:453
SDL_SystemCursor
SDL_SystemCursor
Cursor types for SDL_CreateSystemCursor().
Definition: SDL_mouse.h:47
SDLTest_CommonLogUsage
void SDLTest_CommonLogUsage(SDLTest_CommonState *state, const char *argv0, const char **options)
Logs command line usage info.
Definition: SDL_test_common.c:498
arrow
static const char * arrow[]
Definition: testcustomcursor.c:27
SDL_LoadBMP
#define SDL_LoadBMP(file)
Definition: SDL_surface.h:201
hot_x
int uint32_t uint32_t uint32_t uint32_t uint32_t int drmModeModeInfoPtr mode int uint32_t uint32_t uint32_t uint32_t int32_t hot_x
Definition: SDL_kmsdrmsym.h:57
cursor
SDL_Cursor * cursor
Definition: testwm2.c:40
SDL_CreateColorCursor
#define SDL_CreateColorCursor
Definition: SDL_dynapi_overrides.h:252
SDL_arraysize
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:115
SDLTest_CommonInit
SDL_bool SDLTest_CommonInit(SDLTest_CommonState *state)
Open test window.
Definition: SDL_test_common.c:835
SDL_CreateCursor
#define SDL_CreateCursor
Definition: SDL_dynapi_overrides.h:251
SDL_INIT_VIDEO
#define SDL_INIT_VIDEO
Definition: SDL.h:80
SDL_LOG_PRIORITY_INFO
@ SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
renderer
static SDL_Renderer * renderer
Definition: testaudiocapture.c:21
SDL_LogSetPriority
#define SDL_LogSetPriority
Definition: SDL_dynapi_overrides.h:236
SDL_RenderClear
#define SDL_RenderClear
Definition: SDL_dynapi_overrides.h:334
SDL_SetRenderDrawColor
#define SDL_SetRenderDrawColor
Definition: SDL_dynapi_overrides.h:330
SDL_MOUSEBUTTONDOWN
@ SDL_MOUSEBUTTONDOWN
Definition: SDL_events.h:106
SDL_ShowCursor
#define SDL_ShowCursor
Definition: SDL_dynapi_overrides.h:258
SDL_Event
General event structure.
Definition: SDL_events.h:559
SDLTest_CommonEvent
void SDLTest_CommonEvent(SDLTest_CommonState *state, SDL_Event *event, int *done)
Common event handler for test windows.
Definition: SDL_test_common.c:1579
SDL_FreeCursor
#define SDL_FreeCursor
Definition: SDL_dynapi_overrides.h:257
show_cursor
static int show_cursor
Definition: testcustomcursor.c:139
i
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