diff --git a/.gitignore b/.gitignore index 9132b8e..e5d6259 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.exe *.dll -vscode/* \ No newline at end of file +vscode/* +thepool diff --git a/data/test.gif b/data/test.gif index 5212e2a..4c4d567 100644 Binary files a/data/test.gif and b/data/test.gif differ diff --git a/source/jgame.cpp b/source/jgame.cpp index f50d4d8..3af5529 100644 --- a/source/jgame.cpp +++ b/source/jgame.cpp @@ -1,19 +1,31 @@ #include "jgame.h" #include "jdraw.h" +#include "jinput.h" #include int main(int argc, char *argv[]) { game::init(); + input::init(); bool should_exit=false; SDL_Event e; while (!should_exit) { + uint8_t keyp=SDL_SCANCODE_UNKNOWN, keydown=SDL_SCANCODE_UNKNOWN; while(SDL_PollEvent(&e)) { if (e.type==SDL_QUIT) { should_exit = true; break; } + if (e.type==SDL_KEYDOWN) + { + keydown = e.key.keysym.scancode; + } + if (e.type==SDL_KEYUP) + { + keyp = e.key.keysym.scancode; + } } + input::update(keydown,keyp); if (!game::loop()) should_exit = true; } return 0; diff --git a/source/jinput.cpp b/source/jinput.cpp new file mode 100644 index 0000000..aaa112d --- /dev/null +++ b/source/jinput.cpp @@ -0,0 +1,45 @@ +#include "jinput.h" +#include + +namespace input +{ + static const uint8_t *keys = nullptr; + static uint8_t keypressed = 0; + static uint8_t keydown = 0; + + void init() + { + keys = SDL_GetKeyboardState(NULL); + } + + // Determina si la tecla especificada està sent polsada ara mateix + bool keyDown(const uint8_t key) + { + return keys[key]; + } + + // Determina si la tecla especificada ha sigut polsada, pero no tornarà a ser true fins + bool keyPressed(const uint8_t key) + { + return key == keypressed; + } + + // Determina si hi ha alguna tecla polsada ara mateix + bool anyKey() + { + return keydown != 0; + } + + // Torna el codi de la tecla que està sent polsada ara mateix + const uint8_t whichKey() + { + return keydown; + } + + void update(uint8_t key, uint8_t keyp) + { + keypressed = keyp; + keydown = key; + } + +} diff --git a/source/jinput.h b/source/jinput.h new file mode 100644 index 0000000..27b5ddb --- /dev/null +++ b/source/jinput.h @@ -0,0 +1,29 @@ +#pragma once +#include + +namespace input +{ + /// @brief Inicialitza els sistemes de teclat, ratolí i gamepad + void init(); + + /// @brief Determina si la tecla especificada està sent polsada ara mateix + /// @param key tecla a consultar + /// @return true si està polsada, false si no + bool keyDown(const uint8_t key); + + /// @brief Determina si la tecla especificada ha sigut polsada, pero no tornarà a ser true fins + /// @brief que no se solte la tecla i se torne a polsar. + /// @param key tecla a consultar + /// @return true si està polsada, false si no + bool keyPressed(const uint8_t key); + + /// @brief Determina si hi ha alguna tecla polsada ara mateix + /// @return true si hi ha alguna tecla polsada, false si no + bool anyKey(); + + /// @brief Torna el codi de la tecla que està sent polsada ara mateix + /// @return Quina tecla està sent polsada + const uint8_t whichKey(); + + void update(uint8_t key, uint8_t keyp); +} \ No newline at end of file diff --git a/source/main.cpp b/source/main.cpp index 1731434..9c73af8 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,5 +1,7 @@ #include "jgame.h" #include "jdraw.h" +#include "jinput.h" +#include draw::surface *surf; @@ -11,9 +13,13 @@ void game::init() draw::loadPalette("test.gif"); } +int sx=1, sy=0; + bool game::loop() { - draw::cls(0); + if (input::keyDown(SDL_SCANCODE_LEFT)) sx++; + + draw::cls(8); for (int y=0;y<8;++y) { for (int x=0;x<8;++x) @@ -21,6 +27,7 @@ bool game::loop() draw::draw(148+x*12-y*12,80+x*6+y*6,24,11,0,13); } } + draw::draw(148+sx*2-sy*2, 68+sx+sy,24,24,24,0); draw::render(); return true;