aboutsummaryrefslogtreecommitdiff
path: root/mkb
diff options
context:
space:
mode:
Diffstat (limited to 'mkb')
-rw-r--r--mkb/mkb.c53
-rw-r--r--mkb/mkb.h24
2 files changed, 77 insertions, 0 deletions
diff --git a/mkb/mkb.c b/mkb/mkb.c
new file mode 100644
index 0000000..169aa8c
--- /dev/null
+++ b/mkb/mkb.c
@@ -0,0 +1,53 @@
+#include <mkb.h>
+
+bool mkb_search(MKb *mkb, SDL_Scancode scancode) {
+ for (int i=0; i<mkb->kbnum; i++) {
+ if (mkb->kb[i] == scancode)
+ return true;
+ }
+ return false;
+}
+
+bool mkb_statesearch (MKb *mkb, SDL_Scancode scancode) {
+ const Uint8 *state = SDL_GetKeyboardState(NULL);
+ if (state[scancode]) return true;
+ return false;
+}
+
+void mkb_init(MKb *mkb) {
+ mkb->kbnum = 0;
+ for (int i=0;i<3;mkb->mheld[i]=0,mkb->mclick[i]=0,i++);
+ mkb->mwheeldelta = 0;
+}
+
+void mkb_process(MKb *mkb) {
+ mkb->kbnum = 0;
+ SDL_GetMouseState(&(mkb->mx), &(mkb->my));
+ for (int i=0;i<3;i++) {
+ if (mkb->mclick[i] > 0) mkb->mheld[i] += 1;
+ else if (mkb->mheld[i] > 0) mkb->mheld[i] = -1;
+ else mkb->mheld[i] = 0;
+ }
+ mkb->mwheeldelta = 0;
+}
+
+void mkb_event(MKb *mkb, SDL_Event *e) {
+ if(e->type == SDL_KEYDOWN && e->key.repeat == 0) {
+ mkb->kb[mkb->kbnum++] = e->key.keysym.scancode;
+ }
+ if(e->type == SDL_KEYUP) {
+ }
+ if(e->type == SDL_MOUSEBUTTONDOWN) {
+ if (e->button.button == SDL_BUTTON_LEFT) { mkb->mclick[0] = 1; }
+ if (e->button.button == SDL_BUTTON_MIDDLE) { mkb->mclick[1] = 1; }
+ if (e->button.button == SDL_BUTTON_RIGHT) { mkb->mclick[2] = 1; }
+ }
+ if(e->type == SDL_MOUSEBUTTONUP) {
+ if (e->button.button == SDL_BUTTON_LEFT) { mkb->mclick[0] = 0; }
+ if (e->button.button == SDL_BUTTON_MIDDLE) { mkb->mclick[1] = 0; }
+ if (e->button.button == SDL_BUTTON_RIGHT) { mkb->mclick[2] = 0; }
+ }
+ if(e->type == SDL_MOUSEWHEEL) {
+ mkb->mwheeldelta = e->wheel.y;
+ }
+} \ No newline at end of file
diff --git a/mkb/mkb.h b/mkb/mkb.h
new file mode 100644
index 0000000..74b1a47
--- /dev/null
+++ b/mkb/mkb.h
@@ -0,0 +1,24 @@
+#ifndef MKB_H
+#define MKB_H
+
+#include <SDL.h>
+
+// SDL2 mouse and keyboard
+
+typedef struct {
+ SDL_Scancode kb[128]; /* keyboard state, reset every frame */
+ int kbnum;
+ int mx; /* mouse x pos */
+ int my; /* mouse y pos */
+ int mclick[3];
+ int mheld[3];
+ int mwheeldelta;
+} MKb;
+
+void mkb_init(MKb *mkb);
+bool mkb_search(MKb *mkb, SDL_Scancode scancode);
+bool mkb_statesearch(MKb *mkb, SDL_Scancode scancode);
+void mkb_event(MKb *mkb, SDL_Event *e);
+void mkb_process(MKb *mkb);
+
+#endif \ No newline at end of file