Tests: Add a manual test for a custom cursor (Taken from the API docs)
authorBrandon Schaefer <brandon.schaefer@canonical.com>
Wed, 13 Jul 2016 07:34:06 -0700
changeset 10194a4eb7861e89c
parent 10193 75cd0fc3fec2
child 10195 23fb5052dab4
Tests: Add a manual test for a custom cursor (Taken from the API docs)
test/testcustomcursor.c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testcustomcursor.c	Wed Jul 13 07:34:06 2016 -0700
     1.3 @@ -0,0 +1,194 @@
     1.4 +/*
     1.5 +  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
     1.6 +
     1.7 +  This software is provided 'as-is', without any express or implied
     1.8 +  warranty.  In no event will the authors be held liable for any damages
     1.9 +  arising from the use of this software.
    1.10 +
    1.11 +  Permission is granted to anyone to use this software for any purpose,
    1.12 +  including commercial applications, and to alter it and redistribute it
    1.13 +  freely.
    1.14 +*/
    1.15 +
    1.16 +#include <stdlib.h>
    1.17 +#include <stdio.h>
    1.18 +
    1.19 +#ifdef __EMSCRIPTEN__
    1.20 +#include <emscripten/emscripten.h>
    1.21 +#endif
    1.22 +
    1.23 +#include "SDL_test_common.h"
    1.24 +
    1.25 +/* Stolen from the mailing list */
    1.26 +/* Creates a new mouse cursor from an XPM */
    1.27 +
    1.28 +
    1.29 +/* XPM */
    1.30 +static const char *arrow[] = {
    1.31 +  /* width height num_colors chars_per_pixel */
    1.32 +  "    32    32        3            1",
    1.33 +  /* colors */
    1.34 +  "X c #000000",
    1.35 +  ". c #ffffff",
    1.36 +  "  c None",
    1.37 +  /* pixels */
    1.38 +  "X                               ",
    1.39 +  "XX                              ",
    1.40 +  "X.X                             ",
    1.41 +  "X..X                            ",
    1.42 +  "X...X                           ",
    1.43 +  "X....X                          ",
    1.44 +  "X.....X                         ",
    1.45 +  "X......X                        ",
    1.46 +  "X.......X                       ",
    1.47 +  "X........X                      ",
    1.48 +  "X.....XXXXX                     ",
    1.49 +  "X..X..X                         ",
    1.50 +  "X.X X..X                        ",
    1.51 +  "XX  X..X                        ",
    1.52 +  "X    X..X                       ",
    1.53 +  "     X..X                       ",
    1.54 +  "      X..X                      ",
    1.55 +  "      X..X                      ",
    1.56 +  "       XX                       ",
    1.57 +  "                                ",
    1.58 +  "                                ",
    1.59 +  "                                ",
    1.60 +  "                                ",
    1.61 +  "                                ",
    1.62 +  "                                ",
    1.63 +  "                                ",
    1.64 +  "                                ",
    1.65 +  "                                ",
    1.66 +  "                                ",
    1.67 +  "                                ",
    1.68 +  "                                ",
    1.69 +  "                                ",
    1.70 +  "0,0"
    1.71 +};  
    1.72 +
    1.73 +static SDL_Cursor*
    1.74 +init_system_cursor(const char *image[])
    1.75 +{
    1.76 +  int i, row, col;
    1.77 +  Uint8 data[4*32];
    1.78 +  Uint8 mask[4*32];
    1.79 +  int hot_x, hot_y;
    1.80 +
    1.81 +  i = -1;
    1.82 +  for (row=0; row<32; ++row) {
    1.83 +    for (col=0; col<32; ++col) {
    1.84 +      if (col % 8) {
    1.85 +        data[i] <<= 1;
    1.86 +        mask[i] <<= 1;
    1.87 +      } else {
    1.88 +        ++i;
    1.89 +        data[i] = mask[i] = 0;
    1.90 +      }
    1.91 +      switch (image[4+row][col]) {
    1.92 +        case 'X':
    1.93 +          data[i] |= 0x01;
    1.94 +          mask[i] |= 0x01;
    1.95 +          break;
    1.96 +        case '.':
    1.97 +          mask[i] |= 0x01;
    1.98 +          break;
    1.99 +        case ' ':
   1.100 +          break;
   1.101 +      }
   1.102 +    }
   1.103 +  }
   1.104 +  sscanf(image[4+row], "%d,%d", &hot_x, &hot_y);
   1.105 +  return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y);
   1.106 +}
   1.107 +
   1.108 +static SDLTest_CommonState *state;
   1.109 +int done;
   1.110 +SDL_Cursor *cursor = NULL;
   1.111 +
   1.112 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
   1.113 +static void
   1.114 +quit(int rc)
   1.115 +{
   1.116 +    SDLTest_CommonQuit(state);
   1.117 +    exit(rc);
   1.118 +}
   1.119 +
   1.120 +void
   1.121 +loop()
   1.122 +{
   1.123 +    int i;
   1.124 +    SDL_Event event;
   1.125 +    /* Check for events */
   1.126 +    while (SDL_PollEvent(&event)) {
   1.127 +        SDLTest_CommonEvent(state, &event, &done);
   1.128 +    }
   1.129 +    
   1.130 +    for (i = 0; i < state->num_windows; ++i) {
   1.131 +        SDL_Renderer *renderer = state->renderers[i];
   1.132 +        SDL_RenderClear(renderer);
   1.133 +        SDL_RenderPresent(renderer);
   1.134 +    }
   1.135 +#ifdef __EMSCRIPTEN__
   1.136 +    if (done) {
   1.137 +        emscripten_cancel_main_loop();
   1.138 +    }
   1.139 +#endif
   1.140 +}
   1.141 +
   1.142 +int
   1.143 +main(int argc, char *argv[])
   1.144 +{
   1.145 +    int i;
   1.146 +
   1.147 +    /* Enable standard application logging */
   1.148 +    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
   1.149 +
   1.150 +    /* Initialize test framework */
   1.151 +    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
   1.152 +    if (!state) {
   1.153 +        return 1;
   1.154 +    }
   1.155 +    for (i = 1; i < argc;) {
   1.156 +        int consumed;
   1.157 +
   1.158 +        consumed = SDLTest_CommonArg(state, i);
   1.159 +        if (consumed == 0) {
   1.160 +            consumed = -1;
   1.161 +        }
   1.162 +        if (consumed < 0) {
   1.163 +            SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
   1.164 +            quit(1);
   1.165 +        }
   1.166 +        i += consumed;
   1.167 +    }
   1.168 +
   1.169 +    if (!SDLTest_CommonInit(state)) {
   1.170 +        quit(2);
   1.171 +    }
   1.172 +
   1.173 +	for (i = 0; i < state->num_windows; ++i) {
   1.174 +		SDL_Renderer *renderer = state->renderers[i];
   1.175 +		SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
   1.176 +		SDL_RenderClear(renderer);
   1.177 +	}
   1.178 +
   1.179 +    cursor = init_system_cursor(arrow);
   1.180 +    SDL_SetCursor(cursor);
   1.181 +
   1.182 +    /* Main render loop */
   1.183 +    done = 0;
   1.184 +#ifdef __EMSCRIPTEN__
   1.185 +    emscripten_set_main_loop(loop, 0, 1);
   1.186 +#else
   1.187 +    while (!done) {
   1.188 +        loop();
   1.189 +    }
   1.190 +#endif
   1.191 +
   1.192 +    SDL_FreeCursor(cursor);
   1.193 +    quit(0);
   1.194 +
   1.195 +    /* keep the compiler happy ... */
   1.196 +    return(0);
   1.197 +}