PocketMatrix forums

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

Coding "Issues" I've no idea!!!!

Hi All I've the following code for taking a small "tile" from a larger graphic and placing it to the screen:

Code:

void place_item(int destsizex, int xpos, int ypos, int xsize, int ysize, int number_itemsx, int item, PIXEL* src, PIXEL* dest)
{
unsigned int dest_loc = (destsizex * ypos) + xpos;
unsigned int source_width = number_itemsx * xsize;
unsigned int source_loc = ((item / number_itemsx) * source_width * ysize) +
((item % number_itemsx) * xsize);

for (int y = 0; y < ysize; y++)
{
for (int x = 0; x < xsize; x++)
{
dest[dest_loc + x] = src[source_loc + x];
}
dest_loc += destsizex; // move down a row in dest
source_loc += source_width; // move down a row in source
}
}


the main picture is 15x5 tiles of 20x20px each

this code works..ish. the correct tile is pulled from the main graphic, and placed to the screen etc, BUT, it''s mmm um well wrong.... what I get is:

the first 5 rasters upside down, then the next 5 upside down, then next upside down etc...

so instead of the rasters looking like:

1
2
3
4
5
6
7
8
9
10

they look like

5
4
3
2
1
10
9
8
7
6

etc.... I't completly got me stumped..... given that its doing it in rasters of 5, I though it could somehow relate to the fact that the main image is 5 tiles in the y axis...anyhow HELP!
Conan · 21 Mar 2002, 07:01 UTC — what image format are you using
Hi Brendan, I have a similar issue where a tga file gives the appearance that it's layout is upside down. I cut little 16 x 16 chunks out for display & the first one is coming from the bottom left 16x16 of the image?
Anyway re: your problem.
1. What is the format of the source image ?
2. What happens if you expand the code to show the whole image but using the same loop? If you still get distortion then the original data could be the prob.

:!: See the small sprites post: is this a related problem?
Lewil · 25 Mar 2002, 12:30 UTC — *
i don't know if this has smthg to do with your probleme but be careful with the soft you use to write the .tga for example, Microsoft photo editor will write the tga upside down, photoshop won't...
Conan · 25 Mar 2002, 17:42 UTC — Upside down TGA
Many Thanks Lewil,

I wrote the TGA file with Paintshop pro. I will see what PhotoShop makes of the same file. And to think I spent hours looking at my code trying to figure what I'd done wrong :?
brendan · 08 Apr 2002, 23:28 UTC — It did work,.....
well I've been using photoshop, It used to work when I had all the tiles in one long image e.g 1300x20, then just pull the tile I wan't, that worked fine, except the program I'm using "tile studio" only creates tile sets in "blocks" rather then "strips", so you need to account for the y axis, thus adding the "mod (%)" information etc, and it's no longer working. ???

-Brendan
brendan · 14 Apr 2002, 23:51 UTC — Here's a copy!!!
here is a copy of the org. code.

Code:

void place_item(int destsizex, int xpos, int ypos, int xsize, int ysize, int number_items, int item, PIXEL* src, PIXEL* dest)

{
// These datatypes may need to be a long
unsigned int l_vertical_base;
unsigned int l_x_mult;
unsigned int l_xy_mult;

//moved multiplies out of inner loop
l_vertical_base = (destsizex * ypos) + xpos;
l_x_mult = (xsize*number_items);
l_xy_mult = (item * xsize);

for (int y = 0; y < ysize; y++)
{
for (int x = 0; x < xsize; x++)
{
dest[l_vertical_base + x] = src[l_xy_mult + x];
}

l_vertical_base += destsizex; // move down a row
l_xy_mult+=l_x_mult;

}
}


the other difference is the master graphics, it used to be like

123456789

where each number is a 20x20 image etc.

now:

123
456
789

-thanks in advance.
Not entirely sure what you're asking in your last post... but I thought I'd try and help and came up with the same code as you.

Assuming:

destsizex = x pitch of destination (pixels)
xpos, ypos = destination pixel pos'n for top left of bitmap
xsize, ysize = size of tile (pixels)
number_items = x pitch of source (*tiles*)
item = tile you want
src, dest = pointers to base address of source + destination

I came up with:

Code:
void place_item(int destsizex, int xpos, int ypos, int xsize, int ysize, int number_items, int item, PIXEL* src, PIXEL* dest)

{

unsigned long l_dest_base;
unsigned long l_src_base;
unsigned long l_src_step;

// Calculate the offset into the destination
l_dest_base = (destsizex * ypos) + xpos;

// Calculate the offset into the source (converting from TILE to PIXEL along the way)
l_src_base = ((ysize*xsize*number_items) * (item/number_items)) + (xsize * (item%number_items));

// Calculate the pixel length of a tile row
l_src_step = (xsize * number_items);

// step through x+y of source/dest
for (int y = 0; y < ysize; y++)
{
for (int x = 0; x < xsize; x++)
{
dest[l_dest_base + x] = src[l_src_base + x];
}
l_dest_base+=destsizex;
l_src_base +=l_src_step;
}

// end
}


Untested, uncompiled, "naive", and I'll leave optimisation for later, too :) - looking at it, it's very similar/identical to your first code-post - if it doesn't work then your data source isn't how you expect it to be (or I've screwed up somewhere too :wink: ).

Oh, and the sprites ("item" should start at 0, not 1. For example:

0123
4567

not

1234
5678
)

Cheers,

Refractor
refractor wrote:
Not entirely sure what you're asking in your last post... but I thought I'd try and help and came up with the same code as you.


I was just "trying to say" this is the code that worked before when the tiles were in a graphic that was only 1 tile in the y axis (they were all in a strip, line etc), but since tile studio creates the tiles in the graphic in both multiples of the x and y axis'......
brendan · 16 Apr 2002, 00:30 UTC — Same issues
well your code "worked" identically to mine, same problem.... I think the only way to fix it is to use bmp's of somthing instead..... ? but it is wierd that it worked when the tiles were all in one long strip ?

-Thanks

P.S anyone got some BMP or JPG or somthing code ?
In responce to the question does the error (problem) still occur if you try and place a larger section from the tile graphic to the screen, Yes.

I used the same source graphic, and rather than taking a 20x20 tile, I took a 100x100 tile, same problem....

I't would be nice to get a screen shot of this, but I'm unsure as to how to disable easyce from taking over the buttons, so I cant get IAscreenshot to work :(

Brendan
brendan · 16 Apr 2002, 02:41 UTC — Thanks all :)
I've fixed it.....it was an um err error in my code to place the code into the allocated mem.... :) Thanks for all the help, BTW I tried photoshop, and interestingly the TGA file written out was slightly bigger than photoshop, but both work ?

-Brendan
Village_Ideot · 16 Apr 2002, 03:08 UTC
thatwas a large amount of code
Adobe Photoshop seems to like putting extra header information in the filetypes that it can (especially JPEGs). You can get little utilities to strip the unnecessary header information out of the files. You won't gain huge amounts, but you usually gain.

Cheers,

Refractor