From a62ac36a7e401b39d760237778623f5a2b13f91f Mon Sep 17 00:00:00 2001 From: Paweł Redman Date: Sat, 16 Dec 2017 15:32:44 +0100 Subject: Distort the tile grid when rendering. --- src/common.hpp | 2 ++ src/math.hpp | 5 +++++ src/render.cpp | 40 +++++++++++++++++++++++++++++++++------- 3 files changed, 40 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/common.hpp b/src/common.hpp index 7b784bf..1ae3595 100644 --- a/src/common.hpp +++ b/src/common.hpp @@ -311,6 +311,8 @@ namespace render { class state_t { sf::RenderWindow *window; + procgen::prng_t prng; + procgen::perlin_noise_t perlin; void render_sector(world::sector_t *sector); void drender_text(rectf_t rect, std::string str); diff --git a/src/math.hpp b/src/math.hpp index 13eb36a..5e117fd 100644 --- a/src/math.hpp +++ b/src/math.hpp @@ -216,6 +216,11 @@ public: return r; } + vec_t wrap(void) + { + return *this - this->floor(); + } + T len(void) { return std::sqrt(*this * *this); diff --git a/src/render.cpp b/src/render.cpp index f773f92..2e8447b 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -10,6 +10,9 @@ state_t::state_t(sf::RenderWindow *window_) window = window_; font.loadFromFile("assets/FanwoodText.otf"); + + prng.seed(396); + perlin.generate(&prng, 16); } // FIXME: rename @@ -20,6 +23,7 @@ void register_tile(uint8_t type, const char *path) printf("load %s\n", path); tiles[type] = new sf::Texture; tiles[type]->loadFromFile(path); + tiles[type]->setRepeated(true); } void state_t::begin_frame(double now_, double dt_) @@ -70,10 +74,32 @@ void state_t::drender_entity(world::entity_t *ent) drender_text(ent->render_bounds, ss.str()); } +static const v2f_t tile_base[8] ={ + {1.0f, 0.5f}, {1.0f, 1.0f}, {0.5f, 1.0f}, {0.0f, 1.0f}, + {0.0f, 0.5f}, {0.0f, 0.0f}, {0.5f, 0.0f}, {1.0f, 0.0f} +}; + +static void generate_tile_verts(sf::Vertex *verts, v2f_t tx, procgen::perlin_noise_t *perlin) +{ + for (size_t i = 0; i < 8; i++) { + v2f_t base, turb; + + base = tile_base[i] + tx; + + turb[0] = perlin->get(base, 1.0f) * 0.5f; + turb[1] = perlin->get(base, 2.0f) * 0.5f; + + verts[i].position = base + turb; + verts[i].texCoords = (base + turb) * 32.0f; + verts[i].color = sf::Color::White; + } +} + 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::Vertex verts[8]; sf::Texture *texture; v2f_t tx; world::tile_t *tile; @@ -87,13 +113,9 @@ void state_t::render_sector(world::sector_t *sector) 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); + sf::RenderStates states(texture); + generate_tile_verts(verts, tx, &perlin); + window->draw(verts, 8, sf::TrianglesFan, states); if (debug_draw_tile_coords) { world::tile_index_t local(x, y); @@ -116,6 +138,7 @@ void state_t::render(game::state_t *game) { sf::Vector2u size = window->getSize(); v2f_t A, B, C, D; + const v2f_t margin(1.0f, 1.0f); rectf_t bbox; std::list ents; @@ -129,6 +152,9 @@ void state_t::render(game::state_t *game) bbox[1][0] = std::max({A[0], B[0], C[0], D[0]}); bbox[1][1] = std::max({A[1], B[1], C[1], D[1]}); + bbox[0] -= margin; + bbox[1] += margin; + for (world::sector_t *sector : game->world.get_sectors(bbox)) render_sector(sector); -- cgit