PocketMatrix forums

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

Doozy of a problem ..anyone can help?(arrays of classes etc)

Okay so im happily plodding along in my coding and as some have may followed im using my Nifty Sprite class:
http://www.zianet.com/codejoy/NSprite.cpp
http://www.zianet.com/codejoy/NSprite.h

and it seems to be working great, I have my main character running around on the screen and grabbing pieces.

The pieces he grabs is defined in my main application header file as:
...
NSprite m_pieces[4];
NSprite m_playerpieces[4];
since there are 4 different types of pieces (red, blue, green, purple);

I then in my init code using the above methods from the sprite class I proceed to create the sprite, and set the proper frame (single frame) of animation:
Code:

m_piece[RED].SetTransColor(0,0,0);
m_piece[RED].Create_Sprite(0, 0, 16, 16, 1, SPRITE_ATTR_SINGLE_FRAME | SPRITE_ATTR_VISIBLE, GDSURFACE_SYSTEMMEMORY, 0, 16, hInstance, IDB_CHIPS, NULL);
m_piece[RED].Load_Frame_Sprite(0, 0, 0, BITMAP_EXTRACT_MODE_CELL);


m_piece[BLUE].SetTransColor(0,0,0);
m_piece[BLUE].Create_Sprite(0, 0, 16, 16, 1, SPRITE_ATTR_SINGLE_FRAME | SPRITE_ATTR_VISIBLE, GDSURFACE_SYSTEMMEMORY, 0, 16, hInstance, IDB_CHIPS, NULL);
m_piece[BLUE].Load_Frame_Sprite(0, 1, 0, BITMAP_EXTRACT_MODE_CELL);

m_piece[GREEN].SetTransColor(0,0,0);
m_piece[GREEN].Create_Sprite(0, 0, 16, 16, 1, SPRITE_ATTR_SINGLE_FRAME | SPRITE_ATTR_VISIBLE, GDSURFACE_SYSTEMMEMORY, 0, 16, hInstance, IDB_CHIPS, NULL);
m_piece[GREEN].Load_Frame_Sprite(0, 2, 0, BITMAP_EXTRACT_MODE_CELL);

m_piece[PURPLE].SetTransColor(0,0,0);
m_piece[PURPLE].Create_Sprite(0, 0, 16, 16, 1, SPRITE_ATTR_SINGLE_FRAME | SPRITE_ATTR_VISIBLE, GDSURFACE_SYSTEMMEMORY, 0, 16, hInstance, IDB_CHIPS, NULL);
m_piece[PURPLE].Load_Frame_Sprite(0, 3, 0, BITMAP_EXTRACT_MODE_CELL);

m_piece[SRED].SetTransColor(0,0,0);
m_piece[SRED].Create_Sprite(0, 0, 16, 16, 1, SPRITE_ATTR_SINGLE_FRAME | SPRITE_ATTR_VISIBLE, GDSURFACE_SYSTEMMEMORY, 0, 16, hInstance, IDB_CHIPS, NULL);
m_piece[SRED].Load_Frame_Sprite(0, 0, 0, BITMAP_EXTRACT_MODE_CELL);




//frame: 0 1 2 3
// left right down up
m_playerPiece[RED].SetTransColor(0,0,0);
m_playerPiece[RED].Create_Sprite(8*16, 8*16, 16, 16, 4, SPRITE_ATTR_MULTI_FRAME | SPRITE_ATTR_VISIBLE, GDSURFACE_SYSTEMMEMORY, 0, 16, hInstance, IDB_PLAYERPIECES, NULL);
m_playerPiece[RED].Load_Frame_Sprite(0, 0, 0, BITMAP_EXTRACT_MODE_CELL);
m_playerPiece[RED].Load_Frame_Sprite(1, 1, 0, BITMAP_EXTRACT_MODE_CELL);
m_playerPiece[RED].Load_Frame_Sprite(2, 2, 0, BITMAP_EXTRACT_MODE_CELL);
m_playerPiece[RED].Load_Frame_Sprite(3, 3, 0, BITMAP_EXTRACT_MODE_CELL);



m_playerPiece[BLUE].SetTransColor(0,0,0);
m_playerPiece[BLUE].Create_Sprite(8*16, 8*16, 16, 16, 4, SPRITE_ATTR_MULTI_FRAME | SPRITE_ATTR_VISIBLE, GDSURFACE_SYSTEMMEMORY, 0, 16, hInstance, IDB_PLAYERPIECES, NULL);
m_playerPiece[BLUE].Load_Frame_Sprite(0, 0, 1, BITMAP_EXTRACT_MODE_CELL);
m_playerPiece[BLUE].Load_Frame_Sprite(1, 1, 1, BITMAP_EXTRACT_MODE_CELL);
m_playerPiece[BLUE].Load_Frame_Sprite(2, 2, 1, BITMAP_EXTRACT_MODE_CELL);
m_playerPiece[BLUE].Load_Frame_Sprite(3, 3, 1, BITMAP_EXTRACT_MODE_CELL);



m_playerPiece[GREEN].SetTransColor(0,0,0);
m_playerPiece[GREEN].Create_Sprite(8*16, 8*16, 16, 16, 4, SPRITE_ATTR_MULTI_FRAME | SPRITE_ATTR_VISIBLE, GDSURFACE_SYSTEMMEMORY, 0, 16, hInstance, IDB_PLAYERPIECES, NULL);
m_playerPiece[GREEN].Load_Frame_Sprite(0, 0, 2, BITMAP_EXTRACT_MODE_CELL);
m_playerPiece[GREEN].Load_Frame_Sprite(1, 1, 2, BITMAP_EXTRACT_MODE_CELL);
m_playerPiece[GREEN].Load_Frame_Sprite(2, 2, 2, BITMAP_EXTRACT_MODE_CELL);
m_playerPiece[GREEN].Load_Frame_Sprite(3, 3, 2, BITMAP_EXTRACT_MODE_CELL);


m_playerPiece[PURPLE].SetTransColor(0,0,0);
m_playerPiece[PURPLE].Create_Sprite(8*16, 8*16, 16, 16, 4, SPRITE_ATTR_MULTI_FRAME | SPRITE_ATTR_VISIBLE, GDSURFACE_SYSTEMMEMORY, 0, 16, hInstance, IDB_PLAYERPIECES, NULL);
m_playerPiece[PURPLE].Load_Frame_Sprite(0, 0, 3, BITMAP_EXTRACT_MODE_CELL);
m_playerPiece[PURPLE].Load_Frame_Sprite(1, 1, 3, BITMAP_EXTRACT_MODE_CELL);
m_playerPiece[PURPLE].Load_Frame_Sprite(2, 2, 3, BITMAP_EXTRACT_MODE_CELL);
m_playerPiece[PURPLE].Load_Frame_Sprite(3, 3, 3, BITMAP_EXTRACT_MODE_CELL);

m_playerPiece[current_piece].SetCurrFrame(player_Dir);

m_playerPiece[current_piece].Set_Pos_Sprite(currentPlayer_X *16, currentPlayer_Y*16);



the lovely really lovely thing is in my process next frame when I try to draw the sprite, it dont work ON JUST ONE PIECE... the m_piece[RED] where RED, BLUE, GREEN, P URPLE are enumerated as:
enum pieces[RED=0, BLUE, GREEN, PURPLE];

It works fine for m_piece[BLUE] and the rest, and it works great for m_playerPiece[RED] all the way up purple too.

I tracked the error to Draw_Sprite and apparently when I try to call m_piece[0].Draw_Sprite(&backbuffer); the rects for just that sprite [0] is invalid as i get the GDERR_INVALIDRECTS. So I am curious as to why those rects for that one element of the array is wrong, as one can see, i do the same calls for m_piece[BLUE] m_piece[GREEN] and m_piece[PURPLE]. I even went so far as to nudge the array up:
NSprite m_piece[5]; and then make a SRED=4 and say:
m_piece[SRED].Create_Sprite(...)
m_piece[SRED].Load_Frame_Sprite(...)

...
...
m_piece[SRED].Draw_Sprite(&backbuffer);

It works great, but for some reason the m_piece[0]...just isnt working and I cannot figure out why.

Any ideas? im really lost as to why its not working, am I instantiating the classes wrong? do I need a new operator somewhere? its odd to me since the m_playerpieces[4] stuff works fine, and even the rest of m_piece[>1] calls work fine...just that one element doesn't and I haven't a clue why.

Internally I know some of the values are getting fudged which leads me to believe that yes somewhere memory isnt going how i think it should (again do I need a new operator? like:
NSprite m_piece[4]=new NSprite[4]; ?
hmm perhaps im getting confused with java again)


Ugh this is defiantly a show stopper, I mean i could just do as i said erlier, load the red piece in element 4 instead of 0 and change the rest of the game code to acceess that ignoring m_piece[0] all the time, but I shouldn't have to do that since m_piece[0] should be working.

Well if this made sense or even sparked a idea for a tree or two for me to bark up, id be glad as to me this error is very devious.

-Shane
Pejo Software - Per · 26 Aug 2003, 18:25 UTC
Don't you just love that kind of errors!
I would guess that you are overwriting memory somewhere. When doing that everything can happen!
Codejoy · 26 Aug 2003, 18:30 UTC — Ya
Ya I figured it was that somewhere, and no I dont like these errors :)

The worse part is, line for line, every piece looks right, even the implementation of the classes, it just so happens that one element on that one array doesnt work (I could see it even if it was like the final element, but the 0th element getting overwritten...how!?)

Like I said, I use to many languages to keep anyone straight, does C++ explicitly require you have a constructor /destructor for your classes, if so I dont know what id put in my constructor for the class...and once code goes out of scope is the destructor called?

plus is there any sort of class memory allocation that needs to be done on an array of instantiated objects?

-Shane
Codejoy · 26 Aug 2003, 18:40 UTC — This is too rich.
Oh this is dandy, so my header file looked like:
Code:

protected:
some stuff..
NSprite m_piece[4];
NSprite m_playerPiece[4];
some more stuff..


then the rest of the code i already posted here to create and load and draw that. This wasnt working m_piece[0] wasnt drawing.

So i tried:
Code:

protected:
some stuff..
NSprite m_piece[1];
NSprite m_playerPiece[4];
some more stuff..

still didnt work.

Then i tried:
Code:

protected:
some stuff..
NSprite m_piece[1];
NSprite m_others[4];
NSprite m_playerPiece[4];
some more stuff..

and did all that create and load and draw on the m_others (exact same as m_piece) but just with m_others (doing nothing with m_piece but instantiating as above)...WORKS FINE. now get this, note above that m_piece is still left in there.

So i remove m_piece[1] and now have:
Code:

protected:
some stuff..
NSprite m_others[4];
NSprite m_playerPiece[4];
some more stuff..

and it stops working again, the [0] element doesnt get bltt'ed because apparently the blt is returing invalid rect.

So defiantly a memory issue, but I cannot tell from where..and I cannot possibly think as to how my NSprite class could be causing it, so now im begging, anyone have any places for me to look? Again do i have to do funny in the constructors or destructors or somewhere else if using a instantiation of classes that are an array.

<-- ready to shoot self.

thanks,
Shane
If I read you correctly, you found the problem when you tried drawing the sprite with m_piece[0].Draw_Sprite(&backbuffer);

Does that mean that the code:
Code:

m_piece[RED].SetTransColor(0,0,0);
m_piece[RED].Create_Sprite(0, 0, 16, 16, 1, SPRITE_ATTR_SINGLE_FRAME | SPRITE_ATTR_VISIBLE, GDSURFACE_SYSTEMMEMORY, 0, 16, hInstance, IDB_CHIPS, NULL);
m_piece[RED].Load_Frame_Sprite(0, 0, 0, BITMAP_EXTRACT_MODE_CELL);

completed successfully?

If your memory was getting tromped on right off the bat, then the create and load should fail too. If these succeed, then sometime after you load, but before you draw is where the problem is.

Pam
Codejoy · 26 Aug 2003, 19:09 UTC — Yes
according to my own code return values, yes indeed the Create and Load are working just fine, they both return success...again in the links at first post is where you can see the inner workings of thsoe methods (they are relativly simple).

The funny thing is after the load, and before the draw i do nothing with m_piece[0]. So somehow is the draw trouncing it? or is there some way for other code to trounce that specific element somehow...this is maddeling frustratingly strange.

-Shane
It seems likely it is some other piece of data overwriting part of the m_piece[0] data.

Are you using the GapiApplication class, and if so are you creating and loading the sprites in InitInstance() or in CreateSurfaces()?

If you step through the code, does the Draw fail the very first time the sprite is drawn or after a few times through ProcessNextFrame()?

Pam
Pejo Software - Per · 26 Aug 2003, 19:23 UTC
In C/C++ it is very possible to write to memory you don't intend to write. It is actually a very clasical problem and very frustating. I would reccomend just going through your code and see if there are any places where you potentially could write over memory that you don't intended. Just as Pam is saying you should start to look at the code after you load, but before you draw.
Codejoy · 26 Aug 2003, 19:49 UTC — ya
ugh, need to develop on different screen, my small laptop screen is getting to me. Ill take another run to find the problem, just that like i said, im not doing anything to those class instansiations between creating, loading and drawing them... im doing other code stuffs sure but nothing having to do with that class instantiation...

Its not like im a programming idiot, I just havent firgued out how to effeciently debug on pocket pc apps, this is all hindered by the fact that stupid eVC++ and eVB+ and even my CEFILE explorer thing cannot connect to the Emulator, so alas, im having to send to my device every time I do a compile....*shutter*

It would be nice if I could write a error log function, but I dont know how to do simple file IO in eVC++ to work, apparently there is some c runtime libraries i can use/call somehow, but havent a clue how (which hindered me again when I tried to use time to seed a random number generator...any suggestions there would be nice too :) )

This forum is great for doing gapidraw stuff, but I havent' found a good place to learn general pocket PC 2002 programming, its either high level, or totally basic its not helpful...

id really like a "How to do what you did in VC++ in eVC++" like with stuff like time seeding for random functions, file io etc..

Thanks again,

-Shane
p.s. what exactly causes a GDERR_INVALIDRECTS on blt to happen, obviously with the one of two rects u can send to the blt function, but im curious what makes it puke, rects that have say negative values or values that go outside the surface area?,
Well, that's the beauty of overwriting memory, you don't have to be doing anything with that particular data. In fact, most of the time you won't be. It is some other, seemingly unrelated piece of code that is causing the problems.

EDIT:

I like to use this for seeding the random numbers
Code:
// Initialize random seed
srand(GetTickCount());


EDIT 2:
Feel free to post in the General Developer Discussion also for issues not related to GapiDraw. Plenty of developers far better than me read/post there often.

Pam
Codejoy · 26 Aug 2003, 20:08 UTC — *sigh*
"Theres something on the wing of the plane"

you know whats bad about being totally self taught, is even once i got an education I still had to unlearn my bad habits...

with that said, You know i always assume that memory can only get trounced with pointers, memcpy's, deletes etc...
but alas I had:
Code:

//init map
for(int i=0; i<20; i++)
{
for(int j=0; j<14; j++)
{
map[i][j]=-1;
}
}

and guess what map was defined as? map[14][20];

I am sorry for "wasting" everyone's time with this, I just didnt see it...I was soo flustered with the fear I had no faith in the classes i wrote that i spent all my time thumbing through thoughs, and it was surely enough right there, after I loaded, and created the bitmap, I switched what i and j go up to and the program runs great now.

*phew* thats a load off, god what a stupid mistake, had I had more faith in my other code i could of ditched going over that witha fine tooth comb assuming the error was there, and just goto my main program and find it bingo lickity split...and thanks
Pam, you suggesting i look after the create an load and before the draw did get me out of my own code for a bit and looking where i should of been (Even though I insited I did nothing with the class, and i was right and everyone else was, thats odd how that array troucning trounced the other class).

thanks again everyone, im happily coding again, instead of just coding...

jeez I have to stick with one language and not dozens what u take for granted in one language isn't available in the other and vice versa.

thanks again all,
-Shane
(boy sometimes it helps to just write out problems like this to get ur mind set constantly changing train of thoughts to hit upon the real error).
Glad we could help.

Pam
Placing things before your array is a good technique. Another approach would have been to shuffle around your colour enums, to see if the problem moved with RED or stayed with the first piece.

At that point, if you thought memory was being trampled somewhere in your code, a reasonable way to approach it brute force is to start disabling code, half at a time, to binary search for the offending code. I've done this (e.g. searching for compiler optimizer errors on DEC Alpha), it's tedious but it works.

I would recommend staying away from hardcoded values, use some sort of constant to define both the size and the iteration.

With arrays, if you are storing objects by value you don't need new, but if you have inheritance, be aware of the "slicing problem." Also, it's not a bad idea to prefer using the STL containers to arrays. In this case, it's not hard to manage the array, but if you start creating arrays dynamically, or resizing them, you want to use the STL.

Similarly, you want to start using auto_ptr instead of new. (But don't put auto_ptr in an STL container.) You don't want to be in the business of managing your own memory. Let the standard library do it for you. If auto_ptr doesn't meet your needs, check out the other shared pointers at www.boost.org.

If you're totally new to the standard library, the book Accelerated C++ by Koenig and Moo is a good start.
Codejoy · 27 Aug 2003, 13:33 UTC — Thanks
hey thanks for that book title, I am new to STL in such a way that basically ive been putting it off for as long as possible, and now with me getting all "classy" I realize that I shouldn't put off the power of it anymore, plus im tired of writing my own linked lists, trees etc :)

I am going to check out that book thanks for the nfo

-Shane
Might I recommend:
http://www.pocketmatrix.com/forums/view ... hp?t=10367

And there's an excellent STL tutorial:
http://echellon.hybd.net/issues/5/artic ... part1.html
(as referenced in the thread above)

-John
Let me just say that the book I mentioned in my last post is good for getting into the habit of using the standard library (including the STL), and does a nice job of explaining how it works.

But it isn't necessarily comprehensive in covering all of the standard library or STL.

It's almost a book that says: let's say you were learning programming, and didn't know what a pointer, array, or char was, but had this STL thing in front of you. How would you write a program to do this task? What would that program look like? How would the STL have to be written to make it work like that?

So as I said, it's a good introduction in that sense. Check out the reviews on it. But if you want a more thorough treatment, or a reference, another book would be best.