This is what happens when you ignore your tiredness and try to write complex code at 2:30 in the morning.
uint8_t MakeAnimatedSprite( char * cFileName, uint8_t ui8XFrames, uint8_t ui8YFrames, uint8_t *** ui8Target, uint64_t ** ui64Target, uint8_t *** ui8MapsTarget, uint64_t ** ui64MapsTarget, uint16_t * ui16ExtWidth, uint16_t * ui16ExtHeight, uint8_t * ui8ExtSupportedMaps, uint8_t * ui8ExtMapsCount )
{
uint8_t ui8CompletedMaps = 0;
uint8_t ui8Maps = 0;
GLFWimage glfwiColour;
GLFWimage glfwiHeightmap;
GLFWimage glfwiDamage;
GLFWimage glfwiKnockback;
GLFWimage glfwiGuard;
GLFWimage glfwiSolid;
glfwiColour.Data = NULL;
glfwiHeightmap.Data = NULL;
glfwiDamage.Data = NULL;
glfwiKnockback.Data = NULL;
glfwiGuard.Data = NULL;
glfwiSolid.Data = NULL;
char * cColourFileName = ( char * ) malloc( ( * cFileName ) + 12 );
if( cColourFileName == NULL )
{
printf( "[Animtation creator out of RAM!]" );
return 1;
}
memcpy( cColourFileName, cFileName + 1, * cFileName );
memcpy( cColourFileName + ( * cFileName ), "/colour.tga", 11 );
cColourFileName[ ( * cFileName ) + 11 ] = 0;
char * cHeightmapFileName = ( char * ) malloc( ( * cFileName ) + 15 );
if( cHeightmapFileName == NULL )
{
printf( "[Animtation creator out of RAM!]" );
free( cColourFileName );
return 1;
}
memcpy( cHeightmapFileName, cFileName + 1, * cFileName );
memcpy( cHeightmapFileName + ( * cFileName ), "/heightmap.tga", 14 );
cHeightmapFileName[ ( * cFileName ) + 14 ] = 0;
char * cDamageFileName = ( char * ) malloc( ( * cFileName ) + 12 );
if( cDamageFileName == NULL )
{
printf( "[Animtation creator out of RAM!]" );
free( cColourFileName );
free( cHeightmapFileName );
return 1;
}
memcpy( cDamageFileName, cFileName + 1, * cFileName );
memcpy( cDamageFileName + ( * cFileName ), "/damage.tga", 11 );
cDamageFileName[ ( * cFileName ) + 11 ] = 0;
char * cKnockbackFileName = ( char * ) malloc( ( * cFileName ) + 15 );
if( cKnockbackFileName == NULL )
{
printf( "[Animtation creator out of RAM!]" );
free( cColourFileName );
free( cHeightmapFileName );
free( cDamageFileName );
return 1;
}
memcpy( cKnockbackFileName, cFileName + 1, * cFileName );
memcpy( cKnockbackFileName + ( * cFileName ), "/knockback.tga", 14 );
cKnockbackFileName[ ( * cFileName ) + 14 ] = 0;
char * cGuardFileName = ( char * ) malloc( ( * cFileName ) + 11 );
if( cGuardFileName == NULL )
{
printf( "[Animtation creator out of RAM!]" );
free( cColourFileName );
free( cHeightmapFileName );
free( cDamageFileName );
free( cKnockbackFileName );
return 1;
}
memcpy( cGuardFileName, cFileName + 1, * cFileName );
memcpy( cGuardFileName + ( * cFileName ), "/guard.tga", 10 );
cGuardFileName[ ( * cFileName ) + 10 ] = 0;
char * cSolidFileName = ( char * ) malloc( ( * cFileName ) + 11 );
if( cSolidFileName == NULL )
{
printf( "[Animtation creator out of RAM!]" );
free( cColourFileName );
free( cHeightmapFileName );
free( cDamageFileName );
free( cKnockbackFileName );
free( cGuardFileName );
return 1;
}
memcpy( cSolidFileName, cFileName + 1, * cFileName );
memcpy( cSolidFileName + ( * cFileName ), "/solid.tga", 10 );
cSolidFileName[ ( * cFileName ) + 10 ] = 0;
if( glfwReadImage( cColourFileName, &glfwiColour, GLFW_ORIGIN_UL_BIT ) != GL_TRUE )
{
free( cColourFileName );
free( cHeightmapFileName );
free( cDamageFileName );
free( cKnockbackFileName );
free( cGuardFileName );
free( cSolidFileName );
if( OpenErrorMessage( "The object pointed to does not have a valid colour.tga in its directory." ) )
{
printf( "[Animation creator couldn't open error message to declare missing media!]" );
return 1;
}
return 255;
}
ui8CompletedMaps++;
ui8Maps += 1;
free( cColourFileName );
uint8_t ui8MyBit = 0;
uint8_t ui8YesBits = 0;
while( ui8MyBit < sizeof( int ) * 8 )
{
if( glfwiColour.Width & ( 1 << ui8MyBit ) )
{
ui8YesBits++;
}
ui8MyBit++;
}
if( ui8YesBits != 1 )
{
free( cHeightmapFileName );
free( cDamageFileName );
free( cKnockbackFileName );
free( cGuardFileName );
free( cSolidFileName );
glfwFreeImage( &glfwiColour );
if( OpenErrorMessage( "The object's colour.tga does not have a power of two width!" ) )
{
printf( "[Animation creator couldn't open error message to declare incorrect media!]" );
return 1;
}
return 0;
}
ui8MyBit = 0;
ui8YesBits = 0;
while( ui8MyBit < sizeof( int ) * 8 )
{
if( glfwiColour.Height & ( 1 << ui8MyBit ) )
{
ui8YesBits++;
}
ui8MyBit++;
}
if( ui8YesBits != 1 )
{
free( cHeightmapFileName );
free( cDamageFileName );
free( cKnockbackFileName );
free( cGuardFileName );
free( cSolidFileName );
glfwFreeImage( &glfwiColour );
if( OpenErrorMessage( "The object's colour.tga does not have a power of two height!" ) )
{
printf( "[Animation creator couldn't open error message to declare incorrect media!]" );
return 1;
}
return 255;
}
if( glfwiColour.BytesPerPixel != 4 )
{
free( cHeightmapFileName );
free( cDamageFileName );
free( cKnockbackFileName );
free( cGuardFileName );
free( cSolidFileName );
glfwFreeImage( &glfwiColour );
if( OpenErrorMessage( "The object's colour.tga is not a 32-bit image!" ) )
{
printf( "[Animation creator couldn't open error message to declare incorrect media!]" );
return 1;
}
return 255;
}
ui8MyBit = 0;
ui8YesBits = 0;
while( ui8MyBit < sizeof( uint8_t ) * 8 )
{
if( ui8XFrames & ( 1 << ui8MyBit ) )
{
ui8YesBits++;
}
ui8MyBit++;
}
if( ui8YesBits != 1 )
{
free( cHeightmapFileName );
free( cDamageFileName );
free( cKnockbackFileName );
free( cGuardFileName );
free( cSolidFileName );
glfwFreeImage( &glfwiColour );
if( OpenErrorMessage( "The number of X (left-right) frames given is not a power of two!" ) )
{
printf( "[Animation creator couldn't open error message to declare incorrect media!]" );
return 1;
}
return 255;
}
ui8MyBit = 0;
ui8YesBits = 0;
while( ui8MyBit < sizeof( uint8_t ) * 8 )
{
if( ui8YFrames & ( 1 << ui8MyBit ) )
{
ui8YesBits++;
}
ui8MyBit++;
}
if( ui8YesBits != 1 )
{
glfwFreeImage( &glfwiColour );
free( cHeightmapFileName );
free( cDamageFileName );
free( cKnockbackFileName );
free( cGuardFileName );
free( cSolidFileName );
if( OpenErrorMessage( "The number of Y (top-bottom) frames given is not a power of two!" ) )
{
printf( "[Animation creator couldn't open error message to declare incorrect media!]" );
return 1;
}
return 255;
}
if( ui8XFrames > glfwiColour.Width )
{
glfwFreeImage( &glfwiColour );
free( cHeightmapFileName );
free( cDamageFileName );
free( cKnockbackFileName );
free( cGuardFileName );
free( cSolidFileName );
if( OpenErrorMessage( "There are not enough pixels in colour.tga to fill every frame with at least one pixel! (X-axis)" ) )
{
printf( "[Animation creator couldn't open error message to declare incorrect media!]" );
return 1;
}
return 255;
}
if( ui8YFrames > glfwiColour.Height )
{
glfwFreeImage( &glfwiColour );
free( cHeightmapFileName );
free( cDamageFileName );
free( cKnockbackFileName );
free( cGuardFileName );
free( cSolidFileName );
if( OpenErrorMessage( "There are not enough pixels in colour.tga to fill every frame with at least one pixel! (Y-axis)" ) )
{
printf( "[Animation creator couldn't open error message to declare incorrect media!]" );
return 1;
}
return 255;
}
if( glfwReadImage( cHeightmapFileName, &glfwiHeightmap, GLFW_ORIGIN_UL_BIT ) != GL_TRUE )
{
free( cHeightmapFileName );
if( OpenErrorMessage( "No heightmap was provided and so the object will not have shadows/hightlights." ) )
{
printf( "[Animation creator couldn't open error message to declare incorrect media!]" );
return 1;
}
}
else
{
free( cHeightmapFileName );
if( glfwiHeightmap.BytesPerPixel != 3 )
{
glfwFreeImage( &glfwiColour );
glfwFreeImage( &glfwiHeightmap );
free( cDamageFileName );
free( cKnockbackFileName );
free( cGuardFileName );
free( cSolidFileName );
if( OpenErrorMessage( "Heightmap.tga is a valid TGA image, but it is not RGB. If it has an alpha channel, please remove it." ) )
{
printf( "[Animation creator couldn't open error message to declare incorrect media!]" );
return 1;
}
return 255;
}
if( glfwiHeightmap.Width != glfwiColour.Width )
{
glfwFreeImage( &glfwiColour );
glfwFreeImage( &glfwiHeightmap );
free( cDamageFileName );
free( cKnockbackFileName );
free( cGuardFileName );
free( cSolidFileName );
if( OpenErrorMessage( "Heightmap.tga is a valid TGA image, its dimensions do not match those of colour.tga. (Width)" ) )
{
printf( "[Animation creator couldn't open error message to declare incorrect media!]" );
return 1;
}
return 255;
}
if( glfwiHeightmap.Height != glfwiColour.Height )
{
glfwFreeImage( &glfwiColour );
glfwFreeImage( &glfwiHeightmap );
free( cDamageFileName );
free( cKnockbackFileName );
free( cGuardFileName );
free( cSolidFileName );
if( OpenErrorMessage( "Heightmap.tga is a valid TGA image, its dimensions do not match those of colour.tga. (Height)" ) )
{
printf( "[Animation creator couldn't open error message to declare incorrect media!]" );
return 1;
}
return 255;
}
ui8CompletedMaps++;
ui8Maps += 2;
}
if( glfwReadImage( cDamageFileName, &glfwiDamage, GLFW_ORIGIN_UL_BIT ) != GL_TRUE )
{
free( cDamageFileName );
if( OpenErrorMessage( "No damage map was provided and so the object will not be capable of causing damage." ) )
{
printf( "[Animation creator couldn't open error message to declare incorrect media!]" );
return 1;
}
}
else
{
free( cDamageFileName );
if( glfwiDamage.BytesPerPixel != 4 )
{
glfwFreeImage( &glfwiColour );
if( glfwiHeightmap.Data != NULL )
{
glfwFreeImage( &glfwiHeightmap );
}
glfwFreeImage( &glfwiDamage );
free( cKnockbackFileName );
free( cGuardFileName );
free( cSolidFileName );
if( OpenErrorMessage( "Damage.tga is a valid TGA image, but it is not RGBA. Remember, R=Physical damage, G=Fire damage, B=Ice damage, A=Lightning damage." ) )
{
printf( "[Animation creator couldn't open error message to declare incorrect media!]" );
return 1;
}
return 255;
}
if( glfwiDamage.Width != glfwiColour.Width )
{
glfwFreeImage( &glfwiColour );
if( glfwiHeightmap.Data != NULL )
{
glfwFreeImage( &glfwiHeightmap );
}
glfwFreeImage( &glfwiDamage );
free( cKnockbackFileName );
free( cGuardFileName );
free( cSolidFileName );
if( OpenErrorMessage( "Damage.tga is a valid TGA image, its dimensions do not match those of colour.tga. (Width)" ) )
{
printf( "[Animation creator couldn't open error message to declare incorrect media!]" );
return 1;
}
return 255;
}
if( glfwiDamage.Height != glfwiColour.Height )
{
glfwFreeImage( &glfwiColour );
if( glfwiHeightmap.Data != NULL )
{
glfwFreeImage( &glfwiHeightmap );
}
glfwFreeImage( &glfwiDamage );
free( cKnockbackFileName );
free( cGuardFileName );
free( cSolidFileName );
if( OpenErrorMessage( "Damage.tga is a valid TGA image, its dimensions do not match those of colour.tga. (Height)" ) )
{
printf( "[Animation creator couldn't open error message to declare incorrect media!]" );
return 1;
}
return 255;
}
ui8CompletedMaps++;
ui8Maps += 4;
}
if( glfwReadImage( cGuardFileName, &glfwiGuard, GLFW_ORIGIN_UL_BIT ) != GL_TRUE )
{
free( cGuardFileName );
if( OpenErrorMessage( "No guard map was provided and so the object will take all damage given." ) )
{
printf( "[Animation creator couldn't open error message to declare incorrect media!]" );
return 1;
}
}
else
{
free( cGuardFileName );
if( glfwiGuard.BytesPerPixel != 4 )
{
glfwFreeImage( &glfwiColour );
if( glfwiHeightmap.Data != NULL )
{
glfwFreeImage( &glfwiHeightmap );
}
if( glfwiDamage.Data != NULL )
{
glfwFreeImage( &glfwiDamage );
}
glfwFreeImage( &glfwiGuard );
free( cKnockbackFileName );
free( cSolidFileName );
if( OpenErrorMessage( "Guard.tga is a valid TGA image, but it is not RGBA. Remember, R=Physical damage, G=Fire damage, B=Ice damage, A=Lightning damage." ) )
{
printf( "[Animation creator couldn't open error message to declare incorrect media!]" );
return 1;
}
return 255;
}
if( glfwiGuard.Width != glfwiColour.Width )
{
glfwFreeImage( &glfwiColour );
if( glfwiHeightmap.Data != NULL )
{
glfwFreeImage( &glfwiHeightmap );
}
if( glfwiDamage.Data != NULL )
{
glfwFreeImage( &glfwiDamage );
}
glfwFreeImage( &glfwiGuard );
free( cKnockbackFileName );
free( cSolidFileName );
if( OpenErrorMessage( "Guard.tga is a valid TGA image, its dimensions do not match those of colour.tga. (Width)" ) )
{
printf( "[Animation creator couldn't open error message to declare incorrect media!]" );
return 1;
}
return 255;
}
if( glfwiGuard.Height != glfwiColour.Height )
{
glfwFreeImage( &glfwiColour );
if( glfwiHeightmap.Data != NULL )
{
glfwFreeImage( &glfwiHeightmap );
}
if( glfwiDamage.Data != NULL )
{
glfwFreeImage( &glfwiDamage );
}
glfwFreeImage( &glfwiGuard );
free( cKnockbackFileName );
free( cSolidFileName );
if( OpenErrorMessage( "Guard.tga is a valid TGA image, its dimensions do not match those of colour.tga. (Height)" ) )
{
printf( "[Animation creator couldn't open error message to declare incorrect media!]" );
return 1;
}
return 255;
}
ui8CompletedMaps++;
ui8Maps += 8;
}
if( glfwReadImage( cKnockbackFileName, &glfwiKnockback, GLFW_ORIGIN_UL_BIT ) != GL_TRUE )
{
free( cKnockbackFileName );
if( OpenErrorMessage( "No knockback map was provided and so the object not be capable of inflicting knockback." ) )
{
printf( "[Animation creator couldn't open error message to declare incorrect media!]" );
return 1;
}
}
else
{
free( cKnockbackFileName );
if( glfwiKnockback.BytesPerPixel != 4 )
{
glfwFreeImage( &glfwiColour );
if( glfwiHeightmap.Data != NULL )
{
glfwFreeImage( &glfwiHeightmap );
}
if( glfwiDamage.Data != NULL )
{
glfwFreeImage( &glfwiDamage );
}
if( glfwiGuard.Data != NULL )
{
glfwFreeImage( &glfwiGuard );
}
glfwFreeImage( &glfwiKnockback );
free( cSolidFileName );
if( OpenErrorMessage( "Knockback.tga is a valid TGA image, but it is not RGBA. Remember, R=Up, G=Down, B=Left, A=Right." ) )
{
printf( "[Animation creator couldn't open error message to declare incorrect media!]" );
return 1;
}
return 255;
}
if( glfwiKnockback.Width != glfwiColour.Width )
{
glfwFreeImage( &glfwiColour );
if( glfwiHeightmap.Data != NULL )
{
glfwFreeImage( &glfwiHeightmap );
}
if( glfwiDamage.Data != NULL )
{
glfwFreeImage( &glfwiDamage );
}
if( glfwiGuard.Data != NULL )
{
glfwFreeImage( &glfwiGuard );
}
glfwFreeImage( &glfwiKnockback );
free( cSolidFileName );
if( OpenErrorMessage( "Knockback.tga is a valid TGA image, its dimensions do not match those of colour.tga. (Width)" ) )
{
printf( "[Animation creator couldn't open error message to declare incorrect media!]" );
return 1;
}
return 255;
}
if( glfwiKnockback.Height != glfwiColour.Height )
{
glfwFreeImage( &glfwiColour );
if( glfwiHeightmap.Data != NULL )
{
glfwFreeImage( &glfwiHeightmap );
}
if( glfwiDamage.Data != NULL )
{
glfwFreeImage( &glfwiDamage );
}
if( glfwiGuard.Data != NULL )
{
glfwFreeImage( &glfwiGuard );
}
glfwFreeImage( &glfwiKnockback );
free( cSolidFileName );
if( OpenErrorMessage( "Knockback.tga is a valid TGA image, its dimensions do not match those of colour.tga. (Height)" ) )
{
printf( "[Animation creator couldn't open error message to declare incorrect media!]" );
return 1;
}
return 255;
}
ui8CompletedMaps++;
ui8Maps += 16;
}
if( glfwReadImage( cSolidFileName, &glfwiSolid, GLFW_ORIGIN_UL_BIT ) != GL_TRUE )
{
free( cSolidFileName );
if( OpenErrorMessage( "No solidity map was provided and so the object is not solid, grabbable or capable of grabbing." ) )
{
printf( "[Animation creator couldn't open error message to declare incorrect media!]" );
return 1;
}
}
else
{
free( cSolidFileName );
if( glfwiSolid.BytesPerPixel != 4 )
{
glfwFreeImage( &glfwiColour );
if( glfwiHeightmap.Data != NULL )
{
glfwFreeImage( &glfwiHeightmap );
}
if( glfwiDamage.Data != NULL )
{
glfwFreeImage( &glfwiDamage );
}
if( glfwiGuard.Data != NULL )
{
glfwFreeImage( &glfwiGuard );
}
if( glfwiKnockback.Data != NULL )
{
glfwFreeImage( &glfwiKnockback );
}
glfwFreeImage( &glfwiSolid );
if( OpenErrorMessage( "Solid.tga is a valid TGA image, but it is not RGBA. Remember, R=Solid from underneath, G=Otherwise solid, B=Grabbing, A=Grabbable." ) )
{
printf( "[Animation creator couldn't open error message to declare incorrect media!]" );
return 1;
}
return 255;
}
if( glfwiSolid.Width != glfwiColour.Width )
{
glfwFreeImage( &glfwiColour );
if( glfwiHeightmap.Data != NULL )
{
glfwFreeImage( &glfwiHeightmap );
}
if( glfwiDamage.Data != NULL )
{
glfwFreeImage( &glfwiDamage );
}
if( glfwiGuard.Data != NULL )
{
glfwFreeImage( &glfwiGuard );
}
if( glfwiKnockback.Data != NULL )
{
glfwFreeImage( &glfwiKnockback );
}
glfwFreeImage( &glfwiSolid );
if( OpenErrorMessage( "Solid.tga is a valid TGA image, its dimensions do not match those of colour.tga. (Width)" ) )
{
printf( "[Animation creator couldn't open error message to declare incorrect media!]" );
return 1;
}
return 255;
}
if( glfwiSolid.Height != glfwiColour.Height )
{
glfwFreeImage( &glfwiColour );
if( glfwiHeightmap.Data != NULL )
{
glfwFreeImage( &glfwiHeightmap );
}
if( glfwiDamage.Data != NULL )
{
glfwFreeImage( &glfwiDamage );
}
if( glfwiGuard.Data != NULL )
{
glfwFreeImage( &glfwiGuard );
}
if( glfwiKnockback.Data != NULL )
{
glfwFreeImage( &glfwiKnockback );
}
glfwFreeImage( &glfwiSolid );
if( OpenErrorMessage( "Solid.tga is a valid TGA image, its dimensions do not match those of colour.tga. (Height)" ) )
{
printf( "[Animation creator couldn't open error message to declare incorrect media!]" );
return 1;
}
return 255;
}
ui8CompletedMaps++;
ui8Maps += 32;
}
uint16_t ui16Width = glfwiColour.Width / ui8XFrames;
uint16_t ui16Height = glfwiColour.Height / ui8YFrames;
* ui16ExtWidth = ui16Width;
* ui16ExtHeight = ui16Height;
* ui8ExtSupportedMaps = ui8Maps;
* ui8ExtMapsCount = ui8CompletedMaps;
uint8_t * ui8TempSpace = ( uint8_t * ) malloc( ui16Width * ui16Height * 4 * ( 1 + ( ui8Maps & 2 ) ) );
if( ui8TempSpace == NULL )
{
printf( "[Object editor load out of RAM!]" );
glfwFreeImage( &glfwiColour );
if( glfwiHeightmap.Data != NULL )
{
glfwFreeImage( &glfwiHeightmap );
}
if( glfwiDamage.Data != NULL )
{
glfwFreeImage( &glfwiDamage );
}
if( glfwiGuard.Data != NULL )
{
glfwFreeImage( &glfwiGuard );
}
if( glfwiKnockback.Data != NULL )
{
glfwFreeImage( &glfwiKnockback );
}
if( glfwiSolid.Data != NULL )
{
glfwFreeImage( &glfwiSolid );
}
return 1;
}
uint64_t * ui64Frames = ( uint64_t * ) malloc( sizeof( uint64_t ) * ui8XFrames * ui8YFrames );
if( ui64Frames == NULL )
{
printf( "[Object editor load out of RAM!]" );
glfwFreeImage( &glfwiColour );
if( glfwiHeightmap.Data != NULL )
{
glfwFreeImage( &glfwiHeightmap );
}
if( glfwiDamage.Data != NULL )
{
glfwFreeImage( &glfwiDamage );
}
if( glfwiGuard.Data != NULL )
{
glfwFreeImage( &glfwiGuard );
}
if( glfwiKnockback.Data != NULL )
{
glfwFreeImage( &glfwiKnockback );
}
if( glfwiSolid.Data != NULL )
{
glfwFreeImage( &glfwiSolid );
}
free( ui8TempSpace );
return 1;
}
uint8_t ** ui8Frames = ( uint8_t ** ) malloc( sizeof( uint8_t * ) * ui8XFrames * ui8YFrames );
if( ui8Frames == NULL )
{
printf( "[Object editor load out of RAM!]" );
glfwFreeImage( &glfwiColour );
if( glfwiHeightmap.Data != NULL )
{
glfwFreeImage( &glfwiHeightmap );
}
if( glfwiDamage.Data != NULL )
{
glfwFreeImage( &glfwiDamage );
}
if( glfwiGuard.Data != NULL )
{
glfwFreeImage( &glfwiGuard );
}
if( glfwiKnockback.Data != NULL )
{
glfwFreeImage( &glfwiKnockback );
}
if( glfwiSolid.Data != NULL )
{
glfwFreeImage( &glfwiSolid );
}
free( ui8TempSpace );
free( ui64Frames );
return 1;
}
uint8_t * ui8TempMapsSpace = NULL;
uint64_t * ui64MapFrames = NULL;
uint8_t ** ui8MapFrames = NULL;
uint8_t ui8MapMaps = 0;
if( ( ( ui8CompletedMaps > 1 ) && ( !( ui8Maps & 2 ) ) ) || ( ( ui8CompletedMaps > 2 ) && ( ( ui8Maps & 2 ) ) ) != 0 )
{
if( ( ui8CompletedMaps > 1 ) && ( !( ui8Maps & 2 ) ) )
{
ui8MapMaps = ui8CompletedMaps - 1;
}
else
{
ui8MapMaps = ui8CompletedMaps - 2;
}
ui8TempMapsSpace = ( uint8_t * ) malloc( ui16Width * ui16Height * 4 * ui8MapMaps );
if( ui8TempSpace == NULL )
{
printf( "[Object editor load out of RAM!]" );
glfwFreeImage( &glfwiColour );
if( glfwiHeightmap.Data != NULL )
{
glfwFreeImage( &glfwiHeightmap );
}
if( glfwiDamage.Data != NULL )
{
glfwFreeImage( &glfwiDamage );
}
if( glfwiGuard.Data != NULL )
{
glfwFreeImage( &glfwiGuard );
}
if( glfwiKnockback.Data != NULL )
{
glfwFreeImage( &glfwiKnockback );
}
if( glfwiSolid.Data != NULL )
{
glfwFreeImage( &glfwiSolid );
}
free( ui8TempSpace );
free( ui64Frames );
free( ui8Frames );
return 1;
}
ui64MapFrames = ( uint64_t * ) malloc( sizeof( uint64_t ) * ui8XFrames * ui8YFrames );
if( ui64Frames == NULL )
{
printf( "[Object editor load out of RAM!]" );
glfwFreeImage( &glfwiColour );
if( glfwiHeightmap.Data != NULL )
{
glfwFreeImage( &glfwiHeightmap );
}
if( glfwiDamage.Data != NULL )
{
glfwFreeImage( &glfwiDamage );
}
if( glfwiGuard.Data != NULL )
{
glfwFreeImage( &glfwiGuard );
}
if( glfwiKnockback.Data != NULL )
{
glfwFreeImage( &glfwiKnockback );
}
if( glfwiSolid.Data != NULL )
{
glfwFreeImage( &glfwiSolid );
}
free( ui8TempSpace );
free( ui8TempMapsSpace );
free( ui64Frames );
free( ui8Frames );
return 1;
}
ui8MapFrames = ( uint8_t ** ) malloc( sizeof( uint8_t * ) * ui8XFrames * ui8YFrames );
if( ui8MapFrames == NULL )
{
printf( "[Object editor load out of RAM!]" );
glfwFreeImage( &glfwiColour );
if( glfwiHeightmap.Data != NULL )
{
glfwFreeImage( &glfwiHeightmap );
}
if( glfwiDamage.Data != NULL )
{
glfwFreeImage( &glfwiDamage );
}
if( glfwiGuard.Data != NULL )
{
glfwFreeImage( &glfwiGuard );
}
if( glfwiKnockback.Data != NULL )
{
glfwFreeImage( &glfwiKnockback );
}
if( glfwiSolid.Data != NULL )
{
glfwFreeImage( &glfwiSolid );
}
free( ui8TempSpace );
free( ui8TempMapsSpace );
free( ui64Frames );
free( ui8Frames );
free( ui64MapFrames );
return 1;
}
}
uint64_t ui64TotalSize = 0;
uint16_t ui16Y = 0;
while( ui16Y < ui8YFrames )
{
uint16_t ui16X = 0;
while( ui16X < ui8XFrames )
{
uint16_t ui16TexY = 0;
while( ui16TexY < ui16Height )
{
uint8_t ui8MyMap = 0;
//Colourmap
if( ui8Maps & 1 )
{
memcpy( ui8TempSpace + ( ( ui16TexY + ( ui16Height * ui8MyMap ) ) * ui16Width * 4 ), glfwiDamage.Data + ( ( ( ui16X * ui16Width ) + ( ui16TexY * glfwiColour.Width ) + ( ui16Width * ui16Y * ui16Height ) ) * 4 ), ui16Width * 4 );
}
//Heightmap
if( ui8Maps & 2 )
{
uint8_t * ui8TargetPixel = ui8TempMapsSpace + ( ( ui16Height * ui8MyMap ) * ui16Width * 4 ) + ( ui16Width * ui16TexY );
uint8_t * ui8TargetPixelB = ui8TempMapsSpace + ( ( ui16Height * ui8MyMap ) * ui16Width * 4 ) + ( ui16Width * ui16TexY ) + ( ui16Width * ui16Height );
uint8_t * ui8SourcePixel = glfwiHeightmap.Data + ( ( ( ui16X * ui16Width ) + ( ui16TexY * glfwiColour.Width ) + ( ui16Width * ui16Y * ui16Height ) ) * 3 ) + 3;
* ui8TargetPixel = 0;
* ui8TargetPixelB = 0;
ui8TargetPixel++;
ui8TargetPixelB++;
uint16_t ui16TexX = 1;
while( ui16TexX < ui16Width - 1 )
{
if( * ui8SourcePixel > 0 )
{
* ui8TargetPixel = ( ( ( * ui8SourcePixel ) - ( * ( ui8SourcePixel - 3 ) ) ) - ( ( * ui8SourcePixel ) - ( * ( ui8SourcePixel + 3 ) ) ) ) + 128.0;
* ui8TargetPixelB = ( ( ( * ui8SourcePixel ) - ( * ( ui8SourcePixel + 3 ) ) ) - ( ( * ui8SourcePixel ) - ( * ( ui8SourcePixel - 3 ) ) ) ) + 128.0;
}
else
{
* ui8TargetPixel = 0;
* ui8TargetPixelB = 0;
}
ui8TargetPixel++;
ui8TargetPixelB++;
ui8SourcePixel += 3;
ui16TexX++;
}
* ui8TargetPixel = 0;
* ui8TargetPixelB = 0;
if( ui16TexY == 0 )
{
memset( ui8TempMapsSpace + ( ( ui16Height * ui8MyMap ) * ui16Width * 4 ) + ( ui16Width * ui16TexY ) + ( ui16Width * ui16Height * 2 ), 0, ui16Width );
memset( ui8TempMapsSpace + ( ( ui16Height * ui8MyMap ) * ui16Width * 4 ) + ( ui16Width * ui16TexY ) + ( ui16Width * ui16Height * 3 ), 0, ui16Width );
}
else
{
if( ui16TexY == ui16Height - 1 )
{
memset( ui8TempMapsSpace + ( ( ui16Height * ui8MyMap ) * ui16Width * 4 ) + ( ui16Width * ui16TexY ) + ( ui16Width * ui16Height * 2 ), 0, ui16Width );
memset( ui8TempMapsSpace + ( ( ui16Height * ui8MyMap ) * ui16Width * 4 ) + ( ui16Width * ui16TexY ) + ( ui16Width * ui16Height * 3 ), 0, ui16Width );
}
else
{
ui8TargetPixel = ui8TempMapsSpace + ( ( ui16Height * ui8MyMap ) * ui16Width * 4 ) + ( ui16Width * ui16TexY ) + ( ui16Width * ui16Height * 2 );
ui8TargetPixelB = ui8TempMapsSpace + ( ( ui16Height * ui8MyMap ) * ui16Width * 4 ) + ( ui16Width * ui16TexY ) + ( ui16Width * ui16Height * 3 );
ui8SourcePixel = glfwiHeightmap.Data + ( ( ( ui16X * ui16Width ) + ( ui16TexY * glfwiColour.Width ) + ( ui16Width * ui16Y * ui16Height ) ) * 3 );
uint16_t ui16TexX = 0;
while( ui16TexX < ui16Width )
{
if( * ui8SourcePixel > 0 )
{
* ui8TargetPixel = ( ( ( * ui8SourcePixel ) - ( * ( ui8SourcePixel + ( glfwiHeightmap.Width * 3 ) ) ) ) - ( ( * ui8SourcePixel ) - ( * ( ui8SourcePixel - ( glfwiHeightmap.Width * 3 ) ) ) ) ) + 128.0;
* ui8TargetPixelB = ( ( ( * ui8SourcePixel ) - ( * ( ui8SourcePixel - ( glfwiHeightmap.Width * 3 ) ) ) ) - ( ( * ui8SourcePixel ) - ( * ( ui8SourcePixel + ( glfwiHeightmap.Width * 3 ) ) ) ) ) + 128.0;
}
else
{
* ui8TargetPixel = 0;
* ui8TargetPixelB = 0;
}
ui8TargetPixel++;
ui8TargetPixelB++;
ui8SourcePixel += 3;
ui16TexX++;
}
}
}
}
//Damagemap
if( ui8Maps & 4 )
{
memcpy( ui8TempMapsSpace + ( ( ui16TexY + ( ui16Height * ui8MyMap ) ) * ui16Width * 4 ), glfwiDamage.Data + ( ( ( ui16X * ui16Width ) + ( ui16TexY * glfwiColour.Width ) + ( ui16Width * ui16Y * ui16Height ) ) * 4 ), ui16Width * 4 );
ui8MyMap++;
}
//Guardmap
if( ui8Maps & 8 )
{
memcpy( ui8TempMapsSpace + ( ( ui16TexY + ( ui16Height * ui8MyMap ) ) * ui16Width * 4 ), glfwiGuard.Data + ( ( ( ui16X * ui16Width ) + ( ui16TexY * glfwiColour.Width ) + ( ui16Width * ui16Y * ui16Height ) ) * 4 ), ui16Width * 4 );
ui8MyMap++;
}
//Knockback
if( ui8Maps & 16 )
{
memcpy( ui8TempMapsSpace + ( ( ui16TexY + ( ui16Height * ui8MyMap ) ) * ui16Width * 4 ), glfwiKnockback.Data + ( ( ( ui16X * ui16Width ) + ( ui16TexY * glfwiColour.Width ) + ( ui16Width * ui16Y * ui16Height ) ) * 4 ), ui16Width * 4 );
ui8MyMap++;
}
//Solidmap
if( ui8Maps & 32 )
{
memcpy( ui8TempMapsSpace + ( ( ui16TexY + ( ui16Height * ui8MyMap ) ) * ui16Width * 4 ), glfwiSolid.Data + ( ( ( ui16X * ui16Width ) + ( ui16TexY * glfwiColour.Width ) + ( ui16Width * ui16Y * ui16Height ) ) * 4 ), ui16Width * 4 );
ui8MyMap++;
}
ui16TexY++;
}
lzo_uint lzouiOutSize;
lzo_uint lzouiResult;
uint8_t * ui8ThisFrame = ( uint8_t * ) malloc( ( ui16Width * ui16Height * 4 * ( 1 + ( ui8Maps & 2 ) ) ) + ( ui16Width * ui16Height * 4 * ( 1 + ( ui8Maps & 2 ) ) ) / 16 + 64 + 3 );
if( ui8ThisFrame == NULL )
{
printf( "[Object editor load out of RAM!]" );
free( ui8TempSpace );
glfwFreeImage( &glfwiColour );
if( glfwiHeightmap.Data != NULL )
{
glfwFreeImage( &glfwiHeightmap );
}
if( glfwiDamage.Data != NULL )
{
glfwFreeImage( &glfwiDamage );
}
if( glfwiGuard.Data != NULL )
{
glfwFreeImage( &glfwiGuard );
}
if( glfwiKnockback.Data != NULL )
{
glfwFreeImage( &glfwiKnockback );
}
if( glfwiSolid.Data != NULL )
{
glfwFreeImage( &glfwiSolid );
}
ui16X--;
while( ui16Y != UINT16_MAX )
{
while( ui16X != UINT16_MAX )
{
free( ui8Frames[ ( ui16Y * ui8XFrames ) + ui16X ] );
ui16X--;
}
ui16X = ui8XFrames - 1;
ui16Y--;
}
free( ui8Frames );
free( ui64Frames );
free( ui8Frames );
if( ( ( ui8CompletedMaps > 1 ) && ( !( ui8Maps & 2 ) ) ) || ( ( ui8CompletedMaps > 2 ) && ( ( ui8Maps & 2 ) ) ) != 0 )
{
free( ui64MapFrames );
free( ui8MapFrames );
}
return 1;
}
lzouiResult = lzo1x_1_compress( ui8TempSpace, ui16Width * ui16Height * 4 * ( 1 + ( ui8Maps & 2 ) ), ui8ThisFrame, & lzouiOutSize, wrkmem );
if( lzouiResult != LZO_E_OK )
{
printf("[internal error - compression failed: %d]", ( int ) lzouiResult );
free( ui8TempSpace );
free( ui8ThisFrame );
glfwFreeImage( &glfwiColour );
if( glfwiHeightmap.Data != NULL )
{
glfwFreeImage( &glfwiHeightmap );
}
if( glfwiDamage.Data != NULL )
{
glfwFreeImage( &glfwiDamage );
}
if( glfwiGuard.Data != NULL )
{
glfwFreeImage( &glfwiGuard );
}
if( glfwiKnockback.Data != NULL )
{
glfwFreeImage( &glfwiKnockback );
}
if( glfwiSolid.Data != NULL )
{
glfwFreeImage( &glfwiSolid );
}
ui16X--;
while( ui16Y != UINT16_MAX )
{
while( ui16X != UINT16_MAX )
{
free( ui8Frames[ ( ui16Y * ui8XFrames ) + ui16X ] );
ui16X--;
}
ui16X = ui8XFrames - 1;
ui16Y--;
}
free( ui64Frames );
free( ui8Frames );
if( ( ( ui8CompletedMaps > 1 ) && ( !( ui8Maps & 2 ) ) ) || ( ( ui8CompletedMaps > 2 ) && ( ( ui8Maps & 2 ) ) ) != 0 )
{
free( ui64MapFrames );
free( ui8MapFrames );
}
return 1;
}
ui64TotalSize += lzouiOutSize;
ui64Frames[ ( ui16Y * ui8XFrames ) + ui16X ] = lzouiOutSize;
uint8_t * ui8NewBank = ( uint8_t * ) malloc( lzouiOutSize );
if( ui8NewBank == NULL )
{
printf( "[Object editor load out of RAM!]" );
free( ui8TempSpace );
free( ui8ThisFrame );
glfwFreeImage( &glfwiColour );
if( glfwiHeightmap.Data != NULL )
{
glfwFreeImage( &glfwiHeightmap );
}
if( glfwiDamage.Data != NULL )
{
glfwFreeImage( &glfwiDamage );
}
if( glfwiGuard.Data != NULL )
{
glfwFreeImage( &glfwiGuard );
}
if( glfwiKnockback.Data != NULL )
{
glfwFreeImage( &glfwiKnockback );
}
if( glfwiSolid.Data != NULL )
{
glfwFreeImage( &glfwiSolid );
}
ui16X--;
while( ui16Y != UINT16_MAX )
{
while( ui16X != UINT16_MAX )
{
free( ui8Frames[ ( ui16Y * ui8XFrames ) + ui16X ] );
ui16X--;
}
ui16X = ui8XFrames - 1;
ui16Y--;
}
free( ui64Frames );
free( ui8Frames );
if( ( ( ui8CompletedMaps > 1 ) && ( !( ui8Maps & 2 ) ) ) || ( ( ui8CompletedMaps > 2 ) && ( ( ui8Maps & 2 ) ) ) != 0 )
{
free( ui64MapFrames );
free( ui8MapFrames );
}
return 1;
}
memcpy( ui8NewBank, ui8ThisFrame, ( int ) lzouiOutSize );
free( ui8ThisFrame );
printf( "Compressed to %fkb.n", lzouiOutSize / 1024.0 );
if( ( ( ui8CompletedMaps > 1 ) && ( !( ui8Maps & 2 ) ) ) || ( ( ui8CompletedMaps > 2 ) && ( ( ui8Maps & 2 ) ) ) != 0 )
{
uint8_t * ui8ThisMapFrame = ( uint8_t * ) malloc( ( ui16Width * ui16Height * 4 * ui8MapMaps ) + ( ui16Width * ui16Height * 4 * ui8MapMaps ) / 16 + 64 + 3 );
if( ui8ThisMapFrame == NULL )
{
printf( "[Object editor load out of RAM!]" );
free( ui8TempSpace );
free( ui8TempMapsSpace );
free( ui8NewBank );
glfwFreeImage( &glfwiColour );
if( glfwiHeightmap.Data != NULL )
{
glfwFreeImage( &glfwiHeightmap );
}
if( glfwiDamage.Data != NULL )
{
glfwFreeImage( &glfwiDamage );
}
if( glfwiGuard.Data != NULL )
{
glfwFreeImage( &glfwiGuard );
}
if( glfwiKnockback.Data != NULL )
{
glfwFreeImage( &glfwiKnockback );
}
if( glfwiSolid.Data != NULL )
{
glfwFreeImage( &glfwiSolid );
}
ui16X--;
while( ui16Y != UINT16_MAX )
{
while( ui16X != UINT16_MAX )
{
free( ui8Frames[ ( ui16Y * ui8XFrames ) + ui16X ] );
ui16X--;
}
ui16X = ui8XFrames - 1;
ui16Y--;
}
free( ui8Frames );
free( ui64Frames );
free( ui8Frames );
if( ( ( ui8CompletedMaps > 1 ) && ( !( ui8Maps & 2 ) ) ) || ( ( ui8CompletedMaps > 2 ) && ( ( ui8Maps & 2 ) ) ) != 0 )
{
free( ui64MapFrames );
free( ui8MapFrames );
}
return 1;
}
lzouiResult = lzo1x_1_compress( ui8TempMapsSpace, ui16Width * ui16Height * 4 * ui8MapMaps, ui8ThisMapFrame, & lzouiOutSize, wrkmem );
if( lzouiResult != LZO_E_OK )
{
printf("[internal error - compression failed: %d]", ( int ) lzouiResult );
free( ui8TempSpace );
free( ui8TempMapsSpace );
free( ui8ThisMapFrame );
free( ui8NewBank );
glfwFreeImage( &glfwiColour );
if( glfwiHeightmap.Data != NULL )
{
glfwFreeImage( &glfwiHeightmap );
}
if( glfwiDamage.Data != NULL )
{
glfwFreeImage( &glfwiDamage );
}
if( glfwiGuard.Data != NULL )
{
glfwFreeImage( &glfwiGuard );
}
if( glfwiKnockback.Data != NULL )
{
glfwFreeImage( &glfwiKnockback );
}
if( glfwiSolid.Data != NULL )
{
glfwFreeImage( &glfwiSolid );
}
ui16X--;
while( ui16Y != UINT16_MAX )
{
while( ui16X != UINT16_MAX )
{
free( ui8Frames[ ( ui16Y * ui8XFrames ) + ui16X ] );
ui16X--;
}
ui16X = ui8XFrames - 1;
ui16Y--;
}
free( ui64Frames );
free( ui8Frames );
if( ( ( ui8CompletedMaps > 1 ) && ( !( ui8Maps & 2 ) ) ) || ( ( ui8CompletedMaps > 2 ) && ( ( ui8Maps & 2 ) ) ) != 0 )
{
free( ui64MapFrames );
free( ui8MapFrames );
}
return 1;
}
ui64TotalSize += lzouiOutSize;
ui64MapFrames[ ( ui16Y * ui8XFrames ) + ui16X ] = lzouiOutSize;
uint8_t * ui8NewMapBank = ( uint8_t * ) malloc( lzouiOutSize );
if( ui8NewBank == NULL )
{
printf( "[Object editor load out of RAM!]" );
free( ui8TempSpace );
free( ui8ThisFrame );
free( ui8TempMapsSpace );
free( ui8ThisMapFrame );
free( ui8NewBank );
glfwFreeImage( &glfwiColour );
if( glfwiHeightmap.Data != NULL )
{
glfwFreeImage( &glfwiHeightmap );
}
if( glfwiDamage.Data != NULL )
{
glfwFreeImage( &glfwiDamage );
}
if( glfwiGuard.Data != NULL )
{
glfwFreeImage( &glfwiGuard );
}
if( glfwiKnockback.Data != NULL )
{
glfwFreeImage( &glfwiKnockback );
}
if( glfwiSolid.Data != NULL )
{
glfwFreeImage( &glfwiSolid );
}
ui16X--;
while( ui16Y != UINT16_MAX )
{
while( ui16X != UINT16_MAX )
{
free( ui8Frames[ ( ui16Y * ui8XFrames ) + ui16X ] );
ui16X--;
}
ui16X = ui8XFrames - 1;
ui16Y--;
}
free( ui64Frames );
free( ui8Frames );
if( ( ( ui8CompletedMaps > 1 ) && ( !( ui8Maps & 2 ) ) ) || ( ( ui8CompletedMaps > 2 ) && ( ( ui8Maps & 2 ) ) ) != 0 )
{
free( ui64MapFrames );
free( ui8MapFrames );
}
return 1;
}
memcpy( ui8NewMapBank, ui8ThisMapFrame, ( int ) lzouiOutSize );
free( ui8ThisMapFrame );
printf( "Compressed to %fkb.n", lzouiOutSize / 1024.0 );
ui8MapFrames[ ( ui16Y * ui8XFrames ) + ui16X ] = ui8NewMapBank;
}
ui8Frames[ ( ui16Y * ui8XFrames ) + ui16X ] = ui8NewBank;
ui16X++;
}
ui16Y++;
}
if( ( ( ui8CompletedMaps > 1 ) && ( !( ui8Maps & 2 ) ) ) || ( ( ui8CompletedMaps > 2 ) && ( ( ui8Maps & 2 ) ) ) != 0 )
{
free( ui8TempMapsSpace );
}
free( ui8TempSpace );
glfwFreeImage( &glfwiColour );
if( glfwiHeightmap.Data != NULL )
{
glfwFreeImage( &glfwiHeightmap );
}
if( glfwiDamage.Data != NULL )
{
glfwFreeImage( &glfwiDamage );
}
if( glfwiGuard.Data != NULL )
{
glfwFreeImage( &glfwiGuard );
}
if( glfwiKnockback.Data != NULL )
{
glfwFreeImage( &glfwiKnockback );
}
if( glfwiSolid.Data != NULL )
{
glfwFreeImage( &glfwiSolid );
}
* ui8Target = ui8Frames;
* ui8MapsTarget = ui8MapFrames;
* ui64Target = ui64Frames;
* ui64MapsTarget = ui64MapFrames;
return 0;
}
You know what the best bit is? It works.
Good night.