Programming 2D Games

The official forum for "Programming 2D Games" the book by: Charles Kelly

It is currently Tue Apr 23, 2019 11:00 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Tue Aug 05, 2014 6:28 pm 
Offline

Joined: Sat Oct 12, 2013 4:48 pm
Posts: 44
Hi,

In most of your projects, you rarely use pointers for your game objects, so my question is when and why I would use or not use pointers for my game objects??

KIND REGARDS

JC MAZZA


Top
 Profile  
Reply with quote  
PostPosted: Tue Aug 05, 2014 11:03 pm 
Offline
Site Admin
User avatar

Joined: Sat Jan 28, 2012 4:36 pm
Posts: 551
I try to make the examples simple to follow. Many programmers are confused by pointers so I tend not to use them unless it is necessary. If I were going to create game items dynamically I would use pointers.

_________________
Professor Kelly


Top
 Profile  
Reply with quote  
PostPosted: Sun Oct 19, 2014 7:31 pm 
Offline

Joined: Sat Oct 12, 2013 4:48 pm
Posts: 44
Ok but WHY and WHEN would you want to create game items dynamicaly what is the difference?? Could you give us an example on when it suited to use a pointer.

if I do this for example:

player *ship;
then I have to add new and delete which i get it why we do this but its more coding

but if I do this:
player ship;
memory will be automatically allocated for me right?? which is less coding right??

what would make me decide between the two.

whichever way I do, I get the same results and everything compiles the same way at the same speed

So why and when is it suited to use a pointer or not and what is actually the purpose of it??


Top
 Profile  
Reply with quote  
PostPosted: Fri Oct 24, 2014 12:26 pm 
Offline
Site Admin
User avatar

Joined: Sat Jan 28, 2012 4:36 pm
Posts: 551
Dynamic object creation occurs while the program is running. As opposed to static object creation which occurs at compile time.

Player ship;
This creates a ship object at compile time. The disadvantage of this method is that I must create every ship I will need for the entire game at compile time. That means all of the ship objects will need to reside in memory for the entire game. Maybe not a big deal with just ships, but now expand that to every object in the game. In large AAA game titles there may be thousands of unique game objects per scene with many different scenes in the total game. If we were forced to create all of those objects at compile time it would increase the memory requirements of the game to tremendously.

Player *ship;
All that is created at compile time is a pointer. We create the object when we need it.
ship = new Player();

When we are finished with it we delete it to release the memory.
delete ship;

_________________
Professor Kelly


Top
 Profile  
Reply with quote  
PostPosted: Fri Nov 21, 2014 6:01 am 
Offline

Joined: Fri Oct 19, 2012 5:50 pm
Posts: 32
Is it worth worrying about Stack vs Heap allocation in terms of performance? Probably a trivial distinction for simple games on modern machines, but I don't know much about it.


Top
 Profile  
Reply with quote  
PostPosted: Fri Nov 21, 2014 3:18 pm 
Offline
Site Admin
User avatar

Joined: Sat Jan 28, 2012 4:36 pm
Posts: 551
I think the main difference in stack vs. heap is the allocation time required when using the heap (new) as opposed to the stack (local variable). If your code is super speed sensitive then you would want to perform the memory allocation (new) before. Perhaps during a transition from one level to another.

_________________
Professor Kelly


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group