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_01_welcome.h"
#include "packets/pkt_send_keystate.h"
static uint8_t game_mode;
@ -225,19 +224,8 @@ void game_render() {
}
}
void game_action_send_keystate(float x,
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) {
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_action_send_keystate(game_keystate_data *data) {
pkt_send_keystate_send(active_viewer->view_id, data);
}
void game_request_close() {

View File

@ -1,6 +1,7 @@
#pragma once
#include "system.h"
#include "world_view.h"
#include "packets/pkt_send_keystate.h"
typedef enum {
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);
//~ NOTE(zaklaus): viewer -> host actions
void game_action_send_keystate(float x,
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);
void game_action_send_keystate(game_keystate_data *data);

View File

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

View File

@ -1,76 +1,64 @@
#include "packet_utils.h"
#include "network.h"
#include "packets/pkt_send_keystate.h"
#include "modules/components.h"
#include "modules/systems.h"
#include "world/world.h"
#include "debug_replay.h"
pkt_desc pkt_send_keystate_desc[] = {
{ PKT_REAL(pkt_send_keystate, x) },
{ PKT_REAL(pkt_send_keystate, y) },
{ PKT_REAL(pkt_send_keystate, mx) },
{ PKT_REAL(pkt_send_keystate, my) },
{ PKT_UINT(pkt_send_keystate, use) },
{ PKT_UINT(pkt_send_keystate, sprint) },
{ PKT_UINT(pkt_send_keystate, ctrl) },
{ PKT_UINT(pkt_send_keystate, selected_item) },
{ PKT_UINT(pkt_send_keystate, drop) },
{ PKT_UINT(pkt_send_keystate, swap) },
{ PKT_UINT(pkt_send_keystate, swap_from) },
{ PKT_UINT(pkt_send_keystate, swap_to) },
{ PKT_END },
};
size_t pkt_send_keystate_send(uint16_t view_id,
float x,
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) {
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 };
return pkt_world_write(MSG_ID_SEND_KEYSTATE, pkt_send_keystate_encode(&table), 1, view_id, NULL, 1);
}
size_t pkt_send_keystate_encode(pkt_send_keystate *table) {
cw_pack_context pc = {0};
pkt_pack_msg(&pc, pkt_pack_desc_args(pkt_send_keystate_desc));
pkt_pack_struct(&pc, pkt_send_keystate_desc, PKT_STRUCT_PTR(table));
return pkt_pack_msg_size(&pc);
}
int32_t pkt_send_keystate_handler(pkt_header *header) {
pkt_send_keystate table;
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);
if (!world_entity_valid(e))
return 1;
Input *i = ecs_get_mut(world_ecs(), e, Input, NULL);
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);
i->mx = table.mx;
i->my = table.my;
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;
}
#include "packet_utils.h"
#include "network.h"
#include "packets/pkt_send_keystate.h"
#include "modules/components.h"
#include "modules/systems.h"
#include "world/world.h"
#include "debug_replay.h"
pkt_desc pkt_send_keystate_desc[] = {
{ PKT_REAL(pkt_send_keystate, x) },
{ PKT_REAL(pkt_send_keystate, y) },
{ PKT_REAL(pkt_send_keystate, mx) },
{ PKT_REAL(pkt_send_keystate, my) },
{ PKT_UINT(pkt_send_keystate, use) },
{ PKT_UINT(pkt_send_keystate, sprint) },
{ PKT_UINT(pkt_send_keystate, ctrl) },
{ PKT_UINT(pkt_send_keystate, selected_item) },
{ PKT_UINT(pkt_send_keystate, drop) },
{ PKT_UINT(pkt_send_keystate, swap) },
{ PKT_UINT(pkt_send_keystate, swap_from) },
{ PKT_UINT(pkt_send_keystate, swap_to) },
{ PKT_END },
};
size_t pkt_send_keystate_send(uint16_t view_id,
game_keystate_data *data) {
return pkt_world_write(MSG_ID_SEND_KEYSTATE, pkt_send_keystate_encode(data), 1, view_id, NULL, 1);
}
size_t pkt_send_keystate_encode(pkt_send_keystate *table) {
cw_pack_context pc = {0};
pkt_pack_msg(&pc, pkt_pack_desc_args(pkt_send_keystate_desc));
pkt_pack_struct(&pc, pkt_send_keystate_desc, PKT_STRUCT_PTR(table));
return pkt_pack_msg_size(&pc);
}
int32_t pkt_send_keystate_handler(pkt_header *header) {
pkt_send_keystate table;
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);
if (!world_entity_valid(e))
return 1;
Input *i = ecs_get_mut(world_ecs(), e, Input, NULL);
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);
i->mx = table.mx;
i->my = table.my;
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
#include "system.h"
#include "packet_utils.h"
typedef struct {
float x;
float y;
float mx;
float my;
uint8_t use;
uint8_t sprint;
uint8_t ctrl;
uint8_t selected_item;
uint8_t drop;
uint8_t swap;
uint8_t swap_from;
uint8_t swap_to;
} pkt_send_keystate;
size_t pkt_send_keystate_send(uint16_t view_id,
float x,
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);
size_t pkt_send_keystate_encode(pkt_send_keystate *table);
extern pkt_desc pkt_send_keystate_desc[];
PKT_HANDLER_PROC(pkt_send_keystate_handler);
#pragma once
#include "system.h"
#include "packet_utils.h"
typedef struct {
float x;
float y;
float mx;
float my;
uint8_t use;
uint8_t sprint;
uint8_t ctrl;
uint8_t selected_item;
uint8_t drop;
uint8_t swap;
uint8_t swap_from;
uint8_t swap_to;
} pkt_send_keystate;
typedef pkt_send_keystate game_keystate_data;
size_t pkt_send_keystate_send(uint16_t view_id,
game_keystate_data *data);
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();
screenHeight = GetScreenHeight();
ToggleFullscreen();
renderer_init();
}
@ -88,7 +90,23 @@ void platform_input() {
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