|
|
@@ -11,19 +11,20 @@
|
|
11
|
11
|
#include "sim.h"
|
|
12
|
12
|
|
|
13
|
13
|
static void randmap(Cell map[]);
|
|
14
|
|
-static void drawmap(SDL_Surface *screen, Cell map[]);
|
|
|
14
|
+static void drawmap(Sprites *Sprites, SDL_Surface *screen, Cell map[]);
|
|
15
|
15
|
static void eventWatch();
|
|
16
|
16
|
static void next(Cell map[]);
|
|
17
|
|
-static void drawCell(SDL_Surface *screen, SDL_Rect position, Cell Cell);
|
|
18
|
|
-//static void blitSprite(int orig, SDL_Surface *screen, SDL_Rect position);
|
|
|
17
|
+static void drawCell(Sprites *Sprites, SDL_Surface *screen, SDL_Rect position, Cell Cell);
|
|
|
18
|
+static void refreshCells(Cell map[]);
|
|
19
|
19
|
|
|
20
|
|
-void sim(SDL_Window *ecran, SDL_Surface *screen, int cycles)
|
|
|
20
|
+void sim(Sprites *Sprites, SDL_Window *ecran, SDL_Surface *screen, int cycles)
|
|
21
|
21
|
{
|
|
22
|
22
|
Cell map[MAXMAP];
|
|
23
|
23
|
randmap(map);
|
|
24
|
24
|
int remaining = cycles;
|
|
25
|
25
|
while (remaining != 0){
|
|
26
|
|
- drawmap(screen, map);
|
|
|
26
|
+ refreshCells(map);
|
|
|
27
|
+ drawmap(Sprites, screen, map);
|
|
27
|
28
|
SDL_UpdateWindowSurface(ecran);
|
|
28
|
29
|
eventWatch(&remaining);
|
|
29
|
30
|
SDL_Delay(WAIT);
|
|
|
@@ -39,19 +40,18 @@ static void randmap(Cell map[])
|
|
39
|
40
|
map[i].x = i%NB_BLOCS;
|
|
40
|
41
|
map[i].y = i/NB_BLOCS;
|
|
41
|
42
|
map[i].type = rand()%2;
|
|
42
|
|
- map[i].voisines = 0;
|
|
43
|
43
|
}
|
|
44
|
44
|
}
|
|
45
|
45
|
|
|
46
|
|
-static void drawmap(SDL_Surface *screen, Cell map[])
|
|
|
46
|
+static void drawmap(Sprites *Sprites, SDL_Surface *screen, Cell map[])
|
|
47
|
47
|
{
|
|
48
|
|
- SDL_Rect position;
|
|
|
48
|
+ SDL_Rect position = {0,0};
|
|
49
|
49
|
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
|
|
50
|
50
|
for (int i = 0 ; i < MAXMAP ; i++)
|
|
51
|
51
|
{
|
|
52
|
52
|
position.x = map[i].x * TAILLE_BLOC;
|
|
53
|
53
|
position.y = map[i].y * TAILLE_BLOC;
|
|
54
|
|
- drawCell(screen, position, map[i]);
|
|
|
54
|
+ drawCell(Sprites, screen, position, map[i]);
|
|
55
|
55
|
}
|
|
56
|
56
|
}
|
|
57
|
57
|
|
|
|
@@ -94,58 +94,77 @@ static void eventWatch()
|
|
94
|
94
|
|
|
95
|
95
|
static void next(Cell map[])
|
|
96
|
96
|
{
|
|
97
|
|
- for (int i = 0 ; i < MAXMAP ; i++)
|
|
98
|
|
- {
|
|
99
|
|
- map[i].voisines = 0;
|
|
100
|
|
- map[i].voisines = i%NB_BLOCS == 0 ? map[i].voisines : (map[i-1].type == 1 ? ++map[i].voisines : map[i].voisines);
|
|
101
|
|
- map[i].voisines = i%(NB_BLOCS) == 11 ? map[i].voisines : (map[i+1].type == 1 ? ++map[i].voisines : map[i].voisines);
|
|
102
|
|
- map[i].voisines = i-NB_BLOCS < 0 ? map[i].voisines : (map[i-NB_BLOCS].type == 1 ? ++map[i].voisines : map[i].voisines);
|
|
103
|
|
- map[i].voisines = i+NB_BLOCS > MAXMAP ? map[i].voisines : (map[i+NB_BLOCS].type == 1 ? ++map[i].voisines : map[i].voisines);
|
|
104
|
|
- }
|
|
105
|
|
-
|
|
106
|
97
|
for (int i = 0 ; i < MAXMAP ; i++)
|
|
107
|
98
|
{
|
|
108
|
99
|
map[i].type = map[i].type == 1 ? (map[i].voisines == 2 || map[i].voisines == 3 ? 1 : 2) : (map[i].voisines == 3 ? 1 : 0);
|
|
109
|
100
|
}
|
|
110
|
101
|
}
|
|
111
|
102
|
|
|
112
|
|
-static void drawCell(SDL_Surface *screen, SDL_Rect position, Cell Cell)
|
|
|
103
|
+static void drawCell(Sprites *Sprites, SDL_Surface *screen, SDL_Rect position, Cell Cell)
|
|
113
|
104
|
{
|
|
114
|
|
- SDL_Surface *cell = NULL, *deadcell = NULL, *alivecell = NULL;
|
|
115
|
|
- cell = IMG_Load("sprites/cell.png");
|
|
116
|
|
- alivecell = IMG_Load("sprites/alivecell.png");
|
|
117
|
|
- deadcell = IMG_Load("sprites/deadcell.png");
|
|
118
|
|
-
|
|
119
|
105
|
if (Cell.type == 0)
|
|
120
|
|
- SDL_BlitSurface(cell, NULL, screen, &position);
|
|
|
106
|
+ SDL_BlitSurface(Sprites->cell, NULL, screen, &position);
|
|
121
|
107
|
else if (Cell.type == 2)
|
|
122
|
|
- SDL_BlitSurface(deadcell, NULL, screen, &position);
|
|
|
108
|
+ SDL_BlitSurface(Sprites->deadcell, NULL, screen, &position);
|
|
123
|
109
|
else {
|
|
124
|
|
- SDL_BlitSurface(alivecell, NULL, screen, &position);
|
|
|
110
|
+ //SDL_BlitSurface(Sprites->alivecell, NULL, screen, &position);
|
|
|
111
|
+ SDL_BlitSurface(Sprites->cellsheet, &Sprites->clip[Cell.clip], screen, &position);
|
|
125
|
112
|
}
|
|
126
|
113
|
}
|
|
127
|
114
|
|
|
128
|
|
-/*static void blitSprite(int orig, SDL_Surface *screen, SDL_Rect position)
|
|
|
115
|
+static void refreshCells(Cell map[])
|
|
129
|
116
|
{
|
|
130
|
|
- SDL_Surface *cellsheet;
|
|
131
|
|
- cellsheet = IMG_Load("sprites/cellsheet.png");
|
|
132
|
|
-
|
|
133
|
|
- SDL_Rect clip[9];
|
|
134
|
|
- clip[0].x = clip[0].y = 0;
|
|
135
|
|
- clip[1].x = 34;clip[1].y = 0;
|
|
136
|
|
- clip[2].x = 68;clip[2].y = 0;
|
|
137
|
|
- clip[3].x = 0;clip[3].y = 34;
|
|
138
|
|
- clip[4].x = clip[4].y = 34;
|
|
139
|
|
- clip[5].x = 68;clip[5].y = 34;
|
|
140
|
|
- clip[6].x = 0;clip[6].y = 68;
|
|
141
|
|
- clip[7].x = 34;clip[7].y = 68;
|
|
142
|
|
- clip[8].x = clip[8].y = 68;
|
|
143
|
|
- for (int i = 0; i > 9; i++)
|
|
144
|
|
- clip[i].h = clip[i].w = 34;
|
|
145
|
|
-
|
|
146
|
|
- SDL_BlitSurface(cellsheet, &clip[orig], screen, &position);
|
|
|
117
|
+ for (int i = 0; i < MAXMAP; i++)
|
|
|
118
|
+ {
|
|
|
119
|
+
|
|
|
120
|
+ // CELL.near
|
|
|
121
|
+ for (int j = 0; j < 4; j++)
|
|
|
122
|
+ {
|
|
|
123
|
+ map[i].near[j] = false;
|
|
|
124
|
+ }
|
|
|
125
|
+ map[i].near[TOP] = i-NB_BLOCS < 0 ? false : (map[i-NB_BLOCS].type == 1 ? true : false);
|
|
|
126
|
+ map[i].near[LEFT] = i%NB_BLOCS == 0 ? false : (map[i-1].type == 1 ? true : false);
|
|
|
127
|
+ map[i].near[RIGHT] = i%NB_BLOCS == 11 ? false : (map[i+1].type == 1 ? true : false);
|
|
|
128
|
+ map[i].near[LOW] = i+NB_BLOCS > MAXMAP ? false : (map[i+NB_BLOCS].type == 1 ? true : false);
|
|
|
129
|
+
|
|
|
130
|
+ //CELL.voisines
|
|
|
131
|
+ map[i].voisines = 0;
|
|
|
132
|
+ for (int j = 0; j < 4; j ++)
|
|
|
133
|
+ {
|
|
|
134
|
+ map[i].voisines = map[i].near[j] == true ? ++map[i].voisines : map[i].voisines;
|
|
|
135
|
+ }
|
|
|
136
|
+
|
|
|
137
|
+ // CELL.clip
|
|
|
138
|
+ map[i].clip = 0;
|
|
|
139
|
+ switch (map[i].voisines)
|
|
|
140
|
+ {
|
|
|
141
|
+ case 1:
|
|
|
142
|
+ map[i].clip = map[i].near[TOP] ? 10 : (map[i].near[RIGHT] ? 11 : (map[i].near[LOW] ? 12 : 13));
|
|
|
143
|
+ break;
|
|
|
144
|
+ case 2:
|
|
|
145
|
+ if (map[i].near[TOP])
|
|
|
146
|
+ map[i].clip = map[i].near[RIGHT] ? 9 : (map[i].near[LOW] ? 8 : 7);
|
|
|
147
|
+ else if (map[i].near[RIGHT])
|
|
|
148
|
+ map[i].clip = map[i].near[LOW] ? 6 : 5;
|
|
|
149
|
+ else
|
|
|
150
|
+ map[i].clip = 4;
|
|
|
151
|
+ break;
|
|
|
152
|
+ case 3:
|
|
|
153
|
+ if (!map[i].near[TOP])
|
|
|
154
|
+ map[i].clip = 0;
|
|
|
155
|
+ else if (!map[i].near[RIGHT])
|
|
|
156
|
+ map[i].clip = 1;
|
|
|
157
|
+ else if (!map[i].near[LOW])
|
|
|
158
|
+ map[i].clip = 2;
|
|
|
159
|
+ else
|
|
|
160
|
+ map[i].clip = 3;
|
|
|
161
|
+ break;
|
|
|
162
|
+ case 4:
|
|
|
163
|
+ map[i].clip = 14;
|
|
|
164
|
+ break;
|
|
|
165
|
+ case 0:
|
|
|
166
|
+ map[i].clip = 15;
|
|
|
167
|
+ break;
|
|
|
168
|
+ }
|
|
|
169
|
+ }
|
|
147
|
170
|
}
|
|
148
|
|
-
|
|
149
|
|
-static void switchType(Cell map[])
|
|
150
|
|
-{
|
|
151
|
|
-}*/
|