@@ -1,7 +1,6 @@ | |||
include_directories("${CMAKE_CURRENT_LIST_DIR}/include/swan") | |||
add_library(libswan SHARED | |||
src/Body.cc | |||
src/Chunk.cc | |||
src/Game.cc | |||
src/Mod.cc | |||
src/Player.cc |
@@ -1,7 +1,11 @@ | |||
#pragma once | |||
#include <SFML/Graphics/Texture.hpp> | |||
#include <string.h> | |||
#include "common.h" | |||
#include "Tile.h" | |||
#include "TileMap.h" | |||
namespace Swan { | |||
@@ -9,14 +13,45 @@ class Chunk { | |||
public: | |||
int x_; | |||
int y_; | |||
bool dirty_ = false; | |||
Tile::TileID tiles_[CHUNK_WIDTH][CHUNK_HEIGHT]; | |||
sf::Texture texture_; | |||
sf::Sprite sprite_; | |||
Chunk(int x, int y): x_(x), y_(y) { | |||
texture_.create(CHUNK_WIDTH * TILE_SIZE, CHUNK_HEIGHT * TILE_SIZE); | |||
sprite_ = sf::Sprite(texture_); | |||
} | |||
void setTile(int x, int y, Tile::TileID tile) { | |||
tiles_[x][y] = tile; | |||
void setTile(TileMap &tmap, int x, int y, Tile::TileID id) { | |||
tiles_[x][y] = id; | |||
drawBlock(tmap, x, y, id); | |||
} | |||
void clear(); | |||
void draw(Win &win); | |||
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_); | |||
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 draw(Win &win) { | |||
if (dirty_) { | |||
sprite_.setTexture(texture_); | |||
dirty_ = false; | |||
} | |||
win.setPos(Vec2(x_ * CHUNK_WIDTH, y_ * CHUNK_HEIGHT)); | |||
win.draw(sprite_); | |||
} | |||
}; | |||
} |
@@ -0,0 +1,27 @@ | |||
#pragma once | |||
#include "Tile.h" | |||
namespace Swan { | |||
class TileMap { | |||
public: | |||
std::vector<Tile *> tiles_; | |||
std::map<std::string, Tile::TileID> id_map_; | |||
Tile::TileID getID(const std::string &name) { | |||
return id_map_[name]; | |||
} | |||
Tile *get(Tile::TileID id) { | |||
return tiles_[id]; | |||
} | |||
void registerTile(Tile *t) { | |||
Tile::TileID id = tiles_.size(); | |||
tiles_.push_back(t); | |||
id_map_[t->name_] = id; | |||
} | |||
}; | |||
} |
@@ -16,18 +16,19 @@ public: | |||
WorldPlane::PlaneID current_plane_; | |||
std::vector<WorldPlane> planes_; | |||
std::vector<Tile *> registered_tiles_; | |||
std::map<std::string, Tile::TileID> tile_id_map_; | |||
TileMap tile_map_; | |||
WorldPlane::PlaneID addPlane(); | |||
void setCurrentPlane(WorldPlane::PlaneID id) { current_plane_ = id; } | |||
WorldPlane &getPlane(WorldPlane::PlaneID id) { return planes_[id]; } | |||
Tile::TileID getTileID(const std::string &name) { | |||
return tile_id_map_[name]; | |||
return tile_map_.getID(name); | |||
} | |||
void registerTile(Tile *t); | |||
void registerTile(Tile *t) { | |||
tile_map_.registerTile(t); | |||
} | |||
void draw(Win &win); | |||
void update(float dt); |
@@ -5,17 +5,21 @@ | |||
#include "common.h" | |||
#include "Chunk.h" | |||
#include "Tile.h" | |||
#include "TileMap.h" | |||
namespace Swan { | |||
class World; | |||
class WorldPlane { | |||
public: | |||
using PlaneID = uint16_t; | |||
std::vector<Chunk> chunks_; | |||
PlaneID id_; | |||
World *world_; | |||
void setTile(int x, int y, Tile::TileID tile); | |||
void setTile(int x, int y, Tile::TileID id); | |||
void draw(Win &win); | |||
void update(float dt); |
@@ -5,7 +5,7 @@ | |||
namespace Swan { | |||
static constexpr float UNIT_SIZE = 12; | |||
static constexpr float TILE_SIZE = 32; | |||
static constexpr int TICK_RATE = 20; | |||
static constexpr int CHUNK_HEIGHT = 32; | |||
static constexpr int CHUNK_WIDTH = 32; | |||
@@ -53,18 +53,17 @@ public: | |||
struct Win { | |||
public: | |||
sf::RenderWindow &window_; | |||
sf::Transform &transform_; | |||
Vec2 curr_pos_ = { 0, 0 }; | |||
sf::RenderWindow *window_; | |||
sf::Transform transform_; | |||
void setPos(Vec2 &pos) { | |||
transform_.translate(-curr_pos_.x, -curr_pos_.y); | |||
transform_.translate(pos.x, pos.y); | |||
curr_pos_ = pos; | |||
Win(sf::RenderWindow *win): window_(win) {} | |||
void setPos(const Vec2 &pos) { | |||
transform_ = sf::Transform().translate(pos); | |||
} | |||
void draw(const sf::Drawable &drawable) { | |||
window_.draw(drawable, transform_); | |||
window_->draw(drawable, transform_); | |||
} | |||
}; | |||
@@ -11,12 +11,11 @@ void Body::gravity(Vec2 g) { | |||
} | |||
void Body::outline(Win &win) { | |||
win.setPos(pos_); | |||
sf::RectangleShape rect(size_); | |||
sf::RectangleShape rect(size_ * TILE_SIZE); | |||
rect.setPosition(pos_ * TILE_SIZE); | |||
rect.setFillColor(sf::Color::Transparent); | |||
rect.setOutlineColor(sf::Color(128, 128, 128)); | |||
rect.setOutlineThickness(2 / UNIT_SIZE); | |||
rect.setOutlineThickness(2); | |||
win.draw(rect); | |||
} | |||
@@ -1,14 +0,0 @@ | |||
#include "Chunk.h" | |||
#include <string.h> | |||
namespace Swan { | |||
void Chunk::clear() { | |||
memset(tiles_, 0, sizeof(tiles_)); | |||
} | |||
void Chunk::draw(Win &win) { | |||
} | |||
} |
@@ -7,15 +7,10 @@ WorldPlane::PlaneID World::addPlane() { | |||
planes_.push_back(WorldPlane()); | |||
WorldPlane &plane = planes_.back(); | |||
plane.id_ = id; | |||
plane.world_ = this; | |||
return id; | |||
} | |||
void World::registerTile(Tile *t) { | |||
Tile::TileID id = registered_tiles_.size(); | |||
registered_tiles_.push_back(t); | |||
tile_id_map_[t->name_] = id; | |||
} | |||
void World::draw(Win &win) { | |||
planes_[current_plane_].draw(win); | |||
player_->draw(win); |
@@ -1,8 +1,10 @@ | |||
#include "WorldPlane.h" | |||
#include "World.h" | |||
namespace Swan { | |||
void WorldPlane::setTile(int x, int y, Tile::TileID tile) { | |||
void WorldPlane::setTile(int x, int y, Tile::TileID id) { | |||
int chx = x / CHUNK_WIDTH; | |||
int chy = y / CHUNK_HEIGHT; | |||
int rx = x % CHUNK_WIDTH; | |||
@@ -17,15 +19,18 @@ void WorldPlane::setTile(int x, int y, Tile::TileID tile) { | |||
} | |||
if (chunk == NULL) { | |||
chunks_.push_back(Chunk()); | |||
chunks_.push_back(Chunk(chx, chy)); | |||
chunk = &chunks_.back(); | |||
chunk->clear(); | |||
} | |||
chunk->setTile(rx, ry, tile); | |||
chunk->setTile(world_->tile_map_, rx, ry, id); | |||
} | |||
void WorldPlane::draw(Win &win) { | |||
for (auto &chunk: chunks_) { | |||
chunk.draw(win); | |||
} | |||
} | |||
void WorldPlane::update(float dt) { |
@@ -18,10 +18,7 @@ static double getTime() { | |||
int main() { | |||
sf::RenderWindow window(sf::VideoMode(800, 600), "good gaem"); | |||
window.setVerticalSyncEnabled(true); | |||
sf::Transform transform; | |||
transform.scale(UNIT_SIZE, UNIT_SIZE); | |||
Win win = { window, transform }; | |||
Win win(&window); | |||
Game game; | |||
game.loadMod("core.mod"); | |||
@@ -32,7 +29,11 @@ int main() { | |||
Tile::TileID tStone = game.world_->getTileID("core::stone"); | |||
WorldPlane &plane = game.world_->getPlane(game.world_->current_plane_); | |||
plane.setTile(2, 2, tStone); | |||
for (int x = 1; x < 10; ++x) { | |||
for (int y = 1; y < 10; ++y) { | |||
plane.setTile(x, y, tStone); | |||
} | |||
} | |||
double prevtime = getTime(); | |||
double fpsAcc = 0; |