comparison test/testcursor.c @ 1895:c121d94672cb

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