@@ -4,6 +4,7 @@ | |||
extern "C" void mod_init(Swan::Mod &mod) { | |||
mod.init("core"); | |||
mod.registerTile("air", "assets/tiles/air.png"); | |||
mod.registerTile("stone", "assets/tiles/stone.png"); | |||
mod.registerTile("dirt", "assets/tiles/dirt.png"); | |||
mod.registerTile("grass", "assets/tiles/grass.png"); |
@@ -1,5 +1,6 @@ | |||
include_directories("${CMAKE_CURRENT_LIST_DIR}/include/swan") | |||
add_library(libswan SHARED | |||
src/Chunk.cc | |||
src/Body.cc | |||
src/Game.cc | |||
src/Mod.cc |
@@ -28,30 +28,18 @@ public: | |||
drawBlock(tmap, x, y, id); | |||
} | |||
void drawBlock(TileMap &tmap, int x, int y, Tile::TileID id) { | |||
Tile *t = tmap.get(id); | |||
fprintf(stderr, "Drawing %s to %i,%i in chunk %i,%i\n", t->name_.c_str(), x, y, x_, y_); | |||
void drawBlock(int x, int y, Tile *t) { | |||
texture_.update(t->image_, x * TILE_SIZE, y * TILE_SIZE); | |||
dirty_ = true; | |||
} | |||
void drawBlock(TileMap &tmap, int x, int y) { | |||
drawBlock(tmap, x, y, tiles_[x][y]); | |||
} | |||
void clear() { | |||
memset(tiles_, 0, sizeof(tiles_)); | |||
void drawBlock(TileMap &tmap, int x, int y, Tile::TileID id) { | |||
drawBlock(x, y, tmap.get(id)); | |||
} | |||
void draw(Win &win) { | |||
if (dirty_) { | |||
sprite_.setTexture(texture_); | |||
dirty_ = false; | |||
} | |||
win.setPos(Vec2(x_ * CHUNK_WIDTH, y_ * CHUNK_HEIGHT)); | |||
win.draw(sprite_); | |||
} | |||
void redraw(TileMap &tmap); | |||
void fill(TileMap &tmap, Tile::TileID id); | |||
void draw(Win &win); | |||
}; | |||
} |
@@ -0,0 +1,33 @@ | |||
#include "Chunk.h" | |||
namespace Swan { | |||
void Chunk::redraw(TileMap &tmap) { | |||
for (int x = 0; x < CHUNK_WIDTH; ++x) { | |||
for (int y = 0; y < CHUNK_HEIGHT; ++y) { | |||
drawBlock(tmap, x, y, tiles_[x][y]); | |||
} | |||
} | |||
} | |||
void Chunk::fill(TileMap &tmap, Tile::TileID id) { | |||
Tile *air = tmap.get(0); | |||
for (int x = 0; x < CHUNK_WIDTH; ++x) { | |||
for (int y = 0; y < CHUNK_HEIGHT; ++y) { | |||
tiles_[x][y] = id; | |||
drawBlock(x, y, air); | |||
} | |||
} | |||
} | |||
void Chunk::draw(Win &win) { | |||
if (dirty_) { | |||
sprite_.setTexture(texture_); | |||
dirty_ = false; | |||
} | |||
win.setPos(Vec2(x_ * CHUNK_WIDTH, y_ * CHUNK_HEIGHT)); | |||
win.draw(sprite_); | |||
} | |||
} |
@@ -19,7 +19,7 @@ void Mod::registerTile(const std::string &name, const std::string &asset) { | |||
std::string asset_path = path_ + "/" + asset; | |||
if (!t.image_.loadFromFile(asset_path)) { | |||
fprintf(stderr, "Tile %s: Failed to load image %s!\n", t.name_.c_str(), asset_path.c_str()); | |||
fprintf(stderr, "Tile %s: Failed to load image %s\n", t.name_.c_str(), asset_path.c_str()); | |||
} | |||
} | |||
@@ -21,7 +21,7 @@ void WorldPlane::setTile(int x, int y, Tile::TileID id) { | |||
if (chunk == NULL) { | |||
chunks_.push_back(Chunk(chx, chy)); | |||
chunk = &chunks_.back(); | |||
chunk->clear(); | |||
chunk->fill(world_->tile_map_, 0); | |||
} | |||
chunk->setTile(world_->tile_map_, rx, ry, id); |
@@ -72,7 +72,7 @@ int main() { | |||
game.tick(); | |||
} | |||
window.clear(sf::Color(135, 206, 250)); | |||
window.clear(); | |||
game.draw(win); | |||
window.display(); | |||
} |