How to get the set of sprites from bmp image C++ Texture -
i want generate set of sprites, each 32x32 in size. how can c++, no library used. texture class:
class texture { protected: // cannot create or copy base class texture objects // ever have pointers or references base // class texture objects used in our program (and these // refer 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:
class sprite { private: virtual void draw_impl(canvas & c) = 0; texture_ptr m_ptexture; protected: // ensure sprite objects can constructed derived classes explicit sprite(texture_ptr pt = nullptr,point2f const & p = { 0, 0 }); // sprite objects have position state variable point2f m_position; // sprite objects can copied 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); };
and create sprite way:
texture_ptr plightning = std::make_shared<texture>("resource//lightning.bmp", rgb(255, 0, 255)); std::shared_ptr<sprite> pspark = std::make_shared<sprite>(plightning);
how can generate 9 sprites above image method?
edit come these code still doesn't work
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 windows resource wintexture(uint uid, colorref dwtransparent); // constructor file load wintexture(std::string const & strfilename, colorref dwtransparent); //contruct 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; };
with this, want make constructor create other wintexture:
wintexture::wintexture(std::shared_ptr<wintexture> wt, int xstart, int ystart, int w, int h) : texture(), // 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)); }
edit currently, have created class 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); };
i can't figure out how implement second constructor, copy part of input texture (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 assign values dc? bitblt(hdcmem1, 32, 32, w, h, hdcmem2, xstart, ystart, srccopy); deletedc(hdcmem1); deletedc(hdcmem2); }
at least read things right now, basic intent load texture, create individual sprites copying 32x32 pixel pieces of texture individual sprites. unless intend manipulate sprites separate threads (which strikes me unlikely) i'd avoid doing that.
instead, i'd take note of couple of last parameters supply bitblt:
bitblt(hdcmem2, xstart, ystart, w, h,hdcmem1, 0, 0, srccopy);
the 0, 0
before srccopy
specify location in source bitmap use starting point of bitblt
.
this lets load texture once, , use single texture sprites contains. drawing individual sprite @ particular location requires specify x, y coordinates of sprite within source bitmap (specifically, top, left-hand corner of sprite) , draw 32x32 chunk starting there. if want define individual sprite objects, can that, each needs store shared_ptr
loaded texture, , x, y coordinates of piece of texture.
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); } };
Comments
Post a Comment