From 98dee3e6984284664114b58051188d7b6e2b883f Mon Sep 17 00:00:00 2001 From: /nick haya <74699483+The-SGPT@users.noreply.github.com> Date: Tue, 15 Feb 2022 09:11:07 +0800 Subject: [PATCH] Fix music, fix cameras, fix shit --- CMakeLists.txt | 4 ++ DOCUMENTATION.md | 8 ++- src/Main.cpp | 14 ++--- src/libflixel.hpp | 138 +++++++++++++++++++++++++++----------------- src/sdfml/music.cpp | 6 ++ src/sdfml/music.hpp | 70 ++++++++++++++++++++++ 6 files changed, 177 insertions(+), 63 deletions(-) create mode 100644 src/sdfml/music.cpp create mode 100644 src/sdfml/music.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5731d0f..e628e62 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,7 @@ set(SDL2_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/src/SDL2/") set(OUT_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/src/SoLoud/") set(OUT2_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/src/SoLoud/MIDI/") set(OUT3_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/src/toml/") +set(OUT4_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/src/sdfml/") # Support both 32 and 64 bit builds # Someone make a pull request to support MingW @@ -29,6 +30,7 @@ include_directories(${SDL2_INCLUDE_DIRS}) include_directories(${OUT_INCLUDE_DIRS}) include_directories(${OUT2_INCLUDE_DIRS}) include_directories(${OUT3_INCLUDE_DIRS}) +include_directories(${OUT4_INCLUDE_DIRS}) include_directories(${CMAKE_CURRENT_LIST_DIR}/src) # add EVERY FUCKING source file to SOURCES @@ -44,6 +46,8 @@ FILE(GLOB SOURCES "${CMAKE_CURRENT_LIST_DIR}/src/*.cpp" "${CMAKE_CURRENT_LIST_DIR}/src/SoLoud/MIDI/soloud_midi.h" "${CMAKE_CURRENT_LIST_DIR}/src/SoLoud/MIDI/*.cpp" "${CMAKE_CURRENT_LIST_DIR}/src/toml/*.hpp" +"${CMAKE_CURRENT_LIST_DIR}/src/sdfml/*.cpp" +"${CMAKE_CURRENT_LIST_DIR}/src/sdfml/*.hpp" ) # set c++ std to 17 diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md index ef78a75..1cb855b 100644 --- a/DOCUMENTATION.md +++ b/DOCUMENTATION.md @@ -4,6 +4,7 @@ A wrapper build around SDL2, some of its libraries, and SoLoud to make a lot of more easier to use. Stuff like: + - Rendering functions - Object system - Sound @@ -11,12 +12,13 @@ Stuff like: - and most possibly, more.... idk. - Less dll binary clutter! -# How the shit to use it den +## How the shit to use it den Currently, it only supports MSVC, but you could probably tweak CMakeLists.txt to be able to use MingW, or GCC. -## Requirements +### Requirements + - CMake, to build the damn thing - SDL2 and SDL_ttf 2.0, duh - SoLoud (static library) @@ -35,4 +37,4 @@ You know how to place them. It's fucking common sense. - After successfully making the build files, run `cmake --build --config Release` to build it without the use of VStudio. - If everything worked well, congratulations! You have just compiled a shitty program. -`src/Main.cpp` should contain example code with some explanations. \ No newline at end of file +`src/Main.cpp` should contain example code with some explanations. diff --git a/src/Main.cpp b/src/Main.cpp index bfcd9b2..26a8870 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -24,19 +24,17 @@ class ExampleState : public sdfml::sdState { public: - sdfml::musplayer pl; sdfml::sdAnimatedSprite example; - sdfml::sdCam camera; + sdfml::sdSprite bg1; + SDL_Rect camera = {0, 0, 640, 480}; // vector bgs; virtual void create() { - camera.pos = {0, 0}; - camera.size = {sdfml::mContext.size.x, sdfml::mContext.size.y}; example.create(50, 50, "data/images/smile.png"); example.AddAnimation("idle", {{0, 0, 50, 50}, {50, 0, 50, 50}}); example.PlayAnimation("idle"); example.framerate = 1; - //example.attachCamera(camera); - pl.playSFX("data/sounds/puch.wav"); + sdfml::sound.music.playMod("data/music/DOPE.it"); + example.updateCamera(&camera); add(&example); } virtual void update(float elapsed) { @@ -49,8 +47,7 @@ class ExampleState : public sdfml::sdState { if (sdfml::key_pressed(SDL_SCANCODE_DOWN)) example.y += 1; - //sdfml::focusCamera(&camera, example); - //example.updateCamera(camera); + sdfml::focusCamera(&camera, example); } }; @@ -66,6 +63,7 @@ int main(int argc, char* argv[]) // Verbose redirects io output to console, if available if (pa[{"-v", "--verbose"}]) { RedirectIOToConsole(); + sdfml::llog("Verbose mode", " enabled.", NORMAL, __FILENAME__, __LINE__); } sdfml::switchState(&m); diff --git a/src/libflixel.hpp b/src/libflixel.hpp index d5713e7..bf90f4a 100644 --- a/src/libflixel.hpp +++ b/src/libflixel.hpp @@ -34,6 +34,8 @@ #include "guicon.h" +#include "sdfml/music.hpp" + #ifdef _WIN32 #define __FILENAME__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) #else @@ -135,8 +137,6 @@ namespace sdfml { }; static context mContext; - - static SoLoud::Soloud audio; static SoLoud::SoundFont sf; class sdSprite { @@ -163,8 +163,8 @@ namespace sdfml { _w = width*scale.x; _h = height*scale.y; - _sc.x = _x-_cam.pos.x; - _sc.y = _y-_cam.pos.y; + _sc.x = _x-_camera->x; + _sc.y = _y-_camera->y; _sc.w = _w; _sc.h = _h; @@ -193,37 +193,39 @@ namespace sdfml { virtual void src_rect(SDL_Rect rect) { _src_rect = rect; } - virtual void updateCamera(sdCam camera) { - _cam = camera; + virtual void updateCamera(SDL_Rect* camera) { + _camera = camera; } private: int _x, _y, _w, _h; SDL_Rect _sc; SDL_Rect _src_rect = {0, 0, 0, 0}; + SDL_Rect dummy = {0, 0, 0, 0}; sdCam _cam; + SDL_Rect* _camera = &dummy; SDL_Texture* _tex; }; - inline void focusCamera(sdCam* camera, sdSprite sprite) { - camera->pos.x = ( sprite.x + (sprite.width / 2) ) - mContext.size.x / 2; - camera->pos.y = ( sprite.y + (sprite.height / 2) ) - mContext.size.y / 2; + inline void focusCamera(SDL_Rect* camera, sdSprite sprite) { + camera->x = ( sprite.x + (sprite.width / 2) ) - mContext.size.x / 2; + camera->y = ( sprite.y + (sprite.height / 2) ) - mContext.size.y / 2; - if( camera->pos.x < 0 ) + if( camera->x < 0 ) { - camera->pos.x = 0; + camera->x = 0; } - if( camera->pos.y < 0 ) + if( camera->y < 0 ) { - camera->pos.y = 0; + camera->y = 0; } - if( camera->pos.x > camera->size.x ) + if( camera->x > camera->w ) { - camera->pos.x = camera->size.x; + camera->x = camera->w; } - if( camera->pos.y > camera->size.y ) + if( camera->y > camera->h ) { - camera->pos.y = camera->size.y; + camera->y = camera->h; } } @@ -359,8 +361,7 @@ namespace sdfml { }); soundfont = tomlParse("conf.toml", "config", "soundfont"); - if (audio.init() > 0) - return llog("SoLoud", " has failed to initialize.", ERROR_, __FILENAME__, __LINE__); + sound.init(); llog("SoLoud", " is now initialized.", NORMAL, __FILENAME__, __LINE__); if (SDL_Init(SDL_INIT_EVERYTHING) < 0) return llog("SDL", " has failed to initialize.", ERROR_, __FILENAME__, __LINE__); @@ -390,6 +391,24 @@ namespace sdfml { return llog("", "Fully finalized initialization. Command over.", NORMAL, __FILENAME__, __LINE__); } + template + using Func = std::function; + + static vector _sec; + static vector> _call; + static vector _repeats; + static vector _ticks; + + class sdTimer { + public: + void start(float seconds, Func callback, bool repeat = false) { + _sec.push_back(seconds); + _call.push_back(callback); + _repeats.push_back(repeat); + _ticks.push_back(0); + } + }; + static const Uint8* kb; static const Uint8* kb_last; @@ -411,6 +430,10 @@ namespace sdfml { return false; } + inline int Sec2Tick(float time) { + return FRAMERATE*time; + } + inline int update() { int lastUpdate = SDL_GetTicks(); bool run = true; @@ -432,6 +455,32 @@ namespace sdfml { if (curState != nullptr) curState->update(dT); + // I know this is a shitty way to do this + // but bear with me + // it was hard to work with lambdas properly man + // let me have this just one please :) + if (_sec.size() > 0) { + for (int i = 0; i < _sec.size(); i++) { + if (_ticks[i] != -1) + _ticks[i]++; + + if (_sec[i] != -1) { + if (!(_ticks[i] < Sec2Tick(_sec[i]))) { + if (_call[i] != NULL) + _call[i](); + if (!_repeats[i] && _repeats[i] != NULL) + { + _ticks[i] = -1; + _sec[i] = -1; + _call[i] = NULL; + _repeats[i] = NULL; + } + } + } + // cout << i << endl; + } + } + lastUpdate = current; if (curState != nullptr) @@ -458,8 +507,8 @@ namespace sdfml { } SDL_DestroyRenderer(mContext.renderer); SDL_DestroyWindow(mContext.window); - audio.stopAll(); - audio.deinit(); + sound.deinit(); + ReleaseConsole(); TTF_Quit(); SDL_Quit(); @@ -467,39 +516,24 @@ namespace sdfml { } inline void switchState(sdState* state) { + if (curState != nullptr) { + _ticks = {}; + _call = {}; + _repeats = {}; + _sec = {}; + if (curState->get_spr().size() > 0) { + for (auto texture : curState->get_spr()) { + texture->destroy(); + } + } + if (curState->get_mspr().size() > 0) { + for (auto texture : curState->get_mspr()) { + texture.destroy(); + } + } + } curState = state; curState->create(); } - - static pair curMusic = pair("", 0); - - class musplayer { - public: - SoLoud::WavStream wav; - SoLoud::WavStream sfxWav; - SoLoud::Openmpt mod; - - void playSFX(string path) { - //audio.stopAudioSource(sfxWav); - sfxWav.load(path.c_str()); - audio.play(sfxWav); - } - - void playMus(string path, bool loop = true) { - audio.stopAudioSource(wav); - wav.load(path.c_str()); - wav.setLooping(loop); - curMusic.second = audio.playBackground(wav); - curMusic.first = path; - } - - void playMod(string path, bool loop = true) { - //audio.stopAudioSource(mod); - mod.load(path.c_str()); - mod.setLooping(loop); - curMusic.second = audio.playBackground(mod); - curMusic.first = path; - } - }; } #endif \ No newline at end of file diff --git a/src/sdfml/music.cpp b/src/sdfml/music.cpp new file mode 100644 index 0000000..068ca05 --- /dev/null +++ b/src/sdfml/music.cpp @@ -0,0 +1,6 @@ +#include "music.hpp" + +std::array sdfml::sfxBanks; +std::pair sdfml::musicBank; +SoLoud::Soloud sdfml::audio; +sdfml::AudioHandler sdfml::sound; \ No newline at end of file diff --git a/src/sdfml/music.hpp b/src/sdfml/music.hpp new file mode 100644 index 0000000..ec691a3 --- /dev/null +++ b/src/sdfml/music.hpp @@ -0,0 +1,70 @@ +#ifndef _MUSIC_H +#define _MUSIC_H +#include "../SoLoud/soloud.h" +#include "../SoLoud/soloud_wavstream.h" +#include "../SoLoud/soloud_speech.h" +#include "../SoLoud/soloud_modplug.h" +#include "../SoLoud/soloud_openmpt.h" +#include +#include +#include + +#define MAX_SFX 10 + +namespace sdfml { + + extern std::array sfxBanks; + extern std::pair musicBank; + + extern SoLoud::Soloud audio; + + class MusicHandler { + public: + void playMusic(std::string path) { + musicBank.first.stop(); + audio.stopAudioSource(musicBank.first); + musicBank.first.load(path.c_str()); + audio.play(musicBank.first); + } + + void playMod(std::string path) { + musicBank.second.stop(); + audio.stopAudioSource(musicBank.second); + musicBank.second.load(path.c_str()); + audio.playBackground(musicBank.second); + } + }; + + class AudioHandler { + public: + MusicHandler music; + + void init() { + audio.init(); + } + + void deinit() { + audio.stopAll(); + audio.deinit(); + } + + void play(std::string path) { + for (int i = 0; i < MAX_SFX; i++) { + if (sfxBanks[i].getLength() > 0) { + sfxBanks[i].load(path.c_str()); + audio.play(sfxBanks[i]); + break; + } else { + sfxBanks[i].stop(); + audio.stopAudioSource(sfxBanks[i]); + sfxBanks[i].load(path.c_str()); + audio.play(sfxBanks[i]); + break; + } + } + } + }; + + extern AudioHandler sound; +} +#endif \ No newline at end of file