Simplify input keystate send

isolation_bkp/dynres
Dominik Madarász 2021-11-01 15:32:46 +01:00
parent 8ab803bb5f
commit c45f95348a
6 changed files with 116 additions and 140 deletions

View File

@ -21,7 +21,6 @@
#include "packets/pkt_00_init.h" #include "packets/pkt_00_init.h"
#include "packets/pkt_01_welcome.h" #include "packets/pkt_01_welcome.h"
#include "packets/pkt_send_keystate.h"
static uint8_t game_mode; static uint8_t game_mode;
@ -225,19 +224,8 @@ void game_render() {
} }
} }
void game_action_send_keystate(float x, void game_action_send_keystate(game_keystate_data *data) {
float y, pkt_send_keystate_send(active_viewer->view_id, data);
float mx,
float my,
uint8_t use,
uint8_t sprint,
uint8_t ctrl,
uint8_t drop,
uint8_t selected_item,
uint8_t swap,
uint8_t swap_from,
uint8_t swap_to) {
pkt_send_keystate_send(active_viewer->view_id, x, y, mx, my, use, sprint, ctrl, drop, selected_item, swap, swap_from, swap_to);
} }
void game_request_close() { void game_request_close() {

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "system.h" #include "system.h"
#include "world_view.h" #include "world_view.h"
#include "packets/pkt_send_keystate.h"
typedef enum { typedef enum {
GAMEKIND_SINGLE, GAMEKIND_SINGLE,
@ -32,15 +33,4 @@ void game_world_view_active_entity_map(void (*map_proc)(uint64_t key, entity_vie
entity_view *game_world_view_active_get_entity(uint64_t ent_id); entity_view *game_world_view_active_get_entity(uint64_t ent_id);
//~ NOTE(zaklaus): viewer -> host actions //~ NOTE(zaklaus): viewer -> host actions
void game_action_send_keystate(float x, void game_action_send_keystate(game_keystate_data *data);
float y,
float mx,
float my,
uint8_t use,
uint8_t sprint,
uint8_t ctrl,
uint8_t drop,
uint8_t selected_item,
uint8_t swap,
uint8_t swap_from,
uint8_t swap_to);

View File

@ -0,0 +1 @@
#pragma once

View File

@ -1,76 +1,64 @@
#include "packet_utils.h" #include "packet_utils.h"
#include "network.h" #include "network.h"
#include "packets/pkt_send_keystate.h" #include "packets/pkt_send_keystate.h"
#include "modules/components.h" #include "modules/components.h"
#include "modules/systems.h" #include "modules/systems.h"
#include "world/world.h" #include "world/world.h"
#include "debug_replay.h" #include "debug_replay.h"
pkt_desc pkt_send_keystate_desc[] = { pkt_desc pkt_send_keystate_desc[] = {
{ PKT_REAL(pkt_send_keystate, x) }, { PKT_REAL(pkt_send_keystate, x) },
{ PKT_REAL(pkt_send_keystate, y) }, { PKT_REAL(pkt_send_keystate, y) },
{ PKT_REAL(pkt_send_keystate, mx) }, { PKT_REAL(pkt_send_keystate, mx) },
{ PKT_REAL(pkt_send_keystate, my) }, { PKT_REAL(pkt_send_keystate, my) },
{ PKT_UINT(pkt_send_keystate, use) }, { PKT_UINT(pkt_send_keystate, use) },
{ PKT_UINT(pkt_send_keystate, sprint) }, { PKT_UINT(pkt_send_keystate, sprint) },
{ PKT_UINT(pkt_send_keystate, ctrl) }, { PKT_UINT(pkt_send_keystate, ctrl) },
{ PKT_UINT(pkt_send_keystate, selected_item) }, { PKT_UINT(pkt_send_keystate, selected_item) },
{ PKT_UINT(pkt_send_keystate, drop) }, { PKT_UINT(pkt_send_keystate, drop) },
{ PKT_UINT(pkt_send_keystate, swap) }, { PKT_UINT(pkt_send_keystate, swap) },
{ PKT_UINT(pkt_send_keystate, swap_from) }, { PKT_UINT(pkt_send_keystate, swap_from) },
{ PKT_UINT(pkt_send_keystate, swap_to) }, { PKT_UINT(pkt_send_keystate, swap_to) },
{ PKT_END }, { PKT_END },
}; };
size_t pkt_send_keystate_send(uint16_t view_id, size_t pkt_send_keystate_send(uint16_t view_id,
float x, game_keystate_data *data) {
float y, return pkt_world_write(MSG_ID_SEND_KEYSTATE, pkt_send_keystate_encode(data), 1, view_id, NULL, 1);
float mx, }
float my,
uint8_t use, size_t pkt_send_keystate_encode(pkt_send_keystate *table) {
uint8_t sprint, cw_pack_context pc = {0};
uint8_t ctrl, pkt_pack_msg(&pc, pkt_pack_desc_args(pkt_send_keystate_desc));
uint8_t drop, pkt_pack_struct(&pc, pkt_send_keystate_desc, PKT_STRUCT_PTR(table));
uint8_t selected_item, return pkt_pack_msg_size(&pc);
uint8_t swap, }
uint8_t swap_from,
uint8_t swap_to) { int32_t pkt_send_keystate_handler(pkt_header *header) {
pkt_send_keystate table = { .x = x, .y = y, .mx = mx, .my = my, .use = use, .sprint = sprint, .ctrl = ctrl, .drop = drop, .selected_item = selected_item, .swap = swap, .swap_from = swap_from, .swap_to = swap_to }; pkt_send_keystate table;
return pkt_world_write(MSG_ID_SEND_KEYSTATE, pkt_send_keystate_encode(&table), 1, view_id, NULL, 1); PKT_IF(pkt_msg_decode(header, pkt_send_keystate_desc, pkt_pack_desc_args(pkt_send_keystate_desc), PKT_STRUCT_PTR(&table)));
} ecs_entity_t e = network_server_get_entity(header->udata);
size_t pkt_send_keystate_encode(pkt_send_keystate *table) { if (!world_entity_valid(e))
cw_pack_context pc = {0}; return 1;
pkt_pack_msg(&pc, pkt_pack_desc_args(pkt_send_keystate_desc));
pkt_pack_struct(&pc, pkt_send_keystate_desc, PKT_STRUCT_PTR(table)); Input *i = ecs_get_mut(world_ecs(), e, Input, NULL);
return pkt_pack_msg_size(&pc); if (i && !i->is_blocked) {
} i->x = zpl_clamp(table.x, -1.0f, 1.0f);
i->y = zpl_clamp(table.y, -1.0f, 1.0f);
int32_t pkt_send_keystate_handler(pkt_header *header) { i->mx = table.mx;
pkt_send_keystate table; i->my = table.my;
PKT_IF(pkt_msg_decode(header, pkt_send_keystate_desc, pkt_pack_desc_args(pkt_send_keystate_desc), PKT_STRUCT_PTR(&table))); i->use = table.use;
ecs_entity_t e = network_server_get_entity(header->udata); i->sprint = table.sprint;
i->ctrl = table.ctrl;
if (!world_entity_valid(e)) i->selected_item = zpl_clamp(table.selected_item, 0, ITEMS_INVENTORY_SIZE-1);
return 1; i->drop = table.drop;
i->swap = table.swap;
Input *i = ecs_get_mut(world_ecs(), e, Input, NULL); i->swap_from = zpl_clamp(table.swap_from, 0, ITEMS_INVENTORY_SIZE-1);
if (i && !i->is_blocked) { i->swap_to = zpl_clamp(table.swap_to, 0, ITEMS_INVENTORY_SIZE-1);
i->x = zpl_clamp(table.x, -1.0f, 1.0f); debug_replay_record_keystate(table);
i->y = zpl_clamp(table.y, -1.0f, 1.0f); }
i->mx = table.mx;
i->my = table.my; return 0;
i->use = table.use; }
i->sprint = table.sprint;
i->ctrl = table.ctrl;
i->selected_item = zpl_clamp(table.selected_item, 0, ITEMS_INVENTORY_SIZE-1);
i->drop = table.drop;
i->swap = table.swap;
i->swap_from = zpl_clamp(table.swap_from, 0, ITEMS_INVENTORY_SIZE-1);
i->swap_to = zpl_clamp(table.swap_to, 0, ITEMS_INVENTORY_SIZE-1);
debug_replay_record_keystate(table);
}
return 0;
}

View File

@ -1,37 +1,28 @@
#pragma once #pragma once
#include "system.h" #include "system.h"
#include "packet_utils.h" #include "packet_utils.h"
typedef struct { typedef struct {
float x; float x;
float y; float y;
float mx; float mx;
float my; float my;
uint8_t use; uint8_t use;
uint8_t sprint; uint8_t sprint;
uint8_t ctrl; uint8_t ctrl;
uint8_t selected_item; uint8_t selected_item;
uint8_t drop; uint8_t drop;
uint8_t swap; uint8_t swap;
uint8_t swap_from; uint8_t swap_from;
uint8_t swap_to; uint8_t swap_to;
} pkt_send_keystate; } pkt_send_keystate;
size_t pkt_send_keystate_send(uint16_t view_id, typedef pkt_send_keystate game_keystate_data;
float x,
float y, size_t pkt_send_keystate_send(uint16_t view_id,
float mx, game_keystate_data *data);
float my, size_t pkt_send_keystate_encode(pkt_send_keystate *table);
uint8_t use, extern pkt_desc pkt_send_keystate_desc[];
uint8_t sprint,
uint8_t ctrl, PKT_HANDLER_PROC(pkt_send_keystate_handler);
uint8_t drop,
uint8_t selected_item,
uint8_t swap,
uint8_t swap_from,
uint8_t swap_to);
size_t pkt_send_keystate_encode(pkt_send_keystate *table);
extern pkt_desc pkt_send_keystate_desc[];
PKT_HANDLER_PROC(pkt_send_keystate_handler);

View File

@ -30,6 +30,8 @@ void platform_init() {
screenWidth = GetScreenWidth(); screenWidth = GetScreenWidth();
screenHeight = GetScreenHeight(); screenHeight = GetScreenHeight();
ToggleFullscreen();
renderer_init(); renderer_init();
} }
@ -88,7 +90,23 @@ void platform_input() {
y = -mouse_pos.y; y = -mouse_pos.y;
} }
game_action_send_keystate(x, y, mouse_pos.x, mouse_pos.y, use, sprint, ctrl, drop, inv_selected_item, inv_swap, inv_swap_from, inv_swap_to); game_keystate_data data = {
.x = x,
.y = y,
.mx = mouse_pos.x,
.my = mouse_pos.y,
.use = use,
.sprint = sprint,
.ctrl = ctrl,
.drop = drop,
.selected_item = inv_selected_item,
.swap = inv_swap,
.swap_from = inv_swap_from,
.swap_to = inv_swap_to,
};
game_action_send_keystate(&data);
} }
// NOTE(zaklaus): cycle through viewers // NOTE(zaklaus): cycle through viewers