src/Animation.cc | src/Animation.cc | ||||
src/Body.cc | src/Body.cc | ||||
src/Chunk.cc | src/Chunk.cc | ||||
src/Clock.cc | |||||
src/Game.cc | src/Game.cc | ||||
src/Item.cc | src/Item.cc | ||||
src/Mod.cc | src/Mod.cc | ||||
src/Resource.cc | src/Resource.cc | ||||
src/SRF.cc | src/SRF.cc | ||||
src/Tile.cc | src/Tile.cc | ||||
src/Timer.cc | |||||
src/World.cc | src/World.cc | ||||
src/WorldPlane.cc) | src/WorldPlane.cc) | ||||
target_include_directories(libswan | target_include_directories(libswan | ||||
test/Animation.t.cc | test/Animation.t.cc | ||||
test/Body.t.cc | test/Body.t.cc | ||||
test/BoundingBox.t.cc | test/BoundingBox.t.cc | ||||
test/Clock.t.cc | |||||
test/common.t.cc | test/common.t.cc | ||||
test/Entity.t.cc | test/Entity.t.cc | ||||
test/Game.t.cc | test/Game.t.cc | ||||
test/SRF.t.cc | test/SRF.t.cc | ||||
test/swan.t.cc | test/swan.t.cc | ||||
test/Tile.t.cc | test/Tile.t.cc | ||||
test/Timer.t.cc | |||||
test/util.t.cc | test/util.t.cc | ||||
test/log.t.cc | test/log.t.cc | ||||
test/Vector2.t.cc | test/Vector2.t.cc |
#include "common.h" | #include "common.h" | ||||
#include "Resource.h" | #include "Resource.h" | ||||
#include "Timer.h" | |||||
#include "Clock.h" | |||||
#include "Resource.h" | #include "Resource.h" | ||||
namespace Swan { | namespace Swan { |
#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(); | |||||
}; | |||||
} |
ImageResource &getImage(const std::string &name) const; | ImageResource &getImage(const std::string &name) const; | ||||
void addImage(std::unique_ptr<ImageResource> img) { images_[img->name_] = std::move(img); } | void addImage(std::unique_ptr<ImageResource> img) { images_[img->name_] = std::move(img); } | ||||
std::unique_ptr<ImageResource> invalid_image_; | |||||
private: | private: | ||||
std::unordered_map<std::string, std::unique_ptr<ImageResource>> images_; | std::unordered_map<std::string, std::unique_ptr<ImageResource>> images_; | ||||
}; | }; |
#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; | |||||
}; | |||||
} |
class Win { | class Win { | ||||
public: | 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) { | void setPos(const Vec2 &pos) { | ||||
//transform_ = sf::Transform() | //transform_ = sf::Transform() | ||||
float scale_ = 2; | float scale_ = 2; | ||||
Vec2 cam_; | Vec2 cam_; | ||||
SDL_Renderer *renderer_; | SDL_Renderer *renderer_; | ||||
SDL_RendererInfo rinfo_; | |||||
}; | }; | ||||
} | } |
#include <swan/Body.h> | #include <swan/Body.h> | ||||
#include <swan/BoundingBox.h> | #include <swan/BoundingBox.h> | ||||
#include <swan/Chunk.h> | #include <swan/Chunk.h> | ||||
#include <swan/Clock.h> | |||||
#include <swan/Entity.h> | #include <swan/Entity.h> | ||||
#include <swan/Game.h> | #include <swan/Game.h> | ||||
#include <swan/Item.h> | #include <swan/Item.h> | ||||
#include <swan/Resource.h> | #include <swan/Resource.h> | ||||
#include <swan/SRF.h> | #include <swan/SRF.h> | ||||
#include <swan/Tile.h> | #include <swan/Tile.h> | ||||
#include <swan/Timer.h> | |||||
#include <swan/Vector2.h> | #include <swan/Vector2.h> | ||||
#include <swan/Win.h> | #include <swan/Win.h> | ||||
#include <swan/World.h> | #include <swan/World.h> |
#include <optional> | #include <optional> | ||||
#include <functional> | #include <functional> | ||||
#include <memory> | #include <memory> | ||||
#include <chrono> | |||||
namespace Swan { | namespace Swan { | ||||
#include <stdint.h> | #include <stdint.h> | ||||
#include "log.h" | #include "log.h" | ||||
#include "Clock.h" | |||||
#include "gfxutil.h" | #include "gfxutil.h" | ||||
#include "World.h" | #include "World.h" | ||||
#include "Game.h" | #include "Game.h" | ||||
auto &srcsurf = tile->image_.surface_; | auto &srcsurf = tile->image_.surface_; | ||||
SDL_Rect srcrect{ 0, 0, srcsurf->w, srcsurf->h }; | 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; | destsurf->pixels = pixels + (y * TILE_SIZE * pitch) + x * TILE_SIZE * 4; | ||||
SDL_Rect destrect{ 0, 0, TILE_SIZE, TILE_SIZE }; | SDL_Rect destrect{ 0, 0, TILE_SIZE, TILE_SIZE }; | ||||
#include "Timer.h" | |||||
#include "Clock.h" | |||||
#include <time.h> | #include <time.h> | ||||
namespace Swan { | namespace Swan { | ||||
bool Timer::periodic(float secs) { | |||||
bool Clock::periodic(float secs) { | |||||
if (time_ >= secs) { | if (time_ >= secs) { | ||||
time_ = 0; | time_ = 0; | ||||
return true; | return true; |
} | } | ||||
Iter<std::unique_ptr<ImageResource>> Mod::buildImages(SDL_Renderer *renderer) { | 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); | return std::make_unique<ImageResource>(renderer, builder); | ||||
}); | }); | ||||
} | } |
namespace Swan { | namespace Swan { | ||||
ImageResource::ImageResource(SDL_Renderer *renderer, const Builder &builder) { | 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())); | 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(); | warn << "Loading image " << builder.name << " failed: " << SDL_GetError(); | ||||
surface_.reset(SDL_CreateRGBSurface( | 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, | SDL_FillRect(surface_.get(), NULL, SDL_MapRGB(surface_->format, | ||||
PLACEHOLDER_RED, PLACEHOLDER_GREEN, PLACEHOLDER_BLUE)); | PLACEHOLDER_RED, PLACEHOLDER_GREEN, PLACEHOLDER_BLUE)); | ||||
texture_.reset(SDL_CreateTexture( | texture_.reset(SDL_CreateTexture( | ||||
renderer, surface_->format->format, SDL_TEXTUREACCESS_STATIC, | renderer, surface_->format->format, SDL_TEXTUREACCESS_STATIC, | ||||
surface_->w, frame_height_)); | surface_->w, frame_height_)); | ||||
if (!texture_) { | |||||
panic << "CreateTexture failed: " << SDL_GetError(); | |||||
abort(); | |||||
} | |||||
num_frames_ = surface_->h / frame_height_; | num_frames_ = surface_->h / frame_height_; | ||||
name_ = builder.name; | name_ = builder.name; | ||||
} | } | ||||
ResourceManager::ResourceManager(Win &win) { | ResourceManager::ResourceManager(Win &win) { | ||||
invalid_image_ = ImageResource::createInvalid(win); | |||||
addImage(std::move(ImageResource::createInvalid(win))); | |||||
} | } | ||||
void ResourceManager::tick(float dt) { | void ResourceManager::tick(float dt) { | ||||
auto it = images_.find(name); | auto it = images_.find(name); | ||||
if (it == end(images_)) { | if (it == end(images_)) { | ||||
warn << "Couldn't find image " << name << "!"; | warn << "Couldn't find image " << name << "!"; | ||||
return *invalid_image_; | |||||
return getImage("@internal::invalid"); | |||||
} | } | ||||
return *it->second; | return *it->second; | ||||
} | } |
#include "log.h" | #include "log.h" | ||||
#include "World.h" | #include "World.h" | ||||
#include "Game.h" | #include "Game.h" | ||||
#include "Timer.h" | |||||
#include "Clock.h" | |||||
#include "Win.h" | #include "Win.h" | ||||
namespace Swan { | namespace Swan { |
#include "Timer.h" | |||||
#include "Clock.h" | |||||
#include "lib/test.h" | #include "lib/test.h" |