diff options
author | kdx <kikoodx@paranoici.org> | 2023-03-27 08:59:27 +0200 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-03-27 09:00:58 +0200 |
commit | 3e4d0b255a3363dc19e8db99845121b2b401e475 (patch) | |
tree | 0489d3ef4a37018acf53765393b207b99efbe398 | |
parent | a65ccb043979b550c3ed516683418fc0e18063f8 (diff) | |
download | tzr-3e4d0b255a3363dc19e8db99845121b2b401e475.tar.gz |
mouse position and screen transform
-rw-r--r-- | headers/TZR_events.h | 2 | ||||
-rw-r--r-- | headers/TZR_globals.h | 3 | ||||
-rw-r--r-- | headers/TZR_render.h | 2 | ||||
-rw-r--r-- | sources/TZR_DrawEnd.c | 18 | ||||
-rw-r--r-- | sources/TZR_GetMousePosition.c | 12 | ||||
-rw-r--r-- | sources/TZR_ScreenTransform.c | 16 | ||||
-rw-r--r-- | sources/globals.c | 3 |
7 files changed, 47 insertions, 9 deletions
diff --git a/headers/TZR_events.h b/headers/TZR_events.h index 4b2b0c6..61262c8 100644 --- a/headers/TZR_events.h +++ b/headers/TZR_events.h @@ -6,3 +6,5 @@ int TZR_PollEvent(TZR_Event *e); /* Drain queued events with TZR_PollEvent and call TZR_ResourcesWatch. */ void TZR_CycleEvents(void); + +void TZR_GetMousePosition(int *x, int *y); diff --git a/headers/TZR_globals.h b/headers/TZR_globals.h index 4f8c00c..548110b 100644 --- a/headers/TZR_globals.h +++ b/headers/TZR_globals.h @@ -20,3 +20,6 @@ extern int ___tzr_mouse_y; extern const char *___tzr_command[___TZR_RES_COUNT]; extern TZR_KeyState ___tzr_keystates[SDL_NUM_SCANCODES]; extern TZR_KeyState ___tzr_mousestates[256]; +extern float ___tzr_scale; +extern int ___tzr_off_x; +extern int ___tzr_off_y; diff --git a/headers/TZR_render.h b/headers/TZR_render.h index 2ba0b3c..592440c 100644 --- a/headers/TZR_render.h +++ b/headers/TZR_render.h @@ -55,3 +55,5 @@ int TZR_DrawRectangle(bool fill, int x, int y, int w, int h); [[nodiscard]] #endif int _TZR_DrawImage(const TZR_DrawImageArgs *args); + +void TZR_ScreenTransform(int *x, int *y); diff --git a/sources/TZR_DrawEnd.c b/sources/TZR_DrawEnd.c index ba379be..38c8e1b 100644 --- a/sources/TZR_DrawEnd.c +++ b/sources/TZR_DrawEnd.c @@ -22,16 +22,16 @@ TZR_DrawEnd(void) const float ratio_w = win_w / (float)___tzr_config.width; const float ratio_h = win_h / (float)___tzr_config.height; - float scale = (ratio_w < ratio_h) ? ratio_w : ratio_h; - if (scale > 1.0f && ___tzr_config.pixel_perfect) - scale = (int)scale; - const int off_x = (win_w - ___tzr_config.width * scale) / 2; - const int off_y = (win_h - ___tzr_config.height * scale) / 2; + ___tzr_scale = (ratio_w < ratio_h) ? ratio_w : ratio_h; + if (___tzr_scale > 1.0f && ___tzr_config.pixel_perfect) + ___tzr_scale = (int)___tzr_scale; + ___tzr_off_x = (win_w - ___tzr_config.width * ___tzr_scale) / 2; + ___tzr_off_y = (win_h - ___tzr_config.height * ___tzr_scale) / 2; const SDL_Rect dest = { - off_x, - off_y, - ___tzr_config.width * scale, - ___tzr_config.height * scale + ___tzr_off_x, + ___tzr_off_y, + ___tzr_config.width * ___tzr_scale, + ___tzr_config.height * ___tzr_scale }; if (SDL_RenderCopy(___tzr_renderer, ___tzr_target, NULL, &dest) < 0) diff --git a/sources/TZR_GetMousePosition.c b/sources/TZR_GetMousePosition.c new file mode 100644 index 0000000..f09bd22 --- /dev/null +++ b/sources/TZR_GetMousePosition.c @@ -0,0 +1,12 @@ +#include "TZR_globals.h" +#include "TZR_render.h" + +void +TZR_GetMousePosition(int *x, int *y) +{ + if (x != NULL) + *x = ___tzr_mouse_x; + if (y != NULL) + *y = ___tzr_mouse_y; + TZR_ScreenTransform(x, y); +} diff --git a/sources/TZR_ScreenTransform.c b/sources/TZR_ScreenTransform.c new file mode 100644 index 0000000..d936781 --- /dev/null +++ b/sources/TZR_ScreenTransform.c @@ -0,0 +1,16 @@ +#include "TZR_globals.h" + +void +TZR_ScreenTransform(int *x, int *y) +{ + if (___tzr_scale == 0.0) + return; + if (x != NULL) { + *x -= ___tzr_off_x; + *x /= ___tzr_scale; + } + if (y != NULL) { + *y -= ___tzr_off_y; + *y /= ___tzr_scale; + } +} diff --git a/sources/globals.c b/sources/globals.c index badcc1e..7c4f26b 100644 --- a/sources/globals.c +++ b/sources/globals.c @@ -20,3 +20,6 @@ int ___tzr_mouse_y = 0; const char *___tzr_command[___TZR_RES_COUNT] = {}; TZR_KeyState ___tzr_keystates[SDL_NUM_SCANCODES] = {}; TZR_KeyState ___tzr_mousestates[256] = {}; //doc says than mouse button is u8 +float ___tzr_scale = 1.0; +int ___tzr_off_x = 1.0; +int ___tzr_off_y = 1.0; |