-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·140 lines (128 loc) · 3.56 KB
/
main.cpp
File metadata and controls
executable file
·140 lines (128 loc) · 3.56 KB
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
/*
Made with ❤ by srinSkit.
Created on 10 March 2018.
*/
#ifdef _WIN32
#include <windows.h>
#endif
#include <GL/glut.h>
#include <cstdio>
#include <iostream>
#include "Loader.h"
#include "Model.h"
#include "Graphics.h"
#include "Common.h"
int win;
extern Tank *mainTank;
int nBullets = 0;
int gridx, gridy, drawGridCount = 0, drawPath = 0;
bool autoMove = false;
stack<Cell *> moveStack, tmpStack;
stack<char> keyStack;
#define SHOWPATHDELAY 50
void display() {
glClear(GL_COLOR_BUFFER_BIT);
Model::renderStatic();
Model::renderDynamic();
Model::nextFrame();
if (drawGridCount > 0) {
if (drawGridCount == SHOWPATHDELAY) {
moveStack = mainTank->autoPilot(gridx, gridy);
if (!moveStack.empty())
drawPath = SHOWPATHDELAY;
}
glColor3ub(255, 255, 255);
glBegin(GL_LINE_LOOP);
glVertex2i(gridx * cellWidth, gridy * cellHeight);
glVertex2i(gridx * cellWidth + cellWidth, gridy * cellHeight);
glVertex2i(gridx * cellWidth + cellWidth, gridy * cellHeight + cellHeight);
glVertex2i(gridx * cellWidth, gridy * cellHeight + cellHeight);
glVertex2i(gridx * cellWidth, gridy * cellHeight);
glEnd();
drawGridCount--;
}
if (drawPath > 0) {
glColor3ub(255, 255, 255);
glBegin(GL_LINE_STRIP);
tmpStack = moveStack;
while (!tmpStack.empty()) {
auto res = tmpStack.top();
tmpStack.pop();
glVertex2i(res->gridx * cellWidth + cellWidth / 2, res->gridy * cellHeight + cellHeight / 2);
}
glEnd();
drawPath--;
if (drawPath <= 0)
mainTank->autoMoveEnable();
}
// if (!moveStack.empty()) {
// auto node = moveStack.top();
// auto theta = atan2(node->gridy * cellHeight + cellHeight / 2 - mainTank->y,
// node->gridx * cellHeight + cellWidth / 2 - mainTank->x);
// }
glutSwapBuffers();
}
void reshape(int w, int h) {
screenWidth = w;
screenHeight = h;
glViewport(w / 2 - h / 2, 0, h, h);
}
void initOpenGL() {
gluOrtho2D(0, worldWidth, 0, worldHeight);
glClearColor(0, 0, 0, 0);
glEnableClientState(GL_VERTEX_ARRAY);
}
void deInitOpenGl() {
glDisableClientState(GL_VERTEX_ARRAY);
}
void onKeyPressed(unsigned char key, int x1, int y1) {
switch (key) {
case 'q':
printf("Score: %d!", max(0, score));
glutDestroyWindow(win);
break;
case 'w':
case 'a':
case 's':
case 'd':
case 'j':
case 'l':
mainTank->actionQ.push(key);
break;
case 'k':
if (nBullets < 30) {
mainTank->actionQ.push(key);
nBullets++;
}
break;
default:
break;
}
while (!moveStack.empty())moveStack.pop();
}
void onMouseClick(int button, int state, int x, int y) {
if (state != GLUT_DOWN)return;
gridx = x;
gridy = y;
screenToGrid(gridx, gridy);
drawGridCount = SHOWPATHDELAY;
}
int main(int argc, char **argv) {
load();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(worldWidth, worldHeight);
glutInitWindowPosition(2000, 0);
win = glutCreateWindow("manVcat");
// glutFullScreen();
initOpenGL();
glutKeyboardFunc(onKeyPressed);
glutMouseFunc(onMouseClick);
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
deInitOpenGl();
unload();
return 0;
}