-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
344 lines (242 loc) · 8.03 KB
/
main.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#include <iostream>
#include <sstream>
//todo ñïðîñè ïðî static class, ìåòîäû, àòòðèáóòû; extern
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_ttf.h"
#include <windows.h>
#include <string>
#include <cstdlib>
#include <ctime>
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
const int SCREEN_WIDTH = 850;
const int SCREEN_HEIGHT = 591+40; // 475
static std::string logs = " "; // ëîãè êîíñîëè
//Globally used font
TTF_Font *gFont = NULL; // øðèôò
SDL_Color textColor;
//SDL_Texture* mTexture;
//Image dimensions
int mWidth;
int mHeight;
//Rendered texture
//LTexture gTextTexture; // òåêñòóðà
std::string intToStr(int i)
{
std::string i_str; // string which will contain the result
std::ostringstream convert; // stream used for the conversion
convert << i; // insert the textual representation of 'Number' in the characters in the stream
i_str = convert.str(); // set 'Result' to the contents of the stream
return i_str;
}
SDL_Texture* LoadImage(std::string file) // ñîçäàíèå òåêñòóðû ïî èçîáðàæåíèþ
{
SDL_Surface *loadedImage = NULL;
SDL_Texture *texture = NULL;
file = "img/" + file;
loadedImage = IMG_Load(file.c_str());
if (loadedImage != NULL){
texture = SDL_CreateTextureFromSurface(renderer, loadedImage);
SDL_FreeSurface(loadedImage);
}
else
std::cout << SDL_GetError() << std::endl;
return texture;
}
bool loadFromRenderedText( std::string textureText, SDL_Color& textColor , TTF_Font *& gFont, int x, int y);
bool Print_Font(TTF_Font *& gFont, SDL_Color& textColor, std::string textureText, int x, int y, int size)
{
//Loading success flag
bool success = true;
//Open the font
gFont = TTF_OpenFont( "ttf/TIMCYR.TTF", size );
//std::cout << "gFont = " << gFont << std::endl;
if( gFont == NULL )
{
std::cout << "Failed to load lazy font! SDL_ttf Error: %s\n" << TTF_GetError() << std::endl;
success = false;
}
else
{
//Render text
SDL_Color textColor = { 255, 255, 255 };
if( !loadFromRenderedText( textureText, textColor, gFont, x, y) )
{
//std::cout << "Failed to render text texture!\n" << std::endl;
success = false;
}
//std::cout << "mTexture = " << mTexture << std::endl;
}
return success;
}
void ApplySurface(int x, int y, SDL_Texture *tex, SDL_Renderer *rend) // ðèñîâàíèå íà ðåíäåðå
{
SDL_Rect pos;
int frameWidth;
int frameHeight;
int textureWidth;
int textureHeight;
SDL_QueryTexture(tex, NULL, NULL, &textureWidth, &textureHeight);
frameWidth = textureWidth;
frameHeight = textureHeight;
pos.x = x;
pos.y = y;
pos.w = frameWidth;
pos.h = frameHeight;
SDL_RenderCopy(rend, tex, NULL, &pos);
}
void ApplySurface(int x, int y, SDL_Texture *tex, SDL_Renderer *rend, int playerNum) // ðèñîâàíèå íà ðåíäåðå
{
SDL_Rect pos;
SDL_Rect cut;
int frameWidth;
int frameHeight;
int textureWidth;
int textureHeight;
SDL_QueryTexture(tex, NULL, NULL, &textureWidth, &textureHeight);
frameWidth = textureWidth/2;
frameHeight = textureHeight;
pos.x = x;
pos.y = y;
pos.w = frameWidth;
pos.h = frameHeight;
SDL_QueryTexture(tex, NULL, NULL, &textureWidth, &textureHeight);
frameWidth = textureWidth/2;
frameHeight = textureHeight;
if(playerNum == 1)
cut.x = 0;
else
cut.x = frameWidth;
cut.y = 0;
cut.w = frameWidth;
cut.h = frameHeight;
SDL_RenderCopy(rend, tex, &cut, &pos);
}
bool loadFromRenderedText( std::string textureText, SDL_Color& textColor , TTF_Font *& gFont, int x, int y)
{
//Get rid of preexisting texture
//free();
//Render text surface
SDL_Texture* mTexture;
SDL_Surface* textSurface = TTF_RenderText_Solid( gFont, textureText.c_str(), textColor );
//std::cout << "textSurface = " << textSurface << std::endl;
if( textSurface == NULL )
{
std::cout << "Unable to render text surface! SDL_ttf Error: %s\n" << TTF_GetError() << std::endl;
}
else
{
//Create texture from surface pixels
//std::cout << "mTexture = " << mTexture << std::endl;
mTexture = SDL_CreateTextureFromSurface( renderer, textSurface );
//std::cout << "mTexture = " << mTexture << std::endl;
ApplySurface(x,y,mTexture,renderer);
if( mTexture == NULL )
{
std::cout << "Unable to create texture from rendered text! SDL Error: %s\n" << SDL_GetError() << std::endl;
}
/*else
{
//Get image dimensions
mWidth = textSurface->w;
mHeight = textSurface->h;
}*/
//Get rid of old surface
SDL_FreeSurface( textSurface );
}
//Return success
return mTexture != NULL;
}
int generate_in(int a, int b) // ãåíåðàòîð ñëó÷àíîãî ÷èñëà îò a äî b
{
if (b >= a)
{
//unsigned int tmp = 41;
srand(time(0));
int tmp = rand();
std::cout << "URON = " << tmp << std::endl;
tmp = tmp % (b - a + 1);
std::cout << "a + tmp = " << a + tmp << std::endl;
return a + tmp;
}
return 0;
}
#include "map.h"
#include "creature.h"
#include "undead.h"
#include "warrior.h"
#include "doctor.h"
#include "archer.h"
#include "vampire.h"
#include "Tent_ambulance.h"
#include "elf.h"
#include "skeleton_archer.h"
#include "monk.h"
#include "id.h"
#include "player.h"
#include "the_game.h"
// ãåíåðàöèÿ óíèêàëüíûõ id
using namespace std;
int main(int argc, char *argv[]) {
map::make_map(5, 10); // êàðòà
if (SDL_Init(SDL_INIT_EVERYTHING) == -1){
std::cout << SDL_GetError() << std::endl;
return 1;
}
if (TTF_Init() < 0)
{
cout << "Error: " << TTF_GetError() << endl;
};
window = SDL_CreateWindow("HMM", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL){
std::cout << SDL_GetError() << std::endl;
return 2;
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED
| SDL_RENDERER_PRESENTVSYNC);
if (renderer == NULL){
std::cout << SDL_GetError() << std::endl;
return 3;
}
//TTF_font * font = TTF_OpenFont("TIMCYR.ttf",10);
//SDL_Color color = {144, 77, 255, 255};
player player1(1);
player player2(2);
cout << "***********" << endl;
cout << "***********" << endl;
//SDL_RenderClear( renderer );
//Render current frame
//cout << "mTexture = " << mTexture << endl;
//cout << "gFont = " << gFont << endl;
//Print_Font(gFont, textColor, "IGOR", 30, 40);
//cout << "gFont = " << gFont << endl;
//loadFromRenderedText( "Igor", textColor, gFont, mTexture, renderer );
//cout << "mTexture = " << mTexture << endl;
//ApplySurface(0,0,mTexture,renderer);
//Update screen
//SDL_RenderPresent( renderer );
//SDL_Delay(3000);
player1.make_army(); // âûáîð èãðîêîì àðìèè
player2.make_army();
player1.put_creatures_on_map(); // ðàññòàíîâêà ïåðâîãî èãðîêà íà êàðòå
player2.put_creatures_on_map(); // ðàññòàíîâêà âòîðîãî èãðîêà íà êàðòå
the_game GAME(player1, player2);
//GAME.print_map();
GAME.start(player1, player2,renderer);
int pos[2];
//cout << "what is in point? " << map::get_creature_ID(1, 1) << endl;
map::destroy_map();
// SDL TEST
//Free loaded images
//gTextTexture.free();
TTF_CloseFont( gFont );
gFont = NULL;
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
TTF_Quit();
IMG_Quit();
SDL_Quit();
return 0;
}