• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
mind_wipe

Obsessive Compulsive/Refactoring Disorder

27 posts in this topic

I know I have this. Why me!? I hate it... Sometimes I feel like I can't get anything done. Like it takes hours to do the simplest task I've done before. I'm always changing code to make it look pretty or other things. It's too much! Please someone tell me I'm not the only one going through this here? I've read that I have to face the fear and not allow my self, or ignore the OCD. I don't know man...

So I've been working on this game for a while and I keep getting stuck on basic architecture. I keep re-writing or changing the image & sound loaders. Constantly loading up thesaurus.com looking for a different word that would align with other variable names... see what I mean? It's bad. I know its most important to deliver a working product, NOT what the source code OF the working product looks like. I've seen lots of professional products and it seams that these guys don't suffer like I do.

There is a catch though. If I re-factor to much then, the OCD part of me, gets all pushed out of shape because to code looks to clean or to procedural if you know what I mean. So it seams that OCD has double standards... or maybe double edged. Either way its holding me back and I'm not able to complete what I want to do. Anyone else feel my pain? I just don't know what to do with my self...
0

Share this post


Link to post
Share on other sites
I'd like to believe it's a stage, but I do the same thing. I've never seen it as a problem though. I value clean organized code and it bothers me when code I work on is not formatted correctly. I've gone through tens of thousands of lines of code and formatted them all to a strict style in the past when I had time. It's important to realize that everyone else is wrong and that you are right when it comes to these changes. B)

Naming problems are common problems. It gets easier to name methods and classes with experience.

Also refactoring code to make it better is a good practice. As said a thousand times evolution, not revolution.

As a tip I recommend coding with whitespace hidden characters on. It helps to relieve the anxiety of not knowing. :unsure:
0

Share this post


Link to post
Share on other sites
[quote name='mind_wipe' timestamp='1299370600' post='4782257']
I know I have this. Why me!? I hate it... Sometimes I feel like I can't get anything done. Like it takes hours to do the simplest task I've done before. I'm always changing code to make it look pretty or other things. It's too much! Please someone tell me I'm not the only one going through this here? I've read that I have to face the fear and not allow my self, or ignore the OCD. I don't know man...
[/quote]
I think it's just you. [img]http://public.gamedev.net/public/style_emoticons/default/biggrin.gif[/img]


[quote]
So I've been working on this game for a while and I keep getting stuck on basic architecture. I keep re-writing or changing the image & sound loaders. Constantly loading up thesaurus.com looking for a different word that would align with other variable names... see what I mean? It's bad. I know its most important to deliver a working product, NOT what the source code OF the working product looks like. I've seen lots of professional products and it seams that these guys don't suffer like I do.[/quote]
Well, if you rewrite modules and make them faster or better, there's nothing wrong with it...If you are just changing variable names, then you've got issues.


[quote]
There is a catch though. If I re-factor to much then, the OCD part of me, gets all pushed out of shape because to code looks to clean or to procedural if you know what I mean. So it seams that OCD has double standards... or maybe double edged. Either way its holding me back and I'm not able to complete what I want to do. Anyone else feel my pain? I just don't know what to do with my self...
[/quote]
Yes and no, but does your IDE has a code formatting tools? Don't use freaking notepad or vim or gedit just because they are fast. So many times I have looked at source code that others have written, and so many times I have seen them improperly formatted. This shit would bug me as hell:

[code]
if (someFlag)
doSomething();

if( someOtherFlag ) {
do_something_else();
}
[/code]

Select All -> Reformat Code. Then refactor do_something_else() to doSomethingElse(). Done.
0

Share this post


Link to post
Share on other sites
I've had this for ages. Something I've started experimenting with recently is schedules. Turns out the same thing that the games industry is so often decried for: unreasonable deadlines and milestones -- are a good way to cajole oneself into coding up a storm with little regard for good architecture. Of course, this way potentially lies unmaintainable spaghetti. A solution for that is obvious: Schedule time specifically for refactoring too. Not so much that you spend all your time in refactoring hell, not so little that you spend more time trying to understand your code than add to it.

I've been doing 3 hour "jaunts" for personal projects off and on, with 30 minute scheduling intervals. Of course, I could spend forever comming up with an ideal schedule: So I schedule myself 10 minutes to come up with a schedule. I coded up a small program to help me keep track of exactly how far behind I'm getting, even.

It seems to work best when I have a clear idea of what I want done. This includes refactoring too: for me, I can't just add a 30 minutes task, "Herp derp refactoring", and hope to get something good done. On the other hand, when I've got a pile of spaghetti in mind, and I know I want to split it up into 4 classes along obvious lines -- sure, I'll throw 30 minutes for "refactoring" onto the list. If I overbudgeted for that specific change, great -- start the next task early. If I underbudgeted, that's another class or two worth of behavior I can refactor out of my mess tomorrow.

Consider trying it :)
0

Share this post


Link to post
Share on other sites
Frigging whitespaces at the ends of lines. Running a macro on them every 5 seconds is slow and looking at them when a block of text is selected burns the eyes even more. Horrible. Just horrbile.
0

Share this post


Link to post
Share on other sites
What I've found most useful is 50/50 as a guide: Do not spend more time refactoring than developing new functionality.

Sure, one could spend hours, or days, or weeks improving the functionality, here and there, just a little... But you do need to get something done and deliver it.

If it's taking significantly longer to refactor than to write real functionality, then something is really wrong. Even if it's really bad code that someone else wrote, then it's time to recognize that some of the correction and repair of their bad work will have to be put off until later. If it's one's own work, then maybe a little more discipline while doing the work is in order. ;->
0

Share this post


Link to post
Share on other sites
And here I thought I was just about the only person who still cared about spaces at the ends of lines! ;->

I knock them off when I happen to be working on those lines for other reasons. And any good editor that can handle regular expressions can kill them easily enough. And formatting tools can do it for you too.

Still, it puzzles me as to how and why people keep adding spaces to the ends of lines -- seemingly by habit, at times. Some behavior is just odd.

What I've found most helpful is to get the team to agree to an automatic formatter, and then apply it consistently -- preferably automatically at check-in. Yes, I dislike automated formatters too. But I've found the inability of some programmers to keep with anything like a consistent style, even all the way through a single line of code, to be quite annoying. Code is read much more than written. It has to be readable.
0

Share this post


Link to post
Share on other sites
There are a lot of things that annoy me: Unimaginative or slightly inconsistent variable/function names, or perhaps the organisation of code blocks are somewhat weird compared to the rest of the project. In my world, these problems tend to appear when I work on something for a long time, or perhaps I revisit a project that was shelved a while back. I noticed my coding style can change over time, which is probably the primary reason why I have these issues. But these a minor issues. I have to discipline myself not to polish too much, and rather focus on getting things done. Therefore, I tend to refactor things when adding a new feature to a section of code. More importantly, I limit refactoring to that part of the code only. I mean, new features need a fair bit of work anyway, so might as well improve the stuff around it at the same time.... [b]with care[/b].

[quote]Still, it puzzles me as to how and why people keep adding spaces to the ends of lines -- seemingly by habit, at times. Some behavior is just odd.[/quote]
Virtual spaces. It usually happens to me when I comment code, then for whatever reason I remove them later, but forget to cull the whitespace before the comment. Another reason, leftovers from indentation.

I'm actually surprised most code editors don't have a feature that strips whitespaces for you (at least in VS i cant find it). I remember Borland Pascal and C had it a while back.
0

Share this post


Link to post
Share on other sites
At work I apply a simple rule: I do not allow myself to cleanup or refactor code unless I have something that needs to be done in the file. So if I'm not fixing a bug or adding a feature, then I leave well enough alone. I don't quite have the disciple to force myself to do this with my own code, but I'm trying.
MaulingMonkey's points are good. If you don't have a schedule, or a bug tracker to prioritise work, then you end up wasting time doing this kind of thing. Refactoring feels like work, but rarely gets the project closer to release. Refactoring is supposed to be a [b]time saver[/b], making the code easier to read, debug, maintain and extend. If you aren't saving time doing it (in the long run), then something is wrong.
0

Share this post


Link to post
Share on other sites
For a time, when working on a project, I would feel slightly displeased with how some code look, that I would start completely from scratch. This compulsion was so disruptive on my projects... it's tough, but I'm trying very hard to keep what I have, and make minimal, surgical changes, as needed, without throwing the baby out with the bath water.

Regarding code refactoring, it is important to have clean, organized code, but it's also important to get your deliverables out there! Users care about the final product, not the code =)
0

Share this post


Link to post
Share on other sites
[quote name='Sirisian' timestamp='1299371283' post='4782261']
As a tip I recommend coding with whitespace hidden characters on. It helps to relieve the anxiety of not knowing. :unsure:
[/quote]

I feel your pain brother!
0

Share this post


Link to post
Share on other sites
Whitespaces at line ends are usually byproducts of automatic formatting or, more commonly, copy-pasting into an already formatted line. Repeated copy-pasting compounds the effect and eventually (eg after you've reformatted your loop like 10 times) you'll end up with more spaces trailing after the closing brace than there are actual characters in the statement. Nasty (well, not really - just annoying) stuff that I think The IDE should take care of automatically.
0

Share this post


Link to post
Share on other sites
[quote name='mind_wipe' timestamp='1299370600' post='4782257']
I've seen lots of professional products and it seams that these guys don't suffer like I do.
[/quote]
That's just because they can lose their jobs if they miss their deadlines. If they had money and time they'd probably be at least nearly as bad.
0

Share this post


Link to post
Share on other sites
Mindwipe - I know I probably shouldn't write this. And I know this will look all too ironic as soon as I click Submit and it loads up in my shiny Firefox web browser, which I've grown to hate so fondly; and I know - I [i]know[/i] - you would never consider anything like this - not seriously at least -, because you're a socially responsible male human.

Nevertheless, having re-read your post and analyzed its apparent cry for reason in a universe that is bordering so close to madness, I have to ask - if only to suggest an alternative solution: have you tried using alcohol to keep your OCD in check and to write more creative code? I know it's not much, but for inspiration, look at Charlie Sheen. Just look at him. Not a worry in the world. Zero refactoring problems. I mean, you can't just come up with that stuff. You need a muse. Let the good old A-hol be your muse.
0

Share this post


Link to post
Share on other sites
Yeah I think I screwed up when I wrote the topic. By refactoring I met changing code to make it look more pleasing to the eye; not just functionality. I don't know why, but for some reason ugly code reminds me of ex-lax. It irritates the *** out of me. Then OCD kicks to change, reword or event rewrite things to make them look neater then a skeeter's peter....lol This is what I'm talking about(the code, not skeeter). I get too obsessive/compulsive about cleanliness and order. Not just in architecture as I said before, but in code formatting, spacing, caps on *certian* comments, where the code hands over 80(column) characters or not, adding spaces at the ends of lines(hehe j/k). Stuff like inserting tabs so that all the variables align to the 29th column, But then that doesn't do because 29 is an odd number so I add a space to make it 30....gawd that sounds so bad when I write it out... but that is what I do sometimes.

I have gotten better because I forced my self to slap some things together and ignore what it looked like, only looking at it functionality wise. Its seams when I just slap stuff together for tests and/or other stuff I get the most successful code. Successful as it only took like 10-15 minutes and its working. So any ways... sorry for the bad news guys
0

Share this post


Link to post
Share on other sites
Its called designing on paper. You can spend a day designing and a week of coding, or no design and a month of coding blindly on the fly.
0

Share this post


Link to post
Share on other sites
Ive been programming 6 hours every single day since I was 12 and got my first 286 laptop with monochrome monitor :)
I probably spend 5.5 of those hours optimizing code.
I never read tutorials, manuals, references, but I find myself asking a few awesome programmers I know on IRC alot about stuff now and then.
Its probably the worst way to code there is, and my projects average on 3-5 years from start to finish. But I like it! I must code!!

Happy coding!
0

Share this post


Link to post
Share on other sites
I think if you ever want to get anything done just stick with a certain convention and follow it through no matter what.

Sticking with a convention (or even an initial concept) for the entirety of a project makes you a better programmer than any new architectural design ever could, as it generally means you get the project finished.

I was recently working on a project with a guy that was a bit too much of a perfectionist when it came to coding and it made me realize just how stupid it is to be perfect when it comes to code. 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?
0

Share this post


Link to post
Share on other sites
[quote]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? [/quote]
That doesn't mean you have to aim low. ;)
0

Share this post


Link to post
Share on other sites
[quote name='Tachikoma' timestamp='1299636620' post='4783367']
[quote]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? [/quote]
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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
[color="#1C2837"][size="2"][quote]As a tip I recommend coding with whitespace hidden characters on. It helps to relieve the anxiety of not knowing. :unsure: [/quote][/size][/color]
[size="2"][color="#1C2837"]My end key is worn down. [/color][/size][img]http://public.gamedev.net/public/style_emoticons/default/sad.gif[/img]
0

Share this post


Link to post
Share on other sites
This is an example of what I'm talking about...

[code]

// ----------------------------------------------------------------------------
// 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 ) ;
}
}
}
[/code]

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.
0

Share this post


Link to post
Share on other sites
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 :)
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0