-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcGame.cpp
176 lines (133 loc) · 3.9 KB
/
cGame.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
// Copyright 2015 Kelvin Chandra, Software Laboratory Center, Binus University. All Rights Reserved.
#pragma warning(disable:4996)
#include "Globals.h"
#include "cGame.h"
#include "cMenuScene.h"
GLuint img_id[2];
cGame::cGame(void)
{
}
cGame::~cGame(void)
{
}
bool cGame::Init()
{
//Graphics initialization
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
visible_area.left = 0;
visible_area.right = GAME_WIDTH;
visible_area.bottom = 0;
visible_area.top = GAME_HEIGHT;
glOrtho(visible_area.left, visible_area.right, visible_area.bottom, visible_area.top, 3, -101);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_ALPHA_TEST);
glEnable(GL_DEPTH_TEST);
//Texture initialization
if (!cAssetManager::getInstance().Load())printf("Some images may missing");
//Stage initialization
active_scene = new cMenuScene();
return true;
}
bool cGame::Loop()
{
int t1, t2;
t1 = glutGet(GLUT_ELAPSED_TIME);
if (bIsFirst) {//only call once for init on active_scene
bIsFirst = false;
active_scene->Init();
}
Update();
Render();
if (bSceneValid == false) {
RealUpdateScene();
}
do {
t2 = glutGet(GLUT_ELAPSED_TIME);
} while (t2 - t1 < 1000 / 30); //30 fps = 1000/30
return true;
}
//Input
void cGame::ReadKeyboard(unsigned char key, int x, int y, bool press)
{
active_scene->ReadKeyboard(key, x, y, press);//call ReadKeyboard on active scene to receive input on scene
}
void cGame::ReadSpecialKeyboard(unsigned char key, int x, int y, bool press)
{
active_scene->ReadSpecialKeyboard(key, x, y, press);//call Read SpecialKeyboard on active scene to receive special key
}
void cGame::ReadMouse(int button, int state, int x, int y)
{
active_scene->ReadMouse(button, state, x, y);
}
bool cGame::Update(float tpf/*=0.0333*/)
{
active_scene->Update(tpf);
return true;
}
void cGame::Reshape(int w, int h) {
glViewport(0, 0, w, h);
}
//Output
void cGame::Render()
{
int tex_w, tex_h;
int tex_w2, tex_h2;
bool run = true;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
active_scene->Render();
//glEnable(GL_BLEND); // Turn Blending On
////glDisable(GL_DEPTH_TEST); // Turn Depth Testing Off
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_BLEND); // Turn Blending Off
//glEnable(GL_DEPTH_TEST); // Turn Depth Testing On
glAlphaFunc(GL_GREATER, 0.05f);
glutSwapBuffers();
}
void cGame::renderBackgroud(int BGID) {
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, img_id[BGID]);
glBegin(GL_QUADS);
glTexCoord2f(0, 1); glVertex3i(0, 0, 100);
glTexCoord2f(1, 1); glVertex3i(GAME_WIDTH, 0, 100);
glTexCoord2f(1, 0); glVertex3i(GAME_WIDTH, GAME_HEIGHT, 100);
glTexCoord2f(0, 0); glVertex3i(0, GAME_HEIGHT, 100);
glEnd();
glDisable(GL_TEXTURE_2D);
}
bool cGame::loadTexture(char *filepath, int identifier) {
int img_width, img_height = 0;
corona::Image *img = corona::OpenImage(filepath);
if (img) {
img = ConvertImage(img, corona::PF_R8G8B8A8);
img_width = img->getWidth();
img_height = img->getHeight();
glGenTextures(1, &img_id[identifier]);
glBindTexture(GL_TEXTURE_2D, img_id[identifier]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img_width, img_height, 0, GL_RGBA, GL_UNSIGNED_BYTE
, img->getPixels());
}
return true;
}
void cGame::loadImage(char *filepath, int BGID)
{
if (!cGame::loadTexture(filepath, BGID)) {
printf("Error Load Background Image");
}
}
void cGame::UpdateScene(cScene *scene)
{
bSceneValid = false;
next_scene = &(*scene);
}
void cGame::RealUpdateScene()
{
bSceneValid = true;
bIsFirst = true;
active_scene = &(*next_scene);
}