[C/C++ Konsole] Bitmap Laderoutine

Pyro Phoenix

Grünschnabel
Ich habe mir eine Lade/Speicherroutine für Half Life 1Sprites geschrieben.
Da ich die Sprites in Bitmaps umwandeln will benötige ich auch eine Lade/Speicherroutine
für Bitmaps. Da ich kein MFC verwende, bräuchte ich einen typedef Struct, mit dem ich
den Header etc. eines Bitmaps Auslesen/Speichern kann. Er soll aber nur aus den
Standard Datentypen bestehen. ( int, short, long, float etc. )

Kann mir jemand weiterhelfen ?

Lade/Speicherroutine für Half Life 1 Sprites

sprite.h
Code:
#define VP_PARALLEL_UPRIGHT		0
#define FACING_UPRIGHT			1
#define VP_PARALLEL				2
#define ORIENTED				3
#define VP_PARALLEL_ORIENTED	4

#define NORMAL					0
#define ADDITIVE				1
#define INDEXALPHA				2
#define ALPHTEST				3

#ifndef MAX_PATH
#define MAX_PATH 4000
#endif

typedef struct {
	unsigned char	ident[4];
	unsigned int	version;
	unsigned int	type;
	unsigned int	texFormat;
	float			boundingRadius;
	unsigned int	width;
	unsigned int	height;
	unsigned int	numFrames;
	float			beamLength;
	unsigned int	syncType;
} d_spriteHeader;

typedef struct {
	unsigned short	colCounter;
	unsigned char	colR[256];
	unsigned char	colG[256];
	unsigned char	colB[256];
} d_spritePalette;

typedef struct {
	unsigned long	frameType;
	unsigned long	numIntervall;
	float			*p_IntervallData;
	unsigned long	originX;
	unsigned long	originY;
	unsigned long	width;
	unsigned long	height;
	unsigned char	*p_frameData;
} d_frameData;

class c_sprite
{
private:
	d_spriteHeader spriteHeader;
	d_spritePalette spritePalette;
	d_frameData *frameData;

public:
	c_sprite ( char szFileName[MAX_PATH] );

	bool saveAsSingleframeSprite ( char szFileName[MAX_PATH] );
	bool saveAsGroupframeSprite ( char szFileName[MAX_PATH] );
	bool convertToGroupframeSprite ( char szFileName[MAX_PATH] );
	bool isSingleframe ();
	bool isGroupframe ();
};

sprite.cpp
Code:
#include <stdio.h>
#include <stdlib.h>
#include "sprite.h"

#pragma warning(disable : 4018)

c_sprite::c_sprite ( char szFileName[MAX_PATH] )
{
	FILE *p_spritefile;

	if ( ( p_spritefile = fopen ( szFileName, "rb" ) ) == NULL )
	{
		printf("Konnte Datei nicht Oeffnen\n\n");
	}
	else
	{
		fread ( &spriteHeader, sizeof ( d_spriteHeader ), 1, p_spritefile );
		fread ( &spritePalette.colCounter, sizeof ( unsigned short ), 1, p_spritefile );
		for ( int i = 0; i < spritePalette.colCounter; i++ )
		{
			fread ( &spritePalette.colR[i], sizeof ( unsigned char ), 1, p_spritefile );
			fread ( &spritePalette.colG[i], sizeof ( unsigned char ), 1, p_spritefile );
			fread ( &spritePalette.colB[i], sizeof ( unsigned char ), 1, p_spritefile );
		}

		frameData = new d_frameData[spriteHeader.numFrames];

		for ( int z = 0; (unsigned)z < spriteHeader.numFrames; z++ )
		{
			fread ( &frameData[z].frameType, sizeof ( unsigned long ), 1, p_spritefile );

			if ( frameData[z].frameType == 0 )
			{
				frameData[z].numIntervall = NULL;

				fread ( &frameData[z].originX, sizeof ( unsigned long ), 1, p_spritefile );
				fread ( &frameData[z].originY, sizeof ( unsigned long ), 1, p_spritefile );
				fread ( &frameData[z].width, sizeof ( unsigned long ), 1, p_spritefile );
				fread ( &frameData[z].height, sizeof ( unsigned long ), 1, p_spritefile );

				frameData[z].p_frameData = new unsigned char[frameData[z].height * frameData[z].width];
				for (int a = 0; a < frameData[z].height * frameData[z].width; a++)
				{
					fread ( &frameData[z].p_frameData[a], sizeof ( unsigned char ), 1, p_spritefile );
				}
			}
			else if ( frameData[z].frameType == 1 )
			{
				fread ( &frameData[z].numIntervall, sizeof ( unsigned long ), 1, p_spritefile );

				frameData[z].p_IntervallData = new float[frameData[z].numIntervall];
				for (int i = 0; i < frameData[z].numIntervall; i++)
				{
					fread ( &frameData[z].p_IntervallData[i], sizeof ( float ), 1, p_spritefile );
				}

				fread ( &frameData[z].originX, sizeof ( unsigned long ), 1, p_spritefile );
				fread ( &frameData[z].originY, sizeof ( unsigned long ), 1, p_spritefile );
				fread ( &frameData[z].width, sizeof ( unsigned long ), 1, p_spritefile );
				fread ( &frameData[z].height, sizeof ( unsigned long ), 1, p_spritefile );

				frameData[z].p_frameData = new unsigned char[frameData[z].height * frameData[z].width];
				for (int a = 0; a < frameData[z].height * frameData[z].width; a++)
				{
					fread ( &frameData[z].p_frameData[a], sizeof ( unsigned char ), 1, p_spritefile );
				}
			}
			else
			{
				printf("ERROR ! UNKNOWN FRAME FORMAT !\a\a\a\n\n");
			}
		}
	}
};

bool c_sprite::saveAsGroupframeSprite ( char szFileName[MAX_PATH] )
{
	FILE *p_spritefile;

	if ( ( p_spritefile = fopen ( szFileName, "wb" ) ) == NULL )
	{
		printf("Konnte Datei nicht Erstellen\n\n");
		return false;
	}
	else
	{
		fwrite ( &spriteHeader, sizeof ( d_spriteHeader ), 1, p_spritefile );
		fwrite ( &spritePalette.colCounter, sizeof ( unsigned short ), 1, p_spritefile );
		for ( int i = 0; i < spritePalette.colCounter; i++ )
		{
			fwrite ( &spritePalette.colR[i], sizeof ( unsigned char ), 1, p_spritefile );
			fwrite ( &spritePalette.colG[i], sizeof ( unsigned char ), 1, p_spritefile );
			fwrite ( &spritePalette.colB[i], sizeof ( unsigned char ), 1, p_spritefile );
		}

		for ( int z = 0; (unsigned)z < spriteHeader.numFrames; z++ )
		{
			fwrite ( &frameData[z].frameType, sizeof ( unsigned long ), 1, p_spritefile );
			fwrite ( &frameData[z].numIntervall, sizeof ( unsigned long ), 1, p_spritefile );
			for (int i = 0; i < frameData[z].numIntervall; i++)
			{
				fwrite ( &frameData[z].p_IntervallData[i], sizeof ( float ), 1, p_spritefile );
			}
			fwrite ( &frameData[z].originX, sizeof ( unsigned long ), 1, p_spritefile );
			fwrite ( &frameData[z].originY, sizeof ( unsigned long ), 1, p_spritefile );
			fwrite ( &frameData[z].width, sizeof ( unsigned long ), 1, p_spritefile );
			fwrite ( &frameData[z].height, sizeof ( unsigned long ), 1, p_spritefile );
			for (int a = 0; a < frameData[z].height * frameData[z].width; a++)
			{
				fwrite ( &frameData[z].p_frameData[a], sizeof ( unsigned char ), 1, p_spritefile );
			}
		}
	}
	return true;
};

bool c_sprite::saveAsSingleframeSprite ( char szFileName[MAX_PATH] )
{
	FILE *p_spritefile;

	if ( ( p_spritefile = fopen ( szFileName, "wb" ) ) == NULL )
	{
		printf("Konnte Datei nicht Erstellen\n\n");
		return false;
	}
	else
	{
		fwrite ( &spriteHeader, sizeof ( d_spriteHeader ), 1, p_spritefile );
		fwrite ( &spritePalette.colCounter, sizeof ( unsigned short ), 1, p_spritefile );
		for ( int i = 0; i < spritePalette.colCounter; i++ )
		{
			fwrite ( &spritePalette.colR[i], sizeof ( unsigned char ), 1, p_spritefile );
			fwrite ( &spritePalette.colG[i], sizeof ( unsigned char ), 1, p_spritefile );
			fwrite ( &spritePalette.colB[i], sizeof ( unsigned char ), 1, p_spritefile );
		}

		for ( int z = 0; (unsigned)z < spriteHeader.numFrames; z++ )
		{
			fwrite ( &frameData[z].frameType, sizeof ( unsigned long ), 1, p_spritefile );
			fwrite ( &frameData[z].originX, sizeof ( unsigned long ), 1, p_spritefile );
			fwrite ( &frameData[z].originY, sizeof ( unsigned long ), 1, p_spritefile );
			fwrite ( &frameData[z].width, sizeof ( unsigned long ), 1, p_spritefile );
			fwrite ( &frameData[z].height, sizeof ( unsigned long ), 1, p_spritefile );

			for (int a = 0; a < frameData[z].height * frameData[z].width; a++)
			{
				fwrite ( &frameData[z].p_frameData[a], sizeof ( unsigned char ), 1, p_spritefile );
			}
		}
	}
	return true;
};

bool c_sprite::convertToGroupframeSprite ( char szFileName[MAX_PATH] )
{
	FILE *p_spritefile;

	if ( ( p_spritefile = fopen ( szFileName, "wb" ) ) == NULL )
	{
		printf("Konnte Datei nicht Erstellen\n\n");
		return false;
	}
	else
	{
		if ( frameData[0].numIntervall == NULL && frameData[0].frameType == 0 )
		{
			for ( int i = 0; i < spriteHeader.numFrames; i++ )
			{
				frameData[i].frameType = 1;
				frameData[i].numIntervall = spriteHeader.numFrames;
				frameData[i].p_IntervallData = new float[spriteHeader.numFrames];
				for ( int a = 0; a < spriteHeader.numFrames; a++ )
				{
					frameData[i].p_IntervallData[a] = (( 1.0 / (float)spriteHeader.numFrames ) * (float)a);
					printf("%f ", (( 1.0 / (float)spriteHeader.numFrames ) * (float)a));
				}
			}
			fwrite ( &spriteHeader, sizeof ( d_spriteHeader ), 1, p_spritefile );
			fwrite ( &spritePalette.colCounter, sizeof ( unsigned short ), 1, p_spritefile );
			for ( int ii = 0; ii < spritePalette.colCounter; ii++ )
			{
				fwrite ( &spritePalette.colR[ii], sizeof ( unsigned char ), 1, p_spritefile );
				fwrite ( &spritePalette.colG[ii], sizeof ( unsigned char ), 1, p_spritefile );
				fwrite ( &spritePalette.colB[ii], sizeof ( unsigned char ), 1, p_spritefile );
			}

			for ( int z = 0; (unsigned)z < spriteHeader.numFrames; z++ )
			{
				fwrite ( &frameData[z].frameType, sizeof ( unsigned long ), 1, p_spritefile );
				fwrite ( &frameData[z].numIntervall, sizeof ( unsigned long ), 1, p_spritefile );
				for (int i = 0; i < frameData[z].numIntervall; i++)
				{
					fwrite ( &frameData[z].p_IntervallData[i], sizeof ( float ), 1, p_spritefile );
				}
				fwrite ( &frameData[z].originX, sizeof ( unsigned long ), 1, p_spritefile );
				fwrite ( &frameData[z].originY, sizeof ( unsigned long ), 1, p_spritefile );
				fwrite ( &frameData[z].width, sizeof ( unsigned long ), 1, p_spritefile );
				fwrite ( &frameData[z].height, sizeof ( unsigned long ), 1, p_spritefile );
				for (int a = 0; a < frameData[z].height * frameData[z].width; a++)
				{
					fwrite ( &frameData[z].p_frameData[a], sizeof ( unsigned char ), 1, p_spritefile );
				}
			}
		}
		return true;
	}
};

bool c_sprite::isSingleframe ()
{
	if ( frameData[0].frameType == 0 )
	{
		printf ("\a");
		return true;
	}
};

bool c_sprite::isGroupframe ()
{
	if ( frameData[0].frameType == 1 )
	{
		printf ("\a");
		return true;
	}
};
 

Neue Beiträge

Zurück