test/testrelative.c
changeset 6311 4e5e46f43438
child 6316 a89c79a3f0cd
equal deleted inserted replaced
6310:f830e73e2acf 6311:4e5e46f43438
       
     1 /*
       
     2   Copyright (C) 1997-2011 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 "common.h"
       
    20 
       
    21 
       
    22 static CommonState *state;
       
    23 static SDL_Rect rect;
       
    24 
       
    25 static void
       
    26 DrawRects(SDL_Renderer * renderer)
       
    27 {
       
    28     SDL_SetRenderDrawColor(renderer, 255, 127, 0, 255);
       
    29     SDL_RenderFillRect(renderer,&rect);
       
    30 }
       
    31 
       
    32 int
       
    33 main(int argc, char *argv[])
       
    34 {
       
    35     int i, done;
       
    36     SDL_Event event;
       
    37 
       
    38  
       
    39     /* Initialize test framework */
       
    40     state = CommonCreateState(argv, SDL_INIT_VIDEO);
       
    41     if (!state) {
       
    42         return 1;
       
    43     }
       
    44     if (!CommonInit(state)) {
       
    45         return 2;
       
    46     }
       
    47 
       
    48     /* Create the windows and initialize the renderers */
       
    49     for (i = 0; i < state->num_windows; ++i) {
       
    50         SDL_Renderer *renderer = state->renderers[i];
       
    51         SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
       
    52         SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
       
    53         SDL_RenderClear(renderer);
       
    54     }
       
    55 
       
    56     srand((unsigned int)time(NULL));
       
    57     SDL_SetRelativeMouseMode(SDL_TRUE);
       
    58 
       
    59     rect.x = DEFAULT_WINDOW_WIDTH / 2;
       
    60     rect.y = DEFAULT_WINDOW_HEIGHT / 2;
       
    61     rect.w = 10;
       
    62     rect.h = 10;
       
    63     /* Main render loop */
       
    64     done = 0;
       
    65     while (!done) {
       
    66         /* Check for events */
       
    67         while (SDL_PollEvent(&event)) {
       
    68             CommonEvent(state, &event, &done);
       
    69             switch(event.type) {
       
    70                 case SDL_MOUSEMOTION:
       
    71                 {
       
    72                     /*printf("mouse motion ABS x %d y %d REL x %d y %d\n",event.motion.x,event.motion.y,event.motion.xrel,event.motion.yrel);*/
       
    73                     rect.x += event.motion.xrel;
       
    74                     rect.y += event.motion.yrel;
       
    75 
       
    76                 }
       
    77                 break;
       
    78             }
       
    79         }
       
    80         for (i = 0; i < state->num_windows; ++i) {
       
    81             SDL_Renderer *renderer = state->renderers[i];
       
    82             SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
       
    83             SDL_RenderClear(renderer);
       
    84 
       
    85             DrawRects(renderer);
       
    86 
       
    87             SDL_RenderPresent(renderer);
       
    88         }
       
    89     }
       
    90 
       
    91     CommonQuit(state);
       
    92     return 0;
       
    93 }
       
    94 
       
    95 /* vi: set ts=4 sw=4 expandtab: */