test/testwm2.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
slouken@5535
     1
/*
slouken@10737
     2
  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
slouken@5535
     3
slouken@5535
     4
  This software is provided 'as-is', without any express or implied
slouken@5535
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     6
  arising from the use of this software.
slouken@5535
     7
slouken@5535
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
     9
  including commercial applications, and to alter it and redistribute it
slouken@5535
    10
  freely.
slouken@5535
    11
*/
slouken@1895
    12
slouken@1933
    13
#include <stdlib.h>
slouken@1933
    14
#include <stdio.h>
slouken@1895
    15
icculus@9278
    16
#ifdef __EMSCRIPTEN__
icculus@9278
    17
#include <emscripten/emscripten.h>
icculus@9278
    18
#endif
icculus@9278
    19
slouken@6785
    20
#include "SDL_test_common.h"
slouken@1895
    21
slouken@6785
    22
static SDLTest_CommonState *state;
icculus@9278
    23
int done;
slouken@1895
    24
icculus@9278
    25
static const char *cursorNames[] = {
slouken@6677
    26
        "arrow",
slouken@6677
    27
        "ibeam",
slouken@6677
    28
        "wait",
slouken@6677
    29
        "crosshair",
slouken@6677
    30
        "waitarrow",
slouken@6677
    31
        "sizeNWSE",
slouken@6677
    32
        "sizeNESW",
slouken@6677
    33
        "sizeWE",
slouken@6677
    34
        "sizeNS",
slouken@6677
    35
        "sizeALL",
slouken@6677
    36
        "NO",
slouken@6677
    37
        "hand",
icculus@9278
    38
};
icculus@9278
    39
int system_cursor = -1;
icculus@9278
    40
SDL_Cursor *cursor = NULL;
icculus@7034
    41
icculus@9278
    42
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
icculus@9278
    43
static void
icculus@9278
    44
quit(int rc)
icculus@9278
    45
{
icculus@9278
    46
    SDLTest_CommonQuit(state);
icculus@9278
    47
    exit(rc);
icculus@9278
    48
}
slouken@1895
    49
icculus@9278
    50
void
icculus@9278
    51
loop()
icculus@9278
    52
{
brandon@10193
    53
    int i;
icculus@9278
    54
    SDL_Event event;
slouken@1895
    55
        /* Check for events */
slouken@1895
    56
        while (SDL_PollEvent(&event)) {
slouken@6785
    57
            SDLTest_CommonEvent(state, &event, &done);
slouken@5246
    58
slouken@5246
    59
            if (event.type == SDL_WINDOWEVENT) {
slouken@6788
    60
                if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
slouken@6788
    61
                    SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
slouken@6788
    62
                    if (window) {
aschiffler@7639
    63
                        SDL_Log("Window %d resized to %dx%d\n",
slouken@6788
    64
                            event.window.windowID,
slouken@6788
    65
                            event.window.data1,
slouken@6788
    66
                            event.window.data2);
slouken@6788
    67
                    }
slouken@6788
    68
                }
slouken@5246
    69
                if (event.window.event == SDL_WINDOWEVENT_MOVED) {
slouken@5246
    70
                    SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
slouken@5246
    71
                    if (window) {
aschiffler@7639
    72
                        SDL_Log("Window %d moved to %d,%d (display %s)\n",
slouken@5246
    73
                            event.window.windowID,
slouken@5246
    74
                            event.window.data1,
slouken@5246
    75
                            event.window.data2,
slouken@6787
    76
                            SDL_GetDisplayName(SDL_GetWindowDisplayIndex(window)));
slouken@5246
    77
                    }
slouken@5246
    78
                }
slouken@5246
    79
            }
slouken@6677
    80
            if (event.type == SDL_KEYUP) {
slouken@6677
    81
                SDL_bool updateCursor = SDL_FALSE;
slouken@6677
    82
slouken@6677
    83
                if (event.key.keysym.sym == SDLK_LEFT) {
slouken@6677
    84
                    --system_cursor;
slouken@6677
    85
                    if (system_cursor < 0) {
slouken@6677
    86
                        system_cursor = SDL_NUM_SYSTEM_CURSORS - 1;
slouken@6677
    87
                    }
slouken@6677
    88
                    updateCursor = SDL_TRUE;
slouken@6677
    89
                } else if (event.key.keysym.sym == SDLK_RIGHT) {
slouken@6677
    90
                    ++system_cursor;
slouken@6677
    91
                    if (system_cursor >= SDL_NUM_SYSTEM_CURSORS) {
slouken@6677
    92
                        system_cursor = 0;
slouken@6677
    93
                    }
slouken@6677
    94
                    updateCursor = SDL_TRUE;
slouken@6677
    95
                }
slouken@6677
    96
                if (updateCursor) {
slouken@6677
    97
                    SDL_Log("Changing cursor to \"%s\"", cursorNames[system_cursor]);
slouken@6677
    98
                    SDL_FreeCursor(cursor);
slouken@6677
    99
                    cursor = SDL_CreateSystemCursor((SDL_SystemCursor)system_cursor);
slouken@6677
   100
                    SDL_SetCursor(cursor);
slouken@6677
   101
                }
slouken@6677
   102
            }
slouken@1895
   103
        }
brandon@10193
   104
brandon@10193
   105
        for (i = 0; i < state->num_windows; ++i) {
brandon@10193
   106
            SDL_Renderer *renderer = state->renderers[i];
brandon@10193
   107
            SDL_RenderClear(renderer);
brandon@10193
   108
            SDL_RenderPresent(renderer);
brandon@10193
   109
        }
philipp@9607
   110
#ifdef __EMSCRIPTEN__
philipp@9607
   111
    if (done) {
philipp@9607
   112
        emscripten_cancel_main_loop();
philipp@9607
   113
    }
philipp@9607
   114
#endif
icculus@9278
   115
}
icculus@9278
   116
icculus@9278
   117
int
icculus@9278
   118
main(int argc, char *argv[])
icculus@9278
   119
{
icculus@9278
   120
    int i;
icculus@9278
   121
philipp@9922
   122
    /* Enable standard application logging */
icculus@9278
   123
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
icculus@9278
   124
icculus@9278
   125
    SDL_assert(SDL_arraysize(cursorNames) == SDL_NUM_SYSTEM_CURSORS);
icculus@9278
   126
icculus@9278
   127
    /* Initialize test framework */
icculus@9278
   128
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
icculus@9278
   129
    if (!state) {
icculus@9278
   130
        return 1;
slouken@1895
   131
    }
icculus@9278
   132
    for (i = 1; i < argc;) {
icculus@9278
   133
        int consumed;
icculus@9278
   134
icculus@9278
   135
        consumed = SDLTest_CommonArg(state, i);
icculus@9278
   136
        if (consumed == 0) {
icculus@9278
   137
            consumed = -1;
icculus@9278
   138
        }
icculus@9278
   139
        if (consumed < 0) {
icculus@9278
   140
            SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
icculus@9278
   141
            quit(1);
icculus@9278
   142
        }
icculus@9278
   143
        i += consumed;
icculus@9278
   144
    }
icculus@9278
   145
    if (!SDLTest_CommonInit(state)) {
icculus@9278
   146
        quit(2);
icculus@9278
   147
    }
icculus@9278
   148
brandon@10193
   149
    for (i = 0; i < state->num_windows; ++i) {
brandon@10193
   150
        SDL_Renderer *renderer = state->renderers[i];
brandon@10193
   151
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
brandon@10193
   152
        SDL_RenderClear(renderer);
brandon@10193
   153
    }
brandon@10193
   154
 
icculus@9278
   155
    /* Main render loop */
icculus@9278
   156
    done = 0;
icculus@9278
   157
#ifdef __EMSCRIPTEN__
icculus@9278
   158
    emscripten_set_main_loop(loop, 0, 1);
icculus@9278
   159
#else
icculus@9278
   160
    while (!done) {
icculus@9278
   161
        loop();
icculus@9278
   162
    }
icculus@9278
   163
#endif
slouken@6677
   164
    SDL_FreeCursor(cursor);
slouken@6677
   165
slouken@1895
   166
    quit(0);
gabomdq@7663
   167
    /* keep the compiler happy ... */
slouken@7191
   168
    return(0);
slouken@1895
   169
}
slouken@1895
   170
slouken@1895
   171
/* vi: set ts=4 sw=4 expandtab: */