SDL  2.0
testcustomcursor.c
Go to the documentation of this file.
1 /*
2  Copyright (C) 1997-2018 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 #include <stdlib.h>
14 #include <stdio.h>
15 
16 #ifdef __EMSCRIPTEN__
17 #include <emscripten/emscripten.h>
18 #endif
19 
20 #include "SDL_test_common.h"
21 
22 /* Stolen from the mailing list */
23 /* Creates a new mouse cursor from an XPM */
24 
25 
26 /* XPM */
27 static const char *arrow[] = {
28  /* width height num_colors chars_per_pixel */
29  " 32 32 3 1",
30  /* colors */
31  "X c #000000",
32  ". c #ffffff",
33  " c None",
34  /* pixels */
35  "X ",
36  "XX ",
37  "X.X ",
38  "X..X ",
39  "X...X ",
40  "X....X ",
41  "X.....X ",
42  "X......X ",
43  "X.......X ",
44  "X........X ",
45  "X.....XXXXX ",
46  "X..X..X ",
47  "X.X X..X ",
48  "XX X..X ",
49  "X X..X ",
50  " X..X ",
51  " X..X ",
52  " X..X ",
53  " XX ",
54  " ",
55  " ",
56  " ",
57  " ",
58  " ",
59  " ",
60  " ",
61  " ",
62  " ",
63  " ",
64  " ",
65  " ",
66  " ",
67  "0,0"
68 };
69 
70 static SDL_Cursor*
71 init_color_cursor(const char *file)
72 {
75  if (surface) {
76  cursor = SDL_CreateColorCursor(surface, 0, 0);
77  SDL_FreeSurface(surface);
78  }
79  return cursor;
80 }
81 
82 static SDL_Cursor*
83 init_system_cursor(const char *image[])
84 {
85  int i, row, col;
86  Uint8 data[4*32];
87  Uint8 mask[4*32];
88  int hot_x, hot_y;
89 
90  i = -1;
91  for (row=0; row<32; ++row) {
92  for (col=0; col<32; ++col) {
93  if (col % 8) {
94  data[i] <<= 1;
95  mask[i] <<= 1;
96  } else {
97  ++i;
98  data[i] = mask[i] = 0;
99  }
100  switch (image[4+row][col]) {
101  case 'X':
102  data[i] |= 0x01;
103  mask[i] |= 0x01;
104  break;
105  case '.':
106  mask[i] |= 0x01;
107  break;
108  case ' ':
109  break;
110  }
111  }
112  }
113  sscanf(image[4+row], "%d,%d", &hot_x, &hot_y);
114  return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y);
115 }
116 
118 int done;
120 
121 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
122 static void
123 quit(int rc)
124 {
125  SDLTest_CommonQuit(state);
126  exit(rc);
127 }
128 
129 void
131 {
132  int i;
134  /* Check for events */
135  while (SDL_PollEvent(&event)) {
136  SDLTest_CommonEvent(state, &event, &done);
137  }
138 
139  for (i = 0; i < state->num_windows; ++i) {
140  SDL_Renderer *renderer = state->renderers[i];
141  SDL_RenderClear(renderer);
142  SDL_RenderPresent(renderer);
143  }
144 #ifdef __EMSCRIPTEN__
145  if (done) {
146  emscripten_cancel_main_loop();
147  }
148 #endif
149 }
150 
151 int
152 main(int argc, char *argv[])
153 {
154  int i;
155  const char *color_cursor = NULL;
156 
157  /* Enable standard application logging */
159 
160  /* Initialize test framework */
162  if (!state) {
163  return 1;
164  }
165  for (i = 1; i < argc;) {
166  int consumed;
167 
168  consumed = SDLTest_CommonArg(state, i);
169  if (consumed == 0) {
170  color_cursor = argv[i];
171  break;
172  }
173  if (consumed < 0) {
174  SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
175  quit(1);
176  }
177  i += consumed;
178  }
179 
180  if (!SDLTest_CommonInit(state)) {
181  quit(2);
182  }
183 
184  for (i = 0; i < state->num_windows; ++i) {
185  SDL_Renderer *renderer = state->renderers[i];
186  SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
187  SDL_RenderClear(renderer);
188  }
189 
190  if (color_cursor) {
191  cursor = init_color_cursor(color_cursor);
192  } else {
193  cursor = init_system_cursor(arrow);
194  }
195  if (!cursor) {
196  SDL_Log("Error, couldn't create cursor\n");
197  quit(2);
198  }
199  SDL_SetCursor(cursor);
200 
201  /* Main render loop */
202  done = 0;
203 #ifdef __EMSCRIPTEN__
204  emscripten_set_main_loop(loop, 0, 1);
205 #else
206  while (!done) {
207  loop();
208  }
209 #endif
210 
211  SDL_FreeCursor(cursor);
212  quit(0);
213 
214  /* keep the compiler happy ... */
215  return(0);
216 }
static void quit(int rc)
#define SDL_PollEvent
GLeglImageOES image
Definition: SDL_opengl.h:2148
#define SDL_LoadBMP(file)
Definition: SDL_surface.h:200
SDLTest_CommonState * SDLTest_CommonCreateState(char **argv, Uint32 flags)
Parse command line parameters and create common state.
void loop()
EGLSurface surface
Definition: eglext.h:248
int SDLTest_CommonArg(SDLTest_CommonState *state, int index)
Process one common argument.
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
#define SDL_SetCursor
SDL_bool SDLTest_CommonInit(SDLTest_CommonState *state)
Open test window.
static const char * arrow[]
#define SDL_Log
int main(int argc, char *argv[])
void SDLTest_CommonEvent(SDLTest_CommonState *state, SDL_Event *event, int *done)
Common event handler for test windows.
#define SDL_FreeSurface
static SDL_Renderer * renderer
uint8_t Uint8
Definition: SDL_stdinc.h:157
struct _cl_event * event
static SDLTest_CommonState * state
GLenum GLint GLuint mask
static SDL_Cursor * init_color_cursor(const char *file)
#define SDL_CreateColorCursor
const char * SDLTest_CommonUsage(SDLTest_CommonState *state)
Returns common usage information.
SDL_Renderer ** renderers
SDL_Cursor * cursor
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_Cursor * init_system_cursor(const char *image[])
#define SDL_CreateCursor
#define SDL_RenderClear
int done
General event structure.
Definition: SDL_events.h:525
#define SDL_SetRenderDrawColor
#define SDL_FreeCursor
GLenum GLenum void * row
void SDLTest_CommonQuit(SDLTest_CommonState *state)
Close test window.
#define SDL_INIT_VIDEO
Definition: SDL.h:78
#define SDL_RenderPresent
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:55