File: Sprite.cpp

package info (click to toggle)
between 6%2Bdfsg1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 3,524 kB
  • sloc: cpp: 28,110; php: 718; ansic: 638; objc: 245; sh: 236; makefile: 102; perl: 67
file content (241 lines) | stat: -rw-r--r-- 6,323 bytes parent folder | download | duplicates (3)
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include "Sprite.h"
#include "common.h"


#include "minorGems/math/geometry/Angle3D.h"



Sprite::Sprite( const char *inImageFileName,
                char inTransparentLowerLeftCorner,
                int inNumFrames,
                int inNumPages ) {
    Image *spriteImage = readTGA( inImageFileName );

    if( spriteImage == NULL ) {
        printf( "Failed to read image file:  %s\n", inImageFileName );
        }
    

    initTexture( spriteImage, inTransparentLowerLeftCorner, inNumFrames,
                 inNumPages );
    
    delete spriteImage;
    }


Sprite::Sprite( Image *inImage,
                char inTransparentLowerLeftCorner,
                int inNumFrames,
                int inNumPages ) {
    
    initTexture( inImage, inTransparentLowerLeftCorner, inNumFrames,
                 inNumPages );
    }
 


void Sprite::initTexture( Image *inImage,
                          char inTransparentLowerLeftCorner,
                          int inNumFrames,
                          int inNumPages ) {
    
    mNumFrames = inNumFrames;
    mNumPages = inNumPages;
    
    Image *spriteImage = inImage;
    
    Image *imageToDelete = NULL;
    
    if( spriteImage->getNumChannels() == 3 && inTransparentLowerLeftCorner ) {
        // use lower-left corner color as transparent color for alpha

        Image *fourChannelImage = new Image( spriteImage->getWidth(),
                                             spriteImage->getHeight(),
                                             4, false );
        
        // copy first three
        for( int c=0; c<3; c++ ) {
            fourChannelImage->pasteChannel( spriteImage->getChannel( c ),
                                            c );
            }

        double *r = fourChannelImage->getChannel( 0 );
        double *g = fourChannelImage->getChannel( 1 );
        double *b = fourChannelImage->getChannel( 2 );
        
        // index of transparency
        int tI = 
            fourChannelImage->getWidth() * 
            ( fourChannelImage->getHeight() - 1 );
        
        // color of transparency
        double tR = r[tI];
        double tG = g[tI];
        double tB = b[tI];
        
        double *alpha = fourChannelImage->getChannel( 3 );
        
        int numPixels = 
            fourChannelImage->getWidth() * 
            fourChannelImage->getHeight();
        
        for( int i=0; i<numPixels; i++ ) {
            
            if( r[i] == tR && 
                g[i] == tG && 
                b[i] == tB ) {
                alpha[i] = 0;
                }
            else {
                alpha[i] = 1;
                }
            }

        spriteImage = fourChannelImage;
        imageToDelete = fourChannelImage;
        }
    
                

    mTexture = new SingleTextureGL( spriteImage,
                                    // no wrap
                                    false );

    mBaseScaleX = spriteImage->getWidth() / mNumPages;
    mBaseScaleY = spriteImage->getHeight() / mNumFrames;
    
    if( imageToDelete != NULL ) {
        delete imageToDelete;
        }

    mFlipHorizontal = false;
    mHorizontalOffset = 0;
    mCurrentPage = 0;
    }



Sprite::~Sprite() {
    delete mTexture;
    }



// opt (found with profiler)
// only construct these once, not every draw call
Vector3D corners[4];

        

void Sprite::draw( int inFrame, 
                   double inRotation, Vector3D *inPosition, 
                   double inScale,
                   double inFadeFactor,
                   Color *inColor ) {
    /*
    printf( "Drawing sprite %d, r%f, (%f,%f), s%f, f%f\n",
            (int)(this), inRotation, inPosition->mX, inPosition->mY, inScale,
            inFadeFactor );
    */
    
    // profiler opt:
    // this is expensive, and the game never needs it
    // (all frame references are specific and never auto-cycling)
    // inFrame = inFrame % mNumFrames;
    

    double xRadius = inScale * mBaseScaleX / 2;
    double yRadius = inScale * mBaseScaleY / 2;

    double xOffset = mHorizontalOffset * inScale;
    

    if( mFlipHorizontal ) {
        xRadius = -xRadius;
        }
    


    // first, set up corners relative to 0,0
    // loop is unrolled here, with all offsets added in
    // also, mZ ignored now, since rotation no longer done
    double posX = inPosition->mX + xOffset;
    double posY = inPosition->mY;
    
    corners[0].mX = posX - xRadius;
    corners[0].mY = posY - yRadius;
    //corners[0].mZ = 0;

    corners[1].mX = posX + xRadius;
    corners[1].mY = posY - yRadius;
    //corners[1].mZ = 0;

    corners[2].mX = posX + xRadius;
    corners[2].mY = posY + yRadius;
    //corners[2].mZ = 0;

    corners[3].mX = posX - xRadius;
    corners[3].mY = posY + yRadius;
    //corners[3].mZ = 0;

    // int i;
    

    // now rotate around center
    // then add inPosition so that center is at inPosition
    
    // Found with profiler:  ignore rotation, since game doesn't use it anyway.
    // Angle3D rot( 0, 0, inRotation );
    
    // found with profiler:
    // unroll this loop
    /*
    for( i=0; i<4; i++ ) {
        corners[i].mX += xOffset;
        
        // corners[i].rotate( &rot );
        corners[i].add( inPosition );
        }
    */

    if( inColor != NULL  ) {
        glColor4f( inColor->r, inColor->g, inColor->b, inFadeFactor );
        }
    else {
        glColor4f( 1, 1, 1, inFadeFactor );
        }
                
    mTexture->enable();
    
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); 
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );

    double textXA = (1.0 / mNumPages) * mCurrentPage;
    double textXB = textXA + (1.0 / mNumPages );


    double textYB = (1.0 / mNumFrames) * inFrame;
    double textYA = textYB + (1.0 / mNumFrames );
    
    
    
    glBegin( GL_QUADS ); {
        
        glTexCoord2f( textXA, textYA );
        glVertex2d( corners[0].mX, corners[0].mY );
        
        glTexCoord2f( textXB, textYA );
        glVertex2d( corners[1].mX, corners[1].mY );
        
        glTexCoord2f( textXB, textYB );
        glVertex2d( corners[2].mX, corners[2].mY );
        
        glTexCoord2f( textXA, textYB );
        glVertex2d( corners[3].mX, corners[3].mY );
        }
    glEnd();
    mTexture->disable();
    }