2D_game_singleton - 8BitsCoding/RobotMentor GitHub Wiki
๊ฐ๋จํ๊ธฐ์ ์ฝ๋๋ก ์ค๋ช
์ฑ๊ธํด์ ๋ง๋๋ ๋ถ๋ถ๋ณด๋ค, ์ฌ์ฉ๋ถ๋ถ์ ์์ฃผ๋ก ๋ณด์
์ฐธ๊ณ ๋ก ์ฌ๊ธฐ์ ์ฌ์ด๋ ์์ง์ ์์ ๋ก ๋ ์ด์ ๋ ์ฌ์ด๋, ๊ทธ๋ํฝ๊ณผ ๊ฐ์ด HW๋ฅผ ์ง์ ์ ์ดํ๋ ์์ง์ ๊ฒฝ์ฐ ์ธ์คํด์ค๋ฅผ ํ๋๋ง ๋ฐ๋ ๊ฒ์ ์ถ์ฒํ๋ค.
HW๋ฅผ ์ ์ดํ๋ ํธ๋ค์ ์ฌ๋ฌ๊ฐ ์์ฑํ ๊ฒฝ์ฐ ์ด๋ค ์๋ฌ๋ฅผ ๋ฐ์์ํฌ์ง ๋ชจ๋ฅธ๋ค.
// SoundEngine_Singleton.h
#pragma once
#include "fmod.hpp"
#include <iostream>
#include <map>
#include <string>
namespace jm
{
//Note: This example implementation of sound engine is inefficient.
class SoundEngine_Singleton
{
private:
FMOD::System *system = nullptr;
//FMOD::Channel *channel = nullptr;
std::map<std::string, FMOD::Sound*> sound_map;
std::map<FMOD::Sound*, FMOD::Channel*> channel_map;// not efficient
FMOD_RESULT result;
unsigned int version;
void *extradriverdata = nullptr;
static SoundEngine_Singleton * instance;
// static์ผ๋ก ์ ์ธ๋์ด ๋จ ํ๋๋ง ์์ฑ ๊ฐ๋ฅํ๋ค.
public:
static SoundEngine_Singleton * getInstance();
private:
// ์์ฑ์๊ฐ private์ด๊ธฐ์ ๋ณธ์ธ๋ง ์ ์ธ์ด ๊ฐ๋ฅํ๋ค.
SoundEngine_Singleton()
{
using namespace std;
result = FMOD::System_Create(&system);
if (result != FMOD_OK) {
cout << "FMOD::System_Create() fail" << endl;
exit(-1);
}
result = system->getVersion(&version);
if (result != FMOD_OK) {
cout << "getVersion() fail" << endl;
exit(-1);
}
else printf("FMOD version %08x\n", version);
result = system->init(32, FMOD_INIT_NORMAL, extradriverdata);
if (result != FMOD_OK) {
cout << "system->init() fail" << endl;
exit(-1);
}
}
public:
~SoundEngine_Singleton()
{
system->release();
}
void createSound(const std::string & filename, const std::string & sound_name, const bool & use_loop)
{
sound_map[sound_name] = nullptr;
auto & sound_ptr = sound_map[sound_name];
const int loop_flag = use_loop ? FMOD_LOOP_NORMAL : FMOD_LOOP_OFF;
result = system->createSound(filename.c_str(), loop_flag, 0, &sound_ptr);
if (result != FMOD_OK) {
std::cout << "system->createSound() fail" << std::endl;
exit(-1);
}
}
void playSound(const std::string & sound_name)
{
if (sound_map.count(sound_name) <= 0) {
std::cout << sound_name << " isn't initialized." << std::endl;
exit(-1);
}
const auto & sound_ptr = sound_map[sound_name];
auto & channel_ptr = channel_map[sound_ptr];
bool is_playing = false;
result = channel_ptr->isPlaying(&is_playing);
if (is_playing) return; // don't play if this is already playing
result = system->playSound(sound_ptr, 0, false, &channel_ptr);
if (result != FMOD_OK) {
std::cout << "system->playSound() fail" << std::endl;
exit(-1);
}
}
void stopSound(const std::string & sound_name)
{
if (sound_map.count(sound_name) <= 0) {
std::cout << sound_name << " isn't initialized." << std::endl;
exit(-1);
}
const auto & sound_ptr = sound_map[sound_name];
auto & channel_ptr = channel_map[sound_ptr];
bool is_playing = false;
result = channel_ptr->isPlaying(&is_playing);
if (is_playing == false) return; // don't stop playing if this is not playing
result = channel_ptr->stop();
if (result != FMOD_OK) {
std::cout << "system->playSound() fail" << std::endl;
exit(-1);
}
}
};
}
#include "SoundEngine_Singleton.h"
namespace jm
{
SoundEngine_Singleton * SoundEngine_Singleton::instance = nullptr;
SoundEngine_Singleton * SoundEngine_Singleton::getInstance()
{
if (instance == nullptr)
{
instance = new SoundEngine_Singleton();
}
return instance;
}
}
#pragma once
#include "Game2D.h"
#include "SoundEngine_Singleton.h"
namespace jm
{
class MyBullet
{
public:
vec2 center = vec2(0.0f, 0.0f);
vec2 velocity = vec2(0.0f, 0.0f);
MyBullet()
{
SoundEngine_Singleton::getInstance()->playSound("missile");
}
~MyBullet()
{
SoundEngine_Singleton::getInstance()->stopSound("missile");
}
???? ์ด๊ฒ ๊ฐ๋ฅํด ????
(::)Scope Operator ๋ฅผ ์ ๋ชฐ๋ผ์ ๊ทธ๋ ๋ค!
Scope Operator์ ๊ฒฝ์ฐ class, namespace ์ ์ ๊ทผ์ด ๊ฐ๋ฅํ๊ฒ ํด์ค๋ค.
class::Fn์ด ๊ฐ๋ฅํ๋ค๋ ๋ง