コード例 #1
0
ファイル: client.c プロジェクト: ablanchard/Nazcolique
void remplir_struct (char* m, sdl_c *map)
{//Lorsque l'on recoit un message du serveur on l'analyse pour savoir quoi mettre a jour
	printf("Analyse du message ...");
	int i=0;
	char buf[32];
	int k=0;
	int j=0;
	int pnj=0;
	int pj=0;
	int player=0;
	if (strncmp(m,"LOAD_PJ",7)==0)//Commande LOAD
	{
		printf("c'est une commande load ... ");
		sscanf(m,"LOAD_PJ%dPNJ%dP%d",&i,&pnj,&player);
		printf("il y a %i pj et %i pnj\n",i,pnj);
		if (map->nb_pnj!=pnj)//Si le client est un nouveau client
		{
			map->nb_pj=i;
			map->nb_pnj=pnj;
			n_player=player;
			allocation_pers(map);//On alloue de la memoire pour tous les perso pj et pnj
		}
		else//Si le client a deja tout charge et qu'il s'agit de la connection d'un nouveau joueur
		{
			load_pj(map,NULL,map->nb_pj);//Il ne charge que le nouveau joueur
			map->nb_pj++;
		}
	}
	else
	{
		if (strncmp(m,"LOAD_PNJ",8)==0)
		{
			printf("c'est une commande load_pnj ... ");
                	sscanf(m,"LOAD_PNJ%d",&pnj);
                	printf("il y a %i pnj\n",pnj);
			map->nb_pnj=pnj;
			load_pnj(map,"102005050",0,map->nb_pnj);
			envoi_ok(socktcp);
		}
		else
		{

		if (strncmp(m,"UNLOAD_PJ",9)==0)//Commande UNLOAD
		{
			printf("C'est une commande unload...");
			sscanf(m,"UNLOAD_PJ%d",&i);
			unload_pj(map,i);//Pas de question a se poser on dealloue la memoire du perso i
		}
		else
		{
			if (strncmp(m,"UNLOAD_PNJ",10)==0)
			{
				printf("C'est une commande Unload_pnj...");
				sscanf(m,"UNLOAD_PNJ%d",&i);
				unload_pnj(map,i);
			}
			else
			{
				if (strncmp(m,"PNJ",3)==0)
				{
					printf("c'est une mise a jour des coord des pnj...\n");
					for (i=3;i<strlen(m);i++)//On decompose le message recu
					{
						while (m[i]!='x')
						{
							buf[k]=m[i];
							i++;
							k++;
						}
						buf[k]='\0';
						j=atoi(buf);
						printf("il s'agit du pnj %i",j);
						i++;
						k=0;
						while (m[i]!='y')
						{
							buf[k]=m[i];
							i++;
							k++;
						}
						buf[k]='\0';
						map->tac[j]->pos.x=atoi(buf);
						printf(" nouvelle coord x=%i",atoi(buf));
						i++;
						k=0;
						while (m[i]!='p')
						{
							buf[k]=m[i];
							i++;
							k++;
						}
						buf[k]='\0';
						map->tac[j]->pos.y=atoi(buf);
						printf(" y=%i",atoi(buf));
						i++;
						k=0;
						while (m[i]!='s')
						{
							buf[k]=m[i];
							i++;
							k++;
						}
						buf[k]='\0';
						map->tac[j]->pv=atoi(buf);
						printf(" pv=%i\n",atoi(buf));
						i++;
						k=0;
						map->tac[j]->sens=m[i]-48;
						i++;
						map->tac[j]->pos_s=m[i]-48;
						i++;
						map->tac[j]->att=m[i]-48;
						move_c_net (map,j);
					}
				}
				else
				{
					printf("C'est une mise a jour des coordonnees...");
					for (i=0;i<strlen(m);i++)
					{
						while (m[i]!='x')
						{
							buf[k]=m[i];
							i++;
							k++;
						}
						buf[k]='\0';
						j=atoi(buf);
						printf("il s'agit du joueur %i",j);
						i++;
						k=0;
						while (m[i]!='y')
						{
							buf[k]=m[i];
							i++;
							k++;
						}
						buf[k]='\0';
						map->tab[j]->pos.x=atoi(buf);
						printf(" nouvelle coord x=%i",atoi(buf));
						i++;
						k=0;
						while (m[i]!='p')
						{
							buf[k]=m[i];
							i++;
							k++;
						}
						buf[k]='\0';
						map->tab[j]->pos.y=atoi(buf);
						printf(" y=%i",atoi(buf));
						i++;
						k=0;
						while (m[i]!='m')
						{
							buf[k]=m[i];
							i++;
							k++;
						}
						buf[k]='\0';
						map->tab[j]->pv=atoi(buf);
						printf(" pv=%i",atoi(buf));
						i++;
						k=0;
						while (m[i]!='d')
						{
							buf[k]=m[i];
							i++;
							k++;
						}
						buf[k]='\0';
						map->tab[j]->pv_m=atoi(buf);
						printf(" pv_m=%i",atoi(buf));
						i++;
						k=0;
						while (m[i]!='s')
						{
							buf[k]=m[i];
							i++;
							k++;
						}
						buf[k]='\0';
						map->tab[j]->deg=atoi(buf);
						printf(" deg=%i",atoi(buf));
						i++;
						k=0;
						map->tab[j]->sens=m[i]-48;
						i++;
						map->tab[j]->pos_s=m[i]-48;
						i++;
						map->tab[j]->att=m[i]-48;
						move_p_net (map,j);
						printf(" sens=%i\n",m[i]-48);
						printf("Le joueur %i se deplace en x=%i, y=%i, s=%i\n",j,map->tab[j]->pos.x,map->tab[j]->pos.y,map->tab[j]->sens);
					}
				}
			}
		}
		}
	}
}
コード例 #2
0
ファイル: main.c プロジェクト: arebena/test_sdl
int		main(int ac, char **av)
{
	t_gen			gen;
	int				f_level, error;
	t_pj			pj;
	SDL_Surface		*back = NULL, *grd = NULL, *hit_box = NULL;
	SDL_Texture		*b_tex, *g_tex, *hb;
	SDL_Rect		pj_box;
	
	SDL_Init(SDL_INIT_VIDEO);
	gen.win = SDL_CreateWindow(
		"test",
		SDL_WINDOWPOS_UNDEFINED,
		SDL_WINDOWPOS_UNDEFINED,
		940, 680,
		SDL_WINDOW_RESIZABLE);
	gen.screen = SDL_GetWindowSurface(gen.win);
	gen.render = SDL_CreateRenderer(gen.win, -1, SDL_RENDERER_ACCELERATED);

	gen.gold = 0;
	gen.cmod = 0;
	gen.cflag = 0;
	gen.rgb = (unsigned int *)malloc(sizeof(unsigned int) * 3);
	gen.rgb[0] = 100;
	gen.rgb[1] = 100;
	gen.rgb[2] = 100;
		
	gen.pj = load_pj(&pj);
	pj.ani = anim_init(2, 2, IDLE_FLEN);

//    back = IMG_Load("img/forest.gif");
//	b_tex = SDL_CreateTextureFromSurface(gen.render, back);
//	draw_backg(b_tex, back, gen.render, gen.screen, -1);
	
	grd = IMG_Load("img/ground.jpg");
	g_tex = SDL_CreateTextureFromSurface(gen.render, grd);

	pj_init(&gen, &pj, "img/pj/");
	ft_putendl("#### PJ_TEXT_DONE");

	pj.box = &pj_box;
	f_level = gen.screen->h * 0.75;
	pj.box->x = gen.screen->w / 2 - 50;
	pj.box->y = f_level - 140;
	pj.box->w = 100;
	pj.box->h = 140;
	
	hit_box = SDL_CreateRGBSurface(0, 100, 140, 32, 0, 0, 0, 0);
	SDL_FillRect(hit_box, NULL, SDL_MapRGB(gen.screen->format, 0, 0, 255));
	hb = SDL_CreateTextureFromSurface(gen.render, hit_box);
	SDL_SetTextureBlendMode(hb,SDL_BLENDMODE_BLEND);
	if ((error = SDL_SetTextureAlphaMod(hb, 100)))
		printf("Alpha for HB problem -> ERR_CODE = %d\n", error);

//	pausing();
	(void)b_tex;
	(void)g_tex;
	(void)pj_box;

	// ##############################################################"

	float	i = 0;
	char	maj = 0;
	int		continuer = 1;
	int		orient = 0;
	int		act = 0;
 	
	clock_t clok;
	clok = clock();
	SDL_Event event;

	get_key();
	
	while (continuer)
	{
		SDL_PollEvent(&event);
			switch(event.type)
			{
			case SDL_QUIT:
				continuer = 0;
				break;
			case SDL_KEYUP:
				switch(event.key.keysym.sym)
				{
				case 1073742049:
					maj = 0;
					break;
				case 27:
					continuer = 0;
					break;
				case 1073741904:
					i = act = 0;
					break;
				case 1073741903:
					i = act = 0;
					break;
				}
				break;
			case SDL_KEYDOWN:
				switch(event.key.keysym.sym)
				{
				case 1073742049:
					maj = 1;
					break;
				case 1073741904:
					orient = 1;
					if (act!=1)
						i = 0;
					act = 1;
					break;
				case 1073741903:
					orient = 0;
					if (act!=1)
						i = 0;
					act = 1;
					break;
				case 49:
					if (maj)
					{gen.gold = (gen.gold + 1) % 2;pj_init(&gen, &pj, "img/pj/");}
					else {
					pj.state[1][2]++;
					if (pj.state[1][2] > 2) pj.state[1][2] = 0;
					pj_init(&gen, &pj, "img/pj/");}
					break;
				case 50:
					pj.state[1][3]++;
					if (pj.state[1][3] > 2)
						pj.state[1][3] = 0;
					pj_init(&gen, &pj, "img/pj/");
					break;
				case 114:
					gen.cmod = 0;
					break;
				case 103:
					gen.cmod = 1;
					break;
				case 98:
					gen.cmod = 2;
					break;
				case 1073741899:
					if(gen.rgb[gen.cmod] <= 250){
						gen.rgb[gen.cmod] += 5;
						gen.cflag = 1;
						pj_init(&gen, &pj, "img/pj/");
						gen.cflag = 0;
					}
					break;
				case 1073741902:
					if(gen.rgb[gen.cmod] >= 5){
						gen.rgb[gen.cmod] -= 5;
						gen.cflag = 1;
						pj_init(&gen, &pj, "img/pj/");
						gen.cflag = 0;
					}
					break;
				}
				break;
			}

		if (clock() > clok + 100)
		{
//			printf("i = %d\n",(int)i);
			clok = clock();
			draw_backg(gen.render, gen.screen);
			draw_floor(g_tex, grd, f_level, gen.render, gen.screen,(act)? orient : -1);
			SDL_RenderCopy(gen.render, pj.ani[act][orient][(int)i], NULL, pj.box);
			SDL_RenderPresent(gen.render);
			i += (act) ? 0.50 : 0.04;
//			printf("i = %f ===> %d\n",i,(int)i);
			if (i > 1.30 && !act)
				i = 0;
			else if (i > 5)
				i = 0;
//			Pausing ();
		}
		event.key.keysym.sym = 0;
	}

	SDL_Quit();
	(void)ac;
	(void)av;
	(void)act;
	(void)back;
	(void)gfxPrimitivesFontdata;
	return (0);
}