55#include < SDL2/SDL_ttf.h>
66#include < SDL2/SDL_surface.h>
77#include < SDL2/SDL_stdinc.h>
8+ #include < string>
89#include " ../../nativefiledialog-extended/src/include/nfd.h"
910#include " screens.h"
11+ #include " start_screen.h"
1012#include " ai_screen_play.h"
1113#include " ../../helpers/utils.h"
14+ #include " ../../helpers/constants.h"
1215
16+ using std::to_string;
1317using std::cout;
1418using std::endl;
1519using std::size_t ;
@@ -19,39 +23,119 @@ namespace Screens {
1923 AIPlayScreen::AIPlayScreen (SDL_Renderer* render) : Screen(render){
2024
2125 if (NFD_Init () != NFD_OKAY){
22- cout << " Failed on load NFD" << endl;
26+ cout << " Failed on load NFD " << NFD_GetError () << endl;
27+ exit (1 );
28+ }
29+
30+ if (!this ->font ){
31+ cout << " Failed on getting font!" << TTF_GetError () << endl;
2332 exit (1 );
2433 }
2534
26- nfdchar_t *outPath;
27- nfdresult_t result = NFD_OpenDialog (&outPath, NULL , 0 , NULL );
35+ nfdresult_t result = NFD_OpenDialog (&this ->nn_path , NULL , 0 , NULL );
2836
2937 if (result == NFD_OKAY){
30- this ->player = new AIPlayer (this ->board_w , this ->board_h , parse_nn (outPath ));
38+ this ->player = new AIPlayer (this ->board_w , this ->board_h , parse_nn (this -> nn_path ));
3139 this ->board .add_player (this ->player );
32- NFD_FreePath (outPath);
33- return ;
40+ }else {
41+ cout << " You must select a weights file!" << endl;
42+ exit (1 );
3443 }
3544
36- cout << " You must select a weights file!" << endl;
37- exit (1 );
45+
46+ SDL_Surface* score_text_surface = TTF_RenderText_Solid (this ->font , " AI Score" , this ->text_color );
47+ this ->score_text_texture = SDL_CreateTextureFromSurface (render, score_text_surface);
48+ this ->score_text_shape = SDL_Rect{20 , 20 , score_text_surface->w , score_text_surface->h };
49+ SDL_FreeSurface (score_text_surface);
50+
51+ if (this ->score_text_texture == nullptr ){
52+ cout << " Failed on creating score text texture!" << SDL_GetError () << endl;
53+ exit (1 );
54+ }
55+
56+ this ->left_padding = 10 * SQUARE_SIDE;
3857 }
3958
4059 void AIPlayScreen::execute (bool & game_loop){
60+ bool won = this ->player ->get_score () >= this ->max_score ;
61+ this ->finished_game = won || this ->player ->is_dead ();
62+ if (this ->finished_game ){
63+ SDL_Surface* game_over_surface = TTF_RenderText_Solid (this ->title_font , won ? " AI Wins!!!" : " Game Over" , this ->text_color );
64+ SDL_Texture* game_over_texture = SDL_CreateTextureFromSurface (this ->render , game_over_surface);
65+ 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 };
66+ SDL_FreeSurface (game_over_surface);
67+
68+ SDL_Surface* reset_surface = TTF_RenderText_Solid (this ->font , " Press 'r' to reset" , this ->text_color );
69+ SDL_Texture* reset_texture = SDL_CreateTextureFromSurface (this ->render , reset_surface);
70+ SDL_Rect reset_shape = SDL_Rect{(WIDTH/2 )-(reset_surface->w /2 ), (HEIGHT/2 )+(reset_surface->h )+20 , reset_surface->w , reset_surface->h };
71+ SDL_FreeSurface (reset_surface);
72+
73+ SDL_Surface* back_surface = TTF_RenderText_Solid (this ->font , " Press 'g' to back to the start screen" , this ->text_color );
74+ SDL_Texture* back_texture = SDL_CreateTextureFromSurface (this ->render , back_surface);
75+ SDL_Rect back_shape = SDL_Rect{(WIDTH/2 )-(back_surface->w /2 ), (HEIGHT/2 )+(back_surface->h )+50 , back_surface->w , back_surface->h };
76+ SDL_FreeSurface (back_surface);
77+
78+ SDL_SetRenderDrawColor (this ->render , 0 , 0 , 0 , 255 );
79+ SDL_RenderCopy (this ->render , game_over_texture, NULL , &game_over_shape);
80+ SDL_RenderCopy (this ->render , reset_texture, NULL , &reset_shape);
81+ SDL_RenderCopy (this ->render , back_texture, NULL , &back_shape);
82+ SDL_DestroyTexture (game_over_texture);
83+ SDL_DestroyTexture (back_texture);
84+ SDL_DestroyTexture (reset_texture);
85+ return ;
86+ }
87+
88+ this ->player ->update_input_data (this ->board .get_food (), this ->board_w , this ->board_h );
89+ this ->player ->compute_next_dir ();
90+ this ->player ->update_dir ();
91+ this ->board .update_player_pos ();
4192 this ->render_board (&this ->board );
93+
4294 SDL_SetRenderDrawColor (this ->render , 0 , 0 , 0 , 255 );
95+ SDL_RenderCopy (this ->render , this ->score_text_texture , NULL , &this ->score_text_shape );
96+
97+ if (this ->score_texture != nullptr )
98+ SDL_DestroyTexture (this ->score_texture );
99+ SDL_Surface* score_surface = TTF_RenderText_Solid (this ->font , to_string (this ->player ->get_score ()).c_str (), this ->text_color );
100+ this ->score_texture = SDL_CreateTextureFromSurface (this ->render , score_surface);
101+ this ->score_shape = SDL_Rect{20 , 60 , score_surface->w , score_surface->h };
102+ SDL_FreeSurface (score_surface);
103+
104+ SDL_RenderCopy (this ->render , this ->score_texture , NULL , &this ->score_shape );
43105 }
44106
45107 Screen* AIPlayScreen::key_event (const SDL_Keycode& key){
108+ switch (key) {
109+ case SDLK_g:
110+ if (this ->finished_game )
111+ return new StartScreen (this ->render );
112+
113+ case SDLK_r:
114+ if (this ->finished_game )
115+ this ->reset ();
116+
117+ default :
118+ break ;
119+ }
46120 return nullptr ;
47121 }
48122
49123 void AIPlayScreen::close_event (){
50124 }
51125
126+ void AIPlayScreen::reset (){
127+ this ->finished_game = false ;
128+ delete this ->player ;
129+ this ->player = new AIPlayer{this ->board_w , this ->board_h , parse_nn (this ->nn_path )};
130+ this ->board .add_player (this ->player );
131+ this ->board .random_food ();
132+ }
52133
53134 AIPlayScreen::~AIPlayScreen (){
54135 delete this ->player ;
55136 NFD_Quit ();
137+ NFD_FreePath (this ->nn_path );
138+ SDL_DestroyTexture (this ->score_texture );
139+ SDL_DestroyTexture (this ->score_text_texture );
56140 }
57141}
0 commit comments