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