Bläddra i källkod

v0.2

- added merge sprites.
- fixed memory leak.
- improved speed and fluidity.

To do:
- Cycles settings.
- Sizeable map and responsive menu.
- Map editor.
coldiary 6 år sedan
förälder
incheckning
98e68c7a95

+ 15
- 13
Life.xcodeproj/project.pbxproj Visa fil

7
 	objects = {
7
 	objects = {
8
 
8
 
9
 /* Begin PBXBuildFile section */
9
 /* Begin PBXBuildFile section */
10
-		6A364541194D85E60072C818 /* SDL2_image.framework in Copy Files */ = {isa = PBXBuildFile; fileRef = 6AED72B1194AB2AB00C47B6F /* SDL2_image.framework */; };
11
-		6A364542194D86000072C818 /* SDL2.framework in Copy Files */ = {isa = PBXBuildFile; fileRef = 6AED72AF194AB22800C47B6F /* SDL2.framework */; };
10
+		6A5FDDBC194D953A001B6075 /* SDL2_image.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6A5FDDBA194D953A001B6075 /* SDL2_image.framework */; };
11
+		6A5FDDBD194D953A001B6075 /* SDL2.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6A5FDDBB194D953A001B6075 /* SDL2.framework */; };
12
+		6A5FDDBE194D9574001B6075 /* SDL2_image.framework in Copy Files */ = {isa = PBXBuildFile; fileRef = 6A5FDDBA194D953A001B6075 /* SDL2_image.framework */; };
13
+		6A5FDDBF194D9574001B6075 /* SDL2.framework in Copy Files */ = {isa = PBXBuildFile; fileRef = 6A5FDDBB194D953A001B6075 /* SDL2.framework */; };
14
+		6A5FDDC0194D957F001B6075 /* sprites in CopyFiles */ = {isa = PBXBuildFile; fileRef = 6A364557194D92340072C818 /* sprites */; };
12
 		6AED72A7194AB16C00C47B6F /* main.c in Sources */ = {isa = PBXBuildFile; fileRef = 6AED72A6194AB16C00C47B6F /* main.c */; };
15
 		6AED72A7194AB16C00C47B6F /* main.c in Sources */ = {isa = PBXBuildFile; fileRef = 6AED72A6194AB16C00C47B6F /* main.c */; };
13
-		6AED72B0194AB22800C47B6F /* SDL2.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6AED72AF194AB22800C47B6F /* SDL2.framework */; };
14
-		6AED72B2194AB2AB00C47B6F /* SDL2_image.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6AED72B1194AB2AB00C47B6F /* SDL2_image.framework */; };
15
 		6AED72B8194AB9E700C47B6F /* sim.c in Sources */ = {isa = PBXBuildFile; fileRef = 6AED72B7194AB9E700C47B6F /* sim.c */; };
16
 		6AED72B8194AB9E700C47B6F /* sim.c in Sources */ = {isa = PBXBuildFile; fileRef = 6AED72B7194AB9E700C47B6F /* sim.c */; };
16
 		6AED72BB194AE63600C47B6F /* menu.c in Sources */ = {isa = PBXBuildFile; fileRef = 6AED72BA194AE63600C47B6F /* menu.c */; };
17
 		6AED72BB194AE63600C47B6F /* menu.c in Sources */ = {isa = PBXBuildFile; fileRef = 6AED72BA194AE63600C47B6F /* menu.c */; };
17
 /* End PBXBuildFile section */
18
 /* End PBXBuildFile section */
23
 			dstPath = sprites;
24
 			dstPath = sprites;
24
 			dstSubfolderSpec = 7;
25
 			dstSubfolderSpec = 7;
25
 			files = (
26
 			files = (
27
+				6A5FDDC0194D957F001B6075 /* sprites in CopyFiles */,
26
 			);
28
 			);
27
 			runOnlyForDeploymentPostprocessing = 0;
29
 			runOnlyForDeploymentPostprocessing = 0;
28
 		};
30
 		};
32
 			dstPath = "";
34
 			dstPath = "";
33
 			dstSubfolderSpec = 10;
35
 			dstSubfolderSpec = 10;
34
 			files = (
36
 			files = (
35
-				6A364542194D86000072C818 /* SDL2.framework in Copy Files */,
36
-				6A364541194D85E60072C818 /* SDL2_image.framework in Copy Files */,
37
+				6A5FDDBE194D9574001B6075 /* SDL2_image.framework in Copy Files */,
38
+				6A5FDDBF194D9574001B6075 /* SDL2.framework in Copy Files */,
37
 			);
39
 			);
38
 			name = "Copy Files";
40
 			name = "Copy Files";
39
 			runOnlyForDeploymentPostprocessing = 1;
41
 			runOnlyForDeploymentPostprocessing = 1;
42
 
44
 
43
 /* Begin PBXFileReference section */
45
 /* Begin PBXFileReference section */
44
 		6A364557194D92340072C818 /* sprites */ = {isa = PBXFileReference; lastKnownFileType = folder; path = sprites; sourceTree = "<group>"; };
46
 		6A364557194D92340072C818 /* sprites */ = {isa = PBXFileReference; lastKnownFileType = folder; path = sprites; sourceTree = "<group>"; };
47
+		6A5FDDBA194D953A001B6075 /* SDL2_image.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SDL2_image.framework; path = ../../../../Library/Frameworks/SDL2_image.framework; sourceTree = "<group>"; };
48
+		6A5FDDBB194D953A001B6075 /* SDL2.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SDL2.framework; path = ../../../../Library/Frameworks/SDL2.framework; sourceTree = "<group>"; };
45
 		6AED72A3194AB16C00C47B6F /* Life */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Life; sourceTree = BUILT_PRODUCTS_DIR; };
49
 		6AED72A3194AB16C00C47B6F /* Life */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = Life; sourceTree = BUILT_PRODUCTS_DIR; };
46
 		6AED72A6194AB16C00C47B6F /* main.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = main.c; sourceTree = "<group>"; };
50
 		6AED72A6194AB16C00C47B6F /* main.c */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.c; path = main.c; sourceTree = "<group>"; };
47
-		6AED72AF194AB22800C47B6F /* SDL2.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SDL2.framework; path = ../../../../Library/Frameworks/SDL2.framework; sourceTree = "<group>"; };
48
-		6AED72B1194AB2AB00C47B6F /* SDL2_image.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SDL2_image.framework; path = ../../../../Library/Frameworks/SDL2_image.framework; sourceTree = "<group>"; };
49
 		6AED72B4194AB36500C47B6F /* constante.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = constante.h; sourceTree = "<group>"; };
51
 		6AED72B4194AB36500C47B6F /* constante.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = constante.h; sourceTree = "<group>"; };
50
 		6AED72B7194AB9E700C47B6F /* sim.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = sim.c; sourceTree = "<group>"; };
52
 		6AED72B7194AB9E700C47B6F /* sim.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = sim.c; sourceTree = "<group>"; };
51
 		6AED72B9194AB9F600C47B6F /* sim.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = sim.h; sourceTree = "<group>"; };
53
 		6AED72B9194AB9F600C47B6F /* sim.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = sim.h; sourceTree = "<group>"; };
58
 			isa = PBXFrameworksBuildPhase;
60
 			isa = PBXFrameworksBuildPhase;
59
 			buildActionMask = 2147483647;
61
 			buildActionMask = 2147483647;
60
 			files = (
62
 			files = (
61
-				6AED72B2194AB2AB00C47B6F /* SDL2_image.framework in Frameworks */,
62
-				6AED72B0194AB22800C47B6F /* SDL2.framework in Frameworks */,
63
+				6A5FDDBC194D953A001B6075 /* SDL2_image.framework in Frameworks */,
64
+				6A5FDDBD194D953A001B6075 /* SDL2.framework in Frameworks */,
63
 			);
65
 			);
64
 			runOnlyForDeploymentPostprocessing = 0;
66
 			runOnlyForDeploymentPostprocessing = 0;
65
 		};
67
 		};
69
 		6AED729A194AB16B00C47B6F = {
71
 		6AED729A194AB16B00C47B6F = {
70
 			isa = PBXGroup;
72
 			isa = PBXGroup;
71
 			children = (
73
 			children = (
72
-				6AED72B1194AB2AB00C47B6F /* SDL2_image.framework */,
73
-				6AED72AF194AB22800C47B6F /* SDL2.framework */,
74
+				6A5FDDBA194D953A001B6075 /* SDL2_image.framework */,
75
+				6A5FDDBB194D953A001B6075 /* SDL2.framework */,
74
 				6AED72A5194AB16C00C47B6F /* Life */,
76
 				6AED72A5194AB16C00C47B6F /* Life */,
75
 				6AED72A4194AB16C00C47B6F /* Products */,
77
 				6AED72A4194AB16C00C47B6F /* Products */,
76
 			);
78
 			);
106
 			buildConfigurationList = 6AED72AC194AB16C00C47B6F /* Build configuration list for PBXNativeTarget "Life" */;
108
 			buildConfigurationList = 6AED72AC194AB16C00C47B6F /* Build configuration list for PBXNativeTarget "Life" */;
107
 			buildPhases = (
109
 			buildPhases = (
108
 				6AED729F194AB16C00C47B6F /* Sources */,
110
 				6AED729F194AB16C00C47B6F /* Sources */,
109
-				6AED72A0194AB16C00C47B6F /* Frameworks */,
110
 				6AED72A1194AB16C00C47B6F /* Copy Files */,
111
 				6AED72A1194AB16C00C47B6F /* Copy Files */,
111
 				6A36454C194D87C60072C818 /* CopyFiles */,
112
 				6A36454C194D87C60072C818 /* CopyFiles */,
113
+				6AED72A0194AB16C00C47B6F /* Frameworks */,
112
 			);
114
 			);
113
 			buildRules = (
115
 			buildRules = (
114
 			);
116
 			);

+ 15
- 2
Life/constante.h Visa fil

17
 #define NB_BLOCS 12
17
 #define NB_BLOCS 12
18
 #define LARGEUR_ECRAN TAILLE_BLOC*NB_BLOCS
18
 #define LARGEUR_ECRAN TAILLE_BLOC*NB_BLOCS
19
 #define HAUTEUR_ECRAN TAILLE_BLOC*NB_BLOCS
19
 #define HAUTEUR_ECRAN TAILLE_BLOC*NB_BLOCS
20
-#define CYCLES 20
21
-#define WAIT 0
20
+#define CYCLES 24
21
+#define WAIT 100
22
 #define MAXMAP (NB_BLOCS*NB_BLOCS)
22
 #define MAXMAP (NB_BLOCS*NB_BLOCS)
23
+#define SHEETSIZE 16
23
 
24
 
24
 enum {HAUT, BAS, GAUCHE, DROITE};
25
 enum {HAUT, BAS, GAUCHE, DROITE};
25
 
26
 
27
+typedef struct Sprites Sprites;
28
+struct Sprites {
29
+    SDL_Surface *menu;
30
+    SDL_Surface *arrow;
31
+    SDL_Surface *cell;
32
+    SDL_Surface *deadcell;
33
+    SDL_Surface *alivecell;
34
+    SDL_Surface *cellsheet;
35
+    SDL_Surface *icon;
36
+    SDL_Rect clip[SHEETSIZE];
37
+};
38
+
26
 #endif
39
 #endif

+ 27
- 5
Life/main.c Visa fil

11
 #include "menu.h"
11
 #include "menu.h"
12
 #include "sim.h"
12
 #include "sim.h"
13
 
13
 
14
+static void initSprites(Sprites *Sprites);
15
+
14
 int main(int argc, const char * argv[])
16
 int main(int argc, const char * argv[])
15
 {
17
 {
16
     if (SDL_Init(SDL_INIT_VIDEO) == -1) {
18
     if (SDL_Init(SDL_INIT_VIDEO) == -1) {
19
     }
21
     }
20
     int choice = 0;
22
     int choice = 0;
21
     SDL_Window *ecran;
23
     SDL_Window *ecran;
22
-    SDL_Surface *screen, *icon;
24
+    SDL_Surface *screen;
25
+    Sprites Sprites;
23
     ecran = SDL_CreateWindow("LIFE", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, LARGEUR_ECRAN, HAUTEUR_ECRAN, SDL_WINDOW_SHOWN);
26
     ecran = SDL_CreateWindow("LIFE", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, LARGEUR_ECRAN, HAUTEUR_ECRAN, SDL_WINDOW_SHOWN);
24
     screen = SDL_GetWindowSurface(ecran);
27
     screen = SDL_GetWindowSurface(ecran);
25
-    icon = IMG_Load("sprites/icon.png");
26
-    SDL_SetWindowIcon(ecran, icon);
28
+    initSprites(&Sprites);
29
+    SDL_SetWindowIcon(ecran, Sprites.icon);
27
     while (choice == 0) {
30
     while (choice == 0) {
28
-        choice = menu(ecran, screen);
31
+        choice = menu(&Sprites, ecran, screen);
29
         switch (choice) {
32
         switch (choice) {
30
             case 1:
33
             case 1:
31
-                sim(ecran, screen, CYCLES);
34
+                sim(&Sprites, ecran, screen, CYCLES);
32
                 choice = 0;
35
                 choice = 0;
33
                 break;
36
                 break;
34
             case 2:
37
             case 2:
43
     return EXIT_SUCCESS;
46
     return EXIT_SUCCESS;
44
 }
47
 }
45
 
48
 
49
+static void initSprites(Sprites *Sprites)
50
+{
51
+    Sprites->cell = IMG_Load("sprites/cell.png");
52
+    Sprites->alivecell = IMG_Load("sprites/alivecell.png");
53
+    Sprites->deadcell = IMG_Load("sprites/deadcell.png");
54
+    Sprites->cellsheet = IMG_Load("sprites/cellsheet.png");
55
+    Sprites->menu = IMG_Load("sprites/menu.png");
56
+    Sprites->arrow = IMG_Load("sprites/arrow.png");
57
+    Sprites->icon = IMG_Load("sprites/icon.png");
58
+    
59
+    for (int i = 0; i < SHEETSIZE; i++)
60
+    {
61
+        Sprites->clip[i].x = (i%(SHEETSIZE/4))*TAILLE_BLOC;
62
+        Sprites->clip[i].y = (i/(SHEETSIZE/4))*TAILLE_BLOC;
63
+        Sprites->clip[i].h = Sprites->clip[i].w = TAILLE_BLOC;
64
+    }
65
+    
66
+}
67
+

+ 9
- 8
Life/menu.c Visa fil

12
 
12
 
13
 static void eventChoice(int* choice, int* running, int* current);
13
 static void eventChoice(int* choice, int* running, int* current);
14
 
14
 
15
-int menu(SDL_Window *ecran, SDL_Surface *screen)
15
+int menu(Sprites *Sprites, SDL_Window *ecran, SDL_Surface *screen)
16
 {
16
 {
17
     int running = 1, choice = 0, current = 1;
17
     int running = 1, choice = 0, current = 1;
18
     SDL_Rect select, position, element1 = {4,7}, element2={5,9};
18
     SDL_Rect select, position, element1 = {4,7}, element2={5,9};
19
-    SDL_Surface *menu = NULL, *arrow = NULL, *wall = NULL;
20
-    menu = IMG_Load("sprites/menu.png");
21
-    arrow = IMG_Load("sprites/arrow.png");
22
-    wall = IMG_Load("sprites/wall.png");
23
     while (running) {
19
     while (running) {
20
+        for (int i = 0 ; i < MAXMAP ; i++)
21
+        {
22
+            position.x = i%NB_BLOCS * TAILLE_BLOC;
23
+            position.y = i/NB_BLOCS * TAILLE_BLOC;
24
+            SDL_BlitSurface(Sprites->cell, NULL, screen, &position);
25
+        }
24
         position.x = position.y = 0;
26
         position.x = position.y = 0;
25
-        SDL_BlitSurface(wall, NULL, screen, &position);
26
-        SDL_BlitSurface(menu, NULL, screen, &position); // Affiche le menu
27
+        SDL_BlitSurface(Sprites->menu, NULL, screen, &position); 
27
         (current == 1) ? (select.x = element1.x * TAILLE_BLOC) : (select.x = element2.x * TAILLE_BLOC);
28
         (current == 1) ? (select.x = element1.x * TAILLE_BLOC) : (select.x = element2.x * TAILLE_BLOC);
28
         (current == 1) ? (select.y = element1.y * TAILLE_BLOC) : (select.y = element2.y * TAILLE_BLOC);
29
         (current == 1) ? (select.y = element1.y * TAILLE_BLOC) : (select.y = element2.y * TAILLE_BLOC);
29
-        SDL_BlitSurface(arrow, NULL, screen, &select);
30
+        SDL_BlitSurface(Sprites->arrow, NULL, screen, &select);
30
         SDL_UpdateWindowSurface(ecran);
31
         SDL_UpdateWindowSurface(ecran);
31
         eventChoice(&choice, &running, &current);
32
         eventChoice(&choice, &running, &current);
32
     }
33
     }

+ 1
- 1
Life/menu.h Visa fil

9
 #ifndef Life_menu_h
9
 #ifndef Life_menu_h
10
 #define Life_menu_h
10
 #define Life_menu_h
11
 
11
 
12
-int menu(SDL_Window *ecran, SDL_Surface *screen);
12
+int menu(Sprites *Sprites, SDL_Window *ecran, SDL_Surface *screen);
13
 
13
 
14
 #endif
14
 #endif

+ 68
- 49
Life/sim.c Visa fil

11
 #include "sim.h"
11
 #include "sim.h"
12
 
12
 
13
 static void randmap(Cell map[]);
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
 static void eventWatch();
15
 static void eventWatch();
16
 static void next(Cell map[]);
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
     Cell map[MAXMAP];
22
     Cell map[MAXMAP];
23
     randmap(map);
23
     randmap(map);
24
     int remaining = cycles;
24
     int remaining = cycles;
25
     while (remaining != 0){
25
     while (remaining != 0){
26
-        drawmap(screen, map);
26
+        refreshCells(map);
27
+        drawmap(Sprites, screen, map);
27
         SDL_UpdateWindowSurface(ecran);
28
         SDL_UpdateWindowSurface(ecran);
28
         eventWatch(&remaining);
29
         eventWatch(&remaining);
29
         SDL_Delay(WAIT);
30
         SDL_Delay(WAIT);
39
         map[i].x = i%NB_BLOCS;
40
         map[i].x = i%NB_BLOCS;
40
         map[i].y = i/NB_BLOCS;
41
         map[i].y = i/NB_BLOCS;
41
         map[i].type = rand()%2;
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
     SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
49
     SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255));
50
     for (int i = 0 ; i < MAXMAP ; i++)
50
     for (int i = 0 ; i < MAXMAP ; i++)
51
     {
51
     {
52
             position.x = map[i].x * TAILLE_BLOC;
52
             position.x = map[i].x * TAILLE_BLOC;
53
             position.y = map[i].y * TAILLE_BLOC;
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
 
94
 
95
 static void next(Cell map[])
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
     for (int i = 0 ; i < MAXMAP ; i++)
97
     for (int i = 0 ; i < MAXMAP ; i++)
107
     {
98
     {
108
         map[i].type = map[i].type == 1 ? (map[i].voisines == 2 || map[i].voisines == 3 ? 1 : 2) : (map[i].voisines == 3 ? 1 : 0);
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
     if (Cell.type == 0)
105
     if (Cell.type == 0)
120
-        SDL_BlitSurface(cell, NULL, screen, &position);
106
+        SDL_BlitSurface(Sprites->cell, NULL, screen, &position);
121
     else if (Cell.type == 2)
107
     else if (Cell.type == 2)
122
-        SDL_BlitSurface(deadcell, NULL, screen, &position);
108
+        SDL_BlitSurface(Sprites->deadcell, NULL, screen, &position);
123
     else {
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
-}*/

+ 4
- 1
Life/sim.h Visa fil

9
 #ifndef Life_sim_h
9
 #ifndef Life_sim_h
10
 #define Life_sim_h
10
 #define Life_sim_h
11
 
11
 
12
-void sim(SDL_Window *ecran, SDL_Surface *screen, int cycles);
12
+void sim(Sprites *Sprites, SDL_Window *ecran, SDL_Surface *screen, int cycles);
13
 
13
 
14
 enum {CELL, ALIVE, DEAD};
14
 enum {CELL, ALIVE, DEAD};
15
+enum { TOP, RIGHT, LOW, LEFT};
15
 
16
 
16
 typedef struct Cell Cell;
17
 typedef struct Cell Cell;
17
 struct Cell
18
 struct Cell
20
     int y;
21
     int y;
21
     int type;
22
     int type;
22
     int voisines;
23
     int voisines;
24
+    bool near[4];
25
+    int clip;
23
 };
26
 };
24
 
27
 
25
 #endif
28
 #endif

Binär
Life/sprites/alivecell.png Visa fil


Binär
Life/sprites/cellsheet.png Visa fil


Binär
Life/sprites/downcell.png Visa fil


Binär
Life/sprites/leftcell.png Visa fil


Binär
Life/sprites/lowerleftcell.png Visa fil


Binär
Life/sprites/lowerrightcell.png Visa fil


Binär
Life/sprites/plaincell.png Visa fil


Binär
Life/sprites/rightcell.png Visa fil


Binär
Life/sprites/upcell.png Visa fil


Binär
Life/sprites/upperleftcell.png Visa fil


Binär
Life/sprites/upperrightcell.png Visa fil