게임 화면

적당히 대나무, 대나무 똥, 푸바오 사진을 가져와서 이미지를 입혔다. 이미지를 입히는 방식은 내가 쓴 포스팅인 DDS 포맷을 읽어들이는 방식을 사용했다.  ( 이미지 입히기 링크 ) 처음이다보니 오류도 많고 DirectX11 라이브러리를 사용한터라 익숙하지않아서 애좀 먹었다.. 그래도 어찌저찌 구현해서 뿌듯하다. 스테이지는 총 3스테이지이고 스스로 맵을 제작해서 추가도 가능하다.

 


 

맵, 이미지 불러오기

#ifndef __DATA_LIST_H__
#define __DATA_LIST_H__
#include <string>
#include <vector>
#include <tuple>

using std::string;
using std::vector;

vector< tuple<string, int, int> > mapData
{
	// 파일 위치, 폭, 높이
	make_tuple("Map\\stage1.txt", 7, 6),
	make_tuple("Map\\stage2.txt", 7, 6),
	make_tuple("Map\\stage3.txt", 9, 6)
};

vector<string> imageData
{
	"Object\\object.dds"
};

#endif
void Bamboo::getData(const vector<MapSet>& __map, const vector<string>& __object)
{
	int i = 0;
	const char* temp = nullptr;
        int tWidth = 0;
	int tHeight = 0;
	for (i = 0; i < __object.size(); ++i)
	{
		temp = __object[i].c_str();
		m_dataBase.readFile(temp);
	}

	for (i = 0; i < __map.size(); ++i)
	{
		temp = get<0>(__map[i]).c_str();
		tWidth = get<1>(__map[i]);
		tHeight = get<2>(__map[i]);
		readMap(temp, tWidth, tHeight);
	}
}

 

맵 데이터를 일일히 수작업으로 넣지않고 파일경로와 맵 너비x높이 정보만 입력해도 자동적으로 맵데이터가 들어가게끔 코드를 구성했다. vector와 tuple을 사용해서 맵데이터를 넣고 이미지는 vector와 string을 사용했다.

 


오브젝트 관리

#ifndef __OBJECT_H__
#define __OBJECT_H__

enum class Object
{
	CLEANER = 0,         // 휴지통
	CLEAN_OT_FUBAO = 8,  // 휴지통 위에 푸바오
	UNKO = 16,           // 대나무 똥
	CLEAN_OT_UNKO = 24,  // 휴지통에 대나무 치움
	WALL = 32,           // 대나무 벽
	FUBAO = 40,          // 푸바오
	SPACE = 48           // 공간
};

enum class Image : int
{
	FUBAO = 0,
	WALL = 32,
	CLEANER = 64,
	FULL  = 96,
	UNKO = 128,
	SPACE = 160
};

#endif // object.h

 

현재 위치정보나 맵 데이터 및 출력 관리하기 유용하게끔 오브젝트 전용 헤더를 따로 만들었다.

 

 


 

이미지 파일을 불러오는 헤더파일

#ifndef __IMAGE_FILE_H__
#define __IMAGE_FILE_H__
#include <vector>
#include <fstream>

using namespace std;
using namespace GameLib;

class ImageFile2D
{
public:
	explicit ImageFile2D() : m_height(0), m_width(0), m_image(nullptr) {}
	virtual ~ImageFile2D() { if (m_image != nullptr) delete[]m_image; }
	
	int getImageHeight() const { return m_height; }
	int getImageWidth(const int __index) const { return m_width; }

	void draw(int dstX, int dstY, Image srcX) const
	{
		Framework f = DirectX::instance();
		unsigned* vram = f.videoMemory();
		unsigned  windowWidth = f.width();
		int id = static_cast<int>(srcX);
		for (int y = 0; y < 32; ++y)
		{
			for (int x = 0; x < 32; ++x)
			{
				int pos = (dstY * 32 + y) * windowWidth + (dstX * 32 + x);
				vram[pos] = m_image[y * m_width + (x + id)];
			}
		}
	}

	void readFile(const char* __fileName)
	{
		ifstream input(__fileName, ifstream::binary);
		if (input)
		{
			int       tSize = 0;
			char*     tBuffer;
			input.seekg(0, ifstream::end);
			tSize = input.tellg();
			input.seekg(0, ifstream::beg);
			tBuffer = new char[tSize];
			input.read(tBuffer, tSize);
			m_width = getUnsigned ( &(tBuffer[16]) ) ;
			m_height = getUnsigned ( &(tBuffer[12]) ) ;
			m_image = new unsigned[m_width * m_height + 1];
			for (int i = 0; i < m_width * m_height; ++i) {
				m_image[i] = getUnsigned(&(tBuffer[128 + i * 4]));
			}
			delete[]tBuffer;
			input.close();
		}
	}
protected:
	// RGB를 읽어들이고 색이 채워진 unsigned를 반환한다.
	unsigned getUnsigned(const char* str)
	{
		const unsigned char* temp;
		temp = reinterpret_cast<const unsigned char*>(str);
		unsigned result = temp[0];
		result |= temp[1] << 8;
		result |= temp[2] << 16;
		result |= temp[3] << 24;
		return result;
	}
private:
	unsigned*  m_image;
	int        m_width;
	int        m_height;
};

#endif

 

dds 포맷으로부터 이미지를 불러오는 클래스를 싱글톤으로 구현하였다. draw는 해당 이미지를 출력해주는 멤버함수다 readFile은 이미지 파일을 읽어온다.

 


맵데이터 관리용 배열

#ifndef __ARRAY_2D_H__
#define __ARRAY_2D_H__
#include "Object.h"

class Array2D
{
public:
	// 생성을 희망하는 폭x높이를 입력받고 Fullsize의 기준으로 배열을 할당한다. 
	explicit Array2D(int Y, int X)
		:m_Y(Y), m_X(X), m_Capacity(Y * X)
	{
		m_Array = new Object[m_Capacity]{Object::SPACE};
	}

	virtual ~Array2D()
	{
		if (m_Array == nullptr)
			return;
		delete[]m_Array;
	}

	int getSize()   const { return m_Capacity; }
	int getY()      const { return m_Y; }
	int getX()      const { return m_X; }
	int getPlayer() const
	{
		int find = 0;
		for (find = 0; find < m_Capacity; ++find)
			if (m_Array[find] == Object::FUBAO || m_Array[find] == Object::CLEAN_OT_FUBAO)
				return find;
		return 0;
	}
	bool checkClear() const
	{
		int  find = 0;
		for (find = 0; find < m_Capacity; ++find) {
			if (m_Array[find] == Object::CLEANER)
				return false;
		}
		return true;
	}

	// operator
	Object& operator()(int Y, int X) { return m_Array[Y * m_X + X]; }
	const Object& operator()(int Y, int X) const { return m_Array[Y * m_X + X]; }

private:
	Object*   m_Array;
	const int m_Y;
	const int m_X;
	const int m_Capacity;
};

#endif // IntArray2D.h

 

오브젝트를 관리하는 배열을 따로 구현해서 사용했다. 1차원 배열 y * 너비 * x 방식으로 2차원배열을 쉽게 다룰 수 있게끔 구성했다. 원하는 2차원 배열 인덱스를 괄호로 넣기만 해도 일차원 배열에서 y * 너비 * x로 인덱스를 찾아서 출력해준다.

 

 


 

'프로젝트 > BamBoo Poop' 카테고리의 다른 글

[#3] bamboo poop - 완성 단계  (0) 2022.09.12
[#1] bamboo poop - 소코반 게임  (0) 2022.09.04