test/testrelative.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sat, 07 Feb 2015 22:40:36 +0100
changeset 9333 de02c2d8a06f
parent 9278 8900afb78a19
child 9607 7746ab058d12
permissions -rw-r--r--
Fixed bug 2866 - testrelative.c: patch to make the orange box wrap around

Eric Wasylishen

Here's a patch to make the 'testrelative' demo program more useful: it just makes the orange rectangle wrap around. Previously, the orange cursor would just disappear off screen if you move the mouse a lot in one direction, so it was hard to tell if relative mouse mode was still working.
slouken@6311
     1
/*
slouken@8149
     2
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
slouken@6311
     3
slouken@6311
     4
  This software is provided 'as-is', without any express or implied
slouken@6311
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@6311
     6
  arising from the use of this software.
slouken@6311
     7
slouken@6311
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@6311
     9
  including commercial applications, and to alter it and redistribute it
slouken@6311
    10
  freely.
slouken@6311
    11
*/
slouken@6311
    12
slouken@6311
    13
/* Simple program:  Test relative mouse motion */
slouken@6311
    14
slouken@6311
    15
#include <stdlib.h>
slouken@6311
    16
#include <stdio.h>
slouken@6311
    17
#include <time.h>
slouken@6311
    18
slouken@6785
    19
#include "SDL_test_common.h"
slouken@6311
    20
icculus@9278
    21
#ifdef __EMSCRIPTEN__
icculus@9278
    22
#include <emscripten/emscripten.h>
icculus@9278
    23
#endif
slouken@6311
    24
slouken@6785
    25
static SDLTest_CommonState *state;
icculus@9278
    26
int i, done;
icculus@9278
    27
SDL_Rect rect;
icculus@9278
    28
SDL_Event event;
slouken@6311
    29
slouken@6311
    30
static void
philipp@8724
    31
DrawRects(SDL_Renderer * renderer, SDL_Rect * rect)
slouken@6311
    32
{
slouken@6311
    33
    SDL_SetRenderDrawColor(renderer, 255, 127, 0, 255);
philipp@8724
    34
    SDL_RenderFillRect(renderer, rect);
slouken@6311
    35
}
slouken@6311
    36
icculus@9278
    37
static void
icculus@9278
    38
loop(){
icculus@9278
    39
    /* Check for events */
icculus@9278
    40
    while (SDL_PollEvent(&event)) {
icculus@9278
    41
        SDLTest_CommonEvent(state, &event, &done);
icculus@9278
    42
        switch(event.type) {
icculus@9278
    43
        case SDL_MOUSEMOTION:
icculus@9278
    44
            {
icculus@9278
    45
                rect.x += event.motion.xrel;
icculus@9278
    46
                rect.y += event.motion.yrel;
icculus@9278
    47
            }
icculus@9278
    48
            break;
icculus@9278
    49
        }
icculus@9278
    50
    }
icculus@9278
    51
    for (i = 0; i < state->num_windows; ++i) {
philipp@9333
    52
        SDL_Rect viewport;
icculus@9278
    53
        SDL_Renderer *renderer = state->renderers[i];
icculus@9278
    54
        if (state->windows[i] == NULL)
icculus@9278
    55
            continue;
icculus@9278
    56
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
icculus@9278
    57
        SDL_RenderClear(renderer);
icculus@9278
    58
philipp@9333
    59
        /* Wrap the cursor rectangle at the screen edges to keep it visible */
philipp@9333
    60
        SDL_RenderGetViewport(renderer, &viewport);
philipp@9333
    61
        if (rect.x < viewport.x) rect.x += viewport.w;
philipp@9333
    62
        if (rect.y < viewport.y) rect.y += viewport.h;
philipp@9333
    63
        if (rect.x > viewport.x + viewport.w) rect.x -= viewport.w;
philipp@9333
    64
        if (rect.y > viewport.y + viewport.h) rect.y -= viewport.h;
philipp@9333
    65
icculus@9278
    66
        DrawRects(renderer, &rect);
icculus@9278
    67
icculus@9278
    68
        SDL_RenderPresent(renderer);
icculus@9278
    69
    }
icculus@9278
    70
}
icculus@9278
    71
slouken@6311
    72
int
slouken@6311
    73
main(int argc, char *argv[])
slouken@6311
    74
{
slouken@6311
    75
philipp@8724
    76
    /* Enable standard application logging */
aschiffler@7639
    77
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
slouken@7191
    78
slouken@6311
    79
    /* Initialize test framework */
slouken@6785
    80
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@6311
    81
    if (!state) {
slouken@6311
    82
        return 1;
slouken@6311
    83
    }
philipp@8724
    84
    for (i = 1; i < argc; ++i) {
slouken@6785
    85
        SDLTest_CommonArg(state, i);
dimitris@6318
    86
    }
slouken@6785
    87
    if (!SDLTest_CommonInit(state)) {
slouken@6311
    88
        return 2;
slouken@6311
    89
    }
slouken@6311
    90
slouken@6311
    91
    /* Create the windows and initialize the renderers */
slouken@6311
    92
    for (i = 0; i < state->num_windows; ++i) {
slouken@6311
    93
        SDL_Renderer *renderer = state->renderers[i];
slouken@6311
    94
        SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
slouken@6311
    95
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
slouken@6311
    96
        SDL_RenderClear(renderer);
slouken@6311
    97
    }
slouken@6311
    98
slouken@6311
    99
    srand((unsigned int)time(NULL));
dimitris@6316
   100
    if(SDL_SetRelativeMouseMode(SDL_TRUE) < 0) {
dimitris@6316
   101
        return 3;
dimitris@6316
   102
    };
slouken@6311
   103
slouken@6311
   104
    rect.x = DEFAULT_WINDOW_WIDTH / 2;
slouken@6311
   105
    rect.y = DEFAULT_WINDOW_HEIGHT / 2;
slouken@6311
   106
    rect.w = 10;
slouken@6311
   107
    rect.h = 10;
slouken@6311
   108
    /* Main render loop */
slouken@6311
   109
    done = 0;
icculus@9278
   110
#ifdef __EMSCRIPTEN__
icculus@9278
   111
    emscripten_set_main_loop(loop, 0, 1);
icculus@9278
   112
#else
slouken@6311
   113
    while (!done) {
icculus@9278
   114
        loop();
slouken@6311
   115
        }
icculus@9278
   116
#endif
slouken@6785
   117
    SDLTest_CommonQuit(state);
slouken@6311
   118
    return 0;
slouken@6311
   119
}
slouken@6311
   120
slouken@6311
   121
/* vi: set ts=4 sw=4 expandtab: */