Problem with sprite moving on background

Graphic based area of development (Graphics Processing Unit), including the Geometry Transform Engine (GTE), TIM, STR (MDEC), etc.
Post Reply
ORART
What is PSXDEV?
What is PSXDEV?
Posts: 4
Joined: Jan 17, 2023

Problem with sprite moving on background

Post by ORART » March 3rd, 2023, 9:04 am

Hi, i'm trying to move a blue ball image through a background. But when moving it leaves painted all the way it was directed just like this:
screen.png
Here's the code I got so far in main.c

Code: Select all

#include <stdio.h>
#include <sys/types.h>
#include <psxetc.h>
#include <psxgte.h>
#include <psxgpu.h>
#include <psxpad.h>
#include <psxapi.h>

// Define display/draw environments for double buffering
DISPENV disp[2];
DRAWENV draw[2];
int i,db;

char		pribuff[2][65536];		
uint32_t	ot[2][1];			
char		*nextpri;				
int			db = 0;		 

typedef struct {
	short x,y;
	unsigned char r,g,b;
} TIPO_OBJETO;

TIPO_OBJETO jugador;

extern const uint32_t circulo16c[];
extern u_long background[];

TIM_IMAGE tim[2];

// Init function
void init(void)
{
	// This not only resets the GPU but it also installs the library's
	// ISR subsystem to the kernel
	ResetGraph(0);
	
	// Define display environments, first on top and second on bottom
	SetDefDispEnv(&disp[0], 0, 0, 320, 240);
	//SetDefDispEnv(&disp[1], 0, 240, 320, 240);
	
	// Define drawing environments, first on bottom and second on top
	SetDefDrawEnv(&draw[0], 0, 0, 320, 240);
	//SetDefDrawEnv(&draw[1], 0, 0, 320, 240);
	
	disp[0].isinter = 1;

	// Set and enable clear color
	//setRGB0(&draw[0], 128, 96, 0);
	//setRGB0(&draw[1], 128, 96, 0);
	//draw[0].isbg = 1;
	//draw[0].dtd = 1;
	
	// Clear double buffer counter
	db = 0;
	
	// Apply the GPU environments
	PutDispEnv(&disp[db]);
	PutDrawEnv(&draw[db]);
	
	// Load test font
	FntLoad(960, 0);
	
	// Open up a test font text stream of 100 characters
	FntOpen(0, 8, 320, 224, 0, 100);

	//SetDispMask( 1 );

	GetTimInfo(circulo16c, &tim[0]);
	GetTimInfo(background, &tim[1]);

	for (i=1;i>=0;i--)
	{
		LoadImage(tim[i].prect, tim[i].paddr);
		if( tim[i].mode & 0x8 )	LoadImage(tim[i].crect, tim[i].caddr);	
	}

	jugador.x = 50;
	jugador.y = 50;
	jugador.r = 150;
	jugador.g = 100;
	jugador.b = 200;
}

// Display function
void display(void)
{
	// Flip buffer index
	//db = !db;
	
	// Wait for all drawing to complete
	DrawSync(0);
	
	// Wait for vertical sync to cap the logic to 60fps (or 50 in PAL mode)
	// and prevent screen tearing
	VSync(0);

	// Switch pages	
	PutDispEnv(&disp[db]);
	PutDrawEnv(&draw[0]);
	
	// Enable display output, ResetGraph() disables it by default
	//SetDispMask(1);
	
}

// Main function, program entrypoint
int main(int argc, const char *argv[])
{
	SPRT_16 *sprt;
	DR_TPAGE *tpri;
	
	int i,counter=0;

	uint8_t pad_buff[2][34];
	InitPAD(pad_buff[0], 34, pad_buff[1], 34);
	StartPAD();
	ChangeClearPAD(0);


	// Init stuff	
	init();
	
	while(1)
	{
		SetDispMask( 1 );
		PADTYPE *pad = (PADTYPE *) pad_buff[0];

		ClearOTagR( ot[db], 1 );
		nextpri = pribuff[db];

		sprt = (SPRT_16*)nextpri;

		setSprt16( sprt );
		setXY0( sprt, jugador.x, jugador.y );
		setRGB0( sprt, jugador.r, jugador.g, jugador.b );
		setUV0( sprt, 0, 0 );
		setClut( sprt, tim[0].crect->x, tim[0].crect->y );
		
		addPrim( ot[db], sprt );
		sprt++;
		
		if (!(pad->btn & PAD_LEFT)) jugador.x--;
		if (!(pad->btn & PAD_RIGHT)) jugador.x++;
		if (!(pad->btn & PAD_UP)) jugador.y--;
		if (!(pad->btn & PAD_DOWN)) jugador.y++;

		nextpri = (char*)sprt;

		tpri = (DR_TPAGE*)nextpri;
		setDrawTPage( tpri, 0, 0, 
			getTPage(0, 0, tim[0].prect->x, tim[0].prect->y ));
		addPrim( ot[db], tpri );
		nextpri += sizeof(DR_TPAGE);
		// Print the obligatory hello world and counter to show that the
		// program isn't locking up to the last created text stream
		FntPrint(-1, "HOLA, SOY OSCARIN VALDEZ\n");
		FntPrint(-1, "COUNTER=%d\n", counter);
		
		// Draw the last created text stream
		FntFlush(-1);
		
		//DrawSync( 0 );
		//VSync( 0 );

		// Update display
		display();
		
		//PutDrawEnv( &draw[0] );

		DrawOTag( ot[db] );

		db = !db;

		// Increment the counter
		//counter++;
	}
	
	return 0;
}
Any corrections to remove the printed way?
You do not have the required permissions to view the files attached to this post.

User avatar
nocash
Verified
PSX Aficionado
PSX Aficionado
Posts: 541
Joined: Nov 12, 2012
Contact:

Post by nocash » March 3rd, 2023, 11:20 pm

That's supposed to look like that. To avoid that effect, you must re-draw the background in each frame.

User avatar
inc^lightforce
Verified
Psy-Q Enthusiast
Psy-Q Enthusiast
Posts: 248
Joined: Mar 07, 2013
I am a: Programmer Windows+PS1, GFX Artist
PlayStation Model: Black
Location: Germany

Post by inc^lightforce » March 4th, 2023, 9:12 am

Use my latest Workshop Code. It moovs a animated sprite over the Screen. All you need , you can find in the attached package.

ORART
What is PSXDEV?
What is PSXDEV?
Posts: 4
Joined: Jan 17, 2023

Post by ORART » March 4th, 2023, 11:30 am

Solved. Put LoadImage at the end of Display function in order to reload the background on every frame.

Code: Select all

void display(void)
{
	// Flip buffer index
	//db = !db;

	// Wait for all drawing to complete
	DrawSync(0);
	
	// Wait for vertical sync to cap the logic to 60fps (or 50 in PAL mode)
	// and prevent screen tearing
	VSync(0);

	// Switch pages	
	//PutDispEnv(&disp[0]);
	PutDrawEnv(&draw[0]);
	
	// Enable display output, ResetGraph() disables it by default
	//SetDispMask(1);
	LoadImage(tim[1].prect, tim[1].paddr);
	if( tim[1].mode & 0x8 )	LoadImage(tim[1].crect, tim[1].caddr);	
}

User avatar
Shadow
Verified
Admin / PSXDEV
Admin / PSXDEV
Posts: 2670
Joined: Dec 31, 2012
PlayStation Model: H2000/5502
Discord: Shadow^PSXDEV

Post by Shadow » March 4th, 2023, 2:29 pm

ORART wrote: March 4th, 2023, 11:30 am Solved. Put LoadImage at the end of Display function in order to reload the background on every frame.

Code: Select all

void display(void)
{
	// Flip buffer index
	//db = !db;

	// Wait for all drawing to complete
	DrawSync(0);
	
	// Wait for vertical sync to cap the logic to 60fps (or 50 in PAL mode)
	// and prevent screen tearing
	VSync(0);

	// Switch pages	
	//PutDispEnv(&disp[0]);
	PutDrawEnv(&draw[0]);
	
	// Enable display output, ResetGraph() disables it by default
	//SetDispMask(1);
	LoadImage(tim[1].prect, tim[1].paddr);
	if( tim[1].mode & 0x8 )	LoadImage(tim[1].crect, tim[1].caddr);	
}
That is a horrible way to do it and will slow down the system by a severe amount. You don't need to keep loading the image from DRAM to VRAM on every CPU cycle. Once the image is in the framebuffer (IE: VRAM) it will always stay there until you clear it. Look at some examples from other users or look at examples from Sony.
Development Console: SCPH-5502 with 8MB RAM, MM3 Modchip, PAL 60 Colour Modification (for NTSC), PSIO Switch Board, DB-9 breakout headers for both RGB and Serial output and an Xplorer with CAETLA 0.34.

PlayStation Development PC: Windows 98 SE, Pentium 3 at 400MHz, 128MB SDRAM, DTL-H2000, DTL-H2010, DTL-H201A, DTL-S2020 (with 4GB SCSI-2 HDD), 21" Sony G420, CD-R burner, 3.25" and 5.25" Floppy Diskette Drives, ZIP 100 Diskette Drive and an IBM Model M keyboard.

User avatar
inc^lightforce
Verified
Psy-Q Enthusiast
Psy-Q Enthusiast
Posts: 248
Joined: Mar 07, 2013
I am a: Programmer Windows+PS1, GFX Artist
PlayStation Model: Black
Location: Germany

Post by inc^lightforce » March 6th, 2023, 8:53 am

Yes that's it Shadow.

@ORART
Your Programm will suck Up If you expand it. Redraw and clear every frame will tear down your Hardware on its knees.
Check the examples and clean your Code.


Shouts and keep on.

PS1 Coders , RS'pect these days !

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests