diff --git a/DevScreenshots/12. Shadows.png b/DevScreenshots/12. Shadows.png new file mode 100644 index 0000000..2124db4 Binary files /dev/null and b/DevScreenshots/12. Shadows.png differ diff --git a/Game/src/MainState.cpp b/Game/src/MainState.cpp index 4160ad8..caf9637 100644 --- a/Game/src/MainState.cpp +++ b/Game/src/MainState.cpp @@ -3,18 +3,21 @@ void MainState::onEnter() { Log("MainState onEnter!"); camera->setType(nf::Camera::Type::FIRST_PERSON); - ap.load("CubeTest.nfpack"); + ap.load("example.nfpack"); test.create(ap["spec.obj"]); test.setPosition(0.0, 0.0, -5.0); plane.create(nf::BaseAssets::plane); plane.setPosition(0.0, -2.0, 0.0); plane.setScale(10.0); - text.create("This is a test text.", nf::Vec2(0.1, 0.025), nf::Vec3(0.7)); + text.create("This is a test text.", nf::Vec2(0.1, 0.025), nf::Vec3(0.8)); text.centered(true); uiTex.create(nf::BaseAssets::logo, nf::Vec2(0.025, 0.025), 0.5); button.create(nf::Vec2(0.8, 0.025), "Reset"); - light.create(nf::Vec3(0.0, 5.0, 0.0), nf::Vec3(1.0, 1.0, 1.0), 1.5); + button2.create(nf::Vec2(0.6, 0.025), "Play Sound"); + light.create(nf::Vec3(-5.0, 10.0, 5.0), nf::Vec3(1.0, 1.0, 1.0), 1.0); cm.create(nf::BaseAssets::cubemap); + + sound.create(ap["sound.wav"]); } void MainState::update(double deltaTime) { @@ -54,6 +57,9 @@ void MainState::update(double deltaTime) { if (button.isClicked()) app->changeState("Main State"); + if (button2.isClicked()) + sound.play(); + if (app->isKeyPressed(NFI_ESCAPE)) app->quit(); } @@ -65,6 +71,7 @@ void MainState::render(nf::Renderer& renderer) { renderer.render(text); renderer.render(uiTex); renderer.render(button); + renderer.render(button2); renderer.render(cm); } diff --git a/Game/src/include/MainState.h b/Game/src/include/MainState.h index 4e417bb..5548919 100644 --- a/Game/src/include/MainState.h +++ b/Game/src/include/MainState.h @@ -16,8 +16,11 @@ private: nf::Text text; nf::UITexture uiTex; nf::Button button; + nf::Button button2; nf::Light light; nf::Cubemap cm; + nf::Sound sound; + float xrot, yrot; }; \ No newline at end of file diff --git a/NFPackCreator/AssetBuild/base/models/cone.mtl b/NFPackCreator/AssetBuild/base/models/cone.mtl index 6e8d955..fee2f9b 100644 --- a/NFPackCreator/AssetBuild/base/models/cone.mtl +++ b/NFPackCreator/AssetBuild/base/models/cone.mtl @@ -2,7 +2,7 @@ # Material Count: 1 newmtl None -Ns 100 +Ns 10 Ka 0.8 0.8 0.8 Kd 0.8 0.8 0.8 Ks 0.8 0.8 0.8 diff --git a/NFPackCreator/AssetBuild/base/models/cube.mtl b/NFPackCreator/AssetBuild/base/models/cube.mtl index 6e8d955..fee2f9b 100644 --- a/NFPackCreator/AssetBuild/base/models/cube.mtl +++ b/NFPackCreator/AssetBuild/base/models/cube.mtl @@ -2,7 +2,7 @@ # Material Count: 1 newmtl None -Ns 100 +Ns 10 Ka 0.8 0.8 0.8 Kd 0.8 0.8 0.8 Ks 0.8 0.8 0.8 diff --git a/NFPackCreator/AssetBuild/base/models/cylinder.mtl b/NFPackCreator/AssetBuild/base/models/cylinder.mtl index 6e8d955..fee2f9b 100644 --- a/NFPackCreator/AssetBuild/base/models/cylinder.mtl +++ b/NFPackCreator/AssetBuild/base/models/cylinder.mtl @@ -2,7 +2,7 @@ # Material Count: 1 newmtl None -Ns 100 +Ns 10 Ka 0.8 0.8 0.8 Kd 0.8 0.8 0.8 Ks 0.8 0.8 0.8 diff --git a/NFPackCreator/AssetBuild/base/models/plane.mtl b/NFPackCreator/AssetBuild/base/models/plane.mtl index 6e8d955..fee2f9b 100644 --- a/NFPackCreator/AssetBuild/base/models/plane.mtl +++ b/NFPackCreator/AssetBuild/base/models/plane.mtl @@ -2,7 +2,7 @@ # Material Count: 1 newmtl None -Ns 100 +Ns 10 Ka 0.8 0.8 0.8 Kd 0.8 0.8 0.8 Ks 0.8 0.8 0.8 diff --git a/NFPackCreator/AssetBuild/base/models/sphere.mtl b/NFPackCreator/AssetBuild/base/models/sphere.mtl index 6e8d955..fee2f9b 100644 --- a/NFPackCreator/AssetBuild/base/models/sphere.mtl +++ b/NFPackCreator/AssetBuild/base/models/sphere.mtl @@ -2,7 +2,7 @@ # Material Count: 1 newmtl None -Ns 100 +Ns 10 Ka 0.8 0.8 0.8 Kd 0.8 0.8 0.8 Ks 0.8 0.8 0.8 diff --git a/NFPackCreator/AssetBuild/base/models/torus.mtl b/NFPackCreator/AssetBuild/base/models/torus.mtl index 6e8d955..fee2f9b 100644 --- a/NFPackCreator/AssetBuild/base/models/torus.mtl +++ b/NFPackCreator/AssetBuild/base/models/torus.mtl @@ -2,7 +2,7 @@ # Material Count: 1 newmtl None -Ns 100 +Ns 10 Ka 0.8 0.8 0.8 Kd 0.8 0.8 0.8 Ks 0.8 0.8 0.8 diff --git a/NFPackCreator/AssetBuild/base/shaders/entityFragment.shader b/NFPackCreator/AssetBuild/base/shaders/entityFragment.shader index b1b12f3..ed4f396 100644 --- a/NFPackCreator/AssetBuild/base/shaders/entityFragment.shader +++ b/NFPackCreator/AssetBuild/base/shaders/entityFragment.shader @@ -106,7 +106,7 @@ void main() { vec3 lightDir = normalize(light[i].pos - fragPos); vec3 norm = normalize(normals); float diff = max(dot(norm, lightDir), 0.0); - vec3 diffuse = light[i].color * (diff * matDiff) * (light[i].strength / 2.0f); + vec3 diffuse = light[i].color * diff * matDiff * (light[i].strength / 2.0f); vec3 viewDir = normalize(camera.pos - fragPos); vec3 reflectDir = reflect(-lightDir, norm); @@ -121,18 +121,18 @@ void main() { vec3 lightDir = normalize(light[i].pos - fragPos); vec3 norm = normalize(normals); float diff = max(dot(norm, lightDir), 0.0); - vec3 diffuse = light[i].color * (diff * matDiff); + vec3 diffuse = light[i].color * diff * matDiff * light[i].strength; vec3 viewDir = normalize(camera.pos - fragPos); vec3 reflectDir = reflect(-lightDir, norm); float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.specPower); - vec3 specular = light[i].color * spec * matSpec; + vec3 specular = light[i].color * spec * matSpec * (light[i].strength / 2.5f); float length = length(light[i].pos - fragPos); - float att = 1.0 / (1.0 + 0.09 * length + 0.032 * (length * length)); + float att = clamp(10.0 / length, 0.0, 1.0) * light[i].strength; float shadow = calcShadowPoint(i, norm, lightDir); - color += (((diffuse + specular) * (1.0 - shadow)) * att) * light[i].strength; + color += ((diffuse + specular) * (1.0 - shadow) * att); continue; } } diff --git a/NFPackCreator/AssetBuild/CubeTest/models/spec.mtl b/NFPackCreator/AssetBuild/example/models/spec.mtl similarity index 100% rename from NFPackCreator/AssetBuild/CubeTest/models/spec.mtl rename to NFPackCreator/AssetBuild/example/models/spec.mtl diff --git a/NFPackCreator/AssetBuild/CubeTest/models/spec.obj b/NFPackCreator/AssetBuild/example/models/spec.obj similarity index 100% rename from NFPackCreator/AssetBuild/CubeTest/models/spec.obj rename to NFPackCreator/AssetBuild/example/models/spec.obj diff --git a/NFPackCreator/AssetBuild/example/sounds/sound.wav b/NFPackCreator/AssetBuild/example/sounds/sound.wav new file mode 100644 index 0000000..06c7062 Binary files /dev/null and b/NFPackCreator/AssetBuild/example/sounds/sound.wav differ diff --git a/NFPackCreator/AssetBuild/CubeTest/textures/cube.png b/NFPackCreator/AssetBuild/example/textures/cube.png similarity index 100% rename from NFPackCreator/AssetBuild/CubeTest/textures/cube.png rename to NFPackCreator/AssetBuild/example/textures/cube.png diff --git a/NFPackCreator/AssetBuild/CubeTest/textures/diff.png b/NFPackCreator/AssetBuild/example/textures/diff.png similarity index 100% rename from NFPackCreator/AssetBuild/CubeTest/textures/diff.png rename to NFPackCreator/AssetBuild/example/textures/diff.png diff --git a/NFPackCreator/AssetBuild/CubeTest/textures/spec.png b/NFPackCreator/AssetBuild/example/textures/spec.png similarity index 100% rename from NFPackCreator/AssetBuild/CubeTest/textures/spec.png rename to NFPackCreator/AssetBuild/example/textures/spec.png diff --git a/NFPackCreator/src/main.cpp b/NFPackCreator/src/main.cpp index e651488..da1f9d3 100644 --- a/NFPackCreator/src/main.cpp +++ b/NFPackCreator/src/main.cpp @@ -91,7 +91,7 @@ int main(int argc, char* argv[]) { } std::set extensions; - extensions.insert({ "obj", "png", "jpg", "shader", "ttf" }); + extensions.insert({ "shader", "obj", "png", "jpg", "ttf", "wav" }); unsigned int dirCount = 0; const std::filesystem::path workingDir = std::filesystem::current_path(); @@ -99,6 +99,8 @@ int main(int argc, char* argv[]) { if (!currDir.is_directory()) continue; std::string filename = currDir.path().filename().string().append(".nfpack"); + /*if (filename == "base.nfpack") + Error("Cannot create a pack called base.nfpack!");*/ Log("Creating pack \"" + filename + (std::string)"\""); std::vector packFilenames; std::string currFileExtension; diff --git a/NothinFancy/NothinFancy.vcxproj b/NothinFancy/NothinFancy.vcxproj index 051b066..6d9ecd8 100644 --- a/NothinFancy/NothinFancy.vcxproj +++ b/NothinFancy/NothinFancy.vcxproj @@ -197,28 +197,31 @@ + - - - - - - - - - + + + + + + + + + + + @@ -236,6 +239,7 @@ + diff --git a/NothinFancy/NothinFancy.vcxproj.filters b/NothinFancy/NothinFancy.vcxproj.filters index d7f7da5..2be8a82 100644 --- a/NothinFancy/NothinFancy.vcxproj.filters +++ b/NothinFancy/NothinFancy.vcxproj.filters @@ -39,16 +39,16 @@ Source Files - + Source Files Source Files - + Source Files - + Source Files @@ -60,22 +60,28 @@ Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + Source Files - + + Source Files + + + Source Files + + Source Files @@ -155,6 +161,12 @@ Header Files + + Header Files + + + Header Files + diff --git a/NothinFancy/src/Application.cpp b/NothinFancy/src/Application.cpp index 0ba2b89..7c9be99 100644 --- a/NothinFancy/src/Application.cpp +++ b/NothinFancy/src/Application.cpp @@ -45,8 +45,8 @@ namespace nf { SetClassLongPtr(m_window, GCLP_HCURSOR, (LONG_PTR)hCursor); } - Renderer* Application::getRenderer() const { - return m_renderer; + AudioEngine* Application::getAudioEngine() const { + return m_audio; } void Application::addState(Gamestate* state, const std::string& stateName) { @@ -280,6 +280,7 @@ namespace nf { void Application::runMainGameThread() { m_sIntro = new IntroGamestate; m_currentState = m_sIntro; + m_audio = new AudioEngine(this); m_renderer = new Renderer(this); m_currentState->setup(this); m_currentState->onEnter(); @@ -292,6 +293,7 @@ namespace nf { if (m_deltaTime >= m_minFrametime) { lastFrame = std::chrono::steady_clock::now(); m_currentState->update(m_deltaTime); + m_audio->updateSources(); m_currentState->render(*m_renderer); m_renderer->doFrame(m_currentState->getCamera(), m_deltaTime); if (m_stateChange) @@ -311,6 +313,7 @@ namespace nf { } } m_currentState->onExit(); + delete m_audio; m_currentState->cleanup(); delete m_renderer; } @@ -323,6 +326,7 @@ namespace nf { } if (m_renderer->isFadeOutComplete()) { + m_audio->cleanup(); m_currentState->onExit(); m_currentState->cleanup(); m_currentState = m_states[m_nextState]; diff --git a/NothinFancy/src/Assets.cpp b/NothinFancy/src/Assets.cpp index 6fec6b7..2174f17 100644 --- a/NothinFancy/src/Assets.cpp +++ b/NothinFancy/src/Assets.cpp @@ -40,6 +40,10 @@ namespace nf { } + ASound::~ASound() { + + } + AssetPack::AssetPack() { } @@ -182,6 +186,16 @@ namespace nf { m_assets[assetName] = font; continue; } + if (extension == "wav") { + ASound* sound = new ASound; + sound->data = new char[assetSize]; + std::memcpy(sound->data, &assetContents[0], assetSize); + sound->size = assetSize; + if (packName == "base.nfpack") + sound->isBaseAsset = true; + m_assets[assetName] = sound; + continue; + } Error("Invalid asset extention in pack \"" + (std::string)packName + (std::string)"\"!"); } if (cubemapCount % 6 != 0) diff --git a/NothinFancy/src/AudioEngine.cpp b/NothinFancy/src/AudioEngine.cpp new file mode 100644 index 0000000..12bc307 --- /dev/null +++ b/NothinFancy/src/AudioEngine.cpp @@ -0,0 +1,56 @@ +#include "AudioEngine.h" + +#include "Application.h" + +namespace nf { + AudioEngine::AudioEngine(Application* app) : + m_app(app), + m_engine(nullptr), + m_masterVoice(nullptr) + { + CoInitializeEx(nullptr, COINIT_MULTITHREADED); + XAudio2Create(&m_engine, XAUDIO2_DEBUG_ENGINE); + XAUDIO2_DEBUG_CONFIGURATION debug = { 0 }; + debug.TraceMask = XAUDIO2_LOG_ERRORS | XAUDIO2_LOG_WARNINGS; + debug.BreakMask = XAUDIO2_LOG_ERRORS; + m_engine->SetDebugConfiguration(&debug, 0); + m_engine->CreateMasteringVoice(&m_masterVoice); + } + + void AudioEngine::updateSources() { + for (unsigned int i = 0; i < m_voices.size(); i++) { + XAUDIO2_VOICE_STATE state; + m_voices[i]->GetState(&state); + if (state.BuffersQueued == 0) { + m_voices[i]->Stop(); + m_voices[i]->FlushSourceBuffers(); + m_voices[i]->DestroyVoice(); + m_voices.erase(m_voices.begin() + i); + i = 0; + } + } + } + + IXAudio2SourceVoice* AudioEngine::getNewSourceVoice(WAVEFORMATEXTENSIBLE* fmt) { + IXAudio2SourceVoice* s; + HRESULT hr = m_engine->CreateSourceVoice(&s, &fmt->Format); + m_voices.push_back(s); + return s; + } + + void AudioEngine::cleanup() { + for (unsigned int i = 0; i < m_voices.size(); i++) { + m_voices[i]->Stop(); + m_voices[i]->FlushSourceBuffers(); + m_voices[i]->DestroyVoice(); + } + m_voices.clear(); + } + + AudioEngine::~AudioEngine() { + cleanup(); + m_masterVoice->DestroyVoice(); + m_engine->Release(); + CoUninitialize(); + } +} \ No newline at end of file diff --git a/NothinFancy/src/Renderer/Drawable/Button.cpp b/NothinFancy/src/NFObject/Button.cpp similarity index 98% rename from NothinFancy/src/Renderer/Drawable/Button.cpp rename to NothinFancy/src/NFObject/Button.cpp index 9768cd1..f0a97b6 100644 --- a/NothinFancy/src/Renderer/Drawable/Button.cpp +++ b/NothinFancy/src/NFObject/Button.cpp @@ -22,6 +22,8 @@ namespace nf { } void Button::create(const Vec2& position, std::string string, Asset* buttonAsset, double scale, double opacity) { + if (m_constructed) + Error("Button already created!"); m_constructed = true; m_position = position; m_string = string; diff --git a/NothinFancy/src/Renderer/Drawable/Cubemap.cpp b/NothinFancy/src/NFObject/Cubemap.cpp similarity index 98% rename from NothinFancy/src/Renderer/Drawable/Cubemap.cpp rename to NothinFancy/src/NFObject/Cubemap.cpp index 250a865..0a7d704 100644 --- a/NothinFancy/src/Renderer/Drawable/Cubemap.cpp +++ b/NothinFancy/src/NFObject/Cubemap.cpp @@ -17,6 +17,8 @@ namespace nf { } void Cubemap::create(Asset* cubemapAsset) { + if (m_constructed) + Error("Cubemap already created!"); m_constructed = true; ACubemap& cm = *(ACubemap*)cubemapAsset; glGenTextures(1, &m_id); diff --git a/NothinFancy/src/Renderer/Drawable/Entity.cpp b/NothinFancy/src/NFObject/Entity.cpp similarity index 97% rename from NothinFancy/src/Renderer/Drawable/Entity.cpp rename to NothinFancy/src/NFObject/Entity.cpp index 549cc19..91ace0c 100644 --- a/NothinFancy/src/Renderer/Drawable/Entity.cpp +++ b/NothinFancy/src/NFObject/Entity.cpp @@ -18,6 +18,8 @@ namespace nf { } void Entity::create(Asset* modelAsset) { + if (m_constructed) + Error("Entity already created!"); m_constructed = true; AModel* model; if ((model = dynamic_cast(modelAsset)) == nullptr) diff --git a/NothinFancy/src/Renderer/Drawable/Light.cpp b/NothinFancy/src/NFObject/Light.cpp similarity index 97% rename from NothinFancy/src/Renderer/Drawable/Light.cpp rename to NothinFancy/src/NFObject/Light.cpp index 2c26e47..f71287c 100644 --- a/NothinFancy/src/Renderer/Drawable/Light.cpp +++ b/NothinFancy/src/NFObject/Light.cpp @@ -13,6 +13,8 @@ namespace nf { } void Light::create(const Vec3& position, const Vec3& color, double strength, Type type) { + if (m_constructed) + Error("Light already created!"); m_constructed = true; m_position = position; m_color = color; diff --git a/NothinFancy/src/NFObject/Sound.cpp b/NothinFancy/src/NFObject/Sound.cpp new file mode 100644 index 0000000..c57847a --- /dev/null +++ b/NothinFancy/src/NFObject/Sound.cpp @@ -0,0 +1,85 @@ +#include "Sound.h" + +#include "Application.h" +#include "Assets.h" +#include "Utility.h" + +namespace nf { + Sound::Sound() : + m_constructed(false), + m_dataSize(0), + m_volume(1.0f), + m_format({ 0 }), + m_buffer(nullptr), + m_currentVoice(nullptr) + { + + } + + void Sound::create(Asset* soundAsset) { + if (m_constructed) + Error("Sound already created!"); + m_constructed = true; + ASound* sound; + if ((sound = dynamic_cast(soundAsset)) == nullptr) + Error("Non-sound asset passed to Sound::create!"); + std::string data(sound->data, sound->size); + if(data.find("RIFF") == std::string::npos) + Error("Sound asset not of correct format!"); + unsigned int fileSize = *(unsigned int*)&data[4]; + unsigned int fmtPos; + if((fmtPos = data.find("fmt")) == std::string::npos) + Error("Sound asset not of correct format!"); + std::memcpy(&m_format, &data[fmtPos + 8], 16); + unsigned int dataPos; + if ((dataPos = data.find("data")) == std::string::npos) + Error("Sound asset not of correct m_format!"); + m_dataSize = *(unsigned int*)&data[dataPos + 4]; + m_buffer = new unsigned char[m_dataSize]; + std::memcpy(m_buffer, &data[dataPos + 8], m_dataSize); + + Application::getApp()->getCurrentState()->m_nfObjects.push_back(this); + } + + void Sound::setVolume(double volume) { + m_volume = (float)volume; + } + + void Sound::play(bool loop) { + m_currentVoice = Application::getApp()->getAudioEngine()->getNewSourceVoice(&m_format); + m_currentVoice->SetVolume(m_volume); + + XAUDIO2_BUFFER xBuffer = { 0 }; + xBuffer.pAudioData = m_buffer; + xBuffer.AudioBytes = m_dataSize; + if (loop) + xBuffer.LoopCount = XAUDIO2_LOOP_INFINITE; + m_currentVoice->SubmitSourceBuffer(&xBuffer); + m_currentVoice->Start(); + } + + void Sound::stop() { + if (m_currentVoice) { + XAUDIO2_VOICE_STATE state; + m_currentVoice->GetState(&state); + if (state.BuffersQueued > 0) { + m_currentVoice->Stop(); + m_currentVoice->FlushSourceBuffers(); + m_currentVoice = nullptr; + } + } + } + + void Sound::destroy() { + m_constructed = false; + m_dataSize = 0; + m_volume = 1.0f; + m_format = { 0 }; + delete m_buffer; + m_currentVoice = nullptr; + } + + Sound::~Sound() { + destroy(); + } +} \ No newline at end of file diff --git a/NothinFancy/src/Renderer/Drawable/Text.cpp b/NothinFancy/src/NFObject/Text.cpp similarity index 98% rename from NothinFancy/src/Renderer/Drawable/Text.cpp rename to NothinFancy/src/NFObject/Text.cpp index 707f6e4..3e55b78 100644 --- a/NothinFancy/src/Renderer/Drawable/Text.cpp +++ b/NothinFancy/src/NFObject/Text.cpp @@ -18,6 +18,8 @@ namespace nf { } void Text::create(const std::string& string, const Vec2& position, const Vec3& color, double opacity, double scale, Asset* font) { + if (m_constructed) + Error("Text already created!"); m_constructed = true; m_string = string; m_position = position; diff --git a/NothinFancy/src/Renderer/Drawable/UITexture.cpp b/NothinFancy/src/NFObject/UITexture.cpp similarity index 97% rename from NothinFancy/src/Renderer/Drawable/UITexture.cpp rename to NothinFancy/src/NFObject/UITexture.cpp index bacef1b..1931995 100644 --- a/NothinFancy/src/Renderer/Drawable/UITexture.cpp +++ b/NothinFancy/src/NFObject/UITexture.cpp @@ -17,6 +17,8 @@ namespace nf { } void UITexture::create(Asset* textureAsset, const Vec2& position, double scale, double opacity) { + if (m_constructed) + Error("UITexture already created!"); m_constructed = true; ATexture* tex; if ((tex = dynamic_cast(textureAsset)) == nullptr) diff --git a/NothinFancy/src/Renderer/Drawable/Drawable.cpp b/NothinFancy/src/Renderer/Drawable.cpp similarity index 100% rename from NothinFancy/src/Renderer/Drawable/Drawable.cpp rename to NothinFancy/src/Renderer/Drawable.cpp diff --git a/NothinFancy/src/Renderer/Drawable/Model.cpp b/NothinFancy/src/Renderer/Model.cpp similarity index 100% rename from NothinFancy/src/Renderer/Drawable/Model.cpp rename to NothinFancy/src/Renderer/Model.cpp diff --git a/NothinFancy/src/Renderer/Drawable/UIElement.cpp b/NothinFancy/src/Renderer/UIElement.cpp similarity index 100% rename from NothinFancy/src/Renderer/Drawable/UIElement.cpp rename to NothinFancy/src/Renderer/UIElement.cpp diff --git a/NothinFancy/src/Utility.cpp b/NothinFancy/src/Utility.cpp index a8aaa8b..78b1977 100644 --- a/NothinFancy/src/Utility.cpp +++ b/NothinFancy/src/Utility.cpp @@ -1,5 +1,4 @@ #include "Utility.h" -#include #include #include #include diff --git a/NothinFancy/src/include/Application.h b/NothinFancy/src/include/Application.h index d09b1f3..cc6e7d3 100644 --- a/NothinFancy/src/include/Application.h +++ b/NothinFancy/src/include/Application.h @@ -7,6 +7,7 @@ #include "Config.h" #include "IntroGamestate.h" #include "Renderer.h" +#include "AudioEngine.h" //TODO: Document ALL frontend functions namespace nf { @@ -18,7 +19,7 @@ namespace nf { void setWindowIcon(HANDLE hIcon); void setWindowCursor(HCURSOR hCursor); - Renderer* getRenderer() const; + AudioEngine* getAudioEngine() const; void addState(Gamestate* state, const std::string& stateName); void addDefaultState(const std::string& stateName); const std::string& getDefaultState(); @@ -91,5 +92,7 @@ namespace nf { //Renderer object to use OpenGL to render the current state Renderer* m_renderer; + + AudioEngine* m_audio; }; } \ No newline at end of file diff --git a/NothinFancy/src/include/Assets.h b/NothinFancy/src/include/Assets.h index 86458ba..b2fe885 100644 --- a/NothinFancy/src/include/Assets.h +++ b/NothinFancy/src/include/Assets.h @@ -10,15 +10,14 @@ namespace nf { struct Asset { char* data = nullptr; + size_t size = 0; bool alreadyLoaded = false; bool isBaseAsset = false; virtual ~Asset(); }; struct ATexture : Asset { - size_t size = 0; Texture* loadedTexture = nullptr; - ~ATexture() override; }; @@ -70,6 +69,10 @@ namespace nf { ~AShader() override; }; + struct ASound : Asset { + ~ASound() override; + }; + class AssetPack : public NFObject { public: AssetPack(); diff --git a/NothinFancy/src/include/AudioEngine.h b/NothinFancy/src/include/AudioEngine.h new file mode 100644 index 0000000..5cf50bd --- /dev/null +++ b/NothinFancy/src/include/AudioEngine.h @@ -0,0 +1,23 @@ +#pragma once +#include +#include + +namespace nf { + class Application; + + class AudioEngine { + public: + AudioEngine(Application* app); + + void updateSources(); + IXAudio2SourceVoice* getNewSourceVoice(WAVEFORMATEXTENSIBLE* fmt); + + void cleanup(); + ~AudioEngine(); + private: + Application* m_app; + IXAudio2* m_engine; + IXAudio2MasteringVoice* m_masterVoice; + std::vector m_voices; + }; +} \ No newline at end of file diff --git a/NothinFancy/src/include/NothinFancy.h b/NothinFancy/src/include/NothinFancy.h index 225261c..f7d4b26 100644 --- a/NothinFancy/src/include/NothinFancy.h +++ b/NothinFancy/src/include/NothinFancy.h @@ -16,12 +16,14 @@ #include "Text.h" #include "UITexture.h" #include "Button.h" +#include "Sound.h" #include "Input.h" namespace nf { class Drawable; class Shader; class Model; + class AudioEngine; class Renderer { public: @@ -86,7 +88,6 @@ namespace nf { void setWindowIcon(HANDLE hIcon); void setWindowCursor(HCURSOR hCursor); - Renderer* getRenderer() const; void addState(Gamestate* state, const std::string& stateName); void addDefaultState(const std::string& stateName); const std::string& getDefaultState(); @@ -159,5 +160,7 @@ namespace nf { //Renderer object to use OpenGL to render the current state Renderer* m_renderer; + + AudioEngine* m_audio; }; } \ No newline at end of file diff --git a/NothinFancy/src/include/Sound.h b/NothinFancy/src/include/Sound.h new file mode 100644 index 0000000..41bb649 --- /dev/null +++ b/NothinFancy/src/include/Sound.h @@ -0,0 +1,28 @@ +#pragma once +#include + +#include "NFObject.h" + +namespace nf { + struct Asset; + + class Sound : public NFObject { + public: + Sound(); + + void create(Asset* soundAsset); + void setVolume(double volume); + void play(bool loop = false); + void stop(); + + void destroy() override; + ~Sound(); + private: + bool m_constructed; + unsigned int m_dataSize; + float m_volume; + WAVEFORMATEXTENSIBLE m_format; + unsigned char* m_buffer; + IXAudio2SourceVoice* m_currentVoice; + }; +} \ No newline at end of file diff --git a/NothinFancy/src/include/Utility.h b/NothinFancy/src/include/Utility.h index fa5241a..6532022 100644 --- a/NothinFancy/src/include/Utility.h +++ b/NothinFancy/src/include/Utility.h @@ -1,4 +1,5 @@ #pragma once +#include #include #include #include