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