Explorar el Código

Merge branch 'getticks'

Conflicts:
	hexagon.c
Adrien Boucaud hace 12 años
padre
commit
b7289fd90d
Se han modificado 4 ficheros con 29 adiciones y 27 borrados
  1. 20 14
      hexagon.c
  2. 3 9
      struct.h
  3. 5 3
      wall.c
  4. 1 1
      wall.h

+ 20 - 14
hexagon.c

@@ -11,20 +11,16 @@
 //duration of a frame
 #define FRAME_TIME 1/FPS
 
-//see struct.h
-Line lines[6] = {{0, NULL, 0},{1, NULL, 60},{2, NULL, 120},{3, NULL, 180},{4, NULL, 240},{5, NULL, 300}};
-
 //oriented angle between the player and the horizontal axis
 int player_angle=0;
 
-Camera cam = {64, 32, 0};
+Camera cam = {64, 32, 0, 0};
 
 //linked list of obstacles
 //at the moment, there is only a list, but at term, we should be using the lists from the Line struct. (And thus remove the "line" member from the Wall struct
 Wall *list = NULL;
 
 //delta angle: defines the 'speed' of the camera
-float dAngle = 10;
 
 //function prototypes
 //draws the player
@@ -32,8 +28,13 @@ void drawPlayer();
 //draws one of the lines (from 0 to 2)
 void drawDiagonal(int nb);
 
+void updateCamera(Camera *cam, unsigned int delta_time);
+
 int AddIn_main(int isAppli, unsigned short OptionNum)
 {
+    unsigned int start_time = RTC_GetTicks(); //1 tick == 1/128 second
+    unsigned int last_time = 0;
+    unsigned int current_time = RTC_GetTicks();
 	//variables for fps calculation
     unsigned int fps = 0, frame = 0, tempsOrigine = RTC_GetTicks();
 	//char string to display the fps
@@ -43,9 +44,10 @@ int AddIn_main(int isAppli, unsigned short OptionNum)
 
     //list = addWall(list, 128, 8, 1, 1);
 
-    while(KeyUp(K_EXIT)){
+    while(KeyUp(K_EXIT)){ //main loop
+    	last_time = current_time;
+	current_time = RTC_GetTicks();
         //fps
-        int current_frame_time = RTC_GetTicks();
         if(RTC_GetTicks() - tempsOrigine >= 32 )//if 1/4 seconds elapsed
         {
             fps = frame*4;
@@ -53,12 +55,8 @@ int AddIn_main(int isAppli, unsigned short OptionNum)
             tempsOrigine = RTC_GetTicks();
         }
         frame++;
-
-	//rotating the camera
-        cam.angle = (int) (cam.angle + dAngle);
-        if(cam.angle >= 360)cam.angle = cam.angle - 359;
         if(list != NULL){ //if the linked list is not empty
-            update(list); //update the linked list
+            update(list, current_time - last_time); //update the linked list
 
         if(isColliding(list, player_angle) == true) //if the player and a wall collide
      	{
@@ -111,9 +109,10 @@ int AddIn_main(int isAppli, unsigned short OptionNum)
             player_angle+=15;
         }
 
+	updateCamera(&cam, current_time - last_time);
 	//manually change the rotation speed
 	//TODO: automatize this
-        if(KeyDown(K_PLUS))
+/*        if(KeyDown(K_PLUS))
         {
             dAngle += 0.2;
         }
@@ -121,7 +120,7 @@ int AddIn_main(int isAppli, unsigned short OptionNum)
         {
             dAngle -= 0.2;
         }
-
+*/
 	//the angle must not be greater than 360
         if(player_angle < 0)
             player_angle = 360 + player_angle;
@@ -140,6 +139,13 @@ int AddIn_main(int isAppli, unsigned short OptionNum)
     return 1;
 }
 
+void updateCamera(Camera *cam, unsigned int delta_time){
+	cam->speed = 1; //to change
+	cam->angle += cam->speed * delta_time / 2.;
+
+	if(cam->angle >= 360)
+		cam->angle -= 359;
+}
 //draws the player
 //at first, was supposed to draw an hexagon in the center, plus a triangle to show the player,
 //but the hexagon was not visible, and it was a pixel mess, so we're showing a circle instead.

+ 3 - 9
struct.h

@@ -22,6 +22,8 @@ struct Camera{
     int cX;
     int cY;
     int angle;
+
+    float speed;
 };
 
 //a simple obstacle structure
@@ -31,19 +33,11 @@ struct Camera{
 //id is self explanatory
 //nxt is used by the linked list
 struct Wall{
-    int d;
+    float d;
     int h;
     int id;
     int line;
 
     Wall *nxt;
 };
-
-//a line. There are six lines (by default) in the game, numeroted from 0 to 5
-//each list has a list of obstacles and an angle
-struct Line{
-    int id;
-    Wall *list;
-    int angle;
-};
 #endif

+ 5 - 3
wall.c

@@ -44,7 +44,7 @@ Wall *removeWall(Wall *list, int d)
 	do{
 		if(tmp1->nxt != NULL)
 		{
-			if(tmp1->nxt->d <= 0)
+			if(tmp1->nxt->d <= 0.0)
 			{
 				tmp2 = tmp1->nxt;
 				if(tmp1->nxt->nxt != NULL)
@@ -59,8 +59,10 @@ Wall *removeWall(Wall *list, int d)
 	return list;
 }
 
-void update(Wall *list)
+void update(Wall *list, unsigned int delta_time)
 {
+	//we want to move the obstacle by 1 every two ticks (1/64 seconds ~= 1/60)
+	//
 	Wall *tmp;
 	tmp = list;
 
@@ -68,7 +70,7 @@ void update(Wall *list)
 		if(tmp != NULL)
 		{
 			//just reducing the distance from the center
-			tmp->d-=1;
+			tmp->d -= 0.5 * delta_time;
 		}
 		tmp = tmp->nxt;
 	}while(tmp != NULL);

+ 1 - 1
wall.h

@@ -13,7 +13,7 @@ Wall *removeWall(Wall *list, int d); //returns a new pointer to the first elemen
 //show the ll "list"
 void show(Wall *list, Camera *cam);
 //updates the ll "list"
-void update(Wall *list);
+void update(Wall *list, unsigned int delta_time);
 //simple collision test. Returns true if a Wall from the list collides with the player
 bool isColliding(Wall *list, int player_angle);
 #endif