Static SDL engines
Hey. I've got an Audio_Engine and Draw_Engine that contain everything needed to play audio and draw to the screen (respectively :s). I was wondering if either of these could be made completely static... maybe each time a function is called a check (perhaps on a simple bool flag) is made to ensure that the engine has been initialised. Has anyone done this?
Cheers.
For my game, I used something similar for sound. My game wasn't complicated enough to warrant much more. Maybe a better way to get feedback would be to post your engines for comments.
/* Universal class. Edits affect multiple projects.*/#ifndef AUDIO_ENGINE_H#define AUDIO_ENGINE_H#include <string>#include <vector>#include <exception>#include "SDL/SDL_mixer.h"class Audio_Engine{public: Audio_Engine(); bool Initialise(); void Clean_Up(); int Get_Volume(); void Set_Volume(int new_volume); void Mute(); void Unmute(); static Mix_Chunk* Load_Sound(std::string file_name); static Mix_Music* Load_Music(std::string file_name); static void Free_Sound(Mix_Chunk*& sound); static void Free_Music(Mix_Music*& music); static bool Play_Sound(Mix_Chunk& sound); static bool Play_Music(Mix_Music& music, int loops = 1);private: static void Throw_If_Uninitialised(); static bool initialised; int volume_before_mute; int frequency; Uint16 format; int channels; int chunk_size;};#endif
/* Universal class. Edits affect multiple projects.*/#include "stdafx.h"#include "Audio_Engine.h"bool Audio_Engine::initialised = false;Audio_Engine::Audio_Engine(){ volume_before_mute = MIX_MAX_VOLUME; frequency = 44100; format = MIX_DEFAULT_FORMAT; channels = 2; chunk_size = 4096;}bool Audio_Engine::Initialise() { initialised = true; return(Mix_OpenAudio(frequency, format, channels, chunk_size) == 0);}void Audio_Engine::Clean_Up(){ Mix_CloseAudio();}int Audio_Engine::Get_Volume() { return Mix_Volume(-1, -1);}void Audio_Engine::Set_Volume(int new_volume) { if(new_volume > MIX_MAX_VOLUME) Mix_Volume(-1, 128); else if(new_volume < 0) Mix_Volume(-1, 0); else Mix_Volume(-1, new_volume);}void Audio_Engine::Mute() { // Get current volume setting volume_before_mute = Mix_Volume(-1, 0);}void Audio_Engine::Unmute() { // Use previous volume setting Mix_Volume(-1, volume_before_mute);}Mix_Chunk* Audio_Engine::Load_Sound(std::string file_name) { Throw_If_Uninitialised(); Mix_Chunk* sound = Mix_LoadWAV(file_name.c_str()); const char* error = Mix_GetError(); return sound;}Mix_Music* Audio_Engine::Load_Music(std::string file_name) { Throw_If_Uninitialised(); Mix_Music* music = Mix_LoadMUS(file_name.c_str()); return music;}void Audio_Engine::Free_Sound(Mix_Chunk*& sound) { Throw_If_Uninitialised(); Mix_FreeChunk(sound); sound = NULL;}void Audio_Engine::Free_Music(Mix_Music*& music) { Throw_If_Uninitialised(); Mix_FreeMusic(music); music = NULL;}bool Audio_Engine::Play_Sound(Mix_Chunk& sound) { Throw_If_Uninitialised(); return(Mix_PlayChannel(-1, &sound, 0) != -1);}bool Audio_Engine::Play_Music(Mix_Music& music, int loops) { Throw_If_Uninitialised(); return(Mix_PlayMusic(&music, loops) == 0);}void Audio_Engine::Throw_If_Uninitialised(){ if(!initialised) { throw std::runtime_error("Audio_Engine not initialised."); }}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement