gamepad fully working :)

This commit is contained in:
DaniTheSkunk 2023-01-20 09:01:15 +00:00
parent cd9a5251e0
commit 647c6778e2
5 changed files with 43 additions and 9 deletions

View File

@ -1,3 +1,4 @@
#include "gamepad.h"
#include <gsa.h> #include <gsa.h>
void init() { void init() {
@ -23,5 +24,13 @@ void init() {
} }
void tick() { void tick() {
sprites[0].x += 1; i32 x, y;
x = sw_gamepads[0].button_down[SW_GAMEPAD_BUTTON_DPAD_LEFT] -
sw_gamepads[0].button_down[SW_GAMEPAD_BUTTON_DPAD_RIGHT];
y = sw_gamepads[0].button_down[SW_GAMEPAD_BUTTON_DPAD_UP] -
sw_gamepads[0].button_down[SW_GAMEPAD_BUTTON_DPAD_DOWN];
maps[0].scrollx -= x;
maps[0].scrolly -= y;
} }

View File

@ -1,17 +1,39 @@
#ifndef GUARD_F8BB6BA29F6FB6745AF8B6E15CD1C086 #ifndef GUARD_F8BB6BA29F6FB6745AF8B6E15CD1C086
#define GUARD_F8BB6BA29F6FB6745AF8B6E15CD1C086 #define GUARD_F8BB6BA29F6FB6745AF8B6E15CD1C086
#include "gamepad.h"
#include "types.h" #include "types.h"
#define SW_MAX_GAMEPADS 4 #define SW_MAX_GAMEPADS 4
#define SW_MAX_GAMEPAD_AXES 4 #define SW_MAX_GAMEPAD_AXES 6
#define SW_MAX_GAMEPAD_BUTTONS 14 #define SW_MAX_GAMEPAD_BUTTONS 15
#define SW_GAMEPAD_BUTTON_FACE_DOWN 0
#define SW_GAMEPAD_BUTTON_FACE_RIGHT 1
#define SW_GAMEPAD_BUTTON_FACE_LEFT 2
#define SW_GAMEPAD_BUTTON_FACE_UP 3
#define SW_GAMEPAD_BUTTON_LEFT_SHOULDER 4
#define SW_GAMEPAD_BUTTON_RIGHT_SHOULDER 5
#define SW_GAMEPAD_BUTTON_BACK 6
#define SW_GAMEPAD_BUTTON_START 7
#define SW_GAMEPAD_BUTTON_GUIDE 8
#define SW_GAMEPAD_BUTTON_LEFT_STICK 9
#define SW_GAMEPAD_BUTTON_RIGHT_STICK 10
#define SW_GAMEPAD_BUTTON_DPAD_UP 11
#define SW_GAMEPAD_BUTTON_DPAD_RIGHT 12
#define SW_GAMEPAD_BUTTON_DPAD_DOWN 13
#define SW_GAMEPAD_BUTTON_DPAD_LEFT 14
#define SW_GAMEPAD_AXIS_LEFT_X 0
#define SW_GAMEPAD_AXIS_RIGHT_X 1
#define SW_GAMEPAD_AXIS_LEFT_Y 2
#define SW_GAMEPAD_AXIS_RIGHT_Y 3
#define SW_GAMEPAD_AXIS_LEFT_TRIGGER 4
#define SW_GAMEPAD_AXIS_RIGHT_TRIGGER 5
struct sw_gamepad { struct sw_gamepad {
f32 axes[SW_MAX_GAMEPAD_AXES]; f32 axes[SW_MAX_GAMEPAD_AXES];
bool button_down; bool button_down[SW_MAX_GAMEPAD_BUTTONS];
bool button_pressed; bool button_pressed[SW_MAX_GAMEPAD_BUTTONS];
}; };
extern struct sw_gamepad sw_gamepads[SW_MAX_GAMEPADS]; extern struct sw_gamepad sw_gamepads[SW_MAX_GAMEPADS];

View File

@ -4,6 +4,7 @@
#include "color32.h" #include "color32.h"
#include "error.h" #include "error.h"
#include "file.h" #include "file.h"
#include "gamepad.h"
#include "image32.h" #include "image32.h"
#include "image8.h" #include "image8.h"
#include "types.h" #include "types.h"

View File

@ -1,5 +1,6 @@
#include "gamepad.h" #include "gamepad.h"
#include "GLFW/glfw3.h" #include "GLFW/glfw3.h"
#include "error.h"
#include "string.h" #include "string.h"
struct sw_gamepad sw_gamepads[SW_MAX_GAMEPADS]; struct sw_gamepad sw_gamepads[SW_MAX_GAMEPADS];
@ -19,9 +20,9 @@ void sw_gamepad_tick() {
} }
for(j = 0; j < SW_MAX_GAMEPAD_BUTTONS; ++j) { for(j = 0; j < SW_MAX_GAMEPAD_BUTTONS; ++j) {
bool down = state.buttons[j]; bool down = state.buttons[j];
sw_gamepads[i].button_pressed = sw_gamepads[i].button_pressed[j] =
!sw_gamepads[i].button_pressed && down; !sw_gamepads[i].button_pressed[j] && down;
sw_gamepads[i].button_down = down; sw_gamepads[i].button_down[j] = down;
} }
} }
} }

View File

@ -83,6 +83,7 @@ void sw_window_run(struct sw_window *window, void (*callback)()) {
/* private functions */ /* private functions */
void _tick() { void _tick() {
sw_gamepad_tick();
glViewport(0, 0, _tick_window->game_size.x, _tick_window->game_size.y); glViewport(0, 0, _tick_window->game_size.x, _tick_window->game_size.y);
glBindFramebuffer(GL_FRAMEBUFFER, _tick_window->_scaler_fb->_fb); glBindFramebuffer(GL_FRAMEBUFFER, _tick_window->_scaler_fb->_fb);
_tick_fn(); _tick_fn();