-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_init.cpp
206 lines (168 loc) · 5.69 KB
/
game_init.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "game_init.h"
#include "constantes.h"
#include <iostream>
#include <future>
#include <thread>
#include <utility>
void create_thread(std::promise<Texto*>&& Promesa, std::string texto_renderizar, std::shared_ptr<SDL_Renderer> ext_render){
Promesa.set_value(new Texto(texto_renderizar, ext_render));
}
Game_Init::Game_Init()
: _Ventana(nullptr, SDL_DestroyWindow),
_Render (nullptr, SDL_DestroyRenderer)
{
if(inicializar()){
// In this case, it is not more efficient to use future and promise, but I did it to to fulfill the requirements of the capstone
///* //future and promise: https://www.modernescpp.com/index.php/promise-and-future
std::promise<Texto*> p_1, p_2, p_3;
std::future<Texto*> f_1 = p_1.get_future();
std::future<Texto*> f_2 = p_2.get_future();
std::future<Texto*> f_3 = p_3.get_future();
std::thread mi_hilo_1(create_thread, std::move(p_1),"Introduce tu nombre:", _Render);
std::thread mi_hilo_2(create_thread, std::move(p_2),"Pulsa Enter para continuar", _Render);
std::thread mi_hilo_3(create_thread, std::move(p_3),"Jugador 1", _Render);
_pedir_nombre_jugador = std::make_unique<Texto>(std::move(*(f_1.get())));
_pulsa_intro_continuar = std::make_unique<Texto>(std::move(*(f_2.get())));
_Nombre_Jugador_Temp = std::make_unique<Texto>(std::move(*(f_3.get())));
mi_hilo_1.join(); mi_hilo_2.join(); mi_hilo_3.join();
//*/
/*
_pulsa_intro_continuar = std::make_unique<Texto> ("Pulsa Enter para continuar",_Render);
_pedir_nombre_jugador = std::make_unique<Texto>("Introduce tu nombre:",_Render);
_Nombre_Jugador_Temp = std::make_unique<Texto>("Jugador 1", _Render);
*/
pantalla_pedir_nombre();
}
}
Game_Init::~Game_Init(){
std::cout<<"Game Init Destructor Called " << this<< "\n" <<std::endl;
}
std::unique_ptr<Texto> Game_Init::get_nombre_jugador(){
return std::move(_Nombre_Jugador_Temp);
}
std::unique_ptr<Texto> Game_Init::get_pulsa_intro_continuar(){
return std::move(_pulsa_intro_continuar);
}
void Game_Init::pantalla_pedir_nombre(){
std::string inputText = "Jugador 1";
bool quit = false;
SDL_Event eventos;
SDL_StartTextInput(); // Enables text input
// To manage the refresh rate
Uint32 frame_start, frame_end, frame_duration;
Uint32 target_frame_duration = _kMsPerFrame;
//While application is running
while( !quit )
{
frame_start = SDL_GetTicks();
bool renderText = false;
while( SDL_PollEvent( &eventos ) != 0 )
{
if(eventos.type == SDL_QUIT)
{ // to exit if x is pressed (in the window corner)
quit = true;
break;
}
else if( eventos.type == SDL_KEYDOWN )
{
if(eventos.key.keysym.scancode == SDL_SCANCODE_RETURN)
{ // to exit if enter is pressed
quit = true;
break;
}
if( eventos.key.keysym.sym == SDLK_BACKSPACE && inputText.length() > 0 )
{// backspace
inputText.pop_back();
renderText = true;
}
}
else if( eventos.type == SDL_TEXTINPUT ) //to add keys pressed
{
inputText += eventos.text.text;
renderText = true;
}
}
if( renderText )
{
if( inputText != "" )
{
_Nombre_Jugador_Temp->cargar_texto( inputText.c_str());
}
else
{
_Nombre_Jugador_Temp->cargar_texto( " ");
}
}
//Clear screen
SDL_SetRenderDrawColor( _Render.get(), 0xFF, 0xFF, 0xFF, 0xFF );
SDL_RenderClear( _Render.get() );
//Render text textures
//!TODO. to implement multi-threading
_pedir_nombre_jugador->renderizar ( (ANCHO - _pedir_nombre_jugador->get_ancho())/2, REJILLA );
_Nombre_Jugador_Temp->renderizar ( (ANCHO - _Nombre_Jugador_Temp->get_ancho())/2 , 3*REJILLA );
_pulsa_intro_continuar->renderizar( (ANCHO - _pulsa_intro_continuar->get_ancho())/2 , 5*REJILLA );
//Update screen
SDL_RenderPresent( _Render.get() );
frame_end = SDL_GetTicks();
frame_duration = frame_end - frame_start;
if (frame_duration < target_frame_duration) {
SDL_Delay(target_frame_duration - frame_duration);
}
}
//Disable text input
SDL_StopTextInput();
//Text is not empty
if( inputText != "" )
{
//Render new text
_Nombre_Jugador_Temp->cargar_texto(inputText);
}
//Text is empty
else
{
//Render space texture
inputText = "Jugador 1";
_Nombre_Jugador_Temp->cargar_texto(inputText);
}
std::cout<< "Nombre del jugador: "<< inputText << std::endl;
}
bool Game_Init::inicializar()
{
bool correcto = true;
if( SDL_Init(SDL_INIT_VIDEO) < 0 )
{
std::cout<<"SDL_Init Error: " << SDL_GetError() <<std::endl;
correcto = false;
}
else
{
if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) ) //https://wiki.libsdl.org/SDL_HINT_RENDER_SCALE_QUALITY
std::cout<<"SDL_SetHint Error"<<std::endl;
// Window creation
_Ventana.reset(SDL_CreateWindow( "Tetris Game", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, ANCHO, ALTO, SDL_WINDOW_SHOWN ));
if( _Ventana == NULL )
{
std::cout<< "SDL_CreateWindow Error: "<< SDL_GetError() <<std::endl;
correcto = false;
}
else
{
//shared pointer with custom deleter //deleter
_Render.reset(SDL_CreateRenderer( _Ventana.get(), -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC ), SDL_DestroyRenderer);
if( _Render == NULL )
{
std::cout<< "SDL_CreateRenderer Error: " << SDL_GetError() <<std::endl;
correcto = false;
}
else
{
SDL_SetRenderDrawColor( _Render.get(), 0xFF, 0xFF, 0xFF, 0xFF );
}
}
}
if( TTF_Init() == -1 ){ // SDL module. To render text
std::cout<<"No se ha podido inicializar TTF_Init() "<< TTF_GetError() <<std::endl;
correcto = false;
}
return correcto;
}