• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Anubiss

Creating A New Object Ingame

19 posts in this topic

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:

[code]
// 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;

}
[/code]
0

Share this post


Link to post
Share on other sites
-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.

[CODE]
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;
}

}
[/CODE]

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

Share this post


Link to post
Share on other sites
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 [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img] 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.
0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
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:

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

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:

[code]
char * String;
String = (char *) malloc(1000);
[/code]

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)
0

Share this post


Link to post
Share on other sites
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 [CODE]
ownstation* stations;
stations = (ownstation*) malloc(n*sizeof(ownstation))
[/CODE]
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:[CODE]
ownstation* temp;
temp = (ownstation*) malloc(newlen*sizeof(ownstation));
memcpy(temp, stations, oldlen*sizeof(ownstation));
free(stations);
stations=temp
[/CODE]
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.
1

Share this post


Link to post
Share on other sites
allright. the 1st part i checked.

now i do following:

[code]
void menu(void){
if (Pad.Newpress.X) n++;
}
[/code]

and

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

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?
0

Share this post


Link to post
Share on other sites
Alright. It´s working so far. I can add a new station. But after making one station the game freezes.

[code]
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);
}
}
[/code]

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

[code]ownstation* stations;
stations = (ownstation*) malloc(n*sizeof(ownstation));[/code]

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;

}
0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
thank you.
now i have one problem more.

my function is now looking like that:

[code]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++;
}
}[/code]

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


[code]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++;
}
}[/code]

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

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites
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));
1

Share this post


Link to post
Share on other sites
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?
0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
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

[code]//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();

}
}
}[/code]

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

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
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:

[code]//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--;

}[/code]

and to call the menu:

[code] 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;
}[/code]

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0