Page 1 of 1

Need help with pointers

Posted: May 31st, 2013, 7:40 pm
by LameGuy64
I'm currently working on a 24-bit bitmap loader for experimental purposes and I was able to get it to work but there's something wrong with my CdEasyFileRead function that has something to do with how I treat the pointers (which I'm pretty sure is incorrect).

Attached in this post is a ZIP file that contains the source code, the makefile, and the test picture in RGB24 RAW format that this snipped uses.

Image

Code:

Code: Select all

// Includes
#include <sys/types.h>
#include <memory.h>
#include <libetc.h>
#include <libgte.h>
#include <libgpu.h>
#include <libgs.h>
#include <libcd.h>
#include <stdio.h>


// Some defines
#define OT_LENGTH 8
#define PACKETMAX (2048)      
#define PACKETMAX2 (PACKETMAX*24)   

#define CDBUFFLEN 8192	// CD data buffer length


// Setup the ordering tables
GsOT myOT[2];
GsOT_TAG myOT_TAG[2][1<<OT_LENGTH];
PACKET GPUPacketArea[2][PACKETMAX2];
int ActiveOT;


// For the easy CD reading function
u_char 	CdBuff[CDBUFFLEN];	// Data buffer for easy CD loading
int	CdBufferOffs;		// Offset of the next byte to read within the buffer
int	CdBytesRead;		// The number of bytes read from the buffer


u_char RowBuff[7680];	// 640x4 24-bit chunk

// Bitmap header structure (failed)
/*
typedef struct 
{

	// Entry header
	//char 	id[2];
	short	id;
	int	size;
	short	res1;
	short	res2;
	int	offset;

	// Bitmap header
	int	hsize;
	int	width;
	int	height;
	short	planes;
	short	bpp;
	int	compression;
	int	imagebytes;
	int	xres;
	int	yres;
	int	numcolors;
	int	sigcolors;
	
} bmpheader_struct;

bmpheader_struct bmpheader;
*/


// Prototypes
int main(void);
void Init(void);
void Display(void);
void PrepDisplay(void);
void CdEasyReadFile(char *filename, u_long *destptr, u_long NumBytes);


int main(void)
{

	RECT	RowRect = { 0, 0, 640*3/2, 4 };
	u_char	cdresult=0;

	int vy;

	// Init everything
	Init();


	// Read the first 4 rows of the bitmap
	//CdReadFile("\\bm24test.raw;1", (u_long*)&CdBuff[0], 8192);
	//CdReadSync(0, result);

	CdEasyReadFile("\\bm24test.raw;1", (u_long*)&RowBuff[0], 7680);

	for (vy=0; vy<479; vy=vy+4)
	{

		// Load the chunk into the framebuffer
		RowRect.y = vy;
		LoadImage(&RowRect, (u_long*)&RowBuff[0]);
		DrawSync(0);

		// Load more chunks
		CdEasyReadFile(0, (u_long*)&RowBuff[0], 7680);

	};

}


void Init(void)
{

	DISPENV disp;

	// Initialize the graphics
	ResetGraph(0);
	SetGraphDebug(0);

	SetDefDispEnv(&disp, 0, 0, 640, 480);
	disp.isrgb24 = 1;
	PutDispEnv(&disp);
	SetDispMask(1);

	/*
	GsInitGraph(320, 240, GsNONINTER|GsOFSGPU, 1, 0);
	GsDefDispBuff(0, 0, 0, 240);

	myOT[0].length = OT_LENGTH;
	myOT[1].length = OT_LENGTH;
	myOT[0].org = myOT_TAG[0];
	myOT[1].org = myOT_TAG[1];

	GsClearOt(0, 0, &myOT[0]);
	GsClearOt(0, 0, &myOT[1]);

	ResetGraph(0);
	SetVideoMode(MODE_NTSC);
	SetGraphDebug(0);

	// Clear the screens
	GsSortClear(0, 0, 0, &myOT[0]);
	GsSortClear(0, 0, 0, &myOT[1]);

	// Initialize debug font
	FntLoad(960, 256);
	FntOpen(0, 0, 320, 240, 0, 512);
	*/


	// Initialize the CD drive
	CdInit();

}


void Display(void)
{

	FntFlush(-1);
	DrawSync(0);

	VSync(0);
	GsSwapDispBuff();
	GsSortClear(0, 0, 0, &myOT[ActiveOT]);
	GsDrawOt(&myOT[ActiveOT]);

}


void PrepDisplay(void)
{

	ActiveOT = GsGetActiveBuff();
	GsSetWorkBase((PACKET*)GPUPacketArea[ActiveOT]);
	GsClearOt(0, 0, &myOT[ActiveOT]);

}


void CdEasyReadFile(char *filename, u_long *destptr, u_long NumBytes)
{

	int 	CdDestOffs=0;
	int 	CdCopyBytes=0;
	u_char	CdResult=0;

	/*
	if (filename > 0)
	{
		CdBytesRead = 0;
		CdBufferOffs = 0;
	};
	*/

	while (1)
	{

		if (CdBytesRead <= 0)
		{
			CdReadFile(filename, (u_long*)&CdBuff[0], CDBUFFLEN);
			CdReadSync(0, &CdResult);

			CdBytesRead = CDBUFFLEN;
			CdBufferOffs = 0;
		};


		CdCopyBytes = (NumBytes - CdDestOffs);

		if ((CdDestOffs + CdCopyBytes) > CdBytesRead)	CdCopyBytes = CdBytesRead;
		if ((CdDestOffs + CdCopyBytes) > NumBytes) 	CdCopyBytes = NumBytes - CdDestOffs;

		memcpy(destptr + CdDestOffs, &CdBuff[CdBufferOffs], CdCopyBytes);


		CdBufferOffs = CdBufferOffs + CdCopyBytes;
		CdDestOffs = CdDestOffs + CdCopyBytes;

		CdBytesRead = CdBytesRead - CdCopyBytes;

		if (CdDestOffs >= NumBytes)	break;

	};

}

Re: Need help with pointers

Posted: May 31st, 2013, 10:56 pm
by t0rxe
There may be a problem in your CD reading routine -> buffer storage. I will load this over my Xplorer soon and see if the problem persists and check out the code more.

Re: Need help with pointers

Posted: June 1st, 2013, 12:20 am
by bizarro2011
someone has an editor with image file extension Bs?

Re: Need help with pointers

Posted: June 1st, 2013, 2:00 am
by LameGuy64
bizarro2011 wrote:someone has an editor with image file extension Bs?
Unfortunately, you can't edit those files but you can convert a bitmap image into a BS image using MC32 in your bin folder. BS files are like JPEG images except its MDEC compatible and the file format is most likely similar to STR video files only supporting one frame. The downside is, its as lossy as JPEG and loading such files on the PlayStation might be tricky as it involves using the MDEC processor.

Re: Need help with pointers

Posted: June 1st, 2013, 4:59 am
by Orion_
I have a BS image loader in my library, you can check the code in BSdec.c

Re: Need help with pointers

Posted: June 1st, 2013, 1:48 pm
by t0rxe
Awesome. Thanks for the post guys! I will take a look at it.

Re: Need help with pointers

Posted: June 1st, 2013, 6:20 pm
by LameGuy64
Did you guys forget about helping me with this pointer problem? No one seems to cooperate about it at all! Oh well, time to scrap this little project.

I don't really know if this is the proper way to manipulate a pointer:

Code: Select all

memcpy(destptr + CdDestOffs, &CdBuff[CdBufferOffs], CdCopyBytes);
Or no one gives a crap about it...

Re: Need help with pointers

Posted: June 1st, 2013, 7:47 pm
by t0rxe
Looks alight to me. On another note, I tried loading your example over my xplorer, and it failed to display correctly.
Anyway, I got Sony's example working, so I'll remove all the garbage from it and share it soon.

Re: Need help with pointers

Posted: June 1st, 2013, 8:39 pm
by LameGuy64
Have you tried burning the raw image file included in the attached zip file into a CD to get my code to work?

EDIT: Never mind. I managed to fix the problem myself (I used an incorrect pointer type for destptr)...I'll release the fixed code once I add a file selector.

Re: Need help with pointers

Posted: June 1st, 2013, 9:28 pm
by t0rxe
No, I didn't burn it to a disc because I am low on them at the moment...

Anyway, here RGB24 all working
http://www.psxdev.net/forum/viewtopic.p ... 2283#p2307