aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/facade
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/facade')
-rw-r--r--src/crepe/facade/DB.cpp29
-rw-r--r--src/crepe/facade/DB.h11
-rw-r--r--src/crepe/facade/SDLContext.cpp70
-rw-r--r--src/crepe/facade/SDLContext.h20
-rw-r--r--src/crepe/facade/Sound.cpp11
-rw-r--r--src/crepe/facade/Sound.h27
-rw-r--r--src/crepe/facade/SoundContext.cpp2
-rw-r--r--src/crepe/facade/SoundContext.h11
8 files changed, 86 insertions, 95 deletions
diff --git a/src/crepe/facade/DB.cpp b/src/crepe/facade/DB.cpp
index 0a2f455..d5d19dc 100644
--- a/src/crepe/facade/DB.cpp
+++ b/src/crepe/facade/DB.cpp
@@ -1,7 +1,6 @@
#include <cstring>
-#include "Exception.h"
-#include "util/log.h"
+#include "util/Log.h"
#include "DB.h"
@@ -15,19 +14,18 @@ DB::DB(const string & path) {
// init database struct
libdb::DB * db;
if ((ret = libdb::db_create(&db, NULL, 0)) != 0)
- throw Exception("db_create: %s", libdb::db_strerror(ret));
+ throw runtime_error(format("db_create: {}", libdb::db_strerror(ret)));
this->db = {db, [](libdb::DB * db) { db->close(db, 0); }};
// load or create database file
- if ((ret = this->db->open(this->db.get(), NULL, path.c_str(), NULL,
- libdb::DB_BTREE, DB_CREATE, 0))
- != 0)
- throw Exception("db->open: %s", libdb::db_strerror(ret));
+ ret = this->db->open(this->db.get(), NULL, path.c_str(), NULL, libdb::DB_BTREE, DB_CREATE,
+ 0);
+ if (ret != 0) throw runtime_error(format("db->open: {}", libdb::db_strerror(ret)));
// create cursor
libdb::DBC * cursor;
- if ((ret = this->db->cursor(this->db.get(), NULL, &cursor, 0)) != 0)
- throw Exception("db->cursor: %s", libdb::db_strerror(ret));
+ ret = this->db->cursor(this->db.get(), NULL, &cursor, 0);
+ if (ret != 0) throw runtime_error(format("db->cursor: {}", libdb::db_strerror(ret)));
this->cursor = {cursor, [](libdb::DBC * cursor) { cursor->close(cursor); }};
}
@@ -45,21 +43,24 @@ string DB::get(const string & key) {
memset(&db_val, 0, sizeof(libdb::DBT));
int ret = this->cursor->get(this->cursor.get(), &db_key, &db_val, DB_FIRST);
- if (ret != 0) throw Exception("cursor->get: %s", libdb::db_strerror(ret));
- return {static_cast<char *>(db_val.data), db_val.size};
+ if (ret == 0) return {static_cast<char *>(db_val.data), db_val.size};
+
+ string err = format("cursor->get: {}", libdb::db_strerror(ret));
+ if (ret == DB_NOTFOUND) throw out_of_range(err);
+ else throw runtime_error(err);
}
void DB::set(const string & key, const string & value) {
libdb::DBT db_key = this->to_thing(key);
libdb::DBT db_val = this->to_thing(value);
int ret = this->db->put(this->db.get(), NULL, &db_key, &db_val, 0);
- if (ret != 0) throw Exception("cursor->get: %s", libdb::db_strerror(ret));
+ if (ret != 0) throw runtime_error(format("cursor->get: {}", libdb::db_strerror(ret)));
}
-bool DB::has(const std::string & key) noexcept {
+bool DB::has(const std::string & key) {
try {
this->get(key);
- } catch (...) {
+ } catch (std::out_of_range &) {
return false;
}
return true;
diff --git a/src/crepe/facade/DB.h b/src/crepe/facade/DB.h
index 7c757a2..629b0eb 100644
--- a/src/crepe/facade/DB.h
+++ b/src/crepe/facade/DB.h
@@ -15,8 +15,8 @@ namespace crepe {
/**
* \brief Berkeley DB facade
*
- * Berkeley DB is a simple key-value database that stores arbitrary data as
- * both key and value. This facade uses STL strings as keys/values.
+ * Berkeley DB is a simple key-value database that stores arbitrary data as both key and value.
+ * This facade uses STL strings as keys/values.
*/
class DB {
public:
@@ -34,7 +34,8 @@ public:
*
* \return The value
*
- * \throws Exception if value is not found in DB or other error occurs
+ * \throws std::out_of_range if value is not found in DB
+ * \throws std::runtime_error if other error occurs
*/
std::string get(const std::string & key);
/**
@@ -43,7 +44,7 @@ public:
* \param key The value key
* \param value The value to store
*
- * \throws Exception if an error occurs
+ * \throws std::runtime_error if an error occurs
*/
void set(const std::string & key, const std::string & value);
/**
@@ -53,7 +54,7 @@ public:
*
* \returns True if the key exists, or false if it does not
*/
- bool has(const std::string & key) noexcept;
+ bool has(const std::string & key);
private:
//! RAII wrapper around \c DB struct
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
index 236bf8c..83e91f8 100644
--- a/src/crepe/facade/SDLContext.cpp
+++ b/src/crepe/facade/SDLContext.cpp
@@ -10,17 +10,16 @@
#include <iostream>
#include <memory>
#include <string>
-#include <utility>
#include "../api/Sprite.h"
#include "../api/Texture.h"
#include "../api/Transform.h"
-#include "../util/log.h"
-#include "Exception.h"
+#include "../util/Log.h"
#include "SDLContext.h"
using namespace crepe;
+using namespace std;
SDLContext & SDLContext::get_instance() {
static SDLContext instance;
@@ -33,41 +32,37 @@ SDLContext::SDLContext() {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
// FIXME: throw exception
- std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError()
- << std::endl;
+ std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() << std::endl;
return;
}
- SDL_Window * tmp_window = SDL_CreateWindow(
- "Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
- this->viewport.w, this->viewport.h, 0);
+ SDL_Window * tmp_window
+ = SDL_CreateWindow("Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
+ this->viewport.w, this->viewport.h, 0);
if (!tmp_window) {
// FIXME: throw exception
- std::cerr << "Window could not be created! SDL_Error: "
- << SDL_GetError() << std::endl;
+ std::cerr << "Window could not be created! SDL_Error: " << SDL_GetError() << std::endl;
return;
}
- this->game_window
- = {tmp_window, [](SDL_Window * window) { SDL_DestroyWindow(window); }};
+ this->game_window = {tmp_window, [](SDL_Window * window) { SDL_DestroyWindow(window); }};
- SDL_Renderer * tmp_renderer = SDL_CreateRenderer(
- this->game_window.get(), -1, SDL_RENDERER_ACCELERATED);
+ SDL_Renderer * tmp_renderer
+ = SDL_CreateRenderer(this->game_window.get(), -1, SDL_RENDERER_ACCELERATED);
if (!tmp_renderer) {
// FIXME: throw exception
- std::cerr << "Renderer could not be created! SDL_Error: "
- << SDL_GetError() << std::endl;
+ std::cerr << "Renderer could not be created! SDL_Error: " << SDL_GetError()
+ << std::endl;
SDL_DestroyWindow(this->game_window.get());
return;
}
- this->game_renderer = {tmp_renderer, [](SDL_Renderer * renderer) {
- SDL_DestroyRenderer(renderer);
- }};
+ this->game_renderer
+ = {tmp_renderer, [](SDL_Renderer * renderer) { SDL_DestroyRenderer(renderer); }};
int img_flags = IMG_INIT_PNG;
if (!(IMG_Init(img_flags) & img_flags)) {
// FIXME: throw exception
- std::cout << "SDL_image could not initialize! SDL_image Error: "
- << IMG_GetError() << std::endl;
+ std::cout << "SDL_image could not initialize! SDL_image Error: " << IMG_GetError()
+ << std::endl;
}
}
@@ -106,12 +101,9 @@ void SDLContext::handle_events(bool & running) {
}
void SDLContext::clear_screen() { SDL_RenderClear(this->game_renderer.get()); }
-void SDLContext::present_screen() {
- SDL_RenderPresent(this->game_renderer.get());
-}
+void SDLContext::present_screen() { SDL_RenderPresent(this->game_renderer.get()); }
-void SDLContext::draw(const Sprite & sprite, const Transform & transform,
- const Camera & cam) {
+void SDLContext::draw(const Sprite & sprite, const Transform & transform, const Camera & cam) {
SDL_RendererFlip render_flip
= (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x)
@@ -136,9 +128,7 @@ void SDLContext::draw(const Sprite & sprite, const Transform & transform,
.h = static_cast<int>(adjusted_h),
};
- SDL_RenderCopyEx(this->game_renderer.get(),
- sprite.sprite_image->texture.get(), &srcrect,
-
+ SDL_RenderCopyEx(this->game_renderer.get(), sprite.sprite_image->texture.get(), &srcrect,
&dstrect, transform.rotation, NULL, render_flip);
}
@@ -148,8 +138,8 @@ void SDLContext::camera(const Camera & cam) {
this->viewport.x = static_cast<int>(cam.x) - (SCREEN_WIDTH / 2);
this->viewport.y = static_cast<int>(cam.y) - (SCREEN_HEIGHT / 2);
- SDL_SetRenderDrawColor(this->game_renderer.get(), cam.bg_color.r,
- cam.bg_color.g, cam.bg_color.b, cam.bg_color.a);
+ SDL_SetRenderDrawColor(this->game_renderer.get(), cam.bg_color.r, cam.bg_color.g,
+ cam.bg_color.b, cam.bg_color.a);
}
uint64_t SDLContext::get_ticks() const { return SDL_GetTicks64(); }
@@ -162,22 +152,18 @@ SDLContext::texture_from_path(const std::string & path) {
tmp = IMG_Load("../asset/texture/ERROR.png");
}
- std::unique_ptr<SDL_Surface, std::function<void(SDL_Surface *)>>
- img_surface;
- img_surface
- = {tmp, [](SDL_Surface * surface) { SDL_FreeSurface(surface); }};
+ std::unique_ptr<SDL_Surface, std::function<void(SDL_Surface *)>> img_surface;
+ img_surface = {tmp, [](SDL_Surface * surface) { SDL_FreeSurface(surface); }};
- SDL_Texture * tmp_texture = SDL_CreateTextureFromSurface(
- this->game_renderer.get(), img_surface.get());
+ SDL_Texture * tmp_texture
+ = SDL_CreateTextureFromSurface(this->game_renderer.get(), img_surface.get());
if (tmp_texture == nullptr) {
- throw Exception("Texture cannot be load from %s", path.c_str());
+ throw runtime_error(format("Texture cannot be load from {}", path));
}
- std::unique_ptr<SDL_Texture, std::function<void(SDL_Texture *)>>
- img_texture;
- img_texture = {tmp_texture,
- [](SDL_Texture * texture) { SDL_DestroyTexture(texture); }};
+ std::unique_ptr<SDL_Texture, std::function<void(SDL_Texture *)>> img_texture;
+ img_texture = {tmp_texture, [](SDL_Texture * texture) { SDL_DestroyTexture(texture); }};
return img_texture;
}
diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h
index 536dec5..007092b 100644
--- a/src/crepe/facade/SDLContext.h
+++ b/src/crepe/facade/SDLContext.h
@@ -17,9 +17,8 @@ const int SCREEN_HEIGHT = 480;
namespace crepe {
-// TODO: SDL_Keycode is defined in a header not distributed with crepe, which
-// means this typedef is unusable when crepe is packaged. Wouter will fix this
-// later.
+// TODO: SDL_Keycode is defined in a header not distributed with crepe, which means this
+// typedef is unusable when crepe is packaged. Wouter will fix this later.
typedef SDL_Keycode CREPE_KEYCODES;
class Texture;
@@ -29,8 +28,8 @@ class LoopManager;
* \class SDLContext
* \brief Facade for the SDL library
*
- * SDLContext is a singleton that handles the SDL window and renderer, provides methods
- * for event handling, and rendering to the screen. It is never used directly by the user
+ * SDLContext is a singleton that handles the SDL window and renderer, provides methods for
+ * event handling, and rendering to the screen. It is never used directly by the user
*/
class SDLContext {
@@ -68,9 +67,8 @@ private:
/**
* \brief Pauses the execution for a specified duration.
*
- * This function uses SDL's delay function to halt the program execution
- * for a given number of milliseconds, allowing for frame rate control
- * or other timing-related functionality.
+ * This function uses SDL's delay function to halt the program execution for a given number
+ * of milliseconds, allowing for frame rate control or other timing-related functionality.
*
* \param ms Duration of the delay in milliseconds.
*/
@@ -127,8 +125,7 @@ private:
* \param transform Reference to the Transform for positioning.
* \param camera Reference to the Camera for view adjustments.
*/
- void draw(const Sprite & sprite, const Transform & transform,
- const Camera & camera);
+ void draw(const Sprite & sprite, const Transform & transform, const Camera & camera);
//! Clears the screen, preparing for a new frame.
void clear_screen();
@@ -147,8 +144,7 @@ private:
std::unique_ptr<SDL_Window, std::function<void(SDL_Window *)>> game_window;
//! renderer for the crepe engine
- std::unique_ptr<SDL_Renderer, std::function<void(SDL_Renderer *)>>
- game_renderer;
+ std::unique_ptr<SDL_Renderer, std::function<void(SDL_Renderer *)>> game_renderer;
//! viewport for the camera window
SDL_Rect viewport = {0, 0, 640, 480};
diff --git a/src/crepe/facade/Sound.cpp b/src/crepe/facade/Sound.cpp
index 648ec81..7aa89a9 100644
--- a/src/crepe/facade/Sound.cpp
+++ b/src/crepe/facade/Sound.cpp
@@ -1,23 +1,22 @@
-#include "../util/log.h"
+#include "../util/Log.h"
#include "Sound.h"
#include "SoundContext.h"
using namespace crepe;
+using namespace std;
-Sound::Sound(std::unique_ptr<Asset> res) {
+Sound::Sound(unique_ptr<Asset> res) {
dbg_trace();
this->load(std::move(res));
}
Sound::Sound(const char * src) {
dbg_trace();
- this->load(std::make_unique<Asset>(src));
+ this->load(make_unique<Asset>(src));
}
-void Sound::load(std::unique_ptr<Asset> res) {
- this->sample.load(res->canonical());
-}
+void Sound::load(unique_ptr<Asset> res) { this->sample.load(res->get_canonical().c_str()); }
void Sound::play() {
SoundContext & ctx = SoundContext::get_instance();
diff --git a/src/crepe/facade/Sound.h b/src/crepe/facade/Sound.h
index 183bd7c..32b6478 100644
--- a/src/crepe/facade/Sound.h
+++ b/src/crepe/facade/Sound.h
@@ -8,34 +8,37 @@
namespace crepe {
+/**
+ * \brief Sound resource facade
+ *
+ * This class is a wrapper around a \c SoLoud::Wav instance, which holds a
+ * single sample. It is part of the sound facade.
+ */
class Sound {
public:
/**
* \brief Pause this sample
*
- * Pauses this sound if it is playing, or does nothing if it is already
- * paused. The playhead position is saved, such that calling \c play() after
- * this function makes the sound resume.
+ * Pauses this sound if it is playing, or does nothing if it is already paused. The playhead
+ * position is saved, such that calling \c play() after this function makes the sound resume.
*/
void pause();
/**
* \brief Play this sample
*
- * Resume playback if this sound is paused, or start from the beginning of
- * the sample.
+ * Resume playback if this sound is paused, or start from the beginning of the sample.
*
- * \note This class only saves a reference to the most recent 'voice' of this
- * sound. Calling \c play() while the sound is already playing causes
- * multiple instances of the sample to play simultaniously. The sample
- * started last is the one that is controlled afterwards.
+ * \note This class only saves a reference to the most recent 'voice' of this sound. Calling
+ * \c play() while the sound is already playing causes multiple instances of the sample to
+ * play simultaniously. The sample started last is the one that is controlled afterwards.
*/
void play();
/**
* \brief Reset playhead position
*
- * Resets the playhead position so that calling \c play() after this function
- * makes it play from the start of the sample. If the sound is not paused
- * before calling this function, this function will stop playback.
+ * Resets the playhead position so that calling \c play() after this function makes it play
+ * from the start of the sample. If the sound is not paused before calling this function,
+ * this function will stop playback.
*/
void rewind();
/**
diff --git a/src/crepe/facade/SoundContext.cpp b/src/crepe/facade/SoundContext.cpp
index 5e5a3a9..deb2b62 100644
--- a/src/crepe/facade/SoundContext.cpp
+++ b/src/crepe/facade/SoundContext.cpp
@@ -1,4 +1,4 @@
-#include "../util/log.h"
+#include "../util/Log.h"
#include "SoundContext.h"
diff --git a/src/crepe/facade/SoundContext.h b/src/crepe/facade/SoundContext.h
index d3123d2..d703c16 100644
--- a/src/crepe/facade/SoundContext.h
+++ b/src/crepe/facade/SoundContext.h
@@ -6,19 +6,24 @@
namespace crepe {
+/**
+ * \brief Sound engine facade
+ *
+ * This class is a wrapper around a \c SoLoud::Soloud instance, which provides
+ * the methods for playing \c Sound instances. It is part of the sound facade.
+ */
class SoundContext {
private:
+ // singleton
SoundContext();
virtual ~SoundContext();
-
- // singleton
- static SoundContext & get_instance();
SoundContext(const SoundContext &) = delete;
SoundContext(SoundContext &&) = delete;
SoundContext & operator=(const SoundContext &) = delete;
SoundContext & operator=(SoundContext &&) = delete;
private:
+ static SoundContext & get_instance();
SoLoud::Soloud engine;
friend class Sound;
};