aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/facade
diff options
context:
space:
mode:
authormax-001 <maxsmits21@kpnmail.nl>2024-11-05 16:29:18 +0100
committermax-001 <maxsmits21@kpnmail.nl>2024-11-05 16:29:18 +0100
commitae6a103946e437ca85cc69c5fc2cbf68d35ffeae (patch)
treea0bd09748c68950353f05d245bed4de470548fc6 /src/crepe/facade
parenta5d3564f6d051986376c98abb9c098a8a7183fe0 (diff)
parent5b248d068a94902be9ca4d00fe07d551f64c49b9 (diff)
Merge remote-tracking branch 'origin/master' into max/gameobject
Diffstat (limited to 'src/crepe/facade')
-rw-r--r--src/crepe/facade/CMakeLists.txt14
-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
9 files changed, 503 insertions, 0 deletions
diff --git a/src/crepe/facade/CMakeLists.txt b/src/crepe/facade/CMakeLists.txt
new file mode 100644
index 0000000..dbddcc6
--- /dev/null
+++ b/src/crepe/facade/CMakeLists.txt
@@ -0,0 +1,14 @@
+target_sources(crepe PUBLIC
+ Sound.cpp
+ SoundContext.cpp
+ SDLApp.cpp
+ SDLContext.cpp
+)
+
+target_sources(crepe PUBLIC FILE_SET HEADERS FILES
+ Sound.h
+ SoundContext.h
+ SDLContext.h
+ SDLContext.h
+)
+
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