test/testcustomcursor.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 07 Dec 2017 16:08:09 -0800
changeset 11730 ac6c607e065c
parent 10737 3406a0f8b041
child 11811 5d94cb6b24d3
permissions -rw-r--r--
Enable building the Metal renderer by default, and weak link the Metal framework so the SDL library is safe to use on older Macs
Also generate iOS versions of the Metal shaders
     1 /*
     2   Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 
    13 #include <stdlib.h>
    14 #include <stdio.h>
    15 
    16 #ifdef __EMSCRIPTEN__
    17 #include <emscripten/emscripten.h>
    18 #endif
    19 
    20 #include "SDL_test_common.h"
    21 
    22 /* Stolen from the mailing list */
    23 /* Creates a new mouse cursor from an XPM */
    24 
    25 
    26 /* XPM */
    27 static const char *arrow[] = {
    28   /* width height num_colors chars_per_pixel */
    29   "    32    32        3            1",
    30   /* colors */
    31   "X c #000000",
    32   ". c #ffffff",
    33   "  c None",
    34   /* pixels */
    35   "X                               ",
    36   "XX                              ",
    37   "X.X                             ",
    38   "X..X                            ",
    39   "X...X                           ",
    40   "X....X                          ",
    41   "X.....X                         ",
    42   "X......X                        ",
    43   "X.......X                       ",
    44   "X........X                      ",
    45   "X.....XXXXX                     ",
    46   "X..X..X                         ",
    47   "X.X X..X                        ",
    48   "XX  X..X                        ",
    49   "X    X..X                       ",
    50   "     X..X                       ",
    51   "      X..X                      ",
    52   "      X..X                      ",
    53   "       XX                       ",
    54   "                                ",
    55   "                                ",
    56   "                                ",
    57   "                                ",
    58   "                                ",
    59   "                                ",
    60   "                                ",
    61   "                                ",
    62   "                                ",
    63   "                                ",
    64   "                                ",
    65   "                                ",
    66   "                                ",
    67   "0,0"
    68 };  
    69 
    70 static SDL_Cursor*
    71 init_color_cursor(const char *file)
    72 {
    73     SDL_Cursor *cursor = NULL;
    74     SDL_Surface *surface = SDL_LoadBMP(file);
    75     if (surface) {
    76         cursor = SDL_CreateColorCursor(surface, 0, 0);
    77         SDL_FreeSurface(surface);
    78     }
    79     return cursor;
    80 }
    81 
    82 static SDL_Cursor*
    83 init_system_cursor(const char *image[])
    84 {
    85   int i, row, col;
    86   Uint8 data[4*32];
    87   Uint8 mask[4*32];
    88   int hot_x, hot_y;
    89 
    90   i = -1;
    91   for (row=0; row<32; ++row) {
    92     for (col=0; col<32; ++col) {
    93       if (col % 8) {
    94         data[i] <<= 1;
    95         mask[i] <<= 1;
    96       } else {
    97         ++i;
    98         data[i] = mask[i] = 0;
    99       }
   100       switch (image[4+row][col]) {
   101         case 'X':
   102           data[i] |= 0x01;
   103           mask[i] |= 0x01;
   104           break;
   105         case '.':
   106           mask[i] |= 0x01;
   107           break;
   108         case ' ':
   109           break;
   110       }
   111     }
   112   }
   113   sscanf(image[4+row], "%d,%d", &hot_x, &hot_y);
   114   return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y);
   115 }
   116 
   117 static SDLTest_CommonState *state;
   118 int done;
   119 SDL_Cursor *cursor = NULL;
   120 
   121 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
   122 static void
   123 quit(int rc)
   124 {
   125     SDLTest_CommonQuit(state);
   126     exit(rc);
   127 }
   128 
   129 void
   130 loop()
   131 {
   132     int i;
   133     SDL_Event event;
   134     /* Check for events */
   135     while (SDL_PollEvent(&event)) {
   136         SDLTest_CommonEvent(state, &event, &done);
   137     }
   138     
   139     for (i = 0; i < state->num_windows; ++i) {
   140         SDL_Renderer *renderer = state->renderers[i];
   141         SDL_RenderClear(renderer);
   142         SDL_RenderPresent(renderer);
   143     }
   144 #ifdef __EMSCRIPTEN__
   145     if (done) {
   146         emscripten_cancel_main_loop();
   147     }
   148 #endif
   149 }
   150 
   151 int
   152 main(int argc, char *argv[])
   153 {
   154     int i;
   155     const char *color_cursor = NULL;
   156 
   157     /* Enable standard application logging */
   158     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
   159 
   160     /* Initialize test framework */
   161     state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
   162     if (!state) {
   163         return 1;
   164     }
   165     for (i = 1; i < argc;) {
   166         int consumed;
   167 
   168         consumed = SDLTest_CommonArg(state, i);
   169         if (consumed == 0) {
   170             color_cursor = argv[i];
   171             break;
   172         }
   173         if (consumed < 0) {
   174             SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
   175             quit(1);
   176         }
   177         i += consumed;
   178     }
   179 
   180     if (!SDLTest_CommonInit(state)) {
   181         quit(2);
   182     }
   183 
   184     for (i = 0; i < state->num_windows; ++i) {
   185         SDL_Renderer *renderer = state->renderers[i];
   186         SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
   187         SDL_RenderClear(renderer);
   188     }
   189 
   190     if (color_cursor) {
   191         cursor = init_color_cursor(color_cursor);
   192     } else {
   193         cursor = init_system_cursor(arrow);
   194     }
   195     if (!cursor) {
   196         SDL_Log("Error, couldn't create cursor\n");
   197         quit(2);
   198     }
   199     SDL_SetCursor(cursor);
   200 
   201     /* Main render loop */
   202     done = 0;
   203 #ifdef __EMSCRIPTEN__
   204     emscripten_set_main_loop(loop, 0, 1);
   205 #else
   206     while (!done) {
   207         loop();
   208     }
   209 #endif
   210 
   211     SDL_FreeCursor(cursor);
   212     quit(0);
   213 
   214     /* keep the compiler happy ... */
   215     return(0);
   216 }