Page 1 of 1

Need help with reading data off of a file

Posted: May 30th, 2013, 1:58 am
by LameGuy64
I'm currently working on a 24-bit bitmap loader to play around with the unused 24-bit color mode of the PlayStation. The problem is, I can't seem to read the 54 byte header off of a file for some reason.

The code:

Code: Select all

// Includes
#include <sys/types.h>
#include <libapi.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)   


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


// Bitmap header structure
typedef struct 
{

	// Entry header
	char 	id[2];
	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);


int main(void)
{

	int 	handle;
	u_char	cdresult;

	// Init everything
	Init();


	// Read some of the bitmap header
	CdReadFile("cdrom:\\bm24test.bmp;1", (u_long*)&bmpheader, sizeof(bmpheader));
	CdReadSync(0, &cdresult);

	
	while (1)
	{

		PrepDisplay();

		FntPrint("\nTest...\n");
		FntPrint("cdresult=%d\n", cdresult);

		FntPrint("\nHeader results:\n");
		FntPrint("hchar=%d\n", bmpheader.id[0]);
		FntPrint("width=%d\n", bmpheader.width);
		FntPrint("height=%d\n", bmpheader.height);

		Display();

	}

}


void Init(void)
{

	// Initialize the graphics
	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]);

}
I've also attached a zip file which contains the makefile, system.cnf file, and the test 640x480 24-bit bitmap drawn by a friend that this snippet uses.

Re: Need help with reading data off of a file

Posted: May 30th, 2013, 7:14 am
by Orion_
Reading the CdReadFile function documentation in "LIBREF46.PDF", tells you that this function reads by chunk of 2048 bytes (one cd sector)
so if you are asking only 54 bytes, it will read 2048 bytes, and corrupt some data in memory.
also, I don't think "cdrom:" is mandaroty, "\\" will work too.