-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain.cpp
More file actions
221 lines (175 loc) · 5.88 KB
/
Main.cpp
File metadata and controls
221 lines (175 loc) · 5.88 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
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
//Main.cpp
#include "assist.hpp"
#include "Stage1.hpp" //最初のステージクラス
//定数定義
#define TIMER_WAIT 33 //タイマーの待ち時間(画面の書き換え間隔)
#define FIRST_STAGECLASS Stage1() //最初に呼ばれるステージクラス
//グローバル変数の宣言
int WindowWidth = 400; //ウィンドウ幅
int WindowHeight = 400; //ウィンドウ高さ
const char WindowTitle[] = "OpenGLGame"; //ウィンドウタイトル
StageClass *Stage, *NextStage; //ステージクラスのポインタ
//関数のプロトタイプ宣言
void Init();
void Disp(); //Display(), Timer() から呼ばれる。描画処理本体
void Display();
void Reshape(int x, int y);
void Keyboard(unsigned char key, int x, int y);
void KeyboardUp(unsigned char key, int x, int y);
void Close();
void Timer(int);
//void ChangeStage(StageClass* s); //外部から呼ぶことがあるため宣言はassistの中
void toNextStage();
/*-----------------------------------------------------------------------------------*
エントリーポイント、初期化
*-----------------------------------------------------------------------------------*/
//エントリーポイント
int main(int argc, char *argv[])
{
//GLUT初期化
glutInit(&argc, argv);
//ウィンドウ作成
glutInitWindowSize(WindowWidth, WindowHeight); //ウィンドウサイズ
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE); //RGBAカラー、zバッファ、ダブルバッファリング
glutCreateWindow(WindowTitle); //ウィンドウを作る
//コールバック関数の登録
glutDisplayFunc(Display); //再描画時
glutCloseFunc(Close); //ウィンドウが閉じられたとき
glutReshapeFunc(Reshape); //ウィンドウサイズ変更時
glutKeyboardFunc(Keyboard); //キー入力
glutKeyboardUpFunc(KeyboardUp); //キーが離された
glutTimerFunc( TIMER_WAIT, Timer, 0); //一定時間後に呼ばれる
//初期化
Init();
//メインループ
glutMainLoop(); //※決して返ってこない
return 0;
}
//初期化処理
void Init(void)
{
// バックバッファをクリアする色(背景色)の指定
glClearColor(0.5, 0.5, 1.0, 1.0);
// 深度テストON
glEnable(GL_DEPTH_TEST);
//ライト
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
//片面表示(高速化のため)
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
//最初のステージクラスをnewする
Stage = new FIRST_STAGECLASS;
//次のステージクラスはNULLにしとく
NextStage = NULL;
}
/*-----------------------------------------------------------------------------------*
画面描き換え
*-----------------------------------------------------------------------------------*/
// 描画処理本体
void Disp(){
// バックバッファをクリア
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// モデルビュー行列の操作に切り替え
glMatrixMode(GL_MODELVIEW);
Stage->Disp(); //ステージDisp()へ
//2Dへ移行
//PROJECTION行列を操作に変更
glMatrixMode(GL_PROJECTION);
glPushMatrix(); //PROJECTION行列を保存
glLoadIdentity();
gluOrtho2D(0, WindowWidth, 0, WindowHeight); //ビューイング領域を設定(正射影で設定)
//MODELVIEW行列を操作
glMatrixMode(GL_MODELVIEW);
glPushMatrix(); //MODELVIEW行列を保存
glLoadIdentity();
//いろいろ無効にする
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
//描画
Stage->Disp2D(WindowWidth, WindowHeight); //ステージDisp2D()へ
//元に戻す(3D用へ戻る)
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glPopMatrix(); //MODELVIEW行列をもとに戻す
glMatrixMode(GL_PROJECTION);
glPopMatrix(); //PROJECTION行列をもとに戻す
}
// 画面描き換え
void Display(){
//描画
Disp();
//ダブルバッファ入れ替え
glutSwapBuffers();
//ステージ変更処理
if( NextStage ) toNextStage();
}
// タイマー
void Timer(int value){
glutTimerFunc( TIMER_WAIT, Timer, 0); //次のタイマーをセット
glutSwapBuffers(); //ダブルバッファ入れ替え
Disp(); //次フレームの描画
//ステージ変更処理
if( NextStage ) toNextStage();
}
// ウィンドウサイズ変更
void Reshape(int x, int y)
{
// ウィンドウサイズを保存(Disp()でも使うため)
WindowWidth = x;
WindowHeight = y;
// ビューポートの設定
glViewport(0, 0, WindowWidth, WindowHeight);
// 射影行列の設定
glMatrixMode(GL_PROJECTION); //PROJECTION行列を操作
glLoadIdentity();
gluPerspective(45.0, (double)WindowWidth/(double)WindowHeight, 0.1, 1000.0); //設定
}
/*-----------------------------------------------------------------------------------*
入力関係
*-----------------------------------------------------------------------------------*/
// キーボード処理
void Keyboard(unsigned char key, int x, int y)
{
//ステージクラスへ
Stage->Input(SC_INPUT_KEY_DOWN, (int)key, x, y);
//ステージ変更処理
if( NextStage ) toNextStage();
switch ( key ){
case '\033': //Esc
Close();
exit(0);
break;
}
}
//キーが離された
void KeyboardUp(unsigned char key, int x, int y)
{
Stage->Input(SC_INPUT_KEY_UP, (int)key, x, y);
//ステージ変更処理
if( NextStage ) toNextStage();
}
/*-----------------------------------------------------------------------------------*
その他
*-----------------------------------------------------------------------------------*/
//ステージ変更の受付
//ここではまだ変更されない。次に呼ばれるtoNextStage()で変更が実施される
void ChangeStage(StageClass* next)
{
NextStage = next;
}
//次のステージへ変更処理
void toNextStage(){
delete Stage;
Stage = NextStage;
NextStage = NULL;
}
// 終了処理
void Close(void)
{
printf("finish!\n");
//ステージクラスを破棄する。デストラクタが呼ばれる。
delete Stage;
}