-
Notifications
You must be signed in to change notification settings - Fork 0
/
atc.c
213 lines (189 loc) · 5.28 KB
/
atc.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
#include <SDL2/SDL.h>
#include<SDL2/SDL_image.h>
#define SCREEN_WIDTH 728
#define SCREEN_HEIGHT 455
typedef struct {
float x;
float y;
} Point;
typedef struct {
Point start;
Point end;
} Line;
typedef struct {
Point position;
Line path;
float progress;
} Plane;
void draw_line(SDL_Renderer *renderer, Line line) {
SDL_RenderDrawLine(renderer, (int)line.start.x, (int)line.start.y, (int)line.end.x, (int)line.end.y);
}
void draw_plane(SDL_Renderer *renderer, Point position) {
SDL_Rect rect = { (int)position.x - 2.5, (int)position.y - 2.5, 5, 5 };
SDL_RenderFillRect(renderer, &rect);
}
void draw_bg(SDL_Renderer *renderer, SDL_Texture* texture){
SDL_RenderCopy(renderer, texture, NULL, NULL);
}
int main(int argc, char *argv[]) {
SDL_Init(SDL_INIT_VIDEO);
// Create a window
SDL_Window *window = SDL_CreateWindow(
"ATC Simulator",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_SHOWN
);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_Surface* surface = IMG_Load("map.jpg");
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
// Create some planes
Plane planes[] = {
//pak-uk
{
{ 460,200 },
{ { 460, 200 }, { 330,150 } },
0
},
//can--afr
{
{ 150, 150 },
{ { 150, 150 }, { 350, 230 } },
0
},
//pak-ind
{
{ 460,200 },
{ { 460, 200 }, { 482,220 } },
0
},
//eur-uk
{
{ 400,130 },
{ { 400, 130 }, { 330,150 } },
0
},
//rus-ind
{
{ 500,120 },
{ { 500, 120 }, { 482,220 } },
0
},
//rus-eur
{
{ 500,160 },
{ { 500, 120 }, { 400,130 } },
0
},
//can--us
{
{ 150, 150 },
{ { 150, 150 }, { 145, 185 } },
0
},
//china-jap
{
{ 480,180 },
{ { 480,180 }, { 587, 193 } },
0
}
,//safr to afr
{
{ 390, 310 },
{ { 390, 310 }, { 350, 230 } },
0
},
//samr--us
{
{ 240 , 300 },
{ { 240, 300 }, { 145, 185 } },
0
},
//samr--uk
{
{ 240 , 300 },
{ { 240, 300 }, { 330, 150 } },
0
},
//china-pak
{
{ 480,180 },
{ { 480,180 }, { 460, 200 } },
0
},
//pak-afr
{
{ 460,200 },
{ { 460, 200 }, { 350,230 } },
0
},
//ind - jap
{
{ 482,220 },
{ { 482, 220 }, { 587,193 } },
0
},
//aus - jap
{
{ 580,300 },
{ { 580, 300 }, { 587,193 } },
0
},
//aus - ind
{
{ 580,300 },
{ { 580, 300 }, { 482,220 } },
0
}
};
int num_planes = sizeof(planes) / sizeof(planes[0]);
// Start the simulation loop
Uint32 last_frame_time = SDL_GetTicks();
while (1) {
// Handle events
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
// Quit the program if the window is closed
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
}
// Calculate the time since the last frame
Uint32 current_time = SDL_GetTicks();
Uint32 elapsed_time = current_time - last_frame_time;
last_frame_time = current_time;
// Clear the screen
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
draw_bg(renderer,texture);
// Draw the lines
SDL_SetRenderDrawColor(renderer, 100, 100, 100, 100);
for (int i = 0; i < num_planes; i++) {
draw_line(renderer, planes[i].path);
}
// Update and draw the planes
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
for (int i = 0; i < num_planes; i++) {
// Update the plane's position
float distance = elapsed_time / 1000.0 * 25; // 50 pixels per second
planes[i].progress += distance / SDL_sqrt(SDL_pow(planes[i].path.end.x - planes[i].path.start.x, 2) + SDL_pow(planes[i].path.end.y - planes[i].path.start.y, 2));
if (planes[i].progress > 1) {
planes[i].progress = 0;
Point tmp = planes[i].path.start;
planes[i].path.start = planes[i].path.end;
planes[i].path.end = tmp;
}
planes[i].position.x = planes[i].path.start.x + planes[i].progress * (planes[i].path.end.x - planes[i].path.start.x);
planes[i].position.y = planes[i].path.start.y + planes[i].progress * (planes[i].path.end.y - planes[i].path.start.y);
// Draw the plane
draw_plane(renderer, planes[i].position);
}
// Present the rendered image to the screen
SDL_RenderPresent(renderer);
}
}