~ubuntu-branches/ubuntu/precise/primrose/precise

« back to all changes in this revision

Viewing changes to tilePlacementGames/game1/gameSource/numeral.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Paul Wise
  • Date: 2009-04-06 19:26:56 UTC
  • Revision ID: james.westby@ubuntu.com-20090406192656-cri7503gebyvfl8t
Tags: upstream-5+dfsg1
ImportĀ upstreamĀ versionĀ 5+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#include "numeral.h"
 
3
#include "spriteBank.h"
 
4
 
 
5
#include <GL/gl.h>
 
6
 
 
7
#include <math.h>
 
8
#include <ctype.h>
 
9
#include <string.h>
 
10
 
 
11
 
 
12
 
 
13
void drawNumeral( int inNumber,
 
14
                  float inCenterX, float inCenterY,
 
15
                  Color *inColor, float inAlpha ) {
 
16
 
 
17
 
 
18
    
 
19
 
 
20
    drawSprite( numerals,
 
21
                inCenterX, inCenterY,
 
22
                8, 10,
 
23
                inColor, inAlpha,
 
24
                inNumber * 20 / 256.0f,
 
25
                20 / 256.0f );
 
26
 
 
27
    }
 
28
 
 
29
 
 
30
 
 
31
void drawNumeralBig( int inNumber,
 
32
                     float inCenterX, float inCenterY,
 
33
                     Color *inColor, float inAlpha ) {
 
34
 
 
35
 
 
36
    
 
37
    drawSprite( numeralsBig,
 
38
                inCenterX, inCenterY,
 
39
                16, 20,
 
40
                inColor, inAlpha,
 
41
                inNumber * 40 / 512.0f,
 
42
                40 / 512.0f );
 
43
 
 
44
    }
 
45
 
 
46
 
 
47
 
 
48
void drawLetter( char inLetter,
 
49
                 float inCenterX, float inCenterY,
 
50
                 Color *inColor, float inAlpha ) {
 
51
    
 
52
    // default to 'a'
 
53
    SpriteHandle handle = abcdefghijkl;
 
54
    int indexOffset = 0;
 
55
    
 
56
    float imageHeight = 256.0f;
 
57
    
 
58
    
 
59
 
 
60
    if( inLetter == '+' ) {
 
61
        handle = yzplus;
 
62
        indexOffset = 2;
 
63
        imageHeight = 64.0f;
 
64
        }
 
65
    else {
 
66
        inLetter = (char)tolower( inLetter );
 
67
        
 
68
        if( inLetter >= 'a' && inLetter <= 'z' ) {
 
69
        
 
70
            if( inLetter <= 'l' ) {
 
71
                handle = abcdefghijkl;
 
72
                indexOffset = inLetter - 'a';
 
73
                }
 
74
            else if( inLetter <= 'x' ) {
 
75
                handle = mnopqrstuvwx;
 
76
                indexOffset = inLetter - 'm';
 
77
                }
 
78
            else {
 
79
                handle = yzplus;
 
80
                indexOffset = inLetter - 'y';
 
81
                imageHeight = 64.0f;
 
82
                }
 
83
            }
 
84
        else {
 
85
            printf( "Error:  letter out of 'a-z' or '+' range: %c\n",
 
86
                    inLetter );
 
87
            }
 
88
        }
 
89
    
 
90
    
 
91
 
 
92
    drawSprite( handle,
 
93
                inCenterX, inCenterY,
 
94
                8, 10,
 
95
                inColor, inAlpha,
 
96
                indexOffset * 20 / imageHeight,
 
97
                20 / imageHeight );
 
98
    }
 
99
 
 
100
 
 
101
 
 
102
void drawLetterBig( char inLetter,
 
103
                    float inCenterX, float inCenterY,
 
104
                    Color *inColor, float inAlpha ) {
 
105
    
 
106
    // default to 'a'
 
107
    SpriteHandle handle = abcdefghijkl_big;
 
108
    int indexOffset = 0;
 
109
    
 
110
    float imageHeight = 512.0f;
 
111
    
 
112
    
 
113
 
 
114
    if( inLetter == '+' ) {
 
115
        handle = yzplus_big;
 
116
        indexOffset = 2;
 
117
        imageHeight = 128.0f;
 
118
        }
 
119
    else {
 
120
        inLetter = (char)tolower( inLetter );
 
121
        
 
122
        if( inLetter >= 'a' && inLetter <= 'z' ) {
 
123
        
 
124
            if( inLetter <= 'l' ) {
 
125
                handle = abcdefghijkl_big;
 
126
                indexOffset = inLetter - 'a';
 
127
                }
 
128
            else if( inLetter <= 'x' ) {
 
129
                handle = mnopqrstuvwx_big;
 
130
                indexOffset = inLetter - 'm';
 
131
                }
 
132
            else {
 
133
                handle = yzplus_big;
 
134
                indexOffset = inLetter - 'y';
 
135
                imageHeight = 128.0f;
 
136
                }
 
137
            }
 
138
        else {
 
139
            printf( "Error:  letter out of 'a-z' or '+' range: %c\n",
 
140
                    inLetter );
 
141
            }
 
142
        }
 
143
    
 
144
    
 
145
 
 
146
    drawSprite( handle,
 
147
                inCenterX, inCenterY,
 
148
                16, 20,
 
149
                inColor, inAlpha,
 
150
                indexOffset * 40 / imageHeight,
 
151
                40 / imageHeight );
 
152
    }
 
153
 
 
154
 
 
155
 
 
156
 
 
157
 
 
158
 
 
159
 
 
160
 
 
161
void drawScorePip( int inScore,
 
162
                   float inCenterX, float inCenterY,
 
163
                   Color *inColor, float inAlpha ) {
 
164
    
 
165
    int numDigits = 1;
 
166
 
 
167
    if( inScore > 0 ) {
 
168
        numDigits = (int)( floor( log10( inScore ) ) ) + 1;
 
169
        }
 
170
    
 
171
 
 
172
    // draw up to 9 digits
 
173
 
 
174
    if( numDigits > 9 ) {
 
175
        printf( "Error:  more than 9 digits passed to drawScorePip: %d\n",
 
176
                inScore );
 
177
        
 
178
        numDigits = 9;
 
179
        }
 
180
    
 
181
    int rowSep = 2;
 
182
    int colSep = 3;
 
183
    
 
184
    int centerOffsetsX[9] = { 
 
185
        6 + colSep,
 
186
        0,
 
187
        -( 6 + colSep ),
 
188
        6 + colSep,
 
189
        0,
 
190
        -( 6 + colSep ),
 
191
        6 + colSep,
 
192
        0,
 
193
        -( 6 + colSep ) 
 
194
        };
 
195
    
 
196
    int centerOffsetsY[9] = {
 
197
        10 + rowSep,
 
198
        10 + rowSep,
 
199
        10 + rowSep,
 
200
        0,
 
201
        0,
 
202
        0,
 
203
        -( 10 + rowSep ),
 
204
        -( 10 + rowSep ),
 
205
        -( 10 + rowSep ) 
 
206
        };
 
207
    
 
208
    // find center of mass for all digits together
 
209
 
 
210
    int xSum = 0;
 
211
    int ySum = 0;
 
212
    
 
213
    int i;
 
214
    
 
215
    for( i=0; i<numDigits; i++ ) {
 
216
        xSum += centerOffsetsX[ i ];
 
217
        ySum += centerOffsetsY[ i ];
 
218
        }
 
219
    
 
220
    int cX = xSum / numDigits;
 
221
    int cY = ySum / numDigits;
 
222
 
 
223
 
 
224
    if( numDigits > 3 ) {
 
225
        // center of mass doesn't look good for more than one row
 
226
 
 
227
        cX = 0;
 
228
        
 
229
        if( numDigits < 7 ) {
 
230
            cY = ( 10 + rowSep ) / 2;
 
231
            }
 
232
        else {
 
233
            cY = 0;
 
234
            }
 
235
        }
 
236
    
 
237
    
 
238
    
 
239
    for( i=0; i<numDigits; i++ ) {
 
240
        
 
241
        int number = ( inScore / ( (int)( pow( 10, i ) ) ) ) % 10;
 
242
        
 
243
 
 
244
        drawNumeral( number,
 
245
                     inCenterX - cX + centerOffsetsX[i], 
 
246
                     inCenterY - cY + centerOffsetsY[i],
 
247
                     inColor, inAlpha );
 
248
        }
 
249
 
 
250
    // commas
 
251
    if( numDigits > 3 ) {
 
252
        drawNumeral( 10,
 
253
                     inCenterX - cX + centerOffsetsX[3] + 3 + colSep,
 
254
                     inCenterY - cY + centerOffsetsY[3] + 1,
 
255
                     inColor, inAlpha );
 
256
        }
 
257
    if( numDigits > 6 ) {
 
258
        drawNumeral( 10,
 
259
                     inCenterX - cX + centerOffsetsX[6] + 3 + colSep,
 
260
                     inCenterY - cY + centerOffsetsY[6] + 1,
 
261
                     inColor, inAlpha );
 
262
        }
 
263
    
 
264
    }
 
265
 
 
266
 
 
267
 
 
268
 
 
269
void drawScore( int inScore,
 
270
                float inRightX, float inBottomY,
 
271
                Color *inColor, float inAlpha ) {
 
272
    
 
273
    
 
274
    int numDigits = 1;
 
275
 
 
276
    if( inScore > 0 ) {
 
277
        numDigits = (int)( floor( log10( inScore ) ) ) + 1;
 
278
        }
 
279
    
 
280
    
 
281
    int rowSep = 2;
 
282
    int colSep = 3;
 
283
        
 
284
    
 
285
    
 
286
    // next digit center
 
287
    int startCX = (int)inRightX - 3;
 
288
    int cX = startCX;
 
289
    int cY = (int)inBottomY - 5;
 
290
    
 
291
    int i;
 
292
    
 
293
    for( i=0; i<numDigits; i++ ) {
 
294
        
 
295
        int number = ( inScore / ( (int)( pow( 10, i ) ) ) ) % 10;
 
296
        
 
297
        if( i % 3 == 0 && i != 0 ) {
 
298
            // make room for comma
 
299
            if( i % 12 != 0 ) {
 
300
                // extra space around comma
 
301
                cX -= colSep - 1;
 
302
                }
 
303
            }
 
304
        
 
305
        drawNumeral( number,
 
306
                     cX, 
 
307
                     cY,
 
308
                     inColor, inAlpha );
 
309
 
 
310
        if( i % 3 == 0 && i != 0 ) {
 
311
            // comma after every 4th digit (between 3 and 4 )
 
312
                    
 
313
            drawNumeral( 10,
 
314
                         cX + 3 + colSep,
 
315
                         cY + 1,
 
316
                         inColor, inAlpha );
 
317
            }
 
318
        
 
319
 
 
320
        // compute next numeral center, twelve digits per line
 
321
        if( (i + 1) % 12 != 0 ) {
 
322
            cX -= 6 + colSep;
 
323
            }
 
324
        else {
 
325
            cX = startCX;
 
326
            cY -= 10 + rowSep;
 
327
            }
 
328
        }
 
329
    
 
330
    }
 
331
 
 
332
 
 
333
 
 
334
 
 
335
 
 
336
 
 
337
 
 
338
void drawScoreBig( int inScore,
 
339
                   float inRightX, float inBottomY,
 
340
                   Color *inColor, float inAlpha ) {
 
341
    
 
342
    
 
343
    int numDigits = 1;
 
344
 
 
345
    if( inScore > 0 ) {
 
346
        numDigits = (int)( floor( log10( inScore ) ) ) + 1;
 
347
        }
 
348
    
 
349
    
 
350
    int rowSep = 4;
 
351
    int colSep = 6;
 
352
        
 
353
    
 
354
    
 
355
    // next digit center
 
356
    int startCX = (int)inRightX - 6;
 
357
    int cX = startCX;
 
358
    int cY = (int)inBottomY - 10;
 
359
    
 
360
    int i;
 
361
    
 
362
    for( i=0; i<numDigits; i++ ) {
 
363
        
 
364
        int number = ( inScore / ( (int)( pow( 10, i ) ) ) ) % 10;
 
365
        
 
366
        if( i % 3 == 0 && i != 0 ) {
 
367
            // make room for comma
 
368
            if( i % 6 != 0 ) {
 
369
                // extra space around comma
 
370
                cX -= colSep - 2;
 
371
                }
 
372
            }
 
373
        
 
374
        drawNumeralBig( number,
 
375
                        cX, 
 
376
                        cY,
 
377
                        inColor, inAlpha );
 
378
 
 
379
        if( i % 3 == 0 && i != 0 ) {
 
380
            // comma after every 4th digit (between 3 and 4 )
 
381
                    
 
382
            drawNumeralBig( 10,
 
383
                            cX + 6 + colSep,
 
384
                            cY + 2,
 
385
                            inColor, inAlpha );
 
386
            }
 
387
        
 
388
 
 
389
        // compute next numeral center, six digits per line
 
390
        if( (i + 1) % 6 != 0 ) {
 
391
            cX -= 12 + colSep;
 
392
            }
 
393
        else {
 
394
            cX = startCX;
 
395
            cY -= 20 + rowSep;
 
396
            }
 
397
        }
 
398
    
 
399
    }
 
400
 
 
401
 
 
402
 
 
403
 
 
404
// internal 
 
405
void drawCounter( int inColSep, int inNumeralWidth,
 
406
                 void (*inNumeralDrawingFunction)( 
 
407
                     int, float, float,
 
408
                     Color *, float ), 
 
409
                  int inCount,
 
410
                  float inCenterX, float inCenterY,
 
411
                  Color *inColor, float inAlpha ) {
 
412
    
 
413
    int numDigits = 1;
 
414
 
 
415
    if( inCount > 0 ) {
 
416
        numDigits = (int)( floor( log10( inCount ) ) ) + 1;
 
417
        }
 
418
    
 
419
 
 
420
    // draw up to 2 digits
 
421
 
 
422
    if( numDigits > 2 ) {
 
423
        printf( "Error:  more than 2 digits passed to drawCounter: %d\n",
 
424
                inCount );
 
425
        
 
426
        numDigits = 2;
 
427
        }
 
428
    
 
429
    int numeralRadius = inNumeralWidth / 2;
 
430
    
 
431
    int centerOffsetsX[2] = { 
 
432
        numeralRadius,
 
433
        -( numeralRadius + inColSep ) };
 
434
        
 
435
    // find center of mass for all digits together
 
436
 
 
437
    int xSum = 0;
 
438
    
 
439
    int i;
 
440
    
 
441
    for( i=0; i<numDigits; i++ ) {
 
442
        xSum += centerOffsetsX[ i ];
 
443
        }
 
444
    
 
445
    int cX = xSum / numDigits;    
 
446
    
 
447
    
 
448
    for( i=0; i<numDigits; i++ ) {
 
449
        
 
450
        int number = ( inCount / ( (int)( pow( 10, i ) ) ) ) % 10;
 
451
        
 
452
 
 
453
        (*inNumeralDrawingFunction)( number,
 
454
                                     inCenterX - cX + centerOffsetsX[i], 
 
455
                                     inCenterY,
 
456
                                     inColor, inAlpha );
 
457
        }
 
458
    
 
459
    }
 
460
 
 
461
 
 
462
 
 
463
void drawCounter( int inCount,
 
464
                  float inCenterX, float inCenterY,
 
465
                  Color *inColor, float inAlpha ) {
 
466
    
 
467
    drawCounter( 3, 6, drawNumeral,
 
468
                 inCount,
 
469
                 inCenterX, inCenterY,
 
470
                 inColor, inAlpha );
 
471
    }
 
472
 
 
473
 
 
474
 
 
475
void drawCounterBig( int inCount,
 
476
                  float inCenterX, float inCenterY,
 
477
                  Color *inColor, float inAlpha ) {
 
478
    
 
479
    drawCounter( 6, 12, drawNumeralBig,
 
480
                 inCount,
 
481
                 inCenterX, inCenterY,
 
482
                 inColor, inAlpha );
 
483
    }
 
484
 
 
485
 
 
486
 
 
487
// internal
 
488
void drawString( int inColSep, int inLetterW,
 
489
                 void (*inLetterDrawingFunction)( 
 
490
                     char ,float, float,
 
491
                     Color *, float ), 
 
492
                 char *inString,
 
493
                 StringAlign inAlign,
 
494
                 float inX, float inY,
 
495
                 Color *inColor, float inAlpha ) {
 
496
    
 
497
    int numChars = strlen( inString );
 
498
    
 
499
 
 
500
    int colSep = inColSep;
 
501
    int letterW = inLetterW;
 
502
 
 
503
    int stringW = numChars * letterW + (numChars-1) * colSep;
 
504
    
 
505
 
 
506
    
 
507
    int startXC = (int)inX;
 
508
    
 
509
    switch( inAlign ) {
 
510
        case left:
 
511
            // no change
 
512
            break;
 
513
        case center:
 
514
            startXC -= stringW / 2;
 
515
            break;
 
516
        case right:
 
517
            startXC -= stringW;
 
518
            break;
 
519
        }
 
520
    
 
521
    startXC += letterW / 2;
 
522
    
 
523
 
 
524
 
 
525
    int currentXC = startXC;
 
526
    
 
527
 
 
528
    for( int i=0; i<numChars; i++ ) {
 
529
        
 
530
        char c = inString[i];
 
531
        
 
532
        // skip drawing anything for space
 
533
        if( c != ' ' ) {
 
534
            (*inLetterDrawingFunction)( c, currentXC, inY, inColor, inAlpha );
 
535
            }
 
536
        
 
537
 
 
538
        // advance
 
539
        currentXC += colSep + letterW;
 
540
        }
 
541
    }
 
542
 
 
543
 
 
544
 
 
545
void drawString( char *inString,
 
546
                 StringAlign inAlign,
 
547
                 float inX, float inY,
 
548
                 Color *inColor, float inAlpha ) {
 
549
    
 
550
    drawString( 3, 6,
 
551
                &drawLetter,
 
552
                inString,
 
553
                inAlign,
 
554
                inX, inY,
 
555
                inColor, inAlpha );
 
556
    }
 
557
 
 
558
 
 
559
void drawStringBig( char *inString,
 
560
                    StringAlign inAlign,
 
561
                    float inX, float inY,
 
562
                    Color *inColor, float inAlpha ) {
 
563
    
 
564
    drawString( 6, 12,
 
565
                &drawLetterBig,
 
566
                inString,
 
567
                inAlign,
 
568
                inX, inY,
 
569
                inColor, inAlpha );
 
570
    }