commit 57c977f49525dc562a66ab5adba1107c7fe5b5c0
parent e19d217620a40bd2f8e9e641f63c0ae4d82f4dfe
Author: Brian Swetland <swetland@frotz.net>
Date: Fri, 5 Jul 2013 02:13:08 -0700
keyboard: use bitmask, use SDL scancodes
Diffstat:
3 files changed, 19 insertions(+), 16 deletions(-)
diff --git a/common/app.h b/common/app.h
@@ -163,6 +163,9 @@ public:
int getWidth(void) { return width; }
int getHeight(void) { return height; }
+ int keydown(unsigned code) {
+ return keystate[code >> 5] & (1 << (code & 0x1f));
+ }
protected:
int width;
int height;
@@ -173,8 +176,9 @@ protected:
int mouseBTN;
/* mouse position in window coordinates */
int mouseWX, mouseWY;
+
/* keys down */
- unsigned char keystate[256];
+ unsigned keystate[16];
private:
int _vsync;
diff --git a/common/glapp.cc b/common/glapp.cc
@@ -47,19 +47,20 @@ static void quit(void) {
}
void App::handleEvents(void) {
+ unsigned code;
SDL_Event ev;
while (SDL_PollEvent(&ev)) {
switch (ev.type) {
case SDL_KEYDOWN:
- if (ev.key.keysym.sym < 256)
- keystate[ev.key.keysym.sym] = 1;
- if (ev.key.keysym.sym == SDLK_ESCAPE)
+ code = ev.key.keysym.scancode;
+ keystate[code >> 5] |= (1 << (code & 0x1F));
+ if (code == SDL_SCANCODE_ESCAPE)
quit();
break;
case SDL_KEYUP:
- if (ev.key.keysym.sym < 256)
- keystate[ev.key.keysym.sym] = 0;
+ code = ev.key.keysym.scancode;
+ keystate[code >> 5] &= ~(1 << (code & 0x1F));
break;
case SDL_QUIT:
quit();
diff --git a/hello/hello.cc b/hello/hello.cc
@@ -171,20 +171,18 @@ void TestApp::render(void) {
if (zoom > 100.0) zoom = 100.0;
}
-#if defined(_WIN32) && !defined(USE_OPENGL)
- if (keystate[DIK_A]) { nx -= 0.01; update = 1; }
- if (keystate[DIK_D]) { nx += 0.01; update = 1; }
- if (keystate[DIK_W]) { ny -= 0.01; update = 1; }
- if (keystate[DIK_S]) { ny += 0.01; update = 1; }
- if (keystate[DIK_P]) {
- loadShader(&ps, "SimplePS.glsl");
- loadShader(&vs, "SimpleVS.glsl", obj_layout,
- sizeof(obj_layout) / sizeof(obj_layout[0]));
+ if (keydown(SDL_SCANCODE_A)) { nx -= 0.01; update = 1; }
+ if (keydown(SDL_SCANCODE_D)) { nx += 0.01; update = 1; }
+ if (keydown(SDL_SCANCODE_W)) { ny -= 0.01; update = 1; }
+ if (keydown(SDL_SCANCODE_S)) { ny += 0.01; update = 1; }
+ if (keydown(SDL_SCANCODE_P)) {
+ ps.load("simple.fragment");
+ vs.load("simple.vertex");
+ pgm.link(&vs, &ps);
}
oops:
if (update)
build();
-#endif
struct {
mat4 mvp;