0
|
1 /*
|
|
2 * LamerTetris 2
|
|
3 * author: prymula
|
|
4 * date: 10-VI-2023
|
|
5 * licence: Public Domain
|
|
6 * tool: Geany
|
|
7 * Program based on:
|
|
8 * -This source code copyrighted by Lazy Foo' Productions (2004-2020)
|
|
9 * and may not be redistributed without written permission.
|
|
10 * -Andrew Lim Chong Liang https://github.com/andrew-lim/sdl2-boilerplate
|
|
11 */
|
|
12
|
|
13
|
|
14 #include "game.h"
|
|
15
|
|
16 Game::Game()
|
|
17 :running(0), window(NULL), renderer(NULL) {
|
|
18 screenSurface = SDL_CreateRGBSurface(0,400,600,32,0,0,0,0);
|
|
19 }
|
|
20
|
|
21 Game::~Game() {
|
|
22 //delete actual_ball;
|
|
23 //delete octagon;
|
|
24 this->stop();
|
|
25 }
|
|
26
|
|
27 void Game::start() {
|
|
28
|
|
29 if (SDL_Init(SDL_INIT_EVERYTHING)) {
|
|
30 return ;
|
|
31 }
|
|
32 // Ten sposób tworzenia okna i renderu gwarantuje że duszki nie będą przerywać
|
|
33 window = SDL_CreateWindow( "Lamer Tetris", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, DISPLAY_WIDTH, DISPLAY_HEIGHT, SDL_WINDOW_SHOWN );
|
|
34 if( window == NULL )
|
|
35 {
|
|
36 printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
|
|
37 return;
|
|
38 }
|
|
39 renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC );
|
|
40 if( renderer == NULL )
|
|
41 {
|
|
42 printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
|
|
43 return;
|
|
44 }
|
|
45 if(TTF_Init()==-1) {
|
|
46 printf("TTF_Init: %s\n", TTF_GetError());
|
|
47 return;
|
|
48 }
|
|
49
|
|
50 if( !loadWallpaper()){
|
|
51 printf( "Failed to load media!\n" );
|
|
52 exit(1);
|
|
53 }
|
|
54
|
|
55 if(hiscore.file_exist())
|
|
56 hi_score = hiscore.load_hiscore();
|
|
57 else hi_score = 0;
|
|
58
|
|
59 this->running = 1 ;
|
|
60 }
|
|
61
|
|
62 void Game::draw(SDL_Rect *srcField, SDL_Rect *positionField, bool end) {
|
|
63
|
|
64 SDL_Rect destrect; //, srcrect;
|
|
65 SDL_Surface * scoreSurface;
|
|
66
|
|
67 //destrect = set_rect(0, 0, background->w, background->h);
|
|
68 //srcrect = set_rect(0, 0, background->w, background->h);
|
|
69 SDL_BlitSurface(background, NULL, screenSurface, NULL);
|
|
70
|
|
71 // dla elementu wysuwanego
|
|
72 if(srcField->y>0 && !end){
|
|
73 SDL_BlitSurface( field.get(figure,n_element), srcField, screenSurface, positionField );
|
|
74 } else // dla elementu wysunietego
|
|
75 SDL_BlitSurface( field.get(figure,n_element), srcField, screenSurface, positionField );
|
|
76
|
|
77
|
|
78 szlam.draw();
|
|
79 SDL_BlitSurface( szlam.get(), NULL, screenSurface, NULL );
|
|
80
|
|
81 // punkty na tapete
|
|
82 scoreSurface = scoreTxt.loadFromRenderedText(std::to_string(score) ,{0xff, 0xff, 0xff}, fontScore);
|
|
83 destrect = set_rect(10, 3, background->w, background->h);
|
|
84 SDL_BlitSurface( scoreSurface, NULL, screenSurface, &destrect );
|
|
85 SDL_FreeSurface( scoreSurface );
|
|
86
|
|
87 // najlepszy wynik na tapete
|
|
88 scoreSurface = scoreTxt.loadFromRenderedText(std::to_string(hi_score) ,{0xe1, 0x06, 0x40}, fontScore);
|
|
89 destrect = set_rect(300, 3, background->w, background->h);
|
|
90 SDL_BlitSurface( scoreSurface, NULL, screenSurface, &destrect );
|
|
91 SDL_FreeSurface( scoreSurface );
|
|
92
|
|
93
|
|
94
|
|
95 texture = SDL_CreateTextureFromSurface( renderer, screenSurface);
|
|
96 if( texture == NULL )
|
|
97 {
|
|
98 printf( "Unable to create texture ! SDL Error: %s\n", SDL_GetError() );
|
|
99 exit(1);
|
|
100 }
|
|
101 destrect = set_rect(0, 0, background->w, background->h);
|
|
102 SDL_RenderCopy(renderer, texture, NULL, &destrect);
|
|
103
|
|
104 SDL_RenderPresent(renderer);
|
|
105 SDL_DestroyTexture(texture);
|
|
106
|
|
107 }
|
|
108
|
|
109 void Game::stop() {
|
|
110 if (NULL != renderer) {
|
|
111 SDL_DestroyRenderer(renderer);
|
|
112 renderer = NULL;
|
|
113 }
|
|
114 if (NULL != window) {
|
|
115 SDL_DestroyWindow(window);
|
|
116 window = NULL;
|
|
117 }
|
|
118 SDL_Quit() ;
|
|
119 }
|
|
120
|
|
121 void Game::fillRect(SDL_Rect* rc, int r, int g, int b ) {
|
|
122 SDL_SetRenderDrawColor(renderer, r, g, b, SDL_ALPHA_OPAQUE);
|
|
123 SDL_RenderFillRect(renderer, rc);
|
|
124 }
|
|
125
|
|
126 void Game::fpsChanged( int fps ) {
|
|
127 char szFps[ 128 ] ;
|
|
128 sprintf( szFps, "%s: %d FPS", "Lamer Tetris", fps );
|
|
129 SDL_SetWindowTitle(window, szFps);
|
|
130 }
|
|
131 /*
|
|
132 void Game::onQuit() {
|
|
133
|
|
134 running = 0 ;
|
|
135 }
|
|
136 */
|
|
137 void Game::up_direction() {
|
|
138 tmp_element=n_element;
|
|
139 if(tmp_element<3) {
|
|
140 tmp_element+=1;
|
|
141 } else tmp_element=0;
|
|
142
|
|
143 if(!szlam.collisionRotate(field.get(figure,tmp_element), tmp_element,
|
|
144 figure, positionField.x, positionField.y, field.getColor(figure)))
|
|
145 if (field.checkEdgeRotate(field.get(figure, tmp_element), positionField.x)) {
|
|
146 if(n_element<3) {
|
|
147 n_element+=1;
|
|
148 } else n_element=0;
|
|
149 }
|
|
150 }
|
|
151
|
|
152 void Game::left_direction() {
|
|
153 left=0;
|
|
154 if(!szlam.collisionLeft(field.get(figure,n_element), n_element,
|
|
155 figure, positionField.x, positionField.y, field.getColor(figure)))
|
|
156 if (field.checkEdgeLeft(field.get(figure, n_element), positionField.x))
|
|
157 positionField.x-=20;
|
|
158 }
|
|
159 void Game::right_direction() {
|
|
160 if(!szlam.collisionRight(field.get(figure,n_element), n_element,
|
|
161 figure, positionField.x, positionField.y, field.getColor(figure)))
|
|
162 if (field.checkEdgeRight(field.get(figure, n_element), positionField.x))
|
|
163 positionField.x+=20;
|
|
164 }
|
|
165 void Game::run() {
|
|
166 int past = SDL_GetTicks();
|
|
167 int now = past, pastFps = past ;
|
|
168 int fps = 0;
|
|
169 bool end = SDL_FALSE;
|
|
170 int rate = 1;
|
|
171 SDL_Joystick *joystick;
|
|
172 joystick = SDL_JoystickOpen(0);
|
|
173 score = 0;
|
|
174
|
|
175 positionField = set_rect(200, 0, 80, 0);
|
|
176 srcField = set_rect(0, 0, 80, 80);
|
|
177
|
|
178 printf("Name: %s\n", SDL_JoystickNameForIndex(0));
|
|
179
|
|
180 SDL_Event event ;
|
|
181 while ( running ) {
|
|
182 int timeElapsed = 0 ;
|
|
183 if (SDL_PollEvent(&event)) {
|
|
184
|
|
185 switch (event.type) {
|
|
186 case SDL_QUIT:
|
|
187 if (score > hi_score)
|
|
188 {
|
|
189 hi_score=score;
|
|
190 hiscore.save_hiscore(hi_score);
|
|
191 }
|
|
192
|
|
193 running = 0 ;
|
|
194 break;
|
|
195 case SDL_KEYDOWN:
|
|
196 if (event.key.keysym.sym==SDLK_ESCAPE)
|
|
197 running = 0;
|
|
198
|
|
199 if (event.key.keysym.sym==SDLK_SPACE) {
|
|
200 if (end) {
|
|
201 szlam.reset();
|
|
202 end = false;
|
|
203 score = 0;
|
|
204 figure = rand()%7;
|
|
205 }
|
|
206 }
|
|
207 if (event.key.keysym.sym==SDLK_UP) {
|
|
208 up_direction();
|
|
209 }
|
|
210 if (event.key.keysym.sym==SDLK_LEFT) {
|
|
211 left_direction();
|
|
212 }
|
|
213 if (event.key.keysym.sym==SDLK_RIGHT) {
|
|
214 right_direction();
|
|
215
|
|
216 }
|
|
217 if (event.key.keysym.sym==SDLK_DOWN) {
|
|
218 rate=5;
|
|
219 }
|
|
220 if (event.key.keysym.sym==SDLK_f) {
|
|
221 if(figure<NFIGURE-1) {
|
|
222 figure+=1;
|
|
223 } else figure=0;
|
|
224 }
|
|
225 break;
|
|
226 case SDL_JOYBUTTONDOWN:
|
|
227 printf("button: %i\n", event.jbutton.button);
|
|
228 // góa
|
|
229 if (event.jbutton.button == 3) {
|
|
230 up_direction();
|
|
231 }
|
|
232 // lewo
|
|
233 if (event.jbutton.button == 2) {
|
|
234 left_direction();
|
|
235 }
|
|
236 // prawo
|
|
237 if (event.jbutton.button == 1) {
|
|
238 right_direction();
|
|
239 }
|
|
240 // dół
|
|
241 if (event.jbutton.button == 0) {
|
|
242 rate = 5;
|
|
243 }
|
|
244 // back
|
|
245 if (event.jbutton.button == 6) {
|
|
246 if (score > hi_score)
|
|
247 {
|
|
248 hi_score=score;
|
|
249 hiscore.save_hiscore(hi_score);
|
|
250 }
|
|
251 running = 0;
|
|
252 }
|
|
253 // lewy przedni
|
|
254 if (event.jbutton.button == 4) {
|
|
255 if(figure>0) {
|
|
256 figure-=1;
|
|
257 } else figure=NFIGURE-1;
|
|
258 }
|
|
259 // prawy przedni
|
|
260 if (event.jbutton.button == 5) {
|
|
261 if(figure<NFIGURE-1) {
|
|
262 figure+=1;
|
|
263 } else figure=0;
|
|
264 }
|
|
265 break;
|
|
266 case SDL_JOYAXISMOTION:
|
|
267 printf("axis: %i %i\n", event.jaxis.axis, event.jaxis.value);
|
|
268 break;
|
|
269 case SDL_KEYUP:
|
|
270 case SDL_MOUSEBUTTONDOWN:
|
|
271 case SDL_MOUSEBUTTONUP:
|
|
272 case SDL_MOUSEMOTION:
|
|
273 break ;
|
|
274 }
|
|
275 }
|
|
276
|
|
277 timeElapsed = (now=SDL_GetTicks()) - past ;
|
|
278 if ( timeElapsed >= UPDATE_INTERVAL ) {
|
|
279 past = now ;
|
|
280
|
|
281 if (szlam.addBottom(field.get(figure,n_element), figure, n_element,
|
|
282 field.getColor(figure),positionField.x,positionField.y )){
|
|
283 printf ("Dodaje na dole\n");
|
|
284 positionField.y=0;
|
|
285 positionField.x=(rand()%6-1)*80;
|
|
286 positionField.h=0;
|
|
287 srcField.y=80;
|
|
288 srcField.h=0;
|
|
289 figure = rand()%7;
|
|
290 rate=1;
|
|
291
|
|
292 score++;
|
|
293 // sprawdza czy równa powierzchnia
|
|
294 int heightLevel=0, evenLevel = szlam.evenSurface(&heightLevel);
|
|
295 if (evenLevel) {
|
|
296 szlam.cutSurface(evenLevel, heightLevel);
|
|
297 score += 15*evenLevel;
|
|
298 }
|
|
299 //bottom = 1;
|
|
300
|
|
301 }
|
|
302 else if (szlam.collisionBottom(field.get(figure,n_element), n_element,
|
|
303 figure, positionField.x, positionField.y, field.getColor(figure))){
|
|
304 if (!end){
|
|
305 printf ("góra\n");
|
|
306 positionField.y=0;
|
|
307 positionField.x=(rand()%6-1)*80;
|
|
308 positionField.h=0;
|
|
309 srcField.y=80;
|
|
310 srcField.h=0;
|
|
311 figure = rand()%7;
|
|
312 rate=1;
|
|
313 score++;
|
|
314
|
|
315 // sprawdza czy równa powierzchnia
|
|
316 int heightLevel=0, evenLevel = szlam.evenSurface(&heightLevel);
|
|
317 if (evenLevel) {
|
|
318 printf ("b1\n");
|
|
319 szlam.cutSurface(evenLevel, heightLevel);
|
|
320 score += 15*evenLevel;
|
|
321 }
|
|
322 }
|
|
323
|
|
324 }
|
|
325
|
|
326 if (szlam.highFull()) end = SDL_TRUE;
|
|
327
|
|
328 draw(&srcField, &positionField, end); // figure -- element
|
|
329 if(srcField.y>0 && !end){
|
|
330 srcField.y--; srcField.h++;
|
|
331 }
|
|
332 else if (srcField.y==0 && !end) {
|
|
333 positionField.y+=rate;
|
|
334 ++fps ;
|
|
335 }
|
|
336 if ( now - pastFps >= 1000 ) {
|
|
337 pastFps = now ;
|
|
338 fpsChanged( fps );
|
|
339 fps = 0 ;
|
|
340 }
|
|
341 }
|
|
342 // sleep?
|
|
343 SDL_Delay( 1 );
|
|
344 }
|
|
345 //SDL_FreeSurface(background);
|
|
346 //SDL_FreeSurface(screenSurface);
|
|
347 SDL_JoystickClose(joystick);
|
|
348 SDL_DestroyTexture(texture);
|
|
349 SDL_DestroyRenderer(renderer);
|
|
350 SDL_DestroyWindow(window);
|
|
351 SDL_Quit();
|
|
352
|
|
353 }
|
|
354
|
|
355 bool Game::loadFromFile( std::string path, SDL_Renderer * renderer)
|
|
356 {
|
|
357
|
|
358 SDL_Texture* backgroundTexture = NULL;
|
|
359 if (background) SDL_FreeSurface(background);
|
|
360 background = SDL_LoadBMP(path.c_str());
|
|
361
|
|
362 if( background == NULL )
|
|
363 {
|
|
364 printf( "Unable to load image %s!\n", path.c_str() );
|
|
365 }
|
|
366 else
|
|
367 {
|
|
368 //Color key image
|
|
369 //SDL_SetColorKey( background, SDL_TRUE, SDL_MapRGB( background>format, 0, 0xFF, 0xFF ) );
|
|
370 backgroundTexture = SDL_CreateTextureFromSurface( renderer, background );
|
|
371
|
|
372 if( backgroundTexture == NULL )
|
|
373 {
|
|
374 printf( "Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError() );
|
|
375 }
|
|
376 }
|
|
377 return backgroundTexture != NULL;
|
|
378 }
|
|
379
|
|
380 bool Game::loadWallpaper()
|
|
381 {
|
|
382 #define SIZEFONT 35
|
|
383
|
|
384 #if WINDOWS
|
|
385 #define FONT "./assets/SpicyRice.ttf"
|
|
386 #define BACK "./assets/wallpaper.bmp"
|
|
387 #elif LINUX
|
|
388 //#define FONT "./assets/SpicyRice.ttf"
|
|
389 //#define BACK "./assets/wallpaper.bmp"
|
|
390 #define FONT "/usr/share/lamertetris/SpicyRice.ttf"
|
|
391 #define BACK "/usr/share/lamertetris/img/wallpaper.bmp"
|
|
392 #endif
|
|
393 //Loading success flag
|
|
394 bool success = true;
|
|
395 for (int i = 0; i<1; i++) {
|
|
396 //Load dot texture
|
|
397 if (!loadFromFile( BACK, renderer))
|
|
398 {
|
|
399 printf( "Failed to load dot texture!\n" );
|
|
400 success = false;
|
|
401 }
|
|
402 }
|
|
403
|
|
404 if((gFont = TTF_OpenFont(FONT, SIZEFONT)) == NULL) {
|
|
405 printf("TTF_OpenFont: %s\n", TTF_GetError());
|
|
406 exit(1);
|
|
407 }
|
|
408 if((fontScore = TTF_OpenFont(FONT, SIZEFONT)) == NULL) {
|
|
409 printf("TTF_OpenFont: %s\n", TTF_GetError());
|
|
410 exit(1);
|
|
411 }
|
|
412
|
|
413 return success;
|
|
414 }
|
|
415
|
|
416 SDL_Rect set_rect(int x, int y, int w, int h)
|
|
417 {
|
|
418 SDL_Rect rect;
|
|
419 rect.x = x;
|
|
420 rect.y = y;
|
|
421 rect.w = w;
|
|
422 rect.h = h;
|
|
423 return rect;
|
|
424 }
|
|
425
|
|
426 int main(int argc, char** argv){
|
|
427
|
|
428 Game game;
|
|
429 game.start();
|
|
430 game.run();
|
|
431 return 0;
|
|
432 }
|