testrelative.c 2.6 KB

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