| 1 |
// |
| 2 |
// Copyright(C) 2021 by Ryan Krafnick |
| 3 |
// |
| 4 |
// This program is free software; you can redistribute it and/or |
| 5 |
// modify it under the terms of the GNU General Public License |
| 6 |
// as published by the Free Software Foundation; either version 2 |
| 7 |
// of the License, or (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
// |
| 14 |
// DESCRIPTION: |
| 15 |
// DSDA Mouse |
| 16 |
// |
| 17 |
|
| 18 |
#include "SDL.h" |
| 19 |
|
| 20 |
#include "dsda/configuration.h" |
| 21 |
#include "dsda/features.h" |
| 22 |
|
| 23 |
#include "i_video.h" |
| 24 |
#include "mouse.h" |
| 25 |
|
| 26 |
static int quickstart_cache_tics; |
| 27 |
static int quickstart_queued; |
| 28 |
static signed short angleturn_cache[35]; |
| 29 |
static unsigned int angleturn_cache_index; |
| 30 |
|
| 31 |
void dsda_InitQuickstartCache(void) { |
| 32 |
quickstart_cache_tics = dsda_IntConfig(dsda_config_quickstart_cache_tics); |
| 33 |
} |
| 34 |
|
| 35 |
void dsda_ApplyQuickstartMouseCache(int* mousex) { |
| 36 |
int i; |
| 37 |
|
| 38 |
if (!quickstart_cache_tics) return; |
| 39 |
|
| 40 |
if (quickstart_queued) { |
| 41 |
signed short result = 0; |
| 42 |
|
| 43 |
quickstart_queued = false; |
| 44 |
|
| 45 |
dsda_TrackFeature(uf_quickstartcache); |
| 46 |
|
| 47 |
for (i = 0; i < quickstart_cache_tics; ++i) |
| 48 |
result += angleturn_cache[i]; |
| 49 |
|
| 50 |
*mousex = result; |
| 51 |
} |
| 52 |
else { |
| 53 |
angleturn_cache[angleturn_cache_index] = *mousex; |
| 54 |
++angleturn_cache_index; |
| 55 |
if (angleturn_cache_index >= quickstart_cache_tics) |
| 56 |
angleturn_cache_index = 0; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
void dsda_QueueQuickstart(void) { |
| 61 |
quickstart_queued = true; |
| 62 |
} |
| 63 |
|
| 64 |
void dsda_GetMousePosition(int *x, int *y) |
| 65 |
{ |
| 66 |
SDL_GetMouseState(x, y); |
| 67 |
|
| 68 |
// Lets account for HiDPI displays since SDL_GetMouseState doesnt |
| 69 |
*x = (int)(*x * (float)renderer_rect.w / (float)window_rect.w); |
| 70 |
*y = (int)(*y * (float)renderer_rect.h / (float)window_rect.h); |
| 71 |
|
| 72 |
// x and y currently have the mouse position in the renderer_rect |
| 73 |
// but we want the mouse position in the viewport_rect |
| 74 |
if (viewport_rect.x < *x && *x < (viewport_rect.w + viewport_rect.x) && |
| 75 |
viewport_rect.y < *y && *y < (viewport_rect.h + viewport_rect.y)) |
| 76 |
{ |
| 77 |
*x -= viewport_rect.x; |
| 78 |
*y -= viewport_rect.y; |
| 79 |
} |
| 80 |
else |
| 81 |
{ |
| 82 |
*x = 0; |
| 83 |
*y = 0; |
| 84 |
} |
| 85 |
} |