File: spriteBank.cpp

package info (click to toggle)
primrose 6%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 5,232 kB
  • ctags: 3,287
  • sloc: cpp: 27,302; php: 765; ansic: 636; objc: 272; makefile: 139; sh: 136; perl: 67
file content (174 lines) | stat: -rw-r--r-- 3,918 bytes parent folder | download | duplicates (5)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "spriteBank.h"

#include "common.h"


#include "minorGems/util/SimpleVector.h"
#include "minorGems/util/stringUtils.h"

#include "minorGems/graphics/openGL/SingleTextureGL.h"


const char *spriteNames[ numSprites ] = 
{ 
    "gridLineTop",
    "gridLineBottom",
    "gridLineLeft",
    "gridLineRight",
    "plus",
    "piece",
    "pieceHalo",
    "pieceCenter",
    "pieceBrightHalo",
    "pieceBrightCenter",
    "numerals",
    "numeralsBig",
    "abcdefghijkl",
    "mnopqrstuvwx",
    "yzplus",
    "abcdefghijkl_big",
    "mnopqrstuvwx_big",
    "yzplus_big"
    };



SingleTextureGL *spriteTextures[ numSprites ];


void initSpriteBank() {
    for( int i=0; i<numSprites; i++ ) {
        
        char *fileName = autoSprintf( "%s.tga", spriteNames[ i ] );
        
        Image *image = readTGA( fileName );
        
        
        
        if( image != NULL ) {
                
            spriteTextures[ i ] = new SingleTextureGL( image, true );
            
            delete image;
            }
        else {
            spriteTextures[i] = NULL;
            }

        delete [] fileName;

        }
    }



void freeSpriteBank() {
    for( int i=0; i<numSprites; i++ ) {
        if( spriteTextures[i] != NULL ) {
            delete spriteTextures[i];
            }
        }
    }



// global
extern int spriteDrawCount;



void drawSprite( SpriteHandle inSpriteHandle,
                 float inCenterX, float inCenterY, 
                 float inXRadius, float inYRadius,
                 Color *inColor, float inAlpha,
                 float inSubsectionOffset,
                 float inSubsectionExtent ) {
    

    SingleTextureGL *texture = spriteTextures[ inSpriteHandle ];

    if( texture == NULL ) {
        return;
        }
    
    
    
    
    
    
    if( inColor != NULL  ) {
        glColor4f( inColor->r, inColor->g, inColor->b, inAlpha );
        }
    else {
        glColor4f( 1, 1, 1, inAlpha );
        }

    
    

    texture->enable(); 


    // NOTE:  this won't look good if we do the zoom-to-clear effect
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); 
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );


    const GLfloat squareVertices[] = {
        inCenterX - inXRadius, inCenterY - inYRadius,
        inCenterX + inXRadius, inCenterY - inYRadius,
        inCenterX - inXRadius, inCenterY + inYRadius,
        inCenterX + inXRadius, inCenterY + inYRadius,
        };
    /*
    const GLubyte squareColors[] = {
        255, 255,   0, 255,
        0,   255, 255, 255,
        255,     0,   0,   0,
        255,   0, 255, 255,
    };
     */
    const GLfloat squareTextureCoords[] = {
        0, inSubsectionOffset,
        1, inSubsectionOffset,
        0, inSubsectionOffset + inSubsectionExtent,
        1, inSubsectionOffset + inSubsectionExtent
    };

    

    glVertexPointer( 2, GL_FLOAT, 0, squareVertices );
    glEnableClientState( GL_VERTEX_ARRAY );
    
    //glColorPointer( 4, GL_FLOAT, 0, squareColors );
    //glEnableClientState( GL_COLOR_ARRAY );
    
    glTexCoordPointer( 2, GL_FLOAT, 0, squareTextureCoords );
    glEnableClientState( GL_TEXTURE_COORD_ARRAY );
    
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

    /*

    glBegin( GL_QUADS ); {
    
        glTexCoord2f( 0, inSubsectionOffset );
        glVertex2f( inCenterX - inXRadius, inCenterY - inYRadius );
        
        glTexCoord2f( 1, inSubsectionOffset );
        glVertex2f( inCenterX + inXRadius, inCenterY - inYRadius );
        
        glTexCoord2f( 1, inSubsectionOffset + inSubsectionExtent );
        glVertex2f( inCenterX + inXRadius, inCenterY + inYRadius );
        
        glTexCoord2f( 0, inSubsectionOffset + inSubsectionExtent );
        glVertex2f( inCenterX - inXRadius, inCenterY + inYRadius );        
        }
    glEnd();
    */
    texture->disable();

    spriteDrawCount ++;

    }