-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_ftl.c
104 lines (98 loc) · 2.21 KB
/
my_ftl.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
/*
** my_ftl.c for in /home/dev/C/My_FTL/bourda_j
**
** Made by BOURDALE Jules
** Login <bourda_j@etna-alternance.net>
**
** Started on Mon Nov 6 10:40:58 2017 BOURDALE Jules
** Last update Fri Nov 10 23:25:31 2017 BOURDALE Jules
*/
#include <stdlib.h>
#include <time.h>
#include "ftl.h"
#include "utils/utils.h"
#include "ship/ship.h"
#include "sector/sector.h"
#include "core/core.h"
#include "output/output.h"
#include "save/save.h"
int run_prompt(t_ship *ship, t_sector **sector)
{
char *command;
int valid_command;
int quit;
command = NULL;
valid_command = 0;
while (! valid_command)
{
my_putstr(PROMPT_GLOBAL);
free(command);
command = readLine();
valid_command = run_command(ship, command);
if (valid_command == 0)
valid_command = run_sector_command(ship, sector, command);
if (! valid_command)
my_putstr(PROMPT_GLOBAL_BAD_COMMAND);
}
quit = 0;
if (my_strcmp(command, QUIT_COMMAND) == 0)
quit = 1;
free(command);
return quit;
}
int run_game(t_ship *ship, t_sector *sectors)
{
int quit;
SDL_Renderer *renderer;
SDL_Window *window;
SDL_Event event;
if (get_options(0, "gui"))
{
if (init_SDL(&window, &renderer))
return 1;
}
quit = 0;
while (won_game(ship) == 0 && quit == 0)
{
if (get_options(0, "gui"))
{
if (SDL_WaitEvent(&event))
quit = handle_event(event, ship, §ors);
if (render(renderer, ship))
return 1;
}
else
quit = run_prompt(ship, §ors);
}
if (quit == 0)
display_win(won_game(ship), renderer);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
return 0;
}
int main(int argc, char **argv)
{
t_ship *ship;
t_sector *sectors;
if (parse_args(argc, argv))
return 1;
srand(time(NULL));
ship = create_ship();
sectors = create_sectors(10);
if (load(&ship, §ors) && get_options(0, "gui"))
enter_sector_gui(§ors);
if (run_game(ship, sectors))
my_putstr("Error when running the game\n");
else
{
free_ship(ship);
free_sectors(sectors);
if (get_options(0, "gui"))
{
TTF_Quit();
SDL_Quit();
}
return 0;
}
return 1;
}