PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Graphics and "IF" statements...

Hi all, I've an interesting problem....

I've a game Im developing, it consists of a background image, then 2 other levels of graphics placed on top of it..... semi background items, tree etc (layer 1), then foreground items (layer 2). the background image is just stored in a buffer, while the other two layers are stored in 2-d arrays as they are larger than the screen, they contain "tile" numbers that are to be drawn in that location, what I currently do is copy all of the layer1 items (pics) to a large buffer and overlay it to the screen and then add layer2 item, the problem is that layer 1 contains transparent pixels, and can't be memcpy'ed to the screen buffer, as can the bg layer, same as layer 2. what I've done is copied the layer1 and layer 2 pics to a buffer to be drawn to the screen.... this is quicker, then use the following code to place the visual part of the "level" to the user. eg:

Code:

void draw_items()
{
for (int ly = 0; ly < 300; ly++)
{
memcpy(screen_buffer_p+(240 * ly),levelbg_scr_p+(level.sizex * level.tile_size * (ly + level.tile_size * player.viewy) + player.viewx * level.tile_size),480);
}
}


but this does not draw them trans, since they have all been copied to the buffer previously (level setup code), the trans color is always black, is the a simple bit of code to draw this buffer to the screen with trans (ignoring the black) without if's, or fast.....
you can't go around without if's. Althrought there's a way to minimalize the amount of ifs. Example is simple RLE compressing of alpha channel. What you do is keep numbers in your pictures meaning how many transparent/non-transparent are following. This way you can make one if check for many pixels.
If im using tile sets, I suppose I can have reserve 1-100 non-trans, 101-200 trans etc.....

-bummer....
what kind of tiles you're using?
if 3d-iso you can simple store your tiles without the transparent part (saves memory) and have a special case bitmap draw function that will know sth like:
go 14 pixels right(on screen)
copy 2 pixels
go 1 pixel down, 3 pixels left
copy 4 pixels
etc etc
so what you do is you draw only pixels that are on your tile (if you tile can have alpha-blended/transparent tiles you still have to use the if statement to check for it), but basically you don't waste time/memory to keep empty pixels around your tile.
So example code might look something like this(tile is 5x5 pixels - with 1 non-transparent pixel in first line, 3 in second, 5 in third, 4 in fourth and 5 in fifth line.
source-bitmap
dest-screen
x,y-starting of tile
Code:

dest=y*240+x+2; //we move 2 pixels right
*dest=*source;//we copy one pixel
source++;
dest+=240-2;//we go one line down, two pixels left
*dest=*source;//we copy three pixels
dest++;
source++;
*dest=*source;
dest++;
source++;
*dest=*source;
source++;
dest+=240-4;//we go one line down, four pixels left
//we copy five pixels
etc etc (got bored;)


hope it helps...
btw, great work Dan, your code generator is awesome!
Mirek,

Why read from the source at all then? Why not just use immediate data and write it to the frame buffer?
sorry, but I don't understand, immediate data??? what's that? my poor english...
what I were pointing to is that source is a tile data without empty spaces (for 3d iso tiles etc) and dest is backbuffer/frontbuffer/whatever he feels it should go to... so what's wrong with it???


or do you mean using precompiled code like this:
*dest=20;
dest++;
*dest=30;
etc?
well I don't know how to code it propertly in C(so you can load images from files etc) and it takes more memory to store. (and I would guess the speedup isn't really worth it most of the time)
Mirek,

You understood me correctly. I was referring to writing hard-coded values into the frame buffer rather than reading them from another buffer (sprite). This is also known as "pre-compiled sprites." I'm not a fan of using this scheme, but I thought I'd throw out the idea for discussion.
brendan · 09 Feb 2002, 03:13 UTC — mmmm.
well currently I've got 64 background tiles, and i'm sure im going to have over 200 by the time I finish, sorry, I can't see myself hand coding 200+ tiles :)

-brendan
I agree with you, I can't see anyone doing this either for a large number of tiles. Unless all of your tiles shared the same transparent pixels, then even what Mirek suggested is going to be a lot of work.
Brendan,

The idea with "compiled sprites" is not to compile them yourself. You write a simple generator, give it your tiles, and it'll give you the code to plot each individual tile.

However, from my point of view, they have more disadvantages than advantages. The generated code will be large (between 1/4 and 3 instructions per *pixel*).. not fun .. say an average pixel takes 2 instructions (a MOV and a STRH)

32*32 = 1024 pixels
1024 * 4 * 2 = 8192
= 8KB/tile..

which is going to *shaft* the cache.

The afore-mentioned RLE scheme would/will probably be the one I'd go for, certainly for partially-transparent tiles.

Another scheme that would work would be to store a mask bit for each pixel in the tile, and use a bitwise AND and OR with the screen data to plot it... the only disadvantage is that you have to load what you've already written to the screen when you plot (but if you're writing in scanlines then it should be in the cache). Obviously it's not as space-efficient as the RLE method.

I always strive for a zero-overdraw method, but for some tile-based engines it simply isn't efficient (because blasting entire tiles to the screen and overdrawing is faster than scan-lining the tiles, I reckon ... it's less complicated, certainly :-D ).

Sorry, I'm waffling again,

Refractor.
digby:nah, why do you think my code will be a lot of work? It's pretty simple. Both the loader and bitmap drawer can be written in 15-30mins.
And you get a nice speedboost (no need for if's and only ~half the amount of pixels is drawn) and save some memory too.
The RLE alpha-compression is tricky. It's hard to code and for pure tiles the algorith above is better. But for objects over the 3d-iso tilemap (and all sprites with a decent amount of fully transparent pixels) it's a huge speed gain.
My 80x86 version works often FEW TIMES FASTER then a normal if version. It often works much faster then a normal bitmap copy(without transparency) because of all the data you DON'T HAVE to copy.
If anyone is interested I can present both my 80x86 asm code and a general approach used in my code(so you can do your own RLE sprites, but as said, it's not easy to implement)
Mirek,

I obviously missed the point of your post then. The code you posted show you adding hard-coded offsets so that you skip over the transparent pixels. That, to me at least, means that the code is going to always skip in that pattern for whatever source image you use. It seems like what you want to do is encode these offsets into the source image (as in RLE) so that you won't be using hard-coded offsets in your code. Again, I'm just looking at the code you posted and making an observation. Perhaps the code doesn't fully reflect what you are proposing?
Mirek,

I obviously missed the point of your post then. The code you posted show you adding hard-coded offsets so that you skip over the transparent pixels. That would have to be determined at compile-time and would not be calculated from the transparent areas of a sprite at run-time. Please enlighten me.
Digby,

There are two "issues" being dealt with here (3d-iso tiles, with a fixed transparency, and "standard" square/rectangular RLE tile).

Mirek's first post with the hard-coded transparency layout was for "3d-iso" tiles, where all of the tiles will be the same, diamond, shape. A "compiled" plotter as Mirek suggested makes good sense for that kind of tile, which always has the same transparent pattern, but different data (it was a generic routine).

I think the discussion got confused because RLE was mentioned as well. Mirek wasn't meaning that the 3d-iso compiled sprite suggestion was using RLE.

Cheers,

Refractor.
yeah, refractor is right.
Ah, now I understand. Thanks for clearing that up for me.