-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
156 lines (149 loc) · 3.61 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
#define _GNU_SOURCE
#include <SDL2/SDL.h>
#include "grid.h"
#include <math.h>
#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 768
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
/**
* drawloop - loop to draw terrain
* @renderer: renderer to use
* @grid: grid to draw
* @gwidth: width of grid (in number of nodes)
* @gheight: height of grid (in number of nodes)
*
* Return: 0 on success
*/
int drawloop(SDL_Renderer *renderer, gridnode **grid,
int gwidth, int gheight)
{
SDL_Event event;
int quit, scale, scalemax, draw = 1;
double rotate = M_PI / -4, incline = 3 * M_PI / -8;
scale = gheight < gwidth ? gheight : gwidth;
scalemax = SCREEN_HEIGHT > SCREEN_WIDTH ? SCREEN_HEIGHT / 1.5 : SCREEN_WIDTH / 1.5;
scalemax = scalemax / scale;
scale = scalemax;
quit = 0;
while (!quit)
{
if (draw == 1)
{
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xff);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 0x00, 0xff, 0xff, 0xff);
calculate_grid(grid, scale, incline, rotate,
SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, gwidth, gheight);
draw_grid(grid, renderer);
SDL_RenderPresent(renderer);
draw = 0;
}
while (SDL_PollEvent(&event) != 0)
{
if (event.type == SDL_QUIT ||
(event.type == SDL_KEYDOWN &&
event.key.keysym.sym == SDLK_ESCAPE))
quit = 1;
if (event.type == SDL_KEYDOWN &&
event.key.keysym.sym == SDLK_LEFT)
rotate -= M_PI / 128;
if (event.type == SDL_KEYDOWN &&
event.key.keysym.sym == SDLK_RIGHT)
rotate += M_PI / 128;
if (event.type == SDL_KEYDOWN &&
event.key.keysym.sym == SDLK_UP)
incline += M_PI / 256;
if (event.type == SDL_KEYDOWN &&
event.key.keysym.sym == SDLK_DOWN)
incline -= M_PI / 256;
if (event.type == SDL_KEYDOWN &&
event.key.keysym.sym == SDLK_w)
if (scale <= scalemax)
scale += 1;
if (event.type == SDL_KEYDOWN &&
event.key.keysym.sym == SDLK_s)
if (scale > 5)
scale -= 1;
draw = 1;
}
}
return (0);
}
/**
* freegrid - frees grid
*
* @grid: grid to free
*
* Return: void
*/
void freegrid(gridnode **grid)
{
int ct = 0;
if (grid != NULL)
if (*grid != NULL)
{
while (grid[ct] != NULL)
free(grid[ct++]);
free(grid);
}
}
/**
* main - draw a heightmap grid and modify display of it with UI
*
* @ac: number of arguments
* @av: array of arguments
*
* Return: 1 on usage error, -1 on init errors, 0 on success
*/
int main(int ac, char *av[])
{
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
int ret = 0;
unsigned int gwidth = 0, gheight;
gridnode **grid = NULL;
FILE *file;
if (ac < 2)
{
dprintf(2, "Usage: terrain filename\n");
return (1);
}
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
dprintf(2, "SDL init error: %s\n", SDL_GetError());
return (-1);
}
window = SDL_CreateWindow("Terrain", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH,
SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL)
{
dprintf(2, "Window creation errror: %s", SDL_GetError());
return (-1);
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (renderer == NULL)
{
dprintf(2, "Renderer creation error: %s", SDL_GetError());
SDL_DestroyWindow(window);
return (-1);
}
file = fopen(av[1], "r");
if (file == NULL)
{
dprintf(2, "Failed to open grid file %s.\n", av[1]);
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
return (-1);
}
ret = init_grid(&grid, file, &gwidth, &gheight);
if (ret != -1)
drawloop(renderer, grid, gwidth, gheight);
freegrid(grid);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return (ret);
}