Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: Scipi on June 20, 2011, 01:00:57 pm

Title: What's wrong with this code?
Post by: Scipi on June 20, 2011, 01:00:57 pm
http://pastebin.com/eZmfPZMK (http://pastebin.com/eZmfPZMK)

I think the root reason is because I am using memblocks but when I try to "draw" using this, it creates a mirror image of what I draw and when I look at the map file it generates in a hex editor it's all weird and wrong.

Here's what it draws when I make an "X" with the two brushes.


(http://dl.dropbox.com/u/10573921/tile_map_error.PNG)
Title: Re: What's wrong with this code?
Post by: nemo on June 20, 2011, 02:12:21 pm
well, for one, this code segment doesn't work:
Code: [Select]
memblock = new char [size];
                memblock[0] = x;
                memblock[1] = y;
                for(int i = 0; i<size; i++)
                {
                        memblock[i+2] = *(array + i);
                }
it writes over memblock[size] and memblock[size + 1] when i don't think it should.
then again i code in Java, not C/C++.

if that doesn't fix anything i'll look over the rest of the code.

edit: to clarify, this is in the SaveFile method
Title: Re: What's wrong with this code?
Post by: Scipi on June 20, 2011, 02:18:30 pm
No that's for saving the map. memblock[0] and memblock[1] are for map size in tiles.

Also,
Code: [Select]
for(int i = 0; i<size; i++)
                {
                        memblock[i+2] = *(array + i);
                }

Writing begins at memblock[2]

Saving and loading work fine though, oddly enough provided the hex in the file is messed up.

This is the write code into the memblock:

Code: [Select]
if (Input.IsMouseButtonDown(sf::Mouse::Left) || Input.IsMouseButtonDown(sf::Mouse::Right))
                {
                        sf::Vector2f MousePos=App.ConvertCoords(Input.GetMouseX(), Input.GetMouseY());
                        x=MousePos.x/64;
                        y=MousePos.y/64;
                        if (x<0)
                                x=0;
                        if (y<0)
                                y=0;
                        if (x>xx-1)
                                x=xx-1;
                        if (y>yy-1)
                                y=yy-1;
                        if (Input.IsMouseButtonDown(sf::Mouse::Left))
                                *(array + x + (x * y))=selectedimage;
                        else if (Input.IsMouseButtonDown(sf::Mouse::Right))
                                *(array + x + (x * y))=selectedimage2;
                }
 

And this reads from the memblock to display:

Code: [Select]
                // Draw stuff on main window
                for (y=0; y<yy; y++)
                {
                        for (x=0; x<xx; x++)
                        {
                                if (*(array + x + (x * y))==-1)
                                        continue;
                                imgs[*(array + x + (x * y))].SetPosition(x*64, y*64);
 
                                App.Draw(imgs[*(array + x + (x * y))]);
                        }
                }
 
                // Draw stuff on the image list
                for (a=0; a<numofimgs; a++)
                {
                        imgs[a].SetPosition(0, a*64);
                        App2.Draw(imgs[a]);
                }
                Selection.SetPosition(0,64*selectedimage);
                App2.Draw(Selection);
 
                Selection2.SetPosition(0,64*selectedimage2);
                App2.Draw(Selection2);
 
                // Update the window
                App.Display();
                App2.Display();
        }
Title: Re: What's wrong with this code?
Post by: nemo on June 20, 2011, 02:34:12 pm
i thought memblock[0] and memblock[1] are for the size? and that code segment starts writing at memblock[2] not memblock[3].

regardless, the code i cited in my first post has an issue, memblock[x * y + 2] and memblock[x * y + 3] are written to when they shouldn't be.

and
Code: [Select]
*(array + x + (x * y))
should be
Code: [Select]
*(array + x + (x * yy))

Title: Re: What's wrong with this code?
Post by: Scipi on June 20, 2011, 03:05:03 pm
Yes memblock[0] and [1] are size. My bad. :P

Quote
memblock[x * y + 2] and memblock[x * y + 3] are written to when they shouldn't be.

Ok I see, yeah that might be a problem. :)

Quote
Code: [Select]
*(array + x + (x * y))should be
Code: [Select]
*(array + x + (x * yy))

I see where you're going with this and I see how it is causing the errors. But it's crashing every time I input the map size and it "initializes" the map.
Title: Re: What's wrong with this code?
Post by: nemo on June 20, 2011, 03:13:41 pm
i've never used pointers in C/C++ (the only thing i know is & and * are related) but this code looks kinda weird:

Code: [Select]
char * tiles;
        tiles = new char [xx * yy]; // this array will store the image number for each tile
        char * array = &tiles[0];

are you sure that works? why not just do this?
Code: [Select]
char * array;
        array = new char [xx * yy]; // this array will store the image number for each tile

i may be misunderstanding pointers and C++ though but it seems like you have two pointers to the same map.

and again the "*(array + x + (x * y))" in the initializing part needs to be changed to "*(array + x + (x * yy))"
Title: Re: What's wrong with this code?
Post by: Scipi on June 20, 2011, 03:41:50 pm
I think I use tiles as the memblock and array as the pointer. &tiles[0] gets me the address of where the memblock begins. This is to pass the address as an argument to the save() and load() functions.

Quote
"*(array + x + (x * y))" in the initializing part needs to be changed to "*(array + x + (x * yy))"

I did that and it crashed at that part, I also changed it to *(array + x + (y * yy)) afterwards, which I think is actually the correct expression in this case.

I could try to use tiles[x + (y * yy)] instead though and see if that works.
Title: Re: What's wrong with this code?
Post by: Ashbad on June 20, 2011, 03:45:22 pm
He does have two pointers to the same character array.  & returns the address of the said object, and setting the char*array pointer to point to the address of the first element of an array just makes to references to the same chunk of data. 

Also, no idea if this is causing any problems, but when you're working with 8 bit chars, make sure everything that stores to it is also a char or at least gets truncated correctly, in some cases it can really mess things up.
Title: Re: What's wrong with this code?
Post by: nemo on June 20, 2011, 04:06:24 pm
yes, *(array + x + (y * yy)) is the right expression. you should rename your variables for clarity.

also, i just googled that the size of char in c++ is 1 byte, while int is 4 bytes. i think that ashbad is right; you're putting integers into character spaces.
Title: Re: What's wrong with this code?
Post by: Scipi on June 20, 2011, 04:10:03 pm
chars work for small ints from -127 - 128 or 0 - 255 no ints go above or below that, or they shouldn't.
Title: Re: What's wrong with this code?
Post by: fb39ca4 on June 20, 2011, 04:13:25 pm
I thought char was 1 byte, int was 2, and long was 4 ???
Title: Re: What's wrong with this code?
Post by: Ashbad on June 20, 2011, 04:14:31 pm
I thought char was 1 byte, int was 2, and long was 4 ???

Char is 1, short is 2, int is 4, long long int is 8 ;)
Title: Re: What's wrong with this code?
Post by: Scipi on June 20, 2011, 04:15:37 pm
I thought char was 1 byte, int was 2, and long was 4 ???

int and long int are the same. short int is 2 bytes

EDIT: ninja'd
Title: Re: What's wrong with this code?
Post by: nemo on June 20, 2011, 04:16:43 pm
chars work for small ints from -127 - 128 or 0 - 255 no ints go above or below that, or they shouldn't.

you could try declaring your char arrays signed to make sure -1 won't mess them up.
Title: Re: What's wrong with this code?
Post by: Ashbad on June 20, 2011, 04:18:32 pm
Or, if size isn't an issue, do an array of ints; or, just do input_int >> 24 first ;) (though it's not quite just that simple)
Title: Re: What's wrong with this code?
Post by: Scipi on June 20, 2011, 04:25:20 pm
chars work for small ints from -127 - 128 or 0 - 255 no ints go above or below that, or they shouldn't.

you could try declaring your char arrays signed to make sure -1 won't mess them up.

I tried and it didn't work. This wasn't giving me a problem before just it was doing weird things with the input.

I'll try int.

EDIT: I figured out what was making it crash, it wasn't initializing correctly! When I changed the "read" code I didn't change the initialize code to use the same expression!

Now all that's left is to fix a new drawing glitch which now has it drawing at wrong squares. But that should be fixed easily. ^^

EDIT 2: Fixed the draw glitch, same thing except with input code. ^^' The program is now working perfectly thank you guys for being such a great help!