comparison test/testcursor.c @ 1862:fe99535ac064

Test case for bug #240
author Sam Lantinga <slouken@libsdl.org>
date Tue, 20 Jun 2006 04:04:25 +0000
parents 2fce7697adca
children 2780f547f5e7
comparison
equal deleted inserted replaced
1861:b42823412c3b 1862:fe99535ac064
53 0xff00, 53 0xff00,
54 0xff00, 54 0xff00,
55 0xff00 55 0xff00
56 }; 56 };
57 57
58
59 /* XPM */
60 static const char *arrow[] = {
61 /* width height num_colors chars_per_pixel */
62 " 32 32 3 1",
63 /* colors */
64 "X c #000000",
65 ". c #ffffff",
66 " c None",
67 /* pixels */
68 "X ",
69 "XX ",
70 "X.X ",
71 "X..X ",
72 "X...X ",
73 "X....X ",
74 "X.....X ",
75 "X......X ",
76 "X.......X ",
77 "X........X ",
78 "X.....XXXXX ",
79 "X..X..X ",
80 "X.X X..X ",
81 "XX X..X ",
82 "X X..X ",
83 " X..X ",
84 " X..X ",
85 " X..X ",
86 " XX ",
87 " ",
88 " ",
89 " ",
90 " ",
91 " ",
92 " ",
93 " ",
94 " ",
95 " ",
96 " ",
97 " ",
98 " ",
99 " ",
100 "0,0"
101 };
102
103 static SDL_Cursor *create_arrow_cursor()
104 {
105 int i, row, col;
106 Uint8 data[4*32];
107 Uint8 mask[4*32];
108 int hot_x, hot_y;
109
110 i = -1;
111 for ( row=0; row<32; ++row ) {
112 for ( col=0; col<32; ++col ) {
113 if ( col % 8 ) {
114 data[i] <<= 1;
115 mask[i] <<= 1;
116 } else {
117 ++i;
118 data[i] = mask[i] = 0;
119 }
120 switch (arrow[4+row][col]) {
121 case 'X':
122 data[i] |= 0x01;
123 mask[i] |= 0x01;
124 break;
125 case '.':
126 mask[i] |= 0x01;
127 break;
128 case ' ':
129 break;
130 }
131 }
132 }
133 sscanf(arrow[4+row], "%d,%d", &hot_x, &hot_y);
134 return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y);
135 }
136
137
58 int main(int argc, char *argv[]) 138 int main(int argc, char *argv[])
59 { 139 {
60 SDL_Surface *screen; 140 SDL_Surface *screen;
61 SDL_bool quit = SDL_FALSE, first_time = SDL_TRUE; 141 SDL_bool quit = SDL_FALSE, first_time = SDL_TRUE;
62 SDL_Cursor *cursor; 142 SDL_Cursor *cursor[2];
63 SDL_Rect update_area; 143 int current;
64 144
65 /* Load the SDL library */ 145 /* Load the SDL library */
66 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) { 146 if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
67 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError()); 147 fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
68 return(1); 148 return(1);
72 if (screen==NULL) { 152 if (screen==NULL) {
73 fprintf(stderr, "Couldn't initialize video mode: %s\n",SDL_GetError()); 153 fprintf(stderr, "Couldn't initialize video mode: %s\n",SDL_GetError());
74 return(1); 154 return(1);
75 } 155 }
76 156
77 update_area.x = update_area.y = 0;
78 update_area.w = screen->w;
79 update_area.h = screen->h;
80
81 SDL_FillRect(screen, NULL, 0x664422); 157 SDL_FillRect(screen, NULL, 0x664422);
82 158
83 cursor = SDL_CreateCursor((Uint8 *)cursor_data, (Uint8 *)cursor_mask, 159 cursor[0] = SDL_CreateCursor((Uint8 *)cursor_data, (Uint8 *)cursor_mask,
84 16, 16, 8, 8); 160 16, 16, 8, 8);
85 if (cursor==NULL) { 161 if (cursor[0]==NULL) {
86 fprintf(stderr, "Couldn't initialize cursor: %s\n",SDL_GetError()); 162 fprintf(stderr, "Couldn't initialize test cursor: %s\n",SDL_GetError());
87 return(1); 163 SDL_Quit();
88 } 164 return(1);
89 165 }
90 SDL_SetCursor(cursor); 166 cursor[1] = create_arrow_cursor();
167 if (cursor[1]==NULL) {
168 fprintf(stderr, "Couldn't initialize arrow cursor: %s\n",SDL_GetError());
169 SDL_FreeCursor(cursor[0]);
170 SDL_Quit();
171 return(1);
172 }
173
174 current = 0;
175 SDL_SetCursor(cursor[current]);
91 176
92 while (!quit) { 177 while (!quit) {
93 SDL_Event event; 178 SDL_Event event;
94 while (SDL_PollEvent(&event)) { 179 while (SDL_PollEvent(&event)) {
95 switch(event.type) { 180 switch(event.type) {
181 case SDL_MOUSEBUTTONDOWN:
182 current = !current;
183 SDL_SetCursor(cursor[current]);
184 break;
96 case SDL_KEYDOWN: 185 case SDL_KEYDOWN:
97 if (event.key.keysym.sym == SDLK_ESCAPE) { 186 if (event.key.keysym.sym == SDLK_ESCAPE) {
98 quit = SDL_TRUE; 187 quit = SDL_TRUE;
99 } 188 }
100 break; 189 break;
101 case SDL_QUIT: 190 case SDL_QUIT:
102 quit = SDL_TRUE; 191 quit = SDL_TRUE;
103 break; 192 break;
104 } 193 }
105 } 194 }
106 if (screen->flags & SDL_DOUBLEBUF) { 195 SDL_Flip(screen);
107 SDL_Flip(screen);
108 } else {
109 if (first_time) {
110 SDL_UpdateRects(screen, 1, &update_area);
111 first_time = SDL_FALSE;
112 }
113 }
114 SDL_Delay(1); 196 SDL_Delay(1);
115 } 197 }
116 198
117 SDL_FreeCursor(cursor); 199 SDL_FreeCursor(cursor[0]);
200 SDL_FreeCursor(cursor[1]);
118 201
119 SDL_Quit(); 202 SDL_Quit();
120 return(0); 203 return(0);
121 } 204 }