Browse Source

pre-convert pixel formats, other stuff

opengl-renderer-broken
Martin Dørum 4 years ago
parent
commit
432061954a

+ 2
- 2
libswan/CMakeLists.txt View File

@@ -2,6 +2,7 @@ add_library(libswan SHARED
src/Animation.cc
src/Body.cc
src/Chunk.cc
src/Clock.cc
src/Game.cc
src/Item.cc
src/Mod.cc
@@ -9,7 +10,6 @@ add_library(libswan SHARED
src/Resource.cc
src/SRF.cc
src/Tile.cc
src/Timer.cc
src/World.cc
src/WorldPlane.cc)
target_include_directories(libswan
@@ -25,6 +25,7 @@ add_executable(test_libswan EXCLUDE_FROM_ALL
test/Animation.t.cc
test/Body.t.cc
test/BoundingBox.t.cc
test/Clock.t.cc
test/common.t.cc
test/Entity.t.cc
test/Game.t.cc
@@ -35,7 +36,6 @@ add_executable(test_libswan EXCLUDE_FROM_ALL
test/SRF.t.cc
test/swan.t.cc
test/Tile.t.cc
test/Timer.t.cc
test/util.t.cc
test/log.t.cc
test/Vector2.t.cc

+ 1
- 1
libswan/include/swan/Animation.h View File

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

#include "common.h"
#include "Resource.h"
#include "Timer.h"
#include "Clock.h"
#include "Resource.h"

namespace Swan {

+ 41
- 0
libswan/include/swan/Clock.h View File

@@ -0,0 +1,41 @@
#pragma once

#include <string>
#include <chrono>
#include <ostream>

namespace Swan {

class Clock {
public:
void tick(float dt) { time_ += dt; }
void reset() { time_ = 0; }
float duration() { return time_; }
bool periodic(float secs);

private:
float time_ = 0;
};

class RTClock {
public:
void reset() {
start_ = std::chrono::steady_clock::now();
}

double duration() const {
return std::chrono::duration<double>(
std::chrono::steady_clock::now() - start_).count();
}

friend std::ostream &operator<<(std::ostream &os, const RTClock &clock) {
os << (double)clock.duration() << 's';
return os;
}

private:
std::chrono::time_point<std::chrono::steady_clock> start_ =
std::chrono::steady_clock::now();
};

}

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

@@ -49,8 +49,6 @@ public:
ImageResource &getImage(const std::string &name) const;
void addImage(std::unique_ptr<ImageResource> img) { images_[img->name_] = std::move(img); }

std::unique_ptr<ImageResource> invalid_image_;

private:
std::unordered_map<std::string, std::unique_ptr<ImageResource>> images_;
};

+ 0
- 17
libswan/include/swan/Timer.h View File

@@ -1,17 +0,0 @@
#pragma once

#include <string>

namespace Swan {

class Timer {
public:
void tick(float dt) { time_ += dt; }
void reset() { time_ = 0; }
bool periodic(float secs);

private:
float time_ = 0;
};

}

+ 9
- 1
libswan/include/swan/Win.h View File

@@ -9,7 +9,14 @@ namespace Swan {

class Win {
public:
Win(SDL_Renderer *renderer): renderer_(renderer) {}
Win(SDL_Renderer *renderer): renderer_(renderer) {
if (SDL_GetRendererInfo(renderer_, &rinfo_) < 0) {
panic << "GetRenedrerInfo failed: " << SDL_GetError();
abort();
}

info << "Using renderer: " << rinfo_.name;
}

void setPos(const Vec2 &pos) {
//transform_ = sf::Transform()
@@ -38,6 +45,7 @@ public:
float scale_ = 2;
Vec2 cam_;
SDL_Renderer *renderer_;
SDL_RendererInfo rinfo_;
};

}

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

@@ -4,6 +4,7 @@
#include <swan/Body.h>
#include <swan/BoundingBox.h>
#include <swan/Chunk.h>
#include <swan/Clock.h>
#include <swan/Entity.h>
#include <swan/Game.h>
#include <swan/Item.h>
@@ -12,7 +13,6 @@
#include <swan/Resource.h>
#include <swan/SRF.h>
#include <swan/Tile.h>
#include <swan/Timer.h>
#include <swan/Vector2.h>
#include <swan/Win.h>
#include <swan/World.h>

+ 1
- 0
libswan/include/swan/util.h View File

@@ -3,6 +3,7 @@
#include <optional>
#include <functional>
#include <memory>
#include <chrono>

namespace Swan {


+ 2
- 3
libswan/src/Chunk.cc View File

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

#include "log.h"
#include "Clock.h"
#include "gfxutil.h"
#include "World.h"
#include "Game.h"
@@ -150,9 +151,7 @@ void Chunk::render(const Context &ctx) {
auto &srcsurf = tile->image_.surface_;
SDL_Rect srcrect{ 0, 0, srcsurf->w, srcsurf->h };

/*
SDL_Rect destrect{ x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE };
*/
//SDL_Rect destrect{ x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE };
destsurf->pixels = pixels + (y * TILE_SIZE * pitch) + x * TILE_SIZE * 4;
SDL_Rect destrect{ 0, 0, TILE_SIZE, TILE_SIZE };


libswan/src/Timer.cc → libswan/src/Clock.cc View File

@@ -1,10 +1,10 @@
#include "Timer.h"
#include "Clock.h"

#include <time.h>

namespace Swan {

bool Timer::periodic(float secs) {
bool Clock::periodic(float secs) {
if (time_ >= secs) {
time_ = 0;
return true;

+ 1
- 1
libswan/src/Mod.cc View File

@@ -47,7 +47,7 @@ void Mod::registerEntity(const std::string &name, std::unique_ptr<Entity::Factor
}

Iter<std::unique_ptr<ImageResource>> Mod::buildImages(SDL_Renderer *renderer) {
return map(begin(images_), end(images_), [&](const ImageResource::Builder &builder) {
return map(begin(images_), end(images_), [=](const ImageResource::Builder &builder) {
return std::make_unique<ImageResource>(renderer, builder);
});
}

+ 34
- 4
libswan/src/Resource.cc View File

@@ -11,12 +11,38 @@
namespace Swan {

ImageResource::ImageResource(SDL_Renderer *renderer, const Builder &builder) {
SDL_RendererInfo rinfo;
if (SDL_GetRendererInfo(renderer, &rinfo) < 0) {
panic << "GetRendererInfo failed: " << SDL_GetError();
abort();
}

uint32_t format = rinfo.texture_formats[0];
int bpp = 32;
uint32_t rmask, gmask, bmask, amask;
if (SDL_PixelFormatEnumToMasks(format, &bpp, &rmask, &gmask, &bmask, &amask) < 0) {
panic << "PixelFormatEnumToMasks failed: " << SDL_GetError();
abort();
}

surface_.reset(IMG_Load((builder.modpath + "/assets/" + builder.path).c_str()));
if (surface_ == nullptr) {

// If we have a surface, and it's the wrong pixel format, convert it
if (surface_ && surface_->format->format != format) {
info
<< builder.name << ": Converting from "
<< SDL_GetPixelFormatName(surface_->format->format) << " to "
<< SDL_GetPixelFormatName(format);
surface_.reset(SDL_ConvertSurfaceFormat(surface_.get(), format, 0));
}

// If we don't have a surface yet (either loading or conversion failed),
// create a placeholder
if (!surface_) {
warn << "Loading image " << builder.name << " failed: " << SDL_GetError();

surface_.reset(SDL_CreateRGBSurface(
0, TILE_SIZE, TILE_SIZE, 32, 0, 0, 0, 0));
0, TILE_SIZE, TILE_SIZE, bpp, rmask, gmask, bmask, amask));

SDL_FillRect(surface_.get(), NULL, SDL_MapRGB(surface_->format,
PLACEHOLDER_RED, PLACEHOLDER_GREEN, PLACEHOLDER_BLUE));
@@ -29,6 +55,10 @@ ImageResource::ImageResource(SDL_Renderer *renderer, const Builder &builder) {
texture_.reset(SDL_CreateTexture(
renderer, surface_->format->format, SDL_TEXTUREACCESS_STATIC,
surface_->w, frame_height_));
if (!texture_) {
panic << "CreateTexture failed: " << SDL_GetError();
abort();
}

num_frames_ = surface_->h / frame_height_;
name_ = builder.name;
@@ -67,7 +97,7 @@ std::unique_ptr<ImageResource> ImageResource::createInvalid(Win &win) {
}

ResourceManager::ResourceManager(Win &win) {
invalid_image_ = ImageResource::createInvalid(win);
addImage(std::move(ImageResource::createInvalid(win)));
}

void ResourceManager::tick(float dt) {
@@ -80,7 +110,7 @@ ImageResource &ResourceManager::getImage(const std::string &name) const {
auto it = images_.find(name);
if (it == end(images_)) {
warn << "Couldn't find image " << name << "!";
return *invalid_image_;
return getImage("@internal::invalid");
}
return *it->second;
}

+ 1
- 1
libswan/src/WorldPlane.cc View File

@@ -6,7 +6,7 @@
#include "log.h"
#include "World.h"
#include "Game.h"
#include "Timer.h"
#include "Clock.h"
#include "Win.h"

namespace Swan {

libswan/test/Timer.t.cc → libswan/test/Clock.t.cc View File

@@ -1,3 +1,3 @@
#include "Timer.h"
#include "Clock.h"

#include "lib/test.h"

Loading…
Cancel
Save