c++ Sprites

博客探讨了C++中的Sprites在游戏开发中的应用,包括UI和主要角色的表示。文章提到了类的设计,如const member function和方法如Clear、Present。此外,还讨论了static member function的概念,强调其为所有对象共享的类函数。接着,博主转向了Game.h文件,提及了单例模式作为设计模式的一种简单形式,用于确保类的唯一实例。虽然单例模式的具体实现细节可能不完全理解,但博客中提供了相关介绍。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

probably all the assets in the game will be sprites,from the UI to the main characters.

Sprites 就是中文的子画面的意思  看下代码

#pragma once

#if !defined(__BACKBUFFER_H__)
#define __BACKBUFFER_H__

// Library Includes
#include <Windows.h>

// Local Includes

// Types

// Constants

// Prototypes
class CBackBuffer
{
	// Member Functions
public:
	CBackBuffer();
	~CBackBuffer();

	bool Initialise(HWND _hWnd, int _iWidth, int _iHeight);

	HDC GetBFDC() const;

	int GetHeight() const;
	int GetWidth() const;

	void Clear();
	void Present();

protected:

private:
	CBackBuffer(const CBackBuffer& _kr);
	CBackBuffer& operator= (const CBackBuffer& _kr);

	// Member Variables
public:

protected:
	HWND m_hWnd;  //画图的用protect
	HDC m_hDC;
	HBITMAP m_hSurface;
	HBITMAP m_hOldObject;
	int m_iWidth;
	int m_iHeight;

private:

};

#endif // __BACKBUFFER_H__

定义了好多个 const member function  还有 许多method  如Clear Present

然而这只是一个叫什么backbuffer的

// Library Includes

// Local Includes

// This include
#include "BackBuffer.h"

// Static Variables

// Static Function Prototypes

// Implementation

CBackBuffer::CBackBuffer()  //在cpp里面初始化
: m_hWnd(0)
, m_hDC(0)
, m_hSurface(0)
, m_hOldObject(0)
, m_iWidth(0)
, m_iHeight(0)
{

}

CBackBuffer::~CBackBuffer()  // destructor
{
	SelectObject(m_hDC, m_hOldObject);

	DeleteObject(m_hSurface);
	DeleteObject(m_hDC);
}

bool
CBackBuffer::Initialise(HWND _hWnd, int _iWidth, int _iHeight)
{
	m_hWnd = _hWnd;

	m_iWidth = _iWidth;
	m_iHeight = _iHeight;

	HDC hWindowDC = ::GetDC(m_hWnd);

	m_hDC = CreateCompatibleDC(hWindowDC);

	m_hSurface = CreateCompatibleBitmap(hWindowDC, m_iWidth, m_iHeight);

	ReleaseDC(m_hWnd, hWindowDC);

	m_hOldObject = static_cast<HBITMAP>(SelectObject(m_hDC, m_hSurface));

	HBRUSH brushWhite = static_cast<HBRUSH>(GetStockObject(LTGRAY_BRUSH));
	HBRUSH oldBrush = static_cast<HBRUSH>(SelectObject(m_hDC, brushWhite));

	Rectangle(m_hDC, 0, 0, m_iWidth, m_iHeight);

	SelectObject(m_hDC, oldBrush);

	return (true);
}   // initialise   and return a bool

void
CBackBuffer::Clear()
{
	HBRUSH hOldBrush = static_cast<HBRUSH>(SelectObject(GetBFDC(), GetStockObject(LTGRAY_BRUSH)));

	Rectangle(GetBFDC(), 0, 0, GetWidth(), GetHeight());

	SelectObject(GetBFDC(), hOldBrush);
} // method 

HDC
CBackBuffer::GetBFDC() const
{
	return (m_hDC);
}// get the shit

int
CBackBuffer::GetWidth() const
{
	return (m_iWidth);
}// don't bother me again screw you 

int
CBackBuffer::GetHeight() const
{
	return (m_iHeight);
}// shits

void
CBackBuffer::Present()
{
	HDC hWndDC = ::GetDC(m_hWnd); // what the fuck is that? ::= means 作用域
	BitBlt(hWndDC, 0, 0, m_iWidth, m_iHeight, m_hDC, 0, 0, SRCCOPY);

	ReleaseDC(m_hWnd, hWndDC);   // what the fuck is ReleaseDC
}
the following is clock.h

#pragma once

#if !defined(__CLOCK_H__)
#define __CLOCK_H__

// Library Includes

// Local Includes

// Types

// Constants

// Prototypes // writh in the .h file in case of forgeting
class CClock
{
	// Member Functions
public:
	CClock();
	~CClock();

	bool Initialise();

	void Process();

	float GetDeltaTick();        //since like the method is in the public

protected:

private:
	CClock(const CClock& _kr);            // the pass value and operator is private  anyway i just guess
	CClock& operator= (const CClock& _kr);

	// Member Variables
public:

protected:            // but what does those protected varibles means?
	float m_fTimeElapsed;
	float m_fDeltaTime;
	float m_fLastTime;
	float m_fCurrentTime;

private:

};

#endif // __CLOCK_H__

now let's look at the cpp file

// Library Includes
#include <windows.h>
#include <timeapi.h>  // i really don't give a shit about the timeapi.h file

// Local Includes
#include "Clock.h"

// Static Variables

// Static Function Prototypes

// Implementation

CClock::CClock()
: m_fTimeElapsed(0.0f)
, m_fDeltaTime(0.0f)
, m_fLastTime(0.0f)
, m_fCurrentTime(0.0f)
{

}

CClock::~CClock()
{

}

bool
CClock::Initialise()         initialise is a bool 
{
	return (true);
}

void
CClock::Process()
{
	m_fLastTime = m_fCurrentTime;

	m_fCurrentTime = static_cast<float>(timeGetTime());  timeGetTime  wtf is that?

	if (m_fLastTime == 0.0f)
	{
		m_fLastTime = m_fCurrentTime;
	}

	m_fDeltaTime = m_fCurrentTime - m_fLastTime;

	m_fTimeElapsed += m_fDeltaTime;  Elapsed is a accumulation
}

float
CClock::GetDeltaTick()// why divided by 1000 
{
	return (m_fDeltaTime / 1000.0f);
}

https://blog.youkuaiyun.com/danky/article/details/1447011 该博文对于类和成员的函数讲的非常好,static member function 其实就是一个所有对象共有的类函数 因此无论怎么调用都是可以的。还有就是不能用类的函数给对象成员赋值。总的来说就是类比对象的级别要低,当然这只是个人理解

Check out the next one  . The following is Game.h file

#pragma once

#if !defined(__GAME_H__)
#define __GAME_H__

// Library Includes
#include <windows.h>

// Local Includes
#include "clock.h"

// Types

// Constants

// Prototypes
class CBackBuffer;

class CGame
{
	// Member Functions
public:
	~CGame();

	bool Initialise(HINSTANCE _hInstance, HWND _hWnd, int _iWidth, int _iHeight);

	void Draw();
	void Process(float _fDeltaTick);

	void ExecuteOneFrame();

	CBackBuffer* GetBackBuffer();
	HINSTANCE GetAppInstance();
	HWND GetWindow();

	// Singleton Methods
	static CGame& GetInstance();  // static means you don't have to know that member shit
	static void DestroyInstance();

protected:

private:
	CGame();
	CGame(const CGame& _kr);
	CGame& operator= (const CGame& _kr);

	// Member Variables
public:

protected:
	CClock* m_pClock;

	CBackBuffer* m_pBackBuffer;

	//Application data
	HINSTANCE m_hApplicationInstance;
	HWND m_hMainWindow;

	// Singleton Instance 
	static CGame* s_pGame;

private:

};

#endif // __GAME_H__

单例模式是设计模式中最简单的形式之一。这一模式的目的是使得类的一个对象成为系统中的唯一实例。要实现这一点,可以从客户端对其进行实例化开始。因此需要用一种只允许生成对象类的唯一实例的机制,“阻止”所有想要生成对象的访问。使用工厂方法来限制实例化过程。这个方法应该是静态方法(类方法),因为让类的实例去生成另一个唯一实例毫无意义。

不知道单例模式是什么鸡儿玩意,但是贴上来就对了 , 不太看得懂

// Library Includes

// Local Includes
#include "Clock.h"
#include "BackBuffer.h"
#include "Utilities.h"

// This Include
#include "Game.h"

// Static Variables
CGame* CGame::s_pGame = 0; static variable is a pointer wtf is that?

// Static Function Prototypes

// Implementation

CGame::CGame()
: m_pClock(0)
, m_hApplicationInstance(0)
, m_hMainWindow(0)
, m_pBackBuffer(0)
{

}

CGame::~CGame()
{
	delete m_pBackBuffer;
	m_pBackBuffer = 0;

	delete m_pClock;
	m_pClock = 0;
}

bool
CGame::Initialise(HINSTANCE _hInstance, HWND _hWnd, int _iWidth, int _iHeight)
{
	m_hApplicationInstance = _hInstance;
	m_hMainWindow = _hWnd;

	m_pClock = new CClock();
	VALIDATE(m_pClock->Initialise());
	m_pClock->Process();

	m_pBackBuffer = new CBackBuffer();
	VALIDATE(m_pBackBuffer->Initialise(_hWnd, _iWidth, _iHeight));

	ShowCursor(false);

	return (true);
}

void
CGame::Draw()
{
	m_pBackBuffer->Clear();

	// Do all the game’s drawing here...

	m_pBackBuffer->Present();
}

void
CGame::Process(float _fDeltaTick)
{
	// Process all the game’s logic here.
}

void
CGame::ExecuteOneFrame()
{
	float fDT = m_pClock->GetDeltaTick();

	Process(fDT);
	Draw();

	m_pClock->Process();

	Sleep(1);
}

CGame&
CGame::GetInstance() //  这就是什么脑残单例模式了,就是只有一种初始化实例的方法 是这样理解?
{
	if (s_pGame == 0)
	{
		s_pGame = new CGame();
	}

	return (*s_pGame);
}

void
CGame::DestroyInstance()
{
	delete s_pGame;
	s_pGame = 0;
}

CBackBuffer*
CGame::GetBackBuffer()
{
	return (m_pBackBuffer);
}

HINSTANCE
CGame::GetAppInstance()
{
	return (m_hApplicationInstance);
}

HWND
CGame::GetWindow()
{
	return (m_hMainWindow);
}
这东西看的我烦了  我决定去刷一下题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值