-
Notifications
You must be signed in to change notification settings - Fork 0
/
Phase 1 (almost)
125 lines (104 loc) · 2.22 KB
/
Phase 1 (almost)
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
#include <GL/glut.h>
#include <math.h>
#include <cstring>
#include <cstdio>
#include "newpac.h"
#include <pthread.h>
#include <unistd.h>
#define COLUMNS 19
#define ROWS 22
pthread_t ghostThreads[4], gameEngineThread, uiThread;
void* updateGhost(void* arg)
{
Ghost* ghost = (Ghost*)arg;
while (1)
{
moveGhost1(*ghost);
usleep(800000);
}
return NULL;
}
void startGhostThreads()
{
pthread_create(&ghostThreads[0], NULL, updateGhost, (void*)&g1);
pthread_create(&ghostThreads[1], NULL, updateGhost, (void*)&g2);
pthread_create(&ghostThreads[2], NULL, updateGhost, (void*)&g3);
pthread_create(&ghostThreads[3], NULL, updateGhost, (void*)&g4);
}
void stopGhostThreads()
{
for (int i = 0; i < 4; ++i)
{
pthread_cancel(ghostThreads[i]);
}
}
void updatePacman(int dummy)
{
movement();
glutTimerFunc(200, updatePacman, 0);
}
void ui_function()
{
glClear(GL_COLOR_BUFFER_BIT);
drawGrid();
drawPacdots();
drawTitle();
drawHomescreenghost();
otherui();
drawPacman();
drawGhost();
glutSwapBuffers();
}
void keyboard_callback(unsigned char key, int x, int y)
{
currentmove = key;
}
void* uifunc(void* arg)
{
while (1)
{
ui_function();
usleep(800000);
}
return NULL;
}
void startuiThread()
{
pthread_create(&uiThread, NULL, uifunc, NULL);
}
void stopuiThread()
{
pthread_cancel(uiThread);
}
void* gameEngine(void* arg)
{
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(1400, 800);
glutCreateWindow("Pacman");
glutDisplayFunc(ui_function);
glutReshapeFunc(reshape_callback);//callback func in header file
glutKeyboardFunc(keyboard_callback);
glutTimerFunc(200, updatePacman, 0);
init();
startuiThread();
startGhostThreads();
glutMainLoop();
stopGhostThreads();
stopuiThread();
return NULL;
}
void startGameEngineThread()
{
pthread_create(&gameEngineThread, NULL, gameEngine, NULL);
}
void stopGameEngineThread()
{
pthread_cancel(gameEngineThread);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
startGameEngineThread();
pthread_join(gameEngineThread, NULL);
return 0;
}