forked from juakofz/Age-of-Space
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Global.cpp
199 lines (168 loc) · 4.02 KB
/
Global.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
#include "Global.h"
//SDL_Window* gWindow = NULL;
SDL_Renderer* gRenderer = NULL;
//Scene textures
Texture gSceneTexture;
//tamaño de la pantalla
const int SCREEN_WIDTH = 150;
const int SCREEN_HEIGHT = 150;
//Límite FPS
const int SCREEN_FPS = 60;
const int SCREEN_TICKS_PER_FRAME = 1000 / SCREEN_FPS;
//Starts up SDL and creates window
bool init();
//Frees media and shuts down SDL
void close();
//cargan el texto del reclado y lo renderizan
bool textinput(std::string *inputText, bool renderText, SDL_Event e);
void Textrender(std::string inputText, bool renderText, SDL_Color textColor, int tamaño);
//texturas del texto
Texture gPromptTextTexture;
Texture gInputTextTexture;
//Globally used font
TTF_Font *gFont = NULL;
//ventana
LWindow gWindow;
//textura texto
Texture gTextTexture;
//inicializa
bool init()
{
//Initialization flag
bool success = true;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Set texture filtering to linear
if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )
{
printf( "Warning: Linear texture filtering not enabled!" );
}
//Set mouse mode to relative
//SDL_SetRelativeMouseMode(SDL_TRUE);
//Create window
if( !gWindow.init() )
{
printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Create renderer for window
gRenderer = gWindow.createRenderer();
if( gRenderer == NULL )
{
printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Initialize renderer color
SDL_SetRenderDrawColor( gRenderer, 0xFF, 0xFF, 0xFF, 0xFF );
//Initialize PNG loading
int imgFlags = IMG_INIT_PNG;
if( !( IMG_Init( imgFlags ) & imgFlags ) )
{
printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
success = false;
}
//Initialize SDL_ttf
if( TTF_Init() == -1 )
{
printf( "SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError() );
success = false;
}
}
}
}
return success;
}
//cierra el programa
void close()
{
//Free loaded images
gTextTexture.free();
//Free global font
TTF_CloseFont( gFont );
gFont = NULL;
//Destroy window
SDL_DestroyRenderer( gRenderer );
gWindow.free();
gRenderer = NULL;
//Quit SDL subsystems
TTF_Quit();
IMG_Quit();
SDL_Quit();
}
typedef struct characts
{
Texture *tex;
string name;
};
//texto por teclado
bool textinput(std::string *inputText, bool renderText, SDL_Event e)
{
if( e.type == SDL_KEYDOWN )
{
//permite introducir texto
SDL_StartTextInput();
//Handle backspace
if( e.key.keysym.sym == SDLK_BACKSPACE && inputText->length() > 0 )
{
//lop off character
inputText->pop_back();
renderText = true;
}
//Handle copy
else if( e.key.keysym.sym == SDLK_c && SDL_GetModState() & KMOD_CTRL )
{
SDL_SetClipboardText( inputText->c_str() );
}
//Handle paste
//esta comentado porq me da un error
/*else if( e.key.keysym.sym == SDLK_v && SDL_GetModState() & KMOD_CTRL )
{
inputText = SDL_GetClipboardText();
renderText = true;
}*/
}
//Special text input event
else if( e.type == SDL_TEXTINPUT )
{
//Not copy or pasting
if( !( ( e.text.text[ 0 ] == 'c' || e.text.text[ 0 ] == 'C' ) && ( e.text.text[ 0 ] == 'v' || e.text.text[ 0 ] == 'V' ) && SDL_GetModState() & KMOD_CTRL ) )
{
//Append character
inputText->append(e.text.text);
renderText = true;
}
}
//devuelve si hay que renderizar el texto
return renderText;
//cierra la comunicacion con el telclado
SDL_StopTextInput();
}
//renderiza el texto
void Textrender(std::string inputText, bool renderText, SDL_Color textColor, int tamaño)
{
if( renderText )
{
//Text is not empty
if( inputText != "" )
{
//Render new text
gInputTextTexture.loadText( inputText.c_str(),tamaño, textColor );
}
//Text is empty
else
{
//Render space texture
gInputTextTexture.loadText( " ", tamaño, textColor );
}
}
}