test/testcursor.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 03 Jun 2011 16:03:10 -0400
changeset 5547 4ccecd0901e2
parent 5535 96594ac5fd1a
permissions -rw-r--r--
Assert code's stdio interface was reading from the wrong variable.

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