update input and opengl

This commit is contained in:
Dennis Eichhorn 2024-09-15 22:08:29 +02:00
parent c7c7686827
commit 51b0cedb5f
10 changed files with 1392 additions and 120 deletions

View File

@ -22,6 +22,7 @@
// @bug This means players might not be able to transition from one area to another?!
struct AssetManagementSystem {
// @question is this even necessary or could we integrate this directly into the system here?
HashMap hash_map;
// The indices of asset_memory and asset_data_memory are always linked
@ -161,7 +162,10 @@ Asset* ams_reserve_asset(AssetManagementSystem* ams, const char* name, uint64 el
// @performance Do we really want a double linked list. Are we really using this feature or is the free_index enough?
if (free_asset > 0 && free_asset < ams->asset_memory.count - 1) {
Asset* next = ams->first;
while (next->next->internal_id < asset->internal_id && next->internal_id < ams->asset_memory.count) {
while (next->next != NULL
&& next->next->internal_id < asset->internal_id
&& next->internal_id < ams->asset_memory.count
) {
next = next->next;
}

View File

@ -9,8 +9,6 @@
#ifndef TOS_GPUAPI_OPENGL_H
#define TOS_GPUAPI_OPENGL_H
#include <gl/GL.h>
// @todo remove some of the unused consts below
//

View File

@ -161,8 +161,6 @@ GLuint shader_make(OpenGL* gl, GLenum type, const char *source, RingMemory* ring
gl->glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE) {
ASSERT_SIMPLE(false);
GLint length;
gl->glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
@ -170,13 +168,15 @@ GLuint shader_make(OpenGL* gl, GLenum type, const char *source, RingMemory* ring
gl->glGetShaderInfoLog(shader, length, NULL, info);
ASSERT_SIMPLE(false);
// @todo log
}
return shader;
}
GLuint shader_load(OpenGL* gl, GLenum type, const char *path, RingMemory* ring) {
GLuint shader_load(OpenGL* gl, GLenum type, const char* path, RingMemory* ring) {
uint64 temp = ring->pos;
FileBody file;
@ -191,11 +191,21 @@ GLuint shader_load(OpenGL* gl, GLenum type, const char *path, RingMemory* ring)
return result;
}
GLuint program_make(OpenGL* gl, GLuint shader1, GLuint shader2, RingMemory* ring) {
GLuint program_make(
OpenGL* gl,
GLuint vertex_shader,
GLuint fragment_shader,
GLint geometry_shader,
RingMemory* ring
) {
GLuint program = gl->glCreateProgram();
gl->glAttachShader(program, shader1);
gl->glAttachShader(program, shader2);
if (geometry_shader > -1) {
gl->glAttachShader(program, geometry_shader);
}
gl->glAttachShader(program, vertex_shader);
gl->glAttachShader(program, fragment_shader);
gl->glLinkProgram(program);
GLint status;
@ -216,20 +226,40 @@ GLuint program_make(OpenGL* gl, GLuint shader1, GLuint shader2, RingMemory* ring
}
// @question really?
gl->glDetachShader(program, shader1);
gl->glDetachShader(program, shader2);
if (geometry_shader > -1) {
gl->glDetachShader(program, geometry_shader);
}
gl->glDetachShader(program, vertex_shader);
gl->glDetachShader(program, fragment_shader);
// @question really?
gl->glDeleteShader(shader1);
gl->glDeleteShader(shader2);
if (geometry_shader > -1) {
gl->glDeleteShader(geometry_shader);
}
gl->glDeleteShader(vertex_shader);
gl->glDeleteShader(fragment_shader);
return program;
}
GLuint program_load(OpenGL* gl, const char *path1, const char *path2, RingMemory* ring) {
GLuint shader1 = shader_load(gl, GL_VERTEX_SHADER, path1, ring);
GLuint shader2 = shader_load(gl, GL_FRAGMENT_SHADER, path2, ring);
GLuint program = program_make(gl, shader1, shader2, ring);
GLuint program_load(
OpenGL* gl,
const char* path1,
const char* path2,
const char* path3,
RingMemory* ring
) {
GLuint vertex_shader = shader_load(gl, GL_VERTEX_SHADER, path1, ring);
GLuint fragment_shader = shader_load(gl, GL_FRAGMENT_SHADER, path2, ring);
GLint geometry_shader = -1;
if (path3) {
geometry_shader = shader_load(gl, GL_GEOMETRY_SHADER, path3, ring);
}
GLuint program = program_make(gl, vertex_shader, fragment_shader, geometry_shader, ring);
return program;
}
@ -446,6 +476,30 @@ uint32 gpuapi_buffer_generate(OpenGL* gl, int size, void* data)
return vbo;
}
inline
uint32 gpuapi_shaderbuffer_generate(OpenGL* gl, int size, void* data)
{
uint32 sbo;
gl->glGenBuffers(1, &sbo);
gl->glBindBuffer(GL_SHADER_STORAGE_BUFFER, sbo);
gl->glBufferData(GL_SHADER_STORAGE_BUFFER, size, data, GL_DYNAMIC_DRAW);
return sbo;
}
inline
uint32 gpuapi_uniformbuffer_generate(OpenGL* gl, int size, void* data)
{
uint32 ubo;
gl->glGenBuffers(1, &ubo);
gl->glBindBuffer(GL_UNIFORM_BUFFER, ubo);
gl->glBufferData(GL_UNIFORM_BUFFER, size, data, GL_STATIC_DRAW);
return ubo;
}
inline
uint32 gpuapi_buffer_element_generate(OpenGL* gl, int size, uint32 *data)
{

File diff suppressed because it is too large Load Diff

View File

@ -12,8 +12,7 @@
#define MAX_KEY_PRESSES 5
#define MIN_INPUT_DEVICES 2
#define INPUT_TYPE_MOUSE 0x01
#define INPUT_TYPE_KEYBOARD 0x02
#define INPUT_TYPE_MOUSE_KEYBOARD 0x01
#define INPUT_TYPE_OTHER 0x03
#define MIN_CONTROLLER_DEVICES 4
@ -27,7 +26,6 @@
struct InputState {
// Device
bool is_connected = false;
char name[256];
byte type = INPUT_TYPE_OTHER;
double time;
@ -44,10 +42,10 @@ struct InputState {
// We only consider up to 4 pressed keys
// Depending on the keyboard you may only be able to detect a limited amount of key presses anyway
int up_index;
uint16 keys_down_old[MAX_KEY_PRESSES];
uint8 keys_down_old[MAX_KEY_PRESSES];
int down_index;
uint16 keys_down[MAX_KEY_PRESSES];
uint8 keys_down[MAX_KEY_PRESSES];
// Mouse
// After handling the mouse state change the game loop should set this to false
@ -59,15 +57,24 @@ struct InputState {
uint32 x_last;
uint32 y_last;
// https://usb.org/sites/default/files/hid1_11.pdf Page 71 or 61
// @question consider to use bit field (one int32 would be sufficient)
bool mouse_down_old[18];
bool mouse1_down[18];
// https://usb.org/sites/default/files/hid1_11.pdf Page 71 or 61 = 18
// the bitfield represents which button is pressed
uint32 mouse_down_old;
uint32 mouse_down;
int16 wheel_delta = 0;
uint32 raw_button = 0;
};
void input_transition(InputState* state)
{
// Mouse
state->x_last = state->x;
state->y_last = state->y;
state->state_change_mouse = false;
}
struct ControllerState {
uint32 id = 0;
bool is_connected = false;
@ -84,9 +91,9 @@ struct ControllerState {
byte trigger_old[4];
byte trigger[4];
// @question consider to use bit field (one int32 would be sufficient)
bool button_old[10];
bool button[10];
// these are bitfields
uint16 button_old;
uint16 button;
int16 stickl_x = 0;
int16 stickl_y = 0;

View File

@ -0,0 +1,20 @@
/**
* Jingga
*
* @copyright Jingga
* @license OMS License 2.0
* @version 1.0.0
* @link https://jingga.app
*/
#ifndef TOS_MODELS_SETTINGS_DUNGEON_SETTINGS_H
#define TOS_MODELS_SETTINGS_DUNGEON_SETTINGS_H
#include "../../stdlib/Types.h"
#include "ItemDistributionType.h"
struct DungeonSettings {
ItemDistributionType item_distribution;
ItemDistributionType gold_distribution;
};
#endif

View File

@ -0,0 +1,19 @@
/**
* Jingga
*
* @copyright Jingga
* @license OMS License 2.0
* @version 1.0.0
* @link https://jingga.app
*/
#ifndef TOS_MODELS_SETTINGS_ITEM_DISTRIBUTION_TYPE_H
#define TOS_MODELS_SETTINGS_ITEM_DISTRIBUTION_TYPE_H
enum ItemDistributionType {
ITEM_DISTRIBUTION_TYPE_DEFAULT, // Mix of individual and shared drops
ITEM_DISTRIBUTION_TYPE_INDIVIDUAL, // All loot is individual BUT not class specific
ITEM_DISTRIBUTION_TYPE_LEADER, // Leader receives all loot
ITEM_DISTRIBUTION_TYPE_GUILD_BANK, // Items go into guild bank
};
#endif

View File

@ -406,11 +406,13 @@ struct CSettings {
byte hotkeys_menu = 0x1B; // ESC
byte hotkeys_window_close = 0x1B; // ESC
byte hotkeys_marker_1 = 0x31; // 1
byte hotkeys_marker_2 = 0x32; // 2
byte hotkeys_marker_3 = 0x33; // 3
byte hotkeys_marker_4 = 0x34; // 4
byte hotkeys_marker_5 = 0x35; // 5
byte hotkeys_marker_1 = 0x31;
byte hotkeys_marker_2 = 0x32;
byte hotkeys_marker_3 = 0x33;
byte hotkeys_marker_4 = 0x34;
byte hotkeys_marker_5 = 0x35;
byte hotkeys_marker_6 = 0x36;
byte hotkeys_ping = 0x37;
// Camera settings/positions
// Makes it easy to switch to customizable camera positions

View File

@ -15,46 +15,61 @@
#include "../../../input/Input.h"
#include "../../../utils/TestUtils.h"
#include "../../../utils/MathUtils.h"
#include "../../../memory/RingMemory.h"
#include "../../../memory/BufferMemory.h"
#include <winDNS.h>
InputState* init_input(HWND hwnd)
// IMPORTANT:
// Even if it is nowhere documented (at least not to our knowledge) the GetRawInputDeviceInfoA, GetRawInputBuffer functions requried
// aligned memory. So far we only figured out that 4 bytes works, maybe this needs to be 8 in the future?!
int input_init(HWND hwnd, InputState* states, RingMemory* ring, BufferMemory* buf)
{
uint32 nDevices;
GetRawInputDeviceList(NULL, &nDevices, sizeof(RAWINPUTDEVICELIST));
PRAWINPUTDEVICELIST pRawInputDeviceList = (PRAWINPUTDEVICELIST) malloc(sizeof(RAWINPUTDEVICELIST) * nDevices);
nDevices = GetRawInputDeviceList(pRawInputDeviceList, &nDevices, sizeof(RAWINPUTDEVICELIST));
uint32 device_count;
GetRawInputDeviceList(NULL, &device_count, sizeof(RAWINPUTDEVICELIST));
PRAWINPUTDEVICELIST pRawInputDeviceList = (PRAWINPUTDEVICELIST) ring_get_memory(ring, sizeof(RAWINPUTDEVICELIST) * device_count, 4);
device_count = GetRawInputDeviceList(pRawInputDeviceList, &device_count, sizeof(RAWINPUTDEVICELIST));
// We always want at least one empty input device slot
// @todo Change so that we store the actual number of devices
InputState *inputs = (InputState *) calloc((nDevices + 1), sizeof(InputState));
if (nDevices == 0) {
free(pRawInputDeviceList);
return inputs;
if (device_count == 0) {
return 0;
}
uint32 cb_size = 256;
for (uint32 i = 0; i < nDevices; ++i) {
GetRawInputDeviceInfoA(pRawInputDeviceList[i].hDevice, RIDI_DEVICENAME, inputs[i].name, &cb_size);
int32 mouse_found = 0;
int32 keyboard_found = 0;
int32 i;
for (i = 0; i < device_count; ++i) {
cb_size = sizeof(RID_DEVICE_INFO);
RID_DEVICE_INFO rdi;
GetRawInputDeviceInfoA(pRawInputDeviceList[i].hDevice, RIDI_DEVICEINFO, &rdi, &cb_size);
switch (rdi.dwType) {
case RIM_TYPEMOUSE: {
inputs[i].handle_mouse = pRawInputDeviceList[i].hDevice;
inputs[i].is_connected = true;
inputs[i].type = INPUT_TYPE_MOUSE;
// @bug Would need fixing once we support controllers here
if (states[mouse_found].handle_mouse != NULL) {
++mouse_found;
}
states[mouse_found].handle_mouse = pRawInputDeviceList[i].hDevice;
states[mouse_found].is_connected = true;
states[mouse_found].type = INPUT_TYPE_MOUSE_KEYBOARD;
} break;
case RIM_TYPEKEYBOARD: {
inputs[i].handle_keyboard = pRawInputDeviceList[i].hDevice;
inputs[i].is_connected = true;
inputs[i].type = INPUT_TYPE_KEYBOARD;
// @bug Would need fixing once we support controllers here (keyboard + controller in one input bug)
if (states[keyboard_found].handle_keyboard != NULL) {
++keyboard_found;
}
states[keyboard_found].handle_keyboard = pRawInputDeviceList[i].hDevice;
states[keyboard_found].is_connected = true;
states[keyboard_found].type = INPUT_TYPE_MOUSE_KEYBOARD;
} break;
case RIM_TYPEHID: {
inputs[i].type = INPUT_TYPE_OTHER;
states[i].type = INPUT_TYPE_OTHER;
} break;
default: {
@ -90,39 +105,34 @@ InputState* init_input(HWND hwnd)
if (!RegisterRawInputDevices((PCRAWINPUTDEVICE) rid, 4, sizeof(RAWINPUTDEVICE))) {
// @todo Log
ASSERT_SIMPLE(false);
}
// free(rid);
free(pRawInputDeviceList);
return inputs;
return i;
}
void handle_input(LPARAM lParam, InputState* states)
void input_raw_handle(RAWINPUT* raw, InputState* states, int state_count)
{
uint32 db_size;
GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &db_size, sizeof(RAWINPUTHEADER));
// @todo pull out, we only need to register this memory once
// Maybe even put it into the general memory pool
LPBYTE lpb = (BYTE *) malloc(db_size * sizeof(BYTE));
GetRawInputData((HRAWINPUT)lParam, RID_INPUT, lpb, &db_size, sizeof(RAWINPUTHEADER));
RAWINPUT* raw = (RAWINPUT*) lpb;
uint32 i = 0;
if (raw->header.dwType == RIM_TYPEMOUSE) {
// @todo Change so we can directly access the correct state (maybe map handle address to index?)
while (states[i].is_connected && states[i].handle_mouse != raw->header.hDevice) {++i;}
while (i < state_count
&& states[i].handle_mouse != raw->header.hDevice
) {
++i;
}
if (!states[i].is_connected) {
if (i >= state_count || !states[i].is_connected) {
return;
}
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-rawmouse
InputState* input_state = states + i;
if (raw->data.mouse.usFlags & MOUSE_MOVE_ABSOLUTE) {
RECT rect;
// @todo move out, this is slow and should be stored in Window
// @performance this is slow and should be handled in the WindowProc !!!
if (raw->data.mouse.usFlags & MOUSE_VIRTUAL_DESKTOP) {
rect.left = GetSystemMetrics(SM_XVIRTUALSCREEN);
rect.top = GetSystemMetrics(SM_YVIRTUALSCREEN);
@ -135,43 +145,90 @@ void handle_input(LPARAM lParam, InputState* states)
rect.bottom = GetSystemMetrics(SM_CYSCREEN);
}
states[i].x_last = states[i].x;
states[i].y_last = states[i].y;
input_state->x_last = input_state->x;
input_state->y_last = input_state->y;
states[i].x = MulDiv(raw->data.mouse.lLastX, rect.right, 65535) + rect.left;
states[i].y = MulDiv(raw->data.mouse.lLastY, rect.bottom, 65535) + rect.top;
input_state->x = MulDiv(raw->data.mouse.lLastX, rect.right, 65535) + rect.left;
input_state->y = MulDiv(raw->data.mouse.lLastY, rect.bottom, 65535) + rect.top;
states[i].state_change_mouse = true;
input_state->state_change_mouse = true;
} else if (raw->data.mouse.lLastX != 0 || raw->data.mouse.lLastY != 0) {
states[i].x_last = states[i].x;
states[i].y_last = states[i].y;
input_state->x_last = input_state->x;
input_state->y_last = input_state->y;
states[i].x = states[i].x + raw->data.mouse.lLastX;
states[i].y = states[i].y + raw->data.mouse.lLastY;
input_state->x = input_state->x + raw->data.mouse.lLastX;
input_state->y = input_state->y + raw->data.mouse.lLastY;
states[i].state_change_mouse = true;
input_state->state_change_mouse = true;
}
} else if (raw->header.dwType == RIM_TYPEKEYBOARD) {
// @todo Change so we can directly access the correct state (maybe map handle address to index?)
while (states[i].is_connected && states[i].handle_keyboard != raw->header.hDevice) {++i;}
while (i < state_count
&& states[i].handle_keyboard != raw->header.hDevice
) {
++i;
}
if (!states[i].is_connected) {
if (i >= state_count || !states[i].is_connected) {
return;
}
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-rawkeyboard
InputState* input_state = states + i;
RAWKEYBOARD rawKB = raw->data.keyboard;
RAWKEYBOARD raw_kb = raw->data.keyboard;
if (rawKB.Flags & RI_KEY_BREAK) {
states[i].keys_down_old[states[i].up_index++] = rawKB.MakeCode;
if (raw_kb.Flags & RI_KEY_BREAK) {
input_state->keys_down_old[input_state->up_index++] = (uint8) raw_kb.VKey;
}
if (rawKB.Flags & RI_KEY_MAKE) {
states[i].keys_down[states[i].down_index++] = rawKB.MakeCode;
if (raw_kb.Flags & RI_KEY_MAKE) {
input_state->keys_down[input_state->down_index++] = (uint8) raw_kb.VKey;
}
states[i].state_change_keyboard = true;
input_state->state_change_keyboard = true;
}
}
void input_handle(LPARAM lParam, InputState* states, int state_count, RingMemory* ring)
{
uint32 db_size;
GetRawInputData((HRAWINPUT) lParam, RID_INPUT, NULL, &db_size, sizeof(RAWINPUTHEADER));
// @todo pull out, we only need to register this memory once
// Maybe even put it into the general memory pool
LPBYTE lpb = (BYTE *) ring_get_memory(ring, db_size * sizeof(BYTE), 4);
uint32 size = GetRawInputData((HRAWINPUT) lParam, RID_INPUT, lpb, &db_size, sizeof(RAWINPUTHEADER));
if (db_size != size) {
return;
}
input_raw_handle((RAWINPUT *) lpb, states, state_count);
}
void input_handle_buffered(LPARAM lParam, int buffer_size, InputState* states, int state_count, RingMemory* ring)
{
uint32 cb_size;
GetRawInputBuffer(NULL, &cb_size, sizeof(RAWINPUTHEADER));
// Max input messages (e.g. 16)
cb_size *= buffer_size;
PRAWINPUT raw_input = (PRAWINPUT) ring_get_memory(ring, cb_size, 4);
uint32 input;
uint32 cb_size_t = cb_size;
while ((input = GetRawInputBuffer(raw_input, &cb_size_t, sizeof(RAWINPUTHEADER))) > 0) {
PRAWINPUT pri = raw_input;
for (uint32 i = 0; i < input; ++i) {
input_raw_handle(pri, states, state_count);
pri = NEXTRAWINPUTBLOCK(pri);
}
// @question is this asign necessary?
cb_size_t = cb_size;
}
}

View File

@ -98,6 +98,7 @@ void handle_controller_input(ControllerState* states)
continue;
}
/*
states[controller_index].up = controller_state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP;
states[controller_index].down = controller_state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN;
states[controller_index].left = controller_state.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT;
@ -123,6 +124,7 @@ void handle_controller_input(ControllerState* states)
states[controller_index].stickr_x = controller_state.Gamepad.sThumbRX;
states[controller_index].stickr_y = controller_state.Gamepad.sThumbRY;
states[controller_index].stickr_press = controller_state.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB;
*/
++controller_index;
}