0
|
1 /*
|
|
2 arkanoid - moja implementacja klasyka
|
|
3 wymaga: SDL2, SDL_TTF2
|
|
4 09-01-2021
|
|
5 licencja: Public Domain
|
|
6 edytor: VS Code
|
|
7 utworzono na platformie: Windows 10 - MinGW32
|
|
8 */
|
|
9
|
|
10 #include "arkanoid.h"
|
|
11 #include "levels.h"
|
|
12
|
|
13 Kafel kafle[8][8];
|
|
14 SDL_Surface * gumka_kafla;
|
|
15 SDL_Window *gWindow=NULL;
|
|
16 SDL_Renderer * renderer=NULL;
|
|
17 SDL_Texture * tex_screen=NULL;
|
|
18
|
|
19 int liczba_kafli=0;
|
|
20 int wynik, najlepszy, poziom, zycia;
|
|
21
|
|
22 void rysuj_obrazek (int x, int y, SDL_Surface * obrazek, int przezroczystosc);
|
|
23 SDL_Surface * utworz_powierzchnie(int sz, int w, int r, int g, int b);
|
|
24
|
|
25 SDL_Rect set_rect(int x, int y, int w, int h)
|
|
26 {
|
|
27 SDL_Rect rect;
|
|
28 rect.x = x;
|
|
29 rect.y = y;
|
|
30 rect.w = w;
|
|
31 rect.h = h;
|
|
32 return rect;
|
|
33 }
|
|
34
|
|
35 Uint32 getpixel(SDL_Surface *surface, int x, int y)
|
|
36 {
|
|
37 int bpp = surface->format->BytesPerPixel;
|
|
38 /* Here p is the address to the pixel we want to retrieve */
|
|
39 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
|
|
40
|
|
41 switch(bpp) {
|
|
42 case 1:
|
|
43 return *p;
|
|
44 case 2:
|
|
45 return *(Uint16 *)p;
|
|
46 case 3:
|
|
47 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
|
|
48 return p[0] << 16 | p[1] << 8 | p[2];
|
|
49 else
|
|
50 return p[0] | p[1] << 8 | p[2] << 16;
|
|
51 case 4:
|
|
52 return *(Uint32 *)p;
|
|
53 default:
|
|
54 return 0;
|
|
55 }
|
|
56 }
|
|
57
|
|
58
|
|
59
|
|
60 void putpixel(SDL_Surface *surface, int x, int y, Uint8 R, Uint8 G, Uint8 B)
|
|
61 {
|
|
62 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x *
|
|
63 surface->format->BytesPerPixel;
|
|
64 if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
|
|
65 {
|
|
66 p[0] = R;
|
|
67 p[1] = G;
|
|
68 p[2] = B;
|
|
69 }
|
|
70 else
|
|
71 {
|
|
72 p[0] = B;
|
|
73 p[1] = G;
|
|
74 p[2] = R;
|
|
75 }
|
|
76 }
|
|
77
|
|
78 SDL_Surface * preparuj_tekst(int r, int b, int g, char *bufor) {
|
|
79
|
|
80 SDL_Color fgcolor;
|
|
81
|
|
82 fgcolor.r = r;
|
|
83 fgcolor.b = b;
|
|
84 fgcolor.g = g;
|
|
85
|
|
86 return TTF_RenderText_Solid(font_panel, bufor, fgcolor);
|
|
87 }
|
|
88
|
|
89
|
|
90 void print_text(int r, int g, int b, char *c, int x, int y, int z) {
|
|
91
|
|
92 SDL_Surface * obraz_info;
|
|
93 SDL_Surface * gum_info;
|
|
94 SDL_Rect zrodlo, cel;
|
|
95
|
|
96 obraz_info = (SDL_Surface *) preparuj_tekst(r, g, b, c);
|
|
97
|
|
98 // chce wymazac napis
|
|
99 //cel = set_rect(obraz_info->clip_rect.x, obraz_info->clip_rect.y,
|
|
100 // obraz_info->w, obraz_info->h);
|
|
101 cel = set_rect(x, y,
|
|
102 obraz_info->w, obraz_info->h);
|
|
103 zrodlo = set_rect(0, 0, obraz_info->w, obraz_info->h);
|
|
104 gum_info=utworz_powierzchnie(obraz_info->w, obraz_info->h, 65, 190, 215);
|
|
105
|
|
106 SDL_BlitSurface(gum_info, &zrodlo, obraz, &cel);
|
|
107 SDL_FreeSurface(gum_info);
|
|
108
|
|
109 rysuj_obrazek(x, y, obraz_info, 1);
|
|
110 SDL_FreeSurface(obraz_info);
|
|
111 }
|
|
112
|
|
113
|
|
114 SDL_Surface * ustaw_kolor (Kolor kol) {
|
|
115
|
|
116 SDL_Surface * kafel;
|
|
117 SDL_Rect cel;
|
|
118
|
|
119 kafel = SDL_CreateRGBSurface(SDL_SWSURFACE, XKAFLA, YKAFLA,16,0,0,0,0);
|
|
120 cel = set_rect(0, 0, XKAFLA, YKAFLA);
|
|
121 SDL_FillRect(kafel, &cel, SDL_MapRGB(kafel->format, kol.r, kol.g, kol.b));
|
|
122
|
|
123 return kafel;
|
|
124
|
|
125 }
|
|
126
|
|
127 void ustaw_kolor_tla(void) {
|
|
128
|
|
129 SDL_Rect przez;
|
|
130 //obraz->clip_rect.x=0;
|
|
131 //obraz->clip_rect.y=0;
|
|
132 //obraz->clip_rect.w=640;
|
|
133 //obraz->clip_rect.h=480;
|
|
134 asm("nop");
|
|
135 przez = set_rect(0, 0, obraz->w, obraz->h);
|
|
136 //przez = set_rect(0, 0, 640, 480);
|
|
137
|
|
138 SDL_FillRect(obraz, &przez, SDL_MapRGB(obraz->format,65,190,215));
|
|
139 //SDL_Flip(obraz);
|
|
140 SDL_RenderPresent ( renderer );
|
|
141 }
|
|
142
|
|
143 void erase (RuchomyObiekt *p){
|
|
144
|
|
145 SDL_Rect zrodlo, cel;
|
|
146
|
|
147 cel = set_rect(p->px, p->py, p->gumka->w, p->gumka->h);
|
|
148 zrodlo = set_rect(0, 0, p->gumka->w, p->gumka->h);
|
|
149 SDL_BlitSurface(p->gumka,&zrodlo,obraz,&cel);
|
|
150 }
|
|
151
|
|
152
|
|
153 void przerysuj(RuchomyObiekt *p) {
|
|
154 SDL_Rect zrodlo, cel;
|
|
155 // wymazuje stara pozycje
|
|
156 cel = set_rect(p->px, p->py, p->gumka->w, p->gumka->h);
|
|
157 zrodlo = set_rect(0, 0, p->gumka->w, p->gumka->h);
|
|
158 SDL_BlitSurface(p->gumka,&zrodlo,obraz,&cel);
|
|
159 // rysuje na nowej pozycji
|
|
160 cel = set_rect(p->ax, p->ay, p->obraz->w, p->obraz->h);
|
|
161 zrodlo = set_rect(0, 0, p->obraz->w, p->obraz->h);
|
|
162 SDL_BlitSurface(p->obraz,&zrodlo,obraz,&cel);
|
|
163
|
|
164 }
|
|
165
|
|
166 void rysuj_obrazek (int x, int y, SDL_Surface * obrazek, int przezroczystosc) {
|
|
167
|
|
168 SDL_Rect srcrect, destrect;
|
|
169
|
|
170 if (przezroczystosc) SDL_SetColorKey(obrazek,SDL_TRUE,getpixel(obrazek,0,0));
|
|
171
|
|
172 destrect = set_rect(x, y, obrazek->w, obrazek->h);
|
|
173 srcrect = set_rect(0,0,obrazek->w, obrazek->h);
|
|
174 SDL_BlitSurface(obrazek,&srcrect,obraz,&destrect);
|
|
175 }
|
|
176
|
|
177
|
|
178 SDL_Surface * utworz_powierzchnie(int sz, int w, int r, int g, int b) {
|
|
179
|
|
180 SDL_Surface * powierzchnia;
|
|
181 SDL_Rect cel;
|
|
182
|
|
183 powierzchnia = SDL_CreateRGBSurface(SDL_SWSURFACE, sz, w,16,0,0,0,0);
|
|
184 cel = set_rect(0, 0, sz, w);
|
|
185 SDL_FillRect(powierzchnia, &cel, SDL_MapRGB(powierzchnia->format, r, g, b));
|
|
186
|
|
187 return powierzchnia;
|
|
188 }
|
|
189
|
|
190 Uint32 ruch_pilki(Uint32 interval, void *param)
|
|
191 {
|
|
192 SDL_Rect zrodlo, cel;
|
|
193 Teczka * t = (Teczka *) param;
|
|
194 int i, j;
|
|
195
|
|
196 przerysuj(t->pilka);
|
|
197
|
|
198 if (t->pilka->ax <= 5) t->pilka->kx=5;
|
|
199 if (t->pilka->ax >= 605) t->pilka->kx=-5;
|
|
200 if (t->pilka->ay <=30) t->pilka->ky=5;
|
|
201
|
|
202 /* odpicie przez paletke */
|
|
203 if (t->pilka->ax+XPILKI >= t->paletka->ax && (t->pilka->ax) <= (t->paletka->ax+XPALETKI) &&
|
|
204 t->pilka->ay >= 420) {
|
|
205
|
|
206 /* odbicie lewym brzegiem */
|
|
207 if (t->pilka->ax >= t->paletka->ax && t->pilka->ax <= t->paletka->ax+30) {
|
|
208 t->pilka->kx=-5;
|
|
209 printf ("kx = -5\n");
|
|
210
|
|
211 }
|
|
212 /* odbicie lewym srodkiem */
|
|
213 else if (t->pilka->ax >= t->paletka->ax+30 && t->pilka->ax <= t->paletka->ax+60) {
|
|
214 t->pilka->kx=-2;
|
|
215 printf ("kx = -2\n");
|
|
216 }
|
|
217 /* odbicie prawym srodkiem */
|
|
218 else if (t->pilka->ax >= t->paletka->ax+60 && t->pilka->ax <= t->paletka->ax+90) {
|
|
219 t->pilka->kx=2;
|
|
220 printf ("kx = 2\n");
|
|
221 }
|
|
222 /* odbicie prawym brzegiem */
|
|
223 else if (t->pilka->ax >= t->paletka->ax+90 && t->pilka->ax <= t->paletka->ax+120) {
|
|
224 // t->pilka->kx=-5;
|
|
225 t->pilka->kx=5;
|
|
226 printf ("kx = 5\n");
|
|
227
|
|
228 }
|
|
229
|
|
230 /* odbijamy od paletki */
|
|
231 t->pilka->ky=-5;
|
|
232 }
|
|
233
|
|
234 int flaga_zmiany=0; // zapobiega przebijaniu sie pilki przez kafle
|
|
235 /* reakcje pilki z kaflami */
|
|
236 for (i=0; i<8; i++) {
|
|
237
|
|
238 for (j=0; j<8; j++) {
|
|
239 if (kafle[i][j].obraz != NULL) {
|
|
240
|
|
241 if (t->pilka->ay+YPILKI >= kafle[i][j].y &&
|
|
242 t->pilka->ay <= kafle[i][j].y+YKAFLA &&
|
|
243 t->pilka->ax >= kafle[i][j].x-XPILKI &&
|
|
244 t->pilka->ax <= kafle[i][j].x+XKAFLA) {
|
|
245 #define XTOL 1
|
|
246 // #define YTOL 3
|
|
247 printf ("twardosc: %d\n", kafle[i][j].twardosc);
|
|
248 if (kafle[i][j].twardosc !=2) {
|
|
249 rysuj_obrazek(kafle[i][j].x, kafle[i][j].y, gumka_kafla, 0);
|
|
250 kafle[i][j].obraz=NULL;
|
|
251 // w ktora strone znikajacy kafel odbije pilke
|
|
252
|
|
253 // lewy przeg kafla
|
|
254
|
|
255 printf ("ax: %d kafel %d \n", t->pilka->ax, kafle[i][j].x+XKAFLA);
|
|
256
|
|
257 if (t->pilka->ay+YPILKI >= kafle[i][j].y &&
|
|
258 t->pilka->ay <= kafle[i][j].y+YKAFLA &&
|
|
259 t->pilka->ax >= kafle[i][j].x-XPILKI &&
|
|
260 t->pilka->ax <= kafle[i][j].x-XPILKI+XTOL) {
|
|
261
|
|
262 if (!flaga_zmiany) {
|
|
263 t->pilka->kx = -5;
|
|
264 flaga_zmiany=1;
|
|
265 }
|
|
266
|
|
267 printf ("lewy bok\n");
|
|
268 liczba_kafli--;
|
|
269 wynik+=PUNKTY;
|
|
270 //
|
|
271 }
|
|
272 // prawy przeg kafla
|
|
273 else if (t->pilka->ay+YPILKI >= kafle[i][j].y &&
|
|
274 t->pilka->ay <= kafle[i][j].y+YKAFLA &&
|
|
275 t->pilka->ax <= kafle[i][j].x+XKAFLA &&
|
|
276 t->pilka->ax >= kafle[i][j].x+XKAFLA-XTOL) {
|
|
277
|
|
278 if (!flaga_zmiany) {
|
|
279 t->pilka->kx = 5;
|
|
280 flaga_zmiany=1;
|
|
281 }
|
|
282 printf ("prawy bok\n");
|
|
283 liczba_kafli--;
|
|
284 wynik+=PUNKTY;
|
|
285 //
|
|
286 }
|
|
287 else if (t->pilka->ky == 5) {
|
|
288 if (!flaga_zmiany) {
|
|
289 t->pilka->ky =-5;
|
|
290 flaga_zmiany=1;
|
|
291 }
|
|
292 printf ("zmina kirunku: do gory (%d)\n", t->pilka->ay);
|
|
293 liczba_kafli--;
|
|
294 wynik+=PUNKTY;
|
|
295 }
|
|
296 else if (t->pilka->ky == -5) {
|
|
297 if (!flaga_zmiany) {
|
|
298 t->pilka->ky =5;
|
|
299 flaga_zmiany=1;
|
|
300 }
|
|
301 printf ("zmina kirunku: na dól %d \n", t->pilka->ay );
|
|
302 liczba_kafli--;
|
|
303 wynik+=PUNKTY;
|
|
304 }
|
|
305
|
|
306
|
|
307 printf ("\n\nliczba kafli: %d\n", liczba_kafli);
|
|
308 }
|
|
309 /* jesli twardosc 2 */
|
|
310 else {
|
|
311 if (t->pilka->ay+YPILKI >= kafle[i][j].y &&
|
|
312 t->pilka->ay <= kafle[i][j].y+YKAFLA &&
|
|
313 t->pilka->ax >= kafle[i][j].x-XPILKI &&
|
|
314 t->pilka->ax <= kafle[i][j].x-XPILKI+XTOL) {
|
|
315
|
|
316 if (!flaga_zmiany) {
|
|
317 t->pilka->kx = -5;
|
|
318 flaga_zmiany=1;
|
|
319 }
|
|
320 printf ("lewy bok\n");
|
|
321 // liczba_kafli--;
|
|
322 kafle[i][j].twardosc=1;
|
|
323 //
|
|
324 }
|
|
325 // prawy przeg kafla
|
|
326 else if (t->pilka->ay+YPILKI >= kafle[i][j].y &&
|
|
327 t->pilka->ay <= kafle[i][j].y+YKAFLA &&
|
|
328 t->pilka->ax <= kafle[i][j].x+XKAFLA &&
|
|
329 t->pilka->ax >= kafle[i][j].x+XKAFLA-XTOL) {
|
|
330
|
|
331 if (!flaga_zmiany) {
|
|
332 t->pilka->kx = 5;
|
|
333 flaga_zmiany=1;
|
|
334 }
|
|
335
|
|
336 printf ("prawy bok\n");
|
|
337 // liczba_kafli--;
|
|
338 kafle[i][j].twardosc=1;
|
|
339 //
|
|
340 }
|
|
341
|
|
342 else if (t->pilka->ky == 5) {
|
|
343 if (!flaga_zmiany) {
|
|
344 t->pilka->ky =-5;
|
|
345 flaga_zmiany=1;
|
|
346 }
|
|
347 printf ("zmina kirunku: do gory (%d)\n", t->pilka->ay);
|
|
348 liczba_kafli--;
|
|
349 }
|
|
350 else if (t->pilka->ky == -5) {
|
|
351 if (!flaga_zmiany) {
|
|
352 t->pilka->ky =5;
|
|
353 flaga_zmiany=1;
|
|
354 }
|
|
355 printf ("zmina kirunku: na dól %d \n", t->pilka->ay );
|
|
356 liczba_kafli--;
|
|
357 }
|
|
358 printf("opuszczam Twardosc 2\n");
|
|
359 }
|
|
360
|
|
361 }
|
|
362 }
|
|
363 }
|
|
364 }
|
|
365
|
|
366
|
|
367
|
|
368 t->pilka->px=t->pilka->ax;
|
|
369 t->pilka->py=t->pilka->ay;
|
|
370 t->pilka->ax+=t->pilka->kx;
|
|
371 t->pilka->ay+=t->pilka->ky;
|
|
372
|
|
373 //return interval;
|
|
374 }
|
|
375
|
|
376
|
|
377
|
|
378 /* poziom liczymy od zera */
|
|
379 void zaladuj_poziom(SDL_Surface ** kolor_kafla, int poziom) {
|
|
380
|
|
381 int i, j, l, x, y;
|
|
382
|
|
383 printf ("bede rysowac poziom: %d\n", poziom);
|
|
384 /* dane odczytwane beda dla poziomu 'level' */
|
|
385 l = poziom*8;
|
|
386 liczba_kafli=0;
|
|
387 x=0;
|
|
388 y=30;
|
|
389 // wiersze
|
|
390 for (i=0; i < 8; ++i) {
|
|
391 // kolumny
|
|
392 for (j = 0; j < 8; ++j) {
|
|
393
|
|
394 printf ("levels -%d-", levels[i+l][j]);
|
|
395 /* jesli pole rozne od zera to jakis kafel */
|
|
396 if (levels[i+l][j] != 48) {
|
|
397 kafle[i][j].obraz= kolor_kafla[levels[i+l][j]-48];
|
|
398
|
|
399 if (levels[i+l][j] != 52) kafle[i][j].twardosc=1;
|
|
400 else kafle[i][j].twardosc=2;
|
|
401
|
|
402 kafle[i][j].x = x;
|
|
403 kafle[i][j].y = y;
|
|
404 liczba_kafli++;
|
|
405 }
|
|
406 else {
|
|
407 kafle[i][j].obraz= NULL;
|
|
408 }
|
|
409 x+=XKAFLA;
|
|
410 }
|
|
411 x=0;
|
|
412 y+=YKAFLA;
|
|
413
|
|
414 putchar('\n');
|
|
415 // if (i == 19) putchar('\n');
|
|
416 }
|
|
417
|
|
418 }
|
|
419
|
|
420 void wyswietl_kafle(void) {
|
|
421
|
|
422 int i, j;
|
|
423 SDL_Rect zrodlo, cel;
|
|
424
|
|
425 for (i=0; i < 8; ++i) {
|
|
426
|
|
427 for (j = 0; j < 8; ++j) {
|
|
428
|
|
429 if (kafle[i][j].obraz != NULL) {
|
|
430 printf ("rysuje\n");
|
|
431 cel = set_rect(kafle[i][j].x, kafle[i][j].y, kafle[i][j].obraz->w, kafle[i][j].obraz->h);
|
|
432 zrodlo = set_rect(0, 0, kafle[i][j].obraz->w, kafle[i][j].obraz->h);
|
|
433 SDL_BlitSurface(kafle[i][j].obraz, &zrodlo, obraz, &cel);
|
|
434
|
|
435 }
|
|
436 //else printf ("Kafle nie maja przydzielonego obrazu\n");
|
|
437 }
|
|
438 }
|
|
439 }
|
|
440
|
|
441 SDL_Surface * utworz_kafle(SDL_Surface ** tab) {
|
|
442 int i;
|
|
443 Kolor kolory[4] = {{62, 246, 26}, // zielony
|
|
444 {246, 26, 202}, // filoet
|
|
445 {26, 36, 246}, // indygo
|
|
446 {246, 26, 52}}; // czerwony
|
|
447 // {253, 157, 79} // pomaranczowy
|
|
448 for (i=0; i<8; i++) {
|
|
449 tab[i] = ustaw_kolor(kolory[i]);
|
|
450 }
|
|
451
|
|
452 }
|
|
453
|
|
454 SDL_bool end() {
|
|
455 SDL_Rect zrodlo, cel;
|
|
456 char txt1[30] = "G A M E O V E R";
|
|
457 char txt2[10] = "Wynik";
|
|
458 char txt3[24];
|
|
459 ustaw_kolor_tla();
|
|
460
|
|
461 print_text(0, 0, 0, txt1, 200, 200, 10);
|
|
462 print_text(234, 0, 0, txt2, 250, 250, 10);
|
|
463 sprintf (txt3, "%d", wynik);
|
|
464 print_text(33, 249, 11, txt3, 260, 300, 10);
|
|
465
|
|
466 cel = set_rect(0, 0, obraz->w, obraz->h);
|
|
467 zrodlo = set_rect(0, 0, obraz->w, obraz->h);
|
|
468 tex_screen = SDL_CreateTextureFromSurface(renderer, obraz);
|
|
469 SDL_RenderClear ( renderer );
|
|
470 SDL_RenderCopy(renderer, tex_screen,&zrodlo,&cel);
|
|
471 SDL_RenderPresent(renderer);
|
|
472 SDL_DestroyTexture(tex_screen);
|
|
473
|
|
474 sleep(5);
|
|
475 return SDL_TRUE;
|
|
476 }
|
|
477
|
|
478 SDL_bool file_exist(){
|
|
479 char *file;
|
|
480
|
|
481 #if WINDOWS
|
|
482 char * FILE_NAME = "\\arkanoid.txt";
|
|
483 char * HOME = "HOMEPATH";
|
|
484
|
|
485 #elif LINUX
|
|
486 char * FILE_NAME = "/.arkanoid.txt";
|
|
487 char * HOME = "HOME";
|
|
488 #endif
|
|
489
|
|
490
|
|
491 if (!getenv(HOME)) exit (1);
|
|
492
|
|
493 file=(char *)malloc(strlen(getenv(HOME))+strlen(FILE_NAME)+1);
|
|
494
|
|
495 strcpy(file, getenv(HOME));
|
|
496 strcat(file, FILE_NAME);
|
|
497 printf("1");
|
|
498
|
|
499 if (access(file, F_OK)==0){
|
|
500 printf("PLIK istnieje %s",file);
|
|
501 free(file);
|
|
502 return SDL_TRUE;
|
|
503 }
|
|
504 printf("PLIK nie istnieje %s",file);
|
|
505 free(file);
|
|
506 return SDL_FALSE;
|
|
507
|
|
508 }
|
|
509
|
|
510 void save_hiscore(){
|
|
511 char *file;
|
|
512
|
|
513 #if WINDOWS
|
|
514 char * FILE_NAME = "\\arkanoid.txt";
|
|
515 char * HOME = "HOMEPATH";
|
|
516
|
|
517 #elif LINUX
|
|
518 char * FILE_NAME = "/.arkanoid.txt";
|
|
519 char * HOME = "HOME";
|
|
520 #endif
|
|
521
|
|
522
|
|
523 char bufor[48];
|
|
524
|
|
525 if (!getenv(HOME)) exit (1);
|
|
526
|
|
527
|
|
528 //file=(char *)malloc(strlen(getenv(HOME)+strlen(FILE_NAME)+1));
|
|
529 file=(char *)malloc(strlen(getenv(HOME))+strlen(FILE_NAME)+1);
|
|
530
|
|
531 strcpy(file, getenv(HOME));
|
|
532 strcat(file, FILE_NAME);
|
|
533
|
|
534 sprintf(bufor,"%d",najlepszy);
|
|
535
|
|
536 FILE * f = fopen(file, "w");
|
|
537 fwrite(bufor, sizeof(char), strlen(bufor), f);
|
|
538 fclose(f);
|
|
539 printf ("Zapisałem: %s\n", file);
|
|
540 free(file);
|
|
541
|
|
542
|
|
543
|
|
544 }
|
|
545
|
|
546 void load_hiscore(){
|
|
547 char *file;
|
|
548
|
|
549 #if WINDOWS
|
|
550 char * FILE_NAME = "\\arkanoid.txt";
|
|
551 char * HOME = "HOMEPATH";
|
|
552
|
|
553 #elif LINUX
|
|
554 char * FILE_NAME = "/.arkanoid.txt";
|
|
555 char * HOME = "HOME";
|
|
556 #endif
|
|
557
|
|
558
|
|
559 char bufor[48];
|
|
560
|
|
561 if (!getenv(HOME)) exit (1);
|
|
562
|
|
563 //file=(char *)malloc(strlen(getenv(HOME)+strlen(FILE_NAME)+1));
|
|
564 file=(char *)malloc(strlen(getenv(HOME))+strlen(FILE_NAME)+1);
|
|
565
|
|
566 strcpy(file, getenv(HOME));
|
|
567 strcat(file, FILE_NAME);
|
|
568
|
|
569 FILE * f = fopen(file, "r");
|
|
570 fscanf(f, "%s", bufor);
|
|
571 fclose(f);
|
|
572 free(file);
|
|
573
|
|
574 najlepszy=atoi(bufor);
|
|
575
|
|
576 // najlepszy=0;
|
|
577 }
|
|
578
|
|
579
|
|
580
|
|
581
|
|
582 SDL_bool gra () {
|
|
583
|
|
584 int wykonano, rozpoczecie;
|
|
585 int px, py, kx, ky;
|
|
586 char bufor[50];
|
|
587 SDL_bool quit=SDL_TRUE;
|
|
588
|
|
589 SDL_Surface * tablica_kolorow_kafli[4];
|
|
590 SDL_TimerID uchwyt_pilki;
|
|
591 Uint32 predkosc_pilki=50, czas_zwloki=50;
|
|
592 RuchomyObiekt pilka, paletka;
|
|
593 Teczka teczka;
|
|
594 // Teczka2 teczka2;
|
|
595 // Kafel kafle[4][8];
|
|
596 int powolna_paletka=0, pauza=0;
|
|
597 SDL_Rect zrodlo, cel;
|
|
598 char level_bufor[24], score_bufor[24], lives_bufor[24], hi_bufor[24];
|
|
599
|
|
600 wynik=000;
|
|
601 pilka.ax=640/2-XPILKI/2;
|
|
602 pilka.ay=420;
|
|
603 pilka.px=pilka.ax;
|
|
604 pilka.py=pilka.ay;
|
|
605 pilka.kx=-5;
|
|
606 pilka.ky=-5;
|
|
607
|
|
608 zycia=2;
|
|
609 poziom=1;
|
|
610 //najlepszy=1000;
|
|
611
|
|
612 asm("nop");
|
|
613 ustaw_kolor_tla();
|
|
614
|
|
615 pilka.obraz=utworz_pilke();
|
|
616 //pilka.obraz = SDL_LoadBMP("ball.bmp");
|
|
617 //pilka.gumka=utworz_powierzchnie(XPILKI, YPILKI, 65, 190, 215);
|
|
618 #if WINDOWS
|
|
619 pilka.gumka = SDL_LoadBMP("icon/gumka_arkanoid.bmp");
|
|
620 #elif LINUX
|
|
621 pilka.gumka = SDL_LoadBMP("/usr/share/pixmaps/gumka_arkanoid.bmp");
|
|
622 #endif
|
|
623
|
|
624 SDL_SetColorKey(pilka.gumka,SDL_TRUE,getpixel(pilka.gumka,0,0));
|
|
625 rysuj_obrazek(pilka.ax, pilka.ay, pilka.obraz, 1); // colorskey powinno byc 1
|
|
626
|
|
627 paletka.ax=640/2-XPALETKI/2;
|
|
628 paletka.ay=450;
|
|
629 // paletka.px=paletka.ax;
|
|
630 paletka.py=paletka.ay;
|
|
631
|
|
632 paletka.obraz=utworz_powierzchnie(XPALETKI, YPALETKI, 0, 0, 0);
|
|
633 paletka.gumka=utworz_powierzchnie(XPALETKI, YPALETKI, 65, 190, 215);
|
|
634
|
|
635 rysuj_obrazek(paletka.ax, paletka.ay, paletka.obraz, 0);
|
|
636
|
|
637
|
|
638 teczka.pilka=&pilka;
|
|
639 teczka.paletka=&paletka;
|
|
640
|
|
641 utworz_kafle(tablica_kolorow_kafli);
|
|
642 zaladuj_poziom(tablica_kolorow_kafli, poziom-1);
|
|
643 wyswietl_kafle();
|
|
644 gumka_kafla=utworz_powierzchnie(XKAFLA, YKAFLA, 65, 190, 215);
|
|
645
|
|
646 rozpoczecie = wykonano = 0;
|
|
647
|
|
648 sprintf (level_bufor, "%d", poziom);
|
|
649 print_text(253, 255, 100, level_bufor, 250, 0, 2);
|
|
650 sprintf (lives_bufor, "%d", zycia);
|
|
651 print_text(33, 249, 11, lives_bufor, 400, 0, 2);
|
|
652 sprintf (hi_bufor, "%d", najlepszy);
|
|
653 print_text(249, 33, 11, hi_bufor, 550, 0, 3);
|
|
654
|
|
655 //SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
|
|
656
|
|
657 while(!wykonano) {
|
|
658
|
|
659 SDL_Event event;
|
|
660 // SDL_KeyboardEvent zd_klawiszy;
|
|
661
|
|
662
|
|
663 if (pilka.ax+pilka.obraz->w <= 0){
|
|
664 pilka.ax=640;
|
|
665 }
|
|
666
|
|
667 while (SDL_PollEvent (&event)) {
|
|
668
|
|
669 switch (event.type) {
|
|
670
|
|
671 case SDL_KEYDOWN:
|
|
672 printf("Naciśnięto klawisz: %s\n", SDL_GetKeyName(event.key.keysym.sym));
|
|
673
|
|
674 if (event.key.keysym.sym==SDLK_ESCAPE)
|
|
675 wykonano = 1;
|
|
676
|
|
677 if (event.key.keysym.sym==SDLK_SPACE && !rozpoczecie) {
|
|
678 // teczka.poprzedni_y=pilka.ay;
|
|
679 // uchwyt_pilki = SDL_AddTimer(predkosc_pilki, ruch_pilki, &teczka);
|
|
680 rozpoczecie = 1;
|
|
681 }
|
|
682
|
|
683 //printf("powolna_paleka: %d", powolna_paletka);
|
|
684 if (event.key.keysym.sym==SDLK_LEFT && paletka.ax>0) {
|
|
685 printf("PALETKA LEWA\n");
|
|
686 if(powolna_paletka>=1) {
|
|
687 paletka.px=paletka.ax;
|
|
688 paletka.ax-=10;
|
|
689 przerysuj(&paletka);
|
|
690 if (!rozpoczecie) {
|
|
691 pilka.px=pilka.ax;
|
|
692 pilka.ax-=10;
|
|
693 przerysuj(&pilka);
|
|
694 }
|
|
695 powolna_paletka=0;
|
|
696 }
|
|
697 }
|
|
698 if (event.key.keysym.sym==SDLK_RIGHT && paletka.ax<(640-XPALETKI)) {
|
|
699 if(powolna_paletka>=1) {
|
|
700 paletka.px=paletka.ax;
|
|
701 paletka.ax+=10;
|
|
702 przerysuj(&paletka);
|
|
703 if (!rozpoczecie) {
|
|
704 pilka.px=pilka.ax;
|
|
705 pilka.ax+=10;
|
|
706 przerysuj(&pilka);
|
|
707 }
|
|
708 powolna_paletka=0;
|
|
709 }
|
|
710 }
|
|
711 if (event.key.keysym.sym==SDLK_r) {
|
|
712 wyswietl_kafle();
|
|
713 sprintf (score_bufor, "%d", wynik);
|
|
714 print_text(255, 255, 255, score_bufor, 10, 0, 3);
|
|
715 sprintf (level_bufor, "%d", poziom);
|
|
716 print_text(253, 255, 100, level_bufor, 250, 0, 2);
|
|
717 sprintf (lives_bufor, "%d", zycia);
|
|
718 print_text(33, 249, 11, lives_bufor, 400, 0, 2);
|
|
719 sprintf (hi_bufor, "%d", najlepszy);
|
|
720 print_text(249, 33, 11, hi_bufor, 550, 0, 3);
|
|
721 przerysuj(&paletka);
|
|
722 przerysuj(&pilka);
|
|
723 }
|
|
724
|
|
725 break;
|
|
726
|
|
727 case SDL_QUIT:
|
|
728 if (wynik>najlepszy){
|
|
729 najlepszy=wynik;
|
|
730 save_hiscore();
|
|
731 }
|
|
732 SDL_DestroyTexture(tex_screen);
|
|
733 SDL_DestroyRenderer(renderer);
|
|
734 SDL_DestroyWindow(gWindow);
|
|
735 exit(0);
|
|
736 break;
|
|
737
|
|
738 case SDL_MOUSEMOTION:
|
|
739 break;
|
|
740
|
|
741 case SDL_MOUSEBUTTONDOWN:
|
|
742 //if (!pauza) pauza=1;
|
|
743 //else pauza=0;
|
|
744 break;
|
|
745
|
|
746 case SDL_MOUSEBUTTONUP:
|
|
747 break;
|
|
748
|
|
749 default:
|
|
750 break;
|
|
751 }
|
|
752
|
|
753 }
|
|
754
|
|
755 if (pilka.ay+pilka.obraz->h>=480){
|
|
756 printf("Nowe rozdanie!\n\n\n");
|
|
757 if(--zycia == -1){
|
|
758 wykonano=1;
|
|
759 quit=SDL_TRUE;
|
|
760 goto label;
|
|
761 }
|
|
762
|
|
763 //erase (&paletka);
|
|
764 //erase (&pilka);
|
|
765 // wymazujemy caly ekran, erase(&paletka) nie wymazuje dokladnie
|
|
766 ustaw_kolor_tla();
|
|
767 wyswietl_kafle();
|
|
768 //pisz_info(253, 255, 100, poziom, 250, 0, 2);
|
|
769 //pisz_info(33, 249, 11, zycia, 400, 0, 2);
|
|
770 //pisz_info(249, 33, 11, najlepszy, 550, 0, 3);
|
|
771 sprintf (level_bufor, "%d", poziom);
|
|
772 print_text(253, 255, 100, level_bufor, 250, 0, 2);
|
|
773 sprintf (lives_bufor, "%d", zycia);
|
|
774 print_text(33, 249, 11, lives_bufor, 400, 0, 2);
|
|
775 sprintf (hi_bufor, "%d", najlepszy);
|
|
776 print_text(249, 33, 11, hi_bufor, 550, 0, 3);
|
|
777 rozpoczecie=0;
|
|
778 pilka.ax=640/2-XPILKI/2;
|
|
779 pilka.ay=420;
|
|
780 pilka.px=pilka.ax;
|
|
781 pilka.py=pilka.ay;
|
|
782 pilka.kx=-5;
|
|
783 pilka.ky=-5;
|
|
784 paletka.ax=640/2-XPALETKI/2;
|
|
785 paletka.ay=450;
|
|
786 // paletka.px=paletka.ax;
|
|
787 paletka.py=paletka.ay;
|
|
788
|
|
789 if(powolna_paletka>=1) {
|
|
790 paletka.px=paletka.ax;
|
|
791 paletka.ax-=10;
|
|
792 przerysuj(&paletka);
|
|
793 if (!rozpoczecie) {
|
|
794 pilka.px=pilka.ax;
|
|
795 pilka.ax-=10;
|
|
796 przerysuj(&pilka);
|
|
797 }
|
|
798 powolna_paletka=0;
|
|
799 }
|
|
800 }
|
|
801 // zapobiega wywietleniu -1 zycia
|
|
802 label:
|
|
803
|
|
804 //pisz_info(255, 255, 255, wynik, 0, 0, 4);
|
|
805 sprintf (score_bufor, "%d", wynik);
|
|
806 print_text(255, 255, 255, score_bufor, 10, 0, 3);
|
|
807
|
|
808
|
|
809 if (rozpoczecie) {
|
|
810 if(!pauza) {
|
|
811
|
|
812 ruch_pilki(0, &teczka);
|
|
813 }
|
|
814 }
|
|
815 powolna_paletka+=1;
|
|
816
|
|
817
|
|
818 if (!liczba_kafli) {
|
|
819 printf ("poziom zaliczony\n");
|
|
820 if (pilka.py>300 && poziom<8) {
|
|
821 poziom++;
|
|
822 printf ("rysuj nowy poziom liczba kafli przed: %d\n", liczba_kafli);
|
|
823 zaladuj_poziom(tablica_kolorow_kafli, poziom-1);
|
|
824 wyswietl_kafle();
|
|
825 sprintf (level_bufor, "%d", poziom);
|
|
826 print_text(253, 255, 100, level_bufor, 250, 0, 2);
|
|
827 printf("liczba kafli po: %d\n", liczba_kafli);
|
|
828 //pisz_info(253, 255, 100, poziom, 180, 0, 1);
|
|
829 }
|
|
830 else if (pilka.py>300 && poziom>=8){
|
|
831 poziom=1;
|
|
832 zaladuj_poziom(tablica_kolorow_kafli, poziom-1);
|
|
833 wyswietl_kafle();
|
|
834 sprintf (level_bufor, "%d", poziom);
|
|
835 print_text(253, 255, 100, level_bufor, 250, 0, 2);
|
|
836 printf("liczba kafli po: %d\n", liczba_kafli);
|
|
837 //wykonano=1;
|
|
838 }
|
|
839 // teczka2.czas=&zwloka;
|
|
840 // uchwyt_pilki = SDL_AddTimer(czas_zwloki, zwloka_dla_pilki, &teczka2);
|
|
841 }
|
|
842
|
|
843
|
|
844
|
|
845 cel = set_rect(0, 0, obraz->w, obraz->h);
|
|
846 zrodlo = set_rect(0, 0, obraz->w, obraz->h);
|
|
847 tex_screen = SDL_CreateTextureFromSurface(renderer, obraz);
|
|
848 SDL_RenderClear ( renderer );
|
|
849 SDL_RenderCopy(renderer, tex_screen,&zrodlo,&cel);
|
|
850 SDL_RenderPresent(renderer);
|
|
851 SDL_DestroyTexture(tex_screen);
|
|
852
|
|
853 SDL_Delay(40);
|
|
854 //SDL_Delay(500);
|
|
855 }
|
|
856 if (wynik>najlepszy){
|
|
857 najlepszy=wynik;
|
|
858 save_hiscore();
|
|
859 }
|
|
860 // SDL_RemoveTimer(uchwyt_pilki);
|
|
861 /* przywrocenie orginalnych ustawien */
|
|
862 //SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
|
|
863 return quit;
|
|
864 }
|
|
865
|
|
866
|
|
867
|
|
868 int main (int argc, char ** argv) {
|
|
869
|
|
870
|
|
871
|
|
872 if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0)
|
|
873 {
|
|
874 printf ("Nie mozna zainicjowac SDL: %s\n", SDL_GetError ());
|
|
875 exit (1);
|
|
876 }
|
|
877 atexit (SDL_Quit);
|
|
878
|
|
879 gWindow = SDL_CreateWindow( "Arkanoid", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN );
|
|
880 if( gWindow == NULL )
|
|
881 {
|
|
882 printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
|
|
883 exit(1);
|
|
884 }
|
|
885 else
|
|
886 {
|
|
887 renderer = SDL_CreateRenderer(gWindow, -1,0);
|
|
888 SDL_SetRenderDrawColor ( renderer ,65, 190, 215 , 255 );
|
|
889 SDL_RenderClear ( renderer );
|
|
890 //Get window surface
|
|
891 obraz = SDL_GetWindowSurface( gWindow );
|
|
892
|
|
893 }
|
|
894
|
|
895
|
|
896 /* inicjalizacja fontow */
|
|
897 if(TTF_Init()==-1) {
|
|
898 printf("TTF_Init: %s\n", TTF_GetError());
|
|
899 exit(2);
|
|
900 }
|
|
901 atexit (TTF_Quit);
|
|
902
|
|
903
|
|
904
|
|
905 #if WINDOWS
|
|
906 #define NAZWAFONTU "SpicyRice.ttf"
|
|
907 #elif LINUX
|
|
908 //#define NAZWAFONTU "/usr/share/fonts/SpicyRice.ttf"
|
|
909 #define NAZWAFONTU "/usr/share/arkanoid/SpicyRice.ttf"
|
|
910 #endif
|
|
911
|
|
912 if((font_panel = TTF_OpenFont(NAZWAFONTU, WIELKOSCFONTU)) == NULL) {
|
|
913 printf("TTF_OpenFont: %s\n", TTF_GetError());
|
|
914 exit(1);
|
|
915 }
|
|
916 if (file_exist()){
|
|
917 load_hiscore();
|
|
918 }
|
|
919 else {
|
|
920 najlepszy=0;
|
|
921 }
|
|
922 while(SDL_TRUE){
|
|
923 if (gra()==SDL_TRUE){
|
|
924 if (end()==SDL_TRUE){
|
|
925 printf ("\n\nkontynuje\n");
|
|
926 continue;
|
|
927 }
|
|
928 else break;
|
|
929 }
|
|
930 }
|
|
931 printf ("\n\nwychodze\n");
|
|
932 SDL_DestroyTexture(tex_screen);
|
|
933 SDL_DestroyRenderer(renderer);
|
|
934 SDL_DestroyWindow(gWindow);
|
|
935 return 0;
|
|
936 }
|
|
937 // vi: sw=4: ts=4
|