diff options
Diffstat (limited to 'src/crepe/facade')
-rw-r--r-- | src/crepe/facade/CMakeLists.txt | 13 | ||||
-rw-r--r-- | src/crepe/facade/SdlContext.cpp | 213 | ||||
-rw-r--r-- | src/crepe/facade/SdlContext.h | 43 | ||||
-rw-r--r-- | src/crepe/facade/Sound.cpp | 60 | ||||
-rw-r--r-- | src/crepe/facade/Sound.h | 82 | ||||
-rw-r--r-- | src/crepe/facade/SoundContext.cpp | 20 | ||||
-rw-r--r-- | src/crepe/facade/SoundContext.h | 26 | ||||
-rw-r--r-- | src/crepe/facade/Texture.cpp | 39 | ||||
-rw-r--r-- | src/crepe/facade/Texture.h | 31 | ||||
-rw-r--r-- | src/crepe/facade/touch | 0 |
10 files changed, 527 insertions, 0 deletions
diff --git a/src/crepe/facade/CMakeLists.txt b/src/crepe/facade/CMakeLists.txt new file mode 100644 index 0000000..1263683 --- /dev/null +++ b/src/crepe/facade/CMakeLists.txt @@ -0,0 +1,13 @@ +target_sources(crepe PUBLIC + Sound.cpp + SoundContext.cpp + Texture.cpp + SdlContext.cpp +) + +target_sources(crepe PUBLIC FILE_SET HEADERS FILES + Sound.h + SoundContext.h + Texture.h + SdlContext.h +) diff --git a/src/crepe/facade/SdlContext.cpp b/src/crepe/facade/SdlContext.cpp new file mode 100644 index 0000000..44d1bdf --- /dev/null +++ b/src/crepe/facade/SdlContext.cpp @@ -0,0 +1,213 @@ + + +#include "SdlContext.h" +#include "SDL_hints.h" +#include "SDL_rect.h" +#include "SDL_stdinc.h" +#include "api/Sprite.h" +#include "api/Transform.h" +#include "facade/Texture.h" +#include "util/log.h" +#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 <cstddef> +#include <iostream> +#include <ostream> + +using namespace crepe; + +SdlContext & SdlContext::get_instance() { + static SdlContext instance; + return instance; +} + +void SdlContext::handleEvents(bool & running) { + SDL_Event event; + while (SDL_PollEvent(&event)) { + if (event.type == SDL_QUIT) { + running = false; + } + } +} +void SdlContext::clearScreen() { SDL_RenderClear(this->m_game_renderer); } + +void SdlContext::presentScreen() { SDL_RenderPresent(this->m_game_renderer); } + +void SdlContext::draw(const api::Sprite & sprite, + const api::Transform & transform) { + static SDL_RendererFlip renderFlip + = (SDL_RendererFlip) ((SDL_FLIP_HORIZONTAL * sprite.flip.flipX) + | (SDL_FLIP_VERTICAL * sprite.flip.flipY)); + + // needs maybe camera for position + static SDL_Rect dstrect = { + .x = static_cast<int>(transform.position.x), + .y = static_cast<int>(transform.position.y), + .w + = static_cast<int>(sprite.sprite_image->get_rect().w * transform.scale), + .h + = static_cast<int>(sprite.sprite_image->get_rect().h * transform.scale), + }; + + SDL_RenderCopyEx(this->m_game_renderer, sprite.sprite_image->get_texture(), + &sprite.sprite_image->get_rect(), &dstrect, 0, NULL, + renderFlip); +} + +SdlContext::SdlContext() { + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + std::cerr << "SDL could not initialize! SDL_Error: " << SDL_GetError() + << std::endl; + return; + } + + m_game_window = SDL_CreateWindow( + "Crepe Game Engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, + 1920, 1080, SDL_WINDOW_SHOWN); + if (!m_game_window) { + std::cerr << "Window could not be created! SDL_Error: " + << SDL_GetError() << std::endl; + } + + m_game_renderer + = SDL_CreateRenderer(m_game_window, -1, SDL_RENDERER_ACCELERATED); + if (!m_game_renderer) { + std::cerr << "Renderer could not be created! SDL_Error: " + << SDL_GetError() << std::endl; + SDL_DestroyWindow(m_game_window); + return; + } + int imgFlags = IMG_INIT_PNG; + if (!(IMG_Init(imgFlags) & imgFlags)) { + std::cout << "SDL_image could not initialize! SDL_image Error: " + << IMG_GetError() << std::endl; + } + + SDL_SetHint(SDL_HINT_RENDER_BATCHING, "1"); + SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl"); + //SDL_SetHint(SDL_HINT_RENDER_OPENGL_SHADERS, "1"); + SDL_SetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION, "X"); + + + + + const char * hint = SDL_GetHint(SDL_HINT_RENDER_BATCHING); + if (hint != NULL) { + std::cout << "SDL_HINT_RENDER_BATCHING: " << hint << std::endl; + } + + hint = SDL_GetHint(SDL_HINT_RENDER_DRIVER); + if (hint != NULL) { + std::cout << "SDL_HINT_RENDER_DRIVER: " << hint << std::endl; + } + + hint = SDL_GetHint(SDL_HINT_RENDER_OPENGL_SHADERS); + if (hint != NULL) { + std::cout << "SDL_HINT_RENDER_OPENGL_SHADERS: " << hint << std::endl; + } + + hint = SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY); + if (hint != NULL) { + std::cout << "SDL_HINT_RENDER_SCALE_QUALITY: " << hint << std::endl; + } + + hint = SDL_GetHint(SDL_HINT_RENDER_VSYNC); + if (hint != NULL) { + std::cout << "SDL_HINT_RENDER_VSYNC: " << hint << std::endl; + } + + hint = SDL_GetHint(SDL_HINT_TIMER_RESOLUTION); + if (hint != NULL) { + std::cout << "SDL_HINT_TIMER_RESOLUTION: " << hint << std::endl; + } + + hint = SDL_GetHint(SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT); + if (hint != NULL) { + std::cout << "SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT: " << hint + << std::endl; + } + + hint = SDL_GetHint(SDL_HINT_WINDOW_FRAME_USABLE_WHILE_CURSOR_HIDDEN); + if (hint != NULL) { + std::cout << "SDL_HINT_WINDOW_FRAME_USABLE_WHILE_CURSOR_HIDDEN: " + << hint << std::endl; + } + + hint = SDL_GetHint(SDL_HINT_RENDER_LOGICAL_SIZE_MODE); + if (hint != NULL) { + std::cout << "SDL_HINT_RENDER_LOGICAL_SIZE_MODE: " << hint << std::endl; + } + + hint = SDL_GetHint(SDL_HINT_VIDEO_DOUBLE_BUFFER); + if (hint != NULL) { + std::cout << "SDL_HINT_VIDEO_DOUBLE_BUFFER: " << hint << std::endl; + } + + hint = SDL_GetHint(SDL_HINT_OPENGL_ES_DRIVER); + if (hint != NULL) { + std::cout << "SDL_HINT_OPENGL_ES_DRIVER: " << hint << std::endl; + } + + hint = SDL_GetHint(SDL_HINT_FRAMEBUFFER_ACCELERATION); + if (hint != NULL) { + std::cout << "SDL_HINT_FRAMEBUFFER_ACCELERATION: " << hint << std::endl; + } + + std::cout << "HALLO " << std::endl; +} + +SdlContext::~SdlContext() { + if (m_game_renderer) SDL_DestroyRenderer(m_game_renderer); + + if (m_game_window) { + SDL_DestroyWindow(m_game_window); + } + IMG_Quit(); + SDL_Quit(); +} + +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(m_game_renderer, tmp); + + if (!CreatedTexture) { + std::cerr << "Error could not create texture " << IMG_GetError + << std::endl; + } + SDL_FreeSurface(tmp); + + return CreatedTexture; +} + +SDL_Texture * SdlContext::setTextureFromPath(const char * path) { + dbg_trace(); + + SDL_Surface * tmp = IMG_Load(path); + if (!tmp) { + std::cerr << "Error surface " << IMG_GetError << std::endl; + } + SDL_Texture * CreatedTexture + = SDL_CreateTextureFromSurface(m_game_renderer, tmp); + + if (!CreatedTexture) { + std::cerr << "Error could not create texture " << IMG_GetError + << std::endl; + } + SDL_FreeSurface(tmp); + + return CreatedTexture; +} diff --git a/src/crepe/facade/SdlContext.h b/src/crepe/facade/SdlContext.h new file mode 100644 index 0000000..c8f1304 --- /dev/null +++ b/src/crepe/facade/SdlContext.h @@ -0,0 +1,43 @@ +#pragma once + +#include "SDL_rect.h" +#include "api/Sprite.h" +#include "api/Transform.h" +#include <SDL2/SDL_render.h> +#include <SDL2/SDL_video.h> + +namespace crepe { + + +class SdlContext { + +public: + + void handleEvents(bool& running); + void clearScreen(); + void presentScreen(); + void draw(const api::Sprite&, const api::Transform&); + + // singleton + static SdlContext & get_instance(); + SDL_Texture* setTextureFromPath(const char*); + SDL_Texture* setTextureFromPath(const char*, SDL_Rect& clip, const int row, const int col); + +private: + SdlContext(); + virtual ~SdlContext(); + + SdlContext(const SdlContext &) = delete; + SdlContext(SdlContext &&) = delete; + SdlContext & operator=(const SdlContext &) = delete; + SdlContext & operator=(SdlContext &&) = delete; + + +private: + + SDL_Window* m_game_window; + SDL_Renderer* m_game_renderer; +}; + +} // + diff --git a/src/crepe/facade/Sound.cpp b/src/crepe/facade/Sound.cpp new file mode 100644 index 0000000..64fa281 --- /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..b11f871 --- /dev/null +++ b/src/crepe/facade/Sound.h @@ -0,0 +1,82 @@ +#pragma once + +#include <soloud/soloud.h> +#include <soloud/soloud_wav.h> + +#include <memory> + +#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..72047d2 --- /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 diff --git a/src/crepe/facade/Texture.cpp b/src/crepe/facade/Texture.cpp new file mode 100644 index 0000000..b4e3aa8 --- /dev/null +++ b/src/crepe/facade/Texture.cpp @@ -0,0 +1,39 @@ + + +#include "util/log.h" + +#include "Texture.h" +#include "SdlContext.h" +#include <SDL2/SDL_render.h> + +using namespace crepe; + +Texture::Texture(std::unique_ptr<api::Resource> res) { + dbg_trace(); + this->load(std::move(res)); +} + +Texture::Texture(const char * src) { + dbg_trace(); + this->load(std::make_unique<api::Resource>(src)); +} + +Texture::~Texture(){ + dbg_trace(); + if(this->m_texture){ + SDL_DestroyTexture(m_texture); + } +} +void Texture::load(std::unique_ptr<api::Resource> res) { + dbg_trace(); + SdlContext& ctx = SdlContext::get_instance(); + m_texture = ctx.setTextureFromPath(res->canonical(), srcrect, 1, 1); +} + +SDL_Texture* Texture::get_texture() const{ + return m_texture; +} + +SDL_Rect& Texture::get_rect() { + return srcrect; +} diff --git a/src/crepe/facade/Texture.h b/src/crepe/facade/Texture.h new file mode 100644 index 0000000..db2f1f9 --- /dev/null +++ b/src/crepe/facade/Texture.h @@ -0,0 +1,31 @@ +#pragma once + +#include "SDL_rect.h" +#include "api/baseResource.h" +#include "api/Resource.h" +#include <SDL2/SDL_render.h> +#include <memory> + +namespace crepe { + + + +class Texture : public api::BaseResource{ + +public: + Texture(const char * src); + Texture(std::unique_ptr<api::Resource> res); + ~Texture(); + + SDL_Texture* get_texture() const; + SDL_Rect& get_rect() ; +private: + void load(std::unique_ptr<api::Resource> res); + +private: + SDL_Texture* m_texture; + SDL_Rect srcrect; +}; + +} // namespace crepe + diff --git a/src/crepe/facade/touch b/src/crepe/facade/touch new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/crepe/facade/touch |