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