This repository has been 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 pathTrack.c
137 lines (86 loc) · 2.69 KB
/
Track.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
#include <stdlib.h>
#include "GL/glut.h"
#include "Track.h"
#include "ImageUtil.h"
Track track;
// テクスチャ画像
Texture trainTex, railTex, bgTexs[2];
void initTrack( void ){
track.sceneNum = 7;
track.scenes = ( Scene * )malloc( sizeof( Scene ) * track.sceneNum );
if( track.scenes == NULL ) exit( EXIT_FAILURE );
track.scenes[0].beginAt = -120;
track.scenes[0].type = 1;
track.scenes[1].beginAt = 5;
track.scenes[1].type = 0;
track.scenes[2].beginAt = 2050;
track.scenes[2].type = 1;
track.scenes[3].beginAt = 2210;
track.scenes[3].type = 0;
track.scenes[4].beginAt = 3250;
track.scenes[4].type = 1;
track.scenes[5].beginAt = 3410;
track.scenes[5].type = 0;
track.scenes[6].beginAt = 3500;
track.scenes[6].type = 0;
track.sectionNum = 2;
track.sections = ( int * )malloc( sizeof( int ) * track.sectionNum );
if( track.sections == NULL ) exit( EXIT_FAILURE );
track.sections[0] = 2200;
track.sections[1] = 3400;
}
void loadTrackAssets( void ){
// テクスチャ画像のロード
GetTextureByPNGImage( &bgTexs[0], BG_TUNNEL_TEXTURE );
GetTextureByPNGImage( &bgTexs[1], BG_STATION_TEXTURE );
GetTextureByPNGImage( &trainTex, TRAIN_TEXTURE );
GetTextureByPNGImage( &railTex, RAIL_TEXTURE );
}
Track getTrack( void ){
return track;
}
int getSceneIndexByPosition( int pos ){
for( int i = 0; i < track.sceneNum; i++ ){
if( track.scenes[i].beginAt >= pos ) return i - 1;
}
return -1;
}
int isSceneExists( int pos ){
if( pos < 0 || track.sceneNum <= pos ) return 0;
else return 1;
}
int getSectionByIndex( int index ){
return track.sections[ index ];
}
void renderTrack( double trainPosition, int width, int train_x, int mp_unit ){
// シーン背景の描画
int posOfRE = trainPosition * mp_unit - ( width - train_x );
int ib = -1;
int pb = width;
// printf( "%d / ", posOfRE );
while( -BG_WIDTH < pb ){
int i = getSceneIndexByPosition( ( ( width - pb ) + posOfRE ) / mp_unit );
if( i < 0 ) break;
if( i != ib ){
int d = track.scenes[i].beginAt * mp_unit - posOfRE;
if( d > -BG_WIDTH ) pb = width - d;
else pb = width + ( -d ) % BG_WIDTH;
ib = i;
}
int type = track.scenes[i].type;
if( pb - BG_WIDTH >= -BG_WIDTH ){
DrawTexture( &bgTexs[type], pb - BG_WIDTH, 0, BG_WIDTH, BG_HEIGHT );
// printf( "%d + %d ", pb - BG_WIDTH, i );
}
pb -= BG_WIDTH;
}
// printf( "\n" );
// 軌道の描画
int pr = width + posOfRE % RAIL_WIDTH + RAIL_WIDTH;
while( 0 < pr ){
DrawTexture( &railTex, pr - RAIL_WIDTH, 0, RAIL_WIDTH, RAIL_HEIGHT );
pr -= RAIL_WIDTH;
}
// 車両の描画
DrawTexture( &trainTex, train_x, RAIL_Y - TRAIN_HEIGHT, TRAIN_WIDTH, TRAIN_HEIGHT );
}