Console print input test
This commit is contained in:
@@ -26,9 +26,9 @@ void gameInit(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void gameUpdate(void) {
|
void gameUpdate(void) {
|
||||||
overworldUpdate();
|
// overworldUpdate();
|
||||||
uiTextboxUpdate();
|
// uiTextboxUpdate();
|
||||||
eventUpdate();
|
// eventUpdate();
|
||||||
|
|
||||||
consoleUpdate();
|
consoleUpdate();
|
||||||
inputUpdate();
|
inputUpdate();
|
||||||
|
@@ -18,11 +18,6 @@ void inputInit(void) {
|
|||||||
void inputUpdate(void) {
|
void inputUpdate(void) {
|
||||||
INPUT.previous = INPUT.current;
|
INPUT.previous = INPUT.current;
|
||||||
INPUT.current = inputStateGet();
|
INPUT.current = inputStateGet();
|
||||||
|
|
||||||
#if DUSK_KEYBOARD_SUPPORT == 1
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool_t inputIsDown(const uint8_t bind) {
|
bool_t inputIsDown(const uint8_t bind) {
|
||||||
|
@@ -31,7 +31,8 @@ errorret_t renderInit(void) {
|
|||||||
SDL_WINDOWPOS_UNDEFINED,
|
SDL_WINDOWPOS_UNDEFINED,
|
||||||
RENDER_WINDOW_WIDTH_DEFAULT,
|
RENDER_WINDOW_WIDTH_DEFAULT,
|
||||||
RENDER_WINDOW_HEIGHT_DEFAULT,
|
RENDER_WINDOW_HEIGHT_DEFAULT,
|
||||||
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI
|
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI |
|
||||||
|
SDL_INIT_GAMECONTROLLER
|
||||||
);
|
);
|
||||||
if(!RENDER_WINDOW) {
|
if(!RENDER_WINDOW) {
|
||||||
errorThrow("SDL_CreateWindow failed: %s", SDL_GetError());
|
errorThrow("SDL_CreateWindow failed: %s", SDL_GetError());
|
||||||
|
@@ -8,8 +8,65 @@
|
|||||||
#include "dusksdl2.h"
|
#include "dusksdl2.h"
|
||||||
#include "input.h"
|
#include "input.h"
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
const SDL_GameControllerButton button;
|
||||||
|
const uint8_t bind;
|
||||||
|
} inputsdlbuttonmap_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
SDL_Scancode code;
|
||||||
|
uint8_t bind;
|
||||||
|
} inputsdlkbmap_t;
|
||||||
|
|
||||||
|
inputsdlbuttonmap_t INPUT_SDL_BUTTON_MAP[] = {
|
||||||
|
{ SDL_CONTROLLER_BUTTON_DPAD_UP, INPUT_BIND_UP },
|
||||||
|
{ SDL_CONTROLLER_BUTTON_DPAD_DOWN, INPUT_BIND_DOWN },
|
||||||
|
{ SDL_CONTROLLER_BUTTON_DPAD_LEFT, INPUT_BIND_LEFT },
|
||||||
|
{ SDL_CONTROLLER_BUTTON_DPAD_RIGHT, INPUT_BIND_RIGHT },
|
||||||
|
{ SDL_CONTROLLER_BUTTON_A, INPUT_BIND_ACTION },
|
||||||
|
{ SDL_CONTROLLER_BUTTON_B, INPUT_BIND_CANCEL },
|
||||||
|
{ 0, 0 }
|
||||||
|
};
|
||||||
|
|
||||||
|
inputsdlkbmap_t INPUT_SDL_KEYBOARD_MAP[] = {
|
||||||
|
{ SDL_SCANCODE_W, INPUT_BIND_UP },
|
||||||
|
{ SDL_SCANCODE_S, INPUT_BIND_DOWN },
|
||||||
|
{ SDL_SCANCODE_A, INPUT_BIND_LEFT },
|
||||||
|
{ SDL_SCANCODE_D, INPUT_BIND_RIGHT },
|
||||||
|
{ SDL_SCANCODE_SPACE, INPUT_BIND_ACTION },
|
||||||
|
{ SDL_SCANCODE_ESCAPE, INPUT_BIND_CANCEL },
|
||||||
|
{ 0, 0 }
|
||||||
|
};
|
||||||
|
|
||||||
uint8_t inputStateGet() {
|
uint8_t inputStateGet() {
|
||||||
uint8_t state = 0;
|
uint8_t state = 0;
|
||||||
|
|
||||||
|
// Get gamepad state.
|
||||||
|
for(int32_t i = 0; i < SDL_NumJoysticks(); i++) {
|
||||||
|
if(!SDL_IsGameController(i)) continue;
|
||||||
|
|
||||||
|
SDL_GameController *controller = SDL_GameControllerOpen(i);
|
||||||
|
if(!controller) continue;
|
||||||
|
|
||||||
|
|
||||||
|
inputsdlbuttonmap_t *map = INPUT_SDL_BUTTON_MAP;
|
||||||
|
do {
|
||||||
|
if(SDL_GameControllerGetButton(controller, map->button)) {
|
||||||
|
state |= map->bind;
|
||||||
|
}
|
||||||
|
map++;
|
||||||
|
} while(map->button != 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get keyboard state.
|
||||||
|
const uint8_t *keyboardState = SDL_GetKeyboardState(NULL);
|
||||||
|
inputsdlkbmap_t *kbmap = INPUT_SDL_KEYBOARD_MAP;
|
||||||
|
do {
|
||||||
|
if(keyboardState[kbmap->code]) {
|
||||||
|
state |= kbmap->bind;
|
||||||
|
}
|
||||||
|
kbmap++;
|
||||||
|
} while(kbmap->code != 0);
|
||||||
|
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
@@ -7,6 +7,8 @@
|
|||||||
|
|
||||||
#include "display/render.h"
|
#include "display/render.h"
|
||||||
#include "game.h"
|
#include "game.h"
|
||||||
|
#include "console/console.h"
|
||||||
|
#include "input.h"
|
||||||
|
|
||||||
#define mainError(ret) \
|
#define mainError(ret) \
|
||||||
if((ret).code != ERROR_OK) { \
|
if((ret).code != ERROR_OK) { \
|
||||||
@@ -23,6 +25,13 @@ int main(int argc, char *argv[]) {
|
|||||||
while(RENDER_RUNNING) {
|
while(RENDER_RUNNING) {
|
||||||
gameUpdate();
|
gameUpdate();
|
||||||
mainError(renderDraw());
|
mainError(renderDraw());
|
||||||
|
|
||||||
|
if(inputPressed(INPUT_BIND_UP)) consolePrint("Up pressed");
|
||||||
|
if(inputPressed(INPUT_BIND_DOWN)) consolePrint("Down pressed");
|
||||||
|
if(inputPressed(INPUT_BIND_LEFT)) consolePrint("Left pressed");
|
||||||
|
if(inputPressed(INPUT_BIND_RIGHT)) consolePrint("Right pressed");
|
||||||
|
if(inputPressed(INPUT_BIND_ACTION)) consolePrint("Action pressed");
|
||||||
|
if(inputPressed(INPUT_BIND_CANCEL)) consolePrint("Cancel pressed");
|
||||||
}
|
}
|
||||||
|
|
||||||
gameDispose();
|
gameDispose();
|
||||||
|
Reference in New Issue
Block a user