test/testcursor.c
branchSDL-1.3
changeset 1708 cd14138a8703
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testcursor.c	Sat Jun 24 17:31:46 2006 +0000
     1.3 @@ -0,0 +1,223 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +
     1.7 +#include "SDL.h"
     1.8 +
     1.9 +/* This is an example 16x16 cursor
    1.10 +	top left :	black
    1.11 +	top right : inverted color or black
    1.12 +	bottom left: white
    1.13 +	bottom right: transparent
    1.14 +	(swap left and right for different endianness)
    1.15 +*/
    1.16 +
    1.17 +Uint16 cursor_data[16] = {
    1.18 +    0xffff,
    1.19 +    0xffff,
    1.20 +    0xffff,
    1.21 +    0xffff,
    1.22 +
    1.23 +    0xffff,
    1.24 +    0xffff,
    1.25 +    0xffff,
    1.26 +    0xffff,
    1.27 +
    1.28 +    0x0000,
    1.29 +    0x0000,
    1.30 +    0x0000,
    1.31 +    0x0000,
    1.32 +
    1.33 +    0x0000,
    1.34 +    0x0000,
    1.35 +    0x0000,
    1.36 +    0x0000
    1.37 +};
    1.38 +
    1.39 +Uint16 cursor_mask[16] = {
    1.40 +    0xff00,
    1.41 +    0xff00,
    1.42 +    0xff00,
    1.43 +    0xff00,
    1.44 +
    1.45 +    0xff00,
    1.46 +    0xff00,
    1.47 +    0xff00,
    1.48 +    0xff00,
    1.49 +
    1.50 +    0xff00,
    1.51 +    0xff00,
    1.52 +    0xff00,
    1.53 +    0xff00,
    1.54 +
    1.55 +    0xff00,
    1.56 +    0xff00,
    1.57 +    0xff00,
    1.58 +    0xff00
    1.59 +};
    1.60 +
    1.61 +/* another test cursor: smaller than 16x16, and with an odd height */
    1.62 +
    1.63 +Uint8 small_cursor_data[11] =
    1.64 +    { 0x00, 0x18, 0x08, 0x38, 0x44, 0x54, 0x44, 0x38, 0x20, 0x20, 0x00 };
    1.65 +Uint8 small_cursor_mask[11] =
    1.66 +    { 0x3C, 0x3C, 0x3C, 0x7C, 0xC6, 0xC6, 0xC6, 0x7C, 0x78, 0x70, 0x70 };
    1.67 +
    1.68 +/* XPM */
    1.69 +static const char *arrow[] = {
    1.70 +    /* width height num_colors chars_per_pixel */
    1.71 +    "    32    32        3            1",
    1.72 +    /* colors */
    1.73 +    "X c #000000",
    1.74 +    ". c #ffffff",
    1.75 +    "  c None",
    1.76 +    /* pixels */
    1.77 +    "X                               ",
    1.78 +    "XX                              ",
    1.79 +    "X.X                             ",
    1.80 +    "X..X                            ",
    1.81 +    "X...X                           ",
    1.82 +    "X....X                          ",
    1.83 +    "X.....X                         ",
    1.84 +    "X......X                        ",
    1.85 +    "X.......X                       ",
    1.86 +    "X........X                      ",
    1.87 +    "X.....XXXXX                     ",
    1.88 +    "X..X..X                         ",
    1.89 +    "X.X X..X                        ",
    1.90 +    "XX  X..X                        ",
    1.91 +    "X    X..X                       ",
    1.92 +    "     X..X                       ",
    1.93 +    "      X..X                      ",
    1.94 +    "      X..X                      ",
    1.95 +    "       XX                       ",
    1.96 +    "                                ",
    1.97 +    "                                ",
    1.98 +    "                                ",
    1.99 +    "                                ",
   1.100 +    "                                ",
   1.101 +    "                                ",
   1.102 +    "                                ",
   1.103 +    "                                ",
   1.104 +    "                                ",
   1.105 +    "                                ",
   1.106 +    "                                ",
   1.107 +    "                                ",
   1.108 +    "                                ",
   1.109 +    "0,0"
   1.110 +};
   1.111 +
   1.112 +static SDL_Cursor *
   1.113 +create_arrow_cursor()
   1.114 +{
   1.115 +    int i, row, col;
   1.116 +    Uint8 data[4 * 32];
   1.117 +    Uint8 mask[4 * 32];
   1.118 +    int hot_x, hot_y;
   1.119 +
   1.120 +    i = -1;
   1.121 +    for (row = 0; row < 32; ++row) {
   1.122 +        for (col = 0; col < 32; ++col) {
   1.123 +            if (col % 8) {
   1.124 +                data[i] <<= 1;
   1.125 +                mask[i] <<= 1;
   1.126 +            } else {
   1.127 +                ++i;
   1.128 +                data[i] = mask[i] = 0;
   1.129 +            }
   1.130 +            switch (arrow[4 + row][col]) {
   1.131 +            case 'X':
   1.132 +                data[i] |= 0x01;
   1.133 +                mask[i] |= 0x01;
   1.134 +                break;
   1.135 +            case '.':
   1.136 +                mask[i] |= 0x01;
   1.137 +                break;
   1.138 +            case ' ':
   1.139 +                break;
   1.140 +            }
   1.141 +        }
   1.142 +    }
   1.143 +    sscanf(arrow[4 + row], "%d,%d", &hot_x, &hot_y);
   1.144 +    return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y);
   1.145 +}
   1.146 +
   1.147 +
   1.148 +int
   1.149 +main(int argc, char *argv[])
   1.150 +{
   1.151 +    SDL_Surface *screen;
   1.152 +    SDL_bool quit = SDL_FALSE, first_time = SDL_TRUE;
   1.153 +    SDL_Cursor *cursor[3];
   1.154 +    int current;
   1.155 +
   1.156 +    /* Load the SDL library */
   1.157 +    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   1.158 +        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   1.159 +        return (1);
   1.160 +    }
   1.161 +
   1.162 +    screen = SDL_SetVideoMode(320, 200, 8, SDL_ANYFORMAT);
   1.163 +    if (screen == NULL) {
   1.164 +        fprintf(stderr, "Couldn't initialize video mode: %s\n",
   1.165 +                SDL_GetError());
   1.166 +        return (1);
   1.167 +    }
   1.168 +
   1.169 +    SDL_FillRect(screen, NULL, 0x664422);
   1.170 +
   1.171 +    cursor[0] = SDL_CreateCursor((Uint8 *) cursor_data, (Uint8 *) cursor_mask,
   1.172 +                                 16, 16, 8, 8);
   1.173 +    if (cursor[0] == NULL) {
   1.174 +        fprintf(stderr, "Couldn't initialize test cursor: %s\n",
   1.175 +                SDL_GetError());
   1.176 +        SDL_Quit();
   1.177 +        return (1);
   1.178 +    }
   1.179 +    cursor[1] = create_arrow_cursor();
   1.180 +    if (cursor[1] == NULL) {
   1.181 +        fprintf(stderr, "Couldn't initialize arrow cursor: %s\n",
   1.182 +                SDL_GetError());
   1.183 +        SDL_FreeCursor(cursor[0]);
   1.184 +        SDL_Quit();
   1.185 +        return (1);
   1.186 +    }
   1.187 +    cursor[2] = SDL_CreateCursor(small_cursor_data, small_cursor_mask,
   1.188 +                                 8, 11, 3, 5);
   1.189 +    if (cursor[2] == NULL) {
   1.190 +        fprintf(stderr, "Couldn't initialize test cursor: %s\n",
   1.191 +                SDL_GetError());
   1.192 +        SDL_Quit();
   1.193 +        return (1);
   1.194 +    }
   1.195 +
   1.196 +    current = 0;
   1.197 +    SDL_SetCursor(cursor[current]);
   1.198 +
   1.199 +    while (!quit) {
   1.200 +        SDL_Event event;
   1.201 +        while (SDL_PollEvent(&event)) {
   1.202 +            switch (event.type) {
   1.203 +            case SDL_MOUSEBUTTONDOWN:
   1.204 +                current = (current + 1) % 3;
   1.205 +                SDL_SetCursor(cursor[current]);
   1.206 +                break;
   1.207 +            case SDL_KEYDOWN:
   1.208 +                if (event.key.keysym.sym == SDLK_ESCAPE) {
   1.209 +                    quit = SDL_TRUE;
   1.210 +                }
   1.211 +                break;
   1.212 +            case SDL_QUIT:
   1.213 +                quit = SDL_TRUE;
   1.214 +                break;
   1.215 +            }
   1.216 +        }
   1.217 +        SDL_Flip(screen);
   1.218 +        SDL_Delay(1);
   1.219 +    }
   1.220 +
   1.221 +    SDL_FreeCursor(cursor[0]);
   1.222 +    SDL_FreeCursor(cursor[1]);
   1.223 +
   1.224 +    SDL_Quit();
   1.225 +    return (0);
   1.226 +}