view trix/src/main.c @ 19:fff7215ba1e8

package for arch
author Przemyslaw <prymula76@outlook.com>
date Sun, 31 Mar 2024 16:10:46 +0200
parents 2787f5e749ae
children
line wrap: on
line source

/* 
 * Trix - klikanie po klockach
 * Przemysław R. Pietraszczyk
 * 
 * paźdżiernik 2006 r.
 * 
 * licencja: Public Domain
 */
#include <sys/stat.h> // mkdir
#include "trix.h"



SDL_Window  *window = NULL;


void options (void);
void about (void);
void hiscores(void);
int menu();

void start_game(void);

SDL_Rect set_rect(int x, int y, int w, int h)
{
	SDL_Rect rect;
	rect.x = x;
	rect.y = y;
	rect.w = w;
	rect.h = h;
	return rect;
}

Uint32 getpixel(SDL_Surface *surface, int x, int y) 
{
	int bpp = surface->format->BytesPerPixel;
	/* Here p is the address to the pixel we want to retrieve */
	Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
            
	switch(bpp) {
		case 1:
			return *p;
		case 2:
			return *(Uint16 *)p;
		case 3:
			if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
				return p[0] << 16 | p[1] << 8 | p[2];
			else
				return p[0] | p[1] << 8 | p[2] << 16;
		case 4:
			return *(Uint32 *)p;
		default:
			return 0;
	}
}

void putpixel(SDL_Surface *surface, int x, int y, Uint8 R, Uint8 G, Uint8 B)
{ 
    Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * 
               surface->format->BytesPerPixel;
    if(SDL_BYTEORDER == SDL_BIG_ENDIAN) 
    {
       p[0] = R; 
       p[1] = G; 
       p[2] = B; 
    } 
    else 
    {
       p[0] = B; 
       p[1] = G; 
       p[2] = R; 
    }
}

void create_net (void) {
   
   SDL_Rect destrect;
   int i, j;
   
   net = SDL_CreateRGBSurface(SDL_SWSURFACE,screen->w,screen->h,16,0,0,0,0);   

   destrect = set_rect(0, 0, net->w, net->h);
   SDL_FillRect(net,&destrect, SDL_MapRGB(net->format,255,255,255));
   
   SDL_SetColorKey(net,SDL_TRUE,getpixel(net,0,0));

   /* tworzy kolumny */
   for (i=20; i<460; i+=20) 
      for (j=40; j<560; j += 3) 
         putpixel(net, i, j, 175, 175, 175);
 
   /* tworzy wiersze */
   for (j=40; j<560; j+=20)
      for (i=0; i<460; i += 3)
         putpixel(net, i, j, 175, 175, 175);
      
   
}

/* drukuje informacje */
void print_inscription(char * buffor, int desx, int desy) {

   SDL_Rect srcrect, destrect;
   SDL_Color bgcolor;
   SDL_Color fgcolor;
   SDL_Surface * image;

   fgcolor.r = 255;
   fgcolor.b = 255;
   fgcolor.g = 255;
   bgcolor.r = 0;
   bgcolor.g = 0;
   bgcolor.b = 0;  
   
   //image = TTF_RenderUTF8_Shaded(font,buffor,fgcolor, bgcolor);  
   image = TTF_RenderText_Solid(font, buffor,  fgcolor);

   SDL_SetColorKey(image,SDL_TRUE,getpixel(image,0,0));

   destrect = set_rect(desx, desy, image->w, image->h);
   srcrect = set_rect(0,0,image->w, image->h);
   SDL_BlitSurface(image,&srcrect,screen,&destrect);
   SDL_FreeSurface(image);

}


void draw_text (int x, int y, SDL_Surface * image) {

   SDL_Rect srcrect, destrect;
  
   //SDL_SetColorKey(image,SDL_TRUE,getpixel(image,0,0));
   
   destrect = set_rect(x, y, image->w, image->h);
   srcrect = set_rect(0,0,image->w, image->h);
   SDL_BlitSurface(image,&srcrect,screen,&destrect);	
//   SDL_UpdateRect(screen, x, y, image->w, image->h);
}

SDL_Surface * prepare_text(int r, int b, int g, char *buffer) {
   
//   SDL_Rect srcrect, destrect;
   SDL_Surface* image;
   SDL_Color bgcolor;
   SDL_Color fgcolor;

   fgcolor.r = r;
   fgcolor.b = b;
   fgcolor.g = g;
   bgcolor.r = 0;
   bgcolor.g = 0;
   bgcolor.b = 0;
   
   return TTF_RenderText_Solid(font, buffer,  fgcolor);
   //return TTF_RenderUTF8_Shaded(font,buffer,fgcolor, bgcolor);
}

void load_banner() {
   
   banner=IMG_ReadXPMFromArray(banner_xpm);
   printf("banner: %p\n", banner);
   
   if(!banner) {
      printf("IMG_ReadXPMFromArray: %s\n", IMG_GetError());
      exit(1);
   }
}


/* odczytuje plik z wynikami i umieszcza dane w strukturze */
void load_hiscores_from_file(void) {
   
   FILE *fp;
   int j;

	if((fp = fopen(hifile,"r")) == NULL) {
		printf("\nCannot read from file %s\n", hifile);
      //exit (1);
	}   
	else {
		for (j = 0; j < 5; ++j)
			fscanf(fp,"%s %d", &tab_hiscores[j].name, &tab_hiscores[j].score);
		fclose(fp);
	}  
}
   
void save_hiscores_to_file (void) {
     
   FILE *fp;
   int j; 
    
  	if((fp = fopen(hifile,"w")) == NULL) {
		printf("\nCannot write to file %s\n", hifile);
      exit (1);
	}  
   for (j = 0; j < 5; ++j)
      fprintf(fp,"%s %d\n", tab_hiscores[j].name, tab_hiscores[j].score);
   fclose(fp); 
}  

void create_hifile(void) {
	
	FILE *fp;
   int j; 
   struct tab hi[] = {{"gecko", 50},
					{"gecko", 40},
					{"gecko", 30},
					{"gecko", 20},
					{"gecko", 10}};
					
  	if((fp = fopen(hifile,"w")) == NULL) {
		printf("\nCannot write to file %s\n", hifile);
      //exit (1);
	}
	else {
		for (j = 0; j < 5; ++j)
			fprintf(fp,"%s %d\n", hi[j].name, hi[j].score);
		fclose(fp);
	}
}

void load_catalogue(void) {
   
   FILE *fp;
   int j;
  
 	if((fp = fopen(CATALOGUE,"r")) == NULL) {
		printf("\nCannot read from file %s\n", CATALOGUE);
      exit (1);
	}   
   for (j = 0; j < N_PIC; ++j)
      fscanf(fp,"%s", &catalogue[j]);
   fclose(fp);  

}   


void load_background(int n) {
   
   char buffor[256];
   
   sprintf(buffor, "%s%s", BG_DIR, catalogue[n]);
   
   SDL_FreeSurface(bg);
   
   if ((bg = IMG_Load(buffor)) == NULL) {
		fprintf(stdout,"Cannot load icon bitmap: %s\n", IMG_GetError ());
		exit(1);
   }
}


void load_settings(void) {

   FILE *fp;
   char line[256];
   char * p;
   
   
#if LINUX
	char *path;
   char * HOME = "HOME";
   path=(char *)malloc(strlen(getenv(HOME))+strlen(SETTINGS)+1);
	
	strcpy(path, getenv(HOME));
	strcat(path, SETTINGS);
   
   if ((fp = fopen(path,"r"))==NULL) {
      printf ("Nie mozna otworzyc pliku: settings\n");

      start_level = 1;
      net = 0;
   } 
#elif WINDOWS
	if ((fp = fopen(SETTINGS,"r"))==NULL) {
      printf ("Nie mozna otworzyc pliku: settings\n");

      start_level = 1;
      net = 0;
   } 
#endif
   if (fp) {
      while((fgets(line, 256 , fp)) != NULL) {
         if (!strncmp(line, "start", 5)) {
            p = strrchr(line, ' ');
            printf (" start: %d\n", atoi(p));
            start_level = atoi(p);
         }
         else if (!strncmp(line, "net", 3)) {
            p = strrchr(line, ' ');
            printf (" net: %d\n", atoi(p));
            use_net = atoi(p);
         }
      }
     fclose(fp); 
   }
#if LINUX
	free(path);
#endif
}

void save_settings(void) {

  FILE *fp;
#if LINUX
	char *path;
   char * HOME = "HOME";
   path=(char *)malloc(strlen(getenv(HOME))+strlen(SETTINGS)+1);
	
	strcpy(path, getenv(HOME));
	strcat(path, SETTINGS);
   printf("save settings: 1\n");
   if ((fp = fopen(path,"w"))==NULL) {
      printf ("Nie mozna zapisac pliku: settings\n");

      start_level = 1;
      net = 0;
   } 
   printf("save settings: 2n");

   free(path);
#elif WINDOWS
	if ((fp = fopen(SETTINGS,"w"))==NULL) {
      printf ("Nie mozna zapisac pliku: settings\n");

      start_level = 1;
      net = 0;
   } 
#endif

/*
  	if((fp = fopen(SETTINGS,"w")) == NULL) {
		printf("\nNie moge zapisac pliku: settings\n");
//      exit (1);
	}
*/  
	if (fp) {
   	fprintf(fp, "start %d\n", start_level);
   	fprintf(fp, "net %d\n", use_net);
		fclose(fp);
   }

}

/*

void load_settings(void) {

   FILE *fp;
   char line[256];
   char * p;
   
   if ((fp = fopen(SETTINGS,"r"))==NULL) {
      printf ("Nie mozna otworzyc pliku: settings\n");

      start_level = 1;
      net = 0;
   } 
   if (fp) {
      while((fgets(line, 256 , fp)) != NULL) {
         if (!strncmp(line, "start", 5)) {
            p = strrchr(line, ' ');
            printf (" start: %d\n", atoi(p));
            start_level = atoi(p);
         }
         else if (!strncmp(line, "net", 3)) {
            p = strrchr(line, ' ');
            printf (" net: %d\n", atoi(p));
            use_net = atoi(p);
         }
      }
     fclose(fp); 
   }
}

void save_settings(void) {

  FILE *fp;

  	if((fp = fopen(SETTINGS,"w")) == NULL) {
		printf("\nNie moge zapisac pliku: settings\n");
//      exit (1);
	}  
	else {
   	fprintf(fp, "start %d\n", start_level);
   	fprintf(fp, "net %d\n", use_net);
		fclose(fp);
   }

}

*/


void load_bitmaps(void) {
   
	int i;
	SDL_Rect srcrect, destrect;
	SDL_Color bgcolor;
   SDL_Color fgcolor;
   SDL_Surface* icons;

	fgcolor.r = 255;
	fgcolor.g = 255;
	fgcolor.b = 255;
	bgcolor.r = 0;
	bgcolor.g = 0;
	bgcolor.b = 0;  



	destrect = set_rect(0,0,FIELD_SIZE,FIELD_SIZE);
	
	if((icons = SDL_LoadBMP(ICONS)) == NULL) {
		fprintf(stdout,"Cannot load icon bitmap: %s\n", SDL_GetError ());
		exit(1);
	}

   /* przestrzen dla klockow */
   for (i=0; i < 8;++i) {
		block[i] = SDL_CreateRGBSurface(SDL_SWSURFACE,FIELD_SIZE,FIELD_SIZE,16,0,0,0,0);
		srcrect = set_rect(i*20,0,FIELD_SIZE,FIELD_SIZE);
		SDL_BlitSurface(icons,&srcrect, block[i],&destrect);
   }


   /* przestrzen dla bomby */ 
	bomb = SDL_CreateRGBSurface(SDL_SWSURFACE,FIELD_SIZE,FIELD_SIZE,16,0,0,0,0);
	srcrect = set_rect(160,0,FIELD_SIZE,FIELD_SIZE);
	SDL_BlitSurface(icons,&srcrect, bomb,&destrect);
	/* ustawiamy kolor przezroczystosci */
	SDL_SetColorKey(bomb,SDL_TRUE,getpixel(bomb,0,0));

   /* przestrzen dla eksplozji */
	explode = SDL_CreateRGBSurface(SDL_SWSURFACE,FIELD_SIZE,FIELD_SIZE,16,0,0,0,0);
	srcrect = set_rect(180,0,FIELD_SIZE,FIELD_SIZE);
	SDL_BlitSurface(icons, &srcrect, explode, &destrect);
	/* ustawiamy kolor przezroczystosci */
	SDL_SetColorKey(explode,SDL_TRUE,getpixel(explode,0,0));


	wall = SDL_CreateRGBSurface(SDL_SWSURFACE,FIELD_SIZE,FIELD_SIZE,16,0,0,0,0);
	srcrect = set_rect(200,0,FIELD_SIZE,FIELD_SIZE);
	SDL_BlitSurface(icons, &srcrect, wall, &destrect);	
	
	/* przestrzen dla dymku */
	smoke = SDL_CreateRGBSurface(SDL_SWSURFACE,FIELD_SIZE,FIELD_SIZE,16,0,0,0,0);
	srcrect = set_rect(220,0,FIELD_SIZE,FIELD_SIZE);
	SDL_BlitSurface(icons, &srcrect, smoke, &destrect);
	/* ustawiamy kolor przezroczystosci */
	SDL_SetColorKey(smoke,SDL_TRUE,getpixel(smoke,0,0));

   SDL_FreeSurface(icons);
}

int atrap() {
	return START_GAME;
}


 int main (int argc, char ** argv)
{
   int done = 0;


   if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0)
   {
      printf ("Nie mozna zainicjowac SDL: %s\n", SDL_GetError ());
      exit (1);
   }
   atexit (SDL_Quit); 
   
   window = SDL_CreateWindow( "Trix", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 460, 560, SDL_WINDOW_SHOWN );

   //screen = SDL_SetVideoMode (460, 560, 16, SDL_SWSURFACE | SDL_DOUBLEBUF);
   if (window == NULL)
   {
      printf ("Nie mozna ustawic trybu 460x560x16: %s\n", SDL_GetError ());
      exit (2);
   }
   else {
		renderer = SDL_CreateRenderer(window, -1,0);
		SDL_SetRenderDrawColor ( renderer ,65, 190, 215 , 255 );
		SDL_RenderClear ( renderer );
		//Get window surface
		screen= SDL_GetWindowSurface( window );
	}  

   /* inicjalizacja fontow */
   if(TTF_Init()==-1) {
      printf("TTF_Init: %s\n", TTF_GetError());
   exit(2);
   }
   atexit (TTF_Quit);
   if((font = TTF_OpenFont(NAMEFONT,FONTSIZE)) == NULL) {
       printf("TTF_OpenFont: %s\n", TTF_GetError());
       exit(1);
	} 

#if LINUX
	char * FILE_HISCORES_FILE = "/.trix/hiscores";
	char * HOME = "HOME";
	char *path;
	int c;

	path=(char *)malloc(strlen(getenv(HOME))+strlen("/.trix")+1);
	strcpy(path, getenv(HOME));
	strcat(path, "/.trix");
	
	
	
	/* jesli brak katalogu to zwraca -1 */
	if ((c = chdir(path))) {
		
		perror("chdir");
		if (mkdir(path, 0777)) {
			perror("mkdir");
		}
		
	}
	printf ("\n\nc: %d\n\n", c);
	free(path);
	
	hifile=(char *)malloc(strlen(getenv(HOME))+strlen(FILE_HISCORES_FILE)+1);
	
	strcpy(hifile, getenv(HOME));
	strcat(hifile, FILE_HISCORES_FILE);
	
	if (access(hifile, F_OK)==0){
		printf("PLIK istnieje %s",hifile);
		//free(hifile);
	
	} 
	else {
		printf("PLIK nie istnieje %s",hifile);
		create_hifile();
		//free(hifile);
	}
	
	
	SDL_Surface * icon = SDL_LoadBMP("/usr/share/trix/img/trix-icon.bmp");
	SDL_SetWindowIcon(window, icon);
   //load_hiscores_from_file();
   
#elif WINDOWS
   char * FILE_HISCORES = "\\trix.txt";
   //char * FILE_HISCORES = "/trix.txt";

	//char * PATH_TRIX = "/.trix";
   char * HOME = "HOMEPATH";
	//char *path;
	int c;

   hifile=(char *)malloc(strlen(getenv(HOME))+strlen(FILE_HISCORES)+1);

   strcpy(hifile, getenv(HOME));
	strcat(hifile, FILE_HISCORES);

   if (access(hifile, F_OK)==0){
		printf("PLIK istnieje %s\n", hifile);
		//free(file);
      char bufor[48];

	   FILE * fp = fopen(hifile, "r");
      for (int j = 0; j < 5; ++j)
         fscanf(fp,"%s %d", &tab_hiscores[j].name, &tab_hiscores[j].score);
	   fclose(fp);
	   

	
	}
   else{
	   printf("PLIK nie istnieje %s\n",hifile);
	   
	create_hifile();
/*
      FILE * fp;
      struct tab hi[] = {{"gecko", 50},
					{"gecko", 40},
					{"gecko", 30},
					{"gecko", 20},
					{"gecko", 10}};
					
  	   if((fp = fopen(hifile,"w")) == NULL) {
		   printf("\nCannot write to file %s\n", hifile);
         exit (1);
	   }  
      for (int j = 0; j < 5; ++j){
         fprintf(fp,"%s %d\n", hi[j].name, hi[j].score);
         strcpy(tab_hiscores[j].name , hi[j].name); 
         tab_hiscores[j].score = hi[j].score;
      }
      fclose(fp);
	   
	   /
	   printf ("Zapisałem: %s\n", hifile);
	   */
   }
#endif

	load_hiscores_from_file();
	printf("\nAAAA\n");
	start_level = 1;
	load_settings();
	printf("BBBB\n");

   load_bitmaps();
	printf("CCCC\n");

   load_banner();
   	printf("DDDD\n");

   printf ("B\n");
  
   load_catalogue();
   	printf("EEEEEE\n");

   create_net();
   printf("FFFFF\n");

   while(!done) {
      switch (menu()) {
	  //switch (atrap()) {
         case START_GAME:
            start_game();
            printf ("start game\n");
            break;      
         case HISCORES:
            hiscores();
            printf ("hiscores\n");
            break;
         case OPTIONS:
            options();
            printf ("options\n");
            break;
         case ABOUT:
            about();
            printf ("about\n");
            break;
         case QUIT_GAME:
            done = 1;
            break;
      }  
   }
      
   
    SDL_DestroyTexture(tex_screen);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
   //free(hifile);
   
}