aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2022-11-23 21:53:17 +0100
committerkdx <kikoodx@paranoici.org>2022-11-23 21:53:17 +0100
commit5e04faf8ff92c35de01a1f69801b5625f13a681b (patch)
treecf979d76bea7619951750720d532755188c97b44
parent00fdadbdcc62e3ad9608ceefd0f82ed825a0a9b7 (diff)
downloadlzr-5e04faf8ff92c35de01a1f69801b5625f13a681b.tar.gz
mouse support (for development)
-rw-r--r--demo.c2
-rw-r--r--lzr.c63
-rw-r--r--lzr.h9
3 files changed, 69 insertions, 5 deletions
diff --git a/demo.c b/demo.c
index 48e53d8..8e5c70a 100644
--- a/demo.c
+++ b/demo.c
@@ -40,6 +40,8 @@ int main(void)
stg.flip_v = false;
y++;
}
+ if (LZR_ButtonDown(LZR_BUTTON_MOUSE_L))
+ LZR_MousePosition(&x, &y);
stg.angle = darkness * 20;
LZR_CycleEvents();
LZR_DrawBegin();
diff --git a/lzr.c b/lzr.c
index b5b9930..865a502 100644
--- a/lzr.c
+++ b/lzr.c
@@ -4,7 +4,6 @@
#include <SDL2/SDL2_gfxPrimitives.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
-#include <SDL2/SDL_render.h>
#include <dx/log.h>
#include <dx/mem.h>
#include <dx/str.h>
@@ -39,6 +38,11 @@ static uint64_t tick = 0;
static Mix_Chunk *sounds[LZR_MAX_SOUNDS] = {0};
static Mix_Music *music = NULL;
static SDL_BlendMode blend_mode = SDL_BLENDMODE_NONE;
+static int off_x = 0;
+static int off_y = 0;
+static float scale = 1.0;
+static int mouse_x = 0;
+static int mouse_y = 0;
static int _scode_to_button(unsigned int scode)
{
@@ -355,6 +359,17 @@ bool LZR_PollEvent(LZR_Event *e)
input[b] = true;
return true;
}
+ case SDL_MOUSEBUTTONDOWN: {
+ e->type = LZR_EVENT_BUTTON_DOWN;
+ e->button = LZR_BUTTON_MOUSE_L + se.button.button - 1;
+ e->x = se.button.x, e->y = se.button.y;
+ LZR_ScreenTransform(&e->x, &e->y);
+ mouse_x = e->x, mouse_y = e->y;
+ if (e->button >= LZR_BUTTON_COUNT)
+ continue;
+ input[e->button] = true;
+ return true;
+ }
case SDL_KEYUP: {
const int b = _scode_to_button(se.key.keysym.scancode);
if (b < 0)
@@ -364,6 +379,24 @@ bool LZR_PollEvent(LZR_Event *e)
input[b] = false;
return true;
}
+ case SDL_MOUSEBUTTONUP: {
+ e->type = LZR_EVENT_BUTTON_DOWN;
+ e->button = LZR_BUTTON_MOUSE_L + se.button.button - 1;
+ e->x = se.button.x, e->y = se.button.y;
+ LZR_ScreenTransform(&e->x, &e->y);
+ mouse_x = e->x, mouse_y = e->y;
+ if (e->button >= LZR_BUTTON_COUNT)
+ continue;
+ input[e->button] = false;
+ return true;
+ }
+ case SDL_MOUSEMOTION: {
+ e->type = LZR_EVENT_MOUSE_MOVE;
+ e->x = se.motion.x, e->y = se.motion.y;
+ LZR_ScreenTransform(&e->x, &e->y);
+ mouse_x = e->x, mouse_y = e->y;
+ return true;
+ }
default:
break;
}
@@ -436,9 +469,9 @@ int LZR_DrawEnd(void)
SDL_GetWindowSize(window, &win_w, &win_h);
const int ratio_w = win_w / config.display_width;
const int ratio_h = win_h / config.display_height;
- const int scale = (ratio_w <= ratio_h) ? ratio_w : ratio_h;
- const int off_x = (win_w - config.display_width * scale) / 2;
- const int off_y = (win_h - config.display_height * scale) / 2;
+ scale = (ratio_w <= ratio_h) ? ratio_w : ratio_h;
+ off_x = (win_w - config.display_width * scale) / 2;
+ off_y = (win_h - config.display_height * scale) / 2;
const SDL_Rect dest = {off_x, off_y, config.display_width * scale,
config.display_height * scale};
if (SDL_RenderCopyEx(renderer, target, NULL, &dest, 0.0, NULL, 0) < 0) {
@@ -727,6 +760,28 @@ uint64_t LZR_GetTick(void)
return tick;
}
+void LZR_ScreenTransform(int *x, int *y)
+{
+ if (scale == 0.0)
+ return;
+ if (x != NULL) {
+ *x -= off_x;
+ *x /= scale;
+ }
+ if (y != NULL) {
+ *y -= off_y;
+ *y /= scale;
+ }
+}
+
+void LZR_MousePosition(int *x, int *y)
+{
+ if (x != NULL)
+ *x = mouse_x;
+ if (y != NULL)
+ *y = mouse_y;
+}
+
/*
** Copyright (c) 2022 kdx
**
diff --git a/lzr.h b/lzr.h
index e244a51..82753c7 100644
--- a/lzr.h
+++ b/lzr.h
@@ -22,6 +22,7 @@ typedef enum LZR_EventType {
LZR_EVENT_QUIT,
LZR_EVENT_BUTTON_DOWN,
LZR_EVENT_BUTTON_UP,
+ LZR_EVENT_MOUSE_MOVE,
} LZR_EventType;
typedef enum LZR_Button {
@@ -31,12 +32,16 @@ typedef enum LZR_Button {
LZR_BUTTON_DOWN,
LZR_BUTTON_O,
LZR_BUTTON_X,
+ LZR_BUTTON_MOUSE_L,
+ LZR_BUTTON_MOUSE_M,
+ LZR_BUTTON_MOUSE_R,
LZR_BUTTON_COUNT
} LZR_Button;
typedef struct LZR_Event {
LZR_EventType type;
LZR_Button button;
+ int x, y;
} LZR_Event;
typedef struct LZR_ImageDrawSettings {
@@ -72,8 +77,10 @@ int LZR_PlaySound(int id);
int LZR_SetMusicVolume(float volume);
int LZR_PlayMusic(const char *path, int loops);
void LZR_StopMusic(void);
-uint64_t LZR_GetTick(void);
void LZR_ToggleFullscreen(void);
+uint64_t LZR_GetTick(void);
+void LZR_ScreenTransform(int *x, int *y);
+void LZR_MousePosition(int *x, int *y);
#endif