Author Topic: What's wrong with this code?  (Read 6850 times)

0 Members and 1 Guest are viewing this topic.

Offline Scipi

  • Omni Kitten Meow~ =^ω^=
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1547
  • Rating: +192/-3
  • Meow :3
    • View Profile
    • ScipiSoftware
What's wrong with this code?
« on: June 20, 2011, 01:00:57 pm »
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.




Imma Cat! =^_^= :3 (It's an emoticon now!)
Spoiler For Things I find interesting:
Spoiler For AI Programming:
Spoiler For Shameless advertising:

Spoiler For OldSig:





Spoiler For IMPORTANT NEWS!:
Late last night, Quebec was invaded by a group calling themselves, "Omnimaga". Not much is known about these mysterious people except that they all carried calculators of some kind and they all seemed to converge on one house in particular. Experts estimate that the combined power of their fabled calculators is greater than all the worlds super computers put together. The group seems to be holding out in the home of a certain DJ_O, who the Omnimagians claim to be their founder. Such power has put the world at a standstill with everyone waiting to see what the Omnimagians will do...

Wait... This just in, the Omnimagians have sent the UN a list of demands that must be met or else the world will be "submitted to the wrath of Netham45's Lobster Army". Such demands include >9001 crates of peanuts, sacrificial blue lobsters, and a wide assortment of cherry flavored items. With such computing power stored in the hands of such people, we can only hope these demands are met.

In the wake of these events, we can only ask, Why? Why do these people make these demands, what caused them to gather, and what are their future plans...

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: What's wrong with this code?
« Reply #1 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
« Last Edit: June 20, 2011, 02:13:16 pm by nemo »


Offline Scipi

  • Omni Kitten Meow~ =^ω^=
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1547
  • Rating: +192/-3
  • Meow :3
    • View Profile
    • ScipiSoftware
Re: What's wrong with this code?
« Reply #2 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();
        }
« Last Edit: June 20, 2011, 02:36:07 pm by HOMER-16 »

Imma Cat! =^_^= :3 (It's an emoticon now!)
Spoiler For Things I find interesting:
Spoiler For AI Programming:
Spoiler For Shameless advertising:

Spoiler For OldSig:





Spoiler For IMPORTANT NEWS!:
Late last night, Quebec was invaded by a group calling themselves, "Omnimaga". Not much is known about these mysterious people except that they all carried calculators of some kind and they all seemed to converge on one house in particular. Experts estimate that the combined power of their fabled calculators is greater than all the worlds super computers put together. The group seems to be holding out in the home of a certain DJ_O, who the Omnimagians claim to be their founder. Such power has put the world at a standstill with everyone waiting to see what the Omnimagians will do...

Wait... This just in, the Omnimagians have sent the UN a list of demands that must be met or else the world will be "submitted to the wrath of Netham45's Lobster Army". Such demands include >9001 crates of peanuts, sacrificial blue lobsters, and a wide assortment of cherry flavored items. With such computing power stored in the hands of such people, we can only hope these demands are met.

In the wake of these events, we can only ask, Why? Why do these people make these demands, what caused them to gather, and what are their future plans...

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: What's wrong with this code?
« Reply #3 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))


Offline Scipi

  • Omni Kitten Meow~ =^ω^=
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1547
  • Rating: +192/-3
  • Meow :3
    • View Profile
    • ScipiSoftware
Re: What's wrong with this code?
« Reply #4 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.
« Last Edit: June 20, 2011, 03:06:08 pm by HOMER-16 »

Imma Cat! =^_^= :3 (It's an emoticon now!)
Spoiler For Things I find interesting:
Spoiler For AI Programming:
Spoiler For Shameless advertising:

Spoiler For OldSig:





Spoiler For IMPORTANT NEWS!:
Late last night, Quebec was invaded by a group calling themselves, "Omnimaga". Not much is known about these mysterious people except that they all carried calculators of some kind and they all seemed to converge on one house in particular. Experts estimate that the combined power of their fabled calculators is greater than all the worlds super computers put together. The group seems to be holding out in the home of a certain DJ_O, who the Omnimagians claim to be their founder. Such power has put the world at a standstill with everyone waiting to see what the Omnimagians will do...

Wait... This just in, the Omnimagians have sent the UN a list of demands that must be met or else the world will be "submitted to the wrath of Netham45's Lobster Army". Such demands include >9001 crates of peanuts, sacrificial blue lobsters, and a wide assortment of cherry flavored items. With such computing power stored in the hands of such people, we can only hope these demands are met.

In the wake of these events, we can only ask, Why? Why do these people make these demands, what caused them to gather, and what are their future plans...

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: What's wrong with this code?
« Reply #5 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))"


Offline Scipi

  • Omni Kitten Meow~ =^ω^=
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1547
  • Rating: +192/-3
  • Meow :3
    • View Profile
    • ScipiSoftware
Re: What's wrong with this code?
« Reply #6 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.

Imma Cat! =^_^= :3 (It's an emoticon now!)
Spoiler For Things I find interesting:
Spoiler For AI Programming:
Spoiler For Shameless advertising:

Spoiler For OldSig:





Spoiler For IMPORTANT NEWS!:
Late last night, Quebec was invaded by a group calling themselves, "Omnimaga". Not much is known about these mysterious people except that they all carried calculators of some kind and they all seemed to converge on one house in particular. Experts estimate that the combined power of their fabled calculators is greater than all the worlds super computers put together. The group seems to be holding out in the home of a certain DJ_O, who the Omnimagians claim to be their founder. Such power has put the world at a standstill with everyone waiting to see what the Omnimagians will do...

Wait... This just in, the Omnimagians have sent the UN a list of demands that must be met or else the world will be "submitted to the wrath of Netham45's Lobster Army". Such demands include >9001 crates of peanuts, sacrificial blue lobsters, and a wide assortment of cherry flavored items. With such computing power stored in the hands of such people, we can only hope these demands are met.

In the wake of these events, we can only ask, Why? Why do these people make these demands, what caused them to gather, and what are their future plans...

Ashbad

  • Guest
Re: What's wrong with this code?
« Reply #7 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.

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: What's wrong with this code?
« Reply #8 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.


Offline Scipi

  • Omni Kitten Meow~ =^ω^=
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1547
  • Rating: +192/-3
  • Meow :3
    • View Profile
    • ScipiSoftware
Re: What's wrong with this code?
« Reply #9 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.

Imma Cat! =^_^= :3 (It's an emoticon now!)
Spoiler For Things I find interesting:
Spoiler For AI Programming:
Spoiler For Shameless advertising:

Spoiler For OldSig:





Spoiler For IMPORTANT NEWS!:
Late last night, Quebec was invaded by a group calling themselves, "Omnimaga". Not much is known about these mysterious people except that they all carried calculators of some kind and they all seemed to converge on one house in particular. Experts estimate that the combined power of their fabled calculators is greater than all the worlds super computers put together. The group seems to be holding out in the home of a certain DJ_O, who the Omnimagians claim to be their founder. Such power has put the world at a standstill with everyone waiting to see what the Omnimagians will do...

Wait... This just in, the Omnimagians have sent the UN a list of demands that must be met or else the world will be "submitted to the wrath of Netham45's Lobster Army". Such demands include >9001 crates of peanuts, sacrificial blue lobsters, and a wide assortment of cherry flavored items. With such computing power stored in the hands of such people, we can only hope these demands are met.

In the wake of these events, we can only ask, Why? Why do these people make these demands, what caused them to gather, and what are their future plans...

Offline fb39ca4

  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1749
  • Rating: +60/-3
    • View Profile
Re: What's wrong with this code?
« Reply #10 on: June 20, 2011, 04:13:25 pm »
I thought char was 1 byte, int was 2, and long was 4 ???

Ashbad

  • Guest
Re: What's wrong with this code?
« Reply #11 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 ;)

Offline Scipi

  • Omni Kitten Meow~ =^ω^=
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1547
  • Rating: +192/-3
  • Meow :3
    • View Profile
    • ScipiSoftware
Re: What's wrong with this code?
« Reply #12 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

Imma Cat! =^_^= :3 (It's an emoticon now!)
Spoiler For Things I find interesting:
Spoiler For AI Programming:
Spoiler For Shameless advertising:

Spoiler For OldSig:





Spoiler For IMPORTANT NEWS!:
Late last night, Quebec was invaded by a group calling themselves, "Omnimaga". Not much is known about these mysterious people except that they all carried calculators of some kind and they all seemed to converge on one house in particular. Experts estimate that the combined power of their fabled calculators is greater than all the worlds super computers put together. The group seems to be holding out in the home of a certain DJ_O, who the Omnimagians claim to be their founder. Such power has put the world at a standstill with everyone waiting to see what the Omnimagians will do...

Wait... This just in, the Omnimagians have sent the UN a list of demands that must be met or else the world will be "submitted to the wrath of Netham45's Lobster Army". Such demands include >9001 crates of peanuts, sacrificial blue lobsters, and a wide assortment of cherry flavored items. With such computing power stored in the hands of such people, we can only hope these demands are met.

In the wake of these events, we can only ask, Why? Why do these people make these demands, what caused them to gather, and what are their future plans...

Offline nemo

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1203
  • Rating: +95/-11
    • View Profile
Re: What's wrong with this code?
« Reply #13 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.


Ashbad

  • Guest
Re: What's wrong with this code?
« Reply #14 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)