Skip to content

Commit

Permalink
Add more audio init code, and refactor to use unique_ptrs to control …
Browse files Browse the repository at this point in the history
…load time in SDLGame
  • Loading branch information
SgtCoDFish committed Jul 6, 2015
1 parent 681a0b6 commit dc59cf1
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 20 deletions.
5 changes: 3 additions & 2 deletions include/APG/Game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@

#include <cstdint>

#include "APG/InputManager.hpp"

namespace APG {
class InputManager;
class AudioManager;

/**
* Note that only one Game is expected to be made, and the public static screen size variables will be wrong if you create more than one.
Expand Down Expand Up @@ -69,6 +69,7 @@ class Game {
virtual void render(float deltaTime) = 0;

virtual const InputManager *input() const = 0;
virtual const AudioManager *audio() const = 0;
};

}
Expand Down
6 changes: 4 additions & 2 deletions include/APG/SDLAudioManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,16 @@
#ifndef INCLUDE_APG_SDLAUDIOMANAGER_HPP_
#define INCLUDE_APG_SDLAUDIOMANAGER_HPP_

#include <SDL2/SDL_mixer.h>

#include "APG/Audio.hpp"

namespace APG {

class SDLAudioManager: public AudioManager {
public:
explicit SDLAudioManager();
virtual ~SDLAudioManager() = default;
explicit SDLAudioManager(int frequency = MIX_DEFAULT_FREQUENCY, uint16_t format = MIX_DEFAULT_FORMAT);
virtual ~SDLAudioManager();

virtual music_handle loadMusicFile(const std::string &filename) override;
virtual sound_handle loadSoundFile(const std::string &filename) override;
Expand Down
14 changes: 11 additions & 3 deletions include/APG/SDLGame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "APG/Game.hpp"
#include "APG/SXXDL.hpp"
#include "APG/SDLInputManager.hpp"
#include "APG/SDLAudioManager.hpp"

namespace APG {

Expand All @@ -57,11 +58,14 @@ class SDLGame: public Game {

virtual void handleEvent(SDL_Event &event);

SDLInputManager inputManager;
std::unique_ptr<SDLInputManager> inputManager = nullptr;
std::unique_ptr<SDLAudioManager> audioManager = nullptr;

public:
/**
* Defaults to SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_EVENTS
* Defaults to SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_EVENTS.
*
* Without these defaults at least, initialization will probably fail.
*/
static uint32_t SDL_INIT_FLAGS;

Expand Down Expand Up @@ -97,7 +101,11 @@ class SDLGame: public Game {
}

virtual const InputManager *input() const override {
return &inputManager;
return inputManager.get();
}

virtual const AudioManager *audio() const override {
return audioManager.get();
}

private:
Expand Down
11 changes: 11 additions & 0 deletions include/APG/SXXDL.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <memory>

#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>

namespace SXXDL {

Expand All @@ -43,6 +44,16 @@ surface_ptr make_surface_ptr(SDL_Surface *surface);
sdl_texture_ptr make_sdl_texture_ptr(SDL_Texture *texture);
renderer_ptr make_renderer_ptr(SDL_Renderer *renderer);

namespace mixer {

using sound_ptr = std::unique_ptr<Mix_Chunk, void (*)(Mix_Chunk *)>;
using music_ptr = std::unique_ptr<Mix_Music, void(*)(Mix_Music *)>;

sound_ptr make_sound_ptr(Mix_Chunk *chunk);
music_ptr make_music_ptr(Mix_Music *music);

}

}

#endif /* SXXDL_HPP_ */
2 changes: 1 addition & 1 deletion src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ void APG::Game::setupLoggingDefault() {

conf.setToDefault();
conf.setGlobally(el::ConfigurationType::Format, "%datetime{%h:%m:%s.%g} [%levshort ] - %msg");
conf.set(el::Level::Verbose, el::ConfigurationType::Format, "%datetime{%h:%m:%s,%g} [%levshort%vlevel] - %msg");
conf.set(el::Level::Verbose, el::ConfigurationType::Format, "%datetime{%h:%m:%s.%g} [%levshort%vlevel] - %msg");
el::Loggers::reconfigureLogger("default", conf);
}
14 changes: 14 additions & 0 deletions src/SDLAudioManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,20 @@

#include "APG/SDLAudioManager.hpp"

APG::SDLAudioManager::SDLAudioManager(int frequency, uint16_t format) {
const auto logger = el::Loggers::getLogger("default");
if (Mix_OpenAudio(frequency, format, 2, 1024) == -1) {
logger->fatal("Couldn't open audio for SDL_mixer: %v", Mix_GetError());
return;
}

logger->info("Successfully started SDL_mixer.");
}

APG::SDLAudioManager::~SDLAudioManager() {
Mix_CloseAudio();
}

APG::AudioManager::music_handle APG::SDLAudioManager::loadMusicFile(const std::string &filename) {
const auto logger = el::Loggers::getLogger("default");
logger->info("Loading music file \"%v\".", filename);
Expand Down
17 changes: 13 additions & 4 deletions src/SDLGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,17 @@ APG::SDLGame::SDLGame(const std::string &windowTitle, uint32_t windowWidth, uint

logSDLVersions();

window = SXXDL::make_window_ptr(
SDL_CreateWindow(windowTitle.c_str(), windowX, windowY, windowWidth, windowHeight, SDL_WINDOW_FLAGS));
inputManager = std::make_unique<SDLInputManager>();
audioManager = std::make_unique<SDLAudioManager>();

const auto sdlWindow = SDL_CreateWindow(windowTitle.c_str(), windowX, windowY, windowWidth, windowHeight,
SDL_WINDOW_FLAGS);

if (sdlWindow == nullptr) {
logger->fatal("Couldn't create window: %v", SDL_GetError());
}

window = SXXDL::make_window_ptr(sdlWindow);

if (window == nullptr) {
logger->fatal("Couldn't create SDL window: %v", SDL_GetError());
Expand Down Expand Up @@ -106,7 +115,7 @@ APG::SDLGame::~SDLGame() {

void APG::SDLGame::handleEvent(SDL_Event &event) {
if (event.type == SDL_KEYDOWN || event.type == SDL_KEYUP) {
inputManager.handleInputEvent(event);
inputManager->handleInputEvent(event);
} else if (event.type == SDL_QUIT) {
quit();
}
Expand All @@ -117,7 +126,7 @@ bool APG::SDLGame::update(float deltaTime) {
return true;
}

inputManager.update(deltaTime);
inputManager->update(deltaTime);

while (SDL_PollEvent(&eventCache)) {
handleEvent(eventCache);
Expand Down
8 changes: 8 additions & 0 deletions src/SXXDL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,11 @@ SXXDL::renderer_ptr SXXDL::make_renderer_ptr(SDL_Renderer *renderer) {
return renderer_ptr(renderer, SDL_DestroyRenderer);
}

SXXDL::mixer::sound_ptr SXXDL::mixer::make_sound_ptr(Mix_Chunk *chunk) {
return sound_ptr(chunk, Mix_FreeChunk);
}

SXXDL::mixer::music_ptr SXXDL::mixer::make_music_ptr(Mix_Music *music) {
return music_ptr(music, Mix_FreeMusic);
}

16 changes: 8 additions & 8 deletions test/APGGLRenderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,24 +99,24 @@ void APG::APGGLRenderTest::render(float deltaTime) {
static float playerX = 128.0f, playerY = 128.0f - ((float) playerAnimation->getHeight() / 4.0f);

// will move the player quickly
if (inputManager.isKeyPressed(SDL_SCANCODE_UP)) {
if (inputManager->isKeyPressed(SDL_SCANCODE_UP)) {
playerY -= 32.0f;
} else if (inputManager.isKeyPressed(SDL_SCANCODE_DOWN)) {
} else if (inputManager->isKeyPressed(SDL_SCANCODE_DOWN)) {
playerY += 32.0f;
} else if (inputManager.isKeyPressed(SDL_SCANCODE_LEFT)) {
} else if (inputManager->isKeyPressed(SDL_SCANCODE_LEFT)) {
playerX -= 32.0f;
} else if (inputManager.isKeyPressed(SDL_SCANCODE_RIGHT)) {
} else if (inputManager->isKeyPressed(SDL_SCANCODE_RIGHT)) {
playerX += 32.0f;
}

// will move the player once per press
if (inputManager.isKeyJustPressed(SDL_SCANCODE_W)) {
if (inputManager->isKeyJustPressed(SDL_SCANCODE_W)) {
playerY -= 32.0f;
} else if (inputManager.isKeyJustPressed(SDL_SCANCODE_S)) {
} else if (inputManager->isKeyJustPressed(SDL_SCANCODE_S)) {
playerY += 32.0f;
} else if (inputManager.isKeyJustPressed(SDL_SCANCODE_A)) {
} else if (inputManager->isKeyJustPressed(SDL_SCANCODE_A)) {
playerX -= 32.0f;
} else if (inputManager.isKeyJustPressed(SDL_SCANCODE_D)) {
} else if (inputManager->isKeyJustPressed(SDL_SCANCODE_D)) {
playerX += 32.0f;
}

Expand Down

0 comments on commit dc59cf1

Please sign in to comment.