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