Browse Source

replace fprintf with log.h stuff

opengl-renderer-broken
Martin Dørum 4 years ago
parent
commit
2a016c2c5f

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

@@ -36,9 +36,8 @@ private:
std::string name_;
};

static Logger log(std::clog, "log");
static Logger info(std::clog, "info");
static Logger warning(std::clog, "warning");
static Logger warn(std::clog, "warning");
static Logger panic(std::clog, "panic");

}

+ 14
- 8
libswan/src/Chunk.cc View File

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

#include <zlib.h>

#include "log.h"
#include "World.h"
#include "Game.h"
#include "Win.h"
@@ -51,13 +52,16 @@ void Chunk::compress() {
visuals_.reset();
compressed_size_ = destlen;

fprintf(stderr, "Compressed chunk %i,%i from %lu bytes to %lu bytes.\n",
pos_.x, pos_.y, CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID), destlen);
info
<< "Compressed chunk " << pos_.x << "," << pos_.y << " "
<< "from " << CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID) << "bytes "
<< "to " << destlen << " bytes.";
} else if (ret == Z_BUF_ERROR) {
fprintf(stderr, "Didn't compress chunk %i,%i because compressing it would've made it bigger.\n",
pos_.x, pos_.y);
info
<< "Didn't compress chunk " << pos_.x << "," << pos_.y << " "
<< "because compressing it would've made it bigger.";
} else {
fprintf(stderr, "Chunk compression error: %i (Out of memory?)\n", ret);
warn << "Chunk compression error: " << ret << " (Out of memory?)";
}
}

@@ -72,7 +76,7 @@ void Chunk::decompress() {
(Bytef *)data_.get(), compressed_size_);

if (ret != Z_OK) {
fprintf(stderr, "Decompressing chunk failed: %i\n", ret);
panic << "Decompressing chunk failed: " << ret;
abort();
}

@@ -84,8 +88,10 @@ void Chunk::decompress() {
visuals_->dirty_ = true;
need_render_ = true;

fprintf(stderr, "Decompressed chunk %i,%i from %li bytes to %lu bytes.\n",
pos_.x, pos_.y, compressed_size_, CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID));
info
<< "Decompressed chunk " << pos_.x << "," << pos_.y << " from "
<< compressed_size_ << " bytes to "
<< CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID) << " bytes.";
compressed_size_ = -1;
}


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

@@ -4,6 +4,7 @@
#include <time.h>
#include <memory>

#include "log.h"
#include "Tile.h"
#include "OS.h"
#include "Win.h"
@@ -15,7 +16,7 @@ std::unique_ptr<Mod> Game::loadMod(const std::string &path) {

auto init = dl.get<void (*)(Swan::Mod &)>("mod_init");
if (init == NULL) {
fprintf(stderr, "%s: No 'mod_init' function!\n", path.c_str());
warn << path << ": No 'mod_init' function!";
return NULL;
}


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

@@ -4,6 +4,7 @@
#include <algorithm>

#include "util.h"
#include "log.h"

namespace Swan {

@@ -11,36 +12,36 @@ void Mod::init(const std::string &name) {
name_ = name;
inited_ = true;

fprintf(stderr, "Mod initing: %s\n", name_.c_str());
info << "Mod initing: " << name_;
}

void Mod::registerImage(const std::string &name, const std::string &path, int frame_height) {
images_.push_back(std::make_unique<ImageResource>(
renderer_, name_ + "::" + name, path_ + "/assets/" + path, frame_height));
fprintf(stderr, "Adding image: %s\n", (name_ + "::" + name).c_str());
info << "Adding image: " << name_ << "::" << name;
}

void Mod::registerTile(Tile::Builder tile) {
tile.name = name_ + "::" + tile.name;
tiles_.push_back(tile);
fprintf(stderr, "Adding tile: %s\n", tile.name.c_str());
info << "Adding tile: " << tile.name;
}

void Mod::registerItem(Item::Builder item) {
item.name = name_ + "::" + item.name;
items_.push_back(item);
fprintf(stderr, "Adding item: %s\n", item.name.c_str());
info << "Adding item: " << item.name;
}

void Mod::registerWorldGen(const std::string &name, std::unique_ptr<WorldGen::Factory> gen) {
gen->name_ = name_ + "::" + name;
fprintf(stderr, "Adding world gen: %s\n", gen->name_.c_str());
info << "Adding world gen: " << gen->name_;
worldgens_.push_back(std::move(gen));
}

void Mod::registerEntity(const std::string &name, std::unique_ptr<Entity::Factory> ent) {
ent->name_ = name_ + "::" + name;
fprintf(stderr, "Adding entity: %s\n",ent->name_.c_str());
info << "Adding entity: " << ent->name_;
entities_.push_back(std::move(ent));
}


+ 2
- 1
libswan/src/Resource.cc View File

@@ -3,6 +3,7 @@
#include <stdio.h>
#include <SDL2/SDL_image.h>

#include "log.h"
#include "common.h"
#include "Game.h"
#include "Win.h"
@@ -15,7 +16,7 @@ ImageResource::ImageResource(

surface_.reset(IMG_Load(path.c_str()));
if (surface_ == nullptr) {
fprintf(stderr, "Loading %s failed: %s\n", path.c_str(), SDL_GetError());
warn << "Loading " << path << " failed: " << SDL_GetError();

surface_.reset(SDL_CreateRGBSurface(
0, TILE_SIZE, TILE_SIZE, 32, 0, 0, 0, 0));

+ 5
- 5
libswan/src/World.cc View File

@@ -1,5 +1,6 @@
#include "World.h"

#include "log.h"
#include "Game.h"
#include "Win.h"

@@ -37,7 +38,7 @@ void World::ChunkRenderer::tick(WorldPlane &plane, ChunkPos abspos) {
}

void World::addMod(std::unique_ptr<Mod> mod) {
fprintf(stderr, "World: Adding mod %s\n", mod->name_.c_str());
info << "World: adding mod " << mod->name_;

for (auto t: mod->buildTiles(resources_)) {
Tile::ID id = tiles_.size();
@@ -75,8 +76,7 @@ void World::setCurrentPlane(WorldPlane &plane) {
WorldPlane &World::addPlane(const std::string &gen) {
WorldPlane::ID id = planes_.size();
if (worldgens_.find(gen) == worldgens_.end()) {
fprintf(stderr, "Tried to add plane with non-existant world gen '%s'!\n",
gen.c_str());
panic << "Tried to add plane with non-existant world gen " << gen << "!";
abort();
}

@@ -88,7 +88,7 @@ WorldPlane &World::addPlane(const std::string &gen) {
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());
warn << "Tried to get non-existant item " << name << "!";
return *game_->invalid_item_;
}

@@ -98,7 +98,7 @@ Item &World::getItem(const std::string &name) {
Tile::ID World::getTileID(const std::string &name) {
auto iter = tiles_map_.find(name);
if (iter == tiles_map_.end()) {
fprintf(stderr, "Tried to get non-existant tile ''%s'!\n", name.c_str());
warn << "Tried to get non-existant item " << name << "!";
return Tile::INVALID_ID;
}


+ 5
- 4
libswan/src/WorldPlane.cc View File

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

#include "log.h"
#include "World.h"
#include "Game.h"
#include "Timer.h"
@@ -32,7 +33,7 @@ Context WorldPlane::getContext() {

Entity &WorldPlane::spawnEntity(const std::string &name, const SRF &params) {
if (world_->ents_.find(name) == world_->ents_.end()) {
fprintf(stderr, "Tried to spawn non-existant entity %s!", name.c_str());
panic << "Tried to spawn a non-existant entity " << name << "!";
abort();
}

@@ -42,13 +43,13 @@ Entity &WorldPlane::spawnEntity(const std::string &name, const SRF &params) {
}

spawn_list_.push_back(std::unique_ptr<Entity>(ent));
fprintf(stderr, "Spawned %s. SRF: ", name.c_str());
params.pretty(std::cerr) << '\n';
info << "Spawned " << name << ". SRF:";
params.pretty(std::clog) << '\n';
return *ent;
}

void WorldPlane::despawnEntity(Entity &ent) {
fprintf(stderr, "Despawned entity.\n");
info << "Despawned entity.";
despawn_list_.push_back(&ent);
}


+ 5
- 6
src/main.cc View File

@@ -1,6 +1,5 @@
#include <time.h>
#include <unistd.h>
#include <stdio.h>
#include <vector>
#include <memory>
#include <chrono>
@@ -16,7 +15,7 @@ using namespace Swan;

#define errassert(expr, str, errfn) do { \
if (!(expr)) { \
fprintf(stderr, "%s: %s\n", str, errfn()); \
panic << (str) << ": " << errfn(); \
return EXIT_FAILURE; \
} \
} while (0)
@@ -47,7 +46,7 @@ int main() {
SDL_CreateRenderer(
window.get(), -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
SDL_DestroyRenderer);
sdlassert(renderer, "Could not create renderer\n");
sdlassert(renderer, "Could not create renderer");

Win win(renderer.get());

@@ -101,7 +100,7 @@ int main() {
fpsAcc += dt;
fcount += 1;
if (fpsAcc >= 4) {
fprintf(stderr, "FPS: %.3f\n", fcount / 4.0);
info << "FPS: " << fcount / 4.0;
fpsAcc -= 4;
fcount = 0;
}
@@ -110,12 +109,12 @@ int main() {

if (dt > 0.1) {
if (slowFrames == 0)
fprintf(stderr, "Warning: delta time is too high! (%.3fs).\n", dt);
warn << "Delta time too high! (" << dt << "s)";
slowFrames += 1;
} else {
if (slowFrames > 0) {
if (slowFrames > 1)
fprintf(stderr, "%i consecutive slow frames.\n", slowFrames);
warn << slowFrames << " consecutive slow frames.";
slowFrames = 0;
}


Loading…
Cancel
Save