gpt4 book ai didi

c++ - 如何从 bmp 图像 C++ 纹理中获取 Sprite 集

转载 作者:行者123 更新时间:2023-11-28 02:19:59 25 4
gpt4 key购买 nike

我有这张图片: enter image description here

我想生成一组 sprite,每个大小为 32x32。我怎么能用 C++ 做到这一点,没有使用任何库。纹理类:

class Texture
{

protected:
// We cannot create or copy base class texture objects
// We will only ever have pointers or references to base
// class texture objects used in our program (and these
// will refer to derived class textures
Texture() = default;
Texture(Texture const &) = default;
Texture & operator=(Texture const &) = default;

virtual void LoadFromFile(std::string const & strFileName) = 0;
virtual void LoadFromResource(unsigned int rid) = 0;

public:
virtual ~Texture(){}

// virtual Rect const & GetBounds() const = 0;
virtual int Width() const = 0;
virtual int Height() const = 0;
};

using TEXTURE_PTR = std::shared_ptr<Texture>;

Sprite 类:

class Sprite
{
private:

virtual void draw_impl(Canvas & c) = 0;
TEXTURE_PTR m_pTexture;

protected:
// Ensure that Sprite objects can only be constructed by derived classes
explicit Sprite(TEXTURE_PTR pt = nullptr,POINT2f const & p = { 0, 0 });

// All Sprite objects have a position state variable
POINT2f m_position;


// Sprite objects can only be copied by derived class objects
Sprite(const Sprite&) = default;
Sprite& operator=(const Sprite&) = default;

public:

virtual ~Sprite(){}

void OnDraw(Canvas & c);

void SetPosition(POINT2f const & pos);
POINT2f const & GetPosition() const;
void SetTexture(TEXTURE_PTR pt);

};

然后我以这种方式创建 Sprite :

TEXTURE_PTR pLightning = std::make_shared<Texture>("resource//Lightning.bmp", RGB(255, 0, 255));
std::shared_ptr<Sprite> pSpark = std::make_shared<Sprite>(pLightning);

如何使用这种方法从上图中生成 9 个 Sprite ?

编辑我想出了这些代码,但仍然不起作用

class WinTexture : public Texture
{

protected:
HBITMAP m_hbmImage;
HBITMAP m_hbmMask;
BITMAP m_bmParam;


virtual void LoadFromResource(UINT rid);
virtual void LoadFromFile(std::string const & strFileName);
void CreateMask(DWORD dwTransparent);

public:
// Construct from Windows Resource
WinTexture(UINT uid, COLORREF dwTransparent);

// Constructor from file load
WinTexture(std::string const & strFilename, COLORREF dwTransparent);

//Contruct from other Texture
WinTexture(std::shared_ptr<WinTexture> wt, int xStart,int yStart, int w, int h);

virtual ~WinTexture();

// Inherited interface
// virtual Rect const & GetBounds() const;
virtual int Width() const;
virtual int Height() const;

HBITMAP ImageHandle() const;
HBITMAP MaskHandle() const;

};

有了这个,我想创建一个从其他 WinTexture 创建的构造函数:

WinTexture::WinTexture(std::shared_ptr<WinTexture> wt, int xStart, int yStart, int w, int h)
: Texture(), // as above
m_hbmImage(NULL),
m_hbmMask(NULL) {
HDC hdcMem1 = CreateCompatibleDC(0);
HDC hdcMem2 = CreateCompatibleDC(0);

m_hbmImage = CreateBitmap(w, h, 1, 1, NULL);
//m_hbmImage = CreateCompatibleBitmap(hdcMem2, 1, 1);
SelectObject(hdcMem1, wt->ImageHandle());
SelectObject(hdcMem2, m_hbmImage);
BitBlt(hdcMem2, xStart, yStart, w, h,hdcMem1, 0, 0, SRCCOPY);
BitBlt(hdcMem1, xStart, yStart, w, h, hdcMem2, 0, 0, SRCINVERT);
//SaveDC(hdcMem2);
DeleteDC(hdcMem1);
DeleteDC(hdcMem2);
CreateMask(RGB(0, 0, 0));
}

编辑目前,我已经从 Sprite 创建了这个类:

class TexturedSprite : public Sprite
{
private:
TEXTURE_PTR m_pTexture;

virtual void draw_impl(Canvas & c);

protected:

public:
explicit TexturedSprite(TEXTURE_PTR pt = nullptr, POINT2f pos = { 32, 32});
explicit TexturedSprite(int xStart,int yStart, int w, int h,TEXTURE_PTR pt = nullptr, POINT2f pos = { 32, 32 });

virtual ~TexturedSprite(){}

void SetTexture(TEXTURE_PTR pt);
};

我不知道如何实现第二个构造函数,复制输入纹理的一部分(pt):

TexturedSprite::TexturedSprite(int xStart, int yStart, int w, int h, TEXTURE_PTR pt , POINT2f pos )
:Sprite(pos)
{
HDC hdcMem1 = CreateCompatibleDC(0);
HDC hdcMem2 = CreateCompatibleDC(0);
//How to assign values to DC?
BitBlt(hdcMem1, 32, 32, w, h, hdcMem2, xStart, yStart, SRCCOPY);
DeleteDC(hdcMem1);
DeleteDC(hdcMem2);
}

最佳答案

至少在我现在阅读内容时,您的基本意图是加载纹理,然后通过将 32x32 像素的纹理片段复制到单个 Sprite 中来创建单个 Sprite 。除非您打算从单独的线程中操纵 Sprite (我觉得这不太可能),否则我会避免这样做。

相反,我会记下您提供给 BitBlt 的最后几个参数:

BitBlt(hdcMem2, xStart, yStart, w, h,hdcMem1, 0, 0, SRCCOPY);

SRCCOPY 之前的 0, 0 指定源位图中用作 BitBlt 起点的位置。

这样您就可以加载一次纹理,并且只需将单个纹理用于​​它包含的所有 Sprite 。在特定位置绘制单个 Sprite 只需要您在源位图中指定该 Sprite 的 X、Y 坐标(特别是该 Sprite 的左上角)并从那里开始绘制一个 32x32 的 block 。如果你想定义单独的 Sprite 对象,你当然可以这样做,但每个只需要存储一些东西,比如 shared_ptr 到加载的纹理,以及它的纹理的 X,Y 坐标。

class Sprite {
shared_ptr<Texture> tex;
int x, y;
public:
Sprite(shared_ptr<Texture> t, int x, int y) tex(t), x(x), y(y) {}

void draw(HDC dc, int dest_x, int dest_y) {
BitBlt(dc, dest_x, dest_y, 32, 32, *tex, x, y, SRCCOPY);
}
};

关于c++ - 如何从 bmp 图像 C++ 纹理中获取 Sprite 集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32864277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com