test/testrelative.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@6311
     1
/*
slouken@10737
     2
  Copyright (C) 1997-2017 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
{
icculus@9627
    33
    SDL_SetRenderDrawColor(renderer, 255, 0, 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@9627
    56
        SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 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
    }
philipp@9607
    70
#ifdef __EMSCRIPTEN__
philipp@9607
    71
    if (done) {
philipp@9607
    72
        emscripten_cancel_main_loop();
philipp@9607
    73
    }
philipp@9607
    74
#endif
icculus@9278
    75
}
icculus@9278
    76
slouken@6311
    77
int
slouken@6311
    78
main(int argc, char *argv[])
slouken@6311
    79
{
slouken@6311
    80
philipp@8724
    81
    /* Enable standard application logging */
aschiffler@7639
    82
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
slouken@7191
    83
slouken@6311
    84
    /* Initialize test framework */
slouken@6785
    85
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@6311
    86
    if (!state) {
slouken@6311
    87
        return 1;
slouken@6311
    88
    }
philipp@8724
    89
    for (i = 1; i < argc; ++i) {
slouken@6785
    90
        SDLTest_CommonArg(state, i);
dimitris@6318
    91
    }
slouken@6785
    92
    if (!SDLTest_CommonInit(state)) {
slouken@6311
    93
        return 2;
slouken@6311
    94
    }
slouken@6311
    95
slouken@6311
    96
    /* Create the windows and initialize the renderers */
slouken@6311
    97
    for (i = 0; i < state->num_windows; ++i) {
slouken@6311
    98
        SDL_Renderer *renderer = state->renderers[i];
slouken@6311
    99
        SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
slouken@6311
   100
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
slouken@6311
   101
        SDL_RenderClear(renderer);
slouken@6311
   102
    }
slouken@6311
   103
slouken@6311
   104
    srand((unsigned int)time(NULL));
dimitris@6316
   105
    if(SDL_SetRelativeMouseMode(SDL_TRUE) < 0) {
dimitris@6316
   106
        return 3;
dimitris@6316
   107
    };
slouken@6311
   108
slouken@6311
   109
    rect.x = DEFAULT_WINDOW_WIDTH / 2;
slouken@6311
   110
    rect.y = DEFAULT_WINDOW_HEIGHT / 2;
slouken@6311
   111
    rect.w = 10;
slouken@6311
   112
    rect.h = 10;
slouken@6311
   113
    /* Main render loop */
slouken@6311
   114
    done = 0;
icculus@9278
   115
#ifdef __EMSCRIPTEN__
icculus@9278
   116
    emscripten_set_main_loop(loop, 0, 1);
icculus@9278
   117
#else
slouken@6311
   118
    while (!done) {
icculus@9278
   119
        loop();
slouken@6311
   120
        }
icculus@9278
   121
#endif
slouken@6785
   122
    SDLTest_CommonQuit(state);
slouken@6311
   123
    return 0;
slouken@6311
   124
}
slouken@6311
   125
slouken@6311
   126
/* vi: set ts=4 sw=4 expandtab: */