test/testcustomcursor.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 23 May 2019 14:19:00 -0700
changeset 12756 4116bf232fe7
parent 12503 806492103856
child 12758 3c6a6dad3487
permissions -rw-r--r--
Added a function to get the current Android SDK version at runtime
brandon@10194
     1
/*
slouken@12503
     2
  Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
brandon@10194
     3
brandon@10194
     4
  This software is provided 'as-is', without any express or implied
brandon@10194
     5
  warranty.  In no event will the authors be held liable for any damages
brandon@10194
     6
  arising from the use of this software.
brandon@10194
     7
brandon@10194
     8
  Permission is granted to anyone to use this software for any purpose,
brandon@10194
     9
  including commercial applications, and to alter it and redistribute it
brandon@10194
    10
  freely.
brandon@10194
    11
*/
brandon@10194
    12
brandon@10194
    13
#include <stdlib.h>
brandon@10194
    14
#include <stdio.h>
brandon@10194
    15
brandon@10194
    16
#ifdef __EMSCRIPTEN__
brandon@10194
    17
#include <emscripten/emscripten.h>
brandon@10194
    18
#endif
brandon@10194
    19
brandon@10194
    20
#include "SDL_test_common.h"
brandon@10194
    21
brandon@10194
    22
/* Stolen from the mailing list */
brandon@10194
    23
/* Creates a new mouse cursor from an XPM */
brandon@10194
    24
brandon@10194
    25
brandon@10194
    26
/* XPM */
brandon@10194
    27
static const char *arrow[] = {
brandon@10194
    28
  /* width height num_colors chars_per_pixel */
brandon@10194
    29
  "    32    32        3            1",
brandon@10194
    30
  /* colors */
brandon@10194
    31
  "X c #000000",
brandon@10194
    32
  ". c #ffffff",
brandon@10194
    33
  "  c None",
brandon@10194
    34
  /* pixels */
brandon@10194
    35
  "X                               ",
brandon@10194
    36
  "XX                              ",
brandon@10194
    37
  "X.X                             ",
brandon@10194
    38
  "X..X                            ",
brandon@10194
    39
  "X...X                           ",
brandon@10194
    40
  "X....X                          ",
brandon@10194
    41
  "X.....X                         ",
brandon@10194
    42
  "X......X                        ",
brandon@10194
    43
  "X.......X                       ",
brandon@10194
    44
  "X........X                      ",
brandon@10194
    45
  "X.....XXXXX                     ",
brandon@10194
    46
  "X..X..X                         ",
brandon@10194
    47
  "X.X X..X                        ",
brandon@10194
    48
  "XX  X..X                        ",
brandon@10194
    49
  "X    X..X                       ",
brandon@10194
    50
  "     X..X                       ",
brandon@10194
    51
  "      X..X                      ",
brandon@10194
    52
  "      X..X                      ",
brandon@10194
    53
  "       XX                       ",
brandon@10194
    54
  "                                ",
brandon@10194
    55
  "                                ",
brandon@10194
    56
  "                                ",
brandon@10194
    57
  "                                ",
brandon@10194
    58
  "                                ",
brandon@10194
    59
  "                                ",
brandon@10194
    60
  "                                ",
brandon@10194
    61
  "                                ",
brandon@10194
    62
  "                                ",
brandon@10194
    63
  "                                ",
brandon@10194
    64
  "                                ",
brandon@10194
    65
  "                                ",
brandon@10194
    66
  "                                ",
brandon@10194
    67
  "0,0"
brandon@10194
    68
};  
brandon@10194
    69
brandon@10194
    70
static SDL_Cursor*
slouken@10464
    71
init_color_cursor(const char *file)
slouken@10464
    72
{
slouken@10464
    73
    SDL_Cursor *cursor = NULL;
slouken@10464
    74
    SDL_Surface *surface = SDL_LoadBMP(file);
slouken@10464
    75
    if (surface) {
slouken@11931
    76
        if (surface->format->palette) {
slouken@11931
    77
            SDL_SetColorKey(surface, 1, *(Uint8 *) surface->pixels);
slouken@11931
    78
        } else {
slouken@11931
    79
            switch (surface->format->BitsPerPixel) {
slouken@11931
    80
            case 15:
slouken@11931
    81
                SDL_SetColorKey(surface, 1, (*(Uint16 *)surface->pixels) & 0x00007FFF);
slouken@11931
    82
                break;
slouken@11931
    83
            case 16:
slouken@11931
    84
                SDL_SetColorKey(surface, 1, *(Uint16 *)surface->pixels);
slouken@11931
    85
                break;
slouken@11931
    86
            case 24:
slouken@11931
    87
                SDL_SetColorKey(surface, 1, (*(Uint32 *)surface->pixels) & 0x00FFFFFF);
slouken@11931
    88
                break;
slouken@11931
    89
            case 32:
slouken@11931
    90
                SDL_SetColorKey(surface, 1, *(Uint32 *)surface->pixels);
slouken@11931
    91
                break;
slouken@11931
    92
            }
slouken@11931
    93
        }
slouken@10464
    94
        cursor = SDL_CreateColorCursor(surface, 0, 0);
slouken@10464
    95
        SDL_FreeSurface(surface);
slouken@10464
    96
    }
slouken@10464
    97
    return cursor;
slouken@10464
    98
}
slouken@10464
    99
slouken@10464
   100
static SDL_Cursor*
brandon@10194
   101
init_system_cursor(const char *image[])
brandon@10194
   102
{
brandon@10194
   103
  int i, row, col;
brandon@10194
   104
  Uint8 data[4*32];
brandon@10194
   105
  Uint8 mask[4*32];
brandon@10194
   106
  int hot_x, hot_y;
brandon@10194
   107
brandon@10194
   108
  i = -1;
brandon@10194
   109
  for (row=0; row<32; ++row) {
brandon@10194
   110
    for (col=0; col<32; ++col) {
brandon@10194
   111
      if (col % 8) {
brandon@10194
   112
        data[i] <<= 1;
brandon@10194
   113
        mask[i] <<= 1;
brandon@10194
   114
      } else {
brandon@10194
   115
        ++i;
brandon@10194
   116
        data[i] = mask[i] = 0;
brandon@10194
   117
      }
brandon@10194
   118
      switch (image[4+row][col]) {
brandon@10194
   119
        case 'X':
brandon@10194
   120
          data[i] |= 0x01;
brandon@10194
   121
          mask[i] |= 0x01;
brandon@10194
   122
          break;
brandon@10194
   123
        case '.':
brandon@10194
   124
          mask[i] |= 0x01;
brandon@10194
   125
          break;
brandon@10194
   126
        case ' ':
brandon@10194
   127
          break;
brandon@10194
   128
      }
brandon@10194
   129
    }
brandon@10194
   130
  }
brandon@10194
   131
  sscanf(image[4+row], "%d,%d", &hot_x, &hot_y);
brandon@10194
   132
  return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y);
brandon@10194
   133
}
brandon@10194
   134
brandon@10194
   135
static SDLTest_CommonState *state;
brandon@10194
   136
int done;
slouken@11931
   137
static SDL_Cursor *cursors[1+SDL_NUM_SYSTEM_CURSORS];
slouken@11931
   138
static int current_cursor;
slouken@11931
   139
static int show_cursor;
brandon@10194
   140
brandon@10194
   141
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
brandon@10194
   142
static void
brandon@10194
   143
quit(int rc)
brandon@10194
   144
{
brandon@10194
   145
    SDLTest_CommonQuit(state);
brandon@10194
   146
    exit(rc);
brandon@10194
   147
}
brandon@10194
   148
brandon@10194
   149
void
brandon@10194
   150
loop()
brandon@10194
   151
{
brandon@10194
   152
    int i;
brandon@10194
   153
    SDL_Event event;
brandon@10194
   154
    /* Check for events */
brandon@10194
   155
    while (SDL_PollEvent(&event)) {
brandon@10194
   156
        SDLTest_CommonEvent(state, &event, &done);
slouken@11931
   157
        if (event.type == SDL_MOUSEBUTTONDOWN) {
slouken@11931
   158
            if (event.button.button == SDL_BUTTON_LEFT) {
slouken@11931
   159
                ++current_cursor;
slouken@11931
   160
                if (current_cursor == SDL_arraysize(cursors)) {
slouken@11931
   161
                    current_cursor = 0;
slouken@11931
   162
                }
slouken@11931
   163
                SDL_SetCursor(cursors[current_cursor]);
slouken@11931
   164
            } else {
slouken@11931
   165
                show_cursor = !show_cursor;
slouken@11931
   166
                SDL_ShowCursor(show_cursor);
slouken@11931
   167
            }
slouken@11931
   168
        }
brandon@10194
   169
    }
brandon@10194
   170
    
brandon@10194
   171
    for (i = 0; i < state->num_windows; ++i) {
brandon@10194
   172
        SDL_Renderer *renderer = state->renderers[i];
brandon@10194
   173
        SDL_RenderClear(renderer);
brandon@10194
   174
        SDL_RenderPresent(renderer);
brandon@10194
   175
    }
brandon@10194
   176
#ifdef __EMSCRIPTEN__
brandon@10194
   177
    if (done) {
brandon@10194
   178
        emscripten_cancel_main_loop();
brandon@10194
   179
    }
brandon@10194
   180
#endif
brandon@10194
   181
}
brandon@10194
   182
brandon@10194
   183
int
brandon@10194
   184
main(int argc, char *argv[])
brandon@10194
   185
{
brandon@10194
   186
    int i;
slouken@10464
   187
    const char *color_cursor = NULL;
brandon@10194
   188
brandon@10194
   189
    /* Enable standard application logging */
brandon@10194
   190
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
brandon@10194
   191
brandon@10194
   192
    /* Initialize test framework */
brandon@10194
   193
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
brandon@10194
   194
    if (!state) {
brandon@10194
   195
        return 1;
brandon@10194
   196
    }
brandon@10194
   197
    for (i = 1; i < argc;) {
brandon@10194
   198
        int consumed;
brandon@10194
   199
brandon@10194
   200
        consumed = SDLTest_CommonArg(state, i);
brandon@10194
   201
        if (consumed == 0) {
slouken@10464
   202
            color_cursor = argv[i];
slouken@10464
   203
            break;
brandon@10194
   204
        }
brandon@10194
   205
        if (consumed < 0) {
brandon@10194
   206
            SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
brandon@10194
   207
            quit(1);
brandon@10194
   208
        }
brandon@10194
   209
        i += consumed;
brandon@10194
   210
    }
brandon@10194
   211
brandon@10194
   212
    if (!SDLTest_CommonInit(state)) {
brandon@10194
   213
        quit(2);
brandon@10194
   214
    }
brandon@10194
   215
brandon@10195
   216
    for (i = 0; i < state->num_windows; ++i) {
brandon@10195
   217
        SDL_Renderer *renderer = state->renderers[i];
brandon@10195
   218
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
brandon@10195
   219
        SDL_RenderClear(renderer);
brandon@10195
   220
    }
brandon@10194
   221
slouken@10464
   222
    if (color_cursor) {
slouken@11931
   223
        cursors[0] = init_color_cursor(color_cursor);
slouken@10464
   224
    } else {
slouken@11931
   225
        cursors[0] = init_system_cursor(arrow);
slouken@10464
   226
    }
slouken@11931
   227
    if (!cursors[0]) {
slouken@10464
   228
        SDL_Log("Error, couldn't create cursor\n");
slouken@10464
   229
        quit(2);
slouken@10464
   230
    }
slouken@11931
   231
    for (i = 0; i < SDL_NUM_SYSTEM_CURSORS; ++i) {
slouken@11931
   232
        cursors[1+i] = SDL_CreateSystemCursor((SDL_SystemCursor)i);
slouken@11931
   233
        if (!cursors[1+i]) {
slouken@11931
   234
            SDL_Log("Error, couldn't create system cursor %d\n", i);
slouken@11931
   235
            quit(2);
slouken@11931
   236
        }
slouken@11931
   237
    }
slouken@11931
   238
    SDL_SetCursor(cursors[0]);
brandon@10194
   239
brandon@10194
   240
    /* Main render loop */
brandon@10194
   241
    done = 0;
brandon@10194
   242
#ifdef __EMSCRIPTEN__
brandon@10194
   243
    emscripten_set_main_loop(loop, 0, 1);
brandon@10194
   244
#else
brandon@10194
   245
    while (!done) {
brandon@10194
   246
        loop();
brandon@10194
   247
    }
brandon@10194
   248
#endif
brandon@10194
   249
slouken@11931
   250
    for (i = 0; i < SDL_arraysize(cursors); ++i) {
slouken@11931
   251
        SDL_FreeCursor(cursors[i]);
slouken@11931
   252
    }
brandon@10194
   253
    quit(0);
brandon@10194
   254
brandon@10194
   255
    /* keep the compiler happy ... */
brandon@10194
   256
    return(0);
brandon@10194
   257
}
slouken@11931
   258
slouken@11931
   259
/* vi: set ts=4 sw=4 expandtab: */