test/testrelative.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sat, 19 Apr 2014 15:28:13 +0200
changeset 8724 9fa8abe556d7
parent 8149 681eb46b8ac4
child 9278 8900afb78a19
permissions -rw-r--r--
Removed global variable from test program.
     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 
    22 static SDLTest_CommonState *state;
    23 
    24 static void
    25 DrawRects(SDL_Renderer * renderer, SDL_Rect * rect)
    26 {
    27     SDL_SetRenderDrawColor(renderer, 255, 127, 0, 255);
    28     SDL_RenderFillRect(renderer, rect);
    29 }
    30 
    31 int
    32 main(int argc, char *argv[])
    33 {
    34     int i, done;
    35     SDL_Rect rect;
    36     SDL_Event event;
    37 
    38     /* Enable standard application logging */
    39     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
    40 
    41     /* Initialize test framework */
    42     state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
    43     if (!state) {
    44         return 1;
    45     }
    46     for (i = 1; i < argc; ++i) {
    47         SDLTest_CommonArg(state, i);
    48     }
    49     if (!SDLTest_CommonInit(state)) {
    50         return 2;
    51     }
    52 
    53     /* Create the windows and initialize the renderers */
    54     for (i = 0; i < state->num_windows; ++i) {
    55         SDL_Renderer *renderer = state->renderers[i];
    56         SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
    57         SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
    58         SDL_RenderClear(renderer);
    59     }
    60 
    61     srand((unsigned int)time(NULL));
    62     if(SDL_SetRelativeMouseMode(SDL_TRUE) < 0) {
    63         return 3;
    64     };
    65 
    66     rect.x = DEFAULT_WINDOW_WIDTH / 2;
    67     rect.y = DEFAULT_WINDOW_HEIGHT / 2;
    68     rect.w = 10;
    69     rect.h = 10;
    70     /* Main render loop */
    71     done = 0;
    72     while (!done) {
    73         /* Check for events */
    74         while (SDL_PollEvent(&event)) {
    75             SDLTest_CommonEvent(state, &event, &done);
    76             switch(event.type) {
    77                 case SDL_MOUSEMOTION:
    78                 {
    79                     rect.x += event.motion.xrel;
    80                     rect.y += event.motion.yrel;
    81                 }
    82                 break;
    83             }
    84         }
    85         for (i = 0; i < state->num_windows; ++i) {
    86             SDL_Renderer *renderer = state->renderers[i];
    87             if (state->windows[i] == NULL)
    88                 continue;
    89             SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
    90             SDL_RenderClear(renderer);
    91 
    92             DrawRects(renderer, &rect);
    93 
    94             SDL_RenderPresent(renderer);
    95         }
    96     }
    97 
    98     SDLTest_CommonQuit(state);
    99     return 0;
   100 }
   101 
   102 /* vi: set ts=4 sw=4 expandtab: */