-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
1064 lines (921 loc) · 31.2 KB
/
game.cpp
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/************************************************************************************
* game.cpp
*
* descp : Frame-based logic/processing,init,shutdown,'big-picture' routines.
*
* auth : javery
*
* path : C:\Program Files\Microsoft Visual Studio\MyProjects\PrimaryCubes3D\game.h
*
* iDate : 09/17/06
* version:
*
* TO DO : [ 9/28/06 ] add helper routine to G_FindLinks using GL_GAME_DIRECTION_* macros *FIXED*
* [10/06/06 ] fix GL_FindAllLinks so that it works with one call.
************************************************************************************/
#include <allegro.h>
#include <memory.h>
#include "gfx_system.h"
#include "gfx_elements.h"
#include "cube.h"
#include "logic.h"
#include "game.h"
#include "common.h"
#include "dev_intro.h"
#include "company_intro.h"
#include "font.h"
#include "underwater_level.h"
#include "gui.h"
#include "audio.h"
extern volatile int globalTickCount;
extern BITMAP* pageOne; // primary graphics buffer
extern BITMAP* pageTwo; // used for dual mode display ( left )
extern BITMAP* pageThree; // used for dual mode display ( right )
extern Cube_t debugGameGrid[ GL_GAME_SIZE_X ][ GL_GAME_SIZE_Y ];
extern BITMAP* cubes[ CUBE_COUNT ];
extern BITMAP* waterLevel;
volatile int gameTickCount;
int stageSpeedFactor[] =
{
20,
15,
12,
10,
8,
6,
5,
4,
3,
2,
};
int stageMaxScore[] =
{
10000,
40000,
70000,
100000,
130000,
160000,
190000,
220000,
300000,
400000
};
/*
========================================================================
G_Init
Preform precalcs,libary init,pre-caching etc. Assumes video mode and
'allegro_init' have been called.
========================================================================
*/
c_boolean G_Init( pGame_t pGame )
{
// [ Allegro ]=============================================================
install_timer();
install_keyboard();
pGame->gameInputProfile.mouseButtons = install_mouse();
// [ timing routines ]=====================================================
LOCK_FUNCTION( GFX_SysTimerFunction );
LOCK_VARIABLE( globalTickCount );
install_int_ex( GFX_SYS_TimerFunction , BPS_TO_TIMER( G_GLOBAL_TICK_RATE ) );
LOCK_FUNCTION( G_GameTimer );
LOCK_VARIABLE( gameTickCounter );
install_int_ex( G_GameTimer , BPM_TO_TIMER( G_SECOND_TICK_RATE ) );
LOCK_FUNCTION( DIN_Animate );
LOCK_FUNCTION( CIN_RunIntro );
LOCK_FUNCTION( G_Delay );
// [ session ]==============================================================
G_StartSession( pGame , LVL_NORM );
memset( (void**)pGame->pGameCubes, 0 , sizeof(Cube_t)* pGame->currentSession.xDimen * pGame->currentSession.yDimen );
// Create off-screen buffer(s)
pageOne = create_bitmap( pGame->gfxConfig.xDimen , pGame->gfxConfig.yDimen );
pageTwo = create_bitmap( pGame->gfxConfig.xDimen / 2 , pGame->gfxConfig.yDimen );
pageThree = create_bitmap( pGame->gfxConfig.xDimen / 2 , pGame->gfxConfig.yDimen );
// [ resources/content ]================================================
//load game fonts
FT_LoadGameFont();
// load game cubes
G_LoadCubes();
// Generate color lookups.
GL_GenerateColorTable();
// generate first two cubes
GL_GenerateCube( &pGame->currentCube );
GL_GenerateCube( &pGame->nextCube );
GL_AddCube( &pGame->currentCube );
//=====================================================================
// run company intro
//CIN_LoadFrames();
//CIN_RunIntro();
//CIN_CleanUp();
// run developer intro
//DIN_LoadFrames();
//DIN_Animate( 1 , 1 );
//DIN_CleanUp();
// [ stages ]================================================================
LVL_UnderWater_LoadFrames();
LVL_UnderWater_InitFish(1);
// [ GUI ]===================================================================
GUI_ELE_LoadGameGUI();
// [ AUDIO ]=================================================================
AUD_InitFMOD( 44100 , 32 );
return c_true;
}
/*
=======================================================================
G_Shutdown
Garbage collection. Deallocate resources. Shutdown APIs. etc.
=======================================================================
*/
void G_ShutDown( pGame_t pGame )
{
destroy_bitmap( pageOne );
destroy_bitmap( pageTwo );
destroy_bitmap( pageThree );
remove_int( GFX_SYS_TimerFunction );
remove_int( G_GameTimer );
FT_DeleteGameFont();
AUD_ShutdownFMOD();
}
/*
=======================================================================
G_MovePlayer
Checks state of devices & reacts accordingly. Called after logic
routine to determine movement possiblities
=======================================================================
*/
void G_MovePlayer( pGame_t pGame )
// kill preveious cell before moving on...
{
pCube_t pCurrentCube = &pGame->currentCube;
GL_CheckDirections( pCurrentCube , pGame->pGameCubes );
if( key[ KEY_RIGHT ] )
{
if( pCurrentCube->active && pCurrentCube->directions[ GL_DIRECTION_RIGHT ] )
{
GL_DeleteCube( &pGame->pGameCubes[ pCurrentCube->gridPosition.xCol ][ pCurrentCube->gridPosition.yRow ] );
pCurrentCube->gridPosition.xCol++;
pGame->pGameCubes[ pCurrentCube->gridPosition.xCol ][ pCurrentCube->gridPosition.yRow ] = *pCurrentCube;
}
}
if( key[ KEY_LEFT ] )
{
if( pCurrentCube->active && pCurrentCube->directions[ GL_DIRECTION_LEFT ] )
{
GL_DeleteCube( &pGame->pGameCubes[ pCurrentCube->gridPosition.xCol ][ pCurrentCube->gridPosition.yRow ] );
pCurrentCube->gridPosition.xCol--;
pGame->pGameCubes[ pCurrentCube->gridPosition.xCol ][ pCurrentCube->gridPosition.yRow ] = *pCurrentCube;
}
}
if( key[ KEY_DOWN ] )
{
if( pCurrentCube->active && pCurrentCube->directions[ GL_DIRECTION_DOWN ] )
{
GL_DeleteCube( &pGame->pGameCubes[ pCurrentCube->gridPosition.xCol ][ pCurrentCube->gridPosition.yRow ] );
pCurrentCube->gridPosition.yRow++;
pGame->pGameCubes[ pCurrentCube->gridPosition.xCol ][ pCurrentCube->gridPosition.yRow ] = *pCurrentCube;
}
}
if( key[ KEY_SPACE ] )
{
CubeColor_t swap = pCurrentCube->frontFace;
pCurrentCube->frontFace = pCurrentCube->rearFace;
pCurrentCube->rearFace = swap;
BITMAP* spriteSwap = pCurrentCube->cubeSprite[ CUBE_FRONT ].data;
pCurrentCube->cubeSprite[ CUBE_FRONT ].data = pCurrentCube->cubeSprite[ CUBE_BACK ].data;
pCurrentCube->cubeSprite[ CUBE_BACK ].data = spriteSwap;
}
else
{
if( pCurrentCube->directions[ GL_DIRECTION_DOWN ] )
{
GL_DeleteCube( &pGame->pGameCubes[ pCurrentCube->gridPosition.xCol ][ pCurrentCube->gridPosition.yRow ] );
pCurrentCube->gridPosition.yRow++;
pGame->pGameCubes[ pCurrentCube->gridPosition.xCol ][ pCurrentCube->gridPosition.yRow ] = *pCurrentCube;
}
// ... maybe play a sound...
}
// update coordinates
int xCol = pCurrentCube->gridPosition.xCol , yRow = pCurrentCube->gridPosition.yMax;
float deltaX = pGame->currentSession.deltaX,
deltaY = pGame->currentSession.deltaY,
xPadding = pGame->currentSession.xPadding,
yPadding = pGame->currentSession.yPadding;
/*pGame->pGameCubes[ xCol ][ yRow ].position.xPosition = (pGame->pGameCubes[ xCol ][ yRow ].gridPosition.xCol * deltaX )+xPadding;
pGame->pGameCubes[ xCol ][ yRow ].position.yPosition = (pGame->pGameCubes[ xCol ][ yRow ].gridPosition.yRow * deltaY )+yPadding;
pGame->pGameCubes[ xCol ][ yRow ].cubeSprite[ CUBE_FRONT ].currentPosition.xPosition = pGame->pGameCubes[ xCol ][ yRow ].position.xPosition;
pGame->pGameCubes[ xCol ][ yRow ].cubeSprite[ CUBE_FRONT ].currentPosition.yPosition = pGame->pGameCubes[ xCol ][ yRow ].position.yPosition;
pGame->pGameCubes[ xCol ][ yRow ].cubeSprite[ CUBE_BACK ].currentPosition.xPosition = pGame->pGameCubes[ xCol ][ yRow ].position.xPosition;
pGame->pGameCubes[ xCol ][ yRow ].cubeSprite[ CUBE_BACK ].currentPosition.yPosition = pGame->pGameCubes[ xCol ][ yRow ].position.yPosition;*/
}
/*
========================================================================
G_GameTimer
Game interrupt routine. Called every second, used for in-game clock.
========================================================================
*/
void G_GameTimer( void )
{
gameTickCount++;
}
END_OF_FUNCTION( G_GameTimer );
/*
=========================================================================
G_GameStartSession
Loads in default values for a game session.
==========================================================================
*/
void G_StartSession( pGame_t pGame ,GameLevel_t level )
{
pGame->currentSession.currentGameLevel = level;
pGame->currentSession.currentStage = 0;
pGame->currentSession.currentScore = 0;
pGame->currentSession.highScore = 0;
pGame->currentSession.paused = c_false;
pGame->currentSession.timeToNextLevel = 100; // hard-coded...
pGame->currentSession.totalTime = 0;
pGame->currentSession.linkSize = 0;
pGame->currentSession.maxLink = 0;
switch( level )
{
case LVL_EASY:
{
pGame->currentSession.xDimen = GL_EASY_GAME_SIZE_X;
pGame->currentSession.yDimen = GL_EASY_GAME_SIZE_Y;
//pGame->pGameCubes = (pCube_t)pEasyGameCubes;
pGame->pCubes = (pCube_t)pGame->pEasyGameCubes;
// calculate cell padding
pGame->currentSession.xPadding = pGame->gfxConfig.xDimen * G_GRID_PADDING_X;
pGame->currentSession.yPadding = pGame->gfxConfig.yDimen * G_GRID_PADDING_Y;
// calculate space in-between each cell
pGame->currentSession.deltaX = ( (pGame->gfxConfig.xDimen-(pGame->currentSession.xPadding*2)) / (pGame->currentSession.xDimen) ) ;
pGame->currentSession.deltaY = ( (pGame->gfxConfig.yDimen-(pGame->currentSession.yPadding*2)) / (pGame->currentSession.yDimen) );
break;
}
case LVL_NORM:
{
pGame->currentSession.xDimen = GL_EASY_GAME_SIZE_X; // CHANGE!!!
pGame->currentSession.yDimen = GL_EASY_GAME_SIZE_Y;
// calculate cell padding
pGame->currentSession.xPadding = pGame->gfxConfig.xDimen * G_NORM_GRID_PADDING_X;
pGame->currentSession.yPadding = pGame->gfxConfig.yDimen * G_NORM_GRID_PADDING_Y;
// calculate space in-between each cell
pGame->currentSession.deltaX = ( ((pGame->gfxConfig.xDimen/2)-(pGame->currentSession.xPadding*2)) / (pGame->currentSession.xDimen) ) ;
pGame->currentSession.deltaY = ( ( pGame->gfxConfig.yDimen-(pGame->currentSession.yPadding*2)) / (pGame->currentSession.yDimen) );
break;
}
case LVL_HARD:
{
pGame->currentSession.xDimen = GL_HARD_GAME_SIZE_X;
pGame->currentSession.yDimen = GL_HARD_GAME_SIZE_Y;
// calculate cell padding
pGame->currentSession.xPadding = pGame->gfxConfig.xDimen * G_HARD_GRID_PADDING_X;
pGame->currentSession.yPadding = pGame->gfxConfig.yDimen * G_HARD_GRID_PADDING_Y;
// calculate space in-between each cell
pGame->currentSession.deltaX = ( ((pGame->gfxConfig.xDimen/2)-(pGame->currentSession.xPadding*2)) / (pGame->currentSession.xDimen) ) ;
pGame->currentSession.deltaY = ( (pGame->gfxConfig.yDimen-(pGame->currentSession.yPadding*2)) / (pGame->currentSession.yDimen) );
break;
}
}
}
/*
===========================================================================
G_FindLinks
Searches through game grid for linked cells. Flags appropiate cells for
score processing/deletion.
===========================================================================
*/
int G_FindLinks( pCube_t pCube , pGame_t pGame )
// Debug routine. Final version will have 3 seperate routines referenced via callback.
{
int multiplier=0,left=0,right=0,up=0,down=0,
row=pCube->gridPosition.yRow,
col=pCube->gridPosition.xCol,
i=0;
c_boolean leftValid=c_false,
rightValid=c_false,
upValid=c_false,
downValid=c_false,
linkDone=c_false;
// restrict searching by min/max dimensions of game grid.
if( !col )
{
left = col;
leftValid = ( pCube->frontFace == pGame->pGameCubes[ left ][ row ].frontFace ) ? c_true : c_false;
}
else
{
left = col - 1;
leftValid = ( pCube->frontFace == pGame->pGameCubes[ left ][ row ].frontFace ) ? c_true : c_false;
}
if( (col + 1) == pCube->gridPosition.xMax )
{
right = col;
rightValid = ( pCube->frontFace == pGame->pGameCubes[ right ][ row ].frontFace ) ? c_true : c_false;
}
else
{
right = col + 1;
rightValid = ( pCube->frontFace == pGame->pGameCubes[ right ][ row ].frontFace ) ? c_true : c_false;
}
if( !row )
{
up = row;
upValid = ( pCube->frontFace == pGame->pGameCubes[ col ][ up ].frontFace ) ? c_true : c_false;
}
else
{
up = row - 1;
upValid = ( pCube->frontFace == pGame->pGameCubes[ col ][ up ].frontFace ) ? c_true : c_false;
}
if( (row + 1) == pCube->gridPosition.yMax )
{
down = row;
downValid = ( pCube->frontFace == pGame->pGameCubes[ col ][ down ].frontFace ) ? c_true : c_false;
}
else
{
down = row + 1;
downValid = ( pCube->frontFace == pGame->pGameCubes[ col ][ down ].frontFace ) ? c_true : c_false;
}
if( !upValid && !downValid && !leftValid && !rightValid )
{
return multiplier;
}
if( leftValid ) // a like cube exsists to the left of the current cube.
{
G_LinearSearch( pCube , pGame , GL_DIRECTION_LEFT , CUBE_FRONT );
}
if( rightValid ) // a like cubes on in the cell to the right, so seach for more links.
{
G_LinearSearch( pCube , pGame , GL_DIRECTION_RIGHT , CUBE_FRONT );
}
if( upValid ) // going up...
{
G_LinearSearch( pCube , pGame , GL_DIRECTION_UP , CUBE_FRONT );
}
if( downValid ) // going down...
{
G_LinearSearch( pCube , pGame , GL_DIRECTION_DOWN , CUBE_FRONT );
}
// Hmmm... for some reason this doesn't pick up all the links on the first pass, so multiple passes are required...
/*G_FindAllLinks( pCube->frontFace , pGame , CUBE_FRONT );
G_FindAllLinks( pCube->frontFace , pGame , CUBE_FRONT );
G_FindAllLinks( pCube->frontFace , pGame , CUBE_FRONT );
G_FindAllLinks( pCube->frontFace , pGame , CUBE_FRONT );
G_FindAllLinks( pCube->frontFace , pGame , CUBE_FRONT );*/
// stuff to handle link values
multiplier = G_FindAllLinks( pCube->frontFace , pGame , CUBE_FRONT );
// add the number of linked cubes on the rear side
multiplier += G_FindRearLinks( pCube , pGame , leftValid , rightValid , downValid , upValid );
// update session variables
pGame->currentSession.linkSize = multiplier;
pGame->currentSession.maxLink = ( multiplier > pGame->currentSession.maxLink ) ? multiplier : pGame->currentSession.maxLink;
// calculate/update game score.
pGame->currentSession.currentScore += multiplier * G_PRIMARY_POINTS;
textprintf_ex( pageOne , font , 5 , 420 , G_COLOR_WHITE , -1 , "col=%d i=%d" , col , i );
textprintf_ex( screen , font , 150 ,420 , G_COLOR_RED , -1 , "%d cubes linked to active cube." , multiplier );
return multiplier;
}
/*
============================================================================
G_FindRearLinks
Same as G_FindLinks, but limited to rear game grid. Used 4 movement restricting
booleans passed from G_FindLinks
============================================================================
*/
int G_FindRearLinks( pCube_t pCube , pGame_t pGame , c_boolean left , c_boolean right , c_boolean down , c_boolean up )
{
int multiplier = 0;
if( !left && !right && !up && !down )
{
return multiplier;
}
if( left )
{
G_LinearSearch( pCube , pGame , GL_DIRECTION_LEFT , CUBE_BACK );
}
if( right )
{
G_LinearSearch( pCube , pGame , GL_DIRECTION_RIGHT , CUBE_BACK );
}
if( up )
{
G_LinearSearch( pCube , pGame , GL_DIRECTION_UP , CUBE_BACK );
}
if( down )
{
G_LinearSearch( pCube , pGame , GL_DIRECTION_DOWN , CUBE_BACK );
}
// this really needs to go...
/*G_FindAllLinks( pCube->rearFace , pGame , CUBE_BACK );
G_FindAllLinks( pCube->rearFace , pGame , CUBE_BACK );
G_FindAllLinks( pCube->rearFace , pGame , CUBE_BACK );
G_FindAllLinks( pCube->rearFace , pGame , CUBE_BACK );*/
multiplier = G_FindAllLinks( pCube->rearFace , pGame , CUBE_BACK );
return multiplier;
}
/*
============================================================================
G_RunLogic
Main logic routine. Determines state of current cube, acts accordingly.
============================================================================
*/
int G_RunLogic( pGame_t pGame )
{
int link=0;
// move player
G_MovePlayer( pGame );
G_SyncSprites( pGame );
// once cube stops moving, run scoring routines
if( pGame->currentCube.active == c_false )
{
G_ClearLinks( pGame ); // clear flags from previous pass
link = G_FindLinks( &pGame->currentCube , pGame ); // search and destory!
if( pGame->currentSession.linkSize > G_SMALLEST_LINK )
{
G_RemoveLinks( pGame ); // remove cubes
G_ResetCubes( pGame ); // push cubes into new position if necessary.
}
pGame->currentCube = pGame->nextCube;
GL_GenerateCube( &pGame->nextCube );
GL_AddCube( &pGame->currentCube );
}
// check to see if session is over
G_TestIfSessionOver( pGame );
if( pGame->currentSession.sessionOver )
{
// Do more stuff here later...
G_ResetSession( pGame );
}
// advance to next stage,if applicable
if( pGame->currentSession.currentScore > stageMaxScore[ pGame->currentSession.currentStage ] )
{
pGame->currentSession.currentStage++;
}
return link;
}
/*
============================================================================
G_RunGraphics
Draws a frame as dictated by the current level/stage etc.
============================================================================
*/
void G_RunGraphics( pGame_t pGame )
{
// ripped from main
clear_to_color( pageOne , G_COLOR_BLACK );
clear_to_color( pageTwo , G_COLOR_WHITE );
clear_to_color( pageThree , G_COLOR_BLACK );
G_DrawBackGround( pGame );
LVL_UnderWater_DrawFish();
LVL_UnderWater_CalcNewWater();
LVL_UnderWater_Rain();
LVL_UnderWater_PaintWater(0.0f );
LVL_UnderWater_DrawFish();
G_DrawCubes( pGame );
GUI_ELE_DrawGameGUI( pGame->currentSession.currentGameLevel );
blit( pageTwo , pageOne , 0 , 0 , 0 , 0 , pGame->gfxConfig.xDimen/2 , pGame->gfxConfig.yDimen );
blit( pageThree , pageOne , 0 , 0 , 320 , 0 , pGame->gfxConfig.xDimen/2 , pGame->gfxConfig.yDimen );
blit( pageOne , screen , 0 , 0 , 0 , 0 , pGame->gfxConfig.xDimen , pGame->gfxConfig.yDimen );
}
/*
============================================================================
G_DrawCubes
Loops through game grid and draws all active cubes.
============================================================================
*/
void G_DrawCubes( pGame_t pGame )
{
for( int yRow=0; yRow<pGame->currentSession.yDimen;yRow++ )
{
for( int xCol=0; xCol<pGame->currentSession.xDimen;xCol++ )
{
if( pGame->pGameCubes[ xCol ][ yRow ].alive )
{
//GFX_ELE_DrawCube( pGame , &pGame->pGameCubes[ xCol ][ yRow ] );
GFX_ELE_DrawFullCube( pGame , &pGame->pGameCubes[ xCol ][ yRow ] );
GFX_ELE_DrawCubeSprite( pGame , &pGame->pGameCubes[ xCol ][ yRow ] );
}
else
{
}
}
}
}
/*
============================================================================
G_ClearLinks
Loops through and clears all flagged cells.
============================================================================
*/
void G_ClearLinks( pGame_t pGame )
{
for( int y=0;y<pGame->currentSession.yDimen;y++ )
{
for( int x=0;x<pGame->currentSession.xDimen;x++ )
{
if( pGame->pGameCubes[ x ][ y ].frontFlagged )
{
pGame->pGameCubes[ x ][ y ].frontFlagged = c_false;
}
if( pGame->pGameCubes[ x ][ y ].rearFlagged )
{
pGame->pGameCubes[ x ][ y ].rearFlagged = c_false;
}
}
}
}
/*
===========================================================================
G_LinearSearch
Searches in a linear, specified direction from the passed cube, and returns
the number of like-colored faces.
===========================================================================
*/
static int G_LinearSearch( pCube_t pCube , pGame_t pGame , int direction , int side )
{
int multiplier=0;
int col = pCube->gridPosition.xCol,
row = pCube->gridPosition.yRow;
int x=0,y=0;
c_boolean linkDone = c_false;
switch( direction )
{
case GL_DIRECTION_LEFT:
{
x = -1;
y = 0;
break;
}
case GL_DIRECTION_RIGHT:
{
x = 1;
y = 0;
break;
}
case GL_DIRECTION_UP:
{
x = 0;
y = -1;
break;
}
case GL_DIRECTION_DOWN:
{
x = 0;
y = 1;
break;
}
}
switch( side)
{
case CUBE_FRONT:
{
while( !linkDone )
{
if( pCube->frontFace == pGame->pGameCubes[ col + x ][ row + y ].frontFace && pGame->pGameCubes[ col ][ row ].alive )
{
pGame->pGameCubes[ col + x ][ row + y ].frontFlagged = c_true;
multiplier++;
}
else
{
linkDone = c_true;
}
switch( direction )
{
case GL_DIRECTION_LEFT:
{
x--;
break;
}
case GL_DIRECTION_RIGHT:
{
x++;
break;
}
case GL_DIRECTION_UP:
{
y--;
break;
}
case GL_DIRECTION_DOWN:
{
y++;
break;
}
}
}
}
case CUBE_BACK:
{
while( !linkDone )
{
if( pCube->rearFace == pGame->pGameCubes[ col + x ][ row + y ].rearFace && pGame->pGameCubes[ col ][ row ].alive )
{
pGame->pGameCubes[ col + x ][ row + y ].rearFlagged = c_true;
multiplier++;
}
else
{
linkDone = c_true;
}
switch( direction )
{
case GL_DIRECTION_LEFT:
{
x--;
break;
}
case GL_DIRECTION_RIGHT:
{
x++;
break;
}
case GL_DIRECTION_UP:
{
y--;
break;
}
case GL_DIRECTION_DOWN:
{
y++;
break;
}
}
}
}
}
return multiplier;
}
/*
=================================================================================
G_FindAllLinks
Searches through game grid for faces of the passed color. Flags faces which are
connected to current link.
=================================================================================
*/
int G_FindAllLinks( CubeColor_t color , pGame_t pGame , int side )
{
int multiplier=0;
int xCol=0,yRow=0;
c_boolean flagged = c_false;
// loop through and find all links.
for( yRow = 0;yRow < pGame->currentSession.yDimen;yRow++ )
{
for( xCol = 0; xCol < pGame->currentSession.xDimen;xCol++ )
{
flagged = ( side == CUBE_FRONT ) ? ( pGame->pGameCubes[ xCol ][ yRow ].frontFlagged ) : ( pGame->pGameCubes[ xCol ][ yRow ].rearFlagged );
if( flagged )
{
G_LinearSearch( &pGame->pGameCubes[ xCol ][ yRow ] , pGame , GL_DIRECTION_DOWN , side );
G_LinearSearch( &pGame->pGameCubes[ xCol ][ yRow ] , pGame , GL_DIRECTION_UP , side );
G_LinearSearch( &pGame->pGameCubes[ xCol ][ yRow ] , pGame , GL_DIRECTION_LEFT , side );
G_LinearSearch( &pGame->pGameCubes[ xCol ][ yRow ] , pGame , GL_DIRECTION_RIGHT , side );
}
}
}
// calculate and return number of linked cubes after completely finished searching.
for( yRow=0;yRow<pGame->currentSession.yDimen;yRow++ )
{
for( xCol=0;xCol<pGame->currentSession.xDimen;xCol++ )
{
flagged = ( side == CUBE_FRONT ) ? ( pGame->pGameCubes[ xCol ][ yRow ].frontFlagged ) : ( pGame->pGameCubes[ xCol ][ yRow ].rearFlagged );
if( flagged )
{
multiplier++;
}
}
}
if( multiplier < G_SMALLEST_LINK )
{
G_ClearLinks( pGame );
}
return multiplier;
}
/*
====================================================================================
G_TestIfSessionOver
Evaluates movement possiblities/position of current cube, and determines if the current
session is able to be played further.
====================================================================================
*/
c_boolean G_TestIfSessionOver( pGame_t pGame )
{
if( !pGame->currentCube.gridPosition.yRow && (pGame->currentCube.gridPosition.xCol == pGame->currentSession.xDimen/2)) // cube is top row
{
if( !pGame->currentCube.directions[ GL_DIRECTION_DOWN ] || pGame->pGameCubes[ pGame->currentCube.gridPosition.xCol ][ pGame->currentCube.gridPosition.yRow + 1 ].alive ) // cube can't go down, so session over.
{
pGame->currentSession.sessionOver = c_true;
return c_true;
}
}
return c_false;
}
/*
====================================================================================
G_ResetSession
Resets the current session.
====================================================================================
*/
void G_ResetSession( pGame_t pGame )
{
memset( (void**)pGame->pGameCubes, 0 , sizeof(Cube_t)* pGame->currentSession.xDimen * pGame->currentSession.yDimen );
pGame->currentSession.currentScore = 1;
pGame->currentSession.currentScore = 0;
pGame->currentSession.linkSize = 0;
pGame->currentSession.maxLink = 0;
pGame->currentSession.paused = c_false;
pGame->currentSession.currentStage = 0;
// generate first two cubes
GL_GenerateCube( &pGame->currentCube );
GL_GenerateCube( &pGame->nextCube );
GL_AddCube( &pGame->currentCube );
pGame->currentSession.sessionOver = c_false;
}
/*
======================================================================================
G_RemoveLinks
Searches through game grid, removes all flagged faces.
=======================================================================================
*/
void G_RemoveLinks( pGame_t pGame )
{
for( int yRow = 0;yRow < pGame->currentSession.yDimen;yRow++ )
{
for( int xCol = 0;xCol < pGame->currentSession.xDimen;xCol++ )
{
// evaluate the front side of the cube
if( pGame->pGameCubes[ xCol ][ yRow ].frontFlagged )
{
if( pGame->pGameCubes[ xCol ][ yRow ].rearFlagged )
{
// if both sides are part of a link, delete
GL_DeleteCube( &pGame->pGameCubes[ xCol ][ yRow ] );
continue;
}
if( (pGame->pGameCubes[ xCol ][ yRow ].frontFace == pGame->pGameCubes[ xCol ][ yRow ].rearFace) && pGame->pGameCubes[ xCol ][ yRow ].frontFace == ALT_STONE )
{
// if both sides have been erased ( seperately or otherwise ), then delete it.
GL_DeleteCube( &pGame->pGameCubes[ xCol ][ yRow ] );
continue;
}
else
{
pGame->pGameCubes[ xCol ][ yRow ].frontFace = ALT_STONE;
pGame->pGameCubes[ xCol ][ yRow ].cubeSprite[ CUBE_FRONT ].data = cubes[ G_CUBE_STONE ];
}
}
// then the back...
else if( pGame->pGameCubes[ xCol ][ yRow ].rearFlagged )
{
if( pGame->pGameCubes[ xCol ][ yRow ].frontFlagged )
{
// delete if both sides have been flagged for deletion.
GL_DeleteCube( &pGame->pGameCubes[ xCol ][ yRow ] );
continue;
}
if( (pGame->pGameCubes[ xCol ][ yRow ].rearFace == pGame->pGameCubes[ xCol ][ yRow ].rearFace) && pGame->pGameCubes[ xCol ][ yRow ].rearFace == ALT_STONE )
{
// delete is both sides have been turned to stone.
GL_DeleteCube( &pGame->pGameCubes[ xCol ][ yRow ] );
continue;
}
else
{
pGame->pGameCubes[ xCol ][ yRow ].rearFace = ALT_STONE;
pGame->pGameCubes[ xCol ][ yRow ].cubeSprite[ CUBE_BACK ].data = cubes[ G_CUBE_STONE ];
}
}
// remove stones if both side are stoned...
if( (pGame->pGameCubes[ xCol ][ yRow ].frontFace == pGame->pGameCubes[ xCol ][ yRow ].rearFace ) && pGame->pGameCubes[ xCol ][ yRow ].frontFace == ALT_STONE )
{
GL_DeleteCube( &pGame->pGameCubes[ xCol ][ yRow ] );
}
}
}
}
/*
====================================================================================
G_ResetCubes
After links have been processed, called to 'push' remaining cubes down into thier
new position. Returns c_true to recommend that game grid be recalculated.
====================================================================================
*/
static c_boolean G_ResetCubes( pGame_t pGame )
{
int xDimen = pGame->currentSession.xDimen,
yDimen = pGame->currentSession.yDimen;
c_boolean recalculate = c_false;
for( int xCol = 0;xCol < xDimen;xCol++ )
{
for( int yRow = yDimen-2;yRow > 0;yRow-- )
// start at the bottom and work up. ( excluding very bottom row )
{
if( pGame->pGameCubes[ xCol ][ yRow ].alive )
{
if( pGame->pGameCubes[ xCol ][ yRow + 1 ].alive )
// cube is supported.
{
continue;
}
else if( !pGame->pGameCubes[ xCol ][ yRow + 1 ].alive )
// cube is floating... determine how many empty cells are beneath it.
{
int push = 1;
while( !pGame->pGameCubes[ xCol ][ yRow + push ].alive && yRow + push < yDimen - 1 )
// search for either another cube, or the bottom of the grid ( whichever comes first )
{
push++;
}
if( pGame->pGameCubes[ xCol ][ yRow + push ].alive )
// only one cube per cell!!!!
{
pGame->pGameCubes[ xCol ][ yRow ].gridPosition.yRow += push-1; // give it a little push
pGame->pGameCubes[ xCol ][ yRow + push-1 ] = pGame->pGameCubes[ xCol ][ yRow ];
}
else
{
pGame->pGameCubes[ xCol ][ yRow ].gridPosition.yRow += push; // give it a little push
pGame->pGameCubes[ xCol ][ yRow + push ] = pGame->pGameCubes[ xCol ][ yRow ];
}
pGame->pGameCubes[ xCol ][ yRow ].alive = c_false;
recalculate = c_true;
}
}
}
}
return recalculate;
}
/*
=====================================================================
G_LoadCubes
Loads cube bitmaps
=====================================================================
*/
static void G_LoadCubes( void )
{
cubes[ G_CUBE_GREEN ] = load_bitmap( "content/gfx/cubes_0000_green.bmp" , NULL );
cubes[ G_CUBE_BLUE ] = load_bitmap( "content/gfx/cubes_0001_blue.bmp" , NULL );
cubes[ G_CUBE_RED ] = load_bitmap( "content/gfx/cubes_0002_red.bmp" , NULL );
cubes[ G_CUBE_YELLOW ] = load_bitmap( "content/gfx/cubes_0003_yellow.bmp" , NULL );
cubes[ G_CUBE_ORANGE ] = load_bitmap( "content/gfx/cubes_0004_orange.bmp" , NULL );
cubes[ G_CUBE_PURPLE ] = load_bitmap( "content/gfx/cubes_0005_purple.bmp" , NULL );
cubes[ G_CUBE_STONE ] = load_bitmap( "content/gfx/cubes_0006_stone.bmp" , NULL );
}
/*
====================================================================