test/testcursor.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 26 Sep 2009 23:17:08 +0000
branchSDL-1.2
changeset 4246 8b8314cc34a6
parent 3912 e7d2858670c1
permissions -rw-r--r--
Fixed bug #810

Lauri Kenttä 2009-09-26 06:42:23 PDT

Support for disabling stdio redirect with environment variables.
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@1883
    58
/* another test cursor: smaller than 16x16, and with an odd height */
slouken@1883
    59
slouken@1883
    60
Uint8 small_cursor_data[11] = { 0x00, 0x18, 0x08, 0x38, 0x44, 0x54, 0x44, 0x38, 0x20, 0x20, 0x00 };
slouken@1883
    61
Uint8 small_cursor_mask[11] = { 0x3C, 0x3C, 0x3C, 0x7C, 0xC6, 0xC6, 0xC6, 0x7C, 0x78, 0x70, 0x70 };
slouken@1862
    62
slouken@1862
    63
/* XPM */
slouken@1862
    64
static const char *arrow[] = {
slouken@1862
    65
  /* width height num_colors chars_per_pixel */
slouken@1862
    66
  "    32    32        3            1",
slouken@1862
    67
  /* colors */
slouken@1862
    68
  "X c #000000",
slouken@1862
    69
  ". c #ffffff",
slouken@1862
    70
  "  c None",
slouken@1862
    71
  /* pixels */
slouken@1862
    72
  "X                               ",
slouken@1862
    73
  "XX                              ",
slouken@1862
    74
  "X.X                             ",
slouken@1862
    75
  "X..X                            ",
slouken@1862
    76
  "X...X                           ",
slouken@1862
    77
  "X....X                          ",
slouken@1862
    78
  "X.....X                         ",
slouken@1862
    79
  "X......X                        ",
slouken@1862
    80
  "X.......X                       ",
slouken@1862
    81
  "X........X                      ",
slouken@1862
    82
  "X.....XXXXX                     ",
slouken@1862
    83
  "X..X..X                         ",
slouken@1862
    84
  "X.X X..X                        ",
slouken@1862
    85
  "XX  X..X                        ",
slouken@1862
    86
  "X    X..X                       ",
slouken@1862
    87
  "     X..X                       ",
slouken@1862
    88
  "      X..X                      ",
slouken@1862
    89
  "      X..X                      ",
slouken@1862
    90
  "       XX                       ",
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
  "                                ",
slouken@1862
   101
  "                                ",
slouken@1862
   102
  "                                ",
slouken@1862
   103
  "                                ",
slouken@1862
   104
  "0,0"
slouken@1862
   105
};
slouken@1862
   106
slouken@1862
   107
static SDL_Cursor *create_arrow_cursor()
slouken@1862
   108
{
slouken@1862
   109
  int i, row, col;
slouken@1862
   110
  Uint8 data[4*32];
slouken@1862
   111
  Uint8 mask[4*32];
slouken@1862
   112
  int hot_x, hot_y;
slouken@1862
   113
slouken@1862
   114
  i = -1;
slouken@1862
   115
  for ( row=0; row<32; ++row ) {
slouken@1862
   116
    for ( col=0; col<32; ++col ) {
slouken@1862
   117
      if ( col % 8 ) {
slouken@1862
   118
        data[i] <<= 1;
slouken@1862
   119
        mask[i] <<= 1;
slouken@1862
   120
      } else {
slouken@1862
   121
        ++i;
slouken@1862
   122
        data[i] = mask[i] = 0;
slouken@1862
   123
      }
slouken@1862
   124
      switch (arrow[4+row][col]) {
slouken@1862
   125
        case 'X':
slouken@1862
   126
          data[i] |= 0x01;
slouken@1862
   127
          mask[i] |= 0x01;
slouken@1862
   128
          break;
slouken@1862
   129
        case '.':
slouken@1862
   130
          mask[i] |= 0x01;
slouken@1862
   131
          break;
slouken@1862
   132
        case ' ':
slouken@1862
   133
          break;
slouken@1862
   134
      }
slouken@1862
   135
    }
slouken@1862
   136
  }
slouken@1862
   137
  sscanf(arrow[4+row], "%d,%d", &hot_x, &hot_y);
slouken@1862
   138
  return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y);
slouken@1862
   139
}
slouken@1862
   140
slouken@1862
   141
patmandin@1858
   142
int main(int argc, char *argv[])
patmandin@1858
   143
{
patmandin@1858
   144
	SDL_Surface *screen;
patmandin@1858
   145
	SDL_bool quit = SDL_FALSE, first_time = SDL_TRUE;
slouken@1883
   146
	SDL_Cursor *cursor[3];
slouken@1862
   147
	int current;
patmandin@1858
   148
patmandin@1858
   149
	/* Load the SDL library */
patmandin@1858
   150
	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
patmandin@1858
   151
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
patmandin@1858
   152
		return(1);
patmandin@1858
   153
	}
patmandin@1858
   154
patmandin@1858
   155
	screen = SDL_SetVideoMode(320,200,8,SDL_ANYFORMAT);
patmandin@1858
   156
	if (screen==NULL) {
patmandin@1858
   157
		fprintf(stderr, "Couldn't initialize video mode: %s\n",SDL_GetError());
patmandin@1858
   158
		return(1);
patmandin@1858
   159
	}
patmandin@1858
   160
patmandin@1858
   161
	SDL_FillRect(screen, NULL, 0x664422);
patmandin@1858
   162
slouken@1862
   163
	cursor[0] = SDL_CreateCursor((Uint8 *)cursor_data, (Uint8 *)cursor_mask,
patmandin@1858
   164
		16, 16, 8, 8);
slouken@1862
   165
	if (cursor[0]==NULL) {
slouken@1862
   166
		fprintf(stderr, "Couldn't initialize test cursor: %s\n",SDL_GetError());
slouken@1862
   167
		SDL_Quit();
slouken@1862
   168
		return(1);
slouken@1862
   169
	}
slouken@1862
   170
	cursor[1] = create_arrow_cursor();
slouken@1862
   171
	if (cursor[1]==NULL) {
slouken@1862
   172
		fprintf(stderr, "Couldn't initialize arrow cursor: %s\n",SDL_GetError());
slouken@1862
   173
		SDL_FreeCursor(cursor[0]);
slouken@1862
   174
		SDL_Quit();
patmandin@1858
   175
		return(1);
patmandin@1858
   176
	}
slouken@1883
   177
	cursor[2] = SDL_CreateCursor(small_cursor_data, small_cursor_mask,
slouken@1883
   178
		8, 11, 3, 5);
slouken@1883
   179
	if (cursor[2]==NULL) {
slouken@1883
   180
		fprintf(stderr, "Couldn't initialize test cursor: %s\n",SDL_GetError());
slouken@1883
   181
		SDL_Quit();
slouken@1883
   182
		return(1);
slouken@1883
   183
	}
patmandin@1858
   184
slouken@1862
   185
	current = 0;
slouken@1862
   186
	SDL_SetCursor(cursor[current]);
patmandin@1858
   187
patmandin@1858
   188
	while (!quit) {
patmandin@1858
   189
		SDL_Event	event;
patmandin@1858
   190
		while (SDL_PollEvent(&event)) {
patmandin@1858
   191
			switch(event.type) {
slouken@1862
   192
				case SDL_MOUSEBUTTONDOWN:
slouken@1883
   193
					current = (current + 1)%3;
slouken@1862
   194
					SDL_SetCursor(cursor[current]);
slouken@1862
   195
					break;
patmandin@1858
   196
				case SDL_KEYDOWN:
patmandin@1858
   197
					if (event.key.keysym.sym == SDLK_ESCAPE) {
patmandin@1858
   198
						quit = SDL_TRUE;
patmandin@1858
   199
					}
patmandin@1858
   200
					break;
patmandin@1858
   201
				case SDL_QUIT:
patmandin@1858
   202
					quit = SDL_TRUE;
patmandin@1858
   203
					break;
patmandin@1858
   204
			}
patmandin@1858
   205
		}	
slouken@1862
   206
		SDL_Flip(screen);
patmandin@1858
   207
		SDL_Delay(1);
patmandin@1858
   208
	}
patmandin@1858
   209
slouken@1862
   210
	SDL_FreeCursor(cursor[0]);
slouken@1862
   211
	SDL_FreeCursor(cursor[1]);
icculus@3912
   212
	SDL_FreeCursor(cursor[2]);
patmandin@1858
   213
patmandin@1858
   214
	SDL_Quit();
patmandin@1858
   215
	return(0);
patmandin@1858
   216
}