Browse Source

almost ready to draw chunks!

opengl-renderer-broken
Martin Dørum 4 years ago
parent
commit
5bbe431f1b

+ 1
- 1
CMakeLists.txt View File

@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 2.6)
cmake_minimum_required(VERSION 2.8)
project(swan)

find_package(SFML 2.5 COMPONENTS graphics system window REQUIRED)

+ 2
- 1
core.mod/CMakeLists.txt View File

@@ -1,2 +1,3 @@
add_library(core.mod SHARED src/mod.cc)
set_target_properties(core.mod PROPERTIES OUTPUT_NAME core.mod PREFIX "")
set_target_properties(core.mod PROPERTIES OUTPUT_NAME mod PREFIX "")
file(COPY assets DESTINATION .)

+ 3
- 3
core.mod/src/mod.cc View File

@@ -4,9 +4,9 @@
extern "C" void mod_init(Swan::Mod &mod) {
mod.init("core");

mod.registerTile("stone", Swan::Tile("assets/tiles/stone.png"));
mod.registerTile("dirt", Swan::Tile("assets/tiles/dirt.png"));
mod.registerTile("grass", Swan::Tile("assets/tiles/grass.png"));
mod.registerTile("stone", "assets/tiles/stone.png");
mod.registerTile("dirt", "assets/tiles/dirt.png");
mod.registerTile("grass", "assets/tiles/grass.png");
}

int main() {

+ 5
- 1
libswan/include/swan/Chunk.h View File

@@ -9,7 +9,11 @@ class Chunk {
public:
int x_;
int y_;
Tile::TileID tiles_[CHUNK_HEIGHT][CHUNK_WIDTH];
Tile::TileID tiles_[CHUNK_WIDTH][CHUNK_HEIGHT];

void setTile(int x, int y, Tile::TileID tile) {
tiles_[x][y] = tile;
}

void clear();
void draw(Win &win);

+ 2
- 1
libswan/include/swan/Game.h View File

@@ -14,9 +14,10 @@ class Game {
public:
std::vector<Mod> registered_mods_;

World *world_;
World *world_ = NULL;

void loadMod(const std::string &path);
void createWorld();

void draw(Win &win);
void update(float dt);

+ 2
- 1
libswan/include/swan/Mod.h View File

@@ -13,11 +13,12 @@ public:
using ModID = uint32_t;

std::string name_;
std::string path_;
std::vector<Tile> tiles_;
bool inited_ = false;

void init(const std::string &name);
void registerTile(const std::string &name, const Tile &tile);
void registerTile(const std::string &name, const std::string &asset);
};

}

+ 2
- 0
libswan/include/swan/Tile.h View File

@@ -2,6 +2,7 @@

#include <stdint.h>
#include <string>
#include <SFML/Graphics/Image.hpp>

namespace Swan {

@@ -10,6 +11,7 @@ public:
using TileID = uint16_t;

std::string name_;
sf::Image image_;
};

}

+ 9
- 3
libswan/include/swan/World.h View File

@@ -13,16 +13,22 @@ class World {
public:
Player *player_;

WorldPlane *current_plane_;
std::vector<WorldPlane *> planes_;
WorldPlane::PlaneID current_plane_;
std::vector<WorldPlane> planes_;

std::vector<Tile> registered_tiles_;
std::vector<Tile *> registered_tiles_;
std::map<std::string, Tile::TileID> tile_id_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];
}

void registerTile(Tile *t);

void draw(Win &win);
void update(float dt);
void tick();

+ 6
- 4
libswan/include/swan/WorldPlane.h View File

@@ -4,16 +4,18 @@

#include "common.h"
#include "Chunk.h"
#include "Tile.h"

namespace Swan {

class WorldPlane {
public:
using PlaneID = uint16_t;

std::vector<Chunk> chunks_;
int max_chunk_x_ = 0;
int min_chunk_x_ = 0;
int max_chunk_y_ = 0;
int min_chunk_y_ = 0;
PlaneID id_;

void setTile(int x, int y, Tile::TileID tile);

void draw(Win &win);
void update(float dt);

+ 15
- 1
libswan/src/Game.cc View File

@@ -5,7 +5,8 @@
namespace Swan {

void Game::loadMod(const std::string &path) {
void *dl = dlopen(path.c_str(), RTLD_LAZY);
std::string dlpath = path + "/mod.so";
void *dl = dlopen(dlpath.c_str(), RTLD_LAZY);
if (dl == NULL) {
fprintf(stderr, "%s\n", dlerror());
return;
@@ -18,9 +19,22 @@ void Game::loadMod(const std::string &path) {

registered_mods_.push_back(Mod());
Mod &mod = registered_mods_.back();
mod.path_ = path;
mod_init(mod);
}

void Game::createWorld() {
if (world_ != NULL)
delete world_;

world_ = new World();
for (auto &mod: registered_mods_) {
for (auto &tile: mod.tiles_) {
world_->registerTile(&tile);
}
}
}

void Game::draw(Win &win) {
if (world_)
world_->draw(win);

+ 7
- 3
libswan/src/Mod.cc View File

@@ -11,12 +11,16 @@ void Mod::init(const std::string &name) {
fprintf(stderr, "Mod initing: %s\n", name_.c_str());
}

void Mod::registerTile(const std::string &name, const Tile &tile) {
tiles_.push_back(tile);
void Mod::registerTile(const std::string &name, const std::string &asset) {
tiles_.push_back(Tile());
Tile &t = tiles_.back();
t.name_ = name_ + "::" + name;
fprintf(stderr, "Adding tile: %s\n", t.name_.c_str());

fprintf(stderr, "Added tile: %s\n", t.name_.c_str());
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());
}
}

}

+ 19
- 7
libswan/src/World.cc View File

@@ -2,23 +2,35 @@

namespace Swan {

void World::draw(Win &win) {
for (WorldPlane *plane: planes_)
plane->draw(win);
WorldPlane::PlaneID World::addPlane() {
WorldPlane::PlaneID id = planes_.size();
planes_.push_back(WorldPlane());
WorldPlane &plane = planes_.back();
plane.id_ = id;
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);
}

void World::update(float dt) {
for (WorldPlane *plane: planes_)
plane->update(dt);
for (auto &plane: planes_)
plane.update(dt);

player_->update(dt);
}

void World::tick() {
for (WorldPlane *plane: planes_)
plane->tick();
for (auto &plane: planes_)
plane.tick();
}

}

+ 23
- 0
libswan/src/WorldPlane.cc View File

@@ -2,6 +2,29 @@

namespace Swan {

void WorldPlane::setTile(int x, int y, Tile::TileID tile) {
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;
}
}

if (chunk == NULL) {
chunks_.push_back(Chunk());
chunk = &chunks_.back();
chunk->clear();
}

chunk->setTile(rx, ry, tile);
}

void WorldPlane::draw(Win &win) {
}


+ 7
- 10
src/main.cc View File

@@ -15,12 +15,6 @@ static double getTime() {
return (double)ts.tv_sec + (double)ts.tv_nsec / 1000000000.0;
}

template<typename T>
static void draw_ents(std::vector<T> ents) {
for (auto &ent: ents)
ent.draw();
}

int main() {
sf::RenderWindow window(sf::VideoMode(800, 600), "good gaem");
window.setVerticalSyncEnabled(true);
@@ -30,12 +24,15 @@ int main() {
Win win = { window, transform };

Game game;
game.world_ = new World();
game.loadMod("core.mod");

game.createWorld();
game.world_->setCurrentPlane(game.world_->addPlane());
game.world_->player_ = new Player(Vec2(1, 1));
game.world_->current_plane_ = new WorldPlane();
game.world_->planes_.push_back(game.world_->current_plane_);

game.loadMod("core.mod/core.mod.so");
Tile::TileID tStone = game.world_->getTileID("core::stone");
WorldPlane &plane = game.world_->getPlane(game.world_->current_plane_);
plane.setTile(2, 2, tStone);

double prevtime = getTime();
double fpsAcc = 0;

Loading…
Cancel
Save