Creating A New Object Ingame

Started by
18 comments, last by Anubiss 12 years, 2 months ago
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..
Advertisement
A quick question: when and how often are you calling ladestationen()? If you call it more than once you might leak memory.
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
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));
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?
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.
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.

Yo dawg, don't even trip.

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()" ?
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.
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!

This topic is closed to new replies.

Advertisement