Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!

1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Member Since 30 Sep 2010
Offline Last Active Jul 23 2013 07:38 AM

Topics I've Started

Using New and Delete in inherited classes and stored pointers to the new object

21 August 2012 - 07:06 AM

I am using much of the framework I got from http://www.sdltutorials.com/ , and while its entity class is great for creating new player entities I cant get the thing to make bullet entities... least not without generating exception faults.

The way to make a player entity is straight forward, declare it in the source file , load its assets when entering the in game state and clear the assets when exiting ... no problem.

The bullets however pose a number of issues, I cant declare each bullet beforehand.... they need to be created dynamically so I create one each time the player fires, and store it in the entity vector like the player... and when the bullet collides with a wall it dies, so I delete it and erase the element in the vector it was stored in.

Code to create a bullet :

Entity_Bullet* pBullet = new Entity_Bullet;

  if(Facing == ENT_DIR_RIGHT)
	pBullet->MoveRight = true;
	pBullet->MoveLeft = false;
	pBullet->X = X + Width;
	pBullet->Y = Y + (Height/2);
  if(Facing == ENT_DIR_LEFT)
	pBullet->MoveLeft = true;
	pBullet->MoveRight = false;
	pBullet->X = X - 20;
	pBullet->Y = Y + (Height/2);


The entity list is declared like this : std::vector<Entity*> Entity::EntityList;
The code to remove the bullet is in the ingame state loop, where it goes through the entity vector and calls up each element.

// Move , Animate and calculate actions for ALL entities
for(unsigned int i = 0; i < Entity::EntityList.size(); i++)
   if(!Entity::EntityList[i]) continue;

   // If the entity is not a player
   if(Entity::EntityList[i]->Type != ENTITY_TYPE_PLAYER)
	// If the Entity is suppose to be destroyed
	if(Entity::EntityList[i]->Dead == true)
	 Text::AddText("Dead one here", 10, 400);
	 if(Entity::EntityList[i]->Type == ENTITY_TYPE_BULLET_PLAYER)
	   delete Entity::EntityList[i];

	 Entity::EntityList.erase(Entity::EntityList.begin() + (i --));

The issue seems to be in the clearing up portion, I think there is a conflict in the vector when a few bullets are made and destroyed... I am thinking it is trying to store a bullet in a vector that was deleted but not cleared ?
If I remove the delete and erase portions of the entity loop it runs without crashing, just leaves bullets all over the place.

I would prefer the actual bullet class to delete the bullet during the OnCleanup() call, but using delete this crashes the game as soon as a bullet is fired, and I cant think of a way to identify the particular bullet beyond a this call.

Hmm, would the bullet object go out of scope if the pointer ( or rather the element the pointer was erased ? ).

Tips , ideas , advice welcome ... thanks.

FYI - I havent just copied the tutorials code , I have modified a number of things in the code except for portions where I like how it works, ie I havent just copy pasted someone elses code and want you to sort out the bugs.

Using cerr with Visual Studio

14 August 2012 - 08:54 AM

Bit of a silly question but I cant seem to find a proper answer to this...

A lot of code snippets will use std::cerr << "Error xyz is doing shit it shouldnt" ... or something like that when a fault occurs, right.
Where the heck do I see these cerr messages in the Visual Studio IDE ? There is no console where the cerr can output here, and it doesnt show on the output window either.
Lots of answers to other questions about it usually slip off to re-directing the cerr output to a file, again in the console... how do I do this in VS ?
I get you can use this to report errors to one pipe and cout streams to its own pipe thus not mixing up error reports with your output buffers.

I would really like to be able to use cerr for sections of the code where I cant rely on the code being able to put up text to the screen... like in SDL_Init , or ttf init, or events where the init causes a crash ( had that trying to get keyboard repeat interval set ...crashed immediately and didnt get to the if init fails tell me about it portions.

(SDL + C++) Can you blit from a surface to another surface then to the screen ?

09 July 2012 - 06:20 AM

In brief , I want to build up a level using tiles then blit that level to a intermediate surface ( not the main screen surface ), then clip a portion of that intermediate surface to blit to the screen.
Is that possible ?

I cant seem to get even a basic image that was blitted to the intermediate surface to be then drawn to the screen, it displays nothing.

The rest of the code works, it displays the text and if I dont try using the experimental intermediate surface blit mode , it displays just fine but I cant seem to get whatever is blitted to the intermediate surface to be used as a source to blit to the destination.

Something like this :

Normal way : Image to surface -> surface to screen surface

What I want : Image to surface -> surface to intermediate surface -> a selected clip of the intermediate surface to screen surface.

Why ?!?

My level code builds up the levels using tiles, a collection of tiles makes a map and a set of maps makes an area. The code currently renders up to 6 maps of the 9 maps that make my test area... the 6 near the camera which is following the player.
The issue is that it draw the entire map which is 640 x 640 in size, so in effect it is drawing (640 x 3) wide x (640 x 2) high each loop, this makes it hard for me to position the 'window' the player will see, and is kind of wastefull.
I have tried to just render what the camera is over for a week or two , but that led to stuttering updating of the level as it was updating tile by tile and had to cross into several maps ( which are on different files, stored in a vector and refered to by ID ).

If this isnt possible I will probably resort to cutting out the area is built up with maps part of the code, and simply fall back to a simple single map system... but I would rather play with the more complex system as it is giving me good experience working with complex level systems.

Sumary : Is it possible to blit from a surface to another intermediate surface and then a portion of that intermediate surface to the screen ?

bitmap font performance issue (SDL and C++ )

20 June 2012 - 05:13 AM


I am developing a small framework that I can use when starting a new game project, the framework provides the basics I need each project ... like initialising , putting text on the screen, loading images, feedback from the code.
It was all going fine until I added the FPS meter, then I notice the FPS take a dive when the console was displaying messages... a HUGE dive, from max of 500 FPS ( no message from console) to 293 with only 23 messages being displayed.

To display text on the screen I use a basic bitmap font system, taken from Lazy Foo's tutorial and mixed with sdltutorials code.
In brief it :

Loads the bitmap font image onto a SDL_Surface, converting the image using SDL_DisplayFormat .
Stores the clipping data for each character in an static array of SDL_Rect objects
Uses ASCII values of each character to reference the relevant SDL_Rect object to provide clipping data to the draw function
Draw function is the typical blit from source ( the surface holding the font image) to destination ( main display).

video is set using SDL_HWSURFACE | SDL_DOUBLEBUF flags.

I realise it is a lot of looping and work to put up an image for each character in a string of text, but losing 200+ FPS for loosely spaced text on a screen normal ?

Question: Is a huge loss of updates per second to be expected when dsplaying a lot of text on the screen using a bitmap font system ?
Is there anyway to avoid this loss in SDL / C++ ?

C++ and SDL : Pointers and objects, acessing classes.

17 November 2010 - 07:21 AM


I am STILL battling to work out how to access classes, the methods I am currently using are messy and make for really convuluted code in my opinion.

I would really appreciate some sources for refrence to methods used to store object pointers, lists and message ID's ( or whatever methods other programmers use to access thier classes and the class functions, beyond the basic declare object or make a simple pointer )

NOTE: I dont need to know when or why use classes, I want to learn how to access objects, the class functions and data more efficiently.
Currently I use the normal declare object and access via the object membership operator , or a declare a object and send a pile of pointers to that objects constructor so it can set its pointers so it can access other classes.


In main I would have a World mWorld( &mPlayer , &mAli , &mIO ... more &'s).
In the World.h file I would have a list of pointers declared, and in the constructor I have a pack of pointers = parameter supplied pointers.

In short, I would appreciate some material to peruse that deals with programmers methods of dealing with lots of classes and lots of objects.