- Moviment

This commit is contained in:
2023-03-01 13:09:37 +01:00
parent 71a2195ff0
commit f54e5ebef5
6 changed files with 96 additions and 2 deletions

3
.gitignore vendored
View File

@@ -1,3 +1,4 @@
*.exe *.exe
*.dll *.dll
vscode/* vscode/*
thepool

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@@ -1,19 +1,31 @@
#include "jgame.h" #include "jgame.h"
#include "jdraw.h" #include "jdraw.h"
#include "jinput.h"
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
game::init(); game::init();
input::init();
bool should_exit=false; bool should_exit=false;
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)
{
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; if (!game::loop()) should_exit = true;
} }
return 0; return 0;

45
source/jinput.cpp Normal file
View File

@@ -0,0 +1,45 @@
#include "jinput.h"
#include <SDL2/SDL.h>
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;
}
}

29
source/jinput.h Normal file
View File

@@ -0,0 +1,29 @@
#pragma once
#include <stdint.h>
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);
}

View File

@@ -1,5 +1,7 @@
#include "jgame.h" #include "jgame.h"
#include "jdraw.h" #include "jdraw.h"
#include "jinput.h"
#include <SDL2/SDL.h>
draw::surface *surf; draw::surface *surf;
@@ -11,9 +13,13 @@ void game::init()
draw::loadPalette("test.gif"); draw::loadPalette("test.gif");
} }
int sx=1, sy=0;
bool game::loop() 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 y=0;y<8;++y)
{ {
for (int x=0;x<8;++x) 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+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(); draw::render();
return true; return true;