test/testwm2.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 25 Jul 2013 09:51:21 -0700
changeset 7517 965d57022c01
parent 7191 75360622e65f
child 7639 9406b7dd2f2d
permissions -rw-r--r--
Updated the copyright year for the test programs
slouken@5535
     1
/*
slouken@7517
     2
  Copyright (C) 1997-2013 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
slouken@6785
    16
#include "SDL_test_common.h"
slouken@1895
    17
slouken@6785
    18
static SDLTest_CommonState *state;
slouken@1895
    19
slouken@1895
    20
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@1895
    21
static void
slouken@1895
    22
quit(int rc)
slouken@1895
    23
{
slouken@6785
    24
    SDLTest_CommonQuit(state);
slouken@1895
    25
    exit(rc);
slouken@1895
    26
}
slouken@1895
    27
slouken@1895
    28
int
slouken@1895
    29
main(int argc, char *argv[])
slouken@1895
    30
{
slouken@6677
    31
    static const char *cursorNames[] = {
slouken@6677
    32
        "arrow",
slouken@6677
    33
        "ibeam",
slouken@6677
    34
        "wait",
slouken@6677
    35
        "crosshair",
slouken@6677
    36
        "waitarrow",
slouken@6677
    37
        "sizeNWSE",
slouken@6677
    38
        "sizeNESW",
slouken@6677
    39
        "sizeWE",
slouken@6677
    40
        "sizeNS",
slouken@6677
    41
        "sizeALL",
slouken@6677
    42
        "NO",
slouken@6677
    43
        "hand",
slouken@6677
    44
    };
slouken@6677
    45
slouken@1895
    46
    int i, done;
slouken@1895
    47
    SDL_Event event;
slouken@6677
    48
    int system_cursor = -1;
slouken@6677
    49
    SDL_Cursor *cursor = NULL;
slouken@1895
    50
icculus@7034
    51
    SDL_assert(SDL_arraysize(cursorNames) == SDL_NUM_SYSTEM_CURSORS);
icculus@7034
    52
slouken@1933
    53
    /* Initialize test framework */
slouken@6785
    54
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@1933
    55
    if (!state) {
slouken@1933
    56
        return 1;
slouken@1933
    57
    }
slouken@1933
    58
    state->skip_renderer = SDL_TRUE;
slouken@1933
    59
    for (i = 1; i < argc;) {
slouken@1933
    60
        int consumed;
slouken@1933
    61
slouken@6785
    62
        consumed = SDLTest_CommonArg(state, i);
slouken@1933
    63
        if (consumed == 0) {
slouken@1933
    64
            consumed = -1;
slouken@1933
    65
        }
slouken@1933
    66
        if (consumed < 0) {
slouken@6785
    67
            fprintf(stderr, "Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
slouken@1933
    68
            quit(1);
slouken@1933
    69
        }
slouken@1933
    70
        i += consumed;
slouken@1933
    71
    }
slouken@6785
    72
    if (!SDLTest_CommonInit(state)) {
slouken@1933
    73
        quit(2);
slouken@1895
    74
    }
slouken@1895
    75
slouken@1933
    76
    /* Main render loop */
slouken@1895
    77
    done = 0;
slouken@1895
    78
    while (!done) {
slouken@1895
    79
        /* Check for events */
slouken@1895
    80
        while (SDL_PollEvent(&event)) {
slouken@6785
    81
            SDLTest_CommonEvent(state, &event, &done);
slouken@5246
    82
slouken@5246
    83
            if (event.type == SDL_WINDOWEVENT) {
slouken@6788
    84
                if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
slouken@6788
    85
                    SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
slouken@6788
    86
                    if (window) {
slouken@6788
    87
                        printf("Window %d resized to %dx%d\n",
slouken@6788
    88
                            event.window.windowID,
slouken@6788
    89
                            event.window.data1,
slouken@6788
    90
                            event.window.data2);
slouken@6788
    91
                    }
slouken@6788
    92
                }
slouken@5246
    93
                if (event.window.event == SDL_WINDOWEVENT_MOVED) {
slouken@5246
    94
                    SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
slouken@5246
    95
                    if (window) {
slouken@6787
    96
                        printf("Window %d moved to %d,%d (display %s)\n",
slouken@5246
    97
                            event.window.windowID,
slouken@5246
    98
                            event.window.data1,
slouken@5246
    99
                            event.window.data2,
slouken@6787
   100
                            SDL_GetDisplayName(SDL_GetWindowDisplayIndex(window)));
slouken@5246
   101
                    }
slouken@5246
   102
                }
slouken@5246
   103
            }
slouken@6677
   104
            if (event.type == SDL_KEYUP) {
slouken@6677
   105
                SDL_bool updateCursor = SDL_FALSE;
slouken@6677
   106
slouken@6677
   107
                if (event.key.keysym.sym == SDLK_LEFT) {
slouken@6677
   108
                    --system_cursor;
slouken@6677
   109
                    if (system_cursor < 0) {
slouken@6677
   110
                        system_cursor = SDL_NUM_SYSTEM_CURSORS - 1;
slouken@6677
   111
                    }
slouken@6677
   112
                    updateCursor = SDL_TRUE;
slouken@6677
   113
                } else if (event.key.keysym.sym == SDLK_RIGHT) {
slouken@6677
   114
                    ++system_cursor;
slouken@6677
   115
                    if (system_cursor >= SDL_NUM_SYSTEM_CURSORS) {
slouken@6677
   116
                        system_cursor = 0;
slouken@6677
   117
                    }
slouken@6677
   118
                    updateCursor = SDL_TRUE;
slouken@6677
   119
                }
slouken@6677
   120
                if (updateCursor) {
slouken@6677
   121
                    SDL_Log("Changing cursor to \"%s\"", cursorNames[system_cursor]);
slouken@6677
   122
                    SDL_FreeCursor(cursor);
slouken@6677
   123
                    cursor = SDL_CreateSystemCursor((SDL_SystemCursor)system_cursor);
slouken@6677
   124
                    SDL_SetCursor(cursor);
slouken@6677
   125
                }
slouken@6677
   126
            }
slouken@1895
   127
        }
slouken@1895
   128
    }
slouken@6677
   129
    SDL_FreeCursor(cursor);
slouken@6677
   130
slouken@1895
   131
    quit(0);
slouken@7191
   132
    // keep the compiler happy ...
slouken@7191
   133
    return(0);
slouken@1895
   134
}
slouken@1895
   135
slouken@1895
   136
/* vi: set ts=4 sw=4 expandtab: */