Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » What is this exactly? (BITMAP related)

This thread is locked; no one can reply to it. rss feed Print
 1   2 
What is this exactly? (BITMAP related)
julian_boolean
Member #8,201
January 2007

I was searching around for posts that discuss movement and collision using Mappy and found this: http://www.allegro.cc/forums/thread/588658

Inside I found some code and this is what I'm trying to understand:

BITMAP *grabframe(BITMAP *source, int width, int height, int startx, int starty, int columns, int frame)
{
    BITMAP *temp = create_bitmap(width,height);

    int x = startx + (frame % columns) * width;
    int y = starty + (frame / columns) * height;
    
    masked_blit(source,temp,x,y,0,0,width,height);

    return temp;
}

I mean, I KNOW what it's suppose to do.. But, what the hell? Is it some kind of bitmap function? I was trying to use this particular piece of code (in C++), but it's kinda sketchy and I'm getting weird errors like undefined reference to vtable.

Suppose I should also mention I'm trying to use it in a class.

Kris Asick
Member #1,424
July 2001

It returns a pointer to a freshly created bitmap using the following arguments:

<b>BITMAP source
This is a single BITMAP which acts as a sheet of sprites you want to grab a sprite from.

int width, int height
The size of each sprite on the sprite sheet. Each sprite on the sheet should be the exact same size and arranged in a grid.

int startx, int starty
Almost pointless. The top left corner of the grid. Most of the time this will be 0,0.

int columns
The number of sprites there are in a single row of sprites on the sprite sheet. For instance, if the grid has 80 sprites, arranged in rows of 10, you would use the number 10 here.

int frame
The sprite number to grab, from left to right then top to bottom. So given a 10x8 grid of sprites, sprite number 23 would be the fourth sprite in the third row. In a 7x15 grid, 23 would be the third sprite in the fourth row.

Since this routine actually creates the BITMAP object, you must store the result into a BITMAP pointer that hasn't been initialized, or is NULL.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Elverion
Member #6,239
September 2005
avatar

An "undefined reference to vtable" error generally means you haven't defined a function for a baseclass or one of it's children. The error has to do with your code in the class and not the function you posted, so it's hard to pinpoint with what we were given. If you are unable to find the error in your code, post the declaration and definition of your baseclass (and childrens') member functions, as well as the class itself.

--
SolarStrike Software - MicroMacro home - Automation software.

23yrold3yrold
Member #1,134
March 2001
avatar

Quote:

undefined reference to vtable

I can't imagine how the code you posted could possibly generate that ...

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Kibiz0r
Member #6,203
September 2005
avatar

23yrold3yrold said:

Quote:

undefined reference to vtable

I can't imagine how the code you posted could possibly generate that ...

julian_boolean said:

Suppose I should also mention I'm trying to use it in a class.

Quote:

Since this routine actually creates the BITMAP object, you must store the result into a BITMAP pointer that hasn't been initialized, or is NULL.

It's worth mentioning that you are also responsible for destroying the object Just making sure you don't forget to manage your memory properly. ;)

bamccaig
Member #7,536
July 2006
avatar

julian_boolean said:

BITMAP *temp = create_bitmap(width,height);

temp isn't a very good identifier for this variable. It's temporary in the sense that all local variables are temporary, but in the context of the function it actually stores the BITMAP of the frame you're grabbing.

Kris Asick said:

It returns a pointer to a freshly created bitmap...

Out of curiosity, is there an advantage to storing your frames as a sheet of sprites as opposed to an array of BITMAPs or self-defined FRAME objects? It seems that memory usage should be slightly less then if each frame had a separate BITMAP object.

Is the processing time of ripping frames from a sheet of sprites small enough to make it worthwhile getting them from a sheet every time you access them or is it better to store them as separate BITMAPs?

Or was that function designed to initialize an ANIMATION object with FRAME objects? :-/

???

ImLeftFooted
Member #3,935
October 2003
avatar

bamccaig
Member #7,536
July 2006
avatar

Dustin Dettmer said:

Ever heard of

create_sub_bitmap

create_sub_bitmap() sounds somewhat more appropriate for this function (maybe), however, my question still remains. Is it common/good practice to leave your sprites on a sheet-bitmap and rip them over and over again to save memory or is it better to rip them once into memory (when loading the game or level and reuse them)?

???:-/

Elverion
Member #6,239
September 2005
avatar

It doesn't mater much how you load them, but a sprite sheet is generally easier to work with. You should load all sprites at the beginning of the game, and not unload them until game exit, unless you're making a really big game and targeting older systems.

--
SolarStrike Software - MicroMacro home - Automation software.

julian_boolean
Member #8,201
January 2007

Thanks everyone, I think I'm going to try out create_sub_bitmap. I guess that's what this guy was trying to make.

m c
Member #5,337
December 2004
avatar

separate images is better.

the big bitmap is stored as a sequence of bits like so:

1478f92379e97397a7893478bc8976d987f

for example.

"image 1" (a box inside the bigger image) might be stored in

1478f92379e97397a7893478bc8976d987f...

whilest "image 2" (a different box inside the same bigger image) might be stored in

1478f92379e97397a7893478bc8976d987f....

etc.

If they are separate then each image is stored completely sequentially.

This probably has a very mimor performance improvement as the memory prefetch system gets more of what you want (as it has finite capability and is wasting less time).

That's about all there is to it. QED etc.

(\ /)
(O.o)
(> <)

Kibiz0r
Member #6,203
September 2005
avatar

Of course, you're using more HD space by keeping them separate. :P

But then there's the argument for taking them in as one big bitmap and splitting them up on initialization.

Tobias Dammers
Member #2,604
August 2002
avatar

Quote:

Out of curiosity, is there an advantage to storing your frames as a sheet of sprites as opposed to an array of BITMAPs or self-defined FRAME objects? It seems that memory usage should be slightly less then if each frame had a separate BITMAP object.

This question has been done to death on the forum. Bottom line is, it doesn't really matter that much. Sprite sheets are generally a bit easier to work with, especially if you use a sub-standard sprite editor (say, MS Paint), and provide a natural way of bundling related sprites together, while single frames are a tiny bit more efficient speed-wise and will probably produce slightly cleaner code. The differences, especially performance-wise, are really negligible, so you should really just use what you feel more comfortable with and enjoy. No need to ponder.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

julian_boolean
Member #8,201
January 2007

I'm not so sure if I can really use create_sub_bitmap. It seems to be missing some parameters that this other function has, namely int columns and int frame.

My sprite sheet (bmp) looks sorta like this (each number represents the frame):

// yeah.. please try to picture this as 40x80 sprites. hehe

n  n  n  n  n // north
0  1  2  3  4

e  e  e  e  e // east
5  6  7  8  9

s  s  s  s  s // south
10  11  12  13  14

w  w  w  w  w // west
15  16  17  18  19

Jakub Wasilewski
Member #3,653
June 2003
avatar

You would use it instead of this part:

    BITMAP *temp = create_bitmap(width,height);

    int x = startx + (frame % columns) * width;
    int y = starty + (frame / columns) * height;
    
    masked_blit(source,temp,x,y,0,0,width,height);

Like this:

    int x = startx + (frame % columns) * width;
    int y = starty + (frame / columns) * height;
    
    return create_sub_bitmap(source, x, y, width, height);

This way you can spare some memory space on those temporary bitmaps.

---------------------------
[ ChristmasHack! | My games ] :::: One CSS to style them all, One Javascript to script them, / One HTML to bring them all and in the browser bind them / In the Land of Fantasy where Standards mean something.

julian_boolean
Member #8,201
January 2007

Hm.. But, how would that work if those two variables aren't even being used?

Jakub Wasilewski
Member #3,653
June 2003
avatar

Err, they are being used. Specifically, they are being used for calculating the x and y a specific frame starts at. Once you know those, you have all the information about the frame (because the width and height is fixed), so you can create a subbitmap (look them up if you like) that represents the single frame.

---------------------------
[ ChristmasHack! | My games ] :::: One CSS to style them all, One Javascript to script them, / One HTML to bring them all and in the browser bind them / In the Land of Fantasy where Standards mean something.

julian_boolean
Member #8,201
January 2007

I'm trying to avoid using the whole BITMAP *grabframe function thing since I can't seem to get it to work properly. :-/

Edit:

What does "%" and "/" mean and do anyhow?

GullRaDriel
Member #3,861
September 2003
avatar

julian_boolean said:

What does "%" and "/" mean and do anyhow?

You really should start at the beginning. Learn coding.

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

julian_boolean
Member #8,201
January 2007

Um. Just because I don't know what those two mean, means I don't know how to code? If you don't have a helpful reply, please don't reply at all.

Edit:

NM asked someone else about it. Thanks for your help.

Thomas Fjellstrom
Member #476
June 2000
avatar

Quote:

Just because I don't know what those two mean, means I don't know how to code?

Nor do you know math :P They are absolutely fundamental operators in coding. If you don't know what they are, you've missed a TON of stuff.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

julian_boolean
Member #8,201
January 2007

I may have missed a thing or two along the way. :P I've just never used these before, or seen them being used.. Well until now.

Rampage
Member #3,035
December 2002
avatar

Quote:

What does "%" and "/" mean and do anyhow?

Sigged! ;D

-R

julian_boolean
Member #8,201
January 2007

Isn't there a bridge you should be guarding? Like I said, there's the odd thing I didn't learn when I started c++ (like half a year ago.)

Rampage
Member #3,035
December 2002
avatar

Quote:

Isn't there a bridge you should be guarding? Like I said, there's the odd thing I didn't learn when I started c++ (like half a year ago.)

No, and yes, everybody misses the basic arithmetic operators when learning C++.

-R

 1   2 


Go to: