Skip to content

Commit

Permalink
impl. audio support #40
Browse files Browse the repository at this point in the history
very basic and not at a state I'm happy with quite yet
  • Loading branch information
ZackeryRSmith committed Jan 13, 2024
1 parent 260bcd1 commit 3649590
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 5 deletions.
9 changes: 4 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ if(WIN32)
FetchContent_MakeAvailable(SFML)
log(STATUS "Made SFML available")

# don't build these as they're unused
set(SFML_BUILD_AUDIO FALSE)
# don't build as it's unused
set(SFML_BUILD_NETWORK FALSE)

else()
Expand All @@ -126,13 +125,13 @@ else()
# ----------------------------#
find_package(
SFML 2.5
COMPONENTS graphics window system
COMPONENTS graphics window system audio
REQUIRED)
endif()

# link SFML
target_link_libraries(${PROJECT_NAME} PRIVATE sfml-graphics sfml-window
sfml-system)
sfml-system sfml-audio)
log(STATUS "Linked SFML")

# ---------------------------------------------------------------------#
Expand All @@ -152,7 +151,7 @@ if(UNIX)
execute_process(
COMMAND
${CMAKE_COMMAND} -E echo_append
"\n${BoldWhite}___|${ColorReset}) ${BoldBlue}*${ColorReset}NIX SPECIFIC'S ${BoldWhite}|___${ColorReset} : "
"\n${BoldWhite}___|${ColorReset} ${BoldBlue}*${ColorReset}NIX SPECIFIC'S ${BoldWhite}|___${ColorReset} : "
)

if(LINUX)
Expand Down
16 changes: 16 additions & 0 deletions include/audio.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef BONGO_CAT_AUDIO_HPP
#define BONGO_CAT_AUDIO_HPP
#include <global.hpp>

namespace BongoAudio {
//============================================================================
// LOAD FROM ...
//============================================================================
sf::Sound *loadSoundFromFile(const std::string &);
sf::Music *loadMusicFromFile(const std::string &);
//============================================================================
// BIND TO LUA
//============================================================================
void bindToLua();
}; // namespace BongoAudio
#endif
1 change: 1 addition & 0 deletions include/global.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ extern "C" {
#include <LuaBridge/LuaBridge.h>

#include <LuaBridge/Array.h>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>

// used for std::clamp in input.cpp
Expand Down
41 changes: 41 additions & 0 deletions src/audio.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <audio.hpp>
#include <global.hpp>

//============================================================================
// LOAD FROM ...
//============================================================================
sf::Sound *BongoAudio::loadSoundFromFile(const std::string &path) {
sf::SoundBuffer *buffer = new sf::SoundBuffer();

if (!buffer->loadFromFile(path)) {
return nullptr;
}

sf::Sound *sound = new sf::Sound();
sound->setBuffer(*buffer);

return sound;
}

sf::Music *BongoAudio::loadMusicFromFile(const std::string &path) {
sf::Music *music = new sf::Music();

if (!music->openFromFile(path)) {
return nullptr;
}

return music;
}

//============================================================================
// BIND TO LUA
//============================================================================
void BongoAudio::bindToLua() {
luabridge::getGlobalNamespace(LuaState)
.beginNamespace("BongoAudio")
// load functions
.addFunction("loadSoundFromFile", &BongoAudio::loadSoundFromFile)
.addFunction("loadMusicFromFile", &BongoAudio::loadMusicFromFile)
// all things like controlling playback is defined in sfml.hpp
.endNamespace();
}
2 changes: 2 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <lua.hpp> // bongo lua
#include <sfml.hpp> // sfml lua bindings

#include <audio.hpp>
#include <global.hpp>
#include <helper.hpp>
#include <input.hpp>
Expand All @@ -21,6 +22,7 @@ int main() {
BongoWindow::bindToLua();
BongoSprite::bindToLua();
BongoInput::bindToLua();
BongoAudio::bindToLua();
BongoHelper::bindToLua();

BongoLua::executeScript("src/test.lua");
Expand Down

0 comments on commit 3649590

Please sign in to comment.