-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
74 lines (63 loc) · 1.89 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
#define SCREEN_HEIGHT 720
#define SCREEN_WIDTH 1000
#define PIPE_HOLE_SIZE 85
#define PIPE_WIDTH 60
#define PIPE_TIP_HEIGHT 12
#define BASE_HEIGHT 100
#define GAME_SPEED 8
#define BIRD_SIZE 30
#define JUMP_SIZE 28
#define GRAVITY 5
#define N_MAX_BIRDS 200
#define N_MAX_PIPES 64
#include "SDL2/SDL.h"
#include "network.c"
#include "pipes.h"
#include "birds.h"
#include "background.h"
int main()
{
srand(time(NULL));
SDL_Window *window;
SDL_Renderer *renderer;
SDL_CreateWindowAndRenderer(SCREEN_WIDTH, SCREEN_HEIGHT, 0, &window, &renderer);
SDL_Surface *pipe_s = SDL_LoadBMP("./sources/pipe.bmp");
SDL_Surface *pipe_tip_s = SDL_LoadBMP("./sources/pipe_tip.bmp");
SDL_Surface *bird_s = SDL_LoadBMP("./sources/bird.bmp");
SDL_Surface *base_s = SDL_LoadBMP("./sources/base.bmp");
SDL_Texture *base_texture = SDL_CreateTextureFromSurface(renderer, base_s);
SDL_Texture *pipe_texture = SDL_CreateTextureFromSurface(renderer, pipe_s);
SDL_Texture *pipe_tip_texture = SDL_CreateTextureFromSurface(renderer, pipe_tip_s);
SDL_Texture *bird_texture = SDL_CreateTextureFromSurface(renderer, bird_s);
GAME_Background background = GAME_CreateBackground();
GAME_Pipe pipes[N_MAX_PIPES];
GAME_Bird birds[N_MAX_BIRDS];
GAME_CreateFirstPipe(pipes);
GAME_CreateFirstBirdGeneration(birds);
while (1)
{
SDL_SetRenderDrawColor(renderer, 85, 200, 200, 255);
SDL_RenderClear(renderer);
GAME_UpdateBirds(renderer, bird_texture, birds, pipes);
GAME_UpdatePipes(renderer, pipes, pipe_texture, pipe_tip_texture);
GAME_UpdateBackground(renderer, &background, base_texture);
if (GAME_GetNumberOfBirdsAlive(birds) == 0)
{
GAME_CalculateFit(birds);
GAME_CreateNextBirdsGeneration(birds);
GAME_RestartPipes(pipes);
};
SDL_Event event;
if (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
break;
};
};
SDL_RenderPresent(renderer);
SDL_Delay(20);
};
SDL_Quit();
return 0;
};