Fix music, fix cameras, fix shit

This commit is contained in:
/nick haya 2022-02-15 09:11:07 +08:00
parent b412a3b6cf
commit 98dee3e698
6 changed files with 177 additions and 63 deletions

View file

@ -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

View file

@ -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)

View file

@ -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<sdfml::sdSprite> 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);

View file

@ -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<string>("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 <typename T>
using Func = std::function<T()>;
static vector<float> _sec;
static vector<Func<int>> _call;
static vector<bool> _repeats;
static vector<int> _ticks;
class sdTimer {
public:
void start(float seconds, Func<int> 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<string, int> curMusic = pair<string, int>("", 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

6
src/sdfml/music.cpp Normal file
View file

@ -0,0 +1,6 @@
#include "music.hpp"
std::array<SoLoud::WavStream, MAX_SFX> sdfml::sfxBanks;
std::pair<SoLoud::WavStream, SoLoud::Openmpt> sdfml::musicBank;
SoLoud::Soloud sdfml::audio;
sdfml::AudioHandler sdfml::sound;

70
src/sdfml/music.hpp Normal file
View file

@ -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 <array>
#include <string>
#include <utility>
#define MAX_SFX 10
namespace sdfml {
extern std::array<SoLoud::WavStream, MAX_SFX> sfxBanks;
extern std::pair<SoLoud::WavStream, SoLoud::Openmpt> 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