| 95 | } |
| 96 | |
| 97 | void |
| 98 | loop(void *arg) |
| 99 | { |
| 100 | SDL_Event event; |
| 101 | int i; |
| 102 | SDL_GameController *gamecontroller = (SDL_GameController *)arg; |
| 103 | |
| 104 | /* blank screen, set up for drawing this frame. */ |
| 105 | SDL_SetRenderDrawColor(screen, 0xFF, 0xFF, 0xFF, SDL_ALPHA_OPAQUE); |
| 106 | SDL_RenderClear(screen); |
| 107 | SDL_RenderCopy(screen, background, NULL, NULL); |
| 108 | |
| 109 | while (SDL_PollEvent(&event)) { |
| 110 | switch (event.type) { |
| 111 | case SDL_CONTROLLERAXISMOTION: |
| 112 | SDL_Log("Controller axis %s changed to %d\n", SDL_GameControllerGetStringForAxis((SDL_GameControllerAxis)event.caxis.axis), event.caxis.value); |
| 113 | break; |
| 114 | case SDL_CONTROLLERBUTTONDOWN: |
| 115 | case SDL_CONTROLLERBUTTONUP: |
| 116 | SDL_Log("Controller button %s %s\n", SDL_GameControllerGetStringForButton((SDL_GameControllerButton)event.cbutton.button), event.cbutton.state ? "pressed" : "released"); |
| 117 | break; |
| 118 | case SDL_KEYDOWN: |
| 119 | if (event.key.keysym.sym != SDLK_ESCAPE) { |
| 120 | break; |
| 121 | } |
| 122 | /* Fall through to signal quit */ |
| 123 | case SDL_QUIT: |
| 124 | done = SDL_TRUE; |
| 125 | break; |
| 126 | default: |
| 127 | break; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /* Update visual controller state */ |
| 132 | for (i = 0; i < SDL_CONTROLLER_BUTTON_MAX; ++i) { |
| 133 | if (SDL_GameControllerGetButton(gamecontroller, (SDL_GameControllerButton)i) == SDL_PRESSED) { |
| 134 | const SDL_Rect dst = { button_positions[i].x, button_positions[i].y, 50, 50 }; |
| 135 | SDL_RenderCopyEx(screen, button, NULL, &dst, 0, NULL, SDL_FLIP_NONE); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | for (i = 0; i < SDL_CONTROLLER_AXIS_MAX; ++i) { |
| 140 | const Sint16 deadzone = 8000; /* !!! FIXME: real deadzone */ |
| 141 | const Sint16 value = SDL_GameControllerGetAxis(gamecontroller, (SDL_GameControllerAxis)(i)); |
| 142 | if (value < -deadzone) { |
| 143 | const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 }; |
| 144 | const double angle = axis_positions[i].angle; |
| 145 | SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, SDL_FLIP_NONE); |
| 146 | } else if (value > deadzone) { |
| 147 | const SDL_Rect dst = { axis_positions[i].x, axis_positions[i].y, 50, 50 }; |
| 148 | const double angle = axis_positions[i].angle + 180.0; |
| 149 | SDL_RenderCopyEx(screen, axis, NULL, &dst, angle, NULL, SDL_FLIP_NONE); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /* Update rumble based on trigger state */ |
| 154 | { |
no test coverage detected