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