소스 검색

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 년 전
부모
커밋
98e68c7a95

+ 15
- 13
Life.xcodeproj/project.pbxproj 파일 보기

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

+ 15
- 2
Life/constante.h 파일 보기

@@ -17,10 +17,23 @@
17 17
 #define NB_BLOCS 12
18 18
 #define LARGEUR_ECRAN TAILLE_BLOC*NB_BLOCS
19 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 22
 #define MAXMAP (NB_BLOCS*NB_BLOCS)
23
+#define SHEETSIZE 16
23 24
 
24 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 39
 #endif

+ 27
- 5
Life/main.c 파일 보기

@@ -11,6 +11,8 @@
11 11
 #include "menu.h"
12 12
 #include "sim.h"
13 13
 
14
+static void initSprites(Sprites *Sprites);
15
+
14 16
 int main(int argc, const char * argv[])
15 17
 {
16 18
     if (SDL_Init(SDL_INIT_VIDEO) == -1) {
@@ -19,16 +21,17 @@ int main(int argc, const char * argv[])
19 21
     }
20 22
     int choice = 0;
21 23
     SDL_Window *ecran;
22
-    SDL_Surface *screen, *icon;
24
+    SDL_Surface *screen;
25
+    Sprites Sprites;
23 26
     ecran = SDL_CreateWindow("LIFE", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, LARGEUR_ECRAN, HAUTEUR_ECRAN, SDL_WINDOW_SHOWN);
24 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 30
     while (choice == 0) {
28
-        choice = menu(ecran, screen);
31
+        choice = menu(&Sprites, ecran, screen);
29 32
         switch (choice) {
30 33
             case 1:
31
-                sim(ecran, screen, CYCLES);
34
+                sim(&Sprites, ecran, screen, CYCLES);
32 35
                 choice = 0;
33 36
                 break;
34 37
             case 2:
@@ -43,3 +46,22 @@ int main(int argc, const char * argv[])
43 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 파일 보기

@@ -12,21 +12,22 @@
12 12
 
13 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 17
     int running = 1, choice = 0, current = 1;
18 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 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 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 28
         (current == 1) ? (select.x = element1.x * TAILLE_BLOC) : (select.x = element2.x * TAILLE_BLOC);
28 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 31
         SDL_UpdateWindowSurface(ecran);
31 32
         eventChoice(&choice, &running, &current);
32 33
     }

+ 1
- 1
Life/menu.h 파일 보기

@@ -9,6 +9,6 @@
9 9
 #ifndef Life_menu_h
10 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 14
 #endif

+ 68
- 49
Life/sim.c 파일 보기

@@ -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
-}*/

+ 4
- 1
Life/sim.h 파일 보기

@@ -9,9 +9,10 @@
9 9
 #ifndef Life_sim_h
10 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 14
 enum {CELL, ALIVE, DEAD};
15
+enum { TOP, RIGHT, LOW, LEFT};
15 16
 
16 17
 typedef struct Cell Cell;
17 18
 struct Cell
@@ -20,6 +21,8 @@ struct Cell
20 21
     int y;
21 22
     int type;
22 23
     int voisines;
24
+    bool near[4];
25
+    int clip;
23 26
 };
24 27
 
25 28
 #endif

BIN
Life/sprites/alivecell.png 파일 보기


BIN
Life/sprites/cellsheet.png 파일 보기


BIN
Life/sprites/downcell.png 파일 보기


BIN
Life/sprites/leftcell.png 파일 보기


BIN
Life/sprites/lowerleftcell.png 파일 보기


BIN
Life/sprites/lowerrightcell.png 파일 보기


BIN
Life/sprites/plaincell.png 파일 보기


BIN
Life/sprites/rightcell.png 파일 보기


BIN
Life/sprites/upcell.png 파일 보기


BIN
Life/sprites/upperleftcell.png 파일 보기


BIN
Life/sprites/upperrightcell.png 파일 보기