This repository was archived by the owner on Sep 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
368 lines (223 loc) · 6.29 KB
/
main.c
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "GL/glut.h"
// タイマーライブラリ
#include "GenericTimer.h"
#include "main.h"
#include "drive.h"
#include "Track.h"
#include "map.h"
#include "Ranking.h"
#include "ImageUtil.h"
#include "TextUtil.h"
// 走行位置
double trainPosition = 0;
// 走行速度(km/h)
double trainSpeed = 0;
// 描画インターバル時刻の記録
int lastIdleTime = 0;
// 運行ステータス
int state = 0;
// 起点出発時刻
double startTime = 0;
// 発車時刻
double departureTime = 0;
// スコアタイム
int scoreTime = 0;
// セクション
int sectionIndex = 0;
// 名前入力
char rankingName[20];
Texture msg[2], cover[5];
// キー入力ハンドラ
void ( *onKeyHandler )( void ) = NULL;
void startGame( void );
void toGameOP( void );
void joinRanking( void );
void writeRanking( void );
void init( void ){
// CLI起動ロゴ
printf( " _______ ____________ \n /_ __/ |/ / ____/ __ \\\n / / | / / __/ / / /\n / / / / /_/ / /_/ / \n/_/ /_/|_\\____/\\____/ \n \n" );
// クリアカラーの設定
glClearColor( 0.0, 0.0, 0.0, 1.0 );
// 座標系の設定
glOrtho( 0, WIN_WIDTH, WIN_HEIGHT, 0, -1, 1 );
// 路線の初期化
initTrack();
loadTrackAssets();
// 運転操作系の初期化
loadControlsAssets();
// マップ表示初期化
InitMap();
// タイマー開始
StartTimer();
// 描画インターバル時刻の初期値
lastIdleTime = GetTime();
GetTextureByPNGImage( &msg[0], MSG_TIME_LEFT );
GetTextureByPNGImage( &msg[1], MSG_CANNOT_STOP_HERE );
GetTextureByPNGImage( &cover[0], COVER_OP );
GetTextureByPNGImage( &cover[1], COVER_OVER );
GetTextureByPNGImage( &cover[2], COVER_END );
GetTextureByPNGImage( &cover[3], COVER_NAME_INPUT );
GetTextureByPNGImage( &cover[4], COVER_RANKING );
toGameOP();
}
void display( void ){
double now = GetTime();
glClear(GL_COLOR_BUFFER_BIT);
// 軌道と車両の描画
renderTrack( trainPosition, WIN_WIDTH, TRAIN_HEAD_X, MP_UNIT );
// 操作系の描画
renderControls();
if( state == 0 ){
DrawTexture( &msg[0], MSG_X + 150, 150, MSG_WIDTH, MSG_HEIGHT );
char leftTimeStr[10];
sprintf( leftTimeStr, "%2.0lf", ( departureTime - now ) / 1000 );
RenderText( leftTimeStr, MSG_X + 160, 200 );
renderTime( (int)( now - startTime ) / 1000 );
}else if( state == 1 ){
double distanceLeft = getSectionByIndex( sectionIndex ) - trainPosition;
renderMeters( trainSpeed, distanceLeft );
renderTime( (int)( now - startTime ) / 1000 );
if( distanceLeft < 30 && distanceLeft >= 2 && trainSpeed == 0 ){
DrawTexture( &msg[1], MSG_X, MSG_Y, MSG_WIDTH, MSG_HEIGHT );
}
// オープニング画面
}else if( state == 2 ){
DrawTexture( &cover[0], 0, 0, WIN_WIDTH, WIN_HEIGHT );
// ゲームオーバー
}else if( state == 3 ){
DrawTexture( &cover[1], 0, 0, WIN_WIDTH, WIN_HEIGHT );
// 終了画面
}else if( state == 4 ){
char timeChar[20];
sprintf( timeChar, "%2d:%02d", scoreTime / 60, scoreTime % 60 );
DrawTexture( &cover[2], 0, 0, WIN_WIDTH, WIN_HEIGHT );
RenderText( timeChar, 440, 200 );
if( isRankedIn( scoreTime ) == 1 ){
onKeyHandler = joinRanking;
}else{
onKeyHandler = toGameOP;
}
// 名前入力
}else if( state == 5 ){
DrawTexture( &cover[3], 0, 0, WIN_WIDTH, WIN_HEIGHT );
RenderText( rankingName, 430, 200 );
// ランキング表示
}else if( state == 6 ){
DrawTexture( &cover[4], 0, 0, WIN_WIDTH, WIN_HEIGHT );
renderRanking();
onKeyHandler = toGameOP;
}
RenderMap( state, sectionIndex, trainPosition );
glutSwapBuffers();
}
void idle( void ){
double now = GetTime();
double distanceLeft = getSectionByIndex( sectionIndex ) - trainPosition;
Track track = getTrack();
if( state == 0 ){
if( departureTime < now ) state = 1;
}else if( state == 1 ){
if( distanceLeft < -2 ){
printf( "== GAME OVER ==\n" );
state = 3;
onKeyHandler = toGameOP;
}
if( trainSpeed == 0 ){
if( distanceLeft < 2 ){
state = 0;
departureTime = now + 20000;
sectionIndex++;
if( sectionIndex >= track.sectionNum ){
state = 4;
scoreTime = (int)( GetTime() - startTime ) / 1000;
}
}
}
}
double t = GetRapTime( lastIdleTime );
if( t > 100 ){
tickDrive( t, &trainSpeed, &trainPosition );
lastIdleTime = now;
}
display();
}
void startGame( void ){
state = 0;
// 初期発車時刻
departureTime = GetTime() + 10000;
startTime = GetTime();
trainPosition = 0;
trainSpeed = 0;
sectionIndex = 0;
resetDrive();
}
void toGameOP( void ){
state = 2;
onKeyHandler = startGame;
}
void joinRanking( void ){
state = 5;
rankingName[0] = '\0';
}
void writeRanking( void ){
joinToRanking( rankingName, scoreTime );
state = 6;
}
void onKeyboard( unsigned char key, int x, int y ){
if( onKeyHandler != NULL ){
void (*k)(void) = onKeyHandler;
onKeyHandler = NULL;
k();
}else if( state == 5 ){
// Enter
if( (int)key == 13 ){
writeRanking();
// Backspace
}else if( (int)key == 127 ){
if( strlen( rankingName ) > 0 ){
rankingName[ strlen( rankingName ) - 1 ] = '\0';
}
}else if( strlen( rankingName ) < 19 ){
int len = strlen( rankingName );
rankingName[ len ] = key;
rankingName[ len + 1 ] = '\0';
}
}else{
if( key == 'q' ) exit( 0 );
}
}
void onSpecialKey( int key, int x, int y ){
if( onKeyHandler != NULL ){
void (*k)(void) = onKeyHandler;
onKeyHandler = NULL;
k();
}else if( state == 5 ){
}else{
int locked;
if( state == 1 ) locked = 0;
else locked = 1;
if( key == GLUT_KEY_DOWN ){
stepDrive( 1, locked );
}else if( key == GLUT_KEY_UP ){
stepDrive( -1, locked );
}
}
}
int main( int argc, char *argv[] ){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize( WIN_WIDTH, WIN_HEIGHT );
glutCreateWindow( "TXGO" );
glutDisplayFunc( display );
glutIdleFunc( idle );
glutKeyboardFunc( onKeyboard );
glutSpecialFunc( onSpecialKey );
//glutMouseFunc(mouse);
//glutReshapeFunc(reshape);
init();
glutMainLoop();
return 0;
}