test/testcustomcursor.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 03 Jan 2018 10:03:25 -0800
changeset 11811 5d94cb6b24d3
parent 10737 3406a0f8b041
child 11931 d479188e0155
permissions -rw-r--r--
Updated copyright for 2018
brandon@10194
     1
/*
slouken@11811
     2
  Copyright (C) 1997-2018 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@10464
    76
        cursor = SDL_CreateColorCursor(surface, 0, 0);
slouken@10464
    77
        SDL_FreeSurface(surface);
slouken@10464
    78
    }
slouken@10464
    79
    return cursor;
slouken@10464
    80
}
slouken@10464
    81
slouken@10464
    82
static SDL_Cursor*
brandon@10194
    83
init_system_cursor(const char *image[])
brandon@10194
    84
{
brandon@10194
    85
  int i, row, col;
brandon@10194
    86
  Uint8 data[4*32];
brandon@10194
    87
  Uint8 mask[4*32];
brandon@10194
    88
  int hot_x, hot_y;
brandon@10194
    89
brandon@10194
    90
  i = -1;
brandon@10194
    91
  for (row=0; row<32; ++row) {
brandon@10194
    92
    for (col=0; col<32; ++col) {
brandon@10194
    93
      if (col % 8) {
brandon@10194
    94
        data[i] <<= 1;
brandon@10194
    95
        mask[i] <<= 1;
brandon@10194
    96
      } else {
brandon@10194
    97
        ++i;
brandon@10194
    98
        data[i] = mask[i] = 0;
brandon@10194
    99
      }
brandon@10194
   100
      switch (image[4+row][col]) {
brandon@10194
   101
        case 'X':
brandon@10194
   102
          data[i] |= 0x01;
brandon@10194
   103
          mask[i] |= 0x01;
brandon@10194
   104
          break;
brandon@10194
   105
        case '.':
brandon@10194
   106
          mask[i] |= 0x01;
brandon@10194
   107
          break;
brandon@10194
   108
        case ' ':
brandon@10194
   109
          break;
brandon@10194
   110
      }
brandon@10194
   111
    }
brandon@10194
   112
  }
brandon@10194
   113
  sscanf(image[4+row], "%d,%d", &hot_x, &hot_y);
brandon@10194
   114
  return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y);
brandon@10194
   115
}
brandon@10194
   116
brandon@10194
   117
static SDLTest_CommonState *state;
brandon@10194
   118
int done;
brandon@10194
   119
SDL_Cursor *cursor = NULL;
brandon@10194
   120
brandon@10194
   121
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
brandon@10194
   122
static void
brandon@10194
   123
quit(int rc)
brandon@10194
   124
{
brandon@10194
   125
    SDLTest_CommonQuit(state);
brandon@10194
   126
    exit(rc);
brandon@10194
   127
}
brandon@10194
   128
brandon@10194
   129
void
brandon@10194
   130
loop()
brandon@10194
   131
{
brandon@10194
   132
    int i;
brandon@10194
   133
    SDL_Event event;
brandon@10194
   134
    /* Check for events */
brandon@10194
   135
    while (SDL_PollEvent(&event)) {
brandon@10194
   136
        SDLTest_CommonEvent(state, &event, &done);
brandon@10194
   137
    }
brandon@10194
   138
    
brandon@10194
   139
    for (i = 0; i < state->num_windows; ++i) {
brandon@10194
   140
        SDL_Renderer *renderer = state->renderers[i];
brandon@10194
   141
        SDL_RenderClear(renderer);
brandon@10194
   142
        SDL_RenderPresent(renderer);
brandon@10194
   143
    }
brandon@10194
   144
#ifdef __EMSCRIPTEN__
brandon@10194
   145
    if (done) {
brandon@10194
   146
        emscripten_cancel_main_loop();
brandon@10194
   147
    }
brandon@10194
   148
#endif
brandon@10194
   149
}
brandon@10194
   150
brandon@10194
   151
int
brandon@10194
   152
main(int argc, char *argv[])
brandon@10194
   153
{
brandon@10194
   154
    int i;
slouken@10464
   155
    const char *color_cursor = NULL;
brandon@10194
   156
brandon@10194
   157
    /* Enable standard application logging */
brandon@10194
   158
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
brandon@10194
   159
brandon@10194
   160
    /* Initialize test framework */
brandon@10194
   161
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
brandon@10194
   162
    if (!state) {
brandon@10194
   163
        return 1;
brandon@10194
   164
    }
brandon@10194
   165
    for (i = 1; i < argc;) {
brandon@10194
   166
        int consumed;
brandon@10194
   167
brandon@10194
   168
        consumed = SDLTest_CommonArg(state, i);
brandon@10194
   169
        if (consumed == 0) {
slouken@10464
   170
            color_cursor = argv[i];
slouken@10464
   171
            break;
brandon@10194
   172
        }
brandon@10194
   173
        if (consumed < 0) {
brandon@10194
   174
            SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
brandon@10194
   175
            quit(1);
brandon@10194
   176
        }
brandon@10194
   177
        i += consumed;
brandon@10194
   178
    }
brandon@10194
   179
brandon@10194
   180
    if (!SDLTest_CommonInit(state)) {
brandon@10194
   181
        quit(2);
brandon@10194
   182
    }
brandon@10194
   183
brandon@10195
   184
    for (i = 0; i < state->num_windows; ++i) {
brandon@10195
   185
        SDL_Renderer *renderer = state->renderers[i];
brandon@10195
   186
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
brandon@10195
   187
        SDL_RenderClear(renderer);
brandon@10195
   188
    }
brandon@10194
   189
slouken@10464
   190
    if (color_cursor) {
slouken@10464
   191
        cursor = init_color_cursor(color_cursor);
slouken@10464
   192
    } else {
slouken@10464
   193
        cursor = init_system_cursor(arrow);
slouken@10464
   194
    }
slouken@10464
   195
    if (!cursor) {
slouken@10464
   196
        SDL_Log("Error, couldn't create cursor\n");
slouken@10464
   197
        quit(2);
slouken@10464
   198
    }
brandon@10194
   199
    SDL_SetCursor(cursor);
brandon@10194
   200
brandon@10194
   201
    /* Main render loop */
brandon@10194
   202
    done = 0;
brandon@10194
   203
#ifdef __EMSCRIPTEN__
brandon@10194
   204
    emscripten_set_main_loop(loop, 0, 1);
brandon@10194
   205
#else
brandon@10194
   206
    while (!done) {
brandon@10194
   207
        loop();
brandon@10194
   208
    }
brandon@10194
   209
#endif
brandon@10194
   210
brandon@10194
   211
    SDL_FreeCursor(cursor);
brandon@10194
   212
    quit(0);
brandon@10194
   213
brandon@10194
   214
    /* keep the compiler happy ... */
brandon@10194
   215
    return(0);
brandon@10194
   216
}