aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/facade
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-05 16:12:47 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-05 16:12:47 +0100
commite36ea050972fcaaf3d85d672755bad4ebb2dcd80 (patch)
tree5145e0b66650eea1df301106b7d197a586be65f3 /src/crepe/facade
parent333b07775be1ef20fdb5909672c1e4dcabec1b40 (diff)
parentb770475741b7c33d57331f3139c55a3f237ad274 (diff)
merge `master` into `loek/savemgr`loek/savemgr
Diffstat (limited to 'src/crepe/facade')
-rw-r--r--src/crepe/facade/CMakeLists.txt16
-rw-r--r--src/crepe/facade/DB.cpp67
-rw-r--r--src/crepe/facade/DB.h34
-rw-r--r--src/crepe/facade/SDLApp.cpp71
-rw-r--r--src/crepe/facade/SDLApp.h26
-rw-r--r--src/crepe/facade/SDLContext.cpp157
-rw-r--r--src/crepe/facade/SDLContext.h48
-rw-r--r--src/crepe/facade/Sound.cpp60
-rw-r--r--src/crepe/facade/Sound.h81
-rw-r--r--src/crepe/facade/SoundContext.cpp20
-rw-r--r--src/crepe/facade/SoundContext.h26
11 files changed, 606 insertions, 0 deletions
diff --git a/src/crepe/facade/CMakeLists.txt b/src/crepe/facade/CMakeLists.txt
new file mode 100644
index 0000000..bb52e7a
--- /dev/null
+++ b/src/crepe/facade/CMakeLists.txt
@@ -0,0 +1,16 @@
+target_sources(crepe PUBLIC
+ Sound.cpp
+ SoundContext.cpp
+ SDLApp.cpp
+ SDLContext.cpp
+ DB.cpp
+)
+
+target_sources(crepe PUBLIC FILE_SET HEADERS FILES
+ Sound.h
+ SoundContext.h
+ SDLContext.h
+ SDLContext.h
+ DB.h
+)
+
diff --git a/src/crepe/facade/DB.cpp b/src/crepe/facade/DB.cpp
new file mode 100644
index 0000000..bd2f089
--- /dev/null
+++ b/src/crepe/facade/DB.cpp
@@ -0,0 +1,67 @@
+#include <cstring>
+
+#include "util/log.h"
+
+#include "DB.h"
+
+using namespace std;
+using namespace crepe;
+
+DB::DB(const string & path) {
+ dbg_trace();
+ int ret;
+
+ // init database struct
+ libdb::DB * db;
+ if ((ret = libdb::db_create(&db, NULL, 0)) != 0)
+ throw nullptr; // TODO: exception
+ 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 nullptr;
+ }
+
+ // create cursor
+ libdb::DBC * cursor;
+ if ((ret = this->db->cursor(this->db.get(), NULL, &cursor, 0)) != 0) {
+ throw nullptr;
+ }
+ this->cursor = { cursor, [] (libdb::DBC * cursor) { cursor->close(cursor); } };
+}
+
+
+libdb::DBT DB::to_thing(const string & thing) const {
+ libdb::DBT thang;
+ memset(&thang, 0, sizeof(libdb::DBT));
+ thang.data = (void *) thing.data();
+ thang.size = thing.size();
+ return thang;
+}
+
+string DB::get(const string & key) {
+ libdb::DBT db_key = this->to_thing(key);
+ libdb::DBT db_val;
+ 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 nullptr; // TODO: proper exception
+ return { static_cast<char *>(db_val.data), db_val.size };
+}
+
+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 nullptr; // TODO: proper exception
+}
+
+bool DB::has(const std::string & key) noexcept {
+ try {
+ this->get(key);
+ } catch (...) {
+ return false;
+ }
+ return true;
+}
+
diff --git a/src/crepe/facade/DB.h b/src/crepe/facade/DB.h
new file mode 100644
index 0000000..06442ad
--- /dev/null
+++ b/src/crepe/facade/DB.h
@@ -0,0 +1,34 @@
+#pragma once
+
+#include <string>
+#include <functional>
+#include <memory>
+
+namespace libdb {
+extern "C" {
+#include <db.h>
+}
+}
+
+namespace crepe {
+
+class DB {
+public:
+ DB(const std::string & path);
+ virtual ~DB() = default;
+
+public:
+ std::string get(const std::string & key);
+ void set(const std::string & key, const std::string & value);
+ bool has(const std::string & key) noexcept;
+
+private:
+ std::unique_ptr<libdb::DB, std::function<void(libdb::DB *)>> db;
+ std::unique_ptr<libdb::DBC, std::function<void(libdb::DBC *)>> cursor;
+
+private:
+ libdb::DBT to_thing(const std::string & thing) const;
+};
+
+}
+
diff --git a/src/crepe/facade/SDLApp.cpp b/src/crepe/facade/SDLApp.cpp
new file mode 100644
index 0000000..c6ddeaa
--- /dev/null
+++ b/src/crepe/facade/SDLApp.cpp
@@ -0,0 +1,71 @@
+#include <iostream>
+
+#include "SDLApp.h"
+
+SDLApp::SDLApp(int window_width, int window_height)
+ : window_width(window_width), window_height(window_height), window(nullptr),
+ renderer(nullptr) {}
+
+// FIXME: why is there clean_up and ~SDLApp?
+SDLApp::~SDLApp() { clean_up(); }
+
+bool SDLApp::initialize() {
+ if (SDL_Init(SDL_INIT_VIDEO) != 0) {
+ // FIXME: throw exception
+ std::cerr << "SDL Initialization Error: " << SDL_GetError()
+ << std::endl;
+ return false;
+ }
+
+ window = SDL_CreateWindow("Particle System", SDL_WINDOWPOS_CENTERED,
+ SDL_WINDOWPOS_CENTERED, window_width,
+ window_height, SDL_WINDOW_SHOWN);
+ if (!window) {
+ // FIXME: throw exception
+ std::cerr << "Window Creation Error: " << SDL_GetError() << std::endl;
+ return false;
+ }
+
+ renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
+ if (!renderer) {
+ // FIXME: throw exception
+ std::cerr << "Renderer Creation Error: " << SDL_GetError() << std::endl;
+ return false;
+ }
+
+ return true;
+}
+
+void SDLApp::handle_events(bool & running) {
+ SDL_Event event;
+ while (SDL_PollEvent(&event)) {
+ if (event.type == SDL_QUIT) {
+ running = false;
+ }
+ }
+}
+
+void SDLApp::clear_screen() {
+ SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
+ SDL_RenderClear(renderer);
+}
+
+void SDLApp::present_screen() { SDL_RenderPresent(renderer); }
+
+void SDLApp::draw_square(int x, int y, int size) {
+ SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
+ SDL_Rect rect = {x, y, size, size};
+ SDL_RenderFillRect(renderer, &rect);
+}
+
+SDL_Texture * square_texture = nullptr; // Load this with an image or create it
+
+void SDLApp::clean_up() {
+ if (renderer) {
+ SDL_DestroyRenderer(renderer);
+ }
+ if (window) {
+ SDL_DestroyWindow(window);
+ }
+ SDL_Quit();
+}
diff --git a/src/crepe/facade/SDLApp.h b/src/crepe/facade/SDLApp.h
new file mode 100644
index 0000000..6d8f3f4
--- /dev/null
+++ b/src/crepe/facade/SDLApp.h
@@ -0,0 +1,26 @@
+#pragma once
+
+#include <SDL2/SDL.h>
+
+#include "../api/ParticleEmitter.h"
+
+class SDLApp {
+public:
+ SDLApp(int window_width, int window_height);
+ ~SDLApp();
+
+ bool initialize();
+ void handle_events(bool & running);
+ void clear_screen();
+ void present_screen();
+ void draw_square(int x, int y, int size);
+ void clean_up();
+ void draw_particles(const std::vector<crepe::ParticleEmitter> & emitters);
+ void draw_multiple_squares(const std::vector<SDL_Rect> & squares);
+
+private:
+ int window_width;
+ int window_height;
+ SDL_Window * window;
+ SDL_Renderer * renderer;
+};
diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp
new file mode 100644
index 0000000..8da93e9
--- /dev/null
+++ b/src/crepe/facade/SDLContext.cpp
@@ -0,0 +1,157 @@
+#include <SDL2/SDL.h>
+#include <SDL2/SDL_image.h>
+#include <SDL2/SDL_render.h>
+#include <SDL2/SDL_surface.h>
+#include <SDL2/SDL_video.h>
+#include <cmath>
+#include <cstddef>
+#include <iostream>
+
+#include "../api/Sprite.h"
+#include "../api/Texture.h"
+#include "../api/Transform.h"
+#include "../util/log.h"
+
+#include "SDLContext.h"
+
+using namespace crepe;
+
+SDLContext & SDLContext::get_instance() {
+ static SDLContext instance;
+ return instance;
+}
+
+void SDLContext::handle_events(bool & running) {
+ SDL_Event event;
+ while (SDL_PollEvent(&event)) {
+ if (event.type == SDL_QUIT) {
+ running = false;
+ }
+ }
+}
+
+SDLContext::~SDLContext() {
+ dbg_trace();
+
+ if (this->game_renderer != nullptr)
+ SDL_DestroyRenderer(this->game_renderer);
+
+ if (this->game_window != nullptr) {
+ SDL_DestroyWindow(this->game_window);
+ }
+
+ // TODO: how are we going to ensure that these are called from the same
+ // thread that SDL_Init() was called on? This has caused problems for me
+ // before.
+ IMG_Quit();
+ SDL_Quit();
+}
+
+void SDLContext::clear_screen() { SDL_RenderClear(this->game_renderer); }
+
+SDLContext::SDLContext() {
+ dbg_trace();
+ // FIXME: read window defaults from config manager
+
+ if (SDL_Init(SDL_INIT_VIDEO) < 0) {
+ // FIXME: throw exception
+ std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError()
+ << std::endl;
+ return;
+ }
+
+ this->game_window = SDL_CreateWindow(
+ "Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
+ 1920, 1080, SDL_WINDOW_SHOWN);
+ if (!this->game_window) {
+ // FIXME: throw exception
+ std::cerr << "Window could not be created! SDL_Error: "
+ << SDL_GetError() << std::endl;
+ }
+
+ this->game_renderer
+ = SDL_CreateRenderer(this->game_window, -1, SDL_RENDERER_ACCELERATED);
+ if (!this->game_renderer) {
+ // FIXME: throw exception
+ std::cerr << "Renderer could not be created! SDL_Error: "
+ << SDL_GetError() << std::endl;
+ SDL_DestroyWindow(this->game_window);
+ return;
+ }
+
+ 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;
+ }
+}
+
+void SDLContext::present_screen() { SDL_RenderPresent(this->game_renderer); }
+
+void SDLContext::draw(const Sprite & sprite, const Transform & transform) {
+
+ static SDL_RendererFlip render_flip
+ = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flip_x)
+ | (SDL_FLIP_VERTICAL * sprite.flip.flip_y));
+
+ int w, h;
+ SDL_QueryTexture(sprite.sprite_image->texture, NULL, NULL, &w, &h);
+ // needs maybe camera for position
+ SDL_Rect dstrect = {
+ .x = static_cast<int>(transform.position.x),
+ .y = static_cast<int>(transform.position.y),
+ .w = static_cast<int>(w * transform.scale),
+ .h = static_cast<int>(h * transform.scale),
+ };
+
+ double degrees = transform.rotation * 180 / M_PI;
+ SDL_RenderCopyEx(this->game_renderer, sprite.sprite_image->texture, NULL,
+ &dstrect, degrees, NULL, render_flip);
+}
+
+/*
+SDL_Texture * SDLContext::setTextureFromPath(const char * path, SDL_Rect & clip,
+ const int row, const int col) {
+ dbg_trace();
+
+ SDL_Surface * tmp = IMG_Load(path);
+ if (!tmp) {
+ std::cerr << "Error surface " << IMG_GetError << std::endl;
+ }
+
+ clip.
+ w = tmp->w / col;
+ clip.h = tmp->h / row;
+
+ SDL_Texture * CreatedTexture
+ = SDL_CreateTextureFromSurface(this->game_renderer, tmp);
+
+ if (!CreatedTexture) {
+ std::cerr << "Error could not create texture " << IMG_GetError
+ << std::endl;
+ }
+ SDL_FreeSurface(tmp);
+
+ return CreatedTexture;
+}
+*/
+
+SDL_Texture * SDLContext::texture_from_path(const char * path) {
+ dbg_trace();
+
+ SDL_Surface * tmp = IMG_Load(path);
+ if (!tmp) {
+ std::cerr << "Error surface " << IMG_GetError << std::endl;
+ }
+ SDL_Texture * created_texture
+ = SDL_CreateTextureFromSurface(this->game_renderer, tmp);
+
+ if (!created_texture) {
+ std::cerr << "Error could not create texture " << IMG_GetError
+ << std::endl;
+ }
+ SDL_FreeSurface(tmp);
+
+ return created_texture;
+}
diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h
new file mode 100644
index 0000000..f1ba8a6
--- /dev/null
+++ b/src/crepe/facade/SDLContext.h
@@ -0,0 +1,48 @@
+#pragma once
+
+#include <SDL2/SDL_render.h>
+#include <SDL2/SDL_video.h>
+
+#include "../api/Sprite.h"
+#include "../api/Transform.h"
+#include "../system/RenderSystem.h"
+
+namespace crepe {
+
+class Texture;
+class SDLContext {
+
+public:
+ // singleton
+ static SDLContext & get_instance();
+ SDLContext(const SDLContext &) = delete;
+ SDLContext(SDLContext &&) = delete;
+ SDLContext & operator=(const SDLContext &) = delete;
+ SDLContext & operator=(SDLContext &&) = delete;
+
+ //TODO decide events wouter?
+
+private:
+ void handle_events(bool & running);
+
+private:
+ SDLContext();
+ virtual ~SDLContext();
+
+private:
+ friend class Texture;
+ SDL_Texture * texture_from_path(const char *);
+ //SDL_Texture* setTextureFromPath(const char*, SDL_Rect& clip, const int row, const int col);
+
+private:
+ friend class RenderSystem;
+ void draw(const Sprite &, const Transform &);
+ void clear_screen();
+ void present_screen();
+
+private:
+ SDL_Window * game_window = nullptr;
+ SDL_Renderer * game_renderer = nullptr;
+};
+
+} // namespace crepe
diff --git a/src/crepe/facade/Sound.cpp b/src/crepe/facade/Sound.cpp
new file mode 100644
index 0000000..648ec81
--- /dev/null
+++ b/src/crepe/facade/Sound.cpp
@@ -0,0 +1,60 @@
+#include "../util/log.h"
+
+#include "Sound.h"
+#include "SoundContext.h"
+
+using namespace crepe;
+
+Sound::Sound(std::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));
+}
+
+void Sound::load(std::unique_ptr<Asset> res) {
+ this->sample.load(res->canonical());
+}
+
+void Sound::play() {
+ SoundContext & ctx = SoundContext::get_instance();
+ if (ctx.engine.getPause(this->handle)) {
+ // resume if paused
+ ctx.engine.setPause(this->handle, false);
+ } else {
+ // or start new sound
+ this->handle = ctx.engine.play(this->sample, this->volume);
+ ctx.engine.setLooping(this->handle, this->looping);
+ }
+}
+
+void Sound::pause() {
+ SoundContext & ctx = SoundContext::get_instance();
+ if (ctx.engine.getPause(this->handle)) return;
+ ctx.engine.setPause(this->handle, true);
+}
+
+void Sound::rewind() {
+ SoundContext & ctx = SoundContext::get_instance();
+ if (!ctx.engine.isValidVoiceHandle(this->handle)) return;
+ ctx.engine.seek(this->handle, 0);
+}
+
+void Sound::set_volume(float volume) {
+ this->volume = volume;
+
+ SoundContext & ctx = SoundContext::get_instance();
+ if (!ctx.engine.isValidVoiceHandle(this->handle)) return;
+ ctx.engine.setVolume(this->handle, this->volume);
+}
+
+void Sound::set_looping(bool looping) {
+ this->looping = looping;
+
+ SoundContext & ctx = SoundContext::get_instance();
+ if (!ctx.engine.isValidVoiceHandle(this->handle)) return;
+ ctx.engine.setLooping(this->handle, this->looping);
+}
diff --git a/src/crepe/facade/Sound.h b/src/crepe/facade/Sound.h
new file mode 100644
index 0000000..183bd7c
--- /dev/null
+++ b/src/crepe/facade/Sound.h
@@ -0,0 +1,81 @@
+#pragma once
+
+#include <memory>
+#include <soloud/soloud.h>
+#include <soloud/soloud_wav.h>
+
+#include "../Asset.h"
+
+namespace crepe {
+
+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.
+ */
+ void pause();
+ /**
+ * \brief Play this 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.
+ */
+ 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.
+ */
+ void rewind();
+ /**
+ * \brief Set playback volume / gain
+ *
+ * \param volume Volume (0 = muted, 1 = full volume)
+ */
+ void set_volume(float volume);
+ /**
+ * \brief Get playback volume / gain
+ *
+ * \return Volume
+ */
+ float get_volume() const { return this->volume; }
+ /**
+ * \brief Set looping behavior for this sample
+ *
+ * \param looping Looping behavior (false = one-shot, true = loop)
+ */
+ void set_looping(bool looping);
+ /**
+ * \brief Get looping behavior
+ *
+ * \return true if looping, false if one-shot
+ */
+ bool get_looping() const { return this->looping; }
+
+public:
+ Sound(const char * src);
+ Sound(std::unique_ptr<Asset> res);
+
+private:
+ void load(std::unique_ptr<Asset> res);
+
+private:
+ SoLoud::Wav sample;
+ SoLoud::handle handle;
+
+ float volume = 1.0f;
+ bool looping = false;
+};
+
+} // namespace crepe
diff --git a/src/crepe/facade/SoundContext.cpp b/src/crepe/facade/SoundContext.cpp
new file mode 100644
index 0000000..5e5a3a9
--- /dev/null
+++ b/src/crepe/facade/SoundContext.cpp
@@ -0,0 +1,20 @@
+#include "../util/log.h"
+
+#include "SoundContext.h"
+
+using namespace crepe;
+
+SoundContext & SoundContext::get_instance() {
+ static SoundContext instance;
+ return instance;
+}
+
+SoundContext::SoundContext() {
+ dbg_trace();
+ engine.init();
+}
+
+SoundContext::~SoundContext() {
+ dbg_trace();
+ engine.deinit();
+}
diff --git a/src/crepe/facade/SoundContext.h b/src/crepe/facade/SoundContext.h
new file mode 100644
index 0000000..d3123d2
--- /dev/null
+++ b/src/crepe/facade/SoundContext.h
@@ -0,0 +1,26 @@
+#pragma once
+
+#include <soloud/soloud.h>
+
+#include "Sound.h"
+
+namespace crepe {
+
+class SoundContext {
+private:
+ 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:
+ SoLoud::Soloud engine;
+ friend class Sound;
+};
+
+} // namespace crepe