Jump to content

  • Log In with Google      Sign In   
  • Create Account

Creating A New Object Ingame


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
19 replies to this topic

#1 Anubiss   Members   -  Reputation: 100

Like
0Likes
Like

Posted 10 January 2012 - 12:08 PM

Hey friends.
I hope you can help me with my lil game what i´m writing in C with the PaLib library for my nintendo ds. I have a ship what´s flying through the orbit. when the ship is landing in the space-station i want to have the option "buy new station". But how do i code the creation of a new station. i just need the idea of how doing that.
Will be happy about any help.


Here´s the code till now:

// Includes
#include <PA9.h>
#include "gfx/all_gfx.c"
#include "gfx/all_gfx.h"
int main(int argc, char ** argv)
{
PA_Init();  
PA_InitVBL();
PA_EasyBgLoad(0, 1, aa);
PA_InitText(1,0);
PA_LoadSpritePal(0, 0, (void*)vaisseau_Pal);
PA_LoadSpritePal(0, 1, (void*)station01_Pal);
PA_CreateSprite(0, 0,(void*)vaisseau_Sprite, OBJ_SIZE_32X32,1, 0, 128-16, 96-16); // Das SChiff auf Mitte des unteren Screens setzen
PA_SetSpriteRotEnable(0,0,0);
u16 angle = 0;
int x = (128) << 8;
int y = (96) << 8;
int sx;
int sy;
typedef struct{
int x;
int xmax;
int schalter;
}pflanze;
pflanze buche;
buche.xmax=1000;
buche.schalter=0;
int scrollx = 0;
int scrolly = 0;

//Raumstation:
/////////////////////////////////////////////////
typedef struct{
int x;
int y;
int flag;
int entfx;
int entfy;
}station;
station rs;
//station fremd[10];//0-9
rs.x=200;
rs.y=96;
rs.flag=0;
rs.entfx;
rs.entfy;
void station01test(void){
rs.entfx=(sx>>8)-(rs.x);//Entfernung zwischen Station und Raumschiff messen
  if (rs.entfx<0){rs.entfx=-rs.entfx;}//Betrag, falls Entfernung negativ!
rs.entfy=(sy>>8)-(rs.y);
  if (rs.entfy<0){rs.entfy=-rs.entfy;}


if (rs.flag==0){
  PA_CreateSprite(0, 2,(void*)station01_Sprite, OBJ_SIZE_64X64,1, 1, rs.x-16, rs.y-16);
  rs.flag=1;
}

if ((rs.entfx>100)&&rs.flag==1){
  PA_DeleteSprite (0, 2);
  rs.flag=0;
}

if ((rs.entfy>100)&&rs.flag==1){
  PA_DeleteSprite (0, 2);
  rs.flag=0;
}

if ((rs.entfx<30)&&(rs.entfy<30)){
  PA_OutputText(1,0,2,"'START' zum Landen-");
}
if ((rs.entfx>30)||(rs.entfy>30)){
  PA_OutputText(1,0,2,"					  ");
}

}
//Einfacher test:
/////////////////////////////////////////////////
void wachse(void){
if (buche.schalter==1)
  if(buche.x<buche.xmax)
  buche.x++;
  if(buche.x==buche.xmax)
  buche.x=buche.xmax;

}

//STATUSBILDSCHIRM
//////////////////////////////////////////////////////////////////
void statusscreen(void){
  //Ausgabe von Informationen auf oberen Screen:
  ////////////////////////////////////////////////////////////////////////////////
  PA_OutputText(1,0,12,"Map-x: %02d", scrollx>>8);
  PA_OutputText(1,0,13,"Map-y: %02d", scrolly>>8);
  PA_OutputText(1,0,14,"-------------------");
  PA_OutputText(1,0,15,"Schiff-x: %02d", sx>>8);
  PA_OutputText(1,0,16,"Schiff-y: %02d", sy>>8);
  PA_OutputText(1,0,17,"-------------------");
  PA_OutputText(1,0,18,"x von Station entfernt: %02d", rs.entfx);
  PA_OutputText(1,0,19,"y von Station entfernt: %02d", rs.entfy);
  PA_OutputText(1,0,20,"Winkel des Schiffes: %02d", angle);
}

//MAIN:
////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
while (1)
{
  //Test-Buche: zählt von 0 bis 1000, wenn "A" gedrückt wird
  if (Pad.Newpress.A) buche.schalter=1;
  wachse();
  PA_OutputText(1,0,0,"%02d", buche.x);


  //Winkelberechnung-Flugschiff:
  ///////////////////////////////////////////////////////////////////////////////
  angle = PA_GetAngle(x>>8, y>>8, Stylus.X, Stylus.Y);					   //
  PA_SetRotsetNoZoom(0, 0, angle); // Änderung des Schiffswinkels	   //
  //Flugschiff-Koordinaten AUSSERHALB DER ENGINE:
  sx=(scrollx+x);
  sy=(scrolly+y);

  //Hintergrund-Scrolling:
  //////////////////////////////////////////////////////////////////////////////
  if(Pad.Held.B){				//
   scrollx += PA_Cos(angle);			//
   scrolly += PA_Sin(angle);
  };
  //Map-Grenzen:				//
  if((scrollx>>8)>(2560-255)){
   scrollx=590080;
  };
  if((scrollx>>8)<0){
   scrollx=0;
  };
  if((-scrolly>>8)>(1600-191)){
   scrolly=-360704;
  };
  if((-scrolly>>8)<0){
   scrolly=0;
  };
  //Set-Map:
  PA_LargeScrollXY(0, 1, scrollx>>8, -scrolly>>8);

  statusscreen();

  PA_SetSpriteXY(0, 0, (x>>8)-16, (y>>8)-16);
  PA_SetSpriteXY(0, 2, ((rs.x-16)+(-scrollx>>8))-16, ((rs.y-16)+(scrolly>>8))-16);
  station01test();
  PA_WaitForVBL();
}

return 0;

}


Sponsor:

#2 menyo   Members   -  Reputation: 449

Like
-2Likes
Like

Posted 10 January 2012 - 02:29 PM

-edit- Yeah, like Boogyman said this is for C#. Sorry i skipped over your code and thought you where asking help for C#.

You could just code the station like you always do. Just make a class for it and give it attributes. It should probably have a owner attribute and a location attribute too.

When a player buys it you instantiate the class and maybe put it in a list.

class Station
	{

		public int health { get; private set; }
		public int ownerID { get; private set; }
		public Vector2 location { get; private set; }


		public Station(int stationType, int ownerID, Vector2 location)
		{

			if (stationType == 1)
			{
				this.health = 500;
			}
			else if (stationType == 2)
			{
				this.health = 750;
			}

			this.ownerID = ownerID;

			this.location = location;
		}

	}

When your player buys a station do something like this.
Station newstation = new Station(stationType, playerID, location);
//offcourse these variables need to get filled.

Current Project: TechnoFlux read all about it on my

DEV BLOG


#3 boogyman19946   Members   -  Reputation: 1061

Like
0Likes
Like

Posted 10 January 2012 - 02:38 PM

I believe the language in question is C, not C#, alas, the new keyword is non existent, nor are constructors.

To the OP, I take it you have not yet covered dynamic memory. If you have not, you can always create an array of stations (if you haven't learned about arrays, I guess you can create a "StationManager" struct and put however many stations you want) and each station could contain a variable rather it's activated or not (this will be very painful to code. It will be much easier if you use arrays). This little hack should allow you to do what you want, but you should probably limit your use of it to this program only Posted Image Besides, this setup is limited to a static number of stations.

Of course, the best solution is to use malloc() and free() to create them during execution. I don't know why I didn't mention that.
"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ

My personal links :)
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs :D
- C++ Standard Library Reference - For some of your C++ needs ^.^

#4 return0   Members   -  Reputation: 444

Like
1Likes
Like

Posted 10 January 2012 - 07:57 PM

Make a struct to represent the stations data, use the malloc function to get a pointer to a new instance. You should call the free() function with this pointer when your'e done with it.

#5 Anubiss   Members   -  Reputation: 100

Like
0Likes
Like

Posted 11 January 2012 - 02:29 AM

this malloc function sounds very good, but it seems like i need to understand a bit more about this and also pointers.
So i made now a struct for the stations i want to buy:

typedef struct{
int id;
int x;
int y;
int flag;
int entfx;
int entfy;
}ownstation;
ownstation neu[10];

Is it correct to make an array now if i for example don´t want to have more than 10 stations?

then i have found following for this malloc:

char * String;
String = (char *) malloc(1000);

so if the code would be:

if (klicking on "buy"){
do what?
}

EDIT:
ownstation* neu(neu[10](int id[10]));

something like this maybe? (i know that it is not correct, but maybe i am on the right way)

#6 RulerOfNothing   Members   -  Reputation: 1160

Like
1Likes
Like

Posted 11 January 2012 - 02:43 AM

If you don't want more than 10 stations, that code is what you want. If you want to use malloc to dynamically allocate an array of n ownstations then you want
ownstation* stations;
stations = (ownstation*) malloc(n*sizeof(ownstation))
before you use the array and free(stations) to release the memory once you are finished with it. To resize the array you want the following code:
ownstation* temp;
temp = (ownstation*) malloc(newlen*sizeof(ownstation));
memcpy(temp, stations, oldlen*sizeof(ownstation));
free(stations);
stations=temp
which will extend the length of the stations array from oldlen to newlen. If you want to make the array pointed to by stations to be shorter, simply replace oldlen with newlen.

#7 Anubiss   Members   -  Reputation: 100

Like
0Likes
Like

Posted 11 January 2012 - 03:25 AM

allright. the 1st part i checked.

now i do following:

void menu(void){
if (Pad.Newpress.X) n++;
}

and

if ((rs.entfx<30)&&(rs.entfy<30)){
  PA_OutputText(1,0,2,"'X' zum kaufen-");
  menu();
}

So i add "1" to n, when the X-Button is pressed.
And how are the stations looking like? I mean, if n=2, do i have stations[0] and stations[1] then?

#8 RulerOfNothing   Members   -  Reputation: 1160

Like
0Likes
Like

Posted 11 January 2012 - 03:39 AM

If you have allocated space for two stations, you can use stations[0] and stations[1] to access the allocated memory.

#9 Anubiss   Members   -  Reputation: 100

Like
0Likes
Like

Posted 13 January 2012 - 04:17 AM

Alright. It´s working so far. I can add a new station. But after making one station the game freezes.

void ladestationen(void){
while (istationen<n){
  stations[istationen].x=210;
  stations[istationen].y=100;
  if (stations[istationen].flag!=1){
   PA_CreateSprite(0, istationen,(void*)station01_Sprite, OBJ_SIZE_64X64,1, 1, stations[istationen].x-16, stations[istationen].y-16);
   stations[istationen].flag=1;
  }
  PA_SetSpriteXY(0, 3, ((stations[istationen].x-16)+(-scrollx>>8))-16, ((stations[istationen].y-16)+(scrolly>>8))-16);
}
}

I almost think, that this is correct.
But following I wrote out of a function and out of the main-function:

ownstation* stations;
stations = (ownstation*) malloc(n*sizeof(ownstation));

And how does it work with resizing?
I don´t really understand where exactly this has to be written.

EDIT:
That´s wrong, aye?


void ladestationen(void){
stations = (ownstation*) malloc(n*sizeof(ownstation));
while (istationen<n){
stations[istationen].x=210;
stations[istationen].y=100;
if (stations[istationen].flag!=1){
PA_CreateSprite(0, istationen,(void*)station01_Sprite, OBJ_SIZE_64X64,1, 1, stations[istationen].x-16, stations[istationen].y-16);
stations[istationen].flag=1;
}
PA_SetSpriteXY(0, 3, ((stations[istationen].x-16)+(-scrollx>>8))-16, ((stations[istationen].y-16)+(scrolly>>8))-16);
}

temp = (ownstation*) malloc(newlen*sizeof(ownstation));
memcpy(temp, stations, oldlen*sizeof(ownstation));
free(stations);
stations=temp;

}

#10 RulerOfNothing   Members   -  Reputation: 1160

Like
1Likes
Like

Posted 13 January 2012 - 04:33 AM

The line
stations = (ownstation*) malloc(n*sizeof(ownstation));
allocates enough space for n ownstation structs. You replace n with however many ownstations you want in your array. With resizing, I just remembered that you can use the realloc function so you can just say stations = (ownstation*) realloc(stations,newlen); where newlen is the expression representing the length you wish to resize the array to.

#11 Anubiss   Members   -  Reputation: 100

Like
0Likes
Like

Posted 13 January 2012 - 05:14 AM

thank you.
now i have one problem more.

my function is now looking like that:

oid ladestationen(void){
stations = (ownstation*) malloc(n*sizeof(ownstation));
istationen=0;
while (istationen<n){
  if (stations[istationen].flag!=1){
   stations[istationen].x=330;
   stations[istationen].y=120;
   PA_CreateSprite(0, istationen,(void*)station01_Sprite, OBJ_SIZE_64X64,1, 1, stations[istationen].x-16, stations[istationen].y-16);
   stations[istationen].flag=1;
  }
  PA_SetSpriteXY(0, istationen, ((stations[istationen].x-16)+(-scrollx>>8))-16, ((stations[istationen].y-16)+(scrolly>>8))-16);
  istationen++;
}
}

It works everything so far, but when i create a new station the game freezes.
it does not freeze, when it looks like so:


void ladestationen(void){
stations = (ownstation*) malloc(n*sizeof(ownstation));
while (istationen<n){
  if (stations[istationen].flag!=1){
   stations[istationen].x=330;
   stations[istationen].y=120;
   PA_CreateSprite(0, istationen,(void*)station01_Sprite, OBJ_SIZE_64X64,1, 1, stations[istationen].x-16, stations[istationen].y-16);
   stations[istationen].flag=1;
  }
  PA_SetSpriteXY(0, istationen, ((stations[istationen].x-16)+(-scrollx>>8))-16, ((stations[istationen].y-16)+(scrolly>>8))-16);
  istationen++;
}
}

but now the x and y values of the station do not change..

#12 RulerOfNothing   Members   -  Reputation: 1160

Like
0Likes
Like

Posted 13 January 2012 - 05:20 AM

A quick question: when and how often are you calling ladestationen()? If you call it more than once you might leak memory.

#13 Anubiss   Members   -  Reputation: 100

Like
0Likes
Like

Posted 13 January 2012 - 05:24 AM

i´m calling it in each frame.
should i make some kind of counter or something like that?

but i don´t think it can be a lack of mem, because a few minutes ago i was able to load about 30 new stations, with 30 sprites. but they just where created on the same coordinates.. and didn´t move

#14 RulerOfNothing   Members   -  Reputation: 1160

Like
1Likes
Like

Posted 13 January 2012 - 05:28 AM

If you are calling it in every frame then stations will always point to a new array of ownstation structs. You might want to try replacing the line
stations = (ownstation*) malloc(n*sizeof(ownstation));

with stations = (ownstation*) realloc(stations, n*sizeof(ownstation));

#15 Anubiss   Members   -  Reputation: 100

Like
0Likes
Like

Posted 13 January 2012 - 05:39 AM

glorious!
have much thanks. it´s working very fine.

but for understanding. realloc is reserving memory as malloc is doing. But realloc keeps the given values and new values are simply added behind the already excisting values?

#16 RulerOfNothing   Members   -  Reputation: 1160

Like
1Likes
Like

Posted 13 January 2012 - 05:43 AM

Yes. As I understand it, if you use realloc to enlarge a block of memory, it allocates a larger block of memory, copies the contents of the original memory block to the new memory block and then frees the original memory block.

#17 boogyman19946   Members   -  Reputation: 1061

Like
1Likes
Like

Posted 13 January 2012 - 02:06 PM

From what I can tell, malloc() allocates memory and gives you a pointer to it. However, the memory is not released back to the system unless you explicitly call free() on the pointer or close your program. If you call malloc to get memory and you no longer need it, you have to call free to give it back to the OS. If you don't, you're generating what is known as a memory leak, as you're rendering memory unusable. realloc() does this for you. The end result is much like RuleOfNothing said (I think). If you chose to use malloc() instead of realloc(), make sure to call free() on the old pointer when you're done.
"If highly skilled generalists are rare, though, then highly skilled innovators are priceless." - ApochPiQ

My personal links :)
- Khan Academy - For all your math needs
- Java API Documentation - For all your Java info needs :D
- C++ Standard Library Reference - For some of your C++ needs ^.^

#18 Anubiss   Members   -  Reputation: 100

Like
0Likes
Like

Posted 24 January 2012 - 03:57 PM

But i only have to use free() if i really want to destroy an "object"? So i can create, create and create and only for "n"- 1 (for example) i need to free the memory then?

EDIT:

btw, there´s a new question i have.
i made an option-menu

//Handelsbildschirm:
/////////////////////////////////////////////////
void handelsbildschirm(void){
while(1){
  PA_OutputText(1,0,1,"++++++++++++++++");
  PA_OutputText(1,0,2," Station: %02d", stations[istationen].id);
  PA_OutputText(1,0,3,"++++++++++++++++");
  PA_OutputText(1,2,5,"Handelsbildschirm");
  PA_OutputText(1,0,6,"++++++++++++++++");
 
  PA_OutputText(1,0,optionskursor,"o");
   if(Pad.Newpress.Down)optionskursor++;
if(Pad.Newpress.Up)optionskursor--;
  if(Pad.Newpress.B)break;
}
}

//Stations-Menü:
/////////////////////////////////////////////////
void stationsmenu(void){

PA_OutputText(1,0,1,"++++++++++++++++");
PA_OutputText(1,0,2," Station: %02d", stations[istationen].id);
PA_OutputText(1,0,3,"++++++++++++++++");
PA_OutputText(1,2,5,"Handelsbildschirm");
PA_OutputText(1,2,6,"Stationsinformationen");
PA_OutputText(1,2,7,"Schiffsinformationen");
PA_OutputText(1,0,optionskursor,"o");
if(Pad.Newpress.Down)optionskursor++;
if(Pad.Newpress.Up)optionskursor--;
if(Pad.Newpress.A){
  PA_ClearTextBg(1);
  switch(optionskursor){
 
  case 5:
  handelsbildschirm();
 
  }
}
}

I choose with my cursor the option "Handelsbildschirm" an it leads me to the new menu, but there the cursor does not move. is it because of the "while()" ?

#19 RulerOfNothing   Members   -  Reputation: 1160

Like
1Likes
Like

Posted 25 January 2012 - 12:13 AM

It's because you don't take in keypad input in your menu loop, so Pad.Newpress.Down, Pad.Newpress.Up and Pad.Newpress.B will never have non-zero values, so the various if statements will never be triggered. Also, you should use free() only in situations where you no longer want to use the memory you pass to free(), because if you use memory after it has been freed, it may have been overwritten.

#20 Anubiss   Members   -  Reputation: 100

Like
0Likes
Like

Posted 25 January 2012 - 07:56 AM

what do you mean with that i "dont take keypad input in my menu loop"? they´re in each loop as i see. in the first Menu it´s even working, but if i´m pressing the A-Button the new menu appears but with each klick on up or down the "o" will put out on each y-value


EDIT:
Allright, I understood what you mean.
made it now so:

//Handelsbildschirm:
/////////////////////////////////////////////////
void handelsbildschirm(void){

  PA_OutputText(1,0,1,"++++++++++++++++");
  PA_OutputText(1,0,2," Station: %02d", stations[istationen].id);
  PA_OutputText(1,0,3,"++++++++++++++++");
  PA_OutputText(1,2,5,"Handelsbildschirm");
  PA_OutputText(1,0,6,"++++++++++++++++");
  PA_OutputText(1,0,optionskursor,"o");
  if(Pad.Newpress.Down)optionskursor++;
  if(Pad.Newpress.Up)optionskursor--;
}

//Stations-Menü:
/////////////////////////////////////////////////
void stationsmenu(void){

PA_OutputText(1,0,1,"++++++++++++++++");
PA_OutputText(1,0,2," Station: %02d", stations[istationen].id);
PA_OutputText(1,0,3,"++++++++++++++++");
PA_OutputText(1,2,5,"Handelsbildschirm");
PA_OutputText(1,2,6,"Stationsinformationen");
PA_OutputText(1,2,7,"Schiffsinformationen");
PA_OutputText(1,0,optionskursor,"o");
if(Pad.Newpress.Down)optionskursor++;
if(Pad.Newpress.Up)optionskursor--;

}

and to call the menu:

while ((stations[istationen].entfx<30)&&(stations[istationen].entfy<30)){
   //PA_OutputText(1,2,9,"'X' zum kaufen");
  //
   //menu();
   if(optionsflag==0)stationsmenu();
   if(Pad.Newpress.A)optionsflag=1;
   if(optionsflag==1)handelsbildschirm();
  
   break;
  }

I´m of course not ready with it, but seeing that it´s working, is bringing me forward. Have much thanks!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS