diff --git a/.gitignore b/.gitignore index d0c76a1..b8a74d7 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ *.swo main_check.c .vscode/**/* +./endgame + diff --git a/Makefile b/Makefile index 1d02aad..6ece795 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # ======== variables ======== # -- files and dirs -- -NAME = game +NAME = endgame INC_DIR = inc SRC_DIR = src @@ -13,7 +13,7 @@ FFLAGS = -F ./resource/frameworks -framework SDL2 -rpath ./resource/frameworks \ -F ./resource/frameworks -framework SDL2_ttf -rpath ./resource/frameworks \ -F ./resource/frameworks -framework SDL2_mixer -rpath ./resource/frameworks \ -# INC_FILES = $(wildcard $(INC_DIR)/*.h) +INC_FILES = $(wildcard $(INC_DIR)/*.h) SRC_FILES = $(wildcard $(SRC_DIR)/*.c) # -- commands -- @@ -31,8 +31,10 @@ all: $(NAME) $(NAME): $(SRC_FILES) $(INC_DIR) $(REC_DIR) @$(CC) $(C_FLAGS) $(FFLAGS) $(SRC_FILES) -I $(INC_DIR) -o $(NAME) + @printf "\r\33[2K$@\t \033[32;1mcreated\033[0m\n" uninstall: @$(RM) $(NAME) + @printf "\r\33[2K$(NAME)\t \033[32;1mdeleted\033[0m\n" reinstall: uninstall all diff --git a/game b/game deleted file mode 100755 index 8534234..0000000 Binary files a/game and /dev/null differ diff --git a/img/sound_off.png b/img/sound_off.png deleted file mode 100644 index 7c7e6cf..0000000 Binary files a/img/sound_off.png and /dev/null differ diff --git a/img/sound_on.png b/img/sound_on.png deleted file mode 100644 index c2cc3ca..0000000 Binary files a/img/sound_on.png and /dev/null differ diff --git a/inc/game.h b/inc/game.h index 5db8891..3d6a25b 100644 --- a/inc/game.h +++ b/inc/game.h @@ -1,17 +1,56 @@ #pragma once -// defines -#define MX_GRAVITY 0.02f +// === defines === #define MX_WIND_W 1600 #define MX_WIND_H 1000 -#define MX_PLANE_W 300 -#define MX_PLANE_H 100 -#define MX_RING_L 0 + +#define MX_GRAVITY 0.02f + +#define MX_RING_L 5 +#define MX_RING_W 40 +#define MX_RING_H 40 +120 + #define MX_RANDOM_X ((rand() % (MX_WIND_W + 1 - 0)) +0) #define MX_RANDOM_Y ((rand() % (MX_WIND_H + 1 - 0)) +0) + +#define MX_PLANE_W 300/2 +#define MX_PLANE_H 240/2 #define MX_PLANE_ST_X (MX_WIND_W / 2 - MX_PLANE_W / 2) #define MX_PLANE_ST_Y (MX_WIND_H / 2 - MX_PLANE_H / 2) -// #define MX_PLANE_ST_Y + +#define MX_BTN_W 300 +#define MX_BTN_H 60 +#define MX_BTN_X ((MX_WIND_W - MX_BTN_W) / 2) +#define MX_BTN_Y (MX_WIND_H / 2 - MX_BTN_H) +#define HEART_SIZE 30 +#define GAME_LIFE 3 + +#define MX_R_MENU 0 +#define MX_R_GAME 1 +#define MX_R_OVER 2 +#define MX_R_BOARD 3 +#define MX_R_EXIT -1 + +#define HEART "resource/img/heart.png" +#define HEART_EMPTY "resource/img/heart_empty.png" + +#define CIRCLE "resource/img/circles/circle_orange.png" +#define CIRCLE2 "resource/img/circles/circle_green.png" + +#define PLANE "resource/img/car_orange.png" +#define PLANE2 "resource/img/car_green.png" + +#define BACKGROUND_IMG "resource/img/space_bg.jpg" + +#define FONT_MAIN "resource/font/Sansation-Bold.ttf" +#define MUSIC_MENU "resource/audio/space_oddity.mp3" + +// colors +#define BLACK {0, 0, 0, 0} +#define WHITE {255, 255, 255, 0} +#define TEAL {10, 128, 128, 0} +#define ORANGE {255, 100, 0, 0} +#define GREEN {144, 178, 71, 0} // includes #include @@ -29,47 +68,101 @@ #include #include +typedef enum e_scenestatus { + MENU_STATE, + GAME_STATE, + GAMEOVER_STATE, + MULTIPLAYER_STATE, + LEADERBOARD_STATE, + EXIT_STATE +} e_scenes; + // structs -// TODO razobratsia s tipami na rayone + +typedef struct s_calculation { + int temp; + int dec; + int rem; + int size; + int i; +} t_calculation; + typedef struct s_world { float x; float y; float w; float h; - float cnt; + int cnt; float dy; -} t_world; + int dw; + int life; +} t_world; typedef struct s_state { // objects t_world plane; + t_world plane2; t_world gate; + t_world gate2; t_world space; + t_world heart; + t_world heart_empty; // imgs SDL_Texture *bg; SDL_Texture *car; - SDL_Texture *gate_img[2]; + SDL_Texture *car2; + SDL_Texture *gate_img; + SDL_Texture *gate2_img; + SDL_Texture *heart_img; + SDL_Texture *heart_empty_img; + SDL_Texture *life_img; SDL_Renderer *renderer; Mix_Music *bg_music; + TTF_Font *font; + int play; + int exit; + + e_scenes status; } t_state; // funcs char *mx_strcpy(char *dst, const char *src); +void mx_do_render(SDL_Renderer *rend, t_state *game); +void mx_do_render_mult(SDL_Renderer *rend, t_state *game); + +void mx_play_game(t_state *game); +void mx_play_game_mult(t_state *game); + +void mx_load_game(t_state *game); + int mx_strlen(const char *s); -int mx_process_events(SDL_Window *window, t_state *game); -int mx_check_pass(t_state *game); +int mx_check_pass(t_world *gate, t_world *plane); -void mx_err_check(void *target); void mx_printerr(const char *s); -void mx_do_render(SDL_Renderer *rend, t_state *game); -void mx_scale_gate(t_state *game); -void mx_scale_car(t_state *game, int direction); -void mx_load_game(t_state *game); -void process(t_state *game); +void mx_is_over(t_world *plane); +void mx_scale_car(t_world *plane); +void mx_scale_gate(t_world *gate); +void mx_gravitation (t_world *plane); + +char *mx_int_to_str(int n); +void mx_initial_state(t_world *gate, t_world *plane); + + +e_scenes mx_menu(SDL_Renderer *renderer); +e_scenes mx_leaderboard(SDL_Renderer *renderer); +e_scenes mx_game(SDL_Window *window, t_state *game); +e_scenes mx_multiplayer(SDL_Window *window, t_state *game); +e_scenes mx_gameover(SDL_Renderer *renderer); + + + +void mx_draw_text( SDL_Color color, int x, int y, char *text, + SDL_Renderer *renderer, TTF_Font *font); + diff --git a/resource/audio/space_oddity.mp3 b/resource/audio/space_oddity.mp3 index e573442..f21132f 100644 Binary files a/resource/audio/space_oddity.mp3 and b/resource/audio/space_oddity.mp3 differ diff --git a/resource/audio/space_sampel.mp3 b/resource/audio/space_sampel.mp3 deleted file mode 100644 index 498b6e6..0000000 Binary files a/resource/audio/space_sampel.mp3 and /dev/null differ diff --git a/resource/font/Russo_One.ttf b/resource/font/Russo_One.ttf new file mode 100644 index 0000000..8b47097 Binary files /dev/null and b/resource/font/Russo_One.ttf differ diff --git a/resource/font/Sansation-Bold.ttf b/resource/font/Sansation-Bold.ttf new file mode 100755 index 0000000..4ea3585 Binary files /dev/null and b/resource/font/Sansation-Bold.ttf differ diff --git a/resource/img/Car-Back-Red-icon.png b/resource/img/Car-Back-Red-icon.png deleted file mode 100644 index d58191c..0000000 Binary files a/resource/img/Car-Back-Red-icon.png and /dev/null differ diff --git a/resource/img/GameOver.png b/resource/img/GameOver.png new file mode 100644 index 0000000..6503405 Binary files /dev/null and b/resource/img/GameOver.png differ diff --git a/resource/img/buttons/exit.png b/resource/img/buttons/exit.png deleted file mode 100644 index c83a5bf..0000000 Binary files a/resource/img/buttons/exit.png and /dev/null differ diff --git a/resource/img/buttons/restart.png b/resource/img/buttons/restart.png deleted file mode 100644 index 3e84a78..0000000 Binary files a/resource/img/buttons/restart.png and /dev/null differ diff --git a/resource/img/buttons/resume.png b/resource/img/buttons/resume.png deleted file mode 100644 index a2845f5..0000000 Binary files a/resource/img/buttons/resume.png and /dev/null differ diff --git a/resource/img/buttons/rocket.png b/resource/img/buttons/rocket.png deleted file mode 100644 index 358f3f9..0000000 Binary files a/resource/img/buttons/rocket.png and /dev/null differ diff --git a/resource/img/buttons/start.png b/resource/img/buttons/start.png deleted file mode 100644 index 544f1ac..0000000 Binary files a/resource/img/buttons/start.png and /dev/null differ diff --git a/resource/img/car_blue.png b/resource/img/car_blue.png new file mode 100644 index 0000000..3bdf96b Binary files /dev/null and b/resource/img/car_blue.png differ diff --git a/resource/img/car_green.png b/resource/img/car_green.png new file mode 100644 index 0000000..fbe8c04 Binary files /dev/null and b/resource/img/car_green.png differ diff --git a/resource/img/car_orange.png b/resource/img/car_orange.png new file mode 100644 index 0000000..79ea4b4 Binary files /dev/null and b/resource/img/car_orange.png differ diff --git a/resource/img/circle.png b/resource/img/circle.png deleted file mode 100644 index 39d9469..0000000 Binary files a/resource/img/circle.png and /dev/null differ diff --git a/resource/img/circle_green .png b/resource/img/circle_green .png deleted file mode 100644 index 868f135..0000000 Binary files a/resource/img/circle_green .png and /dev/null differ diff --git a/img/circles/circle_blue.png b/resource/img/circles/circle_blue.png similarity index 100% rename from img/circles/circle_blue.png rename to resource/img/circles/circle_blue.png diff --git a/img/circles/circle_green.png b/resource/img/circles/circle_green.png similarity index 100% rename from img/circles/circle_green.png rename to resource/img/circles/circle_green.png diff --git a/img/circles/circle_orange.png b/resource/img/circles/circle_orange.png similarity index 100% rename from img/circles/circle_orange.png rename to resource/img/circles/circle_orange.png diff --git a/img/circles/circle_pink.png b/resource/img/circles/circle_pink.png similarity index 100% rename from img/circles/circle_pink.png rename to resource/img/circles/circle_pink.png diff --git a/img/circles/circle_red.png b/resource/img/circles/circle_red.png similarity index 100% rename from img/circles/circle_red.png rename to resource/img/circles/circle_red.png diff --git a/resource/img/cropped_car.png b/resource/img/cropped_car.png deleted file mode 100644 index 6cea598..0000000 Binary files a/resource/img/cropped_car.png and /dev/null differ diff --git a/resource/img/empty.PNG b/resource/img/empty.PNG new file mode 100644 index 0000000..8d075da Binary files /dev/null and b/resource/img/empty.PNG differ diff --git a/resource/img/empty_back.png b/resource/img/empty_back.png new file mode 100644 index 0000000..f6f3b7b Binary files /dev/null and b/resource/img/empty_back.png differ diff --git a/resource/img/heart.png b/resource/img/heart.png new file mode 100644 index 0000000..985a2da Binary files /dev/null and b/resource/img/heart.png differ diff --git a/resource/img/heart_empty.png b/resource/img/heart_empty.png new file mode 100644 index 0000000..ecd38d5 Binary files /dev/null and b/resource/img/heart_empty.png differ diff --git a/resource/img/main_page1.png b/resource/img/main_page1.png deleted file mode 100644 index e546605..0000000 Binary files a/resource/img/main_page1.png and /dev/null differ diff --git a/resource/img/minio.bmp b/resource/img/minio.bmp deleted file mode 100644 index 6d14510..0000000 Binary files a/resource/img/minio.bmp and /dev/null differ diff --git a/resource/img/new_red_car.png b/resource/img/new_red_car.png deleted file mode 100644 index b4fe0d2..0000000 Binary files a/resource/img/new_red_car.png and /dev/null differ diff --git a/resource/img/space_car.png b/resource/img/space_car.png deleted file mode 100644 index 5e1eb5f..0000000 Binary files a/resource/img/space_car.png and /dev/null differ diff --git a/resource/img/star.png b/resource/img/star.png deleted file mode 100644 index 01d9ac6..0000000 Binary files a/resource/img/star.png and /dev/null differ diff --git a/resource/img/starfield_1922x1037.jpg b/resource/img/starfield_1922x1037.jpg deleted file mode 100644 index fc8ee87..0000000 Binary files a/resource/img/starfield_1922x1037.jpg and /dev/null differ diff --git a/src/main.c b/src/main.c index 684461c..2b9f141 100644 --- a/src/main.c +++ b/src/main.c @@ -3,49 +3,71 @@ int main(void) { t_state game; SDL_Window *wind = NULL; - SDL_Renderer *rend = NULL; - int quit; + // SDL_Renderer *rend = NULL; + // int quit; SDL_Init(SDL_INIT_EVERYTHING); IMG_Init(IMG_INIT_PNG | IMG_INIT_JPG); SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO); Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048); + SDL_ShowCursor(SDL_DISABLE); TTF_Init(); - if (SDL_Init(SDL_INIT_VIDEO)) { - mx_printerr("ne paluchilos"); - exit(1); - } - - wind = SDL_CreateWindow("Kuku", SDL_WINDOWPOS_UNDEFINED, + wind = SDL_CreateWindow("Ď€Lot", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, MX_WIND_W, MX_WIND_H, 0); - rend = SDL_CreateRenderer( + game.renderer = SDL_CreateRenderer( wind, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); - game.renderer = rend; mx_load_game(&game); - - quit = 0; - srand(time(0)); - Mix_PlayMusic(game.bg_music, -1); - while (!quit) { - quit = mx_process_events(wind, &game); + game.status = MENU_STATE; + game.exit = 0; + srand(time(0)); - mx_scale_gate(&game); - // mx_collision_detect(rend, &game); + game.play = MX_R_MENU; - process(&game); + while (!game.exit) { - mx_do_render(game.renderer, &game); + switch (game.status) { + case MENU_STATE: + game.status = mx_menu(game.renderer); + break; + case GAME_STATE: + game.status = mx_game(wind, &game); + break; + case GAMEOVER_STATE: + game.status = mx_gameover(game.renderer); + break; + case MULTIPLAYER_STATE: + game.status = mx_multiplayer(wind, &game); + break; + case LEADERBOARD_STATE: + game.status = mx_leaderboard(game.renderer); + break; + case EXIT_STATE: + game.exit = 1; + break; + } } SDL_DestroyTexture(game.bg); SDL_DestroyTexture(game.car); + SDL_DestroyTexture(game.car2); + SDL_DestroyTexture(game.gate_img); + SDL_DestroyTexture(game.gate2_img); + SDL_DestroyTexture(game.heart_img); + SDL_DestroyTexture(game.heart_empty_img); + SDL_DestroyTexture(game.life_img); + SDL_DestroyWindow(wind); SDL_DestroyRenderer(game.renderer); + + IMG_Quit(); + TTF_Quit(); + Mix_CloseAudio(); + TTF_CloseFont(game.font); SDL_Quit(); return 0; diff --git a/src/mx_check_pass.c b/src/mx_check_pass.c index fad24b4..d4b2ff9 100644 --- a/src/mx_check_pass.c +++ b/src/mx_check_pass.c @@ -1,21 +1,16 @@ #include "game.h" -int mx_check_pass(t_state *game) { - float pl_max_x = game->plane.x + game->plane.w; - float pl_max_y = game->plane.y + game->plane.h; - float gt_in_max_x = game->gate.x - MX_RING_L + game->gate.w - MX_RING_L; - float gt_in_max_y = game->gate.y - MX_RING_L + game->gate.h - MX_RING_L; +int mx_check_pass(t_world *gate, t_world *plane) { + float pl_max_x = plane->x + plane->w; + float pl_max_y = plane->y + plane->h; - if (game->plane.x > game->gate.x && pl_max_x < gt_in_max_x) { - if (game->plane.y > game->gate.y && pl_max_y < gt_in_max_y) { - return -1; + float gt_in_max_x = gate->x - MX_RING_L + gate->w - MX_RING_L; + float gt_in_max_y = gate->y - MX_RING_L + gate->h - MX_RING_L; + + if (plane->x > gate->x && pl_max_x < gt_in_max_x) { + if (plane->y > gate->y && pl_max_y < gt_in_max_y) { + return 1; } } - return 1; + return -1; } - - -// TODO music && sfx -// TODO dying -// TODO wings ??? -// TODO consts and responcive scaling diff --git a/src/mx_do_render.c b/src/mx_do_render.c index 1cad0f1..364f961 100644 --- a/src/mx_do_render.c +++ b/src/mx_do_render.c @@ -1,7 +1,6 @@ #include "game.h" void mx_do_render(SDL_Renderer *rend, t_state *game) { - SDL_SetRenderDrawColor(rend, 4, 21, 66, 1); SDL_RenderClear(rend); @@ -11,19 +10,13 @@ void mx_do_render(SDL_Renderer *rend, t_state *game) { game->space.h}; SDL_RenderCopy(rend, game->bg, NULL, &bg_rect); - // SDL_SetRenderDrawColor(rend, 25, 50, 200, 255); - // SDL_Rect tor_pb = {game->gate.x + MX_RING_L , - // game->gate.y + 10, - // game->gate.w - MX_RING_L*2, - // game->gate.h -20}; - // SDL_RenderFillRect(rend, &tor_pb); - SDL_SetRenderDrawColor(rend, 25, 50, 200, 255); SDL_Rect tor_p = {game->gate.x, game->gate.y, game->gate.w, game->gate.h}; - SDL_RenderCopy(rend, game->gate_img[0], NULL, &tor_p); + // SDL_RenderFillRect(rend, &tor_p); + SDL_RenderCopy(rend, game->gate_img, NULL, &tor_p); SDL_SetRenderDrawColor(rend, 250, 50, 20, 0); SDL_Rect plane_p = {game->plane.x, @@ -32,5 +25,25 @@ void mx_do_render(SDL_Renderer *rend, t_state *game) { game->plane.h}; // SDL_RenderFillRect(rend, &plane_p); SDL_RenderCopy(rend, game->car, NULL, &plane_p); + + for (int i = GAME_LIFE; i > 0; i--) { + int heart_x = HEART_SIZE + HEART_SIZE * i; + game->life_img = (i <= game->plane.life) + ? game->heart_img + : game->heart_empty_img; + + SDL_Rect heart_p = {heart_x, HEART_SIZE * 2, HEART_SIZE, HEART_SIZE}; + SDL_RenderCopy(rend, game->life_img, NULL, &heart_p); + } + + SDL_Color score_color = ORANGE; + + char *score = mx_int_to_str(game->plane.cnt); + mx_draw_text(score_color, 20, 20, "SCORE: ", game->renderer, game->font); + mx_draw_text(score_color, 120, 20, score, game->renderer, game->font); + free(score); + score = NULL; + SDL_RenderPresent(rend); } + diff --git a/src/mx_do_render_mult.c b/src/mx_do_render_mult.c new file mode 100644 index 0000000..9ecc68a --- /dev/null +++ b/src/mx_do_render_mult.c @@ -0,0 +1,99 @@ +#include "game.h" + +void mx_do_render_mult(SDL_Renderer *rend, t_state *game) { + SDL_SetRenderDrawColor(rend, 4, 21, 66, 1); + SDL_RenderClear(rend); + + SDL_Rect bg_rect = {game->space.x, + game->space.y, + game->space.w, + game->space.h}; + SDL_RenderCopy(rend, game->bg, NULL, &bg_rect); + + SDL_SetRenderDrawColor(rend, 25, 50, 200, 255); + SDL_Rect tor_p = {game->gate.x, + game->gate.y, + game->gate.w, + game->gate.h}; + SDL_RenderCopy(rend, game->gate_img, NULL, &tor_p); + + SDL_SetRenderDrawColor(rend, 25, 50, 200, 255); + SDL_Rect tor_p2 = {game->gate2.x, + game->gate2.y, + game->gate2.w, + game->gate2.h}; + SDL_RenderCopy(rend, game->gate2_img, NULL, &tor_p2); + + SDL_SetRenderDrawColor(rend, 250, 50, 20, 0); + SDL_Rect plane_p = {game->plane.x, + game->plane.y, + game->plane.w, + game->plane.h}; + SDL_RenderCopy(rend, game->car, NULL, &plane_p); + + SDL_SetRenderDrawColor(rend, 250, 50, 20, 0); + SDL_Rect plane_p2 = {game->plane2.x, + game->plane2.y, + game->plane2.w, + game->plane2.h}; + SDL_RenderCopy(rend, game->car2, NULL, &plane_p2); + + for (int i = GAME_LIFE; i > 0; i--) { + int heart_x = HEART_SIZE + HEART_SIZE * i; + game->life_img = (i <= game->plane.life) + ? game->heart_img + : game->heart_empty_img; + + SDL_Rect heart_1_p = {heart_x, HEART_SIZE * 2, HEART_SIZE, HEART_SIZE}; + SDL_RenderCopy(rend, game->life_img, NULL, &heart_1_p); + } + + for (int i = GAME_LIFE; i > 0; i--) { + int heart_x = MX_WIND_W - HEART_SIZE * 2 - HEART_SIZE * i; + game->life_img = (i <= game->plane2.life) + ? game->heart_img + : game->heart_empty_img; + + SDL_Rect heart_2_p = {heart_x, HEART_SIZE * 2, HEART_SIZE, HEART_SIZE}; + SDL_RenderCopy(rend, game->life_img, NULL, &heart_2_p); + } + + SDL_Color score_color_or = GREEN; + + char *score_1 = mx_int_to_str(game->plane.cnt); + mx_draw_text(score_color_or, + 20, + 20, + "SCORE: ", + game->renderer, + game->font); + mx_draw_text(score_color_or, + 120, + 20, + score_1, + game->renderer, + game->font); + free(score_1); + score_1 = NULL; + + + SDL_Color score_color_te = ORANGE; + + char *score_2 = mx_int_to_str(game->plane2.cnt); + mx_draw_text(score_color_te, + MX_WIND_W - 220, + 20, + "SCORE: ", + game->renderer, + game->font); + mx_draw_text(score_color_te, + MX_WIND_W - 120, + 20, + score_2, + game->renderer, + game->font); + free(score_2); + score_2 = NULL; + + SDL_RenderPresent(rend); +} diff --git a/src/mx_draw_text.c b/src/mx_draw_text.c new file mode 100644 index 0000000..0ac32ae --- /dev/null +++ b/src/mx_draw_text.c @@ -0,0 +1,20 @@ +#include "game.h" + +void mx_draw_text(SDL_Color color, + int x, + int y, + char *text, + SDL_Renderer *renderer, + TTF_Font *font) { + SDL_Surface *text_surface = TTF_RenderText_Solid(font, text, color); + SDL_Texture *textureGameName = + SDL_CreateTextureFromSurface(renderer, text_surface); + + SDL_Rect text_rect = {x, y, 1, 1}; + SDL_QueryTexture(textureGameName, NULL, NULL, &text_rect.w, &text_rect.h); + SDL_RenderCopy(renderer, textureGameName, NULL, &text_rect); + + SDL_FreeSurface(text_surface); + text_surface = 0; + SDL_DestroyTexture(textureGameName); +} diff --git a/src/mx_err_check.c b/src/mx_err_check.c deleted file mode 100644 index 64c6722..0000000 --- a/src/mx_err_check.c +++ /dev/null @@ -1,9 +0,0 @@ -#include "game.h" - -void mx_err_check(void *itm){ - - if (itm == NULL) { - mx_printerr(SDL_GetError()); - exit (1); - } -} diff --git a/src/mx_game.c b/src/mx_game.c new file mode 100644 index 0000000..3ecaede --- /dev/null +++ b/src/mx_game.c @@ -0,0 +1,79 @@ +#include "game.h" + +e_scenes mx_game(SDL_Window *window, t_state *game) { + SDL_Event e; + e_scenes status = MENU_STATE; + int running = 1; + game->plane.life = 3; + game->plane.cnt = 0; + while (running) { + if (SDL_PollEvent(&e)) { + switch (e.type) { + case SDL_WINDOWEVENT_CLOSE: + if (window) { + SDL_DestroyWindow(window); + window = NULL; + status = EXIT_STATE; + } + break; + case SDL_KEYDOWN: + switch (e.key.keysym.sym) { + case SDLK_q: + return GAMEOVER_STATE; + // game.exit = 0; + break; + case SDLK_UP: + game->plane.dy = -2; + break; + + case SDLK_w: + game->plane2.dy = -2; + break; + + default: + break; + } + break; + case SDL_QUIT: + status = EXIT_STATE; + break; + default: + break; + } + } + + const Uint8 *state = SDL_GetKeyboardState(NULL); + if (state[SDL_SCANCODE_LEFT]) { + game->plane.x -= 5; + } + if (state[SDL_SCANCODE_RIGHT]) { + game->plane.x += 5; + } + if(state[SDL_SCANCODE_UP]){ + game->plane.y -= 5; + } + if(state[SDL_SCANCODE_DOWN]){ + game->plane.y += 5; + } + if (state[SDL_SCANCODE_A]) { + game->plane2.x -= 5; + } + if (state[SDL_SCANCODE_D]) { + game->plane2.x += 5; + } + if(state[SDL_SCANCODE_W]){ + game->plane2.y -= 5; + } + if(state[SDL_SCANCODE_S]){ + game->plane2.y += 5; + } + mx_play_game(game); + mx_do_render(game->renderer, game); + + + if (game->plane.life <= 0) + return GAMEOVER_STATE; + } + + return status; +} diff --git a/src/mx_gameover.c b/src/mx_gameover.c new file mode 100644 index 0000000..ce6ebee --- /dev/null +++ b/src/mx_gameover.c @@ -0,0 +1,85 @@ +#include "game.h" + +e_scenes mx_gameover(SDL_Renderer *renderer) { + int running = 1; + SDL_Event event; + e_scenes result = MENU_STATE; + int index_menu = 1; + TTF_Init(); + if (TTF_Init() == -1) { + printf("TTF_Init: %s\n", TTF_GetError()); + exit(1); + } + + TTF_Font *font = TTF_OpenFont("resource/font/Russo_One.ttf", 35); + SDL_Color color = TEAL; + SDL_Color color_selected = ORANGE; + + SDL_Texture *menu_bg = IMG_LoadTexture(renderer, "resource/img/GameOver.png"); + SDL_Texture *btn_img = IMG_LoadTexture(renderer, "resource/img/empty.PNG"); + + + SDL_Rect backgroundRect = {0, 0, MX_WIND_W, MX_WIND_H}; + + SDL_Rect replay_btn = {MX_BTN_X - MX_BTN_W - 20, + MX_BTN_X + MX_BTN_H, + MX_BTN_W, + MX_BTN_H}; + SDL_Rect menu_btn = {MX_BTN_X + MX_BTN_W + 20, + MX_BTN_X + MX_BTN_H, + MX_BTN_W, + MX_BTN_H}; + + while (running) { + while (SDL_PollEvent(&event)) { + if (event.type == SDL_KEYUP) { + if (event.key.keysym.sym == SDLK_ESCAPE) { + return EXIT_STATE; + } + if (event.key.keysym.sym == SDLK_LEFT) { + index_menu = 1; + + } + if (event.key.keysym.sym == SDLK_RIGHT) { + + index_menu = 2; + + } + if (event.key.keysym.sym == SDLK_RETURN) { + + SDL_DestroyTexture(btn_img); + SDL_DestroyTexture(menu_bg); + TTF_CloseFont(font); + + if (index_menu == 1) + return GAME_STATE; + else + return MENU_STATE; + } + } + } + SDL_RenderCopy(renderer, menu_bg, NULL, &backgroundRect); + + SDL_RenderCopy(renderer, btn_img, NULL, &replay_btn); + SDL_RenderCopy(renderer, btn_img, NULL, &menu_btn); + + + if (index_menu == 1) { + mx_draw_text(color_selected, replay_btn.x + 75, replay_btn.y+5, "REPLAY", renderer, font); + mx_draw_text(color, menu_btn.x + 90 , menu_btn.y+5, "MENU", renderer, font); + + } else if (index_menu == 2) { + mx_draw_text(color, replay_btn.x + 75, replay_btn.y+5, "REPLAY", renderer, font); + mx_draw_text(color_selected, menu_btn.x + 90 , menu_btn.y+5, "MENU", renderer, font); + + } + + usleep(100); + SDL_RenderPresent(renderer); + } + SDL_DestroyTexture(btn_img); + SDL_DestroyTexture(menu_bg); + TTF_CloseFont(font); + + return result; +} diff --git a/src/mx_gravity.c b/src/mx_gravitation.c similarity index 54% rename from src/mx_gravity.c rename to src/mx_gravitation.c index 54da72d..bd06e6b 100644 --- a/src/mx_gravity.c +++ b/src/mx_gravitation.c @@ -1,8 +1,7 @@ #include "game.h" -void process(t_state *game) { - t_world *plane = &game->plane; - plane->y += plane->dy; +void mx_gravitation (t_world *plane) { + plane->y += plane->dy; plane->dy += MX_GRAVITY; } diff --git a/src/mx_initial_state.c b/src/mx_initial_state.c new file mode 100644 index 0000000..1869899 --- /dev/null +++ b/src/mx_initial_state.c @@ -0,0 +1,11 @@ +#include "game.h" + +void mx_initial_state(t_world *gate, t_world *plane) { + gate->x = MX_RANDOM_X; + gate->y = MX_RANDOM_Y; + gate->w = MX_RING_W; + gate->h = MX_RING_H ; + + plane->x = MX_PLANE_ST_X; + plane->dw = 0; +} diff --git a/src/mx_int_to_str.c b/src/mx_int_to_str.c new file mode 100644 index 0000000..6d900d8 --- /dev/null +++ b/src/mx_int_to_str.c @@ -0,0 +1,29 @@ +#include "game.h" + +char *mx_int_to_str(int n) { + t_calculation to_str = {n, n, 0, 0, 0}; + char *result = NULL; + + if (n >= 0) { + for(to_str.size = 0; to_str.temp > 0; to_str.size ++) { + to_str.temp /= 10; + } + + result = (char*)malloc(to_str.size + 1); + if (result == NULL) + return NULL; + + for (int i = to_str.size; i >= 0; i--) { + if (i == to_str.size) { + result[i] = '\0'; + } + else { + to_str.temp = to_str.dec; + to_str.dec = to_str.temp / 10; + to_str.rem = to_str.temp % 10; + result[i] = to_str.rem + 48; + } + } + } + return result; +} diff --git a/src/mx_is_over.c b/src/mx_is_over.c new file mode 100644 index 0000000..da67a37 --- /dev/null +++ b/src/mx_is_over.c @@ -0,0 +1,15 @@ +#include "game.h" + +void mx_is_over(t_world *plane) { + + switch (plane->dw) { + case 1: + plane->cnt++; + break; + case -1: + plane->life--; + break; + default: + break; + } +} diff --git a/src/mx_leaderboard.c b/src/mx_leaderboard.c new file mode 100644 index 0000000..3b8af32 --- /dev/null +++ b/src/mx_leaderboard.c @@ -0,0 +1,99 @@ +#include "game.h" +// #include + +e_scenes mx_leaderboard(SDL_Renderer *renderer) { + SDL_Event event; + int running = 1; + e_scenes result = MENU_STATE; + int index = 1; + + TTF_Font *font = TTF_OpenFont("resource/font/Russo_One.ttf", 35); + SDL_Color color = BLACK; + SDL_Color color_selected = WHITE; + + SDL_Texture *menu_bg = + IMG_LoadTexture(renderer, "resource/img/empty_back.png"); + SDL_Texture *btn_img = IMG_LoadTexture(renderer, "resource/img/empty.PNG"); + + SDL_Rect backgroundRect = {0, 0, MX_WIND_W, MX_WIND_H}; + // SDL_Rect play_btn = {MX_BTN_X, MX_BTN_Y, MX_BTN_W , MX_BTN_H}; + // SDL_Rect exit_btn = {MX_BTN_X, MX_BTN_Y + MX_BTN_H, MX_BTN_W, MX_BTN_H}; + // SDL_Rect board_btn = {MX_BTN_X, MX_BTN_Y + MX_BTN_H / 2, MX_BTN_W, MX_BTN_H}; + SDL_Rect play_btn = {MX_BTN_X, MX_BTN_Y - 70, MX_BTN_W, MX_BTN_H}; + SDL_Rect board_btn = {MX_BTN_X, MX_BTN_Y -5 , MX_BTN_W, + MX_BTN_H}; + SDL_Rect exit_btn = {MX_BTN_X, MX_BTN_Y + 60 , MX_BTN_W, + MX_BTN_H}; + while (running) { + while (SDL_PollEvent(&event)) { + if (event.type == SDL_KEYUP) { + if (event.key.keysym.sym == SDLK_UP) { + index = (index > 1) ? (index - 1) : 3; + + } + if (event.key.keysym.sym == SDLK_DOWN) { + + index = (index < 3) ? (index + 1) : 1; + + } + if (event.key.keysym.sym == SDLK_ESCAPE) { + + SDL_DestroyTexture(btn_img); + SDL_DestroyTexture(menu_bg); + TTF_CloseFont(font); + + return MENU_STATE; + } + } + } + SDL_RenderCopy(renderer, menu_bg, NULL, &backgroundRect); + + SDL_RenderCopy(renderer, btn_img, NULL, &play_btn); + SDL_RenderCopy(renderer, btn_img, NULL, &board_btn); + SDL_RenderCopy(renderer, btn_img, NULL, &exit_btn); + + if (index == 1) { + // mx_draw_text(color_selected, play_btn.x + 40, play_btn.y+50, "FIRST", renderer, font); + // mx_draw_text(color, board_btn.x + 40 , board_btn.y+50, "SECOND", renderer, font); + // mx_draw_text(color, exit_btn.x + 40 , exit_btn.y+50, "THIRD", renderer, font); + mx_draw_text(color_selected, play_btn.x + 95, play_btn.y + 5, + "FIRST", renderer, font); + mx_draw_text(color, board_btn.x + 80, board_btn.y + 5, + "SECOND", renderer, font); + mx_draw_text(color, exit_btn.x + 90, exit_btn.y + 5, + "THIRD", renderer, font); + + } else if (index == 2) { + // mx_draw_text(color, play_btn.x + 40, play_btn.y+50, "FIRST", renderer, font); + // mx_draw_text(color_selected,board_btn.x + 40 , board_btn.y+50, "SECOND", renderer, font); + // mx_draw_text(color, exit_btn.x + 40, exit_btn.y+50, "THIRD", renderer, font); + + mx_draw_text(color, play_btn.x + 95, play_btn.y + 5, + "FIRST", renderer, font); + mx_draw_text(color_selected, board_btn.x + 80, board_btn.y + 5, + "SECOND", renderer, font); + mx_draw_text(color, exit_btn.x + 90, exit_btn.y + 5, + "THIRD", renderer, font); + + + } else if (index == 3) { + // mx_draw_text(color, play_btn.x + 40, play_btn.y+50, "FIRST", renderer, font); + // mx_draw_text(color, board_btn.x + 40, board_btn.y+50, "SECOND", renderer, font); + // mx_draw_text(color_selected, exit_btn.x + 40, exit_btn.y+50, "THIRD", renderer, font); + mx_draw_text(color, play_btn.x + 95, play_btn.y + 5, + "FIRST", renderer, font); + mx_draw_text(color, board_btn.x + 80, board_btn.y + 5, + "SECOND", renderer, font); + mx_draw_text(color_selected, exit_btn.x + 90, exit_btn.y + 5, + "THIRD", renderer, font); + } + + usleep(100); + SDL_RenderPresent(renderer); + } + SDL_DestroyTexture(btn_img); + SDL_DestroyTexture(menu_bg); + TTF_CloseFont(font); + + return result; +} diff --git a/src/mx_load_game.c b/src/mx_load_game.c index 69f9f52..2267ab1 100644 --- a/src/mx_load_game.c +++ b/src/mx_load_game.c @@ -3,18 +3,19 @@ void mx_load_game(t_state *game) { SDL_Surface *surf = NULL; - //load BG musix - Mix_Music *namemusic = Mix_LoadMUS("resource/audio/space_oddity.mp3"); + Mix_Music *namemusic = Mix_LoadMUS(MUSIC_MENU); if(!namemusic) { - printf("Mix_LoadMUS(\"namemusic.mp3\"): %s\n", Mix_GetError()); + printf("Mix_LoadMUS(\"music.mp3\"): %s\n", Mix_GetError()); // this might be a critical error... } game->bg_music = namemusic; + game->font = TTF_OpenFont(FONT_MAIN, 25); + // space bg: - surf = IMG_Load("resource/img/space_bg.jpg"); + surf = IMG_Load(BACKGROUND_IMG); if (surf == NULL) { mx_printerr("can't find space bg"); SDL_Quit(); @@ -24,28 +25,27 @@ void mx_load_game(t_state *game) { game->bg = SDL_CreateTextureFromSurface(game->renderer, surf); // orange circle - surf = IMG_Load("resource/img/circle.png"); + surf = IMG_Load(CIRCLE); if (surf == NULL) { mx_printerr("Can't load orange gate"); SDL_Quit(); exit(1); } - game->gate_img[0] = SDL_CreateTextureFromSurface(game->renderer, surf); - - // green circle - surf = IMG_Load("resource/img/circle_green .png"); + game->gate_img = SDL_CreateTextureFromSurface(game->renderer, surf); + + surf = IMG_Load(CIRCLE2); if (surf == NULL) { - mx_printerr(" Can't load green gate"); + mx_printerr("Can't load orange gate"); SDL_Quit(); exit(1); } - game->gate_img[1] = SDL_CreateTextureFromSurface(game->renderer, surf); - + game->gate2_img = SDL_CreateTextureFromSurface(game->renderer, surf); + // car - surf = IMG_Load("resource/img/space_car.png"); + surf = IMG_Load(PLANE); if (surf == NULL) { mx_printerr("Can't load car"); SDL_Quit(); @@ -54,6 +54,34 @@ void mx_load_game(t_state *game) { game->car = SDL_CreateTextureFromSurface(game->renderer, surf); + surf = IMG_Load(PLANE2); + if (surf == NULL) { + mx_printerr("Can't load car"); + SDL_Quit(); + exit(1); + } + + game->car2 = SDL_CreateTextureFromSurface(game->renderer, surf); + + surf = IMG_Load(HEART); + if (surf == NULL) { + mx_printerr("Can't load heart"); + SDL_Quit(); + exit(1); + } + game->heart_img = SDL_CreateTextureFromSurface(game->renderer, surf); + + surf = IMG_Load(HEART_EMPTY); + if (surf == NULL) { + mx_printerr("Can't load empty heart"); + SDL_Quit(); + exit(1); + } + game->heart_empty_img = SDL_CreateTextureFromSurface(game->renderer, surf); + + + + SDL_FreeSurface(surf); game->plane.x = MX_PLANE_ST_X; @@ -61,16 +89,23 @@ void mx_load_game(t_state *game) { game->plane.w = MX_PLANE_W; game->plane.h = MX_PLANE_H; game->plane.dy = 0; - // game->plane.cnt = 0; + game->plane.dw = 0; + game->plane.cnt = 0; + + game->plane2 = game->plane; game->gate.x = MX_RANDOM_X; game->gate.y = MX_RANDOM_Y; - game->gate.w = 50; - game->gate.h = 50; - // game->gate.cnt = 1; - + game->gate.w = MX_RING_W; + game->gate.h = MX_RING_H; game->space.x = 0; game->space.y = 0; game->space.w = MX_WIND_W; game->space.h = MX_WIND_H; + game->plane.life = GAME_LIFE; + game->plane2.life = GAME_LIFE; + + game->heart.w = 30; + game->heart.h = 30; + // SDL_FreeSurface(surf); } diff --git a/src/mx_menu.c b/src/mx_menu.c new file mode 100644 index 0000000..82e077f --- /dev/null +++ b/src/mx_menu.c @@ -0,0 +1,139 @@ +#include "game.h" + +e_scenes mx_menu(SDL_Renderer *renderer) { + int running = 1; + SDL_Event event; + e_scenes result = MENU_STATE; + int index_menu = 1; + + + if (TTF_Init() == -1) { + printf("TTF_Init: %s\n", TTF_GetError()); + exit(1); + } + + TTF_Font *font = TTF_OpenFont("resource/font/Russo_One.ttf", 35); + SDL_Color color = BLACK; + SDL_Color color_selected = WHITE; + + SDL_Texture *menu_bg = + IMG_LoadTexture(renderer, "resource/img/empty_back.png"); + SDL_Texture *btn_img = IMG_LoadTexture(renderer, "resource/img/empty.png"); + + SDL_Rect backgroundRect = {0, 0, MX_WIND_W, MX_WIND_H}; + + + + SDL_Rect play_btn = {MX_BTN_X, MX_BTN_Y - 70, MX_BTN_W, MX_BTN_H}; + SDL_Rect mult_btn = {MX_BTN_X, MX_BTN_Y -5 , MX_BTN_W, + MX_BTN_H}; + SDL_Rect board_btn = {MX_BTN_X, MX_BTN_Y + 60 , MX_BTN_W, + MX_BTN_H}; + SDL_Rect exit_btn = {MX_BTN_X, MX_BTN_Y + MX_BTN_H + 65 , MX_BTN_W, MX_BTN_H}; + + + while (running) { + while (SDL_PollEvent(&event)) { + if (event.type == SDL_KEYUP) { + + if (event.key.keysym.sym == SDLK_ESCAPE) { + return EXIT_STATE; + } + if (event.key.keysym.sym == SDLK_UP) { + index_menu = (index_menu > 1) ? (index_menu - 1) : 4; + + } + if (event.key.keysym.sym == SDLK_DOWN) { + + index_menu = (index_menu < 4) ? (index_menu + 1) : 1; + + } + if (event.key.keysym.sym == SDLK_RETURN) { + + SDL_DestroyTexture(btn_img); + SDL_DestroyTexture(menu_bg); + TTF_CloseFont(font); + switch (index_menu) + { + case 1: + return GAME_STATE; + break; + case 2: + return MULTIPLAYER_STATE; + break; + case 3: + return LEADERBOARD_STATE; + break; + case 4: + return EXIT_STATE; + break; + default: + break; + } + } + } + } + SDL_RenderCopy(renderer, menu_bg, NULL, &backgroundRect); + + SDL_RenderCopy(renderer, btn_img, NULL, &play_btn); + SDL_RenderCopy(renderer, btn_img, NULL, &mult_btn); + SDL_RenderCopy(renderer, btn_img, NULL, &board_btn); + SDL_RenderCopy(renderer, btn_img, NULL, &exit_btn); + + switch (index_menu) { + case 1: + mx_draw_text(color_selected, play_btn.x + 100, play_btn.y + 5, + "PLAY", renderer, font); + mx_draw_text(color, mult_btn.x + 20, mult_btn.y + 5, + "MULTIPLAYER", renderer, font); + mx_draw_text(color, board_btn.x + 15, board_btn.y + 5, + "LEADERBOARD", renderer, font); + mx_draw_text(color, exit_btn.x + 100, exit_btn.y + 5, "QUIT", + renderer, font); + break; + + case 2: + mx_draw_text(color, play_btn.x + 100, play_btn.y + 5, "PLAY", + renderer, font); + mx_draw_text(color_selected, mult_btn.x + 20, mult_btn.y + 5, + "MULTIPLAYER", renderer, font); + mx_draw_text(color, board_btn.x + 15, board_btn.y + 5, + "LEADERBOARD", renderer, font); + mx_draw_text(color, exit_btn.x + 100, exit_btn.y + 5, "QUIT", + renderer, font); + break; + + case 3: + mx_draw_text(color, play_btn.x + 100, play_btn.y + 5, "PLAY", + renderer, font); + mx_draw_text(color, mult_btn.x + 20, mult_btn.y + 5, + "MULTIPLAYER", renderer, font); + mx_draw_text(color_selected, board_btn.x + 15, board_btn.y + 5, + "LEADERBOARD", renderer, font); + mx_draw_text(color, exit_btn.x + 100, exit_btn.y + 5, + "QUIT", renderer, font); + break; + case 4: + mx_draw_text(color, play_btn.x + 100, play_btn.y + 5, "PLAY", + renderer, font); + mx_draw_text(color, mult_btn.x + 20, mult_btn.y + 5, + "MULTIPLAYER", renderer, font); + mx_draw_text(color, board_btn.x + 15, board_btn.y + 5, + "LEADERBOARD", renderer, font); + mx_draw_text(color_selected, exit_btn.x + 100, exit_btn.y + 5, + "QUIT", renderer, font); + break; + default: + break; + } + + usleep(100); + SDL_RenderPresent(renderer); + } + SDL_DestroyTexture(btn_img); + SDL_DestroyTexture(menu_bg); + TTF_CloseFont(font); + + + return result; +} diff --git a/src/mx_multiplayer.c b/src/mx_multiplayer.c new file mode 100644 index 0000000..2794d51 --- /dev/null +++ b/src/mx_multiplayer.c @@ -0,0 +1,80 @@ +#include "game.h" + +e_scenes mx_multiplayer(SDL_Window *window, t_state *game) { + SDL_Event e; + e_scenes status = MENU_STATE; + int running = 1; + game->plane.life = GAME_LIFE; + game->plane.cnt = 0; + game->plane2.life = GAME_LIFE; + game->plane2.cnt = 0; + + while (running) { + while (SDL_PollEvent(&e)) { + switch (e.type) { + case SDL_WINDOWEVENT_CLOSE: + if (window) { + SDL_DestroyWindow(window); + window = NULL; + status = EXIT_STATE; + } + break; + case SDL_KEYDOWN: + switch (e.key.keysym.sym) { + case SDLK_q: + return GAMEOVER_STATE; + break; + case SDLK_UP: + game->plane.dy = -2; + break; + case SDLK_w: + game->plane2.dy = -2; + break; + + default: + break; + } + break; + case SDL_QUIT: + status = EXIT_STATE; + break; + default: + break; + } + } + + const Uint8 *state = SDL_GetKeyboardState(NULL); + if (state[SDL_SCANCODE_LEFT]) { + game->plane.x -= 5; + } + if (state[SDL_SCANCODE_RIGHT]) { + game->plane.x += 5; + } + if(state[SDL_SCANCODE_UP]){ + game->plane.y -= 5; + } + if(state[SDL_SCANCODE_DOWN]){ + game->plane.y += 5; + } + if (state[SDL_SCANCODE_A]) { + game->plane2.x -= 5.5; + } + if (state[SDL_SCANCODE_D]) { + game->plane2.x += 5.5; + } + if(state[SDL_SCANCODE_W]){ + game->plane2.y -= 5.5; + } + if(state[SDL_SCANCODE_S]){ + game->plane2.y += 5.5; + } + mx_play_game_mult(game); + mx_do_render_mult(game->renderer, game); + + + if (game->plane.life <= 0 || game->plane2.life <= 0) + return GAMEOVER_STATE; + } + + return status; +} diff --git a/src/mx_play_game.c b/src/mx_play_game.c new file mode 100644 index 0000000..e147358 --- /dev/null +++ b/src/mx_play_game.c @@ -0,0 +1,27 @@ +#include "game.h" + +void mx_play_game(t_state *game) { + mx_gravitation (&game->plane); + + // mx_score_to_str(game); + + if (game->gate.w < MX_PLANE_W * 2) { + mx_scale_gate(&game->gate); + } + + if (game->gate.w == MX_PLANE_W * 1.7) { + game->plane.dw = mx_check_pass(&game->gate, &game->plane); + mx_is_over(&game->plane); + } + + if (game->gate.w > MX_PLANE_W * 1.7 && game->gate.w < MX_PLANE_W * 2) { + mx_scale_car(&game->plane); + } + + if (game->gate.w >= MX_PLANE_W * 2) { + mx_initial_state(&game->gate, &game->plane); + mx_scale_car(&game->plane); + } +} + + diff --git a/src/mx_play_game_mult.c b/src/mx_play_game_mult.c new file mode 100644 index 0000000..16ad0f2 --- /dev/null +++ b/src/mx_play_game_mult.c @@ -0,0 +1,41 @@ +#include "game.h" + +void mx_play_game_mult(t_state *game) { + mx_gravitation (&game->plane); + mx_gravitation (&game->plane2); + + // mx_score_to_str(game); + + if (game->gate.w < MX_PLANE_W * 2) { + mx_scale_gate(&game->gate); + } + if (game->gate2.w < MX_PLANE_W * 2) { + mx_scale_gate(&game->gate2); + } + + if (game->gate.w == MX_PLANE_W * 1.7) { + game->plane.dw = mx_check_pass(&game->gate, &game->plane); + mx_is_over(&game->plane); + } + + if (game->gate2.w == MX_PLANE_W * 1.7) { + game->plane2.dw = mx_check_pass(&game->gate2, &game->plane2); + mx_is_over(&game->plane2); + } + + if (game->gate.w > MX_PLANE_W * 1.7 && game->gate.w < MX_PLANE_W * 2) { + mx_scale_car(&game->plane); + } + if (game->gate2.w > MX_PLANE_W * 1.7 && game->gate2.w < MX_PLANE_W * 2) { + mx_scale_car(&game->plane2); + } + + if (game->gate.w >= MX_PLANE_W * 2) { + mx_initial_state(&game->gate, &game->plane); + mx_scale_car(&game->plane); + } + if (game->gate2.w >= MX_PLANE_W * 2) { + mx_initial_state(&game->gate2, &game->plane2); + mx_scale_car(&game->plane2); + } +} diff --git a/src/mx_printerr.c b/src/mx_printerr.c index 7936808..1aca9cc 100644 --- a/src/mx_printerr.c +++ b/src/mx_printerr.c @@ -1,11 +1,6 @@ #include "game.h" void mx_printerr(const char *s) { - int s_len = mx_strlen(s) + 2; - char err_msg[s_len]; - - mx_strcpy(err_msg, s); - mx_strcpy(err_msg, "\n"); - write(2, "oops: ", 6); - write(2, err_msg, s_len); + while(*s != '\0') + write(2, s++, 1); } diff --git a/src/mx_process_events.c b/src/mx_process_events.c deleted file mode 100644 index b093d51..0000000 --- a/src/mx_process_events.c +++ /dev/null @@ -1,53 +0,0 @@ -#include "game.h" - - -int mx_process_events(SDL_Window *window, t_state *game) { - SDL_Event e; - int quit = 0; - - if (SDL_PollEvent(&e)) { - switch (e.type) { - case SDL_WINDOWEVENT_CLOSE: - if (window) { - SDL_DestroyWindow(window); - window = NULL; - quit = 1; - } - break; - case SDL_KEYDOWN: - switch (e.key.keysym.sym) { - case SDLK_ESCAPE: - quit = 1; - break; - case SDLK_UP: - game-> plane.dy = -2; - break; - - default: - break; - } - break; - case SDL_QUIT: - quit = 1; - break; - default: - break; - } - } - - const Uint8 *state = SDL_GetKeyboardState(NULL); - if (state[SDL_SCANCODE_LEFT]) { - game->plane.x -= 5; - } - if (state[SDL_SCANCODE_RIGHT]) { - game->plane.x += 5; - } - if(state[SDL_SCANCODE_UP]){ - game->plane.y -= 5; - } - if(state[SDL_SCANCODE_DOWN]){ - game->plane.y += 5; - } - - return quit; -} diff --git a/src/mx_scale_car.c b/src/mx_scale_car.c index 8467cf2..59090c2 100644 --- a/src/mx_scale_car.c +++ b/src/mx_scale_car.c @@ -1,13 +1,18 @@ #include "game.h" -void mx_scale_car(t_state *game, int direction) { - - if (direction == 0) { - game->plane.w = MX_PLANE_W; - game->plane.h = MX_PLANE_H; - } - else { - game->plane.w += 7 * direction; - game->plane.h += 5 * direction; +void mx_scale_car(t_world *plane) { + switch (plane->dw) { + case -1: + plane->w -= 5 * plane->dw; + plane->h -= 5 * plane->dw; + break; + case 1: + plane->w -= 5 * plane->dw; + plane->h -= 5 * plane->dw; + break; + default: + plane->w = MX_PLANE_W; + plane->h = MX_PLANE_H; + break; } } diff --git a/src/mx_scale_gate.c b/src/mx_scale_gate.c index 9ff0f69..451b67c 100644 --- a/src/mx_scale_gate.c +++ b/src/mx_scale_gate.c @@ -1,47 +1,21 @@ #include "game.h" -void mx_scale_gate(t_state *game) { +void mx_scale_gate(t_world *gate) { + gate->w++; + gate->h++; - if (game->gate.w < MX_PLANE_W + 80) { - game->gate.w++; - game->gate.h++; - - if (game->gate.x > (game->gate.w - 200)) { - game->gate.x -= 1; - } - else { - game->gate.x += 0.5; - } - - if (game->gate.y > (game->gate.w - 200)) { - game->gate.y -= 2; - } - else { - game->gate.y++; - } + if (gate->x > (gate->w - 200)) { + gate->x -= 1; } - - if (game->gate.w > MX_PLANE_W+40 && game->gate.w < MX_PLANE_W + 80) { - game->plane.dy = mx_check_pass(game); - - mx_scale_car(game, game->plane.dy); - - // if(is_crush > 0) { - // mx_printerr(" UMIR \n"); - // } + else { + gate->x += 0.5; } - if (game->gate.w >= MX_PLANE_W + 80) { - game->gate.x = MX_RANDOM_X; - game->gate.y = MX_RANDOM_Y; - game->gate.w = 100; - game->gate.h = 50; - - game->plane.x = MX_PLANE_ST_X; - // game->plane.x = MX_PLANE_ST_X; - - - mx_scale_car(game, 0); - + if (gate->y > (gate->w - 200)) { + gate->y -= 2; + } + else { + gate->y++; } + } diff --git a/src/mx_strcpy.c b/src/mx_strcpy.c deleted file mode 100644 index 4e8a3f1..0000000 --- a/src/mx_strcpy.c +++ /dev/null @@ -1,9 +0,0 @@ -char *mx_strcpy(char *dst, const char *src) { - int i; - for ( i = 0; src[i] != '\0'; i++) { - dst[i] = src[i]; - } - dst[i] = '\0'; - return dst; -} - diff --git a/src/mx_strlen.c b/src/mx_strlen.c deleted file mode 100644 index 158665e..0000000 --- a/src/mx_strlen.c +++ /dev/null @@ -1,8 +0,0 @@ -int mx_strlen(const char *s) { - int result = 0; - - while (s[result] != '\0') { - result++; - } - return result; -}