File: NamedColorFactory.cpp

package info (click to toggle)
transcend 0.3.dfsg1-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 5,816 kB
  • ctags: 2,912
  • sloc: cpp: 26,890; ansic: 693; sh: 210; makefile: 131; perl: 67
file content (97 lines) | stat: -rw-r--r-- 2,504 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Modification History
 *
 * 2004-June-12   Jason Rohrer
 * Created.
 *
 * 2004-June-14   Jason Rohrer
 * Changed to allow specification of a directory to read colors from.
 *
 * 2004-June-15   Jason Rohrer
 * Changed to use LevelDirectoryManager to determine where to read colors from.
 *
 * 2004-August-12   Jason Rohrer
 * Optimized Color constructor.
 */



#include "NamedColorFactory.h"
#include "LevelDirectoryManager.h"


#include "minorGems/io/file/File.h"
#include "minorGems/util/stringUtils.h"


#include <stdio.h>



Color *NamedColorFactory::getColor( char *inColorName ) {

    // will remain NULL if reading the color fails.
    Color *color = NULL;
    

    File *levelDirectory = LevelDirectoryManager::getLevelDirectory();
    File *colorDirectory = levelDirectory->getChildFile( "colors" );

    delete levelDirectory;

    
    File *colorFile = colorDirectory->getChildFile( inColorName );
    delete colorDirectory;

    
    if( colorFile != NULL ) {
        
        if( colorFile->exists() ) {
            
            char *contents = colorFile->readFileContents();

            if( contents != NULL ) {

                SimpleVector<char *> *tokens = tokenizeString( contents );

                int numTokens = tokens->size();

                if( numTokens == 4 ) {

                    float red, green, blue, alpha;

                    int totalNumRead = 0;

                    totalNumRead += sscanf( *( tokens->getElement( 0 ) ),
                                            "%f", &red );
                    totalNumRead += sscanf( *( tokens->getElement( 1 ) ),
                                            "%f", &green );
                    totalNumRead += sscanf( *( tokens->getElement( 2 ) ),
                                            "%f", &blue );
                    totalNumRead += sscanf( *( tokens->getElement( 3 ) ),
                                            "%f", &alpha );

                    // make sure we read all 4 values
                    if( totalNumRead == 4 ) {
                        color = new Color( red, green, blue, alpha, false );
                        }
                    }

                for( int i=0; i<numTokens; i++ ) {
                    delete [] *( tokens->getElement( i ) );
                    }
                delete tokens;
                
                
                delete [] contents;
                }
            }
        
        delete colorFile;
        }
    
    
    return color;
    }