@@ -3,7 +3,7 @@ project(swan) | |||
find_package(SFML 2.5 COMPONENTS graphics system window REQUIRED) | |||
add_compile_options(-Wall -Wextra -Wpedantic -Wno-unused-parameter) | |||
add_compile_options(-Wall -Wextra -Wpedantic -Wno-unused-parameter -g) | |||
include_directories("${PROJECT_SOURCE_DIR}/libswan/include") | |||
add_subdirectory(libswan) |
@@ -1,5 +1,3 @@ | |||
include_directories("${CMAKE_CURRENT_LIST_DIR}/include/swan") | |||
add_library(libswan SHARED | |||
src/Body.cc | |||
src/Chunk.cc | |||
@@ -8,4 +6,5 @@ add_library(libswan SHARED | |||
src/Player.cc | |||
src/World.cc | |||
src/WorldPlane.cc) | |||
target_include_directories(libswan PUBLIC "include/swan") | |||
set_target_properties(libswan PROPERTIES OUTPUT_NAME swan) |
@@ -23,11 +23,15 @@ public: | |||
sprite_ = sf::Sprite(texture_); | |||
} | |||
void setTile(TileMap &tmap, int x, int y, Tile::ID id) { | |||
void setTileID(TileMap &tmap, int x, int y, Tile::ID id) { | |||
tiles_[x][y] = id; | |||
drawBlock(tmap, x, y, id); | |||
} | |||
Tile *getTile(TileMap &tmap, int x, int y) { | |||
return tmap.get(tiles_[x][y]); | |||
} | |||
void drawBlock(int x, int y, Tile *t) { | |||
texture_.update(t->image_, x * TILE_SIZE, y * TILE_SIZE); | |||
dirty_ = true; |
@@ -1,6 +1,7 @@ | |||
#pragma once | |||
#include <vector> | |||
#include <utility> | |||
#include "common.h" | |||
#include "Chunk.h" | |||
@@ -14,12 +15,14 @@ class World; | |||
class WorldPlane { | |||
public: | |||
using ID = uint16_t; | |||
using Coord = std::pair<int, int>; | |||
std::vector<Chunk> chunks_; | |||
std::map<Coord, Chunk> chunks_; | |||
ID id_; | |||
World *world_; | |||
void setTile(int x, int y, Tile::ID id); | |||
void setTileID(int x, int y, Tile::ID id); | |||
Tile *getTile(int x, int y); | |||
void draw(Win &win); | |||
void update(float dt); |
@@ -4,32 +4,33 @@ | |||
namespace Swan { | |||
void WorldPlane::setTile(int x, int y, Tile::ID id) { | |||
int chx = x / CHUNK_WIDTH; | |||
int chy = y / CHUNK_HEIGHT; | |||
int rx = x % CHUNK_WIDTH; | |||
int ry = y % CHUNK_HEIGHT; | |||
Chunk *chunk = NULL; | |||
for (auto &ch: chunks_) { | |||
if (ch.x_ == chx && ch.y_ == chy) { | |||
chunk = &ch; | |||
break; | |||
} | |||
void WorldPlane::setTileID(int x, int y, Tile::ID id) { | |||
auto coord = Coord(x / CHUNK_WIDTH, y / CHUNK_HEIGHT); | |||
auto it = chunks_.find(coord); | |||
if (it == chunks_.end()) { | |||
it = chunks_.emplace(coord, Chunk(coord.first, coord.second)).first; | |||
it->second.fill(world_->tile_map_, 0); | |||
} | |||
if (chunk == NULL) { | |||
chunks_.push_back(Chunk(chx, chy)); | |||
chunk = &chunks_.back(); | |||
chunk->fill(world_->tile_map_, 0); | |||
it->second.setTileID(world_->tile_map_, x % CHUNK_WIDTH, y % CHUNK_HEIGHT, id); | |||
} | |||
Tile *WorldPlane::getTile(int x, int y) { | |||
auto coord = Coord(x / CHUNK_WIDTH, y / CHUNK_HEIGHT); | |||
auto it = chunks_.find(coord); | |||
if (it == chunks_.end()) { | |||
it = chunks_.emplace(coord, Chunk(coord.first, coord.second)).first; | |||
it->second.fill(world_->tile_map_, 0); | |||
} | |||
chunk->setTile(world_->tile_map_, rx, ry, id); | |||
return it->second.getTile(world_->tile_map_, x % CHUNK_WIDTH, y % CHUNK_HEIGHT); | |||
} | |||
void WorldPlane::draw(Win &win) { | |||
for (auto &chunk: chunks_) { | |||
chunk.draw(win); | |||
for (auto &p: chunks_) { | |||
p.second.draw(win); | |||
} | |||
} | |||
@@ -33,9 +33,9 @@ int main() { | |||
for (int x = 1; x < 10; ++x) { | |||
for (int y = 3; y < 10; ++y) { | |||
if (y == 3) | |||
plane.setTile(x, y, tGrass); | |||
plane.setTileID(x, y, tGrass); | |||
else | |||
plane.setTile(x, y, tStone); | |||
plane.setTileID(x, y, tStone); | |||
} | |||
} | |||