I need help for gfx management

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
plebihan
Posts: 10
Joined: Sun Jul 16, 2006 11:29 pm

I need help for gfx management

Post by plebihan »

Hi all,
i'm coding a new version of my HB , Picoozir, and i would like to add some gfx features. Here is a piece of code that allows to select a chopper with the left and right arrows, the next chopper scrolls on the screen. I dont know why but it works 5 or 6 times and the PSP crashs. I tried with SDL and with PSPGU/Grpahics.h but it's the same . I think i got memory management troubles . Could someone help me to handle an understand the gfx secret :)

Here are my 2 functions :

Code: Select all

void Scrolling_Helico(char Sens[10], int num_helico)
{
int heli_precedent;
int i=0;
Image *backgin;
Image *backgheli;

char Path_Heli_In[100];
	
backgheli = loadImage("Images/fond_heli.png");	
	clearScreen(0x000000);
	blitImageToScreen(0,0,480,272,backgheli,0,0);

	
	if (Sens=="Gauche") 
	{
		heli_precedent=num_helico+1;
		blitAlphaImageToScreen(0,0,480,272,fleche_gauche_enfoncee,0,0);	
		blitAlphaImageToScreen(0,0,480,272,fleche_droite,0,0);	
		flipScreen();
	}
	
	if (Sens=="Droite")
	{
		heli_precedent=num_helico-1;
		blitAlphaImageToScreen(0,0,480,272,fleche_gauche,0,0);	
		blitAlphaImageToScreen(0,0,480,272,fleche_droite_enfoncee,0,0);	
		flipScreen();
		
	}

	sprintf(Path_Heli_In,"Images/select_%d.png",num_helico);
	backgin = loadImage(Path_Heli_In);
	
	if (Sens == "Droite")
	{
		
		for &#40;i=0;i<400;i=i+30&#41;
		&#123;
			blitImageToScreen&#40;0,0,480,272,backgheli,0,0&#41;;	
			blitAlphaImageToScreen&#40;480-i,0,i,272,backgin,0,0&#41;;	
			blitAlphaImageToScreen&#40;0,0,480,272,fleche_gauche,0,0&#41;;	
			blitAlphaImageToScreen&#40;0,0,480,272,fleche_droite_enfoncee,0,0&#41;;	
			sceDisplayWaitVblankStart&#40;&#41;;
			flipScreen&#40;&#41;;
		&#125;	
		
		for &#40;i=400;i<480;i=i+5&#41;
		&#123;
			blitImageToScreen&#40;0,0,480,272,backgheli,0,0&#41;;			
			blitAlphaImageToScreen&#40;480-i,0,i,272,backgin,0,0&#41;;
			blitAlphaImageToScreen&#40;0,0,480,272,fleche_gauche,0,0&#41;;	
			blitAlphaImageToScreen&#40;0,0,480,272,fleche_droite_enfoncee,0,0&#41;;	
			sceDisplayWaitVblankStart&#40;&#41;;
			flipScreen&#40;&#41;;
		&#125;
	
	&#125;				

	if &#40;Sens == "Gauche"&#41;
	&#123;
		
		for &#40;i=0;i<400;i=i+30&#41;
		&#123;
	
			blitImageToScreen&#40;0,0,480,272,backgheli,0,0&#41;;	
			blitAlphaImageToScreen&#40;0,0,i,272,backgin,480-i,0&#41;;	
			blitAlphaImageToScreen&#40;0,0,480,272,fleche_gauche_enfoncee,0,0&#41;;	
			blitAlphaImageToScreen&#40;0,0,480,272,fleche_droite,0,0&#41;;	
			sceDisplayWaitVblankStart&#40;&#41;;
			flipScreen&#40;&#41;;
		&#125;
		
		for &#40;i=400;i<480;i=i+5&#41;
		&#123;
			blitImageToScreen&#40;0,0,480,272,backgheli,0,0&#41;;	
			blitAlphaImageToScreen&#40;0,0,i,272,backgin,480-i,0&#41;;	
			blitAlphaImageToScreen&#40;0,0,480,272,fleche_gauche_enfoncee,0,0&#41;;	
			blitAlphaImageToScreen&#40;0,0,480,272,fleche_droite,0,0&#41;;				
			sceDisplayWaitVblankStart&#40;&#41;;
			flipScreen&#40;&#41;;
		&#125;
	&#125;
&#125;

void Choix_Helico&#40;&#41;
&#123;
int num_helico=1;
char Path_Heli_Transp&#91;100&#93;;
bool redessiner=true;	
Image *fond;
Image *heli_transp;
fond = loadImage&#40;"Images/fond_heli.png"&#41;;
heli_transp = loadImage&#40;"Images/select_1.png"&#41;;	

	
while &#40;1&#41;
	&#123;
	blitImageToScreen&#40;0,0,480,272,fond,0,0&#41;;
	blitAlphaImageToScreen&#40;0,0,480,272,heli_transp,0,0&#41;;
	blitAlphaImageToScreen&#40;0,0,480,272,fleche_gauche,0,0&#41;;
	blitAlphaImageToScreen&#40;0,0,480,272,fleche_droite,0,0&#41;;
	
	//guStart&#40;&#41;;		
	//if &#40;Langue==1&#41;
	//	&#123;
	//		intraFontPrint&#40;ltn1, 140, 17, "Bienvenue dans PicoozIR"&#41;;
	//		intraFontPrint&#40;ltn1, 360, 260, "By Pacopad"&#41;;
	//	&#125;
	//	if &#40;Langue==2&#41;	
	//	&#123;
	//		intraFontPrint&#40;ltn1, 140, 17, "Welcome in PicoozIR"&#41;;
	//		intraFontPrint&#40;ltn1, 360, 260, "By Pacopad"&#41;;
	//	&#125;
	//sceGuFinish&#40;&#41;;
	//sceGuSync&#40;0,0&#41;;
		sceDisplayWaitVblankStart&#40;&#41;;
	flipScreen&#40;&#41;;
		
	u32 buttons;
		while &#40;&#40;buttons = PollButtons&#40;NULL&#41;&#41; == 0&#41;
			sceDisplayWaitVblankStart&#40;&#41;; 
		if &#40;buttons & PSP_CTRL_SELECT&#41;
            	break;
		if &#40;buttons & PSP_CTRL_TRIANGLE&#41;
			&#123;
		      	Pilotage_Picooz&#40;&#41;;
		    //	Recharge_Fond&#40;num_helico&#41;;
			&#125;
		if &#40;buttons & PSP_CTRL_CROSS&#41;
			&#123;
			switch &#40;num_helico&#41;
				&#123;
					case 1&#58;
						Pilotage_Picooz&#40;&#41;;
						break;
					case 2&#58;
						Pilotage_Picooz_Sky&#40;&#41;;
						break;
					case 3&#58;
						Pilotage_Atlas&#40;&#41;;
						break;
					case 4&#58;
						Pilotage_Tandem&#40;&#41;;
						break;
					case 5&#58;
						Pilotage_Fairy&#40;&#41;;
						break;
				&#125;
			redessiner=true;	
			&#125;
		
		if &#40;buttons & PSP_CTRL_SQUARE&#41;
			&#123;
		    //	Pilotage_Picooz_MX&#40;&#41;;
			//Recharge_Fond&#40;num_helico&#41;;
			&#125;
		
		if &#40;buttons & PSP_CTRL_CIRCLE&#41;
			&#123;
		    //	Pilotage_Picooz_Sky&#40;&#41;;
			//Recharge_Fond&#40;num_helico&#41;;
			&#125;
		if &#40;buttons & PSP_CTRL_LEFT&#41;
			&#123;
			if &#40;num_helico>1&#41;
				&#123;
					num_helico--;
					Scrolling_Helico&#40;"Gauche",num_helico&#41;;
					
				&#125;
				sprintf&#40;Path_Heli_Transp,"Images/select_%d.png",num_helico&#41;;
				heli_transp=loadImage&#40;Path_Heli_Transp&#41;;
				
			&#125;
		if &#40;buttons & PSP_CTRL_RIGHT&#41;
		&#123;
			if &#40;num_helico<4&#41;
				&#123;
					num_helico++;
					Scrolling_Helico&#40;"Droite",num_helico&#41;;
					
				&#125;
			
			sprintf&#40;Path_Heli_Transp,"Images/select_%d.png",num_helico&#41;;
			heli_transp=loadImage&#40;Path_Heli_Transp&#41;;
			
		&#125;
		
	&#125;

&#125;


[/quote]
User avatar
bkc
Posts: 19
Joined: Tue May 20, 2008 8:45 pm
Location: Sweden
Contact:

Post by bkc »

Because you run out of memory :-)
you load the image over and over again without freeing it :-)
Try

Code: Select all

freeImage&#40;fond&#41;;
freeImage&#40;heli_transp&#41;;
at the end of Choix_Helico()

That should work :-)
I only speak these languages:
  • C / C + +
  • (x)HTML
  • PHP
  • CSS
  • SQL
  • JavaScript
plebihan
Posts: 10
Joined: Sun Jul 16, 2006 11:29 pm

Post by plebihan »

Thank you, i'll found the matter and i got a memory probelm.
i'm porting my app with oslib and i try to correct all the gfx part

Pacopad
User avatar
Jim
Posts: 476
Joined: Sat Jul 02, 2005 10:06 pm
Location: Sydney
Contact:

Post by Jim »

Code: Select all

char Sens&#91;10&#93;;
...
if &#40;Sens == "Droite"&#41;
This is not how you compare strings in C. You use strcmp. It's probably working only because your compiler is making all the instances of "Droit' point to the same constant string, but you shouldn't rely on that.

Jim
User avatar
Torch
Posts: 825
Joined: Wed May 28, 2008 2:50 am

Post by Torch »

You should make a generic menu "engine" instead of hard coding it for your particular game.
plebihan
Posts: 10
Joined: Sun Jul 16, 2006 11:29 pm

Post by plebihan »

Thank you for your advices guys. It's my first dev on psp and my firt experience in C language.

@Torch: Each chopper has particularity, so to let the code readable , i cant , for the momoent, create a global engine fonction.

@JIM: Thanx , il'll replace all string test i do with strcmp

Cheers

Pacopad
hlide
Posts: 739
Joined: Sun Sep 10, 2006 2:31 am

Post by hlide »

Jim wrote:

Code: Select all

char Sens&#91;10&#93;;
...
if &#40;Sens == "Droite"&#41;
This is not how you compare strings in C. You use strcmp. It's probably working only because your compiler is making all the instances of "Droit' point to the same constant string, but you shouldn't rely on that.

Jim
Or you may have global constant zero strings :

Code: Select all

constants.h&#58;
extern char const * const C_GAUCHE; // Left
extern char const * const C_DROITE; // Right

constants.c&#58;
char const * const C_GAUCHE = "Gauche";
char const * const C_DROITE = "Droite";

your c files&#58;
#include "constants.h"

...
if &#40;Sens == C_GAUCHE&#41; ...
this way this is faster than a strcmp (just an integer comparison).

Of course, a simple enum can work too if a string is not necessary :

Code: Select all

enum SensEnumeration &#123; Gauche, Droite &#125;;
EDIT:

this line seems to be wrong for me :
void Scrolling_Helico(char Sens[10], int num_helico)

it should be :
void Scrolling_Helico(char const * const Sens, int num_helico)
or :
void Scrolling_Helico(SensEnumeration Sens, int num_helico)
or :
void Scrolling_Helico(int Sens, int num_helico)
Post Reply