eliminating exception use
This commit is contained in:
@@ -5,25 +5,26 @@
|
|||||||
#include "RageUtil.h"
|
#include "RageUtil.h"
|
||||||
#include "RageFile.h"
|
#include "RageFile.h"
|
||||||
|
|
||||||
struct FatalError: public RageException { FatalError(const CString &str): RageException(str) { } };
|
static void WriteBytes( RageFile &f, CString &sError, const void *buf, int size )
|
||||||
|
|
||||||
static void WriteBytes( RageFile &f, const void *buf, int size )
|
|
||||||
{
|
{
|
||||||
|
if( sError.size() != 0 )
|
||||||
|
return;
|
||||||
|
|
||||||
int ret = f.Write( buf, size );
|
int ret = f.Write( buf, size );
|
||||||
if( ret == -1 )
|
if( ret == -1 )
|
||||||
throw FatalError( f.GetError() );
|
sError = f.GetError();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void write_le16( RageFile &f, uint16_t val )
|
static void write_le16( RageFile &f, CString &sError, uint16_t val )
|
||||||
{
|
{
|
||||||
val = Swap16LE( val );
|
val = Swap16LE( val );
|
||||||
WriteBytes( f, &val, sizeof(uint16_t) );
|
WriteBytes( f, sError, &val, sizeof(uint16_t) );
|
||||||
}
|
}
|
||||||
|
|
||||||
static void write_le32( RageFile &f, uint32_t val )
|
static void write_le32( RageFile &f, CString &sError, uint32_t val )
|
||||||
{
|
{
|
||||||
val = Swap32LE( val );
|
val = Swap32LE( val );
|
||||||
WriteBytes( f, &val, sizeof(uint32_t) );
|
WriteBytes( f, sError, &val, sizeof(uint32_t) );
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RageSurface_Save_BMP( RageSurface *surface, RageFile &f )
|
bool RageSurface_Save_BMP( RageSurface *surface, RageFile &f )
|
||||||
@@ -34,45 +35,45 @@ bool RageSurface_Save_BMP( RageSurface *surface, RageFile &f )
|
|||||||
Swap24LE( 0xFF0000 ), Swap24LE( 0x00FF00 ), Swap24LE( 0x0000FF ), 0 );
|
Swap24LE( 0xFF0000 ), Swap24LE( 0x00FF00 ), Swap24LE( 0x0000FF ), 0 );
|
||||||
RageSurfaceUtils::CopySurface( surface, converted_surface );
|
RageSurfaceUtils::CopySurface( surface, converted_surface );
|
||||||
|
|
||||||
try {
|
CString sError;
|
||||||
|
|
||||||
int iFilePitch = converted_surface->pitch;
|
int iFilePitch = converted_surface->pitch;
|
||||||
iFilePitch = (iFilePitch+3) & ~3; // round up a multiple of 4
|
iFilePitch = (iFilePitch+3) & ~3; // round up a multiple of 4
|
||||||
|
|
||||||
int iDataSize = converted_surface->h * iFilePitch;
|
int iDataSize = converted_surface->h * iFilePitch;
|
||||||
const int iHeaderSize = 0x36;
|
const int iHeaderSize = 0x36;
|
||||||
|
|
||||||
WriteBytes( f, "BM", 2 );
|
WriteBytes( f, sError, "BM", 2 );
|
||||||
write_le32( f, iHeaderSize+iDataSize ); // size (offset 0x2)
|
write_le32( f, sError, iHeaderSize+iDataSize ); // size (offset 0x2)
|
||||||
write_le32( f, 0 ); // reserved (offset 0x6)
|
write_le32( f, sError, 0 ); // reserved (offset 0x6)
|
||||||
write_le32( f, iHeaderSize ); // bitmap offset (offset 0xA)
|
write_le32( f, sError, iHeaderSize ); // bitmap offset (offset 0xA)
|
||||||
write_le32( f, 0x28 ); // header size (offset 0xE)
|
write_le32( f, sError, 0x28 ); // header size (offset 0xE)
|
||||||
write_le32( f, surface->w ); // width (offset 0x14)
|
write_le32( f, sError, surface->w ); // width (offset 0x14)
|
||||||
write_le32( f, surface->h ); // height (offset 0x18)
|
write_le32( f, sError, surface->h ); // height (offset 0x18)
|
||||||
write_le16( f, 1 ); // planes (offset 0x1A)
|
write_le16( f, sError, 1 ); // planes (offset 0x1A)
|
||||||
write_le16( f, (uint16_t) converted_surface->fmt.BytesPerPixel*8 ); // bpp (offset 0x1C)
|
write_le16( f, sError, (uint16_t) converted_surface->fmt.BytesPerPixel*8 ); // bpp (offset 0x1C)
|
||||||
write_le32( f, 0 ); // compression (offset 0x1E)
|
write_le32( f, sError, 0 ); // compression (offset 0x1E)
|
||||||
write_le32( f, iDataSize ); // bitmap size (offset 0x22)
|
write_le32( f, sError, iDataSize ); // bitmap size (offset 0x22)
|
||||||
write_le32( f, 0 ); // horiz resolution (offset 0x26)
|
write_le32( f, sError, 0 ); // horiz resolution (offset 0x26)
|
||||||
write_le32( f, 0 ); // vert resolution (offset 0x2A)
|
write_le32( f, sError, 0 ); // vert resolution (offset 0x2A)
|
||||||
write_le32( f, 0 ); // colors (offset 0x2E)
|
write_le32( f, sError, 0 ); // colors (offset 0x2E)
|
||||||
write_le32( f, 0 ); // important colors (offset 0x32)
|
write_le32( f, sError, 0 ); // important colors (offset 0x32)
|
||||||
|
|
||||||
for( int y = converted_surface->h-1; y >= 0; --y )
|
for( int y = converted_surface->h-1; y >= 0; --y )
|
||||||
{
|
{
|
||||||
const uint8_t *pRow = converted_surface->pixels + converted_surface->pitch*y;
|
const uint8_t *pRow = converted_surface->pixels + converted_surface->pitch*y;
|
||||||
WriteBytes( f, pRow, converted_surface->pitch );
|
WriteBytes( f, sError, pRow, converted_surface->pitch );
|
||||||
|
|
||||||
/* Pad */
|
/* Pad the row to the pitch. */
|
||||||
uint8_t padding[4] = { 0,0,0,0 };
|
uint8_t padding[4] = { 0,0,0,0 };
|
||||||
WriteBytes( f, padding, iFilePitch-converted_surface->pitch );
|
WriteBytes( f, sError, padding, iFilePitch-converted_surface->pitch );
|
||||||
}
|
}
|
||||||
} catch(...) {
|
|
||||||
delete converted_surface;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
delete converted_surface;
|
delete converted_surface;
|
||||||
|
|
||||||
|
if( sError.size() != 0 )
|
||||||
|
return false;
|
||||||
|
|
||||||
if( f.Flush() == -1 )
|
if( f.Flush() == -1 )
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user