GP32 Trouble With Structs


Ksmiler

Member
Joined
Apr 30, 2003
Messages
123
Location
Yamaguchi, Japan
I'm having trouble reading values from an array of predefined structs.
Here's the code containing the structs' information and its data. This is stored in an .h file

Code:
#define OBJRKEY 100
#define OBJGKEY 200
#define OBJBKEY 300
#define OBJYKEY 400

struct tKEYOBJ
{
	int _pX;
	int _pY;
	int _objtype;
	int _value;
	int _status;
};

struct tKEYOBJ KeyObj[6] = 
{
     {272,368,300,1,1},
     {272,432,300,1,1},
     {400,368,100,2,1},
     {368,592,200,2,1},
     {176,336,400,1,1},
     {496,336,400,1,1},
};

Problem is, when I want to read a value from the stuct's arrays (for example KeyObj[1]._value), it will nearly always get the values wrong :(
To check which value was retrieved, I did sprintf(msg, "Value: %d", KeyObj[1]._value).

Is there like a special way to get a value from a predefined struct that I have missed to do? :huh:
 
which is the value you get when you read KeyObj[1]._value ?
which is the value you think you have to get ?

Remember arrays are zero based, so wen you read a value from KeyObj[1] you're reading the second member of that array.

Just trying to give my two cents.
 
in c++ you don't need to write typedef.

i think the prob is c++. it doesn't mix with global variables and the gp32 devkits.
 
THe value I get from KeyObj[1]._value is 0. As you can see, there is nothing in the predefined struct array which has this value. It like it didnt pick up a value at all :(
 
as no_skill said, the problem is in c++.
when you create a global object (a class or a struct), the costructor doesn't get called.
the value for each one of the elements of your structs is 0.
 
then your structs can't look like

Code:
struct foo{
int x, y;
};

pure c only allows

Code:
typedef struct{
int x, y;
}foo;

is your file extension .c or .cpp? does your makefile call g++ or gcc?
 
Thanks for the tip no_skill. :)
Also, my file extensions are .c and my makefile calls gcc.

Code:
typedef struct{
int x, y;
}foo;

Just curious but how would I be able to initialise this at declaration time?
 
Okay, here is the code now...

Code:
#define OBJRKEY 100
#define OBJGKEY 200
#define OBJBKEY 300
#define OBJYKEY 400

typedef struct 
{
	int _pX;
	int _pY;
	int _objtype;
	int _value;
	int _status;
}tKEYOBJ;

tKEYOBJ KeyObj[6] = 
{
     {272,368,300,1,1},
     {272,432,300,1,1},
     {400,368,100,2,1},
     {368,592,200,2,1},
     {176,336,400,1,1},
     {496,336,400,1,1},
};

sprintf(msg, "Value: %d",KeyObj[1]._value) /* Result should be 1 */

and the result is... Value: 0

No change to the output then :( .
 
no_skill posted on Aug 2 2004 at 02:58 PM said:
then your structs can't look like

Code:
struct foo{
int x, y;
};

pure c only allows

Code:
typedef struct{
int x, y;
}foo;

is your file extension .c or .cpp? does your makefile call g++ or gcc?
This is simply not true. C DOES allow you do:
struct foo{
int x, y;
};

However, when you declare a variable of type foo you need to do:
struct foo x;
 
Last edited by a moderator:
KSmiler,

Your code works fine for me under devkitARM

Here is the full code I used to test:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gp32.h>
#include <cpp_prototypes.h>

#define OBJRKEY 100
#define OBJGKEY 200
#define OBJBKEY 300
#define OBJYKEY 400

struct tKEYOBJ
{
int _pX;
int _pY;
int _objtype;
int _value;
int _status;
};

struct tKEYOBJ KeyObj[6] =
{
    {272,368,300,1,1},
    {272,432,300,1,1},
    {400,368,100,2,1},
    {368,592,200,2,1},
    {176,336,400,1,1},
    {496,336,400,1,1},
};

u16 *framebuffer;

int main( void )
{
	char displayText[50];

	framebuffer = (u16*) FRAMEBUFFER1;
	gp_setCpuspeed(33);
	gp_initFramebuffer(framebuffer,16,85);
	sprintf(displayText, "Text: %i", KeyObj[1]._value);
	gp_drawString(10, 20, strlen(displayText), displayText, 0, framebuffer);
	while( !gp_getButton() );
	return 0;
}
 
Okay, here is the total code for the chip's challenge recreation I am trying to make.
The initialised struct with the problem is in the file lvlstuff.h and the request for values in the initialised struct are done in gpmain.c.

Controls for chip.fxe:

UP, DOWN, LEFT, RIGHT: Move one tile up, down, left, or right.
START: Reset GP32.
Press and hold A: Displays how many keys you have collected/got.

Aim of the game:
Try and get all the microchips in the lvl the go to the locked gate and warp out of the level.
(Warping out of level not done yet <_< ).

PROBLEM:
For the player to open up the coloured locked doors in the level, you must collect coloured keys. Each key the player picks up, gives the player a certain number of that coloured key (e.g. one red key on the level might give the player one red key, while another red key on the same level might give the player two red keys).

The game gets information on the keys by reading struct tKEYOBJ in the file lvlstuff.h. The struct has information like the posistion of the key in the level, what type of key it is, how many of that type of key does it give to the player when it is picked up and if the key is active or not (if it has been picked up or not).

Now, because of this struct problem, whenever you pick up a key in the level, it does not read the _value of the struct. This makes the game not know how many keys to give the player so the player can progress in the game. :angry:

Can someone please read the code, (if they can, i congratulate you due to I do not lay out code well :wacko: ) and explain how to solve the problem?
Thanks :)

Source + Game at http://www.freewebs.com/ksmiler/files/chip.rar

If you want more information, would be happy to answer :)
 
OK,

The only problem I found is that you are setting the initial status of the keys to 1 which is causing them not appear. I reset the inital status to 0 and voila. It worked as expected. The keys appeared and you could use them to open the doors.

I used devkitARM to build your application.
 
Go to debug mode by pressing select. What number do u get next to ObjType:confused:

I get 0. <_<
I assume you get 1.

Also, did you test all keys?
And did you run it on a real GP32 or Geepee?

I tested this using Geepee and I have a WinXP and a Devkitadv + cygwin + SciTE setup. Could devkitadv or my setup have something to do with this or am I becoming paranoid :wacko: ??
 
Ksmiler posted on Aug 4 2004 at 01:07 AM said:
Go to debug mode by pressing select. What number do u get next to ObjType:confused:

I get 0. <_<
I assume you get 1.

Also, did you test all keys?
And did you run it on a real GP32 or Geepee?

I tested this using Geepee and I have a WinXP and a Devkitadv + cygwin + SciTE setup. Could devkitadv or my setup have something to do with this or am I becoming paranoid :wacko: ??
I do get 1. I am also testing on geepee under XP.

I did test all keys. I even made sure that they decremented properly.

It may be a problem with setup or it could be a problem with your makefile.

I took a look at your Makefile but I have never used dekitadv and am unfamiliar with what it requires.

And as always...I recommend people use devkitARM. You can find it at devkit.tk.
 
Last edited by a moderator:
Back
Top