aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/facade
diff options
context:
space:
mode:
authorWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-11-18 15:34:22 +0100
committerWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-11-18 15:34:22 +0100
commitb92da3c729c8fc7c002cb3acbb75c56cb63bd89e (patch)
tree454b64d31b7e0c9151da88a0d49ee782565f49e9 /src/crepe/facade
parent8b449e764b5c91cd3cb0c4fa404a290ab295a7ef (diff)
parent121b64b1cb6cfead5814070c8b0185d3d7308095 (diff)
Merge branch 'master' of https://github.com/lonkaars/crepe into wouter/events
Diffstat (limited to 'src/crepe/facade')
-rw-r--r--src/crepe/facade/DB.cpp29
-rw-r--r--src/crepe/facade/DB.h7
-rw-r--r--src/crepe/facade/SDLContext.cpp7
-rw-r--r--src/crepe/facade/Sound.cpp9
-rw-r--r--src/crepe/facade/Sound.h6
-rw-r--r--src/crepe/facade/SoundContext.cpp2
-rw-r--r--src/crepe/facade/SoundContext.h11
7 files changed, 42 insertions, 29 deletions
diff --git a/src/crepe/facade/DB.cpp b/src/crepe/facade/DB.cpp
index bf877b5..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 5f8d939..629b0eb 100644
--- a/src/crepe/facade/DB.h
+++ b/src/crepe/facade/DB.h
@@ -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 e72b622..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;
@@ -160,7 +159,7 @@ SDLContext::texture_from_path(const std::string & path) {
= 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;
diff --git a/src/crepe/facade/Sound.cpp b/src/crepe/facade/Sound.cpp
index a1aef17..7aa89a9 100644
--- a/src/crepe/facade/Sound.cpp
+++ b/src/crepe/facade/Sound.cpp
@@ -1,21 +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 adf1fef..32b6478 100644
--- a/src/crepe/facade/Sound.h
+++ b/src/crepe/facade/Sound.h
@@ -8,6 +8,12 @@
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:
/**
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;
};