Restructure/refactor of main.c
This commit is contained in:
@@ -9,10 +9,16 @@ add_subdirectory(engine/)
|
||||
|
||||
|
||||
add_executable(PixelDefense
|
||||
game/systems/entity_renderer.c
|
||||
game/systems/systems.h
|
||||
|
||||
game/utils/building_types.h
|
||||
|
||||
game/common.h
|
||||
game/components.h
|
||||
game/entrypoint.c
|
||||
game/main.c
|
||||
game/map_init.c
|
||||
game/map_init.h
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -17,22 +17,23 @@ typedef struct BzAppDesc {
|
||||
BzAppInitFunc init;
|
||||
BzAppUpdateFunc update;
|
||||
BzAppRenderFunc render;
|
||||
BzAppRenderFunc imguiRender;
|
||||
BzAppDeinitFunc deinit;
|
||||
|
||||
bool useNuklear;
|
||||
bool useFlecs;
|
||||
|
||||
void *userData;
|
||||
} BzAppDesc;
|
||||
|
||||
extern struct nk_context *NK;
|
||||
typedef struct ecs_world_t ecs_world_t;
|
||||
extern ecs_world_t *ECS;
|
||||
|
||||
extern bool bzMain(BzAppDesc *appDesc, int argc, const char **argv);
|
||||
|
||||
#ifdef BZ_ENTRYPOINT
|
||||
#include <flecs.h>
|
||||
#include <rlImGui.h>
|
||||
|
||||
struct nk_context *NK = NULL;
|
||||
ecs_world_t *ECS = NULL;
|
||||
|
||||
// https://www.raylib.com/examples/core/loader.html?name=core_custom_logging
|
||||
@@ -93,9 +94,10 @@ int main(int argc, const char **argv) {
|
||||
SetTargetFPS(appDesc.fps);
|
||||
// Initialize modules
|
||||
|
||||
if (appDesc.useFlecs) {
|
||||
if (appDesc.useFlecs)
|
||||
ECS = ecs_init();
|
||||
}
|
||||
if (appDesc.imguiRender)
|
||||
rlImGuiSetup(true);
|
||||
|
||||
// User initialize
|
||||
if (appDesc.init && !appDesc.init(appDesc.userData)) {
|
||||
@@ -113,6 +115,12 @@ int main(int argc, const char **argv) {
|
||||
BeginDrawing();
|
||||
if (appDesc.render)
|
||||
appDesc.render(dt, appDesc.userData);
|
||||
|
||||
if (appDesc.imguiRender) {
|
||||
rlImGuiBegin();
|
||||
appDesc.imguiRender(dt, appDesc.userData);
|
||||
rlImGuiEnd();
|
||||
}
|
||||
EndDrawing();
|
||||
}
|
||||
|
||||
@@ -121,6 +129,8 @@ int main(int argc, const char **argv) {
|
||||
appDesc.deinit(appDesc.userData);
|
||||
|
||||
// Deinitialize modules
|
||||
if (appDesc.imguiRender)
|
||||
rlImGuiShutdown();
|
||||
if (ECS) {
|
||||
ecs_fini(ECS);
|
||||
ECS = NULL;
|
||||
|
||||
@@ -9,20 +9,20 @@
|
||||
|
||||
BzTileMap BZ_TILEMAP_INVALID = {.isValid = false};
|
||||
|
||||
void bzTileLayerSkipRender(BzTileLayer *layer) {
|
||||
void bzTileLayerSkipRender(BzTileMap *map, BzTileLayer *layer) {
|
||||
|
||||
}
|
||||
void bzTileObjectGroupSkipRender(BzTileObjectGroup *objectGroup) {
|
||||
void bzTileObjectGroupSkipRender(BzTileMap *map, BzTileObjectGroup *objectGroup) {
|
||||
|
||||
}
|
||||
|
||||
BzTileLayerRenderFunc BZ_TILE_LAYER_SKIP_RENDER = bzTileLayerSkipRender;
|
||||
BzTileObjectGroupRenderFunc BZ_TILE_OBJECTS_SKIP_RENDER = bzTileObjectGroupSkipRender;
|
||||
|
||||
bool bzTileLayerClear(BzTileLayer *layer, BzTile *data, i32 dataCount) {
|
||||
bool bzTileLayerClear(BzTileMap *map, BzTileLayer *layer) {
|
||||
return true;
|
||||
}
|
||||
bool bzTileObjectsClear(BzTileObjectGroup *objectGroup, BzTileObject *objects, i32 objectCount) {
|
||||
bool bzTileObjectsClear(BzTileMap *map, BzTileObjectGroup *objectGroup) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -45,6 +45,12 @@ BzTileset *bzTileLayerGetTileset(BzTileMap *map, BzTileLayer *layer) {
|
||||
return NULL;
|
||||
return &map->tilesets[idx];
|
||||
}
|
||||
BzTileset *bzTileObjectGroupGetTileset(BzTileMap *map, BzTileObjectGroup *objectGroup) {
|
||||
i32 idx = objectGroup->tilesetIdx;
|
||||
if (idx < 0 || idx >= map->tilesetCount)
|
||||
return NULL;
|
||||
return &map->tilesets[idx];
|
||||
}
|
||||
|
||||
static void handleTileLayer(BzTileLayer *layer, cute_tiled_layer_t *cuteLayer) {
|
||||
layer->id = cuteLayer->id;
|
||||
@@ -283,7 +289,7 @@ void bzTileMapOverrideLayer(BzTileMap *map, i32 slotID, BzTileLayerFunc func) {
|
||||
BzTileLayer *layer = map->layers + slotID;
|
||||
i32 dataCount = layer->dataCount;
|
||||
BzTile *data = layer->data;
|
||||
if (func(layer, data, dataCount)) {
|
||||
if (func(map, layer)) {
|
||||
if (!layer->hasOwnership) return;
|
||||
bzFree(layer->data);
|
||||
if (layer->data == data) {
|
||||
@@ -300,7 +306,7 @@ void bzTileMapOverrideObjectGroup(BzTileMap *map, i32 slotID, BzTileObjectsFunc
|
||||
BzTileObjectGroup *objectGroup = map->objectGroups + slotID;
|
||||
BzTileObject *objects = objectGroup->objects;
|
||||
i32 objectCount = objectGroup->objectCount;
|
||||
if (func(objectGroup, objects, objectCount)) {
|
||||
if (func(map, objectGroup)) {
|
||||
bzFree(objectGroup->objects);
|
||||
if (objectGroup->objects == objects) {
|
||||
objectGroup->objects = NULL;
|
||||
|
||||
@@ -11,9 +11,10 @@
|
||||
typedef struct BzTileLayer BzTileLayer;
|
||||
typedef struct BzTileObject BzTileObject;
|
||||
typedef struct BzTileObjectGroup BzTileObjectGroup;
|
||||
typedef struct BzTileMap BzTileMap;
|
||||
|
||||
typedef void (*BzTileLayerRenderFunc)(BzTileLayer *layer);
|
||||
typedef void (*BzTileObjectGroupRenderFunc)(BzTileObjectGroup *objectGroup);
|
||||
typedef void (*BzTileLayerRenderFunc)(BzTileMap *map, BzTileLayer *layer);
|
||||
typedef void (*BzTileObjectGroupRenderFunc)(BzTileMap *map, BzTileObjectGroup *objectGroup);
|
||||
|
||||
extern BzTileLayerRenderFunc BZ_TILE_LAYER_SKIP_RENDER;
|
||||
extern BzTileObjectGroupRenderFunc BZ_TILE_OBJECTS_SKIP_RENDER;
|
||||
@@ -105,8 +106,8 @@ typedef struct BzTileMap {
|
||||
extern BzTileMap BZ_TILEMAP_INVALID;
|
||||
|
||||
// Return true, if you want to override data pointer (you are responsible for cleanup, if you override)
|
||||
typedef bool (*BzTileLayerFunc)(BzTileLayer *layer, BzTile *data, i32 dataCount);
|
||||
typedef bool (*BzTileObjectsFunc)(BzTileObjectGroup *objectGroup, BzTileObject *objects, i32 objectsCount);
|
||||
typedef bool (*BzTileLayerFunc)(BzTileMap *map, BzTileLayer *layer);
|
||||
typedef bool (*BzTileObjectsFunc)(BzTileMap *map, BzTileObjectGroup *objectGroup);
|
||||
|
||||
extern BzTileLayerFunc BZ_TILE_LAYER_CLEAR;
|
||||
extern BzTileObjectsFunc BZ_TILE_OBJECTS_CLEAR;
|
||||
@@ -114,6 +115,7 @@ extern BzTileObjectsFunc BZ_TILE_OBJECTS_CLEAR;
|
||||
BzTile bzTileLayerGetTile(BzTileLayer *layer, i32 x, i32 y);
|
||||
void bzTileLayerSetTile(BzTileLayer *layer, BzTile tile, i32 x, i32 y, i32 w, i32 h);
|
||||
BzTileset *bzTileLayerGetTileset(BzTileMap *map, BzTileLayer *layer);
|
||||
BzTileset *bzTileObjectGroupGetTileset(BzTileMap *map, BzTileObjectGroup *objectGroup);
|
||||
|
||||
BzTileMap bzTileMapCreate(const BzTileMapDesc *desc);
|
||||
void bzTileMapDestroy(BzTileMap *map);
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
#ifndef PIXELDEFENSE_COMMON_H
|
||||
#define PIXELDEFENSE_COMMON_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#endif //PIXELDEFENSE_COMMON_H
|
||||
@@ -3,18 +3,33 @@
|
||||
|
||||
#include <breeze.h>
|
||||
|
||||
#include "utils/building_types.h"
|
||||
|
||||
typedef struct TilePosition {
|
||||
BzTile x;
|
||||
BzTile y;
|
||||
} TilePosition;
|
||||
|
||||
typedef struct TileSize {
|
||||
BzTile w;
|
||||
BzTile h;
|
||||
BzTile sizeX;
|
||||
BzTile sizeY;
|
||||
} TileSize;
|
||||
|
||||
typedef struct Owner {
|
||||
BuildingType playerID;
|
||||
} Owner;
|
||||
|
||||
typedef Vector2 Position, Size;
|
||||
|
||||
typedef f32 Rotation;
|
||||
|
||||
typedef f32 Health;
|
||||
|
||||
typedef struct TextureRegion {
|
||||
Texture2D texture;
|
||||
Rectangle rec;
|
||||
} TextureRegion;
|
||||
|
||||
|
||||
|
||||
#endif //PIXELDEFENSE_COMPONENTS_H
|
||||
|
||||
2
game/entrypoint.c
Normal file
2
game/entrypoint.c
Normal file
@@ -0,0 +1,2 @@
|
||||
#define BZ_ENTRYPOINT
|
||||
#include <breeze.h>
|
||||
19
game/game_state.h
Normal file
19
game/game_state.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef PIXELDEFENSE_GAME_STATE_H
|
||||
#define PIXELDEFENSE_GAME_STATE_H
|
||||
|
||||
#include <breeze.h>
|
||||
#include <flecs.h>
|
||||
|
||||
typedef struct Game {
|
||||
Camera2D camera;
|
||||
BzTileset terrainTileset;
|
||||
BzTileset buildingsTileset;
|
||||
BzTileset entitiesTileset;
|
||||
BzTileMap map;
|
||||
int selectedBuilding;
|
||||
ecs_entity_t *entityMap;
|
||||
} Game;
|
||||
|
||||
extern Game *GAME;
|
||||
|
||||
#endif //PIXELDEFENSE_GAME_STATE_H
|
||||
199
game/main.c
199
game/main.c
@@ -1,95 +1,36 @@
|
||||
#include <stdio.h>
|
||||
#include <flecs.h>
|
||||
#define BZ_ENTRYPOINT
|
||||
#include <breeze.h>
|
||||
|
||||
#include <raylib.h>
|
||||
#include <rlImGui.h>
|
||||
|
||||
#include "utils/building_types.h"
|
||||
#include "components.h"
|
||||
#include "game_state.h"
|
||||
#include "map_init.h"
|
||||
|
||||
typedef enum Layers {
|
||||
LAYER_TERRAIN = 0,
|
||||
LAYER_FOLIAGE,
|
||||
LAYER_TREES,
|
||||
LAYER_TREES2,
|
||||
LAYER_BUILDINGS,
|
||||
LAYER_BUILDING_OWNER,
|
||||
} Layers;
|
||||
#include <rlImGui.h>
|
||||
|
||||
typedef enum ObjectGroup {
|
||||
OBJECTS_GAME = 0,
|
||||
OBJECTS_ENTITIES,
|
||||
} ObjectGroup;
|
||||
Game *GAME = NULL;
|
||||
|
||||
typedef struct Game {
|
||||
Camera2D camera;
|
||||
BzTileset terrainTileset;
|
||||
BzTileset buildingsTileset;
|
||||
BzTileset entitiesTileset;
|
||||
BzTileMap map;
|
||||
} Game;
|
||||
bool init(Game *game);
|
||||
void deinit(Game *game);
|
||||
|
||||
static Game GAME = {};
|
||||
static int selectedBuilding = 0;
|
||||
void update(float dt, Game *game);
|
||||
void render(float dt, Game *game);
|
||||
void imguiRender(float dt, Game *game);
|
||||
|
||||
bool handleGameObjects(BzTileObjectGroup *objectLayer, BzTileObject *objects, i32 objectCount) {
|
||||
for (i32 i = 0; i < objectLayer->objectCount; i++) {
|
||||
BzTileObject object = objectLayer->objects[i];
|
||||
if (bzStringDefaultHash("camera") == object.id) {
|
||||
printf("Got camera\n");
|
||||
GAME.camera.target.x = object.shape.x;
|
||||
GAME.camera.target.y = object.shape.y;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
ecs_entity_t *entityMap = NULL;
|
||||
bool bzMain(BzAppDesc *appDesc, int argc, const char **argv) {
|
||||
appDesc->width = 1280;
|
||||
appDesc->height = 720;
|
||||
appDesc->title = "PixelDefense";
|
||||
appDesc->fps = 60;
|
||||
|
||||
bool prepareBuildings(BzTileLayer *layer, BzTile *data, i32 dataCount) {
|
||||
ECS_COMPONENT(ECS, TilePosition);
|
||||
ECS_COMPONENT(ECS, TileSize);
|
||||
ECS_COMPONENT(ECS, Owner);
|
||||
appDesc->init = (BzAppInitFunc) init;
|
||||
appDesc->deinit = (BzAppDeinitFunc) deinit;
|
||||
appDesc->update = (BzAppUpdateFunc) update;
|
||||
appDesc->render = (BzAppRenderFunc) render;
|
||||
appDesc->imguiRender = (BzAppRenderFunc) imguiRender;
|
||||
|
||||
entityMap = bzCalloc(sizeof(*entityMap), layer->width * layer->height);
|
||||
GAME = bzAlloc(sizeof(*GAME));
|
||||
appDesc->userData = GAME;
|
||||
appDesc->useFlecs = true;
|
||||
|
||||
BzTileMap *map = &GAME.map;
|
||||
BzTileLayer *ownershipLayer = layer;
|
||||
BzTileLayer *buildingLayer = bzTileMapGetLayer(map, LAYER_BUILDINGS);
|
||||
BzTileset *buildingTileset = bzTileLayerGetTileset(map, buildingLayer);
|
||||
BzTile *buildingData = buildingLayer->data;
|
||||
|
||||
for (i32 y = 0; y < layer->height; y++) {
|
||||
for (i32 x = 0; x < layer->width; x++) {
|
||||
BzTile ownerTile = data[y * layer->width + x];
|
||||
BzTile buildingRawTile = buildingData[y * layer->width + x];
|
||||
BzTile buildingTile = bzTilesetGetTile(buildingTileset, buildingRawTile);
|
||||
buildingTile = getTileBuilding(buildingTile);
|
||||
if (buildingTile == BUILDINGS_NONE || ownerTile == 0) continue;
|
||||
// We have a building
|
||||
TileSize size = {};
|
||||
getBuildingSize(buildingTile, &size.w, &size.h);
|
||||
bzTileLayerSetTile(ownershipLayer, 0, x, y, size.w, size.h);
|
||||
bzLogInfo("Got size: %2d %2d", size.w, size.h);
|
||||
|
||||
ecs_entity_t e = ecs_new_id(ECS);
|
||||
ecs_set(ECS, e, TilePosition, {.x=x, .y=y});
|
||||
ecs_set(ECS, e, TileSize, {.w=size.w, .h=size.h});
|
||||
ownerTile = bzTilesetGetTile(buildingTileset, ownerTile);
|
||||
ownerTile = getTileBuilding(ownerTile);
|
||||
ecs_set(ECS, e, Owner, {.playerID=ownerTile});
|
||||
|
||||
for (i32 yIdx = y; yIdx < y + size.h; yIdx++) {
|
||||
for (i32 xIdx = x; xIdx < x + size.w; xIdx++) {
|
||||
entityMap[yIdx * layer->width + xIdx] = e;
|
||||
}
|
||||
}
|
||||
|
||||
//bzTileMapUpdateCollider(&GAME.map, x, y);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -167,7 +108,7 @@ void placeBuilding(BzTileMap *map, BuildingType type, i32 posX, i32 posY, i32 si
|
||||
// Create entity
|
||||
ecs_entity_t e = ecs_new_id(ECS);
|
||||
ecs_set(ECS, e, TilePosition, {.x=posX, .y=posY});
|
||||
ecs_set(ECS, e, TileSize, {.w=sizeX, .h=sizeY});
|
||||
ecs_set(ECS, e, TileSize, {.sizeX=sizeX, .sizeY=sizeY});
|
||||
ecs_set(ECS, e, Owner, {.playerID=BUILDINGS_PLAYER_RED});
|
||||
|
||||
for (i32 y = posY; y < posY + sizeY; y++) {
|
||||
@@ -176,7 +117,7 @@ void placeBuilding(BzTileMap *map, BuildingType type, i32 posX, i32 posY, i32 si
|
||||
bzTileLayerSetTile(buildingLayer, layerTile, x, y, 1, 1);
|
||||
buildingTile++;
|
||||
|
||||
entityMap[y * buildingLayer->width + x] = e;
|
||||
GAME->entityMap[y * buildingLayer->width + x] = e;
|
||||
|
||||
bzTileMapUpdateCollider(map, x, y);
|
||||
}
|
||||
@@ -187,7 +128,6 @@ void placeBuilding(BzTileMap *map, BuildingType type, i32 posX, i32 posY, i32 si
|
||||
}
|
||||
|
||||
bool init(Game *game) {
|
||||
rlImGuiSetup(true);
|
||||
int screenWidth = 1280;
|
||||
int screenHeight = 720;
|
||||
|
||||
@@ -227,10 +167,10 @@ bool init(Game *game) {
|
||||
.objectGroups[OBJECTS_ENTITIES]=(BzTileObjectsDesc ) {"Entities"}
|
||||
});
|
||||
|
||||
bzTileMapOverrideLayer(&game->map, LAYER_BUILDING_OWNER, prepareBuildings);
|
||||
bzTileMapOverrideLayer(&game->map, LAYER_BUILDING_OWNER, initBuildingsLayer);
|
||||
|
||||
bzTileMapOverrideObjectGroup(&game->map, OBJECTS_GAME, handleGameObjects);
|
||||
//bzTileMapOverrideObjectGroup(&game->map, OBJECTS_ENTITIES, BZ_TILE_OBJECTS_CLEAR);
|
||||
bzTileMapOverrideObjectGroup(&game->map, OBJECTS_GAME, initGameObjectsLayer);
|
||||
bzTileMapOverrideObjectGroup(&game->map, OBJECTS_ENTITIES, initEntityObjectsLayer);
|
||||
|
||||
|
||||
return true;
|
||||
@@ -240,67 +180,79 @@ void deinit(Game *game) {
|
||||
bzTilesetDestroy(&game->terrainTileset);
|
||||
bzTilesetDestroy(&game->buildingsTileset);
|
||||
bzTilesetDestroy(&game->entitiesTileset);
|
||||
rlImGuiShutdown();
|
||||
|
||||
bzFree(GAME);
|
||||
GAME = NULL;
|
||||
}
|
||||
|
||||
void render(float dt, Game *game) {
|
||||
|
||||
void update(float dt, Game *game) {
|
||||
char titleBuf[32];
|
||||
snprintf(titleBuf, sizeof(titleBuf), "FPS: %d | %.2f ms", GetFPS(), GetFrameTime() * 1000);
|
||||
SetWindowTitle(titleBuf);
|
||||
Camera2D *camera = &game->camera;
|
||||
|
||||
ImGuiIO *io = igGetIO();
|
||||
if (io->WantCaptureMouse || io->WantCaptureKeyboard)
|
||||
return;
|
||||
|
||||
if (!io->WantCaptureMouse && !io->WantCaptureKeyboard) {
|
||||
if (IsKeyDown(KEY_W)) camera->target.y -= 20;
|
||||
if (IsKeyDown(KEY_S)) camera->target.y += 20;
|
||||
if (IsKeyDown(KEY_A)) camera->target.x -= 20;
|
||||
if (IsKeyDown(KEY_D)) camera->target.x += 20;
|
||||
if (IsKeyDown(KEY_W)) game->camera.target.y -= 20;
|
||||
if (IsKeyDown(KEY_S)) game->camera.target.y += 20;
|
||||
if (IsKeyDown(KEY_A)) game->camera.target.x -= 20;
|
||||
if (IsKeyDown(KEY_D)) game->camera.target.x += 20;
|
||||
|
||||
if (IsKeyDown(KEY_Q)) camera->rotation--;
|
||||
if (IsKeyDown(KEY_E)) camera->rotation++;
|
||||
if (IsKeyDown(KEY_Q)) game->camera.rotation--;
|
||||
if (IsKeyDown(KEY_E)) game->camera.rotation++;
|
||||
|
||||
camera->zoom += ((float) GetMouseWheelMove() * 0.05f);
|
||||
}
|
||||
|
||||
|
||||
|
||||
BeginMode2D(*camera);
|
||||
ClearBackground(RAYWHITE);
|
||||
|
||||
bzTileMapDraw(&game->map);
|
||||
bzTileMapDrawColliders(&game->map);
|
||||
game->camera.zoom += ((float) GetMouseWheelMove() * 0.05f);
|
||||
|
||||
Vector2 worldPos = GetScreenToWorld2D(GetMousePosition(), game->camera);
|
||||
int tileX = (int) worldPos.x / 16;
|
||||
int tileY = (int) worldPos.y / 16;
|
||||
|
||||
if (selectedBuilding) {
|
||||
if (game->selectedBuilding) {
|
||||
BzTile sizeX = 0, sizeY = 0;
|
||||
getBuildingSize(selectedBuilding, &sizeX, &sizeY);
|
||||
getBuildingSize(game->selectedBuilding, &sizeX, &sizeY);
|
||||
|
||||
bool canPlace = canBuildOn(&game->map, tileX, tileY, sizeX, sizeY);
|
||||
/*
|
||||
Color placeColor = canPlace ?
|
||||
(Color) {0, 255, 0, 200} :
|
||||
(Color) {255, 0, 0, 200};
|
||||
|
||||
DrawRectangleLines(tileX * 16, tileY * 16, sizeX * 16, sizeY * 16, placeColor);
|
||||
*/
|
||||
|
||||
if (canPlace && IsMouseButtonDown(MOUSE_BUTTON_LEFT) && !io->WantCaptureMouse) {
|
||||
placeBuilding(&game->map, selectedBuilding, tileX, tileY, sizeX, sizeY);
|
||||
if (canPlace && IsMouseButtonDown(MOUSE_BUTTON_LEFT)) {
|
||||
placeBuilding(&game->map, game->selectedBuilding, tileX, tileY, sizeX, sizeY);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void render(float dt, Game *game) {
|
||||
ClearBackground(RAYWHITE);
|
||||
BeginMode2D(game->camera);
|
||||
|
||||
bzTileMapDraw(&game->map);
|
||||
bzTileMapDrawColliders(&game->map);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
EndMode2D();
|
||||
|
||||
rlImGuiBegin();
|
||||
|
||||
|
||||
}
|
||||
|
||||
void imguiRender(float dt, Game *game) {
|
||||
igSetNextWindowSize((ImVec2){300, 400}, ImGuiCond_FirstUseEver);
|
||||
igBegin("Debug Menu", NULL, 0);
|
||||
if (igCollapsingHeader_TreeNodeFlags("BuildMenu", 0)) {
|
||||
for (int i = 0; i < BUILDINGS_COUNT; i++) {
|
||||
if (igSelectable_Bool(getBuildingStr(i), selectedBuilding == i, 0, (ImVec2){0,0}))
|
||||
selectedBuilding = i;
|
||||
if (igSelectable_Bool(getBuildingStr(i), game->selectedBuilding == i, 0, (ImVec2){0,0}))
|
||||
game->selectedBuilding = i;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -309,24 +261,5 @@ void render(float dt, Game *game) {
|
||||
}
|
||||
igEnd();
|
||||
igShowDemoWindow(NULL);
|
||||
rlImGuiEnd();
|
||||
|
||||
|
||||
}
|
||||
|
||||
bool bzMain(BzAppDesc *appDesc, int argc, const char **argv) {
|
||||
appDesc->width = 1280;
|
||||
appDesc->height = 720;
|
||||
appDesc->title = "PixelDefense";
|
||||
appDesc->fps = 60;
|
||||
|
||||
appDesc->init = (BzAppInitFunc) init;
|
||||
appDesc->deinit = (BzAppDeinitFunc) deinit;
|
||||
appDesc->render = (BzAppRenderFunc) render;
|
||||
|
||||
appDesc->userData = &GAME;
|
||||
appDesc->useNuklear = true;
|
||||
appDesc->useFlecs = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
85
game/map_init.c
Normal file
85
game/map_init.c
Normal file
@@ -0,0 +1,85 @@
|
||||
#include "map_init.h"
|
||||
|
||||
#include "components.h"
|
||||
#include "game_state.h"
|
||||
|
||||
#include <flecs.h>
|
||||
|
||||
|
||||
bool initGameObjectsLayer(BzTileMap *map, BzTileObjectGroup *objectGroup) {
|
||||
for (i32 i = 0; i < objectGroup->objectCount; i++) {
|
||||
BzTileObject object = objectGroup->objects[i];
|
||||
if (bzStringDefaultHash("camera") == object.id) {
|
||||
GAME->camera.target.x = object.shape.x;
|
||||
GAME->camera.target.y = object.shape.y;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool initEntityObjectsLayer(BzTileMap *map, BzTileObjectGroup *objectGroup) {
|
||||
ECS_COMPONENT(ECS, Position);
|
||||
ECS_COMPONENT(ECS, Size);
|
||||
ECS_COMPONENT(ECS, Rotation);
|
||||
ECS_COMPONENT(ECS, TextureRegion);
|
||||
|
||||
BzTileset *objectTileset = bzTileObjectGroupGetTileset(&GAME->map, objectGroup);
|
||||
if (!objectTileset) return true;
|
||||
|
||||
for (i32 i = 0; i < objectGroup->objectCount; i++) {
|
||||
BzTileObject object = objectGroup->objects[i];
|
||||
ecs_entity_t e = ecs_new_id(ECS);
|
||||
ecs_set(ECS, e, Position, {object.shape.x, object.shape.y});
|
||||
ecs_set(ECS, e, Size, {object.shape.sizeX, object.shape.sizeY});
|
||||
ecs_set(ECS, e, Rotation, {0.0f});
|
||||
ecs_set(ECS, e, TextureRegion, {objectTileset->tiles, bzTilesetGetTileRegion(objectTileset, object.gid)});
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
bool initBuildingsLayer(BzTileMap *map, BzTileLayer *layer) {
|
||||
ECS_COMPONENT(ECS, TilePosition);
|
||||
ECS_COMPONENT(ECS, TileSize);
|
||||
ECS_COMPONENT(ECS, Owner);
|
||||
|
||||
GAME->entityMap = bzCalloc(sizeof(*GAME->entityMap), layer->width * layer->height);
|
||||
|
||||
BzTileLayer *ownershipLayer = layer;
|
||||
BzTileLayer *buildingLayer = bzTileMapGetLayer(map, LAYER_BUILDINGS);
|
||||
BzTileset *buildingTileset = bzTileLayerGetTileset(map, buildingLayer);
|
||||
BzTile *data = layer->data;
|
||||
BzTile *buildingData = buildingLayer->data;
|
||||
|
||||
for (i32 y = 0; y < layer->height; y++) {
|
||||
for (i32 x = 0; x < layer->width; x++) {
|
||||
BzTile ownerTile = data[y * layer->width + x];
|
||||
BzTile buildingRawTile = buildingData[y * layer->width + x];
|
||||
BzTile buildingTile = bzTilesetGetTile(buildingTileset, buildingRawTile);
|
||||
buildingTile = getTileBuilding(buildingTile);
|
||||
if (buildingTile == BUILDINGS_NONE || ownerTile == 0) continue;
|
||||
// We have a building
|
||||
TileSize size = {};
|
||||
getBuildingSize(buildingTile, &size.sizeX, &size.sizeY);
|
||||
bzTileLayerSetTile(ownershipLayer, 0, x, y, size.sizeX, size.sizeY);
|
||||
bzLogInfo("Got size: %2d %2d", size.sizeX, size.sizeY);
|
||||
|
||||
ecs_entity_t e = ecs_new_id(ECS);
|
||||
ecs_set(ECS, e, TilePosition, {.x=x, .y=y});
|
||||
ecs_set(ECS, e, TileSize, {.sizeX=size.sizeX, .sizeY=size.sizeY});
|
||||
ownerTile = bzTilesetGetTile(buildingTileset, ownerTile);
|
||||
ownerTile = getTileBuilding(ownerTile);
|
||||
ecs_set(ECS, e, Owner, {.playerID=ownerTile});
|
||||
|
||||
for (i32 yIdx = y; yIdx < y + size.sizeY; yIdx++) {
|
||||
for (i32 xIdx = x; xIdx < x + size.sizeX; xIdx++) {
|
||||
GAME->entityMap[yIdx * layer->width + xIdx] = e;
|
||||
}
|
||||
}
|
||||
|
||||
//bzTileMapUpdateCollider(&GAME.map, x, y);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
}
|
||||
27
game/map_init.h
Normal file
27
game/map_init.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef PIXELDEFENSE_MAP_INITIALIZATION_H
|
||||
#define PIXELDEFENSE_MAP_INITIALIZATION_H
|
||||
|
||||
#include <breeze.h>
|
||||
|
||||
typedef enum Layers {
|
||||
LAYER_TERRAIN = 0,
|
||||
LAYER_FOLIAGE,
|
||||
LAYER_TREES,
|
||||
LAYER_TREES2,
|
||||
LAYER_BUILDINGS,
|
||||
LAYER_BUILDING_OWNER,
|
||||
} Layers;
|
||||
|
||||
typedef enum ObjectGroup {
|
||||
OBJECTS_GAME = 0,
|
||||
OBJECTS_ENTITIES,
|
||||
} ObjectGroup;
|
||||
|
||||
|
||||
bool initGameObjectsLayer(BzTileMap *map, BzTileObjectGroup *objectGroup);
|
||||
|
||||
bool initEntityObjectsLayer(BzTileMap *map, BzTileObjectGroup *objectGroup);
|
||||
|
||||
bool initBuildingsLayer(BzTileMap *map, BzTileLayer *layer);
|
||||
|
||||
#endif //PIXELDEFENSE_MAP_INITIALIZATION_H
|
||||
14
game/systems/entity_renderer.c
Normal file
14
game/systems/entity_renderer.c
Normal file
@@ -0,0 +1,14 @@
|
||||
#include "systems.h"
|
||||
|
||||
void renderEntities(ecs_iter_t *it) {
|
||||
Position *p = ecs_field(it, Position, 1);
|
||||
Size *s = ecs_field(it, Size, 2);
|
||||
Rotation *r = ecs_field(it, Rotation, 3);
|
||||
TextureRegion *t = ecs_field(it, TextureRegion, 4);
|
||||
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
Rectangle dst = {p[i].x, p[i].y, s[i].x, s[i].y};
|
||||
Vector2 origin = {dst.width * 0.5f, dst.height * 0.5f};
|
||||
DrawTexturePro(t[i].texture, t[i].rec, dst, origin, r[i], WHITE);
|
||||
}
|
||||
}
|
||||
10
game/systems/systems.h
Normal file
10
game/systems/systems.h
Normal file
@@ -0,0 +1,10 @@
|
||||
#ifndef PIXELDEFENSE_SYSTEMS_H
|
||||
#define PIXELDEFENSE_SYSTEMS_H
|
||||
|
||||
#include <flecs.h>
|
||||
|
||||
#include "../components.h"
|
||||
|
||||
void renderEntities(ecs_iter_t *it);
|
||||
|
||||
#endif //PIXELDEFENSE_SYSTEMS_H
|
||||
Reference in New Issue
Block a user