test/testrelative.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 12 Aug 2016 19:59:00 -0400
changeset 10266 c09f06c4e8c8
parent 9998 f67cf37e9cd4
permissions -rw-r--r--
emscripten: send fake mouse events for touches, like other targets do. (This really should be handled at the higher level and not in the individual targets, but this fixes the immediate bug.)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6311
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
9998
f67cf37e9cd4 Updated copyright to 2016
Sam Lantinga <slouken@libsdl.org>
parents: 9627
diff changeset
     2
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
6311
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     3
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
  This software is provided 'as-is', without any express or implied
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     5
  warranty.  In no event will the authors be held liable for any damages
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     6
  arising from the use of this software.
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     7
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
  Permission is granted to anyone to use this software for any purpose,
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     9
  including commercial applications, and to alter it and redistribute it
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    10
  freely.
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    11
*/
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    13
/* Simple program:  Test relative mouse motion */
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    14
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    15
#include <stdlib.h>
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    16
#include <stdio.h>
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    17
#include <time.h>
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    18
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 6318
diff changeset
    19
#include "SDL_test_common.h"
6311
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    21
#ifdef __EMSCRIPTEN__
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    22
#include <emscripten/emscripten.h>
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    23
#endif
6311
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    24
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 6318
diff changeset
    25
static SDLTest_CommonState *state;
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    26
int i, done;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    27
SDL_Rect rect;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    28
SDL_Event event;
6311
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    29
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    30
static void
8724
9fa8abe556d7 Removed global variable from test program.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 8149
diff changeset
    31
DrawRects(SDL_Renderer * renderer, SDL_Rect * rect)
6311
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    32
{
9627
74986ccdd090 Make dot easier to see in testrelative.
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
    33
    SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
8724
9fa8abe556d7 Removed global variable from test program.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 8149
diff changeset
    34
    SDL_RenderFillRect(renderer, rect);
6311
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    35
}
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    37
static void
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    38
loop(){
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    39
    /* Check for events */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    40
    while (SDL_PollEvent(&event)) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    41
        SDLTest_CommonEvent(state, &event, &done);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    42
        switch(event.type) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    43
        case SDL_MOUSEMOTION:
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    44
            {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    45
                rect.x += event.motion.xrel;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    46
                rect.y += event.motion.yrel;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    47
            }
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    48
            break;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    49
        }
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    50
    }
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    51
    for (i = 0; i < state->num_windows; ++i) {
9333
de02c2d8a06f Fixed bug 2866 - testrelative.c: patch to make the orange box wrap around
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
    52
        SDL_Rect viewport;
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    53
        SDL_Renderer *renderer = state->renderers[i];
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    54
        if (state->windows[i] == NULL)
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    55
            continue;
9627
74986ccdd090 Make dot easier to see in testrelative.
Ryan C. Gordon <icculus@icculus.org>
parents: 9619
diff changeset
    56
        SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    57
        SDL_RenderClear(renderer);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    58
9333
de02c2d8a06f Fixed bug 2866 - testrelative.c: patch to make the orange box wrap around
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
    59
        /* Wrap the cursor rectangle at the screen edges to keep it visible */
de02c2d8a06f Fixed bug 2866 - testrelative.c: patch to make the orange box wrap around
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
    60
        SDL_RenderGetViewport(renderer, &viewport);
de02c2d8a06f Fixed bug 2866 - testrelative.c: patch to make the orange box wrap around
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
    61
        if (rect.x < viewport.x) rect.x += viewport.w;
de02c2d8a06f Fixed bug 2866 - testrelative.c: patch to make the orange box wrap around
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
    62
        if (rect.y < viewport.y) rect.y += viewport.h;
de02c2d8a06f Fixed bug 2866 - testrelative.c: patch to make the orange box wrap around
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
    63
        if (rect.x > viewport.x + viewport.w) rect.x -= viewport.w;
de02c2d8a06f Fixed bug 2866 - testrelative.c: patch to make the orange box wrap around
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
    64
        if (rect.y > viewport.y + viewport.h) rect.y -= viewport.h;
de02c2d8a06f Fixed bug 2866 - testrelative.c: patch to make the orange box wrap around
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
    65
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    66
        DrawRects(renderer, &rect);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    67
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    68
        SDL_RenderPresent(renderer);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    69
    }
9607
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9333
diff changeset
    70
#ifdef __EMSCRIPTEN__
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9333
diff changeset
    71
    if (done) {
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9333
diff changeset
    72
        emscripten_cancel_main_loop();
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9333
diff changeset
    73
    }
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9333
diff changeset
    74
#endif
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    75
}
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
    76
6311
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    77
int
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    78
main(int argc, char *argv[])
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    79
{
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
8724
9fa8abe556d7 Removed global variable from test program.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 8149
diff changeset
    81
    /* Enable standard application logging */
7639
9406b7dd2f2d Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
Andreas Schiffler <aschiffler@ferzkopp.net>
parents: 7517
diff changeset
    82
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
7191
75360622e65f File style cleanup for the SDL 2.0 release
Sam Lantinga <slouken@libsdl.org>
parents: 6785
diff changeset
    83
6311
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    84
    /* Initialize test framework */
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 6318
diff changeset
    85
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
6311
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    86
    if (!state) {
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    87
        return 1;
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    88
    }
8724
9fa8abe556d7 Removed global variable from test program.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 8149
diff changeset
    89
    for (i = 1; i < argc; ++i) {
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 6318
diff changeset
    90
        SDLTest_CommonArg(state, i);
6318
49b2cb56db6e 1.Fixed a memory leak inside XInput2 code
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6316
diff changeset
    91
    }
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 6318
diff changeset
    92
    if (!SDLTest_CommonInit(state)) {
6311
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    93
        return 2;
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    94
    }
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    95
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    96
    /* Create the windows and initialize the renderers */
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    97
    for (i = 0; i < state->num_windows; ++i) {
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    98
        SDL_Renderer *renderer = state->renderers[i];
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    99
        SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   100
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   101
        SDL_RenderClear(renderer);
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   102
    }
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   103
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   104
    srand((unsigned int)time(NULL));
6316
a89c79a3f0cd 1.Moved all xinput2 functionality to its own file
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6311
diff changeset
   105
    if(SDL_SetRelativeMouseMode(SDL_TRUE) < 0) {
a89c79a3f0cd 1.Moved all xinput2 functionality to its own file
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6311
diff changeset
   106
        return 3;
a89c79a3f0cd 1.Moved all xinput2 functionality to its own file
Dimitris Zenios <dimitris.zenios@gmail.com>
parents: 6311
diff changeset
   107
    };
6311
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   108
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   109
    rect.x = DEFAULT_WINDOW_WIDTH / 2;
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   110
    rect.y = DEFAULT_WINDOW_HEIGHT / 2;
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   111
    rect.w = 10;
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   112
    rect.h = 10;
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   113
    /* Main render loop */
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   114
    done = 0;
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
   115
#ifdef __EMSCRIPTEN__
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
   116
    emscripten_set_main_loop(loop, 0, 1);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
   117
#else
6311
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   118
    while (!done) {
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
   119
        loop();
6311
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   120
        }
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8724
diff changeset
   121
#endif
6785
c094c438c0a6 Switched the test code over to use the common functions in the test library.
Sam Lantinga <slouken@libsdl.org>
parents: 6318
diff changeset
   122
    SDLTest_CommonQuit(state);
6311
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   123
    return 0;
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   124
}
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   125
4e5e46f43438 Initial support for XInput2 by Dimitris Zenios
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   126
/* vi: set ts=4 sw=4 expandtab: */