![]() |
|
How to Copy-Construct Sub-Bitmaps |
ADmiral
Member #6,092
August 2005
|
How should i copy one sub-bitmap to another (unallocated) BITMAP * ? |
Jonny Cook
Member #4,055
November 2003
|
The docmuentation on blit said: Copies a rectangular area of the source bitmap to the destination bitmap.
The blit function allows you to blit individual parts of a bitmap. The face of a child can say it all, especially the mouth part of the face. |
ADmiral
Member #6,092
August 2005
|
Thanks for your reply. The problem is that I don't want to copy a bitmap with all it's pixel data, but a sub-bitmap, which I use for tiles to store their location in my tileset. The new sub-bitmap needs to have all the attributes of the original sub-bitmap. Edit:I wrote a little sample program which copies a sub-bitmap using simple, ugly memcpy() and it worked. Thus, hardly surprising, I'm still worried about it. Am I doing the right thing? Will it work in a bigger program with lots of memory allocation / deallocation taking place between those acrobatic actions? If you are interested, here is my testing code:
Any suggestions on a better / more reliable solution? |
Arthur Kalliokoski
Second in Command
February 2005
![]() |
Can't you just create the new bitmap from the sub bitmap height & width? Then blit the sub bitmap to it. A sub bitmap is pretty much like a normal one, the only difference here would be the creation. They all watch too much MSNBC... they get ideas. |
ADmiral
Member #6,092
August 2005
|
Of course I could, but the point is that I don't want to. Look at this piece of code:
Matrix is pretty self-explanatory: It is my 2-dimensional array class.
Don't care about the Bitmap class; It has proven to work perfectly and the only thing you might want to know is that Bitmap::get_raw_bmp() returns the Allegro BITMAP *. And I have tested my project using the memcpy() method. It crashes while copy-constructing a Bitmap from a SubBitmap which was copy-constructed from a SubBitmap which was then deleted (before the Bitmap(SubBitmap) c'tor call). Thus, I really need a way to copy the sub-bitmap itself and not the pixels I can access with it. Additional note: Layer is a map layer which contains Tiles; The vector called layers in Tile class is a vector of the Bitmaps used by a single Tile. |
Arthur Kalliokoski
Second in Command
February 2005
![]() |
OK, I see what you're doing now. I've been paid a midnight visit by the Allegro "Be Good" Mafia after I accessed the bitmap data directly, so I just use Allegro to read in pixels from standard bmp, pcx, tga, whatever and write them to my own proprietary file formats now. Of course I'm not using Allegro to develop a game per se, more like a handy toolbox to make windows specific games. They all watch too much MSNBC... they get ideas. |
CGamesPlay
Member #2,559
July 2002
![]() |
I can't find any way to do this short of memcpy. I think your problem may have been you didn't copy the line data, though I can't be sure. -- Ryan Patterson - <http://cgamesplay.com/> |
Steve++
Member #1,816
January 2002
|
Quote: Thus, I really need a way to copy the sub-bitmap itself and not the pixels I can access with it. I'm not quite sure what you mean by this. Also, I think your use of inheritance here is flawed. Can you really say a sub-bitmap is a bitmap? Sure, you can blit them both, but a bitmap owns a raw bitmap and a sub-bitmap doesn't. The relationship may be better represented by association. A bitmap could be some sort of container that holds a collection of sub-bitmaps. Then it would make sense that when you delete a bitmap, its sub-bitmaps are also deleted. Think of what you do with bitmaps and what you do with sub-bitmaps. If you don't use them seemlessly interchangably, then there's nothing to be gained by inheritance in this case. EDIT: If you do use them seemlessly interchangibly, but you agree that their relationship isn't one of inheritance, you could create an abstract class with only pure virtual functions (an interface), then have both classes inherit from that abstract class and implement its interface. But whatever you do, if you're going to put a sub-bitmap container in your bitmap class, don't let it contain objects of the abstract type. That's just asking for a big mess. |
ADmiral
Member #6,092
August 2005
|
The concept problem is one which I have considered negligible, since I didn't want to spend even more time on my Bitmap classes but move forward instead. It works, and the point at which I should have fixed the problem was when I designed the classes. Back then I had read the description of sub-bitmaps in the manual, but I didn't know how they were implemented. If I were to change them, I would go for the abstract base class approach. Also, this still doesn't answer my question. Maybe Allegro is missing a create_sub_bitmap_copy() function?! |
Steve++
Member #1,816
January 2002
|
I looked into the Allegro source code. If you create a sub-bitmap of a sub-bitmap being the same size, there may be some differences in the BITMAP struct, namely the x_ofs and y_ofs values. Probably some other stuff too. Perhaps you need to store a reference to the parent BITMAP in the SubBitmap class. Quote: The concept problem is one which I have considered negligible, since I didn't want to spend even more time on my Bitmap classes but move forward instead. That could be a mistake. The sooner you fix a problem, the less time it will take to fix it. Put it off now and it will really kick you a$$ later. |
ADmiral
Member #6,092
August 2005
|
I have tried the sub-sub-bitmap method, and it plain works. I don't care about offset values. I just hope there's no pointers to the first sub-bitmap hanging around anywhere in the second sub-bitmap, but blitting can be done and the copy constructor does what it is supposed to do. |
Thomas Fjellstrom
Member #476
June 2000
![]() |
Steve++, afaik creating a sub bitmap of a sub bitmap actually just creates a sub bitmap of the original bitmap. Sub Bitmaps store the original bitmap, and the creation code uses that instead. -- |
Steve++
Member #1,816
January 2002
|
Thanks for the cookie! Quote: Steve++, afaik creating a sub bitmap of a sub bitmap actually just creates a sub bitmap of the original bitmap. Sub Bitmaps store the original bitmap, and the creation code uses that instead. Yeah, I was wondering if that could be the case. That would mean the line[] array in a sub-bitmap is exactly the same as its host, and the offset values are used in blits, right? If so, it would be a very bad idea to access the line[] array of a sub-bitmap directly. |
Thomas Fjellstrom
Member #476
June 2000
![]() |
I assume the line[] array is setup as it should be, offset by the position you gave it. -- |
|