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..6fc305e 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # ======== variables ======== # -- files and dirs -- -NAME = game +NAME = endgame INC_DIR = inc SRC_DIR = src @@ -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/endgame b/endgame new file mode 100755 index 0000000..2dab47a Binary files /dev/null and b/endgame differ 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..e03f504 100644 --- a/inc/game.h +++ b/inc/game.h @@ -1,17 +1,38 @@ #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 150 +#define MX_BTN_X ((MX_WIND_W - MX_BTN_W) / 2) +#define MX_BTN_Y (MX_WIND_H / 2 - MX_BTN_H) +#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 + +// colors +#define TEAL {10, 128, 128, 0} +#define ORANGE {255, 100, 0, 0} // includes #include @@ -29,33 +50,53 @@ #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_world { float x; float y; float w; float h; - float cnt; + int cnt; float dy; -} t_world; + int dw; + char *hello; + int life; +} t_world; typedef struct s_state { // objects t_world plane; t_world gate; + t_world plane2; + t_world gate2; t_world space; // imgs SDL_Texture *bg; SDL_Texture *car; - SDL_Texture *gate_img[2]; + SDL_Texture *car2; + SDL_Texture *gate_img; + SDL_Texture *gate2_img; SDL_Renderer *renderer; Mix_Music *bg_music; + TTF_Font *font; + int play; + int exit; + + e_scenes status; } t_state; @@ -63,13 +104,21 @@ typedef struct s_state { char *mx_strcpy(char *dst, const char *src); int mx_strlen(const char *s); -int mx_process_events(SDL_Window *window, t_state *game); -int mx_check_pass(t_state *game); -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_play_game(t_state *game); void mx_load_game(t_state *game); -void process(t_state *game); + + +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/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/frameworks/SDL2_image.framework/Versions/A/SDL2_image b/resource/frameworks/SDL2_image.framework/Versions/A/SDL2_image index a4397cb..e69de29 100755 Binary files a/resource/frameworks/SDL2_image.framework/Versions/A/SDL2_image and b/resource/frameworks/SDL2_image.framework/Versions/A/SDL2_image differ diff --git a/resource/frameworks/SDL2_mixer.framework/Versions/Current/Frameworks/Vorbis.framework/Vorbis b/resource/frameworks/SDL2_mixer.framework/Versions/Current/Frameworks/Vorbis.framework/Vorbis index e551352..e69de29 100755 Binary files a/resource/frameworks/SDL2_mixer.framework/Versions/Current/Frameworks/Vorbis.framework/Vorbis and b/resource/frameworks/SDL2_mixer.framework/Versions/Current/Frameworks/Vorbis.framework/Vorbis differ diff --git a/resource/frameworks/SDL2_mixer.framework/Versions/Current/Frameworks/modplug.framework/Versions/A/modplug b/resource/frameworks/SDL2_mixer.framework/Versions/Current/Frameworks/modplug.framework/Versions/A/modplug index 15f276e..e69de29 100755 Binary files a/resource/frameworks/SDL2_mixer.framework/Versions/Current/Frameworks/modplug.framework/Versions/A/modplug and b/resource/frameworks/SDL2_mixer.framework/Versions/Current/Frameworks/modplug.framework/Versions/A/modplug differ diff --git a/resource/frameworks/SDL2_mixer.framework/Versions/Current/Frameworks/modplug.framework/Versions/Current/modplug b/resource/frameworks/SDL2_mixer.framework/Versions/Current/Frameworks/modplug.framework/Versions/Current/modplug index 15f276e..e69de29 100755 Binary files a/resource/frameworks/SDL2_mixer.framework/Versions/Current/Frameworks/modplug.framework/Versions/Current/modplug and b/resource/frameworks/SDL2_mixer.framework/Versions/Current/Frameworks/modplug.framework/Versions/Current/modplug differ diff --git a/resource/frameworks/SDL2_mixer.framework/Versions/Current/Frameworks/modplug.framework/modplug b/resource/frameworks/SDL2_mixer.framework/Versions/Current/Frameworks/modplug.framework/modplug index 15f276e..e69de29 100755 Binary files a/resource/frameworks/SDL2_mixer.framework/Versions/Current/Frameworks/modplug.framework/modplug and b/resource/frameworks/SDL2_mixer.framework/Versions/Current/Frameworks/modplug.framework/modplug differ diff --git a/resource/img/GameOver.png b/resource/img/GameOver.png new file mode 100644 index 0000000..25e0f4f Binary files /dev/null and b/resource/img/GameOver.png differ diff --git a/resource/img/button.png b/resource/img/button.png new file mode 100644 index 0000000..8d075da Binary files /dev/null and b/resource/img/button.png differ diff --git a/resource/img/buttons/button.png b/resource/img/buttons/button.png new file mode 100644 index 0000000..04e51f9 Binary files /dev/null and b/resource/img/buttons/button.png differ diff --git a/resource/img/buttons/cursor.png b/resource/img/buttons/cursor.png new file mode 100644 index 0000000..99c26d8 Binary files /dev/null and b/resource/img/buttons/cursor.png differ diff --git a/resource/img/buttons/exit_hover.png b/resource/img/buttons/exit_hover.png new file mode 100644 index 0000000..17ae6ee Binary files /dev/null and b/resource/img/buttons/exit_hover.png differ diff --git a/resource/img/buttons/restart_hover.png b/resource/img/buttons/restart_hover.png new file mode 100644 index 0000000..ddb0c0e Binary files /dev/null and b/resource/img/buttons/restart_hover.png differ diff --git a/resource/img/buttons/resume_hover.png b/resource/img/buttons/resume_hover.png new file mode 100644 index 0000000..cfd6aa2 Binary files /dev/null and b/resource/img/buttons/resume_hover.png differ diff --git a/resource/img/buttons/start_hover.png b/resource/img/buttons/start_hover.png new file mode 100644 index 0000000..11c1ff8 Binary files /dev/null and b/resource/img/buttons/start_hover.png differ diff --git a/resource/img/car_blue.png b/resource/img/car_blue.png new file mode 100644 index 0000000..300fadc 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..a215e44 Binary files /dev/null and b/resource/img/car_green.png 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/empty.PNG b/resource/img/empty.PNG new file mode 100644 index 0000000..cb908f1 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..e546605 Binary files /dev/null and b/resource/img/empty_back.png differ diff --git a/resource/img/end_game.png b/resource/img/end_game.png new file mode 100644 index 0000000..6503405 Binary files /dev/null and b/resource/img/end_game.png differ diff --git a/resource/img/game_background/Background1.png b/resource/img/game_background/Background1.png new file mode 100644 index 0000000..4c00660 Binary files /dev/null and b/resource/img/game_background/Background1.png differ diff --git a/resource/img/game_background/Background2.png b/resource/img/game_background/Background2.png new file mode 100644 index 0000000..e44d243 Binary files /dev/null and b/resource/img/game_background/Background2.png differ diff --git a/resource/img/game_background/Background3.png b/resource/img/game_background/Background3.png new file mode 100644 index 0000000..439839b Binary files /dev/null and b/resource/img/game_background/Background3.png differ diff --git a/resource/img/main_page.png b/resource/img/main_page.png new file mode 100644 index 0000000..23206c7 Binary files /dev/null and b/resource/img/main_page.png differ diff --git a/resource/img/space_car.png b/resource/img/space_car.png index 5e1eb5f..1b4837e 100644 Binary files a/resource/img/space_car.png and b/resource/img/space_car.png differ diff --git a/resource/img/space_car2.png b/resource/img/space_car2.png new file mode 100644 index 0000000..df39a86 Binary files /dev/null and b/resource/img/space_car2.png differ diff --git a/resource/img/space_car_.png b/resource/img/space_car_.png new file mode 100644 index 0000000..5e1eb5f Binary files /dev/null and b/resource/img/space_car_.png differ diff --git a/src/main.c b/src/main.c index 684461c..4c5f729 100644 --- a/src/main.c +++ b/src/main.c @@ -3,8 +3,8 @@ 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); @@ -12,41 +12,62 @@ int main(void) { Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048); TTF_Init(); - if (SDL_Init(SDL_INIT_VIDEO)) { - mx_printerr("ne paluchilos"); - exit(1); - } - wind = SDL_CreateWindow("Kuku", 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); + // printf("GAME OWER!!!"); + break; + case MULTIPLAYER_STATE: + game.status = mx_multiplayer(wind, &game); + // printf("GAME OWER!!!"); + break; + case LEADERBOARD_STATE: + game.status = mx_leaderboard(game.renderer); + // printf("LEADERBOARD!!!"); + break; + case EXIT_STATE: + game.exit = 1; + printf("EXIT"); + break; + } + printf("\n kuku: %d", game.exit); } SDL_DestroyTexture(game.bg); SDL_DestroyTexture(game.car); + SDL_DestroyTexture(game.gate_img); SDL_DestroyWindow(wind); SDL_DestroyRenderer(game.renderer); + + IMG_Quit(); + TTF_Quit(); + Mix_CloseAudio(); + TTF_CloseFont(game.font); SDL_Quit(); + system("leaks -q game"); return 0; } diff --git a/src/mx_check_pass.c b/src/mx_check_pass.c deleted file mode 100644 index fad24b4..0000000 --- a/src/mx_check_pass.c +++ /dev/null @@ -1,21 +0,0 @@ -#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; - - 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; - } - } - 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..58271ed 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,26 +10,28 @@ 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, + 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, game->plane.y, game->plane.w, game->plane.h}; - // SDL_RenderFillRect(rend, &plane_p); + SDL_RenderFillRect(rend, &plane_p); SDL_RenderCopy(rend, game->car, NULL, &plane_p); + + // TTF_CloseFont(game->font); + + SDL_Color score_color = ORANGE; + mx_draw_text(score_color, 20, 20, "SCORE: ", game->renderer, game->font); + mx_draw_text(score_color, 120, 20, game->plane.hello, game->renderer, + game->font); + SDL_RenderPresent(rend); } diff --git a/src/mx_draw_text.c b/src/mx_draw_text.c new file mode 100644 index 0000000..24a9f12 --- /dev/null +++ b/src/mx_draw_text.c @@ -0,0 +1,14 @@ +#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..cee1b03 --- /dev/null +++ b/src/mx_game.c @@ -0,0 +1,75 @@ +#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) { + 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; + // game.exit = 0; + break; + case SDLK_UP: + game->plane.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..acdbfad --- /dev/null +++ b/src/mx_gameover.c @@ -0,0 +1,80 @@ +#include "game.h" +// #include + +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/Sansation-Bold.ttf", 25); + 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_Y, MX_BTN_X + MX_BTN_H, MX_BTN_W , MX_BTN_H}; + SDL_Rect menu_btn = {MX_BTN_Y + MX_BTN_W + 40, 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 + 40, replay_btn.y+50, "REPLAY", renderer, font); + mx_draw_text(color, menu_btn.x + 40 , menu_btn.y+50, "MENU", renderer, font); + + } else if (index_menu == 2) { + mx_draw_text(color, replay_btn.x + 40, replay_btn.y+50, "REPLAY", renderer, font); + mx_draw_text(color_selected, menu_btn.x + 40 , menu_btn.y+50, "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_gravity.c deleted file mode 100644 index 54da72d..0000000 --- a/src/mx_gravity.c +++ /dev/null @@ -1,8 +0,0 @@ -#include "game.h" - -void process(t_state *game) { - t_world *plane = &game->plane; - plane->y += plane->dy; - - plane->dy += MX_GRAVITY; -} diff --git a/src/mx_leaderboard.c b/src/mx_leaderboard.c new file mode 100644 index 0000000..7302622 --- /dev/null +++ b/src/mx_leaderboard.c @@ -0,0 +1,78 @@ +#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/Sansation-Bold.ttf", 25); + SDL_Color color = TEAL; + SDL_Color color_selected = ORANGE; + + 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}; + + 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); + + + } 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); + + + } 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); + + } + + 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..3d5c105 100644 --- a/src/mx_load_game.c +++ b/src/mx_load_game.c @@ -13,6 +13,8 @@ void mx_load_game(t_state *game) { } game->bg_music = namemusic; + game->font = TTF_OpenFont("resource/font/Sansation-Bold.ttf", 25); + // space bg: surf = IMG_Load("resource/img/space_bg.jpg"); if (surf == NULL) { @@ -31,18 +33,17 @@ void mx_load_game(t_state *game) { exit(1); } - game->gate_img[0] = SDL_CreateTextureFromSurface(game->renderer, surf); - - // green circle + //game->gate_img = SDL_CreateTextureFromSurface(game->renderer, surf); + surf = IMG_Load("resource/img/circle_green .png"); 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"); @@ -52,7 +53,16 @@ void mx_load_game(t_state *game) { exit(1); } - game->car = SDL_CreateTextureFromSurface(game->renderer, surf); + game->car2 = SDL_CreateTextureFromSurface(game->renderer, surf); + + surf = IMG_Load("resource/img/space_car2.png"); + if (surf == NULL) { + mx_printerr("Can't load car"); + SDL_Quit(); + exit(1); + } + + game->car2 = SDL_CreateTextureFromSurface(game->renderer, surf); SDL_FreeSurface(surf); @@ -61,16 +71,22 @@ 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->plane.hello = "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; + // SDL_FreeSurface(surf); } diff --git a/src/mx_menu.c b/src/mx_menu.c new file mode 100644 index 0000000..7203ebe --- /dev/null +++ b/src/mx_menu.c @@ -0,0 +1,139 @@ +#include "game.h" +// #include + + +e_scenes mx_menu(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/Sansation-Bold.ttf", 25); + SDL_Color color = TEAL; + SDL_Color color_selected = ORANGE; + + 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 - 20, MX_BTN_W, MX_BTN_H}; + SDL_Rect mult_btn = {MX_BTN_X, MX_BTN_Y + 40, MX_BTN_W, + MX_BTN_H}; + SDL_Rect board_btn = {MX_BTN_X, MX_BTN_Y + 100, MX_BTN_W, + MX_BTN_H}; + SDL_Rect exit_btn = {MX_BTN_X, MX_BTN_Y + MX_BTN_H + 10, 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 + 40, play_btn.y + 50, + "PLAY", renderer, font); + mx_draw_text(color, mult_btn.x + 40, mult_btn.y + 50, + "MULTIPLAYER", renderer, font); + mx_draw_text(color, board_btn.x + 40, board_btn.y + 50, + "LEADERBOARD", renderer, font); + mx_draw_text(color, exit_btn.x + 40, exit_btn.y + 50, "QUIT", + renderer, font); + break; + + case 2: + mx_draw_text(color, play_btn.x + 40, play_btn.y + 50, "PLAY", + renderer, font); + mx_draw_text(color_selected, mult_btn.x + 40, mult_btn.y + 50, + "MULTIPLAYER", renderer, font); + mx_draw_text(color, board_btn.x + 40, board_btn.y + 50, + "LEADERBOARD", renderer, font); + mx_draw_text(color, exit_btn.x + 40, exit_btn.y + 50, "QUIT", + renderer, font); + break; + + case 3: + mx_draw_text(color, play_btn.x + 40, play_btn.y + 50, "PLAY", + renderer, font); + mx_draw_text(color, mult_btn.x + 40, mult_btn.y + 50, + "MULTIPLAYER", renderer, font); + mx_draw_text(color_selected, board_btn.x + 40, board_btn.y + 50, + "LEADERBOARD", renderer, font); + mx_draw_text(color, exit_btn.x + 40, exit_btn.y + 50, + "QUIT", renderer, font); + break; + case 4: + mx_draw_text(color, play_btn.x + 40, play_btn.y + 50, "PLAY", + renderer, font); + mx_draw_text(color, mult_btn.x + 40, mult_btn.y + 50, + "MULTIPLAYER", renderer, font); + mx_draw_text(color, board_btn.x + 40, board_btn.y + 50, + "LEADERBOARD", renderer, font); + mx_draw_text(color_selected, exit_btn.x + 40, exit_btn.y + 50, + "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..34e68c4 --- /dev/null +++ b/src/mx_multiplayer.c @@ -0,0 +1,78 @@ +#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 = 3; + game->plane.cnt = 0; + game->plane2.life = 3; + 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; + // game.exit = 0; + break; + case SDLK_UP: + game->plane.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 || 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..cc0035d --- /dev/null +++ b/src/mx_play_game.c @@ -0,0 +1,150 @@ +#include "game.h" + +static void scale_gate(t_state *game) { + 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++; + } + +} + +static void initial_state(t_state *game) { + game->gate.x = MX_RANDOM_X; + game->gate.y = MX_RANDOM_Y; + game->gate.w = MX_RING_W; + game->gate.h = MX_RING_H ; + + game->plane.x = MX_PLANE_ST_X; + game->plane.dw = 0; +} + +static int 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; + + 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; + } + } + return -1; +} + +static void gravitation (t_state *game) { + game->plane.y += game->plane.dy; + game->plane.dy += MX_GRAVITY; +} + +static void scale_car(t_state *game) { + switch (game->plane.dw) { + case -1: + game->plane.w -= 5 * game->plane.dw; + game->plane.h -= 5 * game->plane.dw; + break; + case 1: + game->plane.w -= 5 * game->plane.dw; + game->plane.h -= 5 * game->plane.dw; + break; + default: + game->plane.w = MX_PLANE_W; + game->plane.h = MX_PLANE_H; + break; + } +} + +// static void render_score(t_state *game) { + +// TTF_Font *font = TTF_OpenFont("resource/font/Sansation-Bold.ttf", 25); +// SDL_Color score_color = ORANGE; +// draw_text (score_color, 20, 20, "SCORE: ", game->renderer, font); +// draw_text (score_color, 120, 20, game->plane.hello, game->renderer, font); +// // SDL_RenderPresent(game->renderer); +// } + +static void is_over(t_state *game) { + + switch (game->plane.dw) { + case 1: + game->plane.cnt++; + game->plane.hello = "kukusiki \n"; + break; + case -1: + game->plane.life--; + break; + default: + break; + } +} + +// void mx_score_to_str(t_state *game){ +// char *result = NULL; +// int leng; +// int cnt = game->plane.cnt; +// int dig; +// int rest; + +// for (leng = 1; cnt > 0; leng++) { +// cnt /= 10; +// } + +// result = (char*)malloc(leng+1); +// if (result == NULL) { +// exit(1); +// } + +// cnt = game->plane.cnt; + +// for (int i = leng; i < 0; i--) { +// dig = cnt % 10; +// cnt /= 10; + +// result[i] = '\0'; +// if (i != leng) +// result[i] = dig; +// } + +// game->plane.hello = result; +// free(result); +// } + + +void mx_play_game(t_state *game) { + gravitation (game); + + // mx_score_to_str(game); + + if (game->gate.w < MX_PLANE_W * 2) { + scale_gate(game); + } + + if (game->gate.w == MX_PLANE_W * 1.7) { + game->plane.dw = check_pass(game); + is_over(game); + } + + if (game->gate.w > MX_PLANE_W * 1.7 && game->gate.w < MX_PLANE_W * 2) { + scale_car(game); + } + + if (game->gate.w >= MX_PLANE_W * 2) { + initial_state(game); + scale_car(game); + } +} + + 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 deleted file mode 100644 index 8467cf2..0000000 --- a/src/mx_scale_car.c +++ /dev/null @@ -1,13 +0,0 @@ -#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; - } -} diff --git a/src/mx_scale_gate.c b/src/mx_scale_gate.c deleted file mode 100644 index 9ff0f69..0000000 --- a/src/mx_scale_gate.c +++ /dev/null @@ -1,47 +0,0 @@ -#include "game.h" - -void mx_scale_gate(t_state *game) { - - 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 (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"); - // } - } - - 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); - - } -} 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; -}