-
Notifications
You must be signed in to change notification settings - Fork 0
/
GLgui.h
825 lines (677 loc) · 15.8 KB
/
GLgui.h
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
/*
Tristan Craddick and Arsh Chauhan
Core Gui Header
02/19/16
Last Updated: 03/10/16
For CS 372 Battleship Game
*/
#ifndef GLgui_h_included
#define GLgui_h_included
#if defined(_WIN32) || defined (__WIN32__) || defined(__WINDOWS__)
# include <Windows.h>
#endif
#include <cstdlib>
using std::exit;
#ifndef __APPLE__
# include <GL/glut.h>
#else
# include <GLUT/glut.h>
#endif
//For random double generator
#include <random>
#include <math.h>
// Other includes
#include "lib381/bitmapprinter.h"
#include "Board.h"
#include "gui_Board.h"
#include "gui_Globals.h"
#include "gui_Buttons.h"
// For class BitmapPrinter
#include <sstream>
using std::ostringstream;
#include <iostream>
using std::cerr;
using std::endl;
#include <cstdlib>
using std::exit;
#include <iomanip>
using std::fixed;
using std::setprecision;
#include <vector>
using std::vector;
#include <tuple>
using std::tuple;
//****************Display, Idle, Reshape Functions*****************
/*myDisplay
The glut display function
Pre: None
Post: draws the information text for the player, the player's board
on the right, and if both players still needs to place ships,
then ship buttons in the bottom left for which ship to place. If
the ships have all been placed, then displays the player's board
in the bottom left and their targeting board on the right.
*/
void myDisplay()
{
glClearColor(0.7f, 0.7f, 0.7f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
BitmapPrinter text(0.0, 0.0, 0.1);
BitmapPrinter player(0.0, 0.0, 0.1);
BitmapPrinter button(0.0, 0.0, 0.1);
glLoadIdentity();
glColor3d(0.4, 0.4, 0.4); //color for the cell borders
if (game_start == false)
{
//Large grid on the right
if (p1turn == true)
{
glPushMatrix();
glTranslated(l_gridstart_x, l_gridstart_y, 0.0);
glScaled(0.8, 0.8, 0.0);
drawLargeGrid(player1Home);
glPopMatrix();
}
else
{
glPushMatrix();
glTranslated(l_gridstart_x, l_gridstart_y, 0.0);
glScaled(0.8, 0.8, 0.0);
drawLargeGrid(player2Home);
glPopMatrix();
}
//ship buttons
glPushMatrix();
glTranslated(-1.0, -0.15, 0.0);
for (int i = 0; i < 5; i++)
{
glTranslated(0.0, -0.125, 0.0);
glColor3d(boxcol[i][0], boxcol[i][1], boxcol[i][2]);
if (box_used[i] == false)
{
boxButton();
}
}
glPopMatrix();
//adds words to the box buttons
glPushMatrix();
boxWords();
glPopMatrix();
}
else //game_start == true, so secondary board is displayed.
{
//Large grid on the right
if (p1turn == true)
{
glPushMatrix();
glTranslated(l_gridstart_x, l_gridstart_y, 0.0);
glScaled(0.8, 0.8, 0.0);
drawLargeGrid(player1Target);
glPopMatrix();
}
else
{
glPushMatrix();
glTranslated(l_gridstart_x, l_gridstart_y, 0.0);
glScaled(0.8, 0.8, 0.0);
drawLargeGrid(player2Target);
glPopMatrix();
}
if (p1turn == true)
{
glPushMatrix();
glTranslated(s_gridstart_x, s_gridstart_y, 0.0);
glScaled(0.8, 0.8, 0.0);
drawSmallBoard(player1Home);
glPopMatrix();
}
else
{
glPushMatrix();
glTranslated(s_gridstart_x, s_gridstart_y, 0.0);
glScaled(0.8, 0.8, 0.0);
drawSmallBoard(player2Home);
glPopMatrix();
}
}
//'Finished' Button
//all ships have been placed for a player or the player has fired
if (ships_placed == 5 || has_fired == true)
{
glPushMatrix();
glColor3d(finishcol[0], finishcol[1], finishcol[2]);
glTranslated(finish_x, finish_y, 0.0);
glScaled(0.15, 0.15, 0.0);
drawCirc();
glColor3d(0., 0., 0.); // Black text
glTranslated(finish_x + 0.25, finish_y - 0.35, 0.0); // x + 0.45, y - 0.1
glScaled(0.75, 0.75, 0.0); //used for 800x600 aspect ratio
button.print("Finished!");
glPopMatrix();
}
//Instruction Text
glPushMatrix();
glColor3d(0., 0., 0.); // Black text
glTranslated(-1.25, 0.95, 0.0); //modify to change text position
glScaled(0.5, 0.45, 0.0); //used for 800x600 aspect ratio.
if (game_start == false)
{
text.print("Welcome to Battleship");
text.print("Please use the mouse to place ships");
text.print("on the board by clicking a grid square,");
text.print("then choosing a direction by clicking");
text.print("the next square over. Click the");
text.print("'finished' button when you are done to ");
text.print("pass it to the next player.");
text.print("");
switch (curr_ship)
{
case 0:
text.print("Ship Selected: Airship Carrier");
break;
case 1:
text.print("Ship Selected: Battleship");
break;
case 2:
text.print("Ship Selected: Submarine");
break;
case 3:
text.print("Ship Selected: Destroyer");
break;
case 4:
text.print("Ship Selected: Patrol Boat");
break;
}
}
else
{
text.print("Click on a grid cell to fire a shot.");
text.print("The first to sink all of the opponent's");
text.print("ships is the winner!");
text.print("");
text.print("Ship Cells Remaining:");
if (p1turn == true)
{
text.print("Aircraft Carrier: " + std::to_string(player1Home.ship_cells[0]));
text.print("Battleship: " + std::to_string(player1Home.ship_cells[1]));
text.print("Submarine: " + std::to_string(player1Home.ship_cells[2]));
text.print("Destroyer: " + std::to_string(player1Home.ship_cells[3]));
text.print("Patrol Boat: " + std::to_string(player1Home.ship_cells[4]));
}
else
{
text.print("Aircraft Carrier: " + std::to_string(player2Home.ship_cells[0]));
text.print("Battleship: " + std::to_string(player2Home.ship_cells[1]));
text.print("Submarine: " + std::to_string(player2Home.ship_cells[2]));
text.print("Destroyer: " + std::to_string(player2Home.ship_cells[3]));
text.print("Patrol Boat: " + std::to_string(player2Home.ship_cells[4]));
}
}
glPopMatrix(); // Restore prev projection
//Text in upper right showing whose turn it is.
glPushMatrix();
glColor3d(0., 0., 0.); // Black text
glTranslated(0.5, 0.875, 0.0); //modify to change text position
glScaled(0.5, 0.45, 0.0); //used for 800x600 aspect ratio.
if (p1turn == true)
{
player.print("It is player 1's turn");
}
else
{
player.print("It is player 2's turn");
}
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glutSwapBuffers();
}
//myIdle
//Basic glut idle function
/*
Pre: None
Post: A consistently updating function used for any variables
that should be constantly checked or updated.
*/
void myIdle()
{
// Print OpenGL errors, if there are any (for debugging)
static int error_count = 0;
if (GLenum err = glGetError())
{
++error_count;
cerr << "OpenGL ERROR " << error_count << ": "
<< gluErrorString(err) << endl;
}
// Compute elapsed time since last movement
double currtime = glutGet(GLUT_ELAPSED_TIME) / 1000.;
double elapsedtime = currtime - savetime;
savetime = currtime;
if (elapsedtime > 0.1)
elapsedtime = 0.1;
//Both players have placed their ships, and the actual targeting game begins.
if (turn_count >= 2)
{
game_start = true;
}
}
// myReshape
// The GLUT reshape function
/*
Pre: None
Post: Appropriately scales the drawn screen based upon the size
of the window whenever it is adjusted. Currently set so that
window will always be readjusted to 800x600 ratio.
*/
void myReshape(int w, int h)
{
// Set viewport & save window dimensions in globals
glViewport(0, 0, startwinwd, startwinht);
winw = startwinwd;
winh = startwinht;
float aspect = startwinwd / startwinht;
// Set up projection
// Save max/min x/y coords in globals
// Projection is orthographic. Aspect ratio is correct,
// and region -1..1 in x, y always just fits in viewport.
if (w > h)
{
cam_xmin = -double(w) / h;
cam_xmax = double(w) / h;
cam_ymin = -1.;
cam_ymax = 1.;
}
else
{
cam_xmin = -1.;
cam_xmax = 1.;
cam_ymin = -double(h) / w;
cam_ymax = double(h) / w;
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(cam_xmin, cam_xmax, cam_ymin, cam_ymax);
glMatrixMode(GL_MODELVIEW); // Always go back to model/view mode
//if the window is resized, resets it back to the following amount.
if (w != startwinwd || h != startwinht)
{
glutReshapeWindow(800, 600);
}
}
//-------------Mouse and Keyboard Functions-------------
// myKeyboard
// The GLUT keyboard function
/*
Pre: None
Post: Detects keypresses and preforms operations based upon
which key is pressed.
*/
void myKeyboard(unsigned char key, int x, int y)
{
switch (key)
{
case ESCKEY: // Escape: quit
exit(0);
break;
case ' ':
if (game_pause == true)
{
game_pause = false;
glutDisplayFunc(myDisplay);
}
if (ships_placed == 5 || has_fired == true)
{
if (game_start == true)
{
winCheck();
}
else
{
game_pause = true;
glutDisplayFunc(pauseScreen);
}
//winCheck(); //use to test winning redisplay
for (int i = 0; i < 5; i++)
{
box_used[i] = false;
}
if (p1turn == true)
{
p1turn = false;
p2turn = true;
}
else
{
p1turn = true;
p2turn = false;
}
turn_count++;
ships_placed = 0;
has_fired = false;
}
break;
}
glutPostRedisplay();
}
// saveMouse
/*
Pre: None
Post: Given mouse pos in GLUT format; computes mouse pos in cam coords,
saves it in globals cam_mousex, cam_mousey.
Uses globals winw, winh, cam_xmin, cam_xmax, cam_ymin, cam_ymax.
*/
void saveMouse(int x, int y)
{
double t; // Intermediate value of lirping
t = double(x) / winw;
cam_mousex = cam_xmin + t * (cam_xmax - cam_xmin);
t = double(y) / winh;
cam_mousey = cam_ymax + t * (cam_ymin - cam_ymax);
}
// myMouse
// The GLUT mouse function
/*
Pre: None
Post: Updates variables when the mouse buttons are clicked.
*/
void myMouse(int button, int state, int x, int y)
{
// Save old mouse pos, for relative mouse-movement computation
double old_cam_mousex = cam_mousex;
double old_cam_mousey = cam_mousey;
// Find mouse pos in cam coords (saved in cam_mousex, cam_mousey)
saveMouse(x, y);
BitmapPrinter p(0.0, 0.0, 0.1);
if (button == GLUT_LEFT_BUTTON)
{
leftmousedown = (state == GLUT_DOWN);
if (ships_placed == 5 || has_fired == true)
{
finishClick();
}
if (game_start == false)
{
if (curr_ship != -1)
{
if (p1turn == true)
{
boardPlace(player1Home);
}
else
{
boardPlace(player2Home);
}
}
boxButtonClick();
}
else //game has started, and targeting begins.
{
if (has_fired == false)
{
if (p1turn == true)
{
boardFire(player1Target, player2Home);
sunkCheck(player1Target, player2Home);
}
else
{
boardFire(player2Target, player1Home);
sunkCheck(player2Target, player1Home);
}
}
}
}
if (button == GLUT_RIGHT_BUTTON)
{
rightmousedown = (state == GLUT_DOWN);
if (isClicking)
{
isClicking = false;
if (p1turn)
{
player1Home.board_[outeri][outerj].removeHead();
}
else
{
player2Home.board_[outeri][outerj].removeHead();
}
}
}
glutPostRedisplay();
}
// myMotion
// The GLUT motion function
/*
Pre: None
Post: Updates the mouse position when the mouse is moved.
*/
void myMotion(int x, int y)
{
// Save old mouse pos, for relative mouse-movement computation
double old_cam_mousex = cam_mousex;
double old_cam_mousey = cam_mousey;
// Find mouse pos in cam coords (saved in cam_mousex, cam_mousey)
saveMouse(x, y);
glutPostRedisplay();
}
// myPassiveMotion
// The GLUT passiveMotion function
/*
Pre: None
Post: Updates variables, generally for the button detection
when the mouse moves.
*/
void myPassiveMotion(int x, int y)
{
// Save old mouse pos, for relative mouse-movement computation
double old_cam_mousex = cam_mousex;
double old_cam_mousey = cam_mousey;
saveMouse(x, y);
float xdist = pow(cam_mousex - finish_x, 2);
float ydist = pow(cam_mousey - finish_y, 2);
if (sqrt(xdist + ydist) < 0.15 && !leftmousedown)
{
finishcol[0] = finish_hover[0];
finishcol[1] = finish_hover[1];
finishcol[2] = finish_hover[2];
}
else
{
finishcol[0] = finish_base[0];
finishcol[1] = finish_base[1];
finishcol[2] = finish_base[2];
}
if (game_start == false)
{
if (p1turn == true)
{
boardHover(player1Home);
}
else
{
boardHover(player2Home);
}
}
else
{
if (p1turn == true)
{
boardHover(player1Target);
}
else
{
boardHover(player2Target);
}
}
boxButtonHover();
glutPostRedisplay();
}
//----------------Initialization and Main-----------------
// init
// Initialize GL states & global data
// Called by main after window creation
/*
Pre: None
Post: Function called at the beginning of the program
that properly sets all uninitialized variables.
*/
void init()
{
// Objects
savetime = glutGet(GLUT_ELAPSED_TIME) / 1000.;
gui_Init = true;
//mouse
cam_mousex = 0.0;
cam_mousey = 0.0;
//sets colors for the array of box buttons and sets their 'used'
//variable to false.
for (int i = 0; i < 5; i++)
{
boxcol[i][0] = 0.3;
boxcol[i][1] = 0.5;
boxcol[i][2] = 0.7;
box_used[i] = false;
}
//sets the sizes of the respective ships.
boxcol[0][3] = 5; //Aircraft Carrier
boxcol[1][3] = 4; //Battleship
boxcol[2][3] = 3; //Destroyer
boxcol[3][3] = 3; //Submarine
boxcol[4][3] = 2; //Patrol Boat
game_start = false;
turn_count = 0;
ships_placed = 0;
has_fired = false;
// OpenGL Stuff
glLineWidth(5.0);
showdisplay = true;
}
//pauseScreen
/*
Pre: None
Post: A seperately drawn screen that displays the pause
screen with a message so that players can switch
between boards without risk of cheating.
*/
void pauseScreen()
{
glClearColor(0.7f, 0.7f, 0.7f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
BitmapPrinter player(-0.5, 0.0, 0.1);
glPushMatrix();
player.print("Please pass to the next player");
player.print("press space to continue");
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glutSwapBuffers();
}
/*
Pre: None
Post: The screen drawn if it is detected that a player has won.
*/
void winDisplay()
{
glClearColor(0.7f, 0.7f, 0.7f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
BitmapPrinter player(-0.2, 0.0, 0.1);
glPushMatrix();
player.print(winner + " wins!");
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glutSwapBuffers();
}
/*
Pre: None
Post: checks to see if a player has won based upon whose turn it was
and whether or not all occupied cells on the opposing player's
board have been hit.
*/
void winCheck()
{
if (p1turn)
{
if (player2Home.ship_cells[0] == 0
&& player2Home.ship_cells[1] == 0
&& player2Home.ship_cells[2] == 0
&& player2Home.ship_cells[3] == 0
&& player2Home.ship_cells[4] == 0)
{
winner = "Player 1";
glutDisplayFunc(winDisplay);
}
else
{
game_pause = true;
glutDisplayFunc(pauseScreen);
}
}
else
{
if (player1Home.ship_cells[0] == 0
&& player1Home.ship_cells[1] == 0
&& player1Home.ship_cells[2] == 0
&& player1Home.ship_cells[3] == 0
&& player1Home.ship_cells[4] == 0)
{
winner = "Player 2";
glutDisplayFunc(winDisplay);
}
else
{
game_pause = true;
glutDisplayFunc(pauseScreen);
}
}
}
/*
Pre: None
Post: checks to see if ship is fully sunk
*/
void sunkCheck(Board& player,Board& opponent)
{
for (int i = 0; i < player.getSize(); i++)
{
for (int j = 0; j < player.getSize(); j++)
{
if (opponent.ship_cells[0] == 0)
{
if (opponent.board_[i][j].getID() == 0)
{
opponent.board_[i][j].setSunk();
player.board_[i][j].setTarSunk();
}
}
if (opponent.ship_cells[1] == 0)
{
if (opponent.board_[i][j].getID() == 1)
{
opponent.board_[i][j].setSunk();
player.board_[i][j].setTarSunk();
}
}
if (opponent.ship_cells[2] == 0)
{
if (opponent.board_[i][j].getID() == 2)
{
opponent.board_[i][j].setSunk();
player.board_[i][j].setTarSunk();
}
}
if (opponent.ship_cells[3] == 0)
{
if (opponent.board_[i][j].getID() == 3)
{
opponent.board_[i][j].setSunk();
player.board_[i][j].setTarSunk();
}
}
if (opponent.ship_cells[4] == 0)
{
if (opponent.board_[i][j].getID() == 4)
{
opponent.board_[i][j].setSunk();
player.board_[i][j].setTarSunk();
}
}
}
}
}
#endif