-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
273 lines (231 loc) · 5.98 KB
/
main.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
#include <bits/stdc++.h>
#include "card.h"
#pragma comment(lib, "glew32.lib")
#pragma comment(lib, "opengl32.lib")
using namespace std;
#define MAXN 10
GLint ScreenWidth = 800;
GLint ScreenHeight = 600;
GLfloat eyex = 0;
GLfloat eyey = 0;
GLfloat eyez = 0;
GLfloat teyex = 0;
GLfloat teyey = 0;
GLfloat teyez = 0;
GLfloat sca = 0.015;
GLfloat deg = 0;
GLboolean key_buf[256];
GLfloat fp = 1000/60;
GLboolean onfocus;
GLboolean swp;
GLboolean mouserdown;
GLboolean keyldown;
GLboolean mouseldown;
GLint select=-1;
card deck[111];
vector<GLint> pri;
GLint siz;
void processKeysDown(unsigned char key,GLint x,GLint y){
if(!onfocus)return;
if(key==27)exit(0);
else key_buf[key]=1;
}
void processKeysUp(unsigned char key,GLint x,GLint y){
if(!onfocus)return;
if(key==27)exit(0);
else key_buf[key]=0;
}
void ff(GLint a){
GLboolean is=false;
if(key_buf['w']){
is=true;
eyez+=0.5;
}
if(key_buf['s']){
is=true;
eyez-=0.5;
}
if(key_buf['a']){
is=true;
eyex-=0.5;
}
if(key_buf['d']){
is=true;
eyex+=0.5;
}
if(key_buf['+']||key_buf['=']){
is=true;
key_buf['+']=key_buf['=']=false;
sca+=0.005;
}
if(key_buf['-']){
is=true;
key_buf['-']=false;
sca-=0.005;
if(sca<0)sca=0;
}
if(key_buf[13]){
keyldown=true;
if(select==-1){
for(GLint i=0;i<siz;i++){
if(deck[pri[i]].mouse_on(eyex,eyez)){
select=i;
deck[pri[i]].ssel();
swp=true;
break;
}
}
}
}
else{
if(select!=-1&&!mouseldown){
deck[pri[0]].ssel();
select=-1;
}
keyldown=false;
}
if(key_buf[8]||key_buf[127]){
for(GLint i=0;i<siz;i++){
if(deck[pri[i]].mouse_on(eyex,eyez)){
deck[pri[i]].svis();
break;
}
}
key_buf[8]=key_buf[127]=false;
}
if(is&&onfocus)glutPostRedisplay();
glutTimerFunc(fp,ff,0);
}
void RendScene() {
//if(!onfocus)return;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if(onfocus)glutWarpPointer(ScreenWidth/2,ScreenHeight/2);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
if(select!=-1){
deck[pri[select]].move_x(eyex);
deck[pri[select]].move_z(eyez);
if(swp){
if(select!=0){
GLint t=pri[select];
pri.erase(pri.begin()+select);
pri.insert(pri.begin(),t);
select=0;
}
swp=false;
}
}
glScalef(sca,sca,sca);
glRotatef(90,-1,0,0);
glTranslatef(-eyex,0,-eyez);
for(GLint s=0;s<siz;s++){
deck[pri[s]].draw(s);
}
glBegin(GL_LINES);
glLineWidth(100);
glColor3f(0,1,1);
glVertex3f(eyex+1,0.5,eyez);
glVertex3f(eyex-1,0.5,eyez);
glVertex3f(eyex,0.5,eyez+1);
glVertex3f(eyex,0.5,eyez-1);
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void MouseFunc(GLint button, GLint state, GLint x, GLint y){
if(!onfocus)return;
else if(state == GLUT_DOWN){
if(button == GLUT_RIGHT_BUTTON){
mouserdown = GL_TRUE;
for(GLint i=0;i<siz;i++){
if(deck[pri[i]].mouse_on(eyex,eyez)){
deck[pri[i]].svis();
break;
}
}
}
if(button == GLUT_LEFT_BUTTON){
mouseldown = GL_TRUE;
if(select==-1){
for(GLint i=0;i<siz;i++){
if(deck[pri[i]].mouse_on(eyex,eyez)){
select=i;
swp=true;
deck[pri[i]].ssel();
break;
}
}
}
}
}
else{
if(button == GLUT_RIGHT_BUTTON) mouserdown = GL_FALSE;
if(button == GLUT_LEFT_BUTTON){
if(select!=-1){
deck[pri[0]].ssel();
select=-1;
}
mouseldown = GL_FALSE;
}
}
eyex+=(x-ScreenWidth/2)/15.0;
eyez-=(y-ScreenHeight/2)/12.0;
glutPostRedisplay();
}
void MouseMotionFunc(GLint x, GLint y){
if(!onfocus)return;
eyex+=(x-ScreenWidth/2)/15.0;
eyez-=(y-ScreenHeight/2)/12.0;
glutPostRedisplay();
}
void EntryFuc(GLint state){
if(state==GLUT_ENTERED)onfocus=true;
else onfocus=false;
}
void ReShape(GLint w,GLint h){
ScreenWidth=w;
ScreenHeight=h;
gluPerspective(0,ScreenWidth/ScreenWidth*1.0,0.1,1000);
glutPostRedisplay();
}
pair<GLint, GLint> li[111];
GLint main(GLint argc, char** argv) {
srand(time(0));
memset(key_buf,0,sizeof(key_buf));
siz=52;
for(int i=0;i<4;i++)
for(int j=0;j<13;j++){
li[i*13+j].first=i;
li[i*13+j].second=j+1;
pri.push_back(i*13+j);
}
random_shuffle(li,li+siz);
for(GLint s=0;s<4;s++){
for(GLint i=1;i<=13;i++){
deck[13*s+i-1]=card(li[13*s+i-1].second,li[13*s+i-1].first,(i-7)*11,0,(s-2)*21,1);
}
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(ScreenWidth, ScreenHeight);
gluPerspective(0,ScreenWidth/ScreenHeight*1.0,0.1,1000);
glutCreateWindow("OpenGL");
glClearColor(160.0/255, 82.0/255, 45.0/255, 1.0f);
glEnable(GL_DEPTH_TEST);
glutKeyboardFunc(processKeysDown);
glutKeyboardUpFunc(processKeysUp);
glutMouseFunc(MouseFunc);
glutPassiveMotionFunc(MouseMotionFunc);
glutMotionFunc(MouseMotionFunc);
glutEntryFunc(EntryFuc);
glutDisplayFunc(RendScene);
glutIdleFunc(RendScene);
glutTimerFunc(fp,ff,0);
glutReshapeFunc(ReShape);
glutSetCursor(GLUT_CURSOR_NONE);
glutWarpPointer(ScreenWidth/2,ScreenHeight/2);
glutMainLoop();
return 0;
}