test/testwm2.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Wed, 25 Nov 2015 21:39:28 +0100
changeset 9922 b066d5fd5fa7
parent 9619 b94b6d0bff0f
child 9998 f67cf37e9cd4
permissions -rw-r--r--
Replaced tabs with spaces in test programs.
slouken@5535
     1
/*
slouken@9619
     2
  Copyright (C) 1997-2015 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;
slouken@6677
    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
}
icculus@9278
    49
icculus@9278
    50
void
icculus@9278
    51
loop()
icculus@9278
    52
{
slouken@1895
    53
    SDL_Event event;
slouken@1895
    54
        /* Check for events */
slouken@1895
    55
        while (SDL_PollEvent(&event)) {
slouken@6785
    56
            SDLTest_CommonEvent(state, &event, &done);
slouken@5246
    57
slouken@5246
    58
            if (event.type == SDL_WINDOWEVENT) {
slouken@6788
    59
                if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
slouken@6788
    60
                    SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
slouken@6788
    61
                    if (window) {
aschiffler@7639
    62
                        SDL_Log("Window %d resized to %dx%d\n",
slouken@6788
    63
                            event.window.windowID,
slouken@6788
    64
                            event.window.data1,
slouken@6788
    65
                            event.window.data2);
slouken@6788
    66
                    }
slouken@6788
    67
                }
slouken@5246
    68
                if (event.window.event == SDL_WINDOWEVENT_MOVED) {
slouken@5246
    69
                    SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
slouken@5246
    70
                    if (window) {
aschiffler@7639
    71
                        SDL_Log("Window %d moved to %d,%d (display %s)\n",
slouken@5246
    72
                            event.window.windowID,
slouken@5246
    73
                            event.window.data1,
slouken@5246
    74
                            event.window.data2,
slouken@6787
    75
                            SDL_GetDisplayName(SDL_GetWindowDisplayIndex(window)));
slouken@5246
    76
                    }
slouken@5246
    77
                }
slouken@5246
    78
            }
slouken@6677
    79
            if (event.type == SDL_KEYUP) {
slouken@6677
    80
                SDL_bool updateCursor = SDL_FALSE;
slouken@6677
    81
slouken@6677
    82
                if (event.key.keysym.sym == SDLK_LEFT) {
slouken@6677
    83
                    --system_cursor;
slouken@6677
    84
                    if (system_cursor < 0) {
slouken@6677
    85
                        system_cursor = SDL_NUM_SYSTEM_CURSORS - 1;
slouken@6677
    86
                    }
slouken@6677
    87
                    updateCursor = SDL_TRUE;
slouken@6677
    88
                } else if (event.key.keysym.sym == SDLK_RIGHT) {
slouken@6677
    89
                    ++system_cursor;
slouken@6677
    90
                    if (system_cursor >= SDL_NUM_SYSTEM_CURSORS) {
slouken@6677
    91
                        system_cursor = 0;
slouken@6677
    92
                    }
slouken@6677
    93
                    updateCursor = SDL_TRUE;
slouken@6677
    94
                }
slouken@6677
    95
                if (updateCursor) {
slouken@6677
    96
                    SDL_Log("Changing cursor to \"%s\"", cursorNames[system_cursor]);
slouken@6677
    97
                    SDL_FreeCursor(cursor);
slouken@6677
    98
                    cursor = SDL_CreateSystemCursor((SDL_SystemCursor)system_cursor);
slouken@6677
    99
                    SDL_SetCursor(cursor);
slouken@6677
   100
                }
slouken@6677
   101
            }
slouken@1895
   102
        }
philipp@9607
   103
#ifdef __EMSCRIPTEN__
philipp@9607
   104
    if (done) {
philipp@9607
   105
        emscripten_cancel_main_loop();
philipp@9607
   106
    }
philipp@9607
   107
#endif
icculus@9278
   108
}
icculus@9278
   109
icculus@9278
   110
int
icculus@9278
   111
main(int argc, char *argv[])
icculus@9278
   112
{
icculus@9278
   113
    int i;
icculus@9278
   114
philipp@9922
   115
    /* Enable standard application logging */
icculus@9278
   116
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
icculus@9278
   117
icculus@9278
   118
    SDL_assert(SDL_arraysize(cursorNames) == SDL_NUM_SYSTEM_CURSORS);
icculus@9278
   119
icculus@9278
   120
    /* Initialize test framework */
icculus@9278
   121
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
icculus@9278
   122
    if (!state) {
icculus@9278
   123
        return 1;
slouken@1895
   124
    }
icculus@9278
   125
    state->skip_renderer = SDL_TRUE;
icculus@9278
   126
    for (i = 1; i < argc;) {
icculus@9278
   127
        int consumed;
icculus@9278
   128
icculus@9278
   129
        consumed = SDLTest_CommonArg(state, i);
icculus@9278
   130
        if (consumed == 0) {
icculus@9278
   131
            consumed = -1;
icculus@9278
   132
        }
icculus@9278
   133
        if (consumed < 0) {
icculus@9278
   134
            SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
icculus@9278
   135
            quit(1);
icculus@9278
   136
        }
icculus@9278
   137
        i += consumed;
icculus@9278
   138
    }
icculus@9278
   139
    if (!SDLTest_CommonInit(state)) {
icculus@9278
   140
        quit(2);
icculus@9278
   141
    }
icculus@9278
   142
icculus@9278
   143
    /* Main render loop */
icculus@9278
   144
    done = 0;
icculus@9278
   145
#ifdef __EMSCRIPTEN__
icculus@9278
   146
    emscripten_set_main_loop(loop, 0, 1);
icculus@9278
   147
#else
icculus@9278
   148
    while (!done) {
icculus@9278
   149
        loop();
icculus@9278
   150
    }
icculus@9278
   151
#endif
slouken@6677
   152
    SDL_FreeCursor(cursor);
slouken@6677
   153
slouken@1895
   154
    quit(0);
gabomdq@7663
   155
    /* keep the compiler happy ... */
slouken@7191
   156
    return(0);
slouken@1895
   157
}
slouken@1895
   158
slouken@1895
   159
/* vi: set ts=4 sw=4 expandtab: */