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