summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaweł Redman <pawel.redman@gmail.com>2017-12-12 15:44:19 +0100
committerPaweł Redman <pawel.redman@gmail.com>2017-12-12 15:44:19 +0100
commitc33d0fa9c43fb316a9c6bbb2be81446dce7f49da (patch)
treea4ded215d9dd7c5098b395925f0c19956c46670c
parent5667984053536d855ba0917deac326d429f2c5b4 (diff)
Move tile data to src/game.
-rw-r--r--Makefile1
-rw-r--r--src/common.hpp19
-rw-r--r--src/game/assets.cpp10
-rw-r--r--src/game/game.cpp8
-rw-r--r--src/game/game.hpp29
-rw-r--r--src/game/worldgen.cpp29
-rw-r--r--src/main.cpp2
-rw-r--r--src/render.cpp95
-rw-r--r--src/world.cpp41
9 files changed, 138 insertions, 96 deletions
diff --git a/Makefile b/Makefile
index 7ddca74..802dc37 100644
--- a/Makefile
+++ b/Makefile
@@ -12,6 +12,7 @@ PP_RM := $(PP_BOLD)$(shell tput setf 4)RM$(PP_RESET)
SRC := src/game/assets.cpp \
src/game/game.cpp \
src/game/interface.cpp \
+ src/game/worldgen.cpp \
src/main.cpp \
src/path_finder.cpp \
src/procgen.cpp \
diff --git a/src/common.hpp b/src/common.hpp
index e3bd19c..20c5a12 100644
--- a/src/common.hpp
+++ b/src/common.hpp
@@ -53,12 +53,6 @@ namespace world {
{-1, 0}, {-1, -1}, {0, -1}, {+1, -1}
};
- enum {
- TILE_NONE,
- TILE_DIRT,
- TILE_WALL
- };
-
class tile_t {
public:
unsigned type : 8;
@@ -94,6 +88,8 @@ namespace world {
float frac;
} trace_t;
+ void register_tile(uint8_t type, cflags_t cflags);
+
class world_t {
procgen::prng_t prng;
procgen::perlin_noise_t perlin;
@@ -107,7 +103,10 @@ namespace world {
friend render::state_t;
public:
+ void (*generator)(tile_t*, tile_index_t, procgen::perlin_noise_t*) = 0;
+
world_t(void);
+
sector_t *get_sector(sector_index_t index, bool partial = false);
tile_t *get_tile(tile_index_t index, bool partial = false);
@@ -266,9 +265,12 @@ namespace render {
ALIGN_CENTER_BOTTOM
} text_align_t;
+ void register_tile(uint8_t type, const char *path);
+
class state_t {
sf::RenderWindow *window;
+ void render_sector(world::sector_t *sector);
void drender_text(rectf_t rect, std::string str);
void drender_entity(world::entity_t *ent);
public:
@@ -277,6 +279,7 @@ namespace render {
state_t(sf::RenderWindow *window_);
void begin_frame(double time_, double dt_);
void end_frame(void);
+
void render(game::state_t *game);
void render(animated_texture_t *anim, rectf_t bounds, bool mirror = false);
void render(oriented_sprite_t *sprite, rectf_t bounds, float angle);
@@ -291,10 +294,6 @@ namespace render {
extern render::state_t *debug_render;
-namespace assets {
- void load(void);
-};
-
namespace text {
extern const wchar_t *unit_no_path;
extern const wchar_t *unit_blocked;
diff --git a/src/game/assets.cpp b/src/game/assets.cpp
index 32eff81..c20d77b 100644
--- a/src/game/assets.cpp
+++ b/src/game/assets.cpp
@@ -1,6 +1,6 @@
#include "game.hpp"
-namespace assets {
+namespace game::assets {
human_assets_t human;
sf::Texture tile_dirt;
@@ -13,8 +13,10 @@ void load(void)
human.legs_idle.load("assets/units/human/legs_idle", 2, 2);
human.legs_walking.load("assets/units/human/legs_walking", 2, 2);
- tile_dirt.loadFromFile("assets/tiles/dirt.png");
- tile_wall.loadFromFile("assets/tiles/wall.png");
+ world::register_tile(TILE_DIRT, 0);
+ world::register_tile(TILE_WALL, 1);
+ render::register_tile(TILE_DIRT, "assets/tiles/dirt.png");
+ render::register_tile(TILE_WALL, "assets/tiles/wall.png");
}
-} // namespace assets
+} // namespace game::assets
diff --git a/src/game/game.cpp b/src/game/game.cpp
index eb3966d..385a940 100644
--- a/src/game/game.cpp
+++ b/src/game/game.cpp
@@ -216,6 +216,8 @@ void state_t::start(void)
{
human_t *human;
+ world.generator = worldgen;
+
human = new human_t;
human->place(&world, v2f_t(0.5, 0.5));
units.insert(human);
@@ -267,4 +269,10 @@ void state_t::tick(double now_, double dt_)
unit->keep_moving(now, dt);
}
+bool load_assets(void)
+{
+ assets::load();
+ return true;
+}
+
} //namespace game
diff --git a/src/game/game.hpp b/src/game/game.hpp
index 3e10bbb..5da6370 100644
--- a/src/game/game.hpp
+++ b/src/game/game.hpp
@@ -1,14 +1,25 @@
#include "../common.hpp"
-namespace assets {
- typedef struct {
- render::oriented_sprite_4M_t head_idle, body_idle;
- render::oriented_sprite_4M2_t legs_idle, legs_walking;
- } human_assets_t;
+namespace game {
+ enum {
+ TILE_NONE,
+ TILE_DIRT,
+ TILE_WALL
+ };
- extern human_assets_t human;
- extern sf::Texture tile_dirt;
- extern sf::Texture tile_wall;
+ void worldgen(world::tile_t *tile, world::tile_index_t x,
+ procgen::perlin_noise_t *perlin);
- void load(void);
+ namespace assets {
+ typedef struct {
+ render::oriented_sprite_4M_t head_idle, body_idle;
+ render::oriented_sprite_4M2_t legs_idle, legs_walking;
+ } human_assets_t;
+
+ extern human_assets_t human;
+ extern sf::Texture tile_dirt;
+ extern sf::Texture tile_wall;
+
+ void load(void);
+ }
};
diff --git a/src/game/worldgen.cpp b/src/game/worldgen.cpp
new file mode 100644
index 0000000..573d03b
--- /dev/null
+++ b/src/game/worldgen.cpp
@@ -0,0 +1,29 @@
+#include "game.hpp"
+
+namespace game {
+
+void worldgen(world::tile_t *tile, world::tile_index_t x,
+ procgen::perlin_noise_t *perlin)
+{
+ float waterlevel, height;
+
+ waterlevel = perlin->get(x, 1000.0f) * 0.3f +
+ perlin->get(x, 500.0f) * 0.1f;
+
+ height = perlin->get(x, 40.0f) * 0.6f +
+ perlin->get(x, 20.0f) * 0.25f +
+ perlin->get(x, 10.0f) * 0.2f +
+ perlin->get(x, 4.0f) * 0.1f +
+ perlin->get(x, 1.0f) * 0.05f;
+
+ if (height < waterlevel)
+ tile->type = TILE_WALL;
+ else if (height < waterlevel + 0.1)
+ tile->type = TILE_DIRT;
+ else if (perlin->get(x, 3.0f) > 0.0f)
+ tile->type = TILE_WALL;
+ else
+ tile->type = TILE_DIRT;
+}
+
+}
diff --git a/src/main.cpp b/src/main.cpp
index f4b1105..3bac2dc 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -28,7 +28,7 @@ int main()
window.setVerticalSyncEnabled(true);
text::load_strings("pl");
- assets::load();
+ game::load_assets();
game.start();
diff --git a/src/render.cpp b/src/render.cpp
index ba50d00..48beaa9 100644
--- a/src/render.cpp
+++ b/src/render.cpp
@@ -5,51 +5,6 @@
static sf::RectangleShape wot_rect;
static sf::Font font;
-static void draw_tile(sf::RenderWindow *window, v2f_t x, world::tile_t *tile,
- world::tile_index_t local)
-{
- sf::Texture *texture;
-
- switch (tile->type) {
- default:
- return;
-
- /*case world::TILE_DIRT:
- texture = &assets::tile_dirt;
- break;
-
- case world::TILE_WALL:
- texture = &assets::tile_wall;
- break;*/
- }
-
- wot_rect.setTexture(texture, true);
- wot_rect.setSize(sf::Vector2f(1.0f, 1.0f));
- wot_rect.setPosition(x);
- wot_rect.setFillColor(sf::Color::White);
- wot_rect.setOutlineColor(sf::Color::Transparent);
- window->draw(wot_rect);
- wot_rect.setTexture(NULL);
-
- if (debug_draw_tile_coords) {
- std::stringstream ss;
- ss << "L=" << local;
- sf::Text text(ss.str(), font, 20);
- text.setPosition(x);
- text.setScale(0.005, 0.005);
- window->draw(text);
- }
-}
-
-static void draw_sector(sf::RenderWindow *window, world::sector_t *sector)
-{
- for (ssize_t y = 0; y < SECTOR_SIZE; y++)
- for (ssize_t x = 0; x < SECTOR_SIZE; x++)
- draw_tile(window, sector->bounds.v[0] + v2f_t(x, y),
- sector->tiles + y * SECTOR_SIZE + x,
- world::tile_index_t(x, y));
-}
-
void interface::state_t::render_to(render::state_t *render)
{
if (select.selecting)
@@ -65,6 +20,16 @@ state_t::state_t(sf::RenderWindow *window_)
font.loadFromFile("assets/LiberationMono-Regular.ttf");
}
+// FIXME: rename
+static sf::Texture *tiles[256] = {0};
+
+void register_tile(uint8_t type, const char *path)
+{
+ printf("load %s\n", path);
+ tiles[type] = new sf::Texture;
+ tiles[type]->loadFromFile(path);
+}
+
void state_t::begin_frame(double now_, double dt_)
{
now = now_;
@@ -109,6 +74,44 @@ void state_t::drender_entity(world::entity_t *ent)
drender_text(ent->render_bounds, ss.str());
}
+void state_t::render_sector(world::sector_t *sector)
+{
+ for (ssize_t y = 0; y < SECTOR_SIZE; y++)
+ for (ssize_t x = 0; x < SECTOR_SIZE; x++) {
+ sf::Texture *texture;
+ v2f_t tx;
+ world::tile_t *tile;
+
+ tx = sector->bounds.v[0] + v2f_t(x, y);
+ tile = sector->tiles + y * SECTOR_SIZE + x;
+
+ texture = tiles[tile->type];
+ if (!texture) {
+ printf("draw_tile: tile %i not registered\n", tile->type);
+ abort();
+ }
+
+ wot_rect.setTexture(texture, true);
+ wot_rect.setSize(sf::Vector2f(1.0f, 1.0f));
+ wot_rect.setPosition(tx);
+ wot_rect.setFillColor(sf::Color::White);
+ wot_rect.setOutlineColor(sf::Color::Transparent);
+ window->draw(wot_rect);
+ wot_rect.setTexture(NULL);
+
+ if (debug_draw_tile_coords) {
+ world::tile_index_t local(x, y);
+ std::stringstream ss;
+
+ ss << "L=" << local;
+ sf::Text text(ss.str(), font, 20);
+ text.setPosition(tx);
+ text.setScale(0.005, 0.005);
+ window->draw(text);
+ }
+ }
+}
+
void state_t::render(game::state_t *game)
{
sf::Vector2u size = window->getSize();
@@ -127,7 +130,7 @@ void state_t::render(game::state_t *game)
bbox[1][1] = std::max({A[1], B[1], C[1], D[1]});
for (world::sector_t *sector : game->world.get_sectors(bbox))
- draw_sector(window, sector);
+ render_sector(sector);
ents = game->world.get_render_entities(bbox);
ents.sort(
diff --git a/src/world.cpp b/src/world.cpp
index dc35f46..e326bf3 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -19,27 +19,12 @@ world_t::world_t(void)
perlin.generate(&prng, 32);
}
-void world_t::generate_tile(tile_t *tile, tile_index_t x)
+// FIXME: rename
+static cflags_t tiles[256] = {0};
+
+void register_tile(uint8_t type, cflags_t cflags)
{
- float waterlevel, height;
-
- waterlevel = perlin.get(x, 1000.0f) * 0.3f +
- perlin.get(x, 500.0f) * 0.1f;
-
- height = perlin.get(x, 40.0f) * 0.6f +
- perlin.get(x, 20.0f) * 0.25f +
- perlin.get(x, 10.0f) * 0.2f +
- perlin.get(x, 4.0f) * 0.1f +
- perlin.get(x, 1.0f) * 0.05f;
-
- if (height < waterlevel)
- tile->type = TILE_NONE;
- else if (height < waterlevel + 0.1)
- tile->type = TILE_DIRT;
- else if (perlin.get(x, 3.0f) > 0.0f)
- tile->type = TILE_WALL;
- else
- tile->type = TILE_DIRT;
+ tiles[type] = cflags;
}
void world_t::generate(sector_t *sector, sector_index_t index, bool partial)
@@ -50,10 +35,12 @@ void world_t::generate(sector_t *sector, sector_index_t index, bool partial)
sector->bounds.v[1] = sector->bounds.v[0] + v2f_t(SECTOR_SIZE, SECTOR_SIZE);
for (coord_t ly = 0; ly < SECTOR_SIZE; ly++)
- for (coord_t lx = 0; lx < SECTOR_SIZE; lx++)
- generate_tile(sector->tiles + ly * SECTOR_SIZE + lx,
- tile_index_t(index[0] * SECTOR_SIZE + lx,
- index[1] * SECTOR_SIZE + ly));
+ for (coord_t lx = 0; lx < SECTOR_SIZE; lx++) {
+ tile_t *tile = sector->tiles + ly * SECTOR_SIZE + lx;
+ tile_index_t tile_index(index[0] * SECTOR_SIZE + lx,
+ index[1] * SECTOR_SIZE + ly);
+ generator(tile, tile_index, &perlin);
+ }
sector->empty = false;
@@ -98,7 +85,7 @@ bool world_t::find_path(v2f_t src, v2f_t dst, cmodel_t *cmodel, entity_t *ignore
index = finder.base + tile_index_t(x, y);
- if (get_tile(index)->type == TILE_DIRT)
+ if (tiles[get_tile(index)->type] & cmodel->cflags)
continue;
combined[0] = v2f_t(index) - cmodel_dims / 2;
@@ -251,7 +238,8 @@ bool world_t::test_rect(const cmodel_t *cmodel, const entity_t *ignore)
tile_t *tile;
tile = sector->tiles + index[1] * SECTOR_SIZE + index[0];
- if (tile->type != TILE_DIRT)
+
+ if (tiles[tile->type] & cmodel->cflags)
return true;
}
@@ -376,6 +364,7 @@ trace_t world_t::trace(v2f_t start, v2f_t end, cflags_t cflags)
trace_t trace_cmodel(v2f_t start, v2f_t end, const cmodel_t *cmodel)
{
// TODO
+ return {0};
}
void world_t::debug_point(sf::Vector2f point)