• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <stdio.h>
2 #include <stdlib.h>
3 
4 #include "SDL.h"
5 
6 /* This is an example 16x16 cursor
7 	top left :	black
8 	top right : inverted color or black
9 	bottom left: white
10 	bottom right: transparent
11 	(swap left and right for different endianness)
12 */
13 
14 Uint16 cursor_data[16]={
15 	0xffff,
16 	0xffff,
17 	0xffff,
18 	0xffff,
19 
20 	0xffff,
21 	0xffff,
22 	0xffff,
23 	0xffff,
24 
25 	0x0000,
26 	0x0000,
27 	0x0000,
28 	0x0000,
29 
30 	0x0000,
31 	0x0000,
32 	0x0000,
33 	0x0000
34 };
35 
36 Uint16 cursor_mask[16]={
37 	0xff00,
38 	0xff00,
39 	0xff00,
40 	0xff00,
41 
42 	0xff00,
43 	0xff00,
44 	0xff00,
45 	0xff00,
46 
47 	0xff00,
48 	0xff00,
49 	0xff00,
50 	0xff00,
51 
52 	0xff00,
53 	0xff00,
54 	0xff00,
55 	0xff00
56 };
57 
58 /* another test cursor: smaller than 16x16, and with an odd height */
59 
60 Uint8 small_cursor_data[11] = { 0x00, 0x18, 0x08, 0x38, 0x44, 0x54, 0x44, 0x38, 0x20, 0x20, 0x00 };
61 Uint8 small_cursor_mask[11] = { 0x3C, 0x3C, 0x3C, 0x7C, 0xC6, 0xC6, 0xC6, 0x7C, 0x78, 0x70, 0x70 };
62 
63 /* XPM */
64 static const char *arrow[] = {
65   /* width height num_colors chars_per_pixel */
66   "    32    32        3            1",
67   /* colors */
68   "X c #000000",
69   ". c #ffffff",
70   "  c None",
71   /* pixels */
72   "X                               ",
73   "XX                              ",
74   "X.X                             ",
75   "X..X                            ",
76   "X...X                           ",
77   "X....X                          ",
78   "X.....X                         ",
79   "X......X                        ",
80   "X.......X                       ",
81   "X........X                      ",
82   "X.....XXXXX                     ",
83   "X..X..X                         ",
84   "X.X X..X                        ",
85   "XX  X..X                        ",
86   "X    X..X                       ",
87   "     X..X                       ",
88   "      X..X                      ",
89   "      X..X                      ",
90   "       XX                       ",
91   "                                ",
92   "                                ",
93   "                                ",
94   "                                ",
95   "                                ",
96   "                                ",
97   "                                ",
98   "                                ",
99   "                                ",
100   "                                ",
101   "                                ",
102   "                                ",
103   "                                ",
104   "0,0"
105 };
106 
create_arrow_cursor()107 static SDL_Cursor *create_arrow_cursor()
108 {
109   int i, row, col;
110   Uint8 data[4*32];
111   Uint8 mask[4*32];
112   int hot_x, hot_y;
113 
114   i = -1;
115   for ( row=0; row<32; ++row ) {
116     for ( col=0; col<32; ++col ) {
117       if ( col % 8 ) {
118         data[i] <<= 1;
119         mask[i] <<= 1;
120       } else {
121         ++i;
122         data[i] = mask[i] = 0;
123       }
124       switch (arrow[4+row][col]) {
125         case 'X':
126           data[i] |= 0x01;
127           mask[i] |= 0x01;
128           break;
129         case '.':
130           mask[i] |= 0x01;
131           break;
132         case ' ':
133           break;
134       }
135     }
136   }
137   sscanf(arrow[4+row], "%d,%d", &hot_x, &hot_y);
138   return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y);
139 }
140 
141 
main(int argc,char * argv[])142 int main(int argc, char *argv[])
143 {
144 	SDL_Surface *screen;
145 	SDL_bool quit = SDL_FALSE, first_time = SDL_TRUE;
146 	SDL_Cursor *cursor[3];
147 	int current;
148 
149 	/* Load the SDL library */
150 	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
151 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
152 		return(1);
153 	}
154 
155 	screen = SDL_SetVideoMode(320,200,8,SDL_ANYFORMAT);
156 	if (screen==NULL) {
157 		fprintf(stderr, "Couldn't initialize video mode: %s\n",SDL_GetError());
158 		return(1);
159 	}
160 
161 	SDL_FillRect(screen, NULL, 0x664422);
162 
163 	cursor[0] = SDL_CreateCursor((Uint8 *)cursor_data, (Uint8 *)cursor_mask,
164 		16, 16, 8, 8);
165 	if (cursor[0]==NULL) {
166 		fprintf(stderr, "Couldn't initialize test cursor: %s\n",SDL_GetError());
167 		SDL_Quit();
168 		return(1);
169 	}
170 	cursor[1] = create_arrow_cursor();
171 	if (cursor[1]==NULL) {
172 		fprintf(stderr, "Couldn't initialize arrow cursor: %s\n",SDL_GetError());
173 		SDL_FreeCursor(cursor[0]);
174 		SDL_Quit();
175 		return(1);
176 	}
177 	cursor[2] = SDL_CreateCursor(small_cursor_data, small_cursor_mask,
178 		8, 11, 3, 5);
179 	if (cursor[2]==NULL) {
180 		fprintf(stderr, "Couldn't initialize test cursor: %s\n",SDL_GetError());
181 		SDL_Quit();
182 		return(1);
183 	}
184 
185 	current = 0;
186 	SDL_SetCursor(cursor[current]);
187 
188 	while (!quit) {
189 		SDL_Event	event;
190 		while (SDL_PollEvent(&event)) {
191 			switch(event.type) {
192 				case SDL_MOUSEBUTTONDOWN:
193 					current = (current + 1)%3;
194 					SDL_SetCursor(cursor[current]);
195 					break;
196 				case SDL_KEYDOWN:
197 					if (event.key.keysym.sym == SDLK_ESCAPE) {
198 						quit = SDL_TRUE;
199 					}
200 					break;
201 				case SDL_QUIT:
202 					quit = SDL_TRUE;
203 					break;
204 			}
205 		}
206 		SDL_Flip(screen);
207 		SDL_Delay(1);
208 	}
209 
210 	SDL_FreeCursor(cursor[0]);
211 	SDL_FreeCursor(cursor[1]);
212 	SDL_FreeCursor(cursor[2]);
213 
214 	SDL_Quit();
215 	return(0);
216 }
217