- Implementat keyPressed

This commit is contained in:
2023-06-04 14:26:34 +02:00
parent 312bed64da
commit f92d483cde
4 changed files with 13 additions and 8 deletions

View File

@@ -24,24 +24,24 @@ int main(int argc, char *argv[])
SDL_Event e; SDL_Event e;
while (!should_exit) while (!should_exit)
{ {
uint8_t keyp=SDL_SCANCODE_UNKNOWN, keydown=SDL_SCANCODE_UNKNOWN;
while(SDL_PollEvent(&e)) while(SDL_PollEvent(&e))
{ {
if (e.type==SDL_QUIT) { should_exit = true; break; } if (e.type==SDL_QUIT) { should_exit = true; break; }
if (e.type==SDL_KEYDOWN) if (e.type==SDL_KEYDOWN)
{ {
keydown = e.key.keysym.scancode; input::updateKey(e.key.keysym.scancode);
} }
if (e.type==SDL_KEYUP) if (e.type==SDL_KEYUP)
{ {
keyp = e.key.keysym.scancode; input::updateKeypressed(e.key.keysym.scancode);
} }
} }
input::update(keydown,keyp);
if (SDL_GetTicks()-current_ticks >= game::ticks_per_frame) if (SDL_GetTicks()-current_ticks >= game::ticks_per_frame)
{ {
if (!game::loop()) should_exit = true; if (!game::loop()) should_exit = true;
input::updateKey(SDL_SCANCODE_UNKNOWN);
input::updateKeypressed(SDL_SCANCODE_UNKNOWN);
current_ticks = SDL_GetTicks(); current_ticks = SDL_GetTicks();
} }
} }

View File

@@ -36,10 +36,14 @@ namespace input
return keydown; return keydown;
} }
void update(uint8_t key, uint8_t keyp) void updateKey(uint8_t key)
{ {
keypressed = keyp;
keydown = key; keydown = key;
} }
void updateKeypressed(uint8_t key)
{
keypressed = key;
}
} }

View File

@@ -25,5 +25,6 @@ namespace input
/// @return Quina tecla està sent polsada /// @return Quina tecla està sent polsada
const uint8_t whichKey(); const uint8_t whichKey();
void update(uint8_t key, uint8_t keyp); void updateKey(uint8_t key);
void updateKeypressed(uint8_t key);
} }

View File

@@ -49,7 +49,7 @@ int sx=1, sy=0;
bool game::loop() bool game::loop()
{ {
if ( input::keyDown(SDL_SCANCODE_Q) ) if ( input::keyPressed(SDL_SCANCODE_Q) )
{ {
ii++;if(ii>3)ii=0; ii++;if(ii>3)ii=0;
restart(); restart();