Move world files to engine
This commit is contained in:
@@ -13,12 +13,6 @@ add_executable(PixelDefense
|
||||
|
||||
game/main.c
|
||||
game/common.h
|
||||
game/world/map.c
|
||||
game/world/map.h
|
||||
game/world/tileset.c
|
||||
game/world/tileset.h
|
||||
game/world/layer.c
|
||||
game/world/layer.h
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -23,6 +23,10 @@ set(BreezeSources
|
||||
breeze/core/module_system.c
|
||||
|
||||
breeze/utils/tokenizer.c
|
||||
|
||||
breeze/world/layer.c
|
||||
breeze/world/map.c
|
||||
breeze/world/tileset.c
|
||||
)
|
||||
|
||||
set(BreezeHeaders
|
||||
@@ -33,6 +37,10 @@ set(BreezeHeaders
|
||||
|
||||
breeze/utils/tokenizer.h
|
||||
|
||||
breeze/world/layer.h
|
||||
breeze/world/map.h
|
||||
breeze/world/tileset.h
|
||||
|
||||
breeze/defines.h
|
||||
breeze/game.h
|
||||
|
||||
|
||||
@@ -8,6 +8,10 @@
|
||||
|
||||
#include "breeze/utils/tokenizer.h"
|
||||
|
||||
#include "breeze/world/layer.h"
|
||||
#include "breeze/world/map.h"
|
||||
#include "breeze/world/tileset.h"
|
||||
|
||||
#include "breeze/defines.h"
|
||||
#include "breeze/game.h"
|
||||
|
||||
|
||||
4
engine/breeze/world/layer.h
Normal file
4
engine/breeze/world/layer.h
Normal file
@@ -0,0 +1,4 @@
|
||||
#ifndef BREEZE_LAYER_H
|
||||
#define BREEZE_LAYER_H
|
||||
|
||||
#endif //BREEZE_LAYER_H
|
||||
4
engine/breeze/world/map.h
Normal file
4
engine/breeze/world/map.h
Normal file
@@ -0,0 +1,4 @@
|
||||
#ifndef BREEZE_MAP_H
|
||||
#define BREEZE_MAP_H
|
||||
|
||||
#endif //BREEZE_MAP_H
|
||||
@@ -1,22 +1,17 @@
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include <cute_tiled.h>
|
||||
|
||||
#include "tileset.h"
|
||||
|
||||
Tileset TILESET_INVALID = {.isValid = false};
|
||||
BzTileset BZ_TILESET_INVALID = {.isValid = false};
|
||||
|
||||
Tileset tilesetCreate(const TilesetDesc *desc) {
|
||||
Tileset tileset = {};
|
||||
cute_tiled_tileset_t *source = desc->source;
|
||||
BzTileset bzTilesetCreate(const BzTilesetDesc *desc) {
|
||||
BzTileset tileset = {};
|
||||
cute_tiled_tileset_t *source = cute_tiled_load_external_tileset(desc->path, NULL);
|
||||
|
||||
char pathBuffer[256];
|
||||
// auto terminates
|
||||
snprintf(pathBuffer, sizeof(pathBuffer), "%s%s%s",
|
||||
desc->assetDir ? desc->assetDir : "",
|
||||
desc->assetDir ? "/" : "",
|
||||
source->image.ptr);
|
||||
|
||||
tileset.tiles = LoadTexture(pathBuffer);
|
||||
tileset.tiles = LoadTexture(desc->texturePath);
|
||||
tileset.startID = source->firstgid;
|
||||
tileset.tileWidth = source->tilewidth;
|
||||
tileset.tileHeight = source->tileheight;
|
||||
@@ -27,17 +22,19 @@ Tileset tilesetCreate(const TilesetDesc *desc) {
|
||||
tileset.width = tileset.tiles.width / tileset.tileWidth;
|
||||
tileset.height = tileset.tiles.height / tileset.tileHeight;
|
||||
|
||||
cute_tiled_free_external_tileset(source);
|
||||
|
||||
if (tileset.tiles.width != source->imagewidth ||
|
||||
tileset.tiles.height != source->imageheight) {
|
||||
tilesetDestroy(&tileset);
|
||||
return TILESET_INVALID;
|
||||
bzTilesetDestroy(&tileset);
|
||||
return BZ_TILESET_INVALID;
|
||||
}
|
||||
|
||||
tileset.isValid = true;
|
||||
return tileset;
|
||||
}
|
||||
|
||||
Rectangle tilesetGetTileRegion(Tileset *tileset, int tileID) {
|
||||
Rectangle bzTilesetGetTileRegion(BzTileset *tileset, int tileID) {
|
||||
tileID = tileID - tileset->startID;
|
||||
int posX = tileID % tileset->width;
|
||||
int posY = tileID / tileset->width;
|
||||
@@ -45,8 +42,8 @@ Rectangle tilesetGetTileRegion(Tileset *tileset, int tileID) {
|
||||
tileset->tileWidth, tileset->tileHeight};
|
||||
}
|
||||
|
||||
void tilesetDestroy(Tileset *tileset) {
|
||||
void bzTilesetDestroy(BzTileset *tileset) {
|
||||
UnloadTexture(tileset->tiles);
|
||||
*tileset = TILESET_INVALID;
|
||||
*tileset = BZ_TILESET_INVALID;
|
||||
}
|
||||
|
||||
33
engine/breeze/world/tileset.h
Normal file
33
engine/breeze/world/tileset.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#ifndef BREEZE_TILESET_H
|
||||
#define BREEZE_TILESET_H
|
||||
|
||||
#include <raylib.h>
|
||||
|
||||
typedef struct BzTilesetDesc {
|
||||
const char *path;
|
||||
const char *texturePath;
|
||||
} BzTilesetDesc;
|
||||
|
||||
typedef struct BzTileset {
|
||||
Texture2D tiles;
|
||||
int startID;
|
||||
int tileWidth;
|
||||
int tileHeight;
|
||||
int width;
|
||||
int height;
|
||||
int offsetX;
|
||||
int offsetY;
|
||||
bool isValid;
|
||||
} BzTileset;
|
||||
|
||||
extern BzTileset BZ_TILESET_INVALID;
|
||||
|
||||
BzTileset bzTilesetCreate(const BzTilesetDesc *desc);
|
||||
|
||||
Rectangle bzTilesetGetTileRegion(BzTileset *tileset, int tileID);
|
||||
|
||||
void bzTilesetDestroy(BzTileset *tileset);
|
||||
|
||||
|
||||
|
||||
#endif //BREEZE_TILESET_H
|
||||
30
game/main.c
30
game/main.c
@@ -1,11 +1,11 @@
|
||||
#include <stdio.h>
|
||||
#include <raylib.h>
|
||||
#include <cute_tiled.h>
|
||||
#include <flecs.h>
|
||||
|
||||
#include <cute_tiled.h>
|
||||
|
||||
#define BZ_ENTRYPOINT
|
||||
#include <breeze.h>
|
||||
#include "world/tileset.h"
|
||||
|
||||
|
||||
static void drawTile(Texture2D texture, int tid, int tileSetWidth, int size, Vector2 pos) {
|
||||
Rectangle rec = {(tid % tileSetWidth) * size, (tid / tileSetWidth) * size, size, size};
|
||||
@@ -13,14 +13,14 @@ static void drawTile(Texture2D texture, int tid, int tileSetWidth, int size, Vec
|
||||
DrawTextureRec(texture, rec, pos, WHITE);
|
||||
}
|
||||
|
||||
static void drawLayer(cute_tiled_layer_t *layer, Tileset *tileset) {
|
||||
static void drawLayer(cute_tiled_layer_t *layer, BzTileset *tileset) {
|
||||
Vector2 pos = {layer->offsetx, layer->offsety};
|
||||
|
||||
for (int y = 0; y < layer->height; y++) {
|
||||
for (int x = 0; x < layer->width; x++) {
|
||||
int tile = layer->data[y * layer->width + x];
|
||||
if (tile - tileset->startID != -1) {
|
||||
Rectangle rec = tilesetGetTileRegion(tileset, tile);
|
||||
Rectangle rec = bzTilesetGetTileRegion(tileset, tile);
|
||||
DrawTextureRec(tileset->tiles, rec, pos, WHITE);
|
||||
}
|
||||
pos.x += tileset->tileWidth;
|
||||
@@ -33,8 +33,8 @@ static void drawLayer(cute_tiled_layer_t *layer, Tileset *tileset) {
|
||||
typedef struct Game {
|
||||
cute_tiled_map_t *map;
|
||||
Camera2D camera;
|
||||
Tileset terrainTileset;
|
||||
Tileset buildingsTileset;
|
||||
BzTileset terrainTileset;
|
||||
BzTileset buildingsTileset;
|
||||
cute_tiled_layer_t *terrain;
|
||||
cute_tiled_layer_t *trees;
|
||||
cute_tiled_layer_t *trees2;
|
||||
@@ -49,13 +49,13 @@ bool init(Game *game) {
|
||||
|
||||
cute_tiled_tileset_t *buildingsSource = cute_tiled_load_external_tileset("assets/buildings.tsj", NULL);
|
||||
|
||||
game->terrainTileset = tilesetCreate( &(TilesetDesc) {
|
||||
.source=terrainSource,
|
||||
.assetDir="assets"
|
||||
game->terrainTileset = bzTilesetCreate( &(BzTilesetDesc) {
|
||||
.path="assets/terrain.tsj",
|
||||
.texturePath="assets/terrain.png"
|
||||
});
|
||||
game->buildingsTileset = tilesetCreate(&(TilesetDesc) {
|
||||
.source=buildingsSource,
|
||||
.assetDir="assets"
|
||||
game->buildingsTileset = bzTilesetCreate(&(BzTilesetDesc) {
|
||||
.path="assets/buildings.tsj",
|
||||
.texturePath="assets/buildings.png"
|
||||
});
|
||||
|
||||
|
||||
@@ -106,8 +106,8 @@ bool init(Game *game) {
|
||||
return true;
|
||||
}
|
||||
void deinit(Game *game) {
|
||||
tilesetDestroy(&game->terrainTileset);
|
||||
tilesetDestroy(&game->buildingsTileset);
|
||||
bzTilesetDestroy(&game->terrainTileset);
|
||||
bzTilesetDestroy(&game->buildingsTileset);
|
||||
cute_tiled_free_map(game->map);
|
||||
}
|
||||
void render(float dt, Game *game) {
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
#ifndef PIXELDEFENSE_LAYER_H
|
||||
#define PIXELDEFENSE_LAYER_H
|
||||
|
||||
#endif //PIXELDEFENSE_LAYER_H
|
||||
@@ -1,4 +0,0 @@
|
||||
#ifndef PIXELDEFENSE_MAP_H
|
||||
#define PIXELDEFENSE_MAP_H
|
||||
|
||||
#endif //PIXELDEFENSE_MAP_H
|
||||
@@ -1,36 +0,0 @@
|
||||
#ifndef PIXELDEFENSE_TILESET_H
|
||||
#define PIXELDEFENSE_TILESET_H
|
||||
|
||||
#include <raylib.h>
|
||||
#include <cute_tiled.h>
|
||||
|
||||
#include "../common.h"
|
||||
|
||||
typedef struct TilesetDesc {
|
||||
cute_tiled_tileset_t *source;
|
||||
const char *assetDir;
|
||||
} TilesetDesc;
|
||||
|
||||
typedef struct Tileset {
|
||||
Texture2D tiles;
|
||||
int startID;
|
||||
int tileWidth;
|
||||
int tileHeight;
|
||||
int width;
|
||||
int height;
|
||||
int offsetX;
|
||||
int offsetY;
|
||||
bool isValid;
|
||||
} Tileset;
|
||||
|
||||
extern Tileset TILESET_INVALID;
|
||||
|
||||
Tileset tilesetCreate(const TilesetDesc *desc);
|
||||
|
||||
Rectangle tilesetGetTileRegion(Tileset *tileset, int tileID);
|
||||
|
||||
void tilesetDestroy(Tileset *tileset);
|
||||
|
||||
|
||||
|
||||
#endif //PIXELDEFENSE_TILESET_H
|
||||
Reference in New Issue
Block a user