Help With Test Demo App

Start a work log and update it occasionally with your projects progress
Post Reply
Estacaco
Interested PSXDEV User
Interested PSXDEV User
Posts: 6
Joined: Oct 30, 2012

Help With Test Demo App

Post by Estacaco » November 1st, 2012, 4:55 pm

Alright so I've been playing around with some code and I can't figure out why my sprite eship (enemy ship) will move right across the screen but will not change directions. Written in C for PSXSDK.

Code: Select all

#include <stdio.h>
#include <psxgpu.h>
#include <psx.h>

GsDispEnv game_dispenv;
GsDrawEnv game_drawenv;

GsImage game_image;
GsSprite ship;
GsSprite shot;
GsSprite eship;

int refresh_needed=0;
int emove=true;

unsigned char file_buffer[0x30000];
unsigned int prim_list[4000];

int ship_coordinates[2];
int shot_coordinates[2];
int eship_coordinates[1];

void load_file_into_buffer(char *fname)
{
	FILE *f;
	int sz, fd;

	f = fopen(fname, "rb");

	fseek(f, 0, SEEK_END);	
	sz = ftell(f);

	fseek(f, 0, SEEK_SET);

	printf("%s (%d)\n", fname, sz);
	
	fread(file_buffer, sizeof(char), sz, f);
	
	fclose(f);
}

void InitializePSX (){
PSX_Init();
	
	GsInit();
	GsClearMem();
	GsSetVideoMode(320,240, VMODE_NTSC);
	
	game_dispenv.x=0;
	game_dispenv.y=0;
	
	GsSetDispEnv(&game_dispenv);
	
	game_drawenv.x=0;
	game_drawenv.y=0;
	
	game_drawenv.w=320;
	game_drawenv.h=240;
	
	game_drawenv.draw_on_display=1;
	game_drawenv.ignore_mask=0;
	game_drawenv.dither=0;
	game_drawenv.set_mask=0;
	
	GsSetDrawEnv(&game_drawenv);
	GsSetList(prim_list);
	
}
void ClearScreen(){
	
GsRectangle gs_rect;
gs_rect.x = 0;
	gs_rect.y = 0;
	gs_rect.w = 320;
	gs_rect.h = 240;
	gs_rect.r = 0;
	gs_rect.g = 0;
	gs_rect.b = 0;
	gs_rect.attribute = 0;	
	
	GsSortRectangle(&gs_rect);
	
	GsDrawList();
	while(GsIsDrawing());

}
void DoubleBuffering(){
if(game_dispenv.y == 0)
		{
			game_dispenv.y = 256;
			game_drawenv.y = 0;
		}
		else
		{
			game_dispenv.y = 0;
			game_drawenv.y = 256;
		}
			
		GsSetDispEnv(&game_dispenv);
		GsSetDrawEnv(&game_drawenv);
			
	}
void game_vblank_handler(){
	refresh_needed++;
}
void ShowFrameBuffer(int ShowIt){
	
	if (ShowIt==0)
	GsSetVideoMode(320,240, VMODE_NTSC);
	if (ShowIt==1)
	GsSetVideoMode(640,480, VMODE_NTSC);
}
void GamePadPlayerOneRead(){
	
	unsigned short pad1, pad2;
	
	PSX_ReadPad(&pad1, &pad2);
	
	if (pad1 & PAD_LEFT){
	if (ship_coordinates[0] > 6){
		ship_coordinates[0]-=3; 
		}else ;
	}
	if (pad1 & PAD_RIGHT){
	if (ship_coordinates[0] < 284){
		ship_coordinates[0]+=3;
		}else ;
	}
	if (pad1 & PAD_UP){
	if (ship_coordinates[1] > 6){
		ship_coordinates[1]-=3;
		}else;
	}
	if (pad1 & PAD_DOWN){
	if (ship_coordinates[1] < 194){
		ship_coordinates[1]+=3;
		}else;
	}
	if (pad1 & PAD_CROSS){
	
	if (shot_coordinates[1] < 0){
		shot_coordinates[0] = ship_coordinates[0]+11;
		shot_coordinates[1] = ship_coordinates[1]-17;
		}else;
	}
	
}

void GameDrawAllSprites(){
	
	ClearScreen();
		
		while(GsIsDrawing());
		
		ship.x=ship_coordinates[0];
		ship.y=ship_coordinates[1];
		shot.x=shot_coordinates[0];
		shot.y=shot_coordinates[1]-=6;
		if (emove=true){
		eship.x=eship_coordinates[0]+=1;
		}
		if (emove=false){
		eship.x=eship_coordinates[0]-=1;
		}
		if (eship.x > 208){
		emove=false;
		}else; emove=true;
		if (shot_coordinates[1] <= -15){
	shot_coordinates[1] = -50;
	}else;
		
		GsSortSprite(&ship);
		GsSortSprite(&shot);
		GsSortSprite(&eship);
	
		GsDrawList();
		while(GsIsDrawing());
		
		DoubleBuffering();
		
	}

int main(){

	InitializePSX();
	
	SetVBlankHandler(game_vblank_handler);
	
	ship_coordinates[0]=145;
    ship_coordinates[1]=155;
	shot_coordinates[0]=-50;
	shot_coordinates[1]=-50;
	eship_coordinates[0]=0;

	GsLoadFont(320, 240, 360, 240);
	
	load_file_into_buffer("cdrom:ship.TIM;1");
	GsImageFromTim(&game_image,file_buffer); 
	GsUploadImage(&game_image); 
	
	GsSpriteFromImage(&ship,&game_image,1);
	
	ship.x=ship_coordinates[0];
	ship.y=ship_coordinates[1];
	ship.w=30;
	ship.h=40;
	
	GsSortSprite(&ship);
	
	load_file_into_buffer("cdrom:shot.TIM;1");
	GsImageFromTim(&game_image,file_buffer); 
	GsUploadImage(&game_image); 
	
	GsSpriteFromImage(&shot,&game_image,1);
	
	shot.x=ship_coordinates[0]+=8;
	shot.y=ship_coordinates[1]-=-15;
	shot.w=7;
	shot.h=17;

	
	GsSortSprite(&shot);
	
	load_file_into_buffer("cdrom:eship.TIM;1");
	GsImageFromTim(&game_image,file_buffer);
	GsUploadImage(&game_image);
	
	GsSpriteFromImage(&eship,&game_image,1);
	
	eship.x=eship_coordinates[0];
	eship.y=0;
	eship.w=25;
	eship.h=35;
	
	GsSortSprite(&eship);
	
	GsDrawList();
	while(GsIsDrawing());
	
	while(1){
		while (refresh_needed){
			
		GamePadPlayerOneRead();
		
		GameDrawAllSprites();
		
	
	
		refresh_needed--;
		}
	}
}
Any help appreciated!

Xavi92
Verified
C Programming Expert
C Programming Expert
Posts: 161
Joined: Oct 06, 2012
PlayStation Model: SCPH-5502
Contact:

Post by Xavi92 » November 1st, 2012, 9:02 pm

Be careful using true and false values when writing in C, since they aren't supported natively. Instead, use 1 or 0, or write on the top of your source code:

Code: Select all

#define true 1
#define false 0
Apart from that, I've seen there are some syntax mistakes here:

Code: Select all

if (emove=true){
      eship.x=eship_coordinates[0]+=1;
      }
      if (emove=false){
      eship.x=eship_coordinates[0]-=1;
      }
      if (eship.x > 208){
      emove=false;
      }else; emove=true;
Comparing values in C needs a == symbol instead of a single =. Moreover, from what I've read, you want your enemy spaceship to change direction at x>208 setting emove to false to make it to go left. If you use an else condition, that means that, if x<=208 (just the opposite condition), emove will be set to true again. So that means your enemy spaceship will be stucked in x=208 forever, since emove will be changing value constantly. BTW, don't write "else" if there's no real need to.

Here is a corrected version of your code:

Code: Select all

#include <stdio.h>
#include <psxgpu.h>
#include <psx.h>

#define true 1
#define false 0

GsDispEnv game_dispenv;
GsDrawEnv game_drawenv;

GsImage game_image;
GsSprite ship;
GsSprite shot;
GsSprite eship;

int refresh_needed=0;
int emove=true;

unsigned char file_buffer[0x30000];
unsigned int prim_list[4000];

int ship_coordinates[2];
int shot_coordinates[2];
int eship_coordinates[1];

void load_file_into_buffer(char *fname)
{
   FILE *f;
   int sz, fd;

   f = fopen(fname, "rb");

   fseek(f, 0, SEEK_END);   
   sz = ftell(f);

   fseek(f, 0, SEEK_SET);

   printf("%s (%d)\n", fname, sz);
   
   fread(file_buffer, sizeof(char), sz, f);
   
   fclose(f);
}

void InitializePSX (){
PSX_Init();
   
   GsInit();
   GsClearMem();
   GsSetVideoMode(320,240, VMODE_NTSC);
   
   game_dispenv.x=0;
   game_dispenv.y=0;
   
   GsSetDispEnv(&game_dispenv);
   
   game_drawenv.x=0;
   game_drawenv.y=0;
   
   game_drawenv.w=320;
   game_drawenv.h=240;
   
   game_drawenv.draw_on_display=1;
   game_drawenv.ignore_mask=0;
   game_drawenv.dither=0;
   game_drawenv.set_mask=0;
   
   GsSetDrawEnv(&game_drawenv);
   GsSetList(prim_list);
   
}
void ClearScreen(){
   
GsRectangle gs_rect;
gs_rect.x = 0;
   gs_rect.y = 0;
   gs_rect.w = 320;
   gs_rect.h = 240;
   gs_rect.r = 0;
   gs_rect.g = 0;
   gs_rect.b = 0;
   gs_rect.attribute = 0;   
   
   GsSortRectangle(&gs_rect);

}

void DoubleBuffering(){
if(game_dispenv.y == 0)
      {
         game_dispenv.y = 256;
         game_drawenv.y = 0;
      }
      else
      {
         game_dispenv.y = 0;
         game_drawenv.y = 256;
      }
         
      GsSetDispEnv(&game_dispenv);
      GsSetDrawEnv(&game_drawenv);
         
   }
   
void game_vblank_handler(){
   refresh_needed=1;
}

void GamePadPlayerOneRead(){
   
   unsigned short pad1, pad2;
   
   PSX_ReadPad(&pad1, &pad2);
   
   if (pad1 & PAD_LEFT){
		if (ship_coordinates[0] > 6)
			ship_coordinates[0]-=3; 
   }
   
   if (pad1 & PAD_RIGHT){
		if (ship_coordinates[0] < 284)
			ship_coordinates[0]+=3;
   }
   
   if (pad1 & PAD_UP){
		if (ship_coordinates[1] > 6)
			ship_coordinates[1]-=3;
   }
   if (pad1 & PAD_DOWN){
		if (ship_coordinates[1] < 194)
			ship_coordinates[1]+=3;
   }
   
   if (pad1 & PAD_CROSS){
		if (shot_coordinates[1] < 0){
			shot_coordinates[0] = ship_coordinates[0]+11;
			shot_coordinates[1] = ship_coordinates[1]-17;
      }
   }
   
}

void GameDrawAllSprites(){
   
	  ClearScreen();
      
      ship.x=ship_coordinates[0];
      ship.y=ship_coordinates[1];
      shot.x=shot_coordinates[0];
      shot.y=shot_coordinates[1]-=6;
      
      if (emove==true)
      eship.x=eship_coordinates[0]+=1;
      
      if (emove==false)
      eship.x=eship_coordinates[0]-=1;
      
      if (eship.x > 208) //If x>208, the spaceship turns left
      emove = false;
     
	  if (eship.x <= 20) //If x<=20, the spaceship turns right
	  emove = true;
	  
	  
      if (shot_coordinates[1] <= -15)
		shot_coordinates[1] = -50;
      
      GsSortSprite(&ship);
      GsSortSprite(&shot);
      GsSortSprite(&eship);
   
      GsDrawList();
      while(GsIsDrawing());
          
      DoubleBuffering();
      
   }

int main(){

   InitializePSX();
   
   SetVBlankHandler(game_vblank_handler);
   
   ship_coordinates[0]=145;
    ship_coordinates[1]=155;
   shot_coordinates[0]=-50;
   shot_coordinates[1]=-50;
   eship_coordinates[0]=0;

   GsLoadFont(320, 240, 360, 240);
   
   load_file_into_buffer("cdrom:ship.TIM;1");
   GsImageFromTim(&game_image,file_buffer); 
   GsUploadImage(&game_image); 
   
   GsSpriteFromImage(&ship,&game_image,1);
   
   ship.x=ship_coordinates[0];
   ship.y=ship_coordinates[1];
   ship.w=30;
   ship.h=40;
   
   GsSortSprite(&ship);
   
   load_file_into_buffer("cdrom:shot.TIM;1");
   GsImageFromTim(&game_image,file_buffer); 
   GsUploadImage(&game_image); 
   
   GsSpriteFromImage(&shot,&game_image,1);
   
   shot.x=ship_coordinates[0]+=8;
   shot.y=ship_coordinates[1]-=-15;
   shot.w=7;
   shot.h=17;

   
   GsSortSprite(&shot);
   
   load_file_into_buffer("cdrom:eship.TIM;1");
   GsImageFromTim(&game_image,file_buffer);
   GsUploadImage(&game_image);
   
   GsSpriteFromImage(&eship,&game_image,1);
   
   eship.x=eship_coordinates[0];
   eship.y=0;
   eship.w=25;
   eship.h=35;
   
   GsSortSprite(&eship);
   
   GsDrawList();
   while(GsIsDrawing());
   
   while(1){
      while (refresh_needed){
         
      GamePadPlayerOneRead();
      
      GameDrawAllSprites();
   
      refresh_needed=0;
      }
   }
}

Some more considerations to take into account:

· The first example I sent you was very old, from the time I started writing with PSXSDK. That means functions such as ShowMeTheBuffer() are completely useless, so you can remove them.
· Instead of writing refresh_needed++, write refresh_needed = 1. Otherwise, if your game uses too many resources in a certain moment, this variable will increase a lot, so your game won't stop being refreshed until it becomes equal to zero again. It's already fixed in the code I've attached.
· Because of the same first reason, just use GsDrawList() and while(GsIsDrawing()) once all of your primitives (sprites, rectangles, whatever) are sorted. This reduces GPU load.
· Using arrays such as eship_coordinates[2] aren't really needed. I know they're used in my example, but, again, it was made when I was a newbie. :P

Estacaco
Interested PSXDEV User
Interested PSXDEV User
Posts: 6
Joined: Oct 30, 2012

Post by Estacaco » November 2nd, 2012, 11:53 am

That's alright. I'm a newbie right now as it is. Thanks for fixing up my code for me!

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests