test/testcustomcursor.c
author Brandon Schaefer <brandon.schaefer@canonical.com>
Wed, 13 Jul 2016 07:34:06 -0700
changeset 10194 a4eb7861e89c
child 10195 23fb5052dab4
permissions -rw-r--r--
Tests: Add a manual test for a custom cursor (Taken from the API docs)
brandon@10194
     1
/*
brandon@10194
     2
  Copyright (C) 1997-2016 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*
brandon@10194
    71
init_system_cursor(const char *image[])
brandon@10194
    72
{
brandon@10194
    73
  int i, row, col;
brandon@10194
    74
  Uint8 data[4*32];
brandon@10194
    75
  Uint8 mask[4*32];
brandon@10194
    76
  int hot_x, hot_y;
brandon@10194
    77
brandon@10194
    78
  i = -1;
brandon@10194
    79
  for (row=0; row<32; ++row) {
brandon@10194
    80
    for (col=0; col<32; ++col) {
brandon@10194
    81
      if (col % 8) {
brandon@10194
    82
        data[i] <<= 1;
brandon@10194
    83
        mask[i] <<= 1;
brandon@10194
    84
      } else {
brandon@10194
    85
        ++i;
brandon@10194
    86
        data[i] = mask[i] = 0;
brandon@10194
    87
      }
brandon@10194
    88
      switch (image[4+row][col]) {
brandon@10194
    89
        case 'X':
brandon@10194
    90
          data[i] |= 0x01;
brandon@10194
    91
          mask[i] |= 0x01;
brandon@10194
    92
          break;
brandon@10194
    93
        case '.':
brandon@10194
    94
          mask[i] |= 0x01;
brandon@10194
    95
          break;
brandon@10194
    96
        case ' ':
brandon@10194
    97
          break;
brandon@10194
    98
      }
brandon@10194
    99
    }
brandon@10194
   100
  }
brandon@10194
   101
  sscanf(image[4+row], "%d,%d", &hot_x, &hot_y);
brandon@10194
   102
  return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y);
brandon@10194
   103
}
brandon@10194
   104
brandon@10194
   105
static SDLTest_CommonState *state;
brandon@10194
   106
int done;
brandon@10194
   107
SDL_Cursor *cursor = NULL;
brandon@10194
   108
brandon@10194
   109
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
brandon@10194
   110
static void
brandon@10194
   111
quit(int rc)
brandon@10194
   112
{
brandon@10194
   113
    SDLTest_CommonQuit(state);
brandon@10194
   114
    exit(rc);
brandon@10194
   115
}
brandon@10194
   116
brandon@10194
   117
void
brandon@10194
   118
loop()
brandon@10194
   119
{
brandon@10194
   120
    int i;
brandon@10194
   121
    SDL_Event event;
brandon@10194
   122
    /* Check for events */
brandon@10194
   123
    while (SDL_PollEvent(&event)) {
brandon@10194
   124
        SDLTest_CommonEvent(state, &event, &done);
brandon@10194
   125
    }
brandon@10194
   126
    
brandon@10194
   127
    for (i = 0; i < state->num_windows; ++i) {
brandon@10194
   128
        SDL_Renderer *renderer = state->renderers[i];
brandon@10194
   129
        SDL_RenderClear(renderer);
brandon@10194
   130
        SDL_RenderPresent(renderer);
brandon@10194
   131
    }
brandon@10194
   132
#ifdef __EMSCRIPTEN__
brandon@10194
   133
    if (done) {
brandon@10194
   134
        emscripten_cancel_main_loop();
brandon@10194
   135
    }
brandon@10194
   136
#endif
brandon@10194
   137
}
brandon@10194
   138
brandon@10194
   139
int
brandon@10194
   140
main(int argc, char *argv[])
brandon@10194
   141
{
brandon@10194
   142
    int i;
brandon@10194
   143
brandon@10194
   144
    /* Enable standard application logging */
brandon@10194
   145
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
brandon@10194
   146
brandon@10194
   147
    /* Initialize test framework */
brandon@10194
   148
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
brandon@10194
   149
    if (!state) {
brandon@10194
   150
        return 1;
brandon@10194
   151
    }
brandon@10194
   152
    for (i = 1; i < argc;) {
brandon@10194
   153
        int consumed;
brandon@10194
   154
brandon@10194
   155
        consumed = SDLTest_CommonArg(state, i);
brandon@10194
   156
        if (consumed == 0) {
brandon@10194
   157
            consumed = -1;
brandon@10194
   158
        }
brandon@10194
   159
        if (consumed < 0) {
brandon@10194
   160
            SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
brandon@10194
   161
            quit(1);
brandon@10194
   162
        }
brandon@10194
   163
        i += consumed;
brandon@10194
   164
    }
brandon@10194
   165
brandon@10194
   166
    if (!SDLTest_CommonInit(state)) {
brandon@10194
   167
        quit(2);
brandon@10194
   168
    }
brandon@10194
   169
brandon@10194
   170
	for (i = 0; i < state->num_windows; ++i) {
brandon@10194
   171
		SDL_Renderer *renderer = state->renderers[i];
brandon@10194
   172
		SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
brandon@10194
   173
		SDL_RenderClear(renderer);
brandon@10194
   174
	}
brandon@10194
   175
brandon@10194
   176
    cursor = init_system_cursor(arrow);
brandon@10194
   177
    SDL_SetCursor(cursor);
brandon@10194
   178
brandon@10194
   179
    /* Main render loop */
brandon@10194
   180
    done = 0;
brandon@10194
   181
#ifdef __EMSCRIPTEN__
brandon@10194
   182
    emscripten_set_main_loop(loop, 0, 1);
brandon@10194
   183
#else
brandon@10194
   184
    while (!done) {
brandon@10194
   185
        loop();
brandon@10194
   186
    }
brandon@10194
   187
#endif
brandon@10194
   188
brandon@10194
   189
    SDL_FreeCursor(cursor);
brandon@10194
   190
    quit(0);
brandon@10194
   191
brandon@10194
   192
    /* keep the compiler happy ... */
brandon@10194
   193
    return(0);
brandon@10194
   194
}