git » fp-git.git » commit 21143cd

resource frees.

author ecalot
2004-07-23 19:11:46 UTC
committer ecalot
2004-07-23 19:11:46 UTC
parent 07822b69ac079ade26110ef331329a479e99f293

resource frees.

FP/src/include/kid.h +1 -0
FP/src/include/object.h +1 -0
FP/src/ker/kernel.c +2 -0
FP/src/ker/kid.c +38 -10
FP/src/ker/object.c +38 -10
FP/src/out/output.c +3 -1
FP/src/res/resources.c +8 -0

diff --git a/FP/src/include/kid.h b/FP/src/include/kid.h
index ffd35c4..0bd9458 100644
--- a/FP/src/include/kid.h
+++ b/FP/src/include/kid.h
@@ -52,6 +52,7 @@ typedef struct {
 tKid kidCreate();
 void kidDraw(tKid kid);
 int  kidMove(tKid* kid,tKey key/*,tRoom room*/);
+void kidFree();
 
 #endif
 
diff --git a/FP/src/include/object.h b/FP/src/include/object.h
index ffd35c4..0bd9458 100644
--- a/FP/src/include/object.h
+++ b/FP/src/include/object.h
@@ -52,6 +52,7 @@ typedef struct {
 tKid kidCreate();
 void kidDraw(tKid kid);
 int  kidMove(tKid* kid,tKey key/*,tRoom room*/);
+void kidFree();
 
 #endif
 
diff --git a/FP/src/ker/kernel.c b/FP/src/ker/kernel.c
index d3f753a..c595b6f 100644
--- a/FP/src/ker/kernel.c
+++ b/FP/src/ker/kernel.c
@@ -145,6 +145,8 @@ int kernel(int optionflag,int level) {
 			quit=control(optionflag,level);
 		}
 	} while(!quit);
+
+	kidFree();
 	outputStop();
 	return 0;
 }
diff --git a/FP/src/ker/kid.c b/FP/src/ker/kid.c
index 66f3473..8759350 100644
--- a/FP/src/ker/kid.c
+++ b/FP/src/ker/kid.c
@@ -32,6 +32,7 @@ kid.h: Free Prince : Kid object
 
 #include "kid.h"
 #include "output.h"
+#include "resources.h" /* resLoad/resFree */
 #include <stdio.h> /* NULL */
 
 static struct {
@@ -42,6 +43,7 @@ static struct {
 	tData* walking[2];
 	tData* running[2];
 	tData* startrunning[2];
+	tData* stoprunning[2];
 	tData* turnrunning[2];
 	tData* jumprunning[2];
 } kidGfx;
@@ -61,20 +63,45 @@ void loadGfx() {
 	kidGfx.running[DIR_RIGHT]=resLoad(RES_ANIM_RUN|RES_MOD_RIGHT);
 	kidGfx.startrunning[DIR_LEFT]=resLoad(RES_ANIM_RUN_START|RES_MOD_LEFT);
 	kidGfx.startrunning[DIR_RIGHT]=resLoad(RES_ANIM_RUN_START|RES_MOD_RIGHT);
+	kidGfx.stoprunning[DIR_LEFT]=resLoad(RES_ANIM_RUN_STOP|RES_MOD_LEFT);
+	kidGfx.stoprunning[DIR_RIGHT]=resLoad(RES_ANIM_RUN_STOP|RES_MOD_RIGHT);
 	kidGfx.turnrunning[DIR_LEFT]=resLoad(RES_ANIM_RUN_TURN|RES_MOD_LEFT);
 	kidGfx.turnrunning[DIR_RIGHT]=resLoad(RES_ANIM_RUN_TURN|RES_MOD_RIGHT);
 	kidGfx.jumprunning[DIR_LEFT]=resLoad(RES_ANIM_JUMPRUN|RES_MOD_LEFT);
 	kidGfx.jumprunning[DIR_RIGHT]=resLoad(RES_ANIM_JUMPRUN|RES_MOD_RIGHT);
 }
 
+void kidFree() {
+	resFree(kidGfx.turning[DIR_LEFT]);
+	resFree(kidGfx.turning[DIR_RIGHT]);
+	resFree(kidGfx.normal[DIR_LEFT]);
+	resFree(kidGfx.normal[DIR_RIGHT]);
+	resFree(kidGfx.walking[DIR_LEFT]);
+	resFree(kidGfx.walking[DIR_RIGHT]);
+	resFree(kidGfx.couching[DIR_LEFT]);
+	resFree(kidGfx.couching[DIR_RIGHT]);
+	resFree(kidGfx.jumping[DIR_LEFT]);
+	resFree(kidGfx.jumping[DIR_RIGHT]);
+	resFree(kidGfx.running[DIR_LEFT]);
+	resFree(kidGfx.running[DIR_RIGHT]);
+	resFree(kidGfx.startrunning[DIR_LEFT]);
+	resFree(kidGfx.startrunning[DIR_RIGHT]);
+	resFree(kidGfx.turnrunning[DIR_LEFT]);
+	resFree(kidGfx.turnrunning[DIR_RIGHT]);
+	resFree(kidGfx.jumprunning[DIR_LEFT]);
+	resFree(kidGfx.jumprunning[DIR_RIGHT]);
+}
+
 /* TODO: send this function to maps.c */
 tKid kidCreate() {
 	tKid kid;
+
+	if (kidGfx.turning[0]==NULL) loadGfx();
+
 	kid.location=100;
 	kid.floor=0;
 	kid.direction=DIR_LEFT;
 	kid.frame=0;
-	if (kidGfx.turning[0]==NULL) loadGfx();
 	kid.action=kidGfx.normal[DIR_LEFT];
 	kid.nextAction=stay;
 	kid.velocity=0;
@@ -111,7 +138,7 @@ int kidMove(tKid* kid,tKey key/*,tRoom room*/) {
 		 *
 		 * Note: falling and climbing context are ignored
 		 */
-		printf("next action=%d direction=%d ",kid->nextAction,kid->direction);
+		printf("next action=%d direction=%d keystatus=%x ",kid->nextAction,kid->direction,key.status);
 
 		/* STAY events
 		 */
@@ -147,10 +174,6 @@ int kidMove(tKid* kid,tKey key/*,tRoom room*/) {
 				/* jump */
 				kid->action=kidGfx.jumping[kid->direction];
 				kid->velocity=0;
-			} else if (!key.status) {
-					/* normal */
-					kid->velocity=0;
-					kid->action=kidGfx.normal[kid->direction];
 			} else if (key.status&K_Left) {
 				if (kid->direction==DIR_LEFT) {
 					/* run left */
@@ -175,6 +198,10 @@ int kidMove(tKid* kid,tKey key/*,tRoom room*/) {
 					kid->velocity=0;
 					kid->direction=DIR_RIGHT;
 				}
+			} else {
+				/* normal */
+				kid->velocity=0;
+				kid->action=kidGfx.normal[kid->direction];
 			}
 			/* TODO: elseif for the K_Left and K_Right to start jumping. Change nextAction */
 		} else if (kid->nextAction==run) {
@@ -208,9 +235,8 @@ int kidMove(tKid* kid,tKey key/*,tRoom room*/) {
 				}
 			} else if (!key.status) {
 				/* Stop running */
-				/* for the moment is normal */
-				kid->velocity=0;
-				kid->action=kidGfx.normal[kid->direction];
+				kid->velocity=kid->velocity/3;
+				kid->action=kidGfx.stoprunning[kid->direction];
 				kid->nextAction=stay;
 			} else if ((key.status&K_Up)==K_Up) {
 				/* Jump running */
@@ -226,7 +252,9 @@ int kidMove(tKid* kid,tKey key/*,tRoom room*/) {
 		return 1;
 	} else {
 		/* TODO: tile traspassing and validations here
-		 * use roomStep(tRoom room,x,y) roomTouch(tRoom room,x,y) to activate tile events */
+		 * use roomStep(tRoom room,x,y) roomTouch(tRoom room,x,y) to activate tile events
+		 * use int roomKidValid(tRoom room,tKid kid) to validate kid position
+		 */
 		kid->location+=kid->velocity;
 		return 0;
 	}
diff --git a/FP/src/ker/object.c b/FP/src/ker/object.c
index 66f3473..8759350 100644
--- a/FP/src/ker/object.c
+++ b/FP/src/ker/object.c
@@ -32,6 +32,7 @@ kid.h: Free Prince : Kid object
 
 #include "kid.h"
 #include "output.h"
+#include "resources.h" /* resLoad/resFree */
 #include <stdio.h> /* NULL */
 
 static struct {
@@ -42,6 +43,7 @@ static struct {
 	tData* walking[2];
 	tData* running[2];
 	tData* startrunning[2];
+	tData* stoprunning[2];
 	tData* turnrunning[2];
 	tData* jumprunning[2];
 } kidGfx;
@@ -61,20 +63,45 @@ void loadGfx() {
 	kidGfx.running[DIR_RIGHT]=resLoad(RES_ANIM_RUN|RES_MOD_RIGHT);
 	kidGfx.startrunning[DIR_LEFT]=resLoad(RES_ANIM_RUN_START|RES_MOD_LEFT);
 	kidGfx.startrunning[DIR_RIGHT]=resLoad(RES_ANIM_RUN_START|RES_MOD_RIGHT);
+	kidGfx.stoprunning[DIR_LEFT]=resLoad(RES_ANIM_RUN_STOP|RES_MOD_LEFT);
+	kidGfx.stoprunning[DIR_RIGHT]=resLoad(RES_ANIM_RUN_STOP|RES_MOD_RIGHT);
 	kidGfx.turnrunning[DIR_LEFT]=resLoad(RES_ANIM_RUN_TURN|RES_MOD_LEFT);
 	kidGfx.turnrunning[DIR_RIGHT]=resLoad(RES_ANIM_RUN_TURN|RES_MOD_RIGHT);
 	kidGfx.jumprunning[DIR_LEFT]=resLoad(RES_ANIM_JUMPRUN|RES_MOD_LEFT);
 	kidGfx.jumprunning[DIR_RIGHT]=resLoad(RES_ANIM_JUMPRUN|RES_MOD_RIGHT);
 }
 
+void kidFree() {
+	resFree(kidGfx.turning[DIR_LEFT]);
+	resFree(kidGfx.turning[DIR_RIGHT]);
+	resFree(kidGfx.normal[DIR_LEFT]);
+	resFree(kidGfx.normal[DIR_RIGHT]);
+	resFree(kidGfx.walking[DIR_LEFT]);
+	resFree(kidGfx.walking[DIR_RIGHT]);
+	resFree(kidGfx.couching[DIR_LEFT]);
+	resFree(kidGfx.couching[DIR_RIGHT]);
+	resFree(kidGfx.jumping[DIR_LEFT]);
+	resFree(kidGfx.jumping[DIR_RIGHT]);
+	resFree(kidGfx.running[DIR_LEFT]);
+	resFree(kidGfx.running[DIR_RIGHT]);
+	resFree(kidGfx.startrunning[DIR_LEFT]);
+	resFree(kidGfx.startrunning[DIR_RIGHT]);
+	resFree(kidGfx.turnrunning[DIR_LEFT]);
+	resFree(kidGfx.turnrunning[DIR_RIGHT]);
+	resFree(kidGfx.jumprunning[DIR_LEFT]);
+	resFree(kidGfx.jumprunning[DIR_RIGHT]);
+}
+
 /* TODO: send this function to maps.c */
 tKid kidCreate() {
 	tKid kid;
+
+	if (kidGfx.turning[0]==NULL) loadGfx();
+
 	kid.location=100;
 	kid.floor=0;
 	kid.direction=DIR_LEFT;
 	kid.frame=0;
-	if (kidGfx.turning[0]==NULL) loadGfx();
 	kid.action=kidGfx.normal[DIR_LEFT];
 	kid.nextAction=stay;
 	kid.velocity=0;
@@ -111,7 +138,7 @@ int kidMove(tKid* kid,tKey key/*,tRoom room*/) {
 		 *
 		 * Note: falling and climbing context are ignored
 		 */
-		printf("next action=%d direction=%d ",kid->nextAction,kid->direction);
+		printf("next action=%d direction=%d keystatus=%x ",kid->nextAction,kid->direction,key.status);
 
 		/* STAY events
 		 */
@@ -147,10 +174,6 @@ int kidMove(tKid* kid,tKey key/*,tRoom room*/) {
 				/* jump */
 				kid->action=kidGfx.jumping[kid->direction];
 				kid->velocity=0;
-			} else if (!key.status) {
-					/* normal */
-					kid->velocity=0;
-					kid->action=kidGfx.normal[kid->direction];
 			} else if (key.status&K_Left) {
 				if (kid->direction==DIR_LEFT) {
 					/* run left */
@@ -175,6 +198,10 @@ int kidMove(tKid* kid,tKey key/*,tRoom room*/) {
 					kid->velocity=0;
 					kid->direction=DIR_RIGHT;
 				}
+			} else {
+				/* normal */
+				kid->velocity=0;
+				kid->action=kidGfx.normal[kid->direction];
 			}
 			/* TODO: elseif for the K_Left and K_Right to start jumping. Change nextAction */
 		} else if (kid->nextAction==run) {
@@ -208,9 +235,8 @@ int kidMove(tKid* kid,tKey key/*,tRoom room*/) {
 				}
 			} else if (!key.status) {
 				/* Stop running */
-				/* for the moment is normal */
-				kid->velocity=0;
-				kid->action=kidGfx.normal[kid->direction];
+				kid->velocity=kid->velocity/3;
+				kid->action=kidGfx.stoprunning[kid->direction];
 				kid->nextAction=stay;
 			} else if ((key.status&K_Up)==K_Up) {
 				/* Jump running */
@@ -226,7 +252,9 @@ int kidMove(tKid* kid,tKey key/*,tRoom room*/) {
 		return 1;
 	} else {
 		/* TODO: tile traspassing and validations here
-		 * use roomStep(tRoom room,x,y) roomTouch(tRoom room,x,y) to activate tile events */
+		 * use roomStep(tRoom room,x,y) roomTouch(tRoom room,x,y) to activate tile events
+		 * use int roomKidValid(tRoom room,tKid kid) to validate kid position
+		 */
 		kid->location+=kid->velocity;
 		return 0;
 	}
diff --git a/FP/src/out/output.c b/FP/src/out/output.c
index c16fde4..13adffb 100644
--- a/FP/src/out/output.c
+++ b/FP/src/out/output.c
@@ -148,7 +148,9 @@ outputLoadBitmap(const unsigned char* data, int size,
 }
 
 /* Frees the abstract object created by the loadBitmap function */
-void outputFreeBitmap(void* image) {}
+void outputFreeBitmap(void* image) {
+	free(image); /* TODO: I think some SDL stuff should be here */
+}
 
 /* Graphics: Primitives for the kernel */
 void outputDrawBitmap(void* image, int x, int y) {
diff --git a/FP/src/res/resources.c b/FP/src/res/resources.c
index 9e529fe..45a909d 100644
--- a/FP/src/res/resources.c
+++ b/FP/src/res/resources.c
@@ -252,3 +252,11 @@ tData* resLoad(long id) {
 	return result;
 }
 
+void resFree(tData* res) {
+	if (res->type==eImages) {
+		while (res->frames) {
+			outputFreeBitmap(res->pFrames[--(res->frames)]);
+		}
+	}
+	free(res);
+}