test/testrelative.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 18 Dec 2014 00:19:52 -0500
changeset 9278 8900afb78a19
parent 8724 9fa8abe556d7
child 9333 de02c2d8a06f
permissions -rw-r--r--
Initial merge of Emscripten port!

With this commit, you can compile SDL2 with Emscripten
( http://emscripten.org/ ), and make your SDL-based C/C++ program
into a web app.

This port was due to the efforts of several people, including: Charlie Birks,
Sathyanarayanan Gunasekaran, Jukka Jylänki, Alon Zakai, Edward Rudd,
Bruce Mitchener, and Martin Gerhardy. (Thanks, everyone!)
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) {
icculus@9278
    52
        SDL_Renderer *renderer = state->renderers[i];
icculus@9278
    53
        if (state->windows[i] == NULL)
icculus@9278
    54
            continue;
icculus@9278
    55
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
icculus@9278
    56
        SDL_RenderClear(renderer);
icculus@9278
    57
icculus@9278
    58
        DrawRects(renderer, &rect);
icculus@9278
    59
icculus@9278
    60
        SDL_RenderPresent(renderer);
icculus@9278
    61
    }
icculus@9278
    62
}
icculus@9278
    63
slouken@6311
    64
int
slouken@6311
    65
main(int argc, char *argv[])
slouken@6311
    66
{
slouken@6311
    67
philipp@8724
    68
    /* Enable standard application logging */
aschiffler@7639
    69
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
slouken@7191
    70
slouken@6311
    71
    /* Initialize test framework */
slouken@6785
    72
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@6311
    73
    if (!state) {
slouken@6311
    74
        return 1;
slouken@6311
    75
    }
philipp@8724
    76
    for (i = 1; i < argc; ++i) {
slouken@6785
    77
        SDLTest_CommonArg(state, i);
dimitris@6318
    78
    }
slouken@6785
    79
    if (!SDLTest_CommonInit(state)) {
slouken@6311
    80
        return 2;
slouken@6311
    81
    }
slouken@6311
    82
slouken@6311
    83
    /* Create the windows and initialize the renderers */
slouken@6311
    84
    for (i = 0; i < state->num_windows; ++i) {
slouken@6311
    85
        SDL_Renderer *renderer = state->renderers[i];
slouken@6311
    86
        SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
slouken@6311
    87
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
slouken@6311
    88
        SDL_RenderClear(renderer);
slouken@6311
    89
    }
slouken@6311
    90
slouken@6311
    91
    srand((unsigned int)time(NULL));
dimitris@6316
    92
    if(SDL_SetRelativeMouseMode(SDL_TRUE) < 0) {
dimitris@6316
    93
        return 3;
dimitris@6316
    94
    };
slouken@6311
    95
slouken@6311
    96
    rect.x = DEFAULT_WINDOW_WIDTH / 2;
slouken@6311
    97
    rect.y = DEFAULT_WINDOW_HEIGHT / 2;
slouken@6311
    98
    rect.w = 10;
slouken@6311
    99
    rect.h = 10;
slouken@6311
   100
    /* Main render loop */
slouken@6311
   101
    done = 0;
icculus@9278
   102
#ifdef __EMSCRIPTEN__
icculus@9278
   103
    emscripten_set_main_loop(loop, 0, 1);
icculus@9278
   104
#else
slouken@6311
   105
    while (!done) {
icculus@9278
   106
        loop();
slouken@6311
   107
        }
icculus@9278
   108
#endif
slouken@6785
   109
    SDLTest_CommonQuit(state);
slouken@6311
   110
    return 0;
slouken@6311
   111
}
slouken@6311
   112
slouken@6311
   113
/* vi: set ts=4 sw=4 expandtab: */