-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.h
132 lines (110 loc) · 2.59 KB
/
render.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
// Rendering Functions //
#include <iostream>
#include <string>
#include <string.h>
#include <GL/glut.h>
#include "game.h"
#include "write-score.h"
using namespace std;
// Print Given Text //
void print_text(float x, float y, char string[], int number)
{
int n = number;
int count = 0;
do
{
n /= 10;
++count;
} while (n != 0);
char *int_str;
int_str = (char*)malloc((count * 2) * sizeof(char));
sprintf(int_str, "%d", number);
glColor3f(1, 1, 1);
glRasterPos2f(x, y);
int len, i;
len = strlen(string);
for (i = 0; i < len; i++)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, string[i]);
}
len = strlen(int_str);
for (i = 0; i < len; i++)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, int_str[i]);
}
glEnd();
glFlush();
}
// Demolish Window(id) //
void destroy_window(void)
{
if (gameplay == 0)
{
glutDestroyWindow(glutGetWindow());
writeDate(score);
cout << "Score = " << score << endl;
}
}
// Player Shape Render //
void player()
{
movement();
glColor3f(0.0, 1.0, 0.0);
glBegin(GL_POLYGON);
glVertex2f(playerPosX, playerPosY);
glVertex2f(playerPosX + DEMX, playerPosY);
glVertex2f(playerPosX + DEMX, playerPosY + DEMY);
glVertex2f(playerPosX, playerPosY + DEMY);
glEnd();
glFlush();
}
// Fruit Shape Render //
void fruit()
{
glColor3f(1.0, 1.0, 0.0);
glBegin(GL_POLYGON);
glVertex2f(fruitPosX, fruitPosY);
glVertex2f(fruitPosX + DEMX, fruitPosY);
glVertex2f(fruitPosX + DEMX, fruitPosY + DEMY);
glVertex2f(fruitPosX, fruitPosY + DEMY);
glEnd();
glFlush();
}
// Text Area //
void text_box()
{
glColor3f(0.5, 0.5, 0.5);
glBegin(GL_POLYGON);
glVertex2f( 0.0, HEIGHT - 2.0);
glVertex2f( WIDTH, HEIGHT - 2.0);
glVertex2f( WIDTH, HEIGHT);
glVertex2f( 0.0, HEIGHT);
glEnd();
glFlush();
}
// Render Game //
void render()
{
glClear(GL_COLOR_BUFFER_BIT);
text_box();
print_text(0.0, 19.4, "SCORE = ", score);
fruit();
player();
glutSwapBuffers();
destroy_window();
}
void init(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(800, 800);
glutCreateWindow("GLsnake");
glClearColor(0, 0, 0 ,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, WIDTH - 1, 0, HEIGHT - 1);
glutSpecialFunc(logic);
glutDisplayFunc(render);
glutMainLoop();
}