Test case for bug #240
authorSam Lantinga <slouken@libsdl.org>
Tue, 20 Jun 2006 04:04:25 +0000
changeset 1862fe99535ac064
parent 1861 b42823412c3b
child 1863 0e44c6f90b95
Test case for bug #240
test/testcursor.c
     1.1 --- a/test/testcursor.c	Tue Jun 20 03:52:41 2006 +0000
     1.2 +++ b/test/testcursor.c	Tue Jun 20 04:04:25 2006 +0000
     1.3 @@ -55,12 +55,92 @@
     1.4  	0xff00
     1.5  };
     1.6  
     1.7 +
     1.8 +/* XPM */
     1.9 +static const char *arrow[] = {
    1.10 +  /* width height num_colors chars_per_pixel */
    1.11 +  "    32    32        3            1",
    1.12 +  /* colors */
    1.13 +  "X c #000000",
    1.14 +  ". c #ffffff",
    1.15 +  "  c None",
    1.16 +  /* pixels */
    1.17 +  "X                               ",
    1.18 +  "XX                              ",
    1.19 +  "X.X                             ",
    1.20 +  "X..X                            ",
    1.21 +  "X...X                           ",
    1.22 +  "X....X                          ",
    1.23 +  "X.....X                         ",
    1.24 +  "X......X                        ",
    1.25 +  "X.......X                       ",
    1.26 +  "X........X                      ",
    1.27 +  "X.....XXXXX                     ",
    1.28 +  "X..X..X                         ",
    1.29 +  "X.X X..X                        ",
    1.30 +  "XX  X..X                        ",
    1.31 +  "X    X..X                       ",
    1.32 +  "     X..X                       ",
    1.33 +  "      X..X                      ",
    1.34 +  "      X..X                      ",
    1.35 +  "       XX                       ",
    1.36 +  "                                ",
    1.37 +  "                                ",
    1.38 +  "                                ",
    1.39 +  "                                ",
    1.40 +  "                                ",
    1.41 +  "                                ",
    1.42 +  "                                ",
    1.43 +  "                                ",
    1.44 +  "                                ",
    1.45 +  "                                ",
    1.46 +  "                                ",
    1.47 +  "                                ",
    1.48 +  "                                ",
    1.49 +  "0,0"
    1.50 +};
    1.51 +
    1.52 +static SDL_Cursor *create_arrow_cursor()
    1.53 +{
    1.54 +  int i, row, col;
    1.55 +  Uint8 data[4*32];
    1.56 +  Uint8 mask[4*32];
    1.57 +  int hot_x, hot_y;
    1.58 +
    1.59 +  i = -1;
    1.60 +  for ( row=0; row<32; ++row ) {
    1.61 +    for ( col=0; col<32; ++col ) {
    1.62 +      if ( col % 8 ) {
    1.63 +        data[i] <<= 1;
    1.64 +        mask[i] <<= 1;
    1.65 +      } else {
    1.66 +        ++i;
    1.67 +        data[i] = mask[i] = 0;
    1.68 +      }
    1.69 +      switch (arrow[4+row][col]) {
    1.70 +        case 'X':
    1.71 +          data[i] |= 0x01;
    1.72 +          mask[i] |= 0x01;
    1.73 +          break;
    1.74 +        case '.':
    1.75 +          mask[i] |= 0x01;
    1.76 +          break;
    1.77 +        case ' ':
    1.78 +          break;
    1.79 +      }
    1.80 +    }
    1.81 +  }
    1.82 +  sscanf(arrow[4+row], "%d,%d", &hot_x, &hot_y);
    1.83 +  return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y);
    1.84 +}
    1.85 +
    1.86 +
    1.87  int main(int argc, char *argv[])
    1.88  {
    1.89  	SDL_Surface *screen;
    1.90  	SDL_bool quit = SDL_FALSE, first_time = SDL_TRUE;
    1.91 -	SDL_Cursor *cursor;
    1.92 -	SDL_Rect update_area;
    1.93 +	SDL_Cursor *cursor[2];
    1.94 +	int current;
    1.95  
    1.96  	/* Load the SDL library */
    1.97  	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
    1.98 @@ -74,25 +154,34 @@
    1.99  		return(1);
   1.100  	}
   1.101  
   1.102 -	update_area.x = update_area.y = 0;
   1.103 -	update_area.w = screen->w;
   1.104 -	update_area.h = screen->h;
   1.105 -
   1.106  	SDL_FillRect(screen, NULL, 0x664422);
   1.107  
   1.108 -	cursor = SDL_CreateCursor((Uint8 *)cursor_data, (Uint8 *)cursor_mask,
   1.109 +	cursor[0] = SDL_CreateCursor((Uint8 *)cursor_data, (Uint8 *)cursor_mask,
   1.110  		16, 16, 8, 8);
   1.111 -	if (cursor==NULL) {
   1.112 -		fprintf(stderr, "Couldn't initialize cursor: %s\n",SDL_GetError());
   1.113 +	if (cursor[0]==NULL) {
   1.114 +		fprintf(stderr, "Couldn't initialize test cursor: %s\n",SDL_GetError());
   1.115 +		SDL_Quit();
   1.116 +		return(1);
   1.117 +	}
   1.118 +	cursor[1] = create_arrow_cursor();
   1.119 +	if (cursor[1]==NULL) {
   1.120 +		fprintf(stderr, "Couldn't initialize arrow cursor: %s\n",SDL_GetError());
   1.121 +		SDL_FreeCursor(cursor[0]);
   1.122 +		SDL_Quit();
   1.123  		return(1);
   1.124  	}
   1.125  
   1.126 -	SDL_SetCursor(cursor);
   1.127 +	current = 0;
   1.128 +	SDL_SetCursor(cursor[current]);
   1.129  
   1.130  	while (!quit) {
   1.131  		SDL_Event	event;
   1.132  		while (SDL_PollEvent(&event)) {
   1.133  			switch(event.type) {
   1.134 +				case SDL_MOUSEBUTTONDOWN:
   1.135 +					current = !current;
   1.136 +					SDL_SetCursor(cursor[current]);
   1.137 +					break;
   1.138  				case SDL_KEYDOWN:
   1.139  					if (event.key.keysym.sym == SDLK_ESCAPE) {
   1.140  						quit = SDL_TRUE;
   1.141 @@ -103,18 +192,12 @@
   1.142  					break;
   1.143  			}
   1.144  		}	
   1.145 -		if (screen->flags & SDL_DOUBLEBUF) {
   1.146 -			SDL_Flip(screen);
   1.147 -		} else {
   1.148 -			if (first_time) {
   1.149 -				SDL_UpdateRects(screen, 1, &update_area);
   1.150 -				first_time = SDL_FALSE;
   1.151 -			}
   1.152 -		}	
   1.153 +		SDL_Flip(screen);
   1.154  		SDL_Delay(1);
   1.155  	}
   1.156  
   1.157 -	SDL_FreeCursor(cursor);
   1.158 +	SDL_FreeCursor(cursor[0]);
   1.159 +	SDL_FreeCursor(cursor[1]);
   1.160  
   1.161  	SDL_Quit();
   1.162  	return(0);