1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
|
int createNewLife(SDL_Surface* screen, life **pp_humans, world *aWorld, signed int i, signed int i2, int sex, int pMouseX, int pMouseY, int stat)
{
int c = 254, p = L_H_CASE*L_H_CASE, s = 5;
int r = 0, g = 0, b = 0;
int error = 0, n = 0;
int x = 0, y = 0;
if(*pp_humans != NULL || stat == 1) /* stat = 1 signifie que la creation debute et que *pp_humans == NULL */
{
SDL_Rect position;
if(aWorld != NULL)
{
n = aWorld->nbr_of_lifes + 1; /* Comme on cree un nouvel humain, on incremente */
life *temp_humans = NULL;
life *temp_humans2 = *pp_humans;
temp_humans = realloc(*pp_humans, n*sizeof *temp_humans);
if(temp_humans != NULL)
{
*pp_humans = temp_humans;
(temp_humans+n-1)->surface = SDL_CreateRGBSurface(SDL_HWSURFACE, L_H_CASE, L_H_CASE, 32, 0, 0, 0, 0);
(temp_humans+n-1)->time_life = TIME_OF_LIFE;
(temp_humans+n-1)->activate = 0;
r = rand() % (c+1);
g = rand() % (c+1);
b = rand() % (c+1);
if(pMouseX != -1 && pMouseX != -1) /* Si l'ordre de creation de vie vient de l'utilisateur */
{
x = pMouseX;
y = pMouseY;
}
else /* Sinon on genere une position au hasard avec une contraite */
{
x = rand() % (p+1);
y = rand() % (p+1);
}
if(i != -1 && i2 != -1) /* Si ce n'est pas la creation de depart */
{
sex = rand() % (s+1);
if(sex >= 2)
{
(temp_humans+n-1)->sex = (temp_humans2+i)->sex; /* Le nouvel humain prend le sexe du geniteur 1 */
}
else
{
(temp_humans+n-1)->sex = (temp_humans2+i2)->sex; /* Il prend celui du geniteur 2 */
}
(temp_humans+n-1)->color.r = mix_color((temp_humans2+i)->color.r, (temp_humans2+i2)->color.r);
(temp_humans+n-1)->color.g = mix_color((temp_humans2+i)->color.g, (temp_humans2+i2)->color.g);
(temp_humans+n-1)->color.b = mix_color((temp_humans2+i)->color.b, (temp_humans2+i2)->color.b);
(temp_humans+n-1)->parents.id_person1 = (temp_humans2+i)->id; /* L'identite' de son pere */
(temp_humans+n-1)->parents.id_person2 = (temp_humans2+i2)->id; /* Celle de sa mere */
}
else /* Si c'est la creation de depart alors on genere tout ale'atoirement */
{
while (r == 0 && g == 0 && b == 0)
{
r = rand() % (c+1);
g = rand() % (c+1);
b = rand() % (c+1);
}
(temp_humans+n-1)->color.r = r;
(temp_humans+n-1)->color.g = g;
(temp_humans+n-1)->color.b = b;
(temp_humans+n-1)->sex = sex;
}
(temp_humans+n-1)->position.x = x;
position.x = x;
(temp_humans+n-1)->position.y = y;
position.y = y;
(temp_humans+n-1)->id = n; /* Son identite' */
SDL_FillRect((temp_humans+n-1)->surface, NULL, SDL_MapRGB(screen->format, r, g, b));
SDL_BlitSurface((temp_humans+n-1)->surface, NULL, screen, &position);
aWorld->nbr_of_lifes = n;
aWorld->nbr_of_lifes_now++;
}
else
{
error = 3;
}
}
else
{
error = 2;
}
}
else
{
error = 1;
}
return error;
} |
Partager