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์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ๋ง

โš ๏ธ **GitHub.com Fallback** โš ๏ธ