Obsessive Compulsive/Refactoring Disorder

Started by
26 comments, last by Ravyne 13 years, 1 month ago

The language you write in is not perfect, The programming language you write in is not perfect, the whole idea of programming as a means of developing a program is not perfect so why should your code be?

That doesn't mean you have to aim low. ;)
[/quote]

I'm not saying that you should try and write the most horrible code ever, but just don't worry too much about sub-designs and conventions.

If you get the work done it says a lot more about you than how neat your code is.
Advertisement
Refactoring should be relatively easy and fast, if it's not then there might be bigger problems. Adding new features to spaghetti code can take a lot of time and introduce bugs at other side of codebase. Also bugfixing, maintenance and understanding the task at hand gets overwhelming, for you and whoever works on your project.

Developement should be fast and bugfree. Major changes in architecture and even renaming commonly used symbol needs team consensus. For one-man project you need to play devils advocate and argue with yourself to drop tasks which are hurting the project.

Try to refactor either before or after implementing new feature. With version control it's easier to see what has changed.
[color="#1C2837"]
As a tip I recommend coding with whitespace hidden characters on. It helps to relieve the anxiety of not knowing. :unsure: [/quote]
[color="#1C2837"]My end key is worn down. sad.gif
This is an example of what I'm talking about...



// ----------------------------------------------------------------------------
// Copyright (C) 2011, Matthew R. Erdman
// All rights reserved
// ----------------------------------------------------------------------------

#ifndef __TGA_H__
#define __TGA_H__

// NOTES:
// This TGA decoder supports color index, black & white, 16, 24 and 32-bit
// images, compressed or non compressed. All images are converted to 32-bit
// upon decoding as follows:
//
// 32-bit images need not be converted and are simple read "as is";
// compressed or non compressed.
//
// 24-bit images are converted to 32-bit images by flood-filling the alpha
// channel with solid white(e.g. full-intensity).
//
// Some image editors don't encode 16-bit TGA images differently. TGA 2.0
// supports two types of 16-bit images; 555 and 565. Where as, the 15-bit (555)
// uses 1-bit for an optional interrupt/alpha value, 5 bits for red, 5 bits for
// green and 5 bits for blue. 16-bit (565) uses 5 bits for red, 6 bits for
// green and 5 bits for blue according to the copy of the TGA specifications I
// have. Some editors list the image as 16-bit using the 555 (a.k.a. 15-bit)
// format, whilst using the image footer to describe the usage of the alpha
// channel if applicable. Here, however, either image decodes as 555 to 32-bit
// images similar to 24-bit images.
//
// Color index images are converted from index to full size images using
// inflation. Black & white images, however, do not contain color information
// as they are colorless. Both images are converted to the 32-bit values listed
// in the color table, or 32-bit pixel value for black & white images.

#define TGA_EMPTY 0
#define TGA_COLORINDEX 1
#define TGA_COLOR 2
#define TGA_BLACKWHITE 3
#define TGA_COMPRESSED 8 // Hack!
#define RLE_COLORINDEX TGA_COMPRESSED + TGA_COLORINDEX
#define RLE_COLOR TGA_COMPRESSED + TGA_COLOR
#define RLE_BLACKWHITE TGA_COMPRESSED + TGA_BLACKWHITE

#define TGA_HFLIP 0x20
#define TGA_VFLIP 0x10
#define TGA_ALPHAFLAGS 0x0F

#pragma pack( push, 1 )
typedef struct
{
byte iIdent ;
byte iColorFlags ;
byte iImageFlags ;
word wFirstColorIndex ;
word wColorCount ;
byte iColorDepth ;
word wMin_X ;
word wMin_Y ;
word wMax_X ;
word wMax_Y ;
byte iDepth ;
byte iDescriptor ;
} TGA_Header_t ;
#pragma pack( pop )

#define TGA_SizeX( head ) head.wMax_X - head.wMin_X
#define TGA_SizeY( head ) head.wMax_Y - head.wMin_Y
#define TGA_FlipH( head ) head.iDescriptor & TGA_HFLIP
#define TGA_FlipV( head ) head.iDescriptor & TGA_VFLIP


class TGA_Image_t : public Image_t
{
public :

TGA_Image_t( void ) ;
virtual ~TGA_Image_t( void ) ;

virtual bool Load( const char* ) ;
virtual void Free( void ) ;

protected :

TGA_Header_t m_Header ;
byte* m_pColorPalette ;
byte* m_pPixelPalette ;
byte* m_pImage ;

bool Read32Bit( FILE*, uint, uint, byte** ) ;
bool Read24Bit( FILE*, uint, uint, byte** ) ;
bool Read16Bit( FILE*, uint, uint, byte** ) ;
bool Read15Bit( FILE*, uint, uint, byte** ) ;
bool ReadIndex( FILE*, uint, uint, byte** ) ;
bool ReadCompressed32Bit( FILE*, uint, uint, byte** ) ;
bool ReadCompressed24Bit( FILE*, uint, uint, byte** ) ;
bool ReadCompressed16Bit( FILE*, uint, uint, byte** ) ;
bool ReadCompressed15Bit( FILE*, uint, uint, byte** ) ;
bool ReadCompressedIndex( FILE*, uint, uint, byte** ) ;
bool ReadColor( FILE*, uint, uint, byte, bool, byte** ) ;
bool JoinImage( void ) ;
void SwapPixel( uint, uint, uint, uint ) ;
void FlipImage( void ) ;
} ;

#endif // __TGA_H__






// ----------------------------------------------------------------------------
// Copyright (C) 2011, Matthew R. Erdman
// All rights reserved
// ----------------------------------------------------------------------------

#include "common.h"
#include "imglib.h"
#include "tga.h"


// ----------------------------------------------------------------------------

TGA_Image_t:: TGA_Image_t( void ) : Image_t( )
{
memset( &m_Header, 0, sizeof( TGA_Header ) ) ;
// m_Header.iIdent = 0 ;
// m_Header.iColorFlags = 0 ;
// m_Header.iImageFlags = 0 ;
// m_Header.wFirstColorIndex = 0 ;
// m_Header.wColorCount = 0 ;
// m_Header.iColorDepth = 0 ;
// m_Header.wMin_X = 0 ;
// m_Header.wMin_Y = 0 ;
// m_Header.wMax_X = 0 ;
// m_Header.wMax_Y = 0 ;
// m_Header.iDepth = 0 ;
// m_Header.iDescriptor = 0 ;
m_pColorPalette = NULL ;
m_pPixelPalette = NULL ;
m_pImage = NULL ;
}

TGA_Image_t::~TGA_Image_t( void )
{
Free( ) ;
}


// ----------------------------------------------------------------------------

bool TGA_Image_t::Load( const char* pFilename )
{
FILE* pFile ;

if ( NULL == ( pFile = fopen( pFilename, "rb" ) ) )
goto Fail ;

fread( &m_Header, sizeof( TGA_Header_t ), 1, pFile ) ;

if ( m_Header.iIdent != 0 )
fseek( pFile, m_Header.iIdent, SEEK_CUR ) ;

m_uSizeX = TGA_SizeX( m_Header ) ;
m_uSizeY = TGA_SizeY( m_Header ) ;

if ( TGA_EMPTY != m_Header.iColorFlags )
{
if ( false == ReadColor( pFile, m_Header.wColorCount, 1, m_Header.iColorDepth, false, &m_pColorPalette ) )
goto Fail ;
}

if ( TGA_EMPTY != m_Header.iImageFlags )
{
if ( false == ReadColor( pFile, m_uSizeX, m_uSizeY, m_Header.iDepth, TGA_IsRLE( m_Header ), &m_pPixelPalette ) )
goto Fail ;
}

fclose( pFile ) ; pFile = NULL ;

if ( false == JoinImage( ) )
goto Fail ;

FlipImage( ) ;

glGenTextures( 1, &m_uImage ) ;
glBindTexture( GL_TEXTURE_2D, m_uImage ) ;

if ( GL_NO_ERROR != glGetError( ) )
goto Fail ;

// FIXME : USE OPENGL'S CURRENT FILTER SETTING
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR ) ;
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR ) ;
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ) ;
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ) ;

if ( gluBuild2DMipmaps(
GL_TEXTURE_2D,
GL_RGBA,
m_uSizeX,
m_uSizeY,
GL_RGBA,
GL_UNSIGNED_BYTE,
m_pImage ) != 0 )
{
goto Fail ;
}

if ( m_pColorPalette ) free( m_pColorPalette ) ;
if ( m_pPixelPalette ) free( m_pPixelPalette ) ;
if ( m_pImage ) free( m_pImage ) ;

return true ;

Fail :
if ( NULL != pFile )
fclose ( pFile ) ;

if ( m_pColorPalette ) free( m_pColorPalette ) ;
if ( m_pPixelPalette ) free( m_pPixelPalette ) ;
if ( m_pImage ) free( m_pImage ) ;

return false ;
}


// ----------------------------------------------------------------------------

void TGA_Image_t::Free( void )
{
if ( m_pColorPalette ) free( m_pColorPalette ) ;
if ( m_pPixelPalette ) free( m_pPixelPalette ) ;
if ( m_pImage ) free( m_pImage ) ;

// error?
// delete this ;
}


// ----------------------------------------------------------------------------

bool TGA_Image_t::Read32Bit( FILE* pFile, uint uSizeX, uint uSizeY, byte** pImage )
{
byte* pData ;
byte vTemp[ 4 ] ;
uint i ;

if ( NULL == ( pData = ( byte* )malloc( uSizeX * uSizeY * 4 ) ) )
return false ;

for ( i = 0 ; i < uSizeX * uSizeY ; i++ )
{
fread( &vTemp, 4, 1, pFile ) ;

pData[ ( i * 4 ) ] = vTemp[ 2 ] ;
pData[ ( i * 4 ) + 1 ] = vTemp[ 1 ] ;
pData[ ( i * 4 ) + 2 ] = vTemp[ 0 ] ;
pData[ ( i * 4 ) + 3 ] = vTemp[ 3 ] ;
}

( *pImage ) = pData ;

return true ;
}


// ----------------------------------------------------------------------------

bool TGA_Image_t::Read24Bit( FILE* pFile, uint uSizeX, uint uSizeY, byte** pImage )
{
byte* pData ;
byte vTemp[ 3 ] ;
uint i ;

if ( NULL == ( pData = ( byte* )malloc( uSizeX * uSizeY * 4 ) ) )
return false ;

for ( i = 0 ; i < uSizeX * uSizeY ; i++ )
{
fread( &vTemp, 3, 1, pFile ) ;

pData[ ( i * 4 ) ] = vTemp[ 2 ] ;
pData[ ( i * 4 ) + 1 ] = vTemp[ 1 ] ;
pData[ ( i * 4 ) + 2 ] = vTemp[ 0 ] ;
pData[ ( i * 4 ) + 3 ] = 255 ; // virtual-whiteout :)
}

( *pImage ) = pData ;

return true ;
}


// ----------------------------------------------------------------------------

bool TGA_Image_t::Read16Bit( FILE* pFile, uint uSizeX, uint uSizeY, byte** pImage )
{
byte* pData ;
word wTemp ;
uint i ;

if ( NULL == ( pData = ( byte* )malloc( uSizeX * uSizeY * 4 ) ) )
return false ;

for ( i = 0 ; i < uSizeX * uSizeY ; i++ )
{
fread( &wTemp, 2, 1, pFile ) ;

pData[ ( i * 4 ) ] = ( ( wTemp & 0xF800 ) >> 11 ) << 3 ;
pData[ ( i * 4 ) + 1 ] = ( ( wTemp & 0x07E0 ) >> 5 ) << 2 ;
pData[ ( i * 4 ) + 2 ] = ( ( wTemp & 0x001F ) << 3 ) ;
pData[ ( i * 4 ) + 3 ] = 255 ;
}

( *pImage ) = pData ;

return true ;
}


// ----------------------------------------------------------------------------

bool TGA_Image_t::Read15Bit( FILE* pFile, uint uSizeX, uint uSizeY, byte** pImage )
{
byte* pData ;
word wTemp ;
uint i ;

if ( NULL == ( pData = ( byte* )malloc( uSizeX * uSizeY * 4 ) ) )
return false ;

for ( i = 0 ; i < uSizeX * uSizeY ; i++ )
{
fread( &wTemp, 2, 1, pFile ) ;

pData[ ( i * 4 ) ] = ( ( wTemp & 0x7C00 ) >> 10 ) << 3 ;
pData[ ( i * 4 ) + 1 ] = ( ( wTemp & 0x03E0 ) >> 5 ) << 3 ;
pData[ ( i * 4 ) + 2 ] = ( ( wTemp & 0x001F ) << 3 ) ;

// if ( m_Header.iDescriptor & 0x0F )
// {
// pData[ ( i * 4 ) + 3 ] = ( wTemp & 0x8000 ) ? 255 : 0 ;
// }
// else
pData[ ( i * 4 ) + 3 ] = 255 ;
}

( *pImage ) = pData ;

return true ;
}


// ----------------------------------------------------------------------------

bool TGA_Image_t::ReadIndex( FILE* pFile, uint uSizeX, uint uSizeY, byte** pImage )
{
byte* pData ;

if ( NULL == ( pData = ( byte* )malloc( uSizeX * uSizeY ) ) )
return false ;

fread( pData, uSizeX * uSizeY, 1, pFile ) ;

( *pImage ) = pData ;

return true ;
}


// ----------------------------------------------------------------------------

bool TGA_Image_t::ReadCompressed32Bit( FILE* pFile, uint uSizeX, uint uSizeY, byte** pImage )
{
byte* pData ;
byte vTemp[ 4 ] ;
uint i, j, ct ;

byte iCode ;

if ( NULL == ( pData = ( byte* )malloc( uSizeX * uSizeY * 4 ) ) )
return false ;

for ( i = 0 ; i < uSizeX * uSizeY ; )
{
fread( &iCode, 1, 1, pFile ) ;

ct = ( iCode & 0x7F ) + 1 ;

if ( iCode & 0x80 )
{
fread( &vTemp, 4, 1, pFile ) ;

for ( j = 0 ; j < ct ; j++, i++ )
{
pData[ ( i * 4 ) ] = vTemp[ 2 ] ;
pData[ ( i * 4 ) + 1 ] = vTemp[ 1 ] ;
pData[ ( i * 4 ) + 2 ] = vTemp[ 0 ] ;
pData[ ( i * 4 ) + 3 ] = vTemp[ 3 ] ;
}
}
else
{
for ( j = 0 ; j < ct ; j++, i++ )
{
fread( &vTemp, 4, 1, pFile ) ;

pData[ ( i * 4 ) ] = vTemp[ 2 ] ;
pData[ ( i * 4 ) + 1 ] = vTemp[ 1 ] ;
pData[ ( i * 4 ) + 2 ] = vTemp[ 0 ] ;
pData[ ( i * 4 ) + 3 ] = vTemp[ 3 ] ;
}
}
}

( *pImage ) = pData ;

return true ;
}


// ----------------------------------------------------------------------------

bool TGA_Image_t::ReadCompressed24Bit( FILE* pFile, uint uSizeX, uint uSizeY, byte** pImage )
{
byte* pData ;
byte vTemp[ 3 ] ;
uint i, j, ct ;

byte iCode ;

if ( NULL == ( pData = ( byte* )malloc( uSizeX * uSizeY * 4 ) ) )
return false ;

for ( i = 0 ; i < uSizeX * uSizeY ; )
{
fread( &iCode, 1, 1, pFile ) ;

ct = ( iCode & 0x7F ) + 1 ;

if ( iCode & 0x80 )
{
fread( &vTemp, 3, 1, pFile ) ;

for ( j = 0 ; j < ct ; j++, i++ )
{
pData[ ( i * 4 ) ] = vTemp[ 2 ] ;
pData[ ( i * 4 ) + 1 ] = vTemp[ 1 ] ;
pData[ ( i * 4 ) + 2 ] = vTemp[ 0 ] ;
pData[ ( i * 4 ) + 3 ] = 255 ;
}
}
else
{
for ( j = 0 ; j < ct ; j++, i++ )
{
fread( &vTemp, 3, 1, pFile ) ;

pData[ ( i * 4 ) ] = vTemp[ 2 ] ;
pData[ ( i * 4 ) + 1 ] = vTemp[ 1 ] ;
pData[ ( i * 4 ) + 2 ] = vTemp[ 0 ] ;
pData[ ( i * 4 ) + 3 ] = 255 ;
}
}
}

( *pImage ) = pData ;

return true ;
}


// ----------------------------------------------------------------------------

bool TGA_Image_t::ReadCompressed16Bit( FILE* pFile, uint uSizeX, uint uSizeY, byte** pImage )
{
byte* pData ;
word wTemp ;
uint i, j, ct ;

byte iCode ;

if ( NULL == ( pData = ( byte* )malloc( uSizeX * uSizeY * 4 ) ) )
return false ;

for ( i = 0 ; i < uSizeX * uSizeY ; )
{
fread( &iCode, 1, 1, pFile ) ;

ct = ( iCode & 0x7F ) + 1 ;

if ( iCode & 0x80 )
{
fread( &wTemp, 2, 1, pFile ) ;

for ( j = 0 ; j < ct ; j++, i++ )
{
pData[ ( i * 4 ) ] = ( ( wTemp & 0xF800 ) >> 11 ) << 3 ;
pData[ ( i * 4 ) + 1 ] = ( ( wTemp & 0x07E0 ) >> 5 ) << 2 ;
pData[ ( i * 4 ) + 2 ] = ( ( wTemp & 0x001F ) << 3 ) ;
pData[ ( i * 4 ) + 3 ] = 255 ;
}
}
else
{
for ( j = 0 ; j < ct ; j++, i++ )
{
fread( &wTemp, 2, 1, pFile ) ;

pData[ ( i * 4 ) ] = ( ( wTemp & 0xF800 ) >> 11 ) << 3 ;
pData[ ( i * 4 ) + 1 ] = ( ( wTemp & 0x07E0 ) >> 5 ) << 2 ;
pData[ ( i * 4 ) + 2 ] = ( ( wTemp & 0x001F ) << 3 ) ;
pData[ ( i * 4 ) + 3 ] = 255 ;
}
}
}

( *pImage ) = pData ;

return true ;
}


// ----------------------------------------------------------------------------

bool TGA_Image_t::ReadCompressed15Bit( FILE* pFile, uint uSizeX, uint uSizeY, byte** pImage )
{
byte* pData ;
word wTemp ;
uint i, j, ct ;

byte iCode ;

if ( NULL == ( pData = ( byte* )malloc( uSizeX * uSizeY * 4 ) ) )
return false ;

for ( i = 0 ; i < uSizeX * uSizeY ; )
{
fread( &iCode, 1, 1, pFile ) ;

ct = ( iCode & 0x7F ) + 1 ;

if ( iCode & 0x80 )
{
fread( &wTemp, 2, 1, pFile ) ;

for ( j = 0 ; j < ct ; j++, i++ )
{
pData[ ( i * 4 ) ] = ( ( wTemp & 0x7C00 ) >> 10 ) << 3 ;
pData[ ( i * 4 ) + 1 ] = ( ( wTemp & 0x03E0 ) >> 5 ) << 3 ;
pData[ ( i * 4 ) + 2 ] = ( ( wTemp & 0x001F ) << 3 ) ;

// if ( m_Header.iDescriptor & 0x0F )
// {
// pData[ ( i * 4 ) + 3 ] = ( wTemp & 0x8000 ) ? 255 : 0 ;
// }
// else
pData[ ( i * 4 ) + 3 ] = 255 ;
}
}
else
{
for ( j = 0 ; j < ct ; j++, i++ )
{
fread( &wTemp, 2, 1, pFile ) ;

pData[ ( i * 4 ) ] = ( ( wTemp & 0x7C00 ) >> 10 ) << 3 ;
pData[ ( i * 4 ) + 1 ] = ( ( wTemp & 0x03E0 ) >> 5 ) << 3 ;
pData[ ( i * 4 ) + 2 ] = ( ( wTemp & 0x001F ) << 3 ) ;

// if ( m_Header.iDescriptor & 0x0F )
// {
// pData[ ( i * 4 ) + 3 ] = ( wTemp & 0x8000 ) ? 255 : 0 ;
// }
// else
pData[ ( i * 4 ) + 3 ] = 255 ;
}
}
}

( *pImage ) = pData ;

return true ;
}


// ----------------------------------------------------------------------------

bool TGA_Image_t::ReadCompressedIndex( FILE* pFile, uint uSizeX, uint uSizeY, byte** pImage )
{
byte* pData ;
byte iTemp ;
uint i, j, ct ;

byte iCode ;

if ( NULL == ( pData = ( byte* )malloc( uSizeX * uSizeY ) ) )
return false ;

for ( i = 0 ; i < uSizeX * uSizeY ; )
{
fread( &iCode, 1, 1, pFile ) ;

ct = ( iCode & 0x7F ) + 1 ;

if ( iCode & 0x80 )
{
fread( &iTemp, 1, 1, pFile ) ;

for ( j = 0 ; j < ct ; j++, i++ )
{
pData[ i ] = iTemp ;
}
}
else
{
for ( j = 0 ; j < ct ; j++, i++ )
{
fread( &pData[ i ], 1, 1, pFile ) ;
}
}
}

( *pImage ) = pData ;

return false ;
}


// ----------------------------------------------------------------------------

bool TGA_Image_t::ReadColor( FILE* pFile, uint uSizeX, uint uSizeY, byte uDepth, bool bIsCompression, byte** pImage )
{
if ( bIsCompression )
{
switch ( uDepth )
{
case 32 : return ReadCompressed32Bit( pFile, uSizeX, uSizeY, pImage ) ;
case 24 : return ReadCompressed24Bit( pFile, uSizeX, uSizeY, pImage ) ;
// case 16 : return ReadCompressed16Bit( pFile, uSizeX, uSizeY, pImage ) ;
// case 15 : return ReadCompressed15Bit( pFile, uSizeX, uSizeY, pImage ) ;
case 16 :
case 15 : return ReadCompressed15Bit( pFile, uSizeX, uSizeY, pImage ) ;
case 8 : return ReadCompressedIndex( pFile, uSizeX, uSizeY, pImage ) ;
}
}
else
{
switch ( uDepth )
{
case 32 : return Read32Bit( pFile, uSizeX, uSizeY, pImage ) ;
case 24 : return Read24Bit( pFile, uSizeX, uSizeY, pImage ) ;
// case 16 : return Read16Bit( pFile, uSizeX, uSizeY, pImage ) ;
// case 15 : return Read15Bit( pFile, uSizeX, uSizeY, pImage ) ;
case 16 :
case 15 : return Read15Bit( pFile, uSizeX, uSizeY, pImage ) ;
case 8 : return ReadIndex( pFile, uSizeX, uSizeY, pImage ) ;
}
}

return false ;
}


// ----------------------------------------------------------------------------

bool TGA_Image_t::JoinImage( void )
{
uint size ;
uint i ;

if ( NULL != m_pColorPalette && NULL != m_pPixelPalette && TGA_BLACKWHITE != m_Header.iImageFlags )
{
if ( NULL == ( m_pImage = ( byte* )malloc( m_uSizeX * m_uSizeY * 4 ) ) )
return false ;

for ( i = 0 ; i < m_uSizeX * m_uSizeY ; i++ )
{
m_pImage[ ( i * 4 ) ] = m_pColorPalette[ ( m_pPixelPalette[ i ] * 4 ) ] ;
m_pImage[ ( i * 4 ) + 1 ] = m_pColorPalette[ ( m_pPixelPalette[ i ] * 4 ) + 1 ] ;
m_pImage[ ( i * 4 ) + 2 ] = m_pColorPalette[ ( m_pPixelPalette[ i ] * 4 ) + 2 ] ;
m_pImage[ ( i * 4 ) + 3 ] = m_pColorPalette[ ( m_pPixelPalette[ i ] * 4 ) + 3 ] ;
}

SAFE_DELETE( m_pColorPalette ) ;
SAFE_DELETE( m_pPixelPalette ) ;
}
else
if ( NULL != m_pColorPalette && NULL != m_pPixelPalette && TGA_BLACKWHITE == m_Header.iImageFlags )
{
if ( NULL == ( m_pImage = ( byte* )malloc( m_uSizeX * m_uSizeY * 4 ) ) )
return false ;

for ( i = 0 ; i < m_uSizeX * m_uSizeY ; i++ )
{
m_pImage[ ( i * 4 ) ] = m_pPixelPalette[ i ] ;
m_pImage[ ( i * 4 ) + 1 ] = m_pPixelPalette[ i ] ;
m_pImage[ ( i * 4 ) + 2 ] = m_pPixelPalette[ i ] ;
m_pImage[ ( i * 4 ) + 3 ] = m_pPixelPalette[ i ] ;
}

SAFE_DELETE( m_pPixelPalette ) ;
}
else
if ( NULL == m_pColorPalette && NULL != m_pPixelPalette )
{
m_pImage = m_pPixelPalette ;
m_pPixelPalette = NULL ;
}
else
return false ;

return true ;
}


// ----------------------------------------------------------------------------

void TGA_Image_t::SwapPixel( uint uX1, uint uY1, uint uX2, uint uY2 )
{
byte* pPixelA ;
byte* pPixelB ;
byte vBuffer[ 4 ] ;

if ( NULL == m_pImage )
return ;

pPixelA = &m_pImage[ uX1 + ( uY1 * TGA_SizeX( m_Header ) ) ] ;
pPixelB = &m_pImage[ uX1 + ( uY1 * TGA_SizeX( m_Header ) ) ] ;

if ( pPixelA != pPixelB )
{
vBuffer[ 0 ] = pPixelA[ 0 ] ;
vBuffer[ 1 ] = pPixelA[ 1 ] ;
vBuffer[ 2 ] = pPixelA[ 2 ] ;
vBuffer[ 3 ] = pPixelA[ 3 ] ;
pPixelA[ 0 ] = pPixelB[ 0 ] ;
pPixelA[ 1 ] = pPixelB[ 1 ] ;
pPixelA[ 2 ] = pPixelB[ 2 ] ;
pPixelA[ 3 ] = pPixelB[ 3 ] ;
pPixelB[ 0 ] = vBuffer[ 0 ] ;
pPixelB[ 1 ] = vBuffer[ 1 ] ;
pPixelB[ 2 ] = vBuffer[ 2 ] ;
pPixelB[ 3 ] = vBuffer[ 3 ] ;
}
}


// ----------------------------------------------------------------------------

void TGA_Image_t::FlipImage( void )
{
uint uX, uY ;

// FIXME : Ignore this for OpenGL's inverted-Y sakes?
if ( TGA_FlipV( m_Header ) )
{
for ( uX = 0 ; uX < TGA_SizeX( m_Header ) ; uX++ )
for ( uY = 0 ; uY < TGA_SizeY( m_Header ) ; uY++ )
{
SwapPixel( uX, uY, uX, TGA_SizeY( m_Header ) - uY ) ;
}
}

if ( TGA_FlipH( m_Header ) )
{
for ( uY = 0 ; uY < TGA_SizeY( m_Header ) ; uY++ )
for ( uX = 0 ; uX < TGA_SizeX( m_Header ) ; uX++ )
{
SwapPixel( uX, uY, TGA_SizeY( m_Header ) - uX, uY ) ;
}
}
}


Sorry for the long, long post... But I spent a lot of time messing around with this particular load so that it looks pretty and I still feel it needs work! But oh well.
I'll step out from the shadows and say that yes, I'm in the same situation as you. What triggers my OCD is the need to discover all the many different ways a problem can be solved (both simple and complex) before going ahead and choosing the "cleanest" one. I love programming and am really good at it (I'm extremely modest so it took some effort to type that), but I shy away from the industry because I'm afraid that I won't be able to control it when dealing with other people's code.

At university I'd strive to make 100% bug-free code (and yes, it irritates me when people say that this isn't possible, and yes, it irritates me when I'm forced to use a buggy 3rd party library that disobeys its own documentation), and in projects where I'd have to collaborate with other students who didn't care about how buggy their code was, I'd have to turn a blind eye to it or go insane and piss them off by rewriting their entire code base behind their backs in 5 minutes flat.

I think the industry would kill any passion I have for programming, but if I were to come up with a decent game idea that I could develop on my own, I'd assure you that I'd have no problem dedicating my passion to completing it.

EDIT: Your code is so pretty :)
Thanks for the compliment! I think people like you or me or others that are really perfectionist about their code just see it in a entirely different light as do other people. To me, any ways, it see my code as showing respect for others and the language I choose to write it in. I like having things cleanly organized for that fact. I would hate to have someone come look over my code and say that they could understand it or it was too sloppy so they didn't bother. Oh well though... OCD!

I've rewrote some of that TGA loader already. I've was working on it anyways so...

Thanks for the compliment! I think people like you or me or others that are really perfectionist about their code just see it in a entirely different light as do other people. To me, any ways, it see my code as showing respect for others and the language I choose to write it in. I like having things cleanly organized for that fact. I would hate to have someone come look over my code and say that they could understand it or it was too sloppy so they didn't bother. Oh well though... OCD!

I've rewrote some of that TGA loader already. I've was working on it anyways so...

I'm a just a Java coder so forgive my ignorance, but why do you declare your variables at the beginning of the functions? I've fixed logical problems caused by this so many times it's not funny and all of those could have been caught by the compiler had a proper scope been used. Big scopes are the enemy of simplicity.

You've got commented out code all over. What is up with that? Don't you have version control?

Couldn't that byte swapping in SwapPixel be replaced with some generic array swapping function?

I'd say that you are obsessed with whitespace :)

As the book Clean Code said it use boy scout mentality. Leave the code that you touch in better shape than it was in when you started.
I can fall victim to something of the same problem and still don't always avoid it completely, but I find that trying to hold myself to "doing it right the first time" reduces the amount of time I feel like going back into functional code later.

This often means that I might have an idea or rough plan in my mind for weeks or even months before I write any code, and during that time I'm refining the idea whenever I have a little time to think about it -- perhaps I read a relevant article that sparked an idea, or I'm just sitting on a bus -- not working on an idea the moment you have it isn't a problem as long as a) you aren't prone to loosing the idea (and if so, write it down in a task list) and B) have enough other work to keep you reasonably busy.

Another part of this is developing good habits and sticking to them -- only change those habits when you are certain a better alternative exists, and never change habits in the middle of a single project unless you are sure its a net gain. This covers a lot of ground -- naming conventions, use of idioms and patterns, code organization, etc.

Also, code that demonstrates, if nothing else, good encapsulation and cohesion properties should be resistant to refactorings spreading through all of your code like a cancer -- if your refactoring tends to have far-reaching consequences its a red flag that perhaps your class design is not what it could be.

throw table_exception("(? ???)? ? ???");

This topic is closed to new replies.

Advertisement