Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions Game/Audio.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#include "Audio.h"

using namespace irr;
using namespace irrklang;

namespace
{
class IrrFileReader : public IFileReader
{
public:
explicit IrrFileReader(io::IReadFile* f) : file_(f) {}
virtual ~IrrFileReader() { if (file_) file_->drop(); }

virtual ik_s32 read(void* buffer, ik_u32 sizeToRead)
{
return file_->read(buffer, sizeToRead);
}
virtual bool seek(ik_s32 finalPos, bool relativeMovement = false)
{
return file_->seek(finalPos, relativeMovement);
}
virtual ik_s32 getSize() { return file_->getSize(); }
virtual ik_s32 getPos() { return file_->getPos(); }
virtual const ik_c8* getFileName() { return file_->getFileName().c_str(); }

private:
io::IReadFile* file_;
};

class IrrFileFactory : public IFileFactory
{
public:
explicit IrrFileFactory(IrrlichtDevice* device) : device_(device) {}

virtual IFileReader* createFileReader(const ik_c8* filename)
{
io::IReadFile* file = device_->getFileSystem()->createAndOpenFile(filename);
if (!file)
return 0;
return new IrrFileReader(file);
}

private:
IrrlichtDevice* device_;
};
}

Audio::Audio()
: engine_(0), music_(0), factory_(0)
{
}

Audio::~Audio()
{
shutdown();
}

bool Audio::init(IrrlichtDevice* device)
{
engine_ = createIrrKlangDevice();
if (!engine_)
return false;

factory_ = new IrrFileFactory(device);
engine_->addFileFactory(factory_);
return true;
}

void Audio::shutdown()
{
if (music_)
{
music_->stop();
music_->drop();
music_ = 0;
}
if (factory_)
{
factory_->drop();
factory_ = 0;
}
if (engine_)
{
engine_->drop();
engine_ = 0;
}
}

void Audio::play(const char* file, bool loop)
{
if (engine_ && file && file[0])
engine_->play2D(file, loop);
}

void Audio::setMusic(const char* file)
{
if (!engine_)
return;

if (music_)
{
music_->stop();
music_->drop();
music_ = 0;
}

if (!file || !file[0])
return;

music_ = engine_->play2D(file, true, false, true);
if (music_)
music_->setVolume(0.5f);
}

void Audio::stopAll()
{
if (engine_)
engine_->stopAllSounds();
if (music_)
{
music_->stop();
music_->drop();
music_ = 0;
}
}
29 changes: 29 additions & 0 deletions Game/Audio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef FINDTHECHAIR_AUDIO_H
#define FINDTHECHAIR_AUDIO_H

#include <irrlicht.h>
#include <irrKlang.h>

// Owns the IrrKlang engine and the Irrlicht file-factory bridge.
class Audio
{
public:
Audio();
~Audio();

bool init(irr::IrrlichtDevice* device);
void shutdown();

void play(const char* file, bool loop = false);
void setMusic(const char* file); // empty string stops music
void stopAll();

irrklang::ISoundEngine* engine() const { return engine_; }

private:
irrklang::ISoundEngine* engine_;
irrklang::ISound* music_;
irrklang::IFileFactory* factory_;
};

#endif
119 changes: 65 additions & 54 deletions Game/EventReceiver.cpp
Original file line number Diff line number Diff line change
@@ -1,54 +1,65 @@
#include "EventReceiver.h"

EventReceiver::EventReceiver(){

//sets all the values in the keys to false - all keys are up
for(int i = 0; i < KEY_KEY_CODES_COUNT; i++){
KeyDown[i] = false;
}

for(int i = 0; i < MMenu::NUM_MMENU_BUTTONS + MMenu::NUM_OVRLY_BUTTONS;i++){
buttonPressed[i] = false;
}

}

bool EventReceiver::OnEvent(const SEvent& event){

switch(event.EventType){

case EET_KEY_INPUT_EVENT:
KeyDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
break;

case EET_GUI_EVENT:
switch (event.GUIEvent.EventType){
case irr::gui::EGET_BUTTON_CLICKED:
buttonPressed[event.GUIEvent.Caller->getID()] = true;
}
break;
default:
break;

}
return false;

}

bool EventReceiver::isKeyDown(EKEY_CODE keyCode) const{
return KeyDown[keyCode];
}

bool EventReceiver::isKeyUp(EKEY_CODE keyCode) const{
return !KeyDown[keyCode];
}

bool EventReceiver::isButtonPressed(int button){
return buttonPressed[button];
}

void EventReceiver::resetButtons(void){
for(int i = 0; i < MMenu::NUM_MMENU_BUTTONS + MMenu::NUM_OVRLY_BUTTONS;i++){
buttonPressed[i] = false;
}
}
#include "EventReceiver.h"

EventReceiver::EventReceiver()
{
for (int i = 0; i < irr::KEY_KEY_CODES_COUNT; ++i)
{
keyDown_[i] = false;
keyWasDown_[i] = false;
}
resetButtons();
}

bool EventReceiver::OnEvent(const irr::SEvent& event)
{
switch (event.EventType)
{
case irr::EET_KEY_INPUT_EVENT:
keyDown_[event.KeyInput.Key] = event.KeyInput.PressedDown;
break;

case irr::EET_GUI_EVENT:
if (event.GUIEvent.EventType == irr::gui::EGET_BUTTON_CLICKED)
{
const int id = event.GUIEvent.Caller->getID();
if (id >= 0 && id < GUI_COUNT)
buttonPressed_[id] = true;
}
break;

default:
break;
}
return false;
}

bool EventReceiver::isKeyDown(irr::EKEY_CODE keyCode) const
{
return keyDown_[keyCode];
}

bool EventReceiver::isKeyUp(irr::EKEY_CODE keyCode) const
{
return !keyDown_[keyCode];
}

bool EventReceiver::isButtonPressed(int buttonId) const
{
if (buttonId < 0 || buttonId >= GUI_COUNT)
return false;
return buttonPressed_[buttonId];
}

void EventReceiver::resetButtons()
{
for (int i = 0; i < GUI_COUNT; ++i)
buttonPressed_[i] = false;
}

bool EventReceiver::consumeKeyPress(irr::EKEY_CODE keyCode)
{
const bool down = keyDown_[keyCode];
const bool pressed = down && !keyWasDown_[keyCode];
keyWasDown_[keyCode] = down;
return pressed;
}
64 changes: 31 additions & 33 deletions Game/EventReceiver.h
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#endif

#ifndef EVENTRECEIVER_H
#define EVENTRECEIVER_H

#include <irrlicht.h>
#include "MMenu.h"
#include "Objective.h"

using namespace irr;
using namespace core;
using namespace video;
using namespace scene;
using namespace gui;

class EventReceiver:public IEventReceiver
{
private:
bool KeyDown[KEY_KEY_CODES_COUNT];
bool buttonPressed[MMenu::NUM_MMENU_BUTTONS + MMenu::NUM_OVRLY_BUTTONS];

public:
EventReceiver();
virtual bool OnEvent(const SEvent& event);
virtual bool isKeyDown(EKEY_CODE keyCode) const; //const means thsi function can not modify private members
virtual bool isKeyUp(EKEY_CODE keyCode) const;
bool isButtonPressed(int);
void resetButtons(void);
};
#endif

#ifndef EVENTRECEIVER_H
#define EVENTRECEIVER_H

#include <irrlicht.h>
#include "Ids.h"

// Pollable input adapter for Irrlicht.
// Keys are level-triggered; GUI buttons latch until resetButtons().
class EventReceiver : public irr::IEventReceiver
{
public:
EventReceiver();

virtual bool OnEvent(const irr::SEvent& event);

bool isKeyDown(irr::EKEY_CODE keyCode) const;
bool isKeyUp(irr::EKEY_CODE keyCode) const;

bool isButtonPressed(int buttonId) const;
void resetButtons();

// Rising-edge helper so menus do not fire every frame a key is held.
bool consumeKeyPress(irr::EKEY_CODE keyCode);

private:
bool keyDown_[irr::KEY_KEY_CODES_COUNT];
bool keyWasDown_[irr::KEY_KEY_CODES_COUNT];
bool buttonPressed_[GUI_COUNT];
};

#endif
Loading