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