Revert "all storage systems are dynamic now"

This reverts commit b6b632899d.
isolation
Dominik Madarász 2022-09-27 15:22:13 +00:00 committed by GitHub
parent d02439b033
commit 0c95f1148a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 318 additions and 583 deletions

View File

@ -6,7 +6,6 @@
#include "platform/signal_handling.h" #include "platform/signal_handling.h"
#include "net/network.h" #include "net/network.h"
#include "ents/entity.h" #include "ents/entity.h"
#include "ents/items.h"
#include "world/world_view.h" #include "world/world_view.h"
#include "world/entity_view.h" #include "world/entity_view.h"
#include "core/camera.h" #include "core/camera.h"
@ -32,7 +31,7 @@ static WORLD_PKT_READER(pkt_reader) {
pkt_header header = {0}; pkt_header header = {0};
uint32_t ok = pkt_header_decode(&header, data, datalen); uint32_t ok = pkt_header_decode(&header, data, datalen);
header.udata = udata; header.udata = udata;
if (ok && header.ok) { if (ok && header.ok) {
return pkt_handlers[header.id].handler(&header) >= 0; return pkt_handlers[header.id].handler(&header) >= 0;
} else { } else {
@ -67,7 +66,7 @@ static WORLD_PKT_WRITER(mp_cli_pkt_writer) {
void world_viewers_init(uint32_t num_viewers) { void world_viewers_init(uint32_t num_viewers) {
zpl_buffer_init(world_viewers, zpl_heap(), num_viewers); zpl_buffer_init(world_viewers, zpl_heap(), num_viewers);
for (uint32_t i = 0; i < num_viewers; i++) { for (uint32_t i = 0; i < num_viewers; i++) {
zpl_buffer_append(world_viewers, world_view_create(i)); zpl_buffer_append(world_viewers, world_view_create(i));
} }
@ -129,39 +128,31 @@ float game_time() {
void game_init(const char *ip, uint16_t port, game_kind play_mode, uint32_t num_viewers, int32_t seed, uint16_t chunk_size, uint16_t chunk_amount, int8_t is_dash_enabled) { void game_init(const char *ip, uint16_t port, game_kind play_mode, uint32_t num_viewers, int32_t seed, uint16_t chunk_size, uint16_t chunk_amount, int8_t is_dash_enabled) {
game_mode = play_mode; game_mode = play_mode;
game_should_close = false; game_should_close = false;
#ifndef _DEBUG #ifndef _DEBUG
const char *host_ip = "lab.zakto.pw"; const char *host_ip = "lab.zakto.pw";
#else #else
const char *host_ip = "127.0.0.1"; const char *host_ip = "127.0.0.1";
#endif #endif
uint16_t host_port = (port > 0) ? port : 27000; uint16_t host_port = (port > 0) ? port : 27000;
if (ip != NULL) { if (ip != NULL) {
host_ip = ip; host_ip = ip;
} }
// NOTE: initialise subsystems
{
assets_setup();
blocks_setup();
item_setup();
entity_spawndef_setup();
}
if (game_mode != GAMEKIND_HEADLESS) { if (game_mode != GAMEKIND_HEADLESS) {
platform_init(); platform_init();
world_viewers_init(num_viewers); world_viewers_init(num_viewers);
active_viewer = &world_viewers[0]; active_viewer = &world_viewers[0];
camera_reset(); camera_reset();
} }
if (game_mode != GAMEKIND_SINGLE) { if (game_mode != GAMEKIND_SINGLE) {
network_init(); network_init();
} }
if (game_mode == GAMEKIND_CLIENT) { if (game_mode == GAMEKIND_CLIENT) {
world_setup_pkt_handlers(pkt_reader, mp_cli_pkt_writer); world_setup_pkt_handlers(pkt_reader, mp_cli_pkt_writer);
network_client_connect(host_ip, host_port); network_client_connect(host_ip, host_port);
@ -170,13 +161,13 @@ void game_init(const char *ip, uint16_t port, game_kind play_mode, uint32_t num_
world_setup_pkt_handlers(pkt_reader, game_mode == GAMEKIND_SINGLE ? sp_pkt_writer : mp_pkt_writer); world_setup_pkt_handlers(pkt_reader, game_mode == GAMEKIND_SINGLE ? sp_pkt_writer : mp_pkt_writer);
world_init(seed, chunk_size, chunk_amount); world_init(seed, chunk_size, chunk_amount);
if (is_dash_enabled) flecs_dash_init(); if (is_dash_enabled) flecs_dash_init();
if (game_mode == GAMEKIND_HEADLESS) { if (game_mode == GAMEKIND_HEADLESS) {
network_server_start(0, 27000); network_server_start(0, 27000);
//ecs_set_target_fps(world_ecs(), 60); //ecs_set_target_fps(world_ecs(), 60);
} }
} }
if (game_mode == GAMEKIND_SINGLE) { if (game_mode == GAMEKIND_SINGLE) {
for (uint32_t i = 0; i < num_viewers; i++) { for (uint32_t i = 0; i < num_viewers; i++) {
pkt_00_init_send(i); pkt_00_init_send(i);
@ -189,35 +180,27 @@ int8_t game_is_networked() {
} }
void game_shutdown() { void game_shutdown() {
if (game_mode == GAMEKIND_CLIENT) { if (game_mode == GAMEKIND_CLIENT) {
network_client_disconnect(); network_client_disconnect();
} else { } else {
world_destroy(); world_destroy();
if (game_mode == GAMEKIND_HEADLESS) { if (game_mode == GAMEKIND_HEADLESS) {
network_server_stop(); network_server_stop();
} }
} }
if (game_mode != GAMEKIND_SINGLE) { if (game_mode != GAMEKIND_SINGLE) {
network_destroy(); network_destroy();
} }
if (game_mode != GAMEKIND_HEADLESS) { if (game_mode != GAMEKIND_HEADLESS) {
world_viewers_destroy(); world_viewers_destroy();
// TODO(zaklaus): crashes on exit // TODO(zaklaus): crashes on exit
//platform_shutdown(); //platform_shutdown();
} }
// NOTE: shutdown subsystems
{
item_cleanup();
entity_spawndef_cleanup();
blocks_cleanup();
assets_cleanup();
}
} }
uint8_t game_is_running() { uint8_t game_is_running() {
@ -245,10 +228,10 @@ void game_update() {
} }
else { else {
world_update(); world_update();
if (game_mode == GAMEKIND_HEADLESS) { if (game_mode == GAMEKIND_HEADLESS) {
network_server_tick(); network_server_tick();
static float ms_report = 2.5f; static float ms_report = 2.5f;
if (ms_report < get_cached_time()) { if (ms_report < get_cached_time()) {
ms_report = get_cached_time() + 5.f; ms_report = get_cached_time() + 5.f;
@ -256,7 +239,7 @@ void game_update() {
} }
} }
} }
last_update = get_cached_time(); last_update = get_cached_time();
} }

View File

@ -11,17 +11,6 @@
// NOTE(zaklaus): bring in entity spawnlist // NOTE(zaklaus): bring in entity spawnlist
#include "entity_spawnlist.c" #include "entity_spawnlist.c"
void entity_spawndef_cleanup() {
zpl_array_free(entity_spawnlist); entity_spawnlist = NULL;
}
void entity_spawndef_register(spawndef def) {
if (!entity_spawnlist) {
zpl_array_init(entity_spawnlist, zpl_heap());
}
zpl_array_append(entity_spawnlist, def);
}
uint64_t entity_spawn(uint16_t class_id) { uint64_t entity_spawn(uint16_t class_id) {
ecs_entity_t e = ecs_new(world_ecs(), 0); ecs_entity_t e = ecs_new(world_ecs(), 0);
@ -49,7 +38,7 @@ uint64_t entity_spawn(uint16_t class_id) {
} }
uint64_t entity_spawn_id(uint16_t id){ uint64_t entity_spawn_id(uint16_t id){
for (zpl_isize i = 0; i < zpl_array_count(entity_spawnlist); ++i){ for (size_t i = 0; i < MAX_ENTITY_SPAWNDEFS; ++i){
if (entity_spawnlist[i].id == id){ if (entity_spawnlist[i].id == id){
ZPL_ASSERT(entity_spawnlist[i].proc); ZPL_ASSERT(entity_spawnlist[i].proc);
return entity_spawnlist[i].proc(); return entity_spawnlist[i].proc();

View File

@ -1,18 +1,8 @@
#pragma once #pragma once
#include "platform/system.h" #include "platform/system.h"
#include "gen/assets.h"
#define ENTITY_ACTION_VELOCITY_THRESHOLD 0.05f #define ENTITY_ACTION_VELOCITY_THRESHOLD 0.05f
typedef struct {
asset_id id;
uint64_t (*proc)();
} spawndef;
void entity_spawndef_setup();
void entity_spawndef_cleanup();
void entity_spawndef_register(spawndef def);
uint64_t entity_spawn(uint16_t class_id /* 0 = no streaming */); uint64_t entity_spawn(uint16_t class_id /* 0 = no streaming */);
uint64_t entity_spawn_id(uint16_t id); uint64_t entity_spawn_id(uint16_t id);
void entity_batch_despawn(uint64_t *ids, size_t num_ids); void entity_batch_despawn(uint64_t *ids, size_t num_ids);

View File

@ -1,8 +1,11 @@
// NOTE(zaklaus): access to spawners // NOTE(zaklaus): access to spawners
#include "ents/storage.h" #include "ents/storage.h"
static spawndef *entity_spawnlist = 0; static struct {
asset_id id;
uint64_t (*proc)();
} entity_spawnlist[] = {
{ .id = ASSET_CHEST, .proc = storage_spawn }
};
void entity_spawndef_setup(void) { #define MAX_ENTITY_SPAWNDEFS ((sizeof(entity_spawnlist))/(sizeof(entity_spawnlist[0])))
entity_spawndef_register((spawndef){ .id = ASSET_CHEST, .proc = storage_spawn });
}

View File

@ -9,21 +9,10 @@
#include "zpl.h" #include "zpl.h"
#include "items_list.c" #include "items_list.c"
#define ITEMS_COUNT (sizeof(items)/sizeof(item_desc))
void item_cleanup() {
zpl_array_free(items); items = NULL;
}
void item_register(item_desc desc) {
if (!items) {
zpl_array_init(items, zpl_heap());
}
zpl_array_append(items, desc);
}
static inline item_id item_resolve_proxy(item_id id) { static inline item_id item_resolve_proxy(item_id id) {
ZPL_ASSERT(id >= 0 && id < zpl_array_count(items)); ZPL_ASSERT(id >= 0 && id < ITEMS_COUNT);
item_usage usage = items[id].usage; item_usage usage = items[id].usage;
if (usage == UKIND_PROXY) { if (usage == UKIND_PROXY) {
return item_find(items[id].proxy.id); return item_find(items[id].proxy.id);
@ -49,7 +38,7 @@ uint64_t item_spawn(asset_id kind, uint32_t qty) {
} }
item_id item_find(asset_id kind) { item_id item_find(asset_id kind) {
for (item_id i=0; i<zpl_array_count(items); i++) { for (item_id i=0; i<ITEMS_COUNT; i++) {
if (items[i].kind == kind) if (items[i].kind == kind)
return item_resolve_proxy(i); return item_resolve_proxy(i);
} }
@ -115,16 +104,16 @@ void item_despawn(uint64_t id) {
} }
uint32_t item_max_quantity(item_id id) { uint32_t item_max_quantity(item_id id) {
ZPL_ASSERT(id >= 0 && id < zpl_array_count(items)); ZPL_ASSERT(id >= 0 && id < ITEMS_COUNT);
return items[id].max_quantity; return items[id].max_quantity;
} }
item_usage item_get_usage(item_id id) { item_usage item_get_usage(item_id id) {
ZPL_ASSERT(id >= 0 && id < zpl_array_count(items)); ZPL_ASSERT(id >= 0 && id < ITEMS_COUNT);
return items[id].usage; return items[id].usage;
} }
bool item_get_place_directional(item_id id) { bool item_get_place_directional(item_id id) {
ZPL_ASSERT(id >= 0 && id < zpl_array_count(items)); ZPL_ASSERT(id >= 0 && id < ITEMS_COUNT);
return items[id].place.directional; return items[id].place.directional;
} }

View File

@ -41,10 +41,6 @@ typedef struct {
typedef uint16_t item_id; typedef uint16_t item_id;
void item_setup();
void item_cleanup();
void item_register(item_desc desc);
// NOTE(zaklaus): item drops // NOTE(zaklaus): item drops
uint64_t item_spawn(asset_id kind, uint32_t qty); uint64_t item_spawn(asset_id kind, uint32_t qty);
void item_despawn(uint64_t id); void item_despawn(uint64_t id);

View File

@ -2,20 +2,18 @@
#include "world/entity_view.h" #include "world/entity_view.h"
#include "items_list_helpers.h" #include "items_list_helpers.h"
static item_desc *items = 0; static item_desc items[] = {
{ .kind = 0, .max_quantity = 0, },
void item_setup() { ITEM_BLOCK(ASSET_DEMO_ICEMAKER, 64, ASSET_WATER),
item_register((item_desc){ .kind = 0, .max_quantity = 0, }); ITEM_SELF(ASSET_FENCE, 64),
item_register(ITEM_BLOCK(ASSET_DEMO_ICEMAKER, 64, ASSET_WATER)); ITEM_SELF(ASSET_WOOD, 64),
item_register(ITEM_SELF(ASSET_FENCE, 64)); ITEM_HOLD(ASSET_TREE, 64),
item_register(ITEM_SELF(ASSET_WOOD, 64));
item_register(ITEM_HOLD(ASSET_TREE, 64)); ITEM_SELF_DIR(ASSET_BELT, 999),
ITEM_PROXY(ASSET_BELT_LEFT, ASSET_BELT),
item_register(ITEM_SELF_DIR(ASSET_BELT, 999)); ITEM_PROXY(ASSET_BELT_RIGHT, ASSET_BELT),
item_register(ITEM_PROXY(ASSET_BELT_LEFT, ASSET_BELT)); ITEM_PROXY(ASSET_BELT_UP, ASSET_BELT),
item_register(ITEM_PROXY(ASSET_BELT_RIGHT, ASSET_BELT)); ITEM_PROXY(ASSET_BELT_DOWN, ASSET_BELT),
item_register(ITEM_PROXY(ASSET_BELT_UP, ASSET_BELT));
item_register(ITEM_PROXY(ASSET_BELT_DOWN, ASSET_BELT)); ITEM_ENT(ASSET_CHEST, 32, ASSET_CHEST),
};
item_register(ITEM_ENT(ASSET_CHEST, 32, ASSET_CHEST));
}

View File

@ -1,14 +1,14 @@
#pragma once #pragma once
#define ITEM_HOLD(asset, qty)\ #define ITEM_HOLD(asset, qty)\
(item_desc){\ {\
.kind = asset,\ .kind = asset,\
.usage = UKIND_HOLD,\ .usage = UKIND_HOLD,\
.max_quantity = qty,\ .max_quantity = qty,\
} }
#define ITEM_BLOCK(asset, qty, build_asset)\ #define ITEM_BLOCK(asset, qty, build_asset)\
(item_desc){\ {\
.kind = asset,\ .kind = asset,\
.usage = UKIND_PLACE,\ .usage = UKIND_PLACE,\
.max_quantity = qty,\ .max_quantity = qty,\
@ -18,7 +18,7 @@
} }
#define ITEM_BLOCK_DIR(asset, qty, build_asset)\ #define ITEM_BLOCK_DIR(asset, qty, build_asset)\
(item_desc){\ {\
.kind = asset,\ .kind = asset,\
.usage = UKIND_PLACE,\ .usage = UKIND_PLACE,\
.max_quantity = qty,\ .max_quantity = qty,\
@ -29,7 +29,7 @@
} }
#define ITEM_PROXY(asset, proxy_id)\ #define ITEM_PROXY(asset, proxy_id)\
(item_desc){\ {\
.kind = asset,\ .kind = asset,\
.usage = UKIND_PROXY,\ .usage = UKIND_PROXY,\
.proxy = {\ .proxy = {\
@ -38,7 +38,7 @@
} }
#define ITEM_ENT(asset, qty, eid)\ #define ITEM_ENT(asset, qty, eid)\
(item_desc){\ {\
.kind = asset,\ .kind = asset,\
.usage = UKIND_PLACE_ITEM,\ .usage = UKIND_PLACE_ITEM,\
.max_quantity = qty,\ .max_quantity = qty,\

View File

@ -2,15 +2,17 @@
#include "raylib.h" #include "raylib.h"
#include "gen/texgen.h" #include "gen/texgen.h"
#define ASSETS_COUNT (sizeof(assets)/sizeof(asset))
typedef struct { typedef struct {
asset_id id; asset_id id;
asset_kind kind; asset_kind kind;
union { union {
Texture2D tex; Texture2D tex;
Sound snd; Sound snd;
}; };
// NOTE(zaklaus): metadata // NOTE(zaklaus): metadata
} asset; } asset;
@ -18,92 +20,80 @@ typedef struct {
#define ASSET_FRAME_RENDER_MS (1.0/5.0) #define ASSET_FRAME_RENDER_MS (1.0/5.0)
#define ASSET_FRAME_SKIP 4 #define ASSET_FRAME_SKIP 4
static int64_t assets_resources_frame_counter = 1; static int64_t assets_frame_counter = 1;
static double assets_resources_frame_next_draw = 0.0; static double assets_frame_next_draw = 0.0;
#include <time.h> #include <time.h>
void assets_register(asset_desc a) { int32_t assets_setup(void) {
if (!assets) { for (uint32_t i=0; i<ASSETS_COUNT; i++) {
zpl_array_init(assets, zpl_heap());
}
zpl_array_append(assets, ((asset){ .id = a.id, .kind = a.kind }));
}
void assets_cleanup(void) {
zpl_array_free(assets); assets = NULL;
}
int32_t assets_resources_setup(void) {
for (zpl_isize i=0; i<zpl_array_count(assets); i++) {
asset *b = &assets[i]; asset *b = &assets[i];
switch (b->kind) { switch (b->kind) {
case AKIND_TEXTURE: { case AKIND_TEXTURE: {
b->tex = texgen_build_sprite(b->id); b->tex = texgen_build_sprite(b->id);
}break; }break;
case AKIND_ANIM: { case AKIND_ANIM: {
b->tex = texgen_build_anim(b->id, 0); b->tex = texgen_build_anim(b->id, 0);
}break; }break;
case AKIND_SOUND: { case AKIND_SOUND: {
// TODO(zaklaus): soundgen // TODO(zaklaus): soundgen
}break; }break;
default: break; default: break;
} }
} }
assets_resources_frame_next_draw = get_cached_time() + ASSET_FRAME_RENDER_MS; assets_frame_next_draw = get_cached_time() + ASSET_FRAME_RENDER_MS;
return 0; return 0;
} }
int32_t assets_resources_frame(void) { int32_t assets_frame(void) {
if (assets_resources_frame_next_draw < get_cached_time()) { if (assets_frame_next_draw < get_cached_time()) {
for (zpl_isize i=0; i<zpl_array_count(assets); i++) { for (uint32_t i=0; i<ASSETS_COUNT; i++) {
asset *b = &assets[i]; asset *b = &assets[i];
switch (b->kind) { switch (b->kind) {
case AKIND_ANIM: { case AKIND_ANIM: {
UnloadTexture(b->tex); UnloadTexture(b->tex);
b->tex = texgen_build_anim(b->id, assets_resources_frame_counter); b->tex = texgen_build_anim(b->id, assets_frame_counter);
}break; }break;
default: break; default: break;
} }
} }
assets_resources_frame_next_draw = get_cached_time() + ASSET_FRAME_RENDER_MS; assets_frame_next_draw = get_cached_time() + ASSET_FRAME_RENDER_MS;
assets_resources_frame_counter += ASSET_FRAME_SKIP; assets_frame_counter += ASSET_FRAME_SKIP;
} }
return 0; return 0;
} }
void assets_resources_destroy(void) { void assets_destroy(void) {
for (zpl_isize i=0; i<zpl_array_count(assets); i++) { for (uint32_t i=0; i<ASSETS_COUNT; i++) {
switch (assets[i].kind) { switch (assets[i].kind) {
case AKIND_ANIM: case AKIND_ANIM:
case AKIND_TEXTURE: { case AKIND_TEXTURE: {
UnloadTexture(assets[i].tex); UnloadTexture(assets[i].tex);
}break; }break;
case AKIND_SOUND: { case AKIND_SOUND: {
// TODO(zaklaus): soundgen // TODO(zaklaus): soundgen
}break; }break;
default: break; default: break;
} }
} }
} }
uint16_t assets_find(asset_id id) { uint16_t assets_find(asset_id id) {
for (zpl_isize i=0; i<zpl_array_count(assets); i++) { for (uint16_t i=0; i<ASSETS_COUNT; i++) {
if (assets[i].id == id) if (assets[i].id == id)
return i; return i;
} }
ZPL_PANIC("Unknown asset id: %d\n", id); ZPL_PANIC("Unknown asset id: %d\n", id);
return ASSET_INVALID; return ASSET_INVALID;
} }

View File

@ -3,30 +3,20 @@
#define ASSET_INVALID 0xFF #define ASSET_INVALID 0xFF
#define ASSET_ENTRY(asset, asset_kind)\
(asset_desc){\
.id = asset,\
.kind = asset_kind,\
}
#define ASSET_SND(asset) ASSET_ENTRY(asset, AKIND_SOUND)
#define ASSET_TEX(asset) ASSET_ENTRY(asset, AKIND_TEXTURE)
#define ASSET_ANI(asset) ASSET_ENTRY(asset, AKIND_ANIM)
typedef enum { typedef enum {
// NOTE(zaklaus): Debug // NOTE(zaklaus): Debug
ASSET_EMPTY, ASSET_EMPTY,
ASSET_BLANK, ASSET_BLANK,
ASSET_BUILDMODE_HIGHLIGHT, ASSET_BUILDMODE_HIGHLIGHT,
// NOTE(zaklaus): entities // NOTE(zaklaus): entities
ASSET_PLAYER, ASSET_PLAYER,
ASSET_THING, ASSET_THING,
ASSET_CHEST, ASSET_CHEST,
// NOTE(zaklaus): items // NOTE(zaklaus): items
ASSET_DEMO_ICEMAKER, ASSET_DEMO_ICEMAKER,
// NOTE(zaklaus): blocks // NOTE(zaklaus): blocks
ASSET_FENCE, ASSET_FENCE,
ASSET_DEV, ASSET_DEV,
@ -40,15 +30,13 @@ typedef enum {
ASSET_HOLE, ASSET_HOLE,
ASSET_WOOD, ASSET_WOOD,
ASSET_TREE, ASSET_TREE,
ASSET_BELT, ASSET_BELT,
ASSET_BELT_LEFT, ASSET_BELT_LEFT,
ASSET_BELT_RIGHT, ASSET_BELT_RIGHT,
ASSET_BELT_UP, ASSET_BELT_UP,
ASSET_BELT_DOWN, ASSET_BELT_DOWN,
ASSET_NEXT_FREE,
MAX_ASSETS = 1024, MAX_ASSETS = 1024,
} asset_id; } asset_id;
@ -56,23 +44,13 @@ typedef enum {
AKIND_TEXTURE, AKIND_TEXTURE,
AKIND_ANIM, AKIND_ANIM,
AKIND_SOUND, AKIND_SOUND,
FORCE_AKIND_UINT8 = UINT8_MAX FORCE_AKIND_UINT8 = UINT8_MAX
} asset_kind; } asset_kind;
typedef struct { int32_t assets_setup(void);
asset_id id; int32_t assets_frame(void);
asset_kind kind; void assets_destroy(void);
} asset_desc;
void assets_setup(void);
void assets_cleanup(void);
void assets_register(asset_desc desc);
// resources
int32_t assets_resources_setup(void);
void assets_resources_destroy(void);
int32_t assets_resources_frame(void);
uint16_t assets_find(asset_id id); uint16_t assets_find(asset_id id);

View File

@ -1,31 +1,39 @@
#include "gen/assets.h" #include "gen/assets.h"
static asset *assets = 0; #define ASSET_ENTRY(asset, asset_kind)\
{\
.id = asset,\
.kind = asset_kind,\
}
void assets_setup() { #define ASSET_SND(asset) ASSET_ENTRY(asset, AKIND_SOUND)
assets_register(ASSET_TEX(ASSET_EMPTY)); #define ASSET_TEX(asset) ASSET_ENTRY(asset, AKIND_TEXTURE)
assets_register(ASSET_TEX(ASSET_BLANK)); #define ASSET_ANI(asset) ASSET_ENTRY(asset, AKIND_ANIM)
assets_register(ASSET_TEX(ASSET_BUILDMODE_HIGHLIGHT));
assets_register(ASSET_TEX(ASSET_DEMO_ICEMAKER));
assets_register(ASSET_TEX(ASSET_CHEST));
// NOTE: blocks static asset assets[] = {
assets_register(ASSET_TEX(ASSET_FENCE)); ASSET_TEX(ASSET_EMPTY),
assets_register(ASSET_TEX(ASSET_DEV)); ASSET_TEX(ASSET_BLANK),
assets_register(ASSET_TEX(ASSET_GROUND)); ASSET_TEX(ASSET_BUILDMODE_HIGHLIGHT),
assets_register(ASSET_TEX(ASSET_DIRT)); ASSET_TEX(ASSET_DEMO_ICEMAKER),
assets_register(ASSET_ANI(ASSET_WATER)); ASSET_TEX(ASSET_CHEST),
assets_register(ASSET_TEX(ASSET_LAVA));
assets_register(ASSET_TEX(ASSET_WALL)); // NOTE(zaklaus): blocks
assets_register(ASSET_TEX(ASSET_HILL)); ASSET_TEX(ASSET_FENCE),
assets_register(ASSET_TEX(ASSET_HILL_SNOW)); ASSET_TEX(ASSET_DEV),
assets_register(ASSET_TEX(ASSET_HOLE)); ASSET_TEX(ASSET_GROUND),
assets_register(ASSET_TEX(ASSET_WOOD)); ASSET_TEX(ASSET_DIRT),
assets_register(ASSET_TEX(ASSET_TREE)); ASSET_ANI(ASSET_WATER),
ASSET_TEX(ASSET_LAVA),
assets_register(ASSET_TEX(ASSET_BELT)); ASSET_TEX(ASSET_WALL),
assets_register(ASSET_TEX(ASSET_BELT_LEFT)); ASSET_TEX(ASSET_HILL),
assets_register(ASSET_TEX(ASSET_BELT_RIGHT)); ASSET_TEX(ASSET_HILL_SNOW),
assets_register(ASSET_TEX(ASSET_BELT_UP)); ASSET_TEX(ASSET_HOLE),
assets_register(ASSET_TEX(ASSET_BELT_DOWN)); ASSET_TEX(ASSET_WOOD),
ASSET_TEX(ASSET_TREE),
ASSET_TEX(ASSET_BELT),
ASSET_TEX(ASSET_BELT_LEFT),
ASSET_TEX(ASSET_BELT_RIGHT),
ASSET_TEX(ASSET_BELT_UP),
ASSET_TEX(ASSET_BELT_DOWN),
}; };

View File

@ -127,13 +127,13 @@ void renderer_init_3d(void) {
DrawText(loading_text, GetScreenWidth()-text_w-15, GetScreenHeight()-135, 120, RAYWHITE); DrawText(loading_text, GetScreenWidth()-text_w-15, GetScreenHeight()-135, 120, RAYWHITE);
EndDrawing(); EndDrawing();
assets_resources_setup(); blocks_setup();
blocks_resources_setup(); assets_setup();
} }
void renderer_shutdown_3d(void) { void renderer_shutdown_3d(void) {
blocks_resources_destroy(); blocks_destroy();
assets_resources_destroy(); assets_destroy();
} }

View File

@ -179,13 +179,13 @@ void renderer_init_v0(void) {
DrawText(loading_text, GetScreenWidth()-text_w-15, GetScreenHeight()-135, 120, RAYWHITE); DrawText(loading_text, GetScreenWidth()-text_w-15, GetScreenHeight()-135, 120, RAYWHITE);
EndDrawing(); EndDrawing();
assets_resources_setup(); blocks_setup();
blocks_resources_setup(); assets_setup();
} }
void renderer_shutdown_v0(void) { void renderer_shutdown_v0(void) {
blocks_resources_destroy(); blocks_destroy();
assets_resources_destroy(); assets_destroy();
} }
void renderer_debug_draw_v0(void) { void renderer_debug_draw_v0(void) {

View File

@ -7,6 +7,7 @@
#include "world/world_view.h" #include "world/world_view.h"
#include "perlin.h" #include "perlin.h"
#define BLOCKS_COUNT (sizeof(blocks)/sizeof(block))
#define WORLD_TEXTURE_BLOCK_SCALE 0.5f #define WORLD_TEXTURE_BLOCK_SCALE 0.5f
ZPL_TABLE(static, blocks__chunk_tbl, blocks__chunk_tbl_, RenderTexture2D); ZPL_TABLE(static, blocks__chunk_tbl, blocks__chunk_tbl_, RenderTexture2D);
@ -18,35 +19,38 @@ static void chunks_unload_textures(uint64_t key, RenderTexture2D *value) {
UnloadRenderTexture(*value); UnloadRenderTexture(*value);
} }
typedef struct {
asset_id kind;
uint32_t flags;
char symbol;
float drag;
float friction;
float bounce;
float velx;
float vely;
// NOTE(zaklaus): viewer data
block_id slot;
} block;
#include "blocks_list.c" #include "blocks_list.c"
void blocks_register(block desc) { int32_t blocks_setup(void) {
if (!blocks) { for (block_id i=0; i<BLOCKS_COUNT; i++) {
zpl_array_init(blocks, zpl_heap());
}
zpl_array_append(blocks, desc);
}
void blocks_cleanup(void) {
zpl_array_free(blocks); blocks = NULL;
}
int32_t blocks_resources_setup(void) {
for (block_id i=0; i<zpl_array_count(blocks); i++) {
blocks[i].slot = assets_find(blocks[i].kind); blocks[i].slot = assets_find(blocks[i].kind);
} }
blocks__chunk_tbl_init(&baked_chunks, zpl_heap()); blocks__chunk_tbl_init(&baked_chunks, zpl_heap());
return 0; return 0;
} }
void blocks_resources_destroy(void) { void blocks_destroy(void) {
blocks__chunk_tbl_map_mut(&baked_chunks, chunks_unload_textures); blocks__chunk_tbl_map_mut(&baked_chunks, chunks_unload_textures);
blocks__chunk_tbl_destroy(&baked_chunks); blocks__chunk_tbl_destroy(&baked_chunks);
} }
block_id blocks_find(asset_id kind) { block_id blocks_find(asset_id kind) {
for (block_id i=0; i<zpl_array_count(blocks); i++) { for (block_id i=0; i<BLOCKS_COUNT; i++) {
if (blocks[i].kind == kind) if (blocks[i].kind == kind)
return i; return i;
} }

View File

@ -2,11 +2,6 @@
#include "platform/system.h" #include "platform/system.h"
#include "gen/assets.h" #include "gen/assets.h"
#define BLOCK(a, f, s, ...)\
(block){\
.kind = a, .flags = f, .symbol = s, __VA_ARGS__\
}
typedef enum { typedef enum {
BLOCK_FLAG_COLLISION = (1 << 1), BLOCK_FLAG_COLLISION = (1 << 1),
BLOCK_FLAG_HAZARD = (1 << 2), BLOCK_FLAG_HAZARD = (1 << 2),
@ -16,28 +11,8 @@ typedef enum {
typedef uint16_t block_id; typedef uint16_t block_id;
typedef struct { int32_t blocks_setup(void);
asset_id kind; void blocks_destroy(void);
uint32_t flags;
char symbol;
float drag;
float friction;
float bounce;
float velx;
float vely;
// NOTE(zaklaus): viewer data
block_id slot;
} block;
void blocks_setup(void);
void blocks_register(block desc);
void blocks_cleanup(void);
// resources
int32_t blocks_resources_setup(void);
void blocks_resources_destroy(void);
block_id blocks_find(asset_id kind); block_id blocks_find(asset_id kind);

View File

@ -1,22 +1,27 @@
#include "world/blocks.h" #include "world/blocks.h"
static block *blocks = 0; #define BLOCK(a, f, s, ...)\
{\
.kind = a, .flags = f, .symbol = s, __VA_ARGS__\
}
void blocks_setup() { static block blocks[] = {
blocks_register(BLOCK(ASSET_EMPTY, 0, 'E')); BLOCK(ASSET_EMPTY, 0, 'E'),
blocks_register(BLOCK(ASSET_GROUND, 0, '.', .drag = 1.0f, .friction = 1.0f)); BLOCK(ASSET_GROUND, 0, '.', .drag = 1.0f, .friction = 1.0f),
blocks_register(BLOCK(ASSET_DIRT, 0, ',', .drag = 2.1f , .friction = 1.0f)); BLOCK(ASSET_DIRT, 0, ',', .drag = 2.1f , .friction = 1.0f),
blocks_register(BLOCK(ASSET_WALL, BLOCK_FLAG_COLLISION, '#', .drag = 1.0f , .friction = 1.0f, .bounce = 1.0f)); BLOCK(ASSET_WALL, BLOCK_FLAG_COLLISION, '#', .drag = 1.0f , .friction = 1.0f, .bounce = 1.0f),
blocks_register(BLOCK(ASSET_HILL, BLOCK_FLAG_COLLISION, '^', .drag = 1.0f , .friction = 1.0f)); BLOCK(ASSET_HILL, BLOCK_FLAG_COLLISION, '^', .drag = 1.0f , .friction = 1.0f),
blocks_register(BLOCK(ASSET_HILL_SNOW, BLOCK_FLAG_COLLISION, '*', .drag = 1.0f , .friction = 1.0f)); BLOCK(ASSET_HILL_SNOW, BLOCK_FLAG_COLLISION, '*', .drag = 1.0f , .friction = 1.0f),
blocks_register(BLOCK(ASSET_WATER, 0, '~', .drag = 0.11f , .friction = 1.0f)); BLOCK(ASSET_WATER, 0, '~', .drag = 0.11f , .friction = 1.0f),
blocks_register(BLOCK(ASSET_LAVA, BLOCK_FLAG_HAZARD, '!', .drag = 6.2f , .friction = 4.0f)); BLOCK(ASSET_LAVA, BLOCK_FLAG_HAZARD, '!', .drag = 6.2f , .friction = 4.0f),
blocks_register(BLOCK(ASSET_FENCE, BLOCK_FLAG_COLLISION, '#', .drag = 1.0f , .friction = 1.0f, .bounce = 1.0f)); BLOCK(ASSET_FENCE, BLOCK_FLAG_COLLISION, '#', .drag = 1.0f , .friction = 1.0f, .bounce = 1.0f),
blocks_register(BLOCK(ASSET_WOOD, BLOCK_FLAG_COLLISION, '#', .drag = 1.0f , .friction = 1.0f, .bounce = 0.0f)); BLOCK(ASSET_WOOD, BLOCK_FLAG_COLLISION, '#', .drag = 1.0f , .friction = 1.0f, .bounce = 0.0f),
blocks_register(BLOCK(ASSET_TREE, BLOCK_FLAG_COLLISION|BLOCK_FLAG_DESTROY_ON_COLLISION, '@', .drag = 1.0f , .friction = 1.0f, .bounce = 0.0f)); BLOCK(ASSET_TREE, BLOCK_FLAG_COLLISION|BLOCK_FLAG_DESTROY_ON_COLLISION, '@', .drag = 1.0f , .friction = 1.0f, .bounce = 0.0f),
blocks_register(BLOCK(ASSET_BELT_LEFT, 0, '@', .drag = 1.0f , .friction = 1.0f, .velx = -150.0f)); BLOCK(ASSET_BELT_LEFT, 0, '@', .drag = 1.0f , .friction = 1.0f, .velx = -150.0f),
blocks_register(BLOCK(ASSET_BELT_RIGHT, 0, '@', .drag = 1.0f , .friction = 1.0f, .velx = 150.0f)); BLOCK(ASSET_BELT_RIGHT, 0, '@', .drag = 1.0f , .friction = 1.0f, .velx = 150.0f),
blocks_register(BLOCK(ASSET_BELT_UP, 0, '@', .drag = 1.0f , .friction = 1.0f, .vely = -150.0f)); BLOCK(ASSET_BELT_UP, 0, '@', .drag = 1.0f , .friction = 1.0f, .vely = -150.0f),
blocks_register(BLOCK(ASSET_BELT_DOWN, 0, '@', .drag = 1.0f , .friction = 1.0f, .vely = 150.0f)); BLOCK(ASSET_BELT_DOWN, 0, '@', .drag = 1.0f , .friction = 1.0f, .vely = 150.0f),
}; };
ZPL_STATIC_ASSERT(sizeof(blocks)/sizeof(block) < ZPL_U16_MAX, "too many registered blocks! (max. 65536)");

View File

@ -16,7 +16,7 @@ typedef enum {
EKIND_MONSTER, EKIND_MONSTER,
EKIND_MACRO_BOT, EKIND_MACRO_BOT,
EKIND_CHUNK, EKIND_CHUNK,
EKIND_NEXT_FREE FORCE_EKIND_UINT16 = UINT16_MAX
} entity_kind; } entity_kind;
typedef enum { typedef enum {

View File

@ -24,9 +24,6 @@ EM_JS(int, canvas_get_height, (), {
}); });
#endif #endif
//temp
bool inv_is_open = false;
static uint16_t screenWidth = 1024; static uint16_t screenWidth = 1024;
static uint16_t screenHeight = 768; static uint16_t screenHeight = 768;
static float target_zoom = 0.6f; static float target_zoom = 0.6f;
@ -35,6 +32,10 @@ static bool request_shutdown;
#define GFX_KIND 2 #define GFX_KIND 2
#include "renderers/renderer_bridge.c" #include "renderers/renderer_bridge.c"
// NOTE(zaklaus): add-ins
#include "gui/build_mode.c"
#include "gui/inventory.c"
void platform_init() { void platform_init() {
SetTraceLogLevel(LOG_ERROR); SetTraceLogLevel(LOG_ERROR);
@ -43,16 +44,32 @@ void platform_init() {
screenHeight = (uint16_t)canvas_get_height(); screenHeight = (uint16_t)canvas_get_height();
#endif #endif
InitWindow(screenWidth, screenHeight, "minimal"); InitWindow(screenWidth, screenHeight, "eco2d");
SetWindowState(/*FLAG_WINDOW_UNDECORATED|*/FLAG_WINDOW_MAXIMIZED|FLAG_WINDOW_RESIZABLE|FLAG_MSAA_4X_HINT); SetWindowState(/*FLAG_WINDOW_UNDECORATED|*/FLAG_WINDOW_MAXIMIZED|FLAG_WINDOW_RESIZABLE|FLAG_MSAA_4X_HINT);
#if !defined(PLATFORM_WEB) #if !defined(PLATFORM_WEB)
screenWidth = (uint16_t)GetScreenWidth(); screenWidth = (uint16_t)GetScreenWidth();
screenHeight = (uint16_t)GetScreenHeight(); screenHeight = (uint16_t)GetScreenHeight();
#endif #endif
// ToggleFullscreen();
// SetTargetFPS(60.0);
renderer_init(); renderer_init();
} }
inline static
void display_conn_status() {
if (game_is_networked()) {
if (network_client_is_connected()) {
DrawText("Connection: online", 5, 5, 12, GREEN);
} else {
DrawText("Connection: offline", 5, 5, 12, RED);
}
} else {
DrawText("Connection: single-player", 5, 5, 12, BLUE);
}
}
void platform_shutdown() { void platform_shutdown() {
renderer_shutdown(); renderer_shutdown();
CloseWindow(); CloseWindow();
@ -62,16 +79,54 @@ uint8_t platform_is_running() {
return !WindowShouldClose(); return !WindowShouldClose();
} }
void platform_get_block_realpos(float *x, float *y){
camera cam = camera_get();
Vector2 mpos = GetMousePosition();
entity_view *e = game_world_view_active_get_entity(cam.ent_id);
if (!e) return;
float zoom = renderer_zoom_get();
mpos.x -= screenWidth/2.0f;
mpos.y -= screenHeight/2.0f;
cam.x += mpos.x*(1.0f/zoom);
cam.y += mpos.y*(1.0f/zoom);
cam.x = ((int32_t)cam.x / (int32_t)(WORLD_BLOCK_SIZE)) * WORLD_BLOCK_SIZE;
cam.y = ((int32_t)cam.y / (int32_t)(WORLD_BLOCK_SIZE)) * WORLD_BLOCK_SIZE;
cam.x += WORLD_BLOCK_SIZE/2.0f;
cam.y += WORLD_BLOCK_SIZE/2.0f;
if (x) *x = (float)cam.x;
if (y) *y = (float)cam.y;
}
static game_keystate_data last_input_data = {0}; static game_keystate_data last_input_data = {0};
static pkt_send_blockpos last_blockpos_data = {0};
inline static inline static
void platform_input_update_input_frame(game_keystate_data data) { void platform_input_update_input_frame(game_keystate_data data) {
float mx = 0, my = 0;
platform_get_block_realpos(&mx, &my);
if (mx != last_blockpos_data.mx || my != last_blockpos_data.my){
last_blockpos_data.mx = mx;
last_blockpos_data.my = my;
game_action_send_blockpos(mx, my);
}
// NOTE(zaklaus): Test if there are any changes // NOTE(zaklaus): Test if there are any changes
if (data.x != last_input_data.x) goto send_data; if (data.x != last_input_data.x) goto send_data;
if (data.y != last_input_data.y) goto send_data; if (data.y != last_input_data.y) goto send_data;
if (data.use != last_input_data.use) goto send_data; if (data.use != last_input_data.use) goto send_data;
if (data.sprint != last_input_data.sprint) goto send_data; if (data.sprint != last_input_data.sprint) goto send_data;
if (data.ctrl != last_input_data.ctrl) goto send_data; if (data.ctrl != last_input_data.ctrl) goto send_data;
if (data.pick != last_input_data.pick) goto send_data;
if (data.storage_action != last_input_data.storage_action) goto send_data;
if (data.selected_item != last_input_data.selected_item) goto send_data;
if (data.drop != last_input_data.drop) goto send_data;
if (data.swap != last_input_data.swap) goto send_data;
if (data.swap_from != last_input_data.swap_from) goto send_data;
if (data.swap_to != last_input_data.swap_to) goto send_data;
if (data.placement_num != last_input_data.placement_num) goto send_data;
if (data.deletion_mode != last_input_data.deletion_mode) goto send_data;
if (zpl_memcompare(data.placements, last_input_data.placements, zpl_size_of(data.placements))) goto send_data;
return; return;
send_data: send_data:
@ -89,7 +144,7 @@ void platform_input() {
// NOTE(zaklaus): keystate handling // NOTE(zaklaus): keystate handling
{ {
float x=0.0f, y=0.0f; float x=0.0f, y=0.0f;
uint8_t use, sprint, drop, ctrl; uint8_t use, sprint, drop, ctrl, pick;
if (IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D)) x += 1.0f; if (IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D)) x += 1.0f;
if (IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A)) x -= 1.0f; if (IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A)) x -= 1.0f;
if (IsKeyDown(KEY_UP) || IsKeyDown(KEY_W)) y += 1.0f; if (IsKeyDown(KEY_UP) || IsKeyDown(KEY_W)) y += 1.0f;
@ -98,6 +153,7 @@ void platform_input() {
use = IsKeyPressed(KEY_SPACE); use = IsKeyPressed(KEY_SPACE);
sprint = IsKeyDown(KEY_LEFT_SHIFT) || IsKeyDown(KEY_RIGHT_SHIFT); sprint = IsKeyDown(KEY_LEFT_SHIFT) || IsKeyDown(KEY_RIGHT_SHIFT);
ctrl = IsKeyDown(KEY_LEFT_CONTROL) || IsKeyDown(KEY_RIGHT_CONTROL); ctrl = IsKeyDown(KEY_LEFT_CONTROL) || IsKeyDown(KEY_RIGHT_CONTROL);
drop = IsKeyPressed(KEY_G) || player_inv.drop_item || storage_inv.drop_item;
// NOTE(zaklaus): NEW! mouse movement // NOTE(zaklaus): NEW! mouse movement
Vector2 mouse_pos = GetMousePosition(); Vector2 mouse_pos = GetMousePosition();
@ -112,6 +168,12 @@ void platform_input() {
y = -mouse_pos.y; y = -mouse_pos.y;
} }
inv_keystate *inv = (inv_is_storage_action) ? &storage_inv : &player_inv;
inv_keystate *inv2 = (!inv_is_storage_action) ? &storage_inv : &player_inv;
// NOTE(zaklaus): don't perform picking if we manipulate our inventories
pick = (inv_is_inside||inv->item_is_held||inv2->item_is_held) ? false : IsMouseButtonDown(MOUSE_LEFT_BUTTON);
game_keystate_data in_data = { game_keystate_data in_data = {
.x = x, .x = x,
.y = y, .y = y,
@ -120,10 +182,73 @@ void platform_input() {
.use = use, .use = use,
.sprint = sprint, .sprint = sprint,
.ctrl = ctrl, .ctrl = ctrl,
.pick = pick,
.drop = drop,
.storage_action = inv_is_storage_action,
.selected_item = player_inv.selected_item,
.storage_selected_item = storage_inv.selected_item,
.swap = inv->swap,
.swap_storage = inv_swap_storage,
.swap_from = inv->swap_from,
.swap_to = inv->swap_to,
.deletion_mode = build_is_deletion_mode,
}; };
if (build_submit_placements) {
in_data.placement_num = build_num_placements;
zpl_memcopy(in_data.placements, build_placements, build_num_placements*zpl_size_of(item_placement));
}
platform_input_update_input_frame(in_data); platform_input_update_input_frame(in_data);
} }
// NOTE(zaklaus): cycle through viewers
{
if (IsKeyPressed(KEY_Q)) {
game_world_view_cycle_active(-1);
}
else if (IsKeyPressed(KEY_E)) {
game_world_view_cycle_active(1);
}
}
// NOTE(zaklaus): switch render modes
{
if (IsKeyPressed(KEY_O)) {
renderer_switch(1-gfx_kind);
}
}
// NOTE(zaklaus): toggle debug drawing
#ifndef ECO2D_PROD
{
if (IsKeyPressed(KEY_T)) {
debug_draw_enable(!debug_draw_state());
}
}
#endif
}
void draw_selected_item() {
camera cam = camera_get();
entity_view *oe = game_world_view_active_get_entity(cam.ent_id);
if (oe) {
// NOTE(zaklaus): sel item
entity_view *e = game_world_view_active_get_entity(oe->sel_ent);
if (e && e->kind == EKIND_DEVICE) {
renderer_draw_single(e->x, e->y, ASSET_BLANK, ColorAlpha(RED, 0.4f));
}else{
// NOTE(zaklaus): hover item
entity_view *e = game_world_view_active_get_entity(oe->pick_ent);
if (e && e->kind == EKIND_DEVICE) {
renderer_draw_single(e->x, e->y, ASSET_BLANK, ColorAlpha(RED, 0.1f));
}
}
}
} }
void platform_render() { void platform_render() {
@ -145,13 +270,22 @@ void platform_render() {
game_world_view_active_entity_map(do_entity_fadeinout); game_world_view_active_entity_map(do_entity_fadeinout);
} }
assets_resources_frame(); assets_frame();
BeginDrawing(); BeginDrawing();
{ {
profile (PROF_RENDER) { profile (PROF_RENDER) {
renderer_draw(); renderer_draw();
draw_selected_item();
} }
renderer_debug_draw();
{
// NOTE(zaklaus): add-ins
buildmode_draw();
inventory_draw();
}
display_conn_status();
debug_draw();
} }
EndDrawing(); EndDrawing();

View File

@ -1,307 +0,0 @@
#include "platform/platform.h"
#include "raylib.h"
#include "raymath.h"
#include "net/network.h"
#include "core/game.h"
#include "world/entity_view.h"
#include "world/prediction.h"
#include "core/camera.h"
#include "math.h"
#include "world/blocks.h"
#include "gen/assets.h"
#include "platform/profiler.h"
#include "debug/debug_ui.h"
#include "utils/raylib_helpers.h"
#if defined(PLATFORM_WEB)
#include <emscripten.h>
EM_JS(int, canvas_get_width, (), {
return canvas.width;
});
EM_JS(int, canvas_get_height, (), {
return canvas.height;
});
#endif
static uint16_t screenWidth = 1024;
static uint16_t screenHeight = 768;
static float target_zoom = 0.6f;
static bool request_shutdown;
#define GFX_KIND 2
#include "renderers/renderer_bridge.c"
// NOTE(zaklaus): add-ins
#include "gui/build_mode.c"
#include "gui/inventory.c"
void platform_init() {
SetTraceLogLevel(LOG_ERROR);
#if defined(PLATFORM_WEB)
screenWidth = (uint16_t)canvas_get_width();
screenHeight = (uint16_t)canvas_get_height();
#endif
InitWindow(screenWidth, screenHeight, "eco2d");
SetWindowState(/*FLAG_WINDOW_UNDECORATED|*/FLAG_WINDOW_MAXIMIZED|FLAG_WINDOW_RESIZABLE|FLAG_MSAA_4X_HINT);
#if !defined(PLATFORM_WEB)
screenWidth = (uint16_t)GetScreenWidth();
screenHeight = (uint16_t)GetScreenHeight();
#endif
// ToggleFullscreen();
// SetTargetFPS(60.0);
renderer_init();
}
inline static
void display_conn_status() {
if (game_is_networked()) {
if (network_client_is_connected()) {
DrawText("Connection: online", 5, 5, 12, GREEN);
} else {
DrawText("Connection: offline", 5, 5, 12, RED);
}
} else {
DrawText("Connection: single-player", 5, 5, 12, BLUE);
}
}
void platform_shutdown() {
renderer_shutdown();
CloseWindow();
}
uint8_t platform_is_running() {
return !WindowShouldClose();
}
void platform_get_block_realpos(float *x, float *y){
camera cam = camera_get();
Vector2 mpos = GetMousePosition();
entity_view *e = game_world_view_active_get_entity(cam.ent_id);
if (!e) return;
float zoom = renderer_zoom_get();
mpos.x -= screenWidth/2.0f;
mpos.y -= screenHeight/2.0f;
cam.x += mpos.x*(1.0f/zoom);
cam.y += mpos.y*(1.0f/zoom);
cam.x = ((int32_t)cam.x / (int32_t)(WORLD_BLOCK_SIZE)) * WORLD_BLOCK_SIZE;
cam.y = ((int32_t)cam.y / (int32_t)(WORLD_BLOCK_SIZE)) * WORLD_BLOCK_SIZE;
cam.x += WORLD_BLOCK_SIZE/2.0f;
cam.y += WORLD_BLOCK_SIZE/2.0f;
if (x) *x = (float)cam.x;
if (y) *y = (float)cam.y;
}
static game_keystate_data last_input_data = {0};
static pkt_send_blockpos last_blockpos_data = {0};
inline static
void platform_input_update_input_frame(game_keystate_data data) {
float mx = 0, my = 0;
platform_get_block_realpos(&mx, &my);
if (mx != last_blockpos_data.mx || my != last_blockpos_data.my){
last_blockpos_data.mx = mx;
last_blockpos_data.my = my;
game_action_send_blockpos(mx, my);
}
// NOTE(zaklaus): Test if there are any changes
if (data.x != last_input_data.x) goto send_data;
if (data.y != last_input_data.y) goto send_data;
if (data.use != last_input_data.use) goto send_data;
if (data.sprint != last_input_data.sprint) goto send_data;
if (data.ctrl != last_input_data.ctrl) goto send_data;
if (data.pick != last_input_data.pick) goto send_data;
if (data.storage_action != last_input_data.storage_action) goto send_data;
if (data.selected_item != last_input_data.selected_item) goto send_data;
if (data.drop != last_input_data.drop) goto send_data;
if (data.swap != last_input_data.swap) goto send_data;
if (data.swap_from != last_input_data.swap_from) goto send_data;
if (data.swap_to != last_input_data.swap_to) goto send_data;
if (data.placement_num != last_input_data.placement_num) goto send_data;
if (data.deletion_mode != last_input_data.deletion_mode) goto send_data;
if (zpl_memcompare(data.placements, last_input_data.placements, zpl_size_of(data.placements))) goto send_data;
return;
send_data:
last_input_data = data;
game_action_send_keystate(&data);
}
void platform_input() {
float mouse_z = (GetMouseWheelMove()*0.5f);
if (mouse_z != 0.0f) {
target_zoom = zpl_clamp(target_zoom+mouse_z, 0.1f, 11.0f);
}
// NOTE(zaklaus): keystate handling
{
float x=0.0f, y=0.0f;
uint8_t use, sprint, drop, ctrl, pick;
if (IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D)) x += 1.0f;
if (IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A)) x -= 1.0f;
if (IsKeyDown(KEY_UP) || IsKeyDown(KEY_W)) y += 1.0f;
if (IsKeyDown(KEY_DOWN) || IsKeyDown(KEY_S)) y -= 1.0f;
use = IsKeyPressed(KEY_SPACE);
sprint = IsKeyDown(KEY_LEFT_SHIFT) || IsKeyDown(KEY_RIGHT_SHIFT);
ctrl = IsKeyDown(KEY_LEFT_CONTROL) || IsKeyDown(KEY_RIGHT_CONTROL);
drop = IsKeyPressed(KEY_G) || player_inv.drop_item || storage_inv.drop_item;
// NOTE(zaklaus): NEW! mouse movement
Vector2 mouse_pos = GetMousePosition();
mouse_pos.x /= screenWidth;
mouse_pos.y /= screenHeight;
mouse_pos.x -= 0.5f;
mouse_pos.y -= 0.5f;
mouse_pos = Vector2Normalize(mouse_pos);
if (game_get_kind() == GAMEKIND_SINGLE && IsMouseButtonDown(MOUSE_MIDDLE_BUTTON)) {
x = mouse_pos.x;
y = -mouse_pos.y;
}
inv_keystate *inv = (inv_is_storage_action) ? &storage_inv : &player_inv;
inv_keystate *inv2 = (!inv_is_storage_action) ? &storage_inv : &player_inv;
// NOTE(zaklaus): don't perform picking if we manipulate our inventories
pick = (inv_is_inside||inv->item_is_held||inv2->item_is_held) ? false : IsMouseButtonDown(MOUSE_LEFT_BUTTON);
game_keystate_data in_data = {
.x = x,
.y = y,
.mx = mouse_pos.x,
.my = mouse_pos.y,
.use = use,
.sprint = sprint,
.ctrl = ctrl,
.pick = pick,
.drop = drop,
.storage_action = inv_is_storage_action,
.selected_item = player_inv.selected_item,
.storage_selected_item = storage_inv.selected_item,
.swap = inv->swap,
.swap_storage = inv_swap_storage,
.swap_from = inv->swap_from,
.swap_to = inv->swap_to,
.deletion_mode = build_is_deletion_mode,
};
if (build_submit_placements) {
in_data.placement_num = build_num_placements;
zpl_memcopy(in_data.placements, build_placements, build_num_placements*zpl_size_of(item_placement));
}
platform_input_update_input_frame(in_data);
}
// NOTE(zaklaus): cycle through viewers
{
if (IsKeyPressed(KEY_Q)) {
game_world_view_cycle_active(-1);
}
else if (IsKeyPressed(KEY_E)) {
game_world_view_cycle_active(1);
}
}
// NOTE(zaklaus): switch render modes
{
if (IsKeyPressed(KEY_O)) {
renderer_switch(1-gfx_kind);
}
}
// NOTE(zaklaus): toggle debug drawing
#ifndef ECO2D_PROD
{
if (IsKeyPressed(KEY_T)) {
debug_draw_enable(!debug_draw_state());
}
}
#endif
}
void draw_selected_item() {
camera cam = camera_get();
entity_view *oe = game_world_view_active_get_entity(cam.ent_id);
if (oe) {
// NOTE(zaklaus): sel item
entity_view *e = game_world_view_active_get_entity(oe->sel_ent);
if (e && e->kind == EKIND_DEVICE) {
renderer_draw_single(e->x, e->y, ASSET_BLANK, ColorAlpha(RED, 0.4f));
}else{
// NOTE(zaklaus): hover item
entity_view *e = game_world_view_active_get_entity(oe->pick_ent);
if (e && e->kind == EKIND_DEVICE) {
renderer_draw_single(e->x, e->y, ASSET_BLANK, ColorAlpha(RED, 0.1f));
}
}
}
}
void platform_render() {
#if !defined(PLATFORM_WEB)
screenWidth = (uint16_t)GetScreenWidth();
screenHeight = (uint16_t)GetScreenHeight();
#else
uint16_t newScreenWidth = (uint16_t)canvas_get_width();
uint16_t newScreenHeight = (uint16_t)canvas_get_height();
if (newScreenWidth != screenWidth || newScreenHeight != screenHeight) {
screenWidth = newScreenWidth;
screenHeight = newScreenHeight;
SetWindowSize(screenWidth, screenHeight);
}
#endif
profile(PROF_ENTITY_LERP) {
game_world_view_active_entity_map(lerp_entity_positions);
game_world_view_active_entity_map(do_entity_fadeinout);
}
assets_resources_frame();
BeginDrawing();
{
profile (PROF_RENDER) {
renderer_draw();
draw_selected_item();
}
renderer_debug_draw();
{
// NOTE(zaklaus): add-ins
buildmode_draw();
inventory_draw();
}
display_conn_status();
debug_draw();
}
EndDrawing();
if (request_shutdown) {
CloseWindow();
}
}
float platform_frametime() {
return GetFrameTime();
}
float platform_zoom_get(void) {
return target_zoom;
}
void platform_request_close(void) {
request_shutdown = true;
}