aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUneven Prankster <unevenprankster@protonmail.com>2023-08-27 19:48:24 -0300
committerUneven Prankster <unevenprankster@protonmail.com>2023-08-27 19:48:24 -0300
commitb7603d3d8a59b8f4ea22927f8bb35ebf75f74acb (patch)
tree54c8bf374bd777d42b3311d03d3080f4005e3597
parent8769c45f025d61ff506579baa2f35ff39a246bdc (diff)
Controller + Keyboard support
-rw-r--r--assets/src/gunner.h152
-rw-r--r--assets/src/main.c14
-rw-r--r--src/config.h2
-rw-r--r--src/controller.c14
-rw-r--r--src/controller.h6
-rw-r--r--src/main.c8
-rw-r--r--src/rumble.c1
-rw-r--r--src/script_sys.c16
8 files changed, 199 insertions, 14 deletions
diff --git a/assets/src/gunner.h b/assets/src/gunner.h
index 51ebc1a..f99a5fd 100644
--- a/assets/src/gunner.h
+++ b/assets/src/gunner.h
@@ -12,6 +12,12 @@ typedef struct{
#define WHITE (Color){255, 255, 255, 255}
#define BLACK (Color){0, 0, 0, 255}
+typedef char i8;
+typedef unsigned char u8;
+
+typedef short i16;
+typedef unsigned short u16;
+
typedef int i32;
typedef long long i64;
@@ -27,6 +33,132 @@ typedef i32 fx32;
#define FIXED_POINT_ONE (1 << FIXED_POINT_BITS)
#define TO_FIXED(x) ((fx32)(x) << FIXED_POINT_BITS)
#define FROM_FIXED(x) ((x) >> FIXED_POINT_BITS)
+#define FP_TO_FIXED(x) (fx32)((x) * FIXED_POINT_ONE)
+
+typedef struct{
+ fx32 x, y;
+}vec2;
+
+typedef i8 b32;
+#define false 0
+#define true 1
+
+typedef enum {
+ KEY_NULL = 0, // Key: NULL, used for no key pressed
+ // Alphanumeric keys
+ KEY_APOSTROPHE = 39, // Key: '
+ KEY_COMMA = 44, // Key: ,
+ KEY_MINUS = 45, // Key: -
+ KEY_PERIOD = 46, // Key: .
+ KEY_SLASH = 47, // Key: /
+ KEY_ZERO = 48, // Key: 0
+ KEY_ONE = 49, // Key: 1
+ KEY_TWO = 50, // Key: 2
+ KEY_THREE = 51, // Key: 3
+ KEY_FOUR = 52, // Key: 4
+ KEY_FIVE = 53, // Key: 5
+ KEY_SIX = 54, // Key: 6
+ KEY_SEVEN = 55, // Key: 7
+ KEY_EIGHT = 56, // Key: 8
+ KEY_NINE = 57, // Key: 9
+ KEY_SEMICOLON = 59, // Key: ;
+ KEY_EQUAL = 61, // Key: =
+ KEY_A = 65, // Key: A | a
+ KEY_B = 66, // Key: B | b
+ KEY_C = 67, // Key: C | c
+ KEY_D = 68, // Key: D | d
+ KEY_E = 69, // Key: E | e
+ KEY_F = 70, // Key: F | f
+ KEY_G = 71, // Key: G | g
+ KEY_H = 72, // Key: H | h
+ KEY_I = 73, // Key: I | i
+ KEY_J = 74, // Key: J | j
+ KEY_K = 75, // Key: K | k
+ KEY_L = 76, // Key: L | l
+ KEY_M = 77, // Key: M | m
+ KEY_N = 78, // Key: N | n
+ KEY_O = 79, // Key: O | o
+ KEY_P = 80, // Key: P | p
+ KEY_Q = 81, // Key: Q | q
+ KEY_R = 82, // Key: R | r
+ KEY_S = 83, // Key: S | s
+ KEY_T = 84, // Key: T | t
+ KEY_U = 85, // Key: U | u
+ KEY_V = 86, // Key: V | v
+ KEY_W = 87, // Key: W | w
+ KEY_X = 88, // Key: X | x
+ KEY_Y = 89, // Key: Y | y
+ KEY_Z = 90, // Key: Z | z
+ KEY_LEFT_BRACKET = 91, // Key: [
+ KEY_BACKSLASH = 92, // Key: '\'
+ KEY_RIGHT_BRACKET = 93, // Key: ]
+ KEY_GRAVE = 96, // Key: `
+ // Function keys
+ KEY_SPACE = 32, // Key: Space
+ KEY_ESCAPE = 256, // Key: Esc
+ KEY_ENTER = 257, // Key: Enter
+ KEY_TAB = 258, // Key: Tab
+ KEY_BACKSPACE = 259, // Key: Backspace
+ KEY_INSERT = 260, // Key: Ins
+ KEY_DELETE = 261, // Key: Del
+ KEY_RIGHT = 262, // Key: Cursor right
+ KEY_LEFT = 263, // Key: Cursor left
+ KEY_DOWN = 264, // Key: Cursor down
+ KEY_UP = 265, // Key: Cursor up
+ KEY_PAGE_UP = 266, // Key: Page up
+ KEY_PAGE_DOWN = 267, // Key: Page down
+ KEY_HOME = 268, // Key: Home
+ KEY_END = 269, // Key: End
+ KEY_CAPS_LOCK = 280, // Key: Caps lock
+ KEY_SCROLL_LOCK = 281, // Key: Scroll down
+ KEY_NUM_LOCK = 282, // Key: Num lock
+ KEY_PRINT_SCREEN = 283, // Key: Print screen
+ KEY_PAUSE = 284, // Key: Pause
+ KEY_F1 = 290, // Key: F1
+ KEY_F2 = 291, // Key: F2
+ KEY_F3 = 292, // Key: F3
+ KEY_F4 = 293, // Key: F4
+ KEY_F5 = 294, // Key: F5
+ KEY_F6 = 295, // Key: F6
+ KEY_F7 = 296, // Key: F7
+ KEY_F8 = 297, // Key: F8
+ KEY_F9 = 298, // Key: F9
+ KEY_F10 = 299, // Key: F10
+ KEY_F11 = 300, // Key: F11
+ KEY_F12 = 301, // Key: F12
+ KEY_LEFT_SHIFT = 340, // Key: Shift left
+ KEY_LEFT_CONTROL = 341, // Key: Control left
+ KEY_LEFT_ALT = 342, // Key: Alt left
+ KEY_LEFT_SUPER = 343, // Key: Super left
+ KEY_RIGHT_SHIFT = 344, // Key: Shift right
+ KEY_RIGHT_CONTROL = 345, // Key: Control right
+ KEY_RIGHT_ALT = 346, // Key: Alt right
+ KEY_RIGHT_SUPER = 347, // Key: Super right
+ KEY_KB_MENU = 348, // Key: KB menu
+ // Keypad keys
+ KEY_KP_0 = 320, // Key: Keypad 0
+ KEY_KP_1 = 321, // Key: Keypad 1
+ KEY_KP_2 = 322, // Key: Keypad 2
+ KEY_KP_3 = 323, // Key: Keypad 3
+ KEY_KP_4 = 324, // Key: Keypad 4
+ KEY_KP_5 = 325, // Key: Keypad 5
+ KEY_KP_6 = 326, // Key: Keypad 6
+ KEY_KP_7 = 327, // Key: Keypad 7
+ KEY_KP_8 = 328, // Key: Keypad 8
+ KEY_KP_9 = 329, // Key: Keypad 9
+ KEY_KP_DECIMAL = 330, // Key: Keypad .
+ KEY_KP_DIVIDE = 331, // Key: Keypad /
+ KEY_KP_MULTIPLY = 332, // Key: Keypad *
+ KEY_KP_SUBTRACT = 333, // Key: Keypad -
+ KEY_KP_ADD = 334, // Key: Keypad +
+ KEY_KP_ENTER = 335, // Key: Keypad Enter
+ KEY_KP_EQUAL = 336, // Key: Keypad =
+ // Android key buttons
+ KEY_BACK = 4, // Key: Android back button
+ KEY_MENU = 82, // Key: Android menu button
+ KEY_VOLUME_UP = 24, // Key: Android volume up button
+ KEY_VOLUME_DOWN = 25 // Key: Android volume down button
+} KeyboardKey;
// Texture //
@@ -52,4 +184,22 @@ void unload_active_shaders(void);
// Text //
-void draw_text(const char* txt, int x, int y, int size, Color col); \ No newline at end of file
+void draw_text(const char* txt, int x, int y, int size, Color col);
+
+// Controller //
+
+b32 is_key_pressed(i32 btn);
+b32 is_key_held(i32 btn);
+b32 is_key_released(i32 btn);
+
+vec2 get_dir_input(void);
+
+void set_rumble(f32 duration, f32 strength);
+
+void stop_rumble(void);
+
+// Random //
+
+u32 rand_u32(void);
+
+int rand_range(i32 min, i32 max);
diff --git a/assets/src/main.c b/assets/src/main.c
index 68ab0f1..2a8ab80 100644
--- a/assets/src/main.c
+++ b/assets/src/main.c
@@ -2,6 +2,7 @@
static int femme = -1;
static int grayscale = -1;
+static vec2 position = (vec2){TO_FIXED(32), TO_FIXED(48)};
void init(void)
{
@@ -11,9 +12,18 @@ void init(void)
void update(void)
{
- draw_text("Femme my beloved!", 4, 4, 20, BLACK);
+ if(is_key_held(KEY_B)){
+ draw_text("Femme my beloved!", 4, 4, 20, BLACK);
+ }
+
+ vec2 current_input = get_dir_input();
+ if(current_input.x != 0 || current_input.y != 0){
+ position.x += current_input.x * 0.5f;
+ position.y += current_input.y * 0.5f;
+ }
+
set_active_shader(grayscale);
- draw_texture(femme, 32, 48);
+ draw_texture(femme, FROM_FIXED(position.x), FROM_FIXED(position.y));
reset_active_shader();
}
diff --git a/src/config.h b/src/config.h
index e0f8fe7..40a095c 100644
--- a/src/config.h
+++ b/src/config.h
@@ -36,7 +36,7 @@ typedef i32 fx32;
#define FIXED_POINT_ONE (1 << FIXED_POINT_BITS)
#define TO_FIXED(x) ((fx32)(x) << FIXED_POINT_BITS)
#define FROM_FIXED(x) ((x) >> FIXED_POINT_BITS)
-#define FP_TO_FIXED(x) (fx32)((x) * FIXED_POINT_BITS)
+#define FP_TO_FIXED(x) (fx32)((x) * FIXED_POINT_ONE)
typedef struct{
fx32 x, y;
diff --git a/src/controller.c b/src/controller.c
index aa8d4d9..e52eec5 100644
--- a/src/controller.c
+++ b/src/controller.c
@@ -2,17 +2,17 @@
#include <raylib.h>
-b32 is_button_pressed(i32 btn)
+b32 is_key_pressed(i32 btn)
{
return IsKeyPressed(btn);
}
-b32 is_button_held(i32 btn)
+b32 is_key_held(i32 btn)
{
return IsKeyDown(btn);
}
-b32 is_button_released(i32 btn)
+b32 is_key_released(i32 btn)
{
return IsKeyReleased(btn);
}
@@ -22,10 +22,10 @@ vec2 get_dir_input(void)
fx32 cont_x = FP_TO_FIXED(GetGamepadAxisMovement(0, 0));
fx32 cont_y = FP_TO_FIXED(GetGamepadAxisMovement(0, 1));
- fx32 key_w = is_button_held(KEY_W) ? FIXED_POINT_ONE : 0;
- fx32 key_s = is_button_held(KEY_S) ? FIXED_POINT_ONE : 0;
- fx32 key_a = is_button_held(KEY_A) ? FIXED_POINT_ONE : 0;
- fx32 key_d = is_button_held(KEY_D) ? FIXED_POINT_ONE : 0;
+ fx32 key_w = IsKeyDown(KEY_W) ? FIXED_POINT_ONE : 0;
+ fx32 key_s = IsKeyDown(KEY_S) ? FIXED_POINT_ONE : 0;
+ fx32 key_a = IsKeyDown(KEY_A) ? FIXED_POINT_ONE : 0;
+ fx32 key_d = IsKeyDown(KEY_D) ? FIXED_POINT_ONE : 0;
// Either Gamepad or Keyboard control, not both!
if(key_w != 0 || key_s != 0 || key_a != 0 || key_d != 0){
diff --git a/src/controller.h b/src/controller.h
index 55098cc..e4a7683 100644
--- a/src/controller.h
+++ b/src/controller.h
@@ -4,9 +4,9 @@
// TODO: Controller button presses you DINGUS
-b32 is_button_pressed(i32 btn);
-b32 is_button_held(i32 btn);
-b32 is_button_released(i32 btn);
+b32 is_key_pressed(i32 btn);
+b32 is_key_held(i32 btn);
+b32 is_key_released(i32 btn);
vec2 get_dir_input(void);
diff --git a/src/main.c b/src/main.c
index c881a18..4fc9bc2 100644
--- a/src/main.c
+++ b/src/main.c
@@ -3,17 +3,25 @@
#include "shader_sys.h"
#include "script_sys.h"
+#include "controller.h"
+#include "rand_sys.h"
+
int main()
{
InitWindow(640, 480, "Gunner Engine");
SetTargetFPS(60);
+ rand_seed();
+
init_tex_sys();
init_shader_sys();
init_script_sys();
while(!WindowShouldClose()){
+ float dt = GetFrameTime();
+ poll_rumble(dt);
+
update_textures();
update_shaders();
diff --git a/src/rumble.c b/src/rumble.c
index 798b5ec..b2da191 100644
--- a/src/rumble.c
+++ b/src/rumble.c
@@ -7,6 +7,7 @@ NOTE FOR SPELUNKERS: this requires linking to xinput1_4 AFAIK
*/
#include "controller.h"
+#include <stdio.h>
// Windows stuff starts //
typedef struct _XINPUT_VIBRATION {
diff --git a/src/script_sys.c b/src/script_sys.c
index 4c87327..761d874 100644
--- a/src/script_sys.c
+++ b/src/script_sys.c
@@ -2,6 +2,10 @@
#include "texture_sys.h"
#include "shader_sys.h"
+#include "controller.h"
+
+#include "rand_sys.h"
+
#include <libtcc.h>
#include <stddef.h>
#include <stdio.h>
@@ -29,6 +33,18 @@ static void restart(void)
tcc_add_symbol(new_state, "draw_texture", draw_texture);
tcc_add_symbol(new_state, "reset_active_shader", reset_active_shader);
+ tcc_add_symbol(new_state, "set_rumble", set_rumble);
+ tcc_add_symbol(new_state, "stop_rumble", stop_rumble);
+
+ tcc_add_symbol(new_state, "rand_u32", rand_u32);
+ tcc_add_symbol(new_state, "rand_range", rand_range);
+
+ tcc_add_symbol(new_state, "is_key_pressed", is_key_pressed);
+ tcc_add_symbol(new_state, "is_key_held", is_key_held);
+ tcc_add_symbol(new_state, "is_key_released", is_key_released);
+
+ tcc_add_symbol(new_state, "get_dir_input", get_dir_input);
+
if(tcc_add_file(new_state, "assets/src/main.c") == -1){
TRACELOG(LOG_ERROR, "Compilation failed!\n");
return;