Restructure/refactor of main.c
This commit is contained in:
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user