Mercurial > hg > pub > prymula > com
comparison trix/src/main.c @ 0:2787f5e749ae
INIT
author | prymula <prymula76@outlook.com> |
---|---|
date | Thu, 21 Sep 2023 22:33:57 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:2787f5e749ae |
---|---|
1 /* | |
2 * Trix - klikanie po klockach | |
3 * Przemysław R. Pietraszczyk | |
4 * | |
5 * paźdżiernik 2006 r. | |
6 * | |
7 * licencja: Public Domain | |
8 */ | |
9 #include <sys/stat.h> // mkdir | |
10 #include "trix.h" | |
11 | |
12 | |
13 | |
14 SDL_Window *window = NULL; | |
15 | |
16 | |
17 void options (void); | |
18 void about (void); | |
19 void hiscores(void); | |
20 int menu(); | |
21 | |
22 void start_game(void); | |
23 | |
24 SDL_Rect set_rect(int x, int y, int w, int h) | |
25 { | |
26 SDL_Rect rect; | |
27 rect.x = x; | |
28 rect.y = y; | |
29 rect.w = w; | |
30 rect.h = h; | |
31 return rect; | |
32 } | |
33 | |
34 Uint32 getpixel(SDL_Surface *surface, int x, int y) | |
35 { | |
36 int bpp = surface->format->BytesPerPixel; | |
37 /* Here p is the address to the pixel we want to retrieve */ | |
38 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp; | |
39 | |
40 switch(bpp) { | |
41 case 1: | |
42 return *p; | |
43 case 2: | |
44 return *(Uint16 *)p; | |
45 case 3: | |
46 if(SDL_BYTEORDER == SDL_BIG_ENDIAN) | |
47 return p[0] << 16 | p[1] << 8 | p[2]; | |
48 else | |
49 return p[0] | p[1] << 8 | p[2] << 16; | |
50 case 4: | |
51 return *(Uint32 *)p; | |
52 default: | |
53 return 0; | |
54 } | |
55 } | |
56 | |
57 void putpixel(SDL_Surface *surface, int x, int y, Uint8 R, Uint8 G, Uint8 B) | |
58 { | |
59 Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * | |
60 surface->format->BytesPerPixel; | |
61 if(SDL_BYTEORDER == SDL_BIG_ENDIAN) | |
62 { | |
63 p[0] = R; | |
64 p[1] = G; | |
65 p[2] = B; | |
66 } | |
67 else | |
68 { | |
69 p[0] = B; | |
70 p[1] = G; | |
71 p[2] = R; | |
72 } | |
73 } | |
74 | |
75 void create_net (void) { | |
76 | |
77 SDL_Rect destrect; | |
78 int i, j; | |
79 | |
80 net = SDL_CreateRGBSurface(SDL_SWSURFACE,screen->w,screen->h,16,0,0,0,0); | |
81 | |
82 destrect = set_rect(0, 0, net->w, net->h); | |
83 SDL_FillRect(net,&destrect, SDL_MapRGB(net->format,255,255,255)); | |
84 | |
85 SDL_SetColorKey(net,SDL_TRUE,getpixel(net,0,0)); | |
86 | |
87 /* tworzy kolumny */ | |
88 for (i=20; i<460; i+=20) | |
89 for (j=40; j<560; j += 3) | |
90 putpixel(net, i, j, 175, 175, 175); | |
91 | |
92 /* tworzy wiersze */ | |
93 for (j=40; j<560; j+=20) | |
94 for (i=0; i<460; i += 3) | |
95 putpixel(net, i, j, 175, 175, 175); | |
96 | |
97 | |
98 } | |
99 | |
100 /* drukuje informacje */ | |
101 void print_inscription(char * buffor, int desx, int desy) { | |
102 | |
103 SDL_Rect srcrect, destrect; | |
104 SDL_Color bgcolor; | |
105 SDL_Color fgcolor; | |
106 SDL_Surface * image; | |
107 | |
108 fgcolor.r = 255; | |
109 fgcolor.b = 255; | |
110 fgcolor.g = 255; | |
111 bgcolor.r = 0; | |
112 bgcolor.g = 0; | |
113 bgcolor.b = 0; | |
114 | |
115 //image = TTF_RenderUTF8_Shaded(font,buffor,fgcolor, bgcolor); | |
116 image = TTF_RenderText_Solid(font, buffor, fgcolor); | |
117 | |
118 SDL_SetColorKey(image,SDL_TRUE,getpixel(image,0,0)); | |
119 | |
120 destrect = set_rect(desx, desy, image->w, image->h); | |
121 srcrect = set_rect(0,0,image->w, image->h); | |
122 SDL_BlitSurface(image,&srcrect,screen,&destrect); | |
123 SDL_FreeSurface(image); | |
124 | |
125 } | |
126 | |
127 | |
128 void draw_text (int x, int y, SDL_Surface * image) { | |
129 | |
130 SDL_Rect srcrect, destrect; | |
131 | |
132 //SDL_SetColorKey(image,SDL_TRUE,getpixel(image,0,0)); | |
133 | |
134 destrect = set_rect(x, y, image->w, image->h); | |
135 srcrect = set_rect(0,0,image->w, image->h); | |
136 SDL_BlitSurface(image,&srcrect,screen,&destrect); | |
137 // SDL_UpdateRect(screen, x, y, image->w, image->h); | |
138 } | |
139 | |
140 SDL_Surface * prepare_text(int r, int b, int g, char *buffer) { | |
141 | |
142 // SDL_Rect srcrect, destrect; | |
143 SDL_Surface* image; | |
144 SDL_Color bgcolor; | |
145 SDL_Color fgcolor; | |
146 | |
147 fgcolor.r = r; | |
148 fgcolor.b = b; | |
149 fgcolor.g = g; | |
150 bgcolor.r = 0; | |
151 bgcolor.g = 0; | |
152 bgcolor.b = 0; | |
153 | |
154 return TTF_RenderText_Solid(font, buffer, fgcolor); | |
155 //return TTF_RenderUTF8_Shaded(font,buffer,fgcolor, bgcolor); | |
156 } | |
157 | |
158 void load_banner() { | |
159 | |
160 banner=IMG_ReadXPMFromArray(banner_xpm); | |
161 printf("banner: %p\n", banner); | |
162 | |
163 if(!banner) { | |
164 printf("IMG_ReadXPMFromArray: %s\n", IMG_GetError()); | |
165 exit(1); | |
166 } | |
167 } | |
168 | |
169 | |
170 /* odczytuje plik z wynikami i umieszcza dane w strukturze */ | |
171 void load_hiscores_from_file(void) { | |
172 | |
173 FILE *fp; | |
174 int j; | |
175 | |
176 if((fp = fopen(hifile,"r")) == NULL) { | |
177 printf("\nCannot read from file %s\n", hifile); | |
178 //exit (1); | |
179 } | |
180 else { | |
181 for (j = 0; j < 5; ++j) | |
182 fscanf(fp,"%s %d", &tab_hiscores[j].name, &tab_hiscores[j].score); | |
183 fclose(fp); | |
184 } | |
185 } | |
186 | |
187 void save_hiscores_to_file (void) { | |
188 | |
189 FILE *fp; | |
190 int j; | |
191 | |
192 if((fp = fopen(hifile,"w")) == NULL) { | |
193 printf("\nCannot write to file %s\n", hifile); | |
194 exit (1); | |
195 } | |
196 for (j = 0; j < 5; ++j) | |
197 fprintf(fp,"%s %d\n", tab_hiscores[j].name, tab_hiscores[j].score); | |
198 fclose(fp); | |
199 } | |
200 | |
201 void create_hifile(void) { | |
202 | |
203 FILE *fp; | |
204 int j; | |
205 struct tab hi[] = {{"gecko", 50}, | |
206 {"gecko", 40}, | |
207 {"gecko", 30}, | |
208 {"gecko", 20}, | |
209 {"gecko", 10}}; | |
210 | |
211 if((fp = fopen(hifile,"w")) == NULL) { | |
212 printf("\nCannot write to file %s\n", hifile); | |
213 //exit (1); | |
214 } | |
215 else { | |
216 for (j = 0; j < 5; ++j) | |
217 fprintf(fp,"%s %d\n", hi[j].name, hi[j].score); | |
218 fclose(fp); | |
219 } | |
220 } | |
221 | |
222 void load_catalogue(void) { | |
223 | |
224 FILE *fp; | |
225 int j; | |
226 | |
227 if((fp = fopen(CATALOGUE,"r")) == NULL) { | |
228 printf("\nCannot read from file %s\n", CATALOGUE); | |
229 exit (1); | |
230 } | |
231 for (j = 0; j < N_PIC; ++j) | |
232 fscanf(fp,"%s", &catalogue[j]); | |
233 fclose(fp); | |
234 | |
235 } | |
236 | |
237 | |
238 void load_background(int n) { | |
239 | |
240 char buffor[256]; | |
241 | |
242 sprintf(buffor, "%s%s", BG_DIR, catalogue[n]); | |
243 | |
244 SDL_FreeSurface(bg); | |
245 | |
246 if ((bg = IMG_Load(buffor)) == NULL) { | |
247 fprintf(stdout,"Cannot load icon bitmap: %s\n", IMG_GetError ()); | |
248 exit(1); | |
249 } | |
250 } | |
251 | |
252 | |
253 void load_settings(void) { | |
254 | |
255 FILE *fp; | |
256 char line[256]; | |
257 char * p; | |
258 | |
259 | |
260 #if LINUX | |
261 char *path; | |
262 char * HOME = "HOME"; | |
263 path=(char *)malloc(strlen(getenv(HOME))+strlen(SETTINGS)+1); | |
264 | |
265 strcpy(path, getenv(HOME)); | |
266 strcat(path, SETTINGS); | |
267 | |
268 if ((fp = fopen(path,"r"))==NULL) { | |
269 printf ("Nie mozna otworzyc pliku: settings\n"); | |
270 | |
271 start_level = 1; | |
272 net = 0; | |
273 } | |
274 #elif WINDOWS | |
275 if ((fp = fopen(SETTINGS,"r"))==NULL) { | |
276 printf ("Nie mozna otworzyc pliku: settings\n"); | |
277 | |
278 start_level = 1; | |
279 net = 0; | |
280 } | |
281 #endif | |
282 if (fp) { | |
283 while((fgets(line, 256 , fp)) != NULL) { | |
284 if (!strncmp(line, "start", 5)) { | |
285 p = strrchr(line, ' '); | |
286 printf (" start: %d\n", atoi(p)); | |
287 start_level = atoi(p); | |
288 } | |
289 else if (!strncmp(line, "net", 3)) { | |
290 p = strrchr(line, ' '); | |
291 printf (" net: %d\n", atoi(p)); | |
292 use_net = atoi(p); | |
293 } | |
294 } | |
295 fclose(fp); | |
296 } | |
297 #if LINUX | |
298 free(path); | |
299 #endif | |
300 } | |
301 | |
302 void save_settings(void) { | |
303 | |
304 FILE *fp; | |
305 #if LINUX | |
306 char *path; | |
307 char * HOME = "HOME"; | |
308 path=(char *)malloc(strlen(getenv(HOME))+strlen(SETTINGS)+1); | |
309 | |
310 strcpy(path, getenv(HOME)); | |
311 strcat(path, SETTINGS); | |
312 printf("save settings: 1\n"); | |
313 if ((fp = fopen(path,"w"))==NULL) { | |
314 printf ("Nie mozna zapisac pliku: settings\n"); | |
315 | |
316 start_level = 1; | |
317 net = 0; | |
318 } | |
319 printf("save settings: 2n"); | |
320 | |
321 free(path); | |
322 #elif WINDOWS | |
323 if ((fp = fopen(SETTINGS,"w"))==NULL) { | |
324 printf ("Nie mozna zapisac pliku: settings\n"); | |
325 | |
326 start_level = 1; | |
327 net = 0; | |
328 } | |
329 #endif | |
330 | |
331 /* | |
332 if((fp = fopen(SETTINGS,"w")) == NULL) { | |
333 printf("\nNie moge zapisac pliku: settings\n"); | |
334 // exit (1); | |
335 } | |
336 */ | |
337 if (fp) { | |
338 fprintf(fp, "start %d\n", start_level); | |
339 fprintf(fp, "net %d\n", use_net); | |
340 fclose(fp); | |
341 } | |
342 | |
343 } | |
344 | |
345 /* | |
346 | |
347 void load_settings(void) { | |
348 | |
349 FILE *fp; | |
350 char line[256]; | |
351 char * p; | |
352 | |
353 if ((fp = fopen(SETTINGS,"r"))==NULL) { | |
354 printf ("Nie mozna otworzyc pliku: settings\n"); | |
355 | |
356 start_level = 1; | |
357 net = 0; | |
358 } | |
359 if (fp) { | |
360 while((fgets(line, 256 , fp)) != NULL) { | |
361 if (!strncmp(line, "start", 5)) { | |
362 p = strrchr(line, ' '); | |
363 printf (" start: %d\n", atoi(p)); | |
364 start_level = atoi(p); | |
365 } | |
366 else if (!strncmp(line, "net", 3)) { | |
367 p = strrchr(line, ' '); | |
368 printf (" net: %d\n", atoi(p)); | |
369 use_net = atoi(p); | |
370 } | |
371 } | |
372 fclose(fp); | |
373 } | |
374 } | |
375 | |
376 void save_settings(void) { | |
377 | |
378 FILE *fp; | |
379 | |
380 if((fp = fopen(SETTINGS,"w")) == NULL) { | |
381 printf("\nNie moge zapisac pliku: settings\n"); | |
382 // exit (1); | |
383 } | |
384 else { | |
385 fprintf(fp, "start %d\n", start_level); | |
386 fprintf(fp, "net %d\n", use_net); | |
387 fclose(fp); | |
388 } | |
389 | |
390 } | |
391 | |
392 */ | |
393 | |
394 | |
395 void load_bitmaps(void) { | |
396 | |
397 int i; | |
398 SDL_Rect srcrect, destrect; | |
399 SDL_Color bgcolor; | |
400 SDL_Color fgcolor; | |
401 SDL_Surface* icons; | |
402 | |
403 fgcolor.r = 255; | |
404 fgcolor.g = 255; | |
405 fgcolor.b = 255; | |
406 bgcolor.r = 0; | |
407 bgcolor.g = 0; | |
408 bgcolor.b = 0; | |
409 | |
410 | |
411 | |
412 destrect = set_rect(0,0,FIELD_SIZE,FIELD_SIZE); | |
413 | |
414 if((icons = SDL_LoadBMP(ICONS)) == NULL) { | |
415 fprintf(stdout,"Cannot load icon bitmap: %s\n", SDL_GetError ()); | |
416 exit(1); | |
417 } | |
418 | |
419 /* przestrzen dla klockow */ | |
420 for (i=0; i < 8;++i) { | |
421 block[i] = SDL_CreateRGBSurface(SDL_SWSURFACE,FIELD_SIZE,FIELD_SIZE,16,0,0,0,0); | |
422 srcrect = set_rect(i*20,0,FIELD_SIZE,FIELD_SIZE); | |
423 SDL_BlitSurface(icons,&srcrect, block[i],&destrect); | |
424 } | |
425 | |
426 | |
427 /* przestrzen dla bomby */ | |
428 bomb = SDL_CreateRGBSurface(SDL_SWSURFACE,FIELD_SIZE,FIELD_SIZE,16,0,0,0,0); | |
429 srcrect = set_rect(160,0,FIELD_SIZE,FIELD_SIZE); | |
430 SDL_BlitSurface(icons,&srcrect, bomb,&destrect); | |
431 /* ustawiamy kolor przezroczystosci */ | |
432 SDL_SetColorKey(bomb,SDL_TRUE,getpixel(bomb,0,0)); | |
433 | |
434 /* przestrzen dla eksplozji */ | |
435 explode = SDL_CreateRGBSurface(SDL_SWSURFACE,FIELD_SIZE,FIELD_SIZE,16,0,0,0,0); | |
436 srcrect = set_rect(180,0,FIELD_SIZE,FIELD_SIZE); | |
437 SDL_BlitSurface(icons, &srcrect, explode, &destrect); | |
438 /* ustawiamy kolor przezroczystosci */ | |
439 SDL_SetColorKey(explode,SDL_TRUE,getpixel(explode,0,0)); | |
440 | |
441 | |
442 wall = SDL_CreateRGBSurface(SDL_SWSURFACE,FIELD_SIZE,FIELD_SIZE,16,0,0,0,0); | |
443 srcrect = set_rect(200,0,FIELD_SIZE,FIELD_SIZE); | |
444 SDL_BlitSurface(icons, &srcrect, wall, &destrect); | |
445 | |
446 /* przestrzen dla dymku */ | |
447 smoke = SDL_CreateRGBSurface(SDL_SWSURFACE,FIELD_SIZE,FIELD_SIZE,16,0,0,0,0); | |
448 srcrect = set_rect(220,0,FIELD_SIZE,FIELD_SIZE); | |
449 SDL_BlitSurface(icons, &srcrect, smoke, &destrect); | |
450 /* ustawiamy kolor przezroczystosci */ | |
451 SDL_SetColorKey(smoke,SDL_TRUE,getpixel(smoke,0,0)); | |
452 | |
453 SDL_FreeSurface(icons); | |
454 } | |
455 | |
456 int atrap() { | |
457 return START_GAME; | |
458 } | |
459 | |
460 | |
461 int main (int argc, char ** argv) | |
462 { | |
463 int done = 0; | |
464 | |
465 | |
466 if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) | |
467 { | |
468 printf ("Nie mozna zainicjowac SDL: %s\n", SDL_GetError ()); | |
469 exit (1); | |
470 } | |
471 atexit (SDL_Quit); | |
472 | |
473 window = SDL_CreateWindow( "Trix", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 460, 560, SDL_WINDOW_SHOWN ); | |
474 | |
475 //screen = SDL_SetVideoMode (460, 560, 16, SDL_SWSURFACE | SDL_DOUBLEBUF); | |
476 if (window == NULL) | |
477 { | |
478 printf ("Nie mozna ustawic trybu 460x560x16: %s\n", SDL_GetError ()); | |
479 exit (2); | |
480 } | |
481 else { | |
482 renderer = SDL_CreateRenderer(window, -1,0); | |
483 SDL_SetRenderDrawColor ( renderer ,65, 190, 215 , 255 ); | |
484 SDL_RenderClear ( renderer ); | |
485 //Get window surface | |
486 screen= SDL_GetWindowSurface( window ); | |
487 } | |
488 | |
489 /* inicjalizacja fontow */ | |
490 if(TTF_Init()==-1) { | |
491 printf("TTF_Init: %s\n", TTF_GetError()); | |
492 exit(2); | |
493 } | |
494 atexit (TTF_Quit); | |
495 if((font = TTF_OpenFont(NAMEFONT,FONTSIZE)) == NULL) { | |
496 printf("TTF_OpenFont: %s\n", TTF_GetError()); | |
497 exit(1); | |
498 } | |
499 | |
500 #if LINUX | |
501 char * FILE_HISCORES_FILE = "/.trix/hiscores"; | |
502 char * HOME = "HOME"; | |
503 char *path; | |
504 int c; | |
505 | |
506 path=(char *)malloc(strlen(getenv(HOME))+strlen("/.trix")+1); | |
507 strcpy(path, getenv(HOME)); | |
508 strcat(path, "/.trix"); | |
509 | |
510 | |
511 | |
512 /* jesli brak katalogu to zwraca -1 */ | |
513 if ((c = chdir(path))) { | |
514 | |
515 perror("chdir"); | |
516 if (mkdir(path, 0777)) { | |
517 perror("mkdir"); | |
518 } | |
519 | |
520 } | |
521 printf ("\n\nc: %d\n\n", c); | |
522 free(path); | |
523 | |
524 hifile=(char *)malloc(strlen(getenv(HOME))+strlen(FILE_HISCORES_FILE)+1); | |
525 | |
526 strcpy(hifile, getenv(HOME)); | |
527 strcat(hifile, FILE_HISCORES_FILE); | |
528 | |
529 if (access(hifile, F_OK)==0){ | |
530 printf("PLIK istnieje %s",hifile); | |
531 //free(hifile); | |
532 | |
533 } | |
534 else { | |
535 printf("PLIK nie istnieje %s",hifile); | |
536 create_hifile(); | |
537 //free(hifile); | |
538 } | |
539 | |
540 | |
541 SDL_Surface * icon = SDL_LoadBMP("/usr/share/trix/img/trix-icon.bmp"); | |
542 SDL_SetWindowIcon(window, icon); | |
543 //load_hiscores_from_file(); | |
544 | |
545 #elif WINDOWS | |
546 char * FILE_HISCORES = "\\trix.txt"; | |
547 //char * FILE_HISCORES = "/trix.txt"; | |
548 | |
549 //char * PATH_TRIX = "/.trix"; | |
550 char * HOME = "HOMEPATH"; | |
551 //char *path; | |
552 int c; | |
553 | |
554 hifile=(char *)malloc(strlen(getenv(HOME))+strlen(FILE_HISCORES)+1); | |
555 | |
556 strcpy(hifile, getenv(HOME)); | |
557 strcat(hifile, FILE_HISCORES); | |
558 | |
559 if (access(hifile, F_OK)==0){ | |
560 printf("PLIK istnieje %s\n", hifile); | |
561 //free(file); | |
562 char bufor[48]; | |
563 | |
564 FILE * fp = fopen(hifile, "r"); | |
565 for (int j = 0; j < 5; ++j) | |
566 fscanf(fp,"%s %d", &tab_hiscores[j].name, &tab_hiscores[j].score); | |
567 fclose(fp); | |
568 | |
569 | |
570 | |
571 } | |
572 else{ | |
573 printf("PLIK nie istnieje %s\n",hifile); | |
574 | |
575 create_hifile(); | |
576 /* | |
577 FILE * fp; | |
578 struct tab hi[] = {{"gecko", 50}, | |
579 {"gecko", 40}, | |
580 {"gecko", 30}, | |
581 {"gecko", 20}, | |
582 {"gecko", 10}}; | |
583 | |
584 if((fp = fopen(hifile,"w")) == NULL) { | |
585 printf("\nCannot write to file %s\n", hifile); | |
586 exit (1); | |
587 } | |
588 for (int j = 0; j < 5; ++j){ | |
589 fprintf(fp,"%s %d\n", hi[j].name, hi[j].score); | |
590 strcpy(tab_hiscores[j].name , hi[j].name); | |
591 tab_hiscores[j].score = hi[j].score; | |
592 } | |
593 fclose(fp); | |
594 | |
595 / | |
596 printf ("Zapisałem: %s\n", hifile); | |
597 */ | |
598 } | |
599 #endif | |
600 | |
601 load_hiscores_from_file(); | |
602 printf("\nAAAA\n"); | |
603 start_level = 1; | |
604 load_settings(); | |
605 printf("BBBB\n"); | |
606 | |
607 load_bitmaps(); | |
608 printf("CCCC\n"); | |
609 | |
610 load_banner(); | |
611 printf("DDDD\n"); | |
612 | |
613 printf ("B\n"); | |
614 | |
615 load_catalogue(); | |
616 printf("EEEEEE\n"); | |
617 | |
618 create_net(); | |
619 printf("FFFFF\n"); | |
620 | |
621 while(!done) { | |
622 switch (menu()) { | |
623 //switch (atrap()) { | |
624 case START_GAME: | |
625 start_game(); | |
626 printf ("start game\n"); | |
627 break; | |
628 case HISCORES: | |
629 hiscores(); | |
630 printf ("hiscores\n"); | |
631 break; | |
632 case OPTIONS: | |
633 options(); | |
634 printf ("options\n"); | |
635 break; | |
636 case ABOUT: | |
637 about(); | |
638 printf ("about\n"); | |
639 break; | |
640 case QUIT_GAME: | |
641 done = 1; | |
642 break; | |
643 } | |
644 } | |
645 | |
646 | |
647 SDL_DestroyTexture(tex_screen); | |
648 SDL_DestroyRenderer(renderer); | |
649 SDL_DestroyWindow(window); | |
650 //free(hifile); | |
651 | |
652 } |