Преглед на файлове

use INVALID_* rather than null

opengl-renderer-broken
Martin Dørum преди 4 години
родител
ревизия
11406a6680

+ 2
- 0
libswan/CMakeLists.txt Целия файл

@@ -1,8 +1,10 @@
add_library(libswan SHARED
src/Animation.cc
src/Asset.cc
src/Body.cc
src/Chunk.cc
src/Game.cc
src/Item.cc
src/Mod.cc
src/Tile.cc
src/Timer.cc

+ 2
- 2
libswan/include/swan/Animation.h Целия файл

@@ -14,11 +14,11 @@ public:
};

Animation() = default;
Animation(int w, int h, double interval, const Asset *asset, int flags = 0) {
Animation(int w, int h, double interval, const Asset &asset, int flags = 0) {
init(w, h, interval, asset, flags);
}

void init(int w, int h, double interval, const Asset *asset, int flags = 0);
void init(int w, int h, double interval, const Asset &asset, int flags = 0);

void tick(double dt);
void draw(Win &win);

+ 5
- 10
libswan/include/swan/Asset.h Целия файл

@@ -9,20 +9,15 @@ class Asset {
public:
Asset(std::string path): path_(path) {}

bool load(std::string pfx) {
if (!img_.loadFromFile(pfx + "/" + path_))
return false;

auto size = img_.getSize();
tex_.create(size.x, size.y);
tex_.update(img_);
return true;
}
bool load(const std::string &pfx);

std::string name_;
std::string path_;
sf::Image img_;
sf::Image image_;
sf::Texture tex_;

static Asset INVALID_ASSET;
static void initGlobal();
};

}

+ 2
- 2
libswan/include/swan/Chunk.h Целия файл

@@ -20,7 +20,7 @@ public:
}

void setTileID(World &world, RelPos pos, Tile::ID id);
Tile *getTile(World &world, RelPos pos);
Tile &getTile(World &world, RelPos pos);
void redraw(World &world);
void draw(Win &win);

@@ -28,7 +28,7 @@ public:
Tile::ID tiles_[CHUNK_WIDTH][CHUNK_HEIGHT];

private:
void drawBlock(RelPos pos, const Tile *t);
void drawBlock(RelPos pos, const Tile &t);
void drawBlock(World &world, RelPos pos, Tile::ID id);
bool dirty_ = false;
sf::Texture texture_;

+ 2
- 1
libswan/include/swan/Item.h Целия файл

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

class Item {

public:
static Item INVALID_ITEM;
};

}

+ 2
- 1
libswan/include/swan/Tile.h Целия файл

@@ -25,7 +25,8 @@ public:
static sf::Image INVALID_IMAGE;
static Tile INVALID_TILE;
static ID INVALID_ID;
static void initInvalid();
static Tile *createInvalid();
static void initGlobal();
};

}

+ 4
- 4
libswan/include/swan/World.h Целия файл

@@ -26,11 +26,11 @@ public:
void registerEntity(std::shared_ptr<Entity::Factory> ent);
void registerAsset(std::shared_ptr<Asset> asset);

Asset *getAsset(const std::string &name);
Item *getItem(const std::string &name);
Asset &getAsset(const std::string &name);
Item &getItem(const std::string &name);
Tile::ID getTileID(const std::string &name);
Tile *getTileByID(Tile::ID id);
Tile *getTile(const std::string &name);
Tile &getTileByID(Tile::ID id);
Tile &getTile(const std::string &name);

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

+ 3
- 3
libswan/src/Animation.cc Целия файл

@@ -2,12 +2,12 @@

namespace Swan {

void Animation::init(int w, int h, double interval, const Asset *asset, int flags) {
void Animation::init(int w, int h, double interval, const Asset &asset, int flags) {
width_ = w;
height_ = h;
interval_ = interval;
asset_ = asset;
fcount_ = asset_->img_.getSize().y / height_;
asset_ = &asset;
fcount_ = asset_->image_.getSize().y / height_;
sprite_.setTexture(asset_->tex_);
sprite_.setTextureRect(sf::IntRect(0, 0, width_, height_));


+ 26
- 0
libswan/src/Asset.cc Целия файл

@@ -0,0 +1,26 @@
#include "Asset.h"

#include "common.h"

namespace Swan {

Asset Asset::INVALID_ASSET("");

bool Asset::load(const std::string &pfx) {
if (!image_.loadFromFile(pfx + "/" + path_))
return false;

auto size = image_.getSize();
tex_.create(size.x, size.y);
tex_.update(image_);
return true;
}

void Asset::initGlobal() {
INVALID_ASSET.name_ = "@internal::invalid";
INVALID_ASSET.image_.create(TILE_SIZE, TILE_SIZE, sf::Color(245, 66, 242));
INVALID_ASSET.tex_.create(TILE_SIZE, TILE_SIZE);
INVALID_ASSET.tex_.update(INVALID_ASSET.image_);
}

}

+ 3
- 3
libswan/src/Chunk.cc Целия файл

@@ -9,12 +9,12 @@ void Chunk::setTileID(World &world, RelPos pos, Tile::ID id) {
drawBlock(world, pos, id);
}

Tile *Chunk::getTile(World &world, RelPos pos) {
Tile &Chunk::getTile(World &world, RelPos pos) {
return world.getTileByID(tiles_[pos.x_][pos.y_]);
}

void Chunk::drawBlock(RelPos pos, const Tile *t) {
texture_.update(t->image_, pos.x_ * TILE_SIZE, pos.y_ * TILE_SIZE);
void Chunk::drawBlock(RelPos pos, const Tile &t) {
texture_.update(t. image_, pos.x_ * TILE_SIZE, pos.y_ * TILE_SIZE);
dirty_ = true;
}


+ 6
- 1
libswan/src/Game.cc Целия файл

@@ -3,6 +3,7 @@
#include <dlfcn.h>

#include "Tile.h"
#include "Asset.h"

namespace Swan {

@@ -28,6 +29,9 @@ void Game::loadMod(const std::string &path) {
void Game::createWorld(std::string worldgen) {
world_.reset(new World());
for (auto &mod: registered_mods_) {
// Register invalids
world_->registerTile(std::shared_ptr<Tile>(Tile::createInvalid()));

for (auto &tile: mod.tiles_)
world_->registerTile(tile);
for (auto &worldgen: mod.worldgens_)
@@ -56,7 +60,8 @@ void Game::tick() {
}

void Game::initGlobal() {
Tile::initInvalid();
Tile::initGlobal();
Asset::initGlobal();
}

}

+ 7
- 0
libswan/src/Item.cc Целия файл

@@ -0,0 +1,7 @@
#include "Item.h"

namespace Swan {

Item Item::INVALID_ITEM;

}

+ 15
- 5
libswan/src/Tile.cc Целия файл

@@ -5,13 +5,23 @@ namespace Swan {

sf::Image Tile::INVALID_IMAGE;
Tile Tile::INVALID_TILE("");
Tile::ID Tile::INVALID_ID = 0xffff;
Tile::ID Tile::INVALID_ID = 0;

void Tile::initInvalid() {
static void initInvalid(Tile *t) {
t->name_ = "@internal::invalid";
t->image_ = Tile::INVALID_IMAGE;
t->is_solid_ = false;
}

Tile *Tile::createInvalid() {
Tile *t = new Tile("");
initInvalid(t);
return t;
}

void Tile::initGlobal() {
INVALID_IMAGE.create(TILE_SIZE, TILE_SIZE, sf::Color(245, 66, 242));
INVALID_TILE.name_ = "INVALID";
INVALID_TILE.image_ = INVALID_IMAGE;
INVALID_TILE.is_solid_ = false;
initInvalid(&INVALID_TILE);
}

}

+ 9
- 11
libswan/src/World.cc Целия файл

@@ -59,24 +59,24 @@ void World::registerAsset(std::shared_ptr<Asset> asset) {
assets_[asset->name_] = asset;
}

Asset *World::getAsset(const std::string &name) {
Asset &World::getAsset(const std::string &name) {
auto iter = assets_.find(name);
if (iter == assets_.end()) {
fprintf(stderr, "Tried to get non-existant asset ''%s'!\n", name.c_str());
return NULL;
return Asset::INVALID_ASSET;
}

return iter->second.get();
return *iter->second;
}

Item *World::getItem(const std::string &name) {
Item &World::getItem(const std::string &name) {
auto iter = items_.find(name);
if (iter == items_.end()) {
fprintf(stderr, "Tried to get non-existant item ''%s'!\n", name.c_str());
return NULL;
return Item::INVALID_ITEM;
}

return iter->second.get();
return *iter->second;
}

Tile::ID World::getTileID(const std::string &name) {
@@ -89,14 +89,12 @@ Tile::ID World::getTileID(const std::string &name) {
return iter->second;
}

Tile *World::getTileByID(Tile::ID id) {
return tiles_[id].get();
Tile &World::getTileByID(Tile::ID id) {
return *tiles_[id];
}

Tile *World::getTile(const std::string &name) {
Tile &World::getTile(const std::string &name) {
Tile::ID id = getTileID(name);
if (id == Tile::INVALID_ID)
return &Tile::INVALID_TILE;
return getTileByID(id);
}


+ 1
- 1
libswan/src/WorldPlane.cc Целия файл

@@ -55,7 +55,7 @@ void WorldPlane::setTileID(TilePos pos, Tile::ID id) {
}

Tile &WorldPlane::getTile(TilePos pos) {
return *getChunk(chunkPos(pos)).getTile(*world_, relPos(pos));
return getChunk(chunkPos(pos)).getTile(*world_, relPos(pos));
}

Entity &WorldPlane::spawnPlayer() {

Loading…
Отказ
Запис