test/testcursor.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 22 Jan 2012 17:21:00 -0500
changeset 6250 cce0ddcf6747
parent 5535 96594ac5fd1a
permissions -rw-r--r--
Updated to SDL 2.0, and SDL 2.0 can now be installed coexisting with SDL 1.2
     1 /*
     2   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 #include <stdio.h>
    13 #include <stdlib.h>
    14 
    15 #include "SDL.h"
    16 
    17 /* This is an example 16x16 cursor
    18 	top left :	black
    19 	top right : inverted color or black
    20 	bottom left: white
    21 	bottom right: transparent
    22 	(swap left and right for different endianness)
    23 */
    24 
    25 Uint16 cursor_data[16] = {
    26     0xffff,
    27     0xffff,
    28     0xffff,
    29     0xffff,
    30 
    31     0xffff,
    32     0xffff,
    33     0xffff,
    34     0xffff,
    35 
    36     0x0000,
    37     0x0000,
    38     0x0000,
    39     0x0000,
    40 
    41     0x0000,
    42     0x0000,
    43     0x0000,
    44     0x0000
    45 };
    46 
    47 Uint16 cursor_mask[16] = {
    48     0xff00,
    49     0xff00,
    50     0xff00,
    51     0xff00,
    52 
    53     0xff00,
    54     0xff00,
    55     0xff00,
    56     0xff00,
    57 
    58     0xff00,
    59     0xff00,
    60     0xff00,
    61     0xff00,
    62 
    63     0xff00,
    64     0xff00,
    65     0xff00,
    66     0xff00
    67 };
    68 
    69 /* another test cursor: smaller than 16x16, and with an odd height */
    70 
    71 Uint8 small_cursor_data[11] =
    72     { 0x00, 0x18, 0x08, 0x38, 0x44, 0x54, 0x44, 0x38, 0x20, 0x20, 0x00 };
    73 Uint8 small_cursor_mask[11] =
    74     { 0x3C, 0x3C, 0x3C, 0x7C, 0xC6, 0xC6, 0xC6, 0x7C, 0x78, 0x70, 0x70 };
    75 
    76 /* XPM */
    77 static const char *arrow[] = {
    78     /* width height num_colors chars_per_pixel */
    79     "    32    32        3            1",
    80     /* colors */
    81     "X c #000000",
    82     ". c #ffffff",
    83     "  c None",
    84     /* pixels */
    85     "X                               ",
    86     "XX                              ",
    87     "X.X                             ",
    88     "X..X                            ",
    89     "X...X                           ",
    90     "X....X                          ",
    91     "X.....X                         ",
    92     "X......X                        ",
    93     "X.......X                       ",
    94     "X........X                      ",
    95     "X.....XXXXX                     ",
    96     "X..X..X                         ",
    97     "X.X X..X                        ",
    98     "XX  X..X                        ",
    99     "X    X..X                       ",
   100     "     X..X                       ",
   101     "      X..X                      ",
   102     "      X..X                      ",
   103     "       XX                       ",
   104     "                                ",
   105     "                                ",
   106     "                                ",
   107     "                                ",
   108     "                                ",
   109     "                                ",
   110     "                                ",
   111     "                                ",
   112     "                                ",
   113     "                                ",
   114     "                                ",
   115     "                                ",
   116     "                                ",
   117     "0,0"
   118 };
   119 
   120 static SDL_Cursor *
   121 create_arrow_cursor()
   122 {
   123     int i, row, col;
   124     Uint8 data[4 * 32];
   125     Uint8 mask[4 * 32];
   126     int hot_x, hot_y;
   127 
   128     i = -1;
   129     for (row = 0; row < 32; ++row) {
   130         for (col = 0; col < 32; ++col) {
   131             if (col % 8) {
   132                 data[i] <<= 1;
   133                 mask[i] <<= 1;
   134             } else {
   135                 ++i;
   136                 data[i] = mask[i] = 0;
   137             }
   138             switch (arrow[4 + row][col]) {
   139             case 'X':
   140                 data[i] |= 0x01;
   141                 mask[i] |= 0x01;
   142                 break;
   143             case '.':
   144                 mask[i] |= 0x01;
   145                 break;
   146             case ' ':
   147                 break;
   148             }
   149         }
   150     }
   151     SDL_sscanf(arrow[4 + row], "%d,%d", &hot_x, &hot_y);
   152     return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y);
   153 }
   154 
   155 SDL_Surface *
   156 LoadSprite(char *file)
   157 {
   158     SDL_Surface *sprite;
   159 
   160     /* Load the sprite image */
   161     sprite = SDL_LoadBMP(file);
   162     if (sprite == NULL) {
   163         fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
   164         return NULL;
   165     }
   166 
   167     /* Set transparent pixel as the pixel at (0,0) */
   168     if (sprite->format->palette) {
   169         SDL_SetColorKey(sprite, (SDL_SRCCOLORKEY | SDL_RLEACCEL),
   170                         *(Uint8 *) sprite->pixels);
   171     }
   172 
   173     /* We're ready to roll. :) */
   174     return sprite;
   175 }
   176 
   177 int
   178 main(int argc, char *argv[])
   179 {
   180     SDL_Surface *screen;
   181     SDL_bool quit = SDL_FALSE, first_time = SDL_TRUE;
   182     SDL_Cursor *cursor[5];
   183     int current;
   184 
   185     /* Load the SDL library */
   186     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   187         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   188         return (1);
   189     }
   190 
   191     screen = SDL_SetVideoMode(640, 480, 8, SDL_ANYFORMAT);
   192     if (screen == NULL) {
   193         fprintf(stderr, "Couldn't initialize video mode: %s\n",
   194                 SDL_GetError());
   195         return (1);
   196     }
   197 
   198     SDL_FillRect(screen, NULL, 0x664422);
   199 
   200     cursor[0] = SDL_CreateCursor((Uint8 *) cursor_data, (Uint8 *) cursor_mask,
   201                                  16, 16, 8, 8);
   202     if (cursor[0] == NULL) {
   203         fprintf(stderr, "Couldn't initialize test cursor: %s\n",
   204                 SDL_GetError());
   205         SDL_Quit();
   206         return (1);
   207     }
   208     cursor[1] = create_arrow_cursor();
   209     if (cursor[1] == NULL) {
   210         fprintf(stderr, "Couldn't initialize arrow cursor: %s\n",
   211                 SDL_GetError());
   212         SDL_FreeCursor(cursor[0]);
   213         SDL_Quit();
   214         return (1);
   215     }
   216     cursor[2] = SDL_CreateCursor(small_cursor_data, small_cursor_mask,
   217                                  8, 11, 3, 5);
   218     if (cursor[2] == NULL) {
   219         fprintf(stderr, "Couldn't initialize test cursor: %s\n",
   220                 SDL_GetError());
   221         SDL_Quit();
   222         return (1);
   223     }
   224     cursor[3] = SDL_CreateColorCursor(LoadSprite("icon.bmp"), 0, 0);
   225     cursor[4] = SDL_GetCursor();
   226 
   227     current = SDL_arraysize(cursor)-1;
   228     SDL_SetCursor(cursor[current]);
   229 
   230     while (!quit) {
   231         SDL_Event event;
   232         while (SDL_PollEvent(&event)) {
   233             switch (event.type) {
   234             case SDL_MOUSEBUTTONDOWN:
   235                 current = (current + 1) % SDL_arraysize(cursor);
   236                 SDL_SetCursor(cursor[current]);
   237                 break;
   238             case SDL_KEYDOWN:
   239                 if (event.key.keysym.sym == SDLK_ESCAPE) {
   240                     quit = SDL_TRUE;
   241                 }
   242                 break;
   243             case SDL_QUIT:
   244                 quit = SDL_TRUE;
   245                 break;
   246             }
   247         }
   248         SDL_Flip(screen);
   249         SDL_Delay(1);
   250     }
   251 
   252     for (current = 0; current < SDL_arraysize(cursor); ++current) {
   253         SDL_FreeCursor(cursor[current]);
   254     }
   255 
   256     SDL_Quit();
   257     return (0);
   258 }
   259 
   260 /* vi: set ts=4 sw=4 expandtab: */