git » fp-git.git » commit 8fffd30

abstracted implementation and fixed q&d code

author ecalot
2004-10-22 18:27:21 UTC
committer ecalot
2004-10-22 18:27:21 UTC
parent a656c353c34ee21b31031e5d9211e206d3d94022

abstracted implementation and fixed q&d code

FP/src/conf/states/main.conf +4 -2
FP/src/include/states.h +5 -3
FP/src/include/types.h +8 -5
FP/src/ker/kid.c +10 -11
FP/src/ker/object.c +10 -11
FP/src/ker/states.c +48 -37

diff --git a/FP/src/conf/states/main.conf b/FP/src/conf/states/main.conf
index 6231920..12894dc 100644
--- a/FP/src/conf/states/main.conf
+++ b/FP/src/conf/states/main.conf
@@ -38,7 +38,9 @@ normal:
 		steps
 			relative 10
 		animation
-			134-145	
+			134-135 p
+			136-142
+			143-145	p
 	action            Turn
 		conditions
 			KeyBack                pressed
@@ -47,7 +49,7 @@ normal:
 		steps
 			relativeTurn 0
 		animation
-			45-52
+			45-52 p
 	action            Jump
 		conditions
 			KeyUp                  pressed
diff --git a/FP/src/include/states.h b/FP/src/include/states.h
index c0faf79..a452b8f 100644
--- a/FP/src/include/states.h
+++ b/FP/src/include/states.h
@@ -8,7 +8,7 @@
 
 typedef enum {esLast=0,esKeyUp,esKeyDown,esKeyForward,esKeyBack,esKeyShift,esMapUp,esMapDown,esMapForward,esMapBack,esMapOn,esForwardTileNearerThan,esForwardTileFartherThan,esScreenUp,esScreenDown,esScreenLeft,esScreenRight,esInScreen,esInLevel,esForwardChangeToScreen,esInFloorTop,esInfloorMiddle,esInfloorBottom}tsConditionType;
 
-
+								
 typedef enum {esDangerous,esNone,esWalk,esPotion,esSword,esMirror,esNotApplicable=0}tsTileType;
 typedef enum {esRelative,esForwardTile,esRelativeTurn}tsMoveType; /*  */
 
@@ -26,11 +26,13 @@ typedef struct {
 	short        animSize; /* number of frames to be shown */
 } tsAction;
 
+#define stateGetImage(a) (((a).action.image))
+
 /* public functions interface */
 
-/* This function should return the image frame
+/* This function should return the frame flags
  * and actions to be performed by this call */
-int stateUpdate(tKey* key, tKid* kid,tRoom* room,short* flags);
+short stateUpdate(tKey* key, tKid* kid,tRoom* room);
 
 /* Create a State depending on the level */
 tState createState(int level);
diff --git a/FP/src/include/types.h b/FP/src/include/types.h
index 3577ba5..74b3f12 100644
--- a/FP/src/include/types.h
+++ b/FP/src/include/types.h
@@ -46,10 +46,13 @@ typedef enum {eDrop,eRaise}tPressableType;
 
 typedef struct { /* The state object: only struct that is not static to the state class */
 	short  frame; /* when zero, animation is released and the next state is taken */
-	short* animation;
-	short* animOffset;
-	short* flags;
-	short  currentState;
+	short* animation; /* list of frames for this state */
+	short* animOffset; /* position of those frames */
+	short* flags;      /* actions to be performed by each frame (make sound, press floor, etc) */
+	short  currentState; /* the Id of the state the kid is */
+	short  image; /* the next image to be shown */
+	float  step; /* the number of pixels a frame has to move */
+	float  acumLocation; /* the kid location in float (will be casted to int in the kid object) */
 } tState;
 
 typedef struct {
@@ -71,7 +74,7 @@ typedef struct {
 } tPressable;
 
 typedef struct {
-	tRoomId        links[4*24];
+	tRoomId        links[ 4*24];
 	unsigned char  fore [24*30];
 	unsigned char  back [24*30];
 	tGate**        screenGates[24];
diff --git a/FP/src/ker/kid.c b/FP/src/ker/kid.c
index 8c5e270..3223532 100644
--- a/FP/src/ker/kid.c
+++ b/FP/src/ker/kid.c
@@ -119,9 +119,9 @@ tKid kidCreate() {
 
 	if (kidGfx.kid[0]==NULL) loadGfx();
 
-	kid.location=100;
-	kid.floor=0;
-	kid.direction=DIR_LEFT;
+	kid.location=30;
+	kid.floor=1;
+	kid.direction=DIR_RIGHT;
 /*	kid.frame=0;
 	kid.action=kidGfx.normal[DIR_LEFT];
 	kid.nextAction=stay;
@@ -132,17 +132,16 @@ tKid kidCreate() {
 }
 
 void kidDraw(tKid kid) {
-	outputDrawBitmap(kid.frame,kid.location,58+kid.floor*TILE_H); /* TODO: change location value in a lower layer*/
+	outputDrawBitmap(
+		kidGfx.kid[kid.direction]->pFrames[stateGetImage(kid)-1], /* TODO: move this -1 to each script frame */
+		(kid.location*32)/10,
+		58+kid.floor*TILE_H
+	);
 }
 
 int kidMove(tKid* kid,tKey key,tRoom* room) {
 #ifdef NEW_KERNEL
-	short flags;
-	int index=stateUpdate(&key,kid,room,&flags)-1;
-	printf("index=%d\n",index);
-	kid->frame=kidGfx.kid[kid->direction]->pFrames[index];
-	/* TODO: stateUpdate should return flags and edit kid->frame by it's own */
-	printf("flags=%x (%d)\n",flags,flags);
+	return stateUpdate(&key,kid,room);
 #else
 	int result;
 	tTile tile;
@@ -296,7 +295,7 @@ int kidMove(tKid* kid,tKey key,tRoom* room) {
 		*room=mapGetRoom((void*)(room->level),room->links[eDown]);
 		kid->floor=0;
 	}
+	return result;
 #endif
-	return 1/*result*/;
 }
 
diff --git a/FP/src/ker/object.c b/FP/src/ker/object.c
index 8c5e270..3223532 100644
--- a/FP/src/ker/object.c
+++ b/FP/src/ker/object.c
@@ -119,9 +119,9 @@ tKid kidCreate() {
 
 	if (kidGfx.kid[0]==NULL) loadGfx();
 
-	kid.location=100;
-	kid.floor=0;
-	kid.direction=DIR_LEFT;
+	kid.location=30;
+	kid.floor=1;
+	kid.direction=DIR_RIGHT;
 /*	kid.frame=0;
 	kid.action=kidGfx.normal[DIR_LEFT];
 	kid.nextAction=stay;
@@ -132,17 +132,16 @@ tKid kidCreate() {
 }
 
 void kidDraw(tKid kid) {
-	outputDrawBitmap(kid.frame,kid.location,58+kid.floor*TILE_H); /* TODO: change location value in a lower layer*/
+	outputDrawBitmap(
+		kidGfx.kid[kid.direction]->pFrames[stateGetImage(kid)-1], /* TODO: move this -1 to each script frame */
+		(kid.location*32)/10,
+		58+kid.floor*TILE_H
+	);
 }
 
 int kidMove(tKid* kid,tKey key,tRoom* room) {
 #ifdef NEW_KERNEL
-	short flags;
-	int index=stateUpdate(&key,kid,room,&flags)-1;
-	printf("index=%d\n",index);
-	kid->frame=kidGfx.kid[kid->direction]->pFrames[index];
-	/* TODO: stateUpdate should return flags and edit kid->frame by it's own */
-	printf("flags=%x (%d)\n",flags,flags);
+	return stateUpdate(&key,kid,room);
 #else
 	int result;
 	tTile tile;
@@ -296,7 +295,7 @@ int kidMove(tKid* kid,tKey key,tRoom* room) {
 		*room=mapGetRoom((void*)(room->level),room->links[eDown]);
 		kid->floor=0;
 	}
+	return result;
 #endif
-	return 1/*result*/;
 }
 
diff --git a/FP/src/ker/states.c b/FP/src/ker/states.c
index 7a2521a..8b108f7 100644
--- a/FP/src/ker/states.c
+++ b/FP/src/ker/states.c
@@ -4,38 +4,45 @@
 #include <stdio.h> /* For debug purposes */
 #include "kid.h" /* DIR_LEFT DIR_RIGHT */
 
+void debugShowFlag(short optionflag) {
+	if (optionflag&STATES_FLAG_U) printf("ScreenUp ");
+	if (optionflag&STATES_FLAG_H) printf("ScreenLeft ");
+	if (optionflag&STATES_FLAG_J) printf("ScreenRight ");
+	if (optionflag&STATES_FLAG_N) printf("ScreenDown ");
+	if (optionflag&STATES_FLAG_P) printf("PressFloor ");
+	if (optionflag&STATES_FLAG_C) printf("PressCeiling ");
+	if (optionflag&STATES_FLAG_S) printf("Sound");
+	printf("\n");
+}
+
+#define STATES_STEPS_PER_TILE 32 
 /* Private static state graph */
 static tsAction statesActionList[]=STATES_ACTIONS;
 static short statesAnimationList[]=STATES_ANIMATIONS;
 static tsCondition statesConditionList[]=STATES_CONDITIONS;
 
-short* stateGetAnimation(int action,short *frames,short** flags,float* offsets) {
-	short* result;
+void stateGetAnimation(int action,tState *state/*short *frames,short** flags,float* offsets*/) {
 	tsAction* a=statesActionList+action;
 	short i=a->animSize;
 	short* j=statesAnimationList+(a->animStart*2);
 	short totaloffset=a->moveOffset;
 	/* TODO: depending on relative and absolute crop the middle frames */
-	*frames=i;
-	result=(short*)malloc(sizeof(short)*i);
-	*flags =(short*)malloc(sizeof(short)*i);
-	printf("animsize=%d animstart=%d\n",i,a->animStart);
-	*offsets=(float)(totaloffset)/(float)(*frames); /* the second short is the flag */
+	state->frame=i;
+	state->animation=(short*)malloc(sizeof(short)*i);
+	state->flags=(short*)malloc(sizeof(short)*i);
+	printf("* Animsize=%d Animstart=%d. (new animation allocated) Next:\n",i,a->animStart);
+	state->step=(float)(totaloffset)/(float)(i); /* the first short is the frame */
 	while (i--) {
-		printf ("copiando animacion=%d ",result[i]=*j);
-		j++;
-		printf("flag=%d j=%p\n",((*flags)[i])=*j,(void*)j); /* the second short is the flag */
-		j++;
+		(state->animation)[i]=*(j++);
+		((state->flags)[i])=*(j++); /* the second short is the flag */
 	}
-	return result;
 }
 
 /* public functions interface */
 tState createState(int level) {
 	tState start;
 	static short statesLevelList[]=STATES_LEVELS;
-	float step;
-	start.animation=stateGetAnimation(statesLevelList[level],&(start.frame),&(start.flags),&step);
+	stateGetAnimation(statesLevelList[level],&start);
 	start.currentState=statesActionList[statesLevelList[level]].nextStateId;
 	return start;
 }
@@ -45,8 +52,8 @@ tState createState(int level) {
 /* Evaluates a condition indexed in the condition table */
 int evaluateCondition(int condition,tKey* key, tKid* kid, tRoom* room) {
 	tsCondition c=statesConditionList[condition];
-	int thisTile=kid->location/32+1+12*kid->floor;
-	int whereInTile=(kid->direction==DIR_LEFT)?(kid->location%32):32-(kid->location%32);
+	int thisTile=kid->location/STATES_STEPS_PER_TILE+1+12*kid->floor;
+	int whereInTile=(kid->direction==DIR_LEFT)?(kid->location%STATES_STEPS_PER_TILE):STATES_STEPS_PER_TILE-(kid->location%STATES_STEPS_PER_TILE);
 	switch(c.type) {
 	case esKeyUp:
 		return inputGetUp(key->status)? /* TODO: argument notPressed isn't supported */
@@ -119,18 +126,22 @@ int evaluateState(int state, tKey* key, tKid* kid, tRoom* room) {
 
 /* This function should return the image frame and actions to be performed by this call
  * returns the animation number corresponding to this frame */
-int stateUpdate(tKey* key, tKid* kid,tRoom* room,short* flags) {
+short stateUpdate(tKey* key, tKid* kid,tRoom* room) {
 	tState* current=&(kid->action);
-	static float step;
-	static float acumLocation;
-	int imageFrame;
-
+	/*static float step;
+	static float acumLocation;*/
+	short flags;
+	
 	current->frame--;
 	
-	imageFrame    =current->animation[current->frame];
-	*flags        =current->flags    [current->frame];
-	printf("stateUpdate: animation=%d flags=%d\n",imageFrame,*flags);
-	/*printf("currentframe=%d\n",current->frame)*/
+	current->image=current->animation[current->frame];
+	flags         =current->flags    [current->frame];
+	
+	/* BEGIN DEBUG */
+	printf("stateUpdate: animation=%d ",current->image);
+	debugShowFlag(flags);
+	/* END DEBUG */
+	
 	if (!current->frame) {
 		int action;
 		free(current->animation);
@@ -140,43 +151,43 @@ int stateUpdate(tKey* key, tKid* kid,tRoom* room,short* flags) {
 
 		/* Performs the events called by this action */
 			/* Remember the animation and flags for the next current->frame frames */
-		current->animation=stateGetAnimation(action,&(current->frame),&(current->flags),&step);
+		stateGetAnimation(action,current);
 			/* Remember the state where we are now */
 		current->currentState=statesActionList[action].nextStateId;
 			/* Move the kid (turn+traslate) */
 		if (kid->direction==DIR_LEFT) {
-			step=-step;
+			current->step=-current->step;
 			switch(statesActionList[action].moveType) {
 			case STATES_MOVETYPES_RELATIVE:
-				kid->location-=statesActionList[action].moveOffset;
+				/*kid->location-=statesActionList[action].moveOffset;*/
 				break;
 			case STATES_MOVETYPES_ABSOLUTEFORWARD:
-				kid->location=kid->location-(kid->location%32);
+				kid->location=kid->location-(kid->location%STATES_STEPS_PER_TILE);
 				break;
 			case STATES_MOVETYPES_RELATIVETURN:
-				kid->location-=statesActionList[action].moveOffset;
+				/*kid->location-=statesActionList[action].moveOffset;*/
 				kid->direction=DIR_RIGHT;
 				break;
 			}
 		} else {
 			switch(statesActionList[action].moveType) {
 			case STATES_MOVETYPES_RELATIVE:
-				kid->location+=statesActionList[action].moveOffset;
+				/*kid->location+=statesActionList[action].moveOffset;*/
 				break;
 			case STATES_MOVETYPES_ABSOLUTEFORWARD:
-				kid->location=32+kid->location-(kid->location%32);
+				kid->location=STATES_STEPS_PER_TILE+kid->location-(kid->location%STATES_STEPS_PER_TILE);
 				break;
 			case STATES_MOVETYPES_RELATIVETURN:
-				kid->location+=statesActionList[action].moveOffset;
+				/*kid->location+=statesActionList[action].moveOffset;*/
 				kid->direction=DIR_LEFT;
 				break;
 			}
 		}
-		acumLocation=kid->location;
+		current->acumLocation=kid->location;
 	}
-	acumLocation+=step;
-	kid->location=acumLocation;
-	return imageFrame;
+	current->acumLocation+=current->step;
+	kid->location=current->acumLocation;
+	return flags;
 }