|
| 1 | +#include <SDL2/SDL_timer.h> |
| 2 | +#include <cstddef> |
| 3 | +#include <SDL2/SDL_keycode.h> |
| 4 | +#include <SDL2/SDL_render.h> |
| 5 | +#include <SDL2/SDL_ttf.h> |
| 6 | +#include <SDL2/SDL_surface.h> |
| 7 | +#include <SDL2/SDL_stdinc.h> |
| 8 | +#include <string> |
| 9 | +#include "../../helpers/constants.h" |
| 10 | +#include "qubo_screen.h" |
| 11 | +#include "start_screen.h" |
| 12 | + |
| 13 | +using std::to_string; |
| 14 | +using std::cout; |
| 15 | +using std::endl; |
| 16 | +using std::size_t; |
| 17 | + |
| 18 | +namespace Screens { |
| 19 | + QuboScreen::QuboScreen(SDL_Renderer* render) : Screen(render){ |
| 20 | + if(!this->font){ |
| 21 | + cout << "Failed on getting font!" << TTF_GetError() << endl; |
| 22 | + exit(1); |
| 23 | + } |
| 24 | + |
| 25 | + SDL_Surface* score_text_surface = TTF_RenderText_Solid(this->font, "QUBO Score", this->text_color); |
| 26 | + this->score_text_texture = SDL_CreateTextureFromSurface(render, score_text_surface); |
| 27 | + this->score_text_shape = SDL_Rect{20, 20, score_text_surface->w, score_text_surface->h}; |
| 28 | + SDL_FreeSurface(score_text_surface); |
| 29 | + |
| 30 | + if(this->score_text_texture == nullptr){ |
| 31 | + cout << "Failed on creating score text texture!" << SDL_GetError() << endl; |
| 32 | + exit(1); |
| 33 | + } |
| 34 | + this->board.add_player(this->player); |
| 35 | + this->left_padding = 10 * SQUARE_SIDE; |
| 36 | + } |
| 37 | + |
| 38 | + void QuboScreen::execute(bool& game_loop){ |
| 39 | + bool won = this->player->get_score() >= this->max_score; |
| 40 | + this->finished_game = won || this->player->is_dead(); |
| 41 | + if(this->finished_game){ |
| 42 | + SDL_Surface* game_over_surface = TTF_RenderText_Solid(this->title_font, won ? "QUBO Wins!!!" : "Game Over", this->text_color); |
| 43 | + SDL_Texture* game_over_texture = SDL_CreateTextureFromSurface(this->render, game_over_surface); |
| 44 | + SDL_Rect game_over_shape = SDL_Rect{(WIDTH/2)-(game_over_surface->w/2), (HEIGHT/2)-(game_over_surface->h), game_over_surface->w, game_over_surface->h}; |
| 45 | + SDL_FreeSurface(game_over_surface); |
| 46 | + |
| 47 | + SDL_Surface* reset_surface = TTF_RenderText_Solid(this->font, "Press 'r' to reset", this->text_color); |
| 48 | + SDL_Texture* reset_texture = SDL_CreateTextureFromSurface(this->render, reset_surface); |
| 49 | + SDL_Rect reset_shape = SDL_Rect{(WIDTH/2)-(reset_surface->w/2), (HEIGHT/2)+(reset_surface->h)+20, reset_surface->w, reset_surface->h}; |
| 50 | + SDL_FreeSurface(reset_surface); |
| 51 | + |
| 52 | + SDL_Surface* back_surface = TTF_RenderText_Solid(this->font, "Press 'g' to back to the start screen", this->text_color); |
| 53 | + SDL_Texture* back_texture = SDL_CreateTextureFromSurface(this->render, back_surface); |
| 54 | + SDL_Rect back_shape = SDL_Rect{(WIDTH/2)-(back_surface->w/2), (HEIGHT/2)+(back_surface->h)+50, back_surface->w, back_surface->h}; |
| 55 | + SDL_FreeSurface(back_surface); |
| 56 | + |
| 57 | + SDL_SetRenderDrawColor(this->render, 0, 0, 0, 255); |
| 58 | + SDL_RenderCopy(this->render, game_over_texture, NULL, &game_over_shape); |
| 59 | + SDL_RenderCopy(this->render, reset_texture, NULL, &reset_shape); |
| 60 | + SDL_RenderCopy(this->render, back_texture, NULL, &back_shape); |
| 61 | + SDL_DestroyTexture(game_over_texture); |
| 62 | + SDL_DestroyTexture(back_texture); |
| 63 | + SDL_DestroyTexture(reset_texture); |
| 64 | + return; |
| 65 | + } |
| 66 | + this->player->next_mov(this->board.get_food()); |
| 67 | + this->board.update_player_pos(); |
| 68 | + this->render_board(&this->board); |
| 69 | + |
| 70 | + SDL_SetRenderDrawColor(this->render, 0, 0, 0, 255); |
| 71 | + SDL_RenderCopy(this->render, this->score_text_texture, NULL, &this->score_text_shape); |
| 72 | + |
| 73 | + if(this->score_texture != nullptr) |
| 74 | + SDL_DestroyTexture(this->score_texture); |
| 75 | + SDL_Surface* score_surface = TTF_RenderText_Solid(this->font, to_string(this->player->get_score()).c_str(), this->text_color); |
| 76 | + this->score_texture = SDL_CreateTextureFromSurface(this->render, score_surface); |
| 77 | + this->score_shape = SDL_Rect{20, 60, score_surface->w, score_surface->h}; |
| 78 | + SDL_FreeSurface(score_surface); |
| 79 | + |
| 80 | + SDL_RenderCopy(this->render, this->score_texture, NULL, &this->score_shape); |
| 81 | + } |
| 82 | + |
| 83 | + Screen* QuboScreen::key_event(const SDL_Keycode& key){ |
| 84 | + switch (key) { |
| 85 | + case SDLK_g: |
| 86 | + if(this->finished_game) |
| 87 | + return new StartScreen(this->render); |
| 88 | + |
| 89 | + case SDLK_r: |
| 90 | + if(this->finished_game) |
| 91 | + this->reset(); |
| 92 | + |
| 93 | + default: |
| 94 | + break; |
| 95 | + } |
| 96 | + return nullptr; |
| 97 | + } |
| 98 | + |
| 99 | + void QuboScreen::close_event(){ |
| 100 | + } |
| 101 | + |
| 102 | + void QuboScreen::reset(){ |
| 103 | + this->finished_game = false; |
| 104 | + delete this->player; |
| 105 | + this->player = new QuboPlayer(this->board_w, this->board_h); |
| 106 | + this->board.add_player(this->player); |
| 107 | + this->board.random_food(); |
| 108 | + } |
| 109 | + |
| 110 | + QuboScreen::~QuboScreen(){ |
| 111 | + delete this->player; |
| 112 | + SDL_DestroyTexture(this->score_texture); |
| 113 | + SDL_DestroyTexture(this->score_text_texture); |
| 114 | + } |
| 115 | +} |
0 commit comments