SDL -- Incorrect output value

Started by
12 comments, last by vman 17 years, 10 months ago
Thanks zahlman ;).

Because i hate copy & paste , i edited your code to this :

Quote:
//if surface == null
if (!stats_text){
stats_text = TTF_RenderText_Blended(font_name,
sLife.c_str(), txt_col );
//update surface :x,y,surface, @ screen
UPDATE_POSITION( 0, 0, stats_text, screen );
}

//if stats_text !=null
if (stats_text){
//release = sdl_freesurface()
RELEASE_( stats_text );
//set it to null
stats_text = NULL;
}

There are no problems now :).
Advertisement
Hi again [totally].

This time i wont ask for help , just for a suggestion.

I've got this code (please read the comment lines)

//------------------- FUNCTION LOAD() -------------\\
//Insert 100 image objects into Enemy array.//max data = 100for (int en=0;en<101;en++){    //load the image & remove the background color    Enemy[en] = load_image("Data\\enemyA.png");    //if enemy array surface !=null    if (Enemy[en]){    //set the EnemyX array position to Screen_width    // +en * its width.    //max data =100    EnemyX[en] =SCREEN_WIDTH +en *Enemy[en]->w;}}

//------------------- -------------- -------------\-
//------------------- MOVE ENEMY() -------------\\
//---This is just a test...i'll add more movement rutines later..   //move only the 1/10 wave of enemies    for (int e=0;e<11;e++)                           {                 // increase x pos forward by [1]                EnemyX[e] -=1;                 //update surface..    UPDATE_POSITION( EnemyX[e], EnemyY[e], Enemy[e], screen );                           }

//------------------- -------------- -------------\My question is : Should i release(using Free_surface()) the
surface array ? or no?
Im asking because i dont want to cause memory leaks on slow machines.

And another thing...
Im going to write a simple collision detection function , and i would like to
know what do i have to do in order to hide the enemy's object.
I think i have to remove the BLIT_SURFACE() while applying surface's position.
Am i right?
---------------------------

Thats it [dead].
Sorry for asking alot , but its the first time im writing a game (well , im on the 6-7th day) , so please understand me [wink].

ThankS.

PS: Here's a screenshot:
:)
{direct link:http://img81.imageshack.us/img81/9251/ahunter22hp.jpg}
The rewrite misses the point of my code (although it should still work).

When you call an SDL creation function, it allocates some stuff and gives you a pointer to the allocated stuff. Later you clean up the stuff with a matching SDL function.

If you iteratively allocate something (i.e. reassigning the pointer to the result of another call), you need to iteratively clean it up (i.e. cleaning up with the current pointer value before you reassigne it). However, you could swap two pointer values without problem (assuming you swap them correctly).

If you allocate something once and *use* the resource multiple times, then you can keep the pointer set as it is, and only clean up when you're done with your uses.

In the case of the Enemy array, assuming you just leave all those entries in the array alone during the "main loop", then you would just free the surfaces after the main loop, matching the creation before the main loop.

In the code I provided, the idea is to track what the current resource *represents*, so that you can use it several times until you determine that you need it to represent something else. When that happens, you can clean up that resource, and recreate a new one representing the new thing ("thing" here being the rendered text "Life: " followed by the numeric data).
Thanks for the information.
I'll have it in mind !.

This topic is closed to new replies.

Advertisement