Kaynağa Gözat

All drawing funtions take the number of lines into account. Don't set nb_lines over 32.

Adrien Boucaud 12 yıl önce
ebeveyn
işleme
91b102f5a3
6 değiştirilmiş dosya ile 38 ekleme ve 54 silme
  1. 24 40
      draw_states.c
  2. 1 1
      draw_states.h
  3. 1 1
      struct.h
  4. 3 3
      update_states.c
  5. 7 7
      wall.c
  6. 2 2
      wall.h

+ 24 - 40
draw_states.c

@@ -3,12 +3,10 @@ void draw_game(Game_Data *data)
 {
 	//draw the player and the lines
     drawPlayer(&(data->cam), data->player_angle, data->nb_lines);
-    drawDiagonal(1, data->cam);
-    drawDiagonal(2, data->cam);
-    drawDiagonal(3, data->cam);
-	//showing the walls
+    drawDiagonals(data->cam, data->nb_lines);
+    	//showing the walls
     if(data->list != NULL)
-        drawWalls(data->list, &(data->cam));
+        drawWalls(data->list, &(data->cam), data->nb_lines);
 }
 void draw_title(Game_Data *data)
 {
@@ -29,11 +27,11 @@ void draw_game_over(Game_Data *data)
 //there is still for code to calculate the vertices of the hexagon, in case we want to change that again
 void drawPlayer(Camera *cam, int player_angle, int nb_lines)
 {
-    int x[6];
-    int y[6];
+    int x[32];
+    int y[32];
     int i = 0;
     int angle = 0;
-    for(i = 0; i<6; ++i)
+    for(i = 0; i < nb_lines; ++i)
     {
         angle = i * 360 / nb_lines;
         x[i] = (8. + cam->zoom)*cos(PI * (angle + cam->angle)/180.) + cam->cX;
@@ -42,45 +40,31 @@ void drawPlayer(Camera *cam, int player_angle, int nb_lines)
 
 	//draw the aforementionned circle, depending on the camera's center
     //ML_filled_circle(cam.cX, cam.cY, 6, BLACK);
-	ML_polygone(x, y, 6, BLACK);
+	ML_polygone(x, y, nb_lines, BLACK);
 	//draw the player. At such a low scale, it was impossible to draw a rotating triangle, so its a radius 1 circle instead.
     ML_filled_circle((9. + cam->zoom)*cos( PI*(player_angle + cam->angle)/180) + cam->cX, (9. + cam->zoom)*sin( PI*(player_angle+cam->angle)/180) + cam->cY, 1, BLACK);
 
 }
 
 //draws one of the three rotating lines
-void drawDiagonal(int nb, Camera cam, int nb_lines)
+void drawDiagonals(Camera cam, int nb_lines)
 {
-    int tmp_angle = 0;
-    float coeff = 0;
-    int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
+	int tmp_angle = cam.angle;
+	float tmp_angle_rad = 0.0f;
+	int i = 0;
+	
+	float x1 = 0.0f, y1 = 0.0f, x2 = 0.0f, y2 = 0.0f;
 
-	//defining the angle between the line and the horizontal axis
-    if(nb == 1)
-        tmp_angle = cam.angle + 60;
-    else if(nb == 2)
-        tmp_angle = cam.angle;
-    else if(nb == 3)
-        tmp_angle = 300 + cam.angle;
-    if(tmp_angle >= 360)tmp_angle = tmp_angle - 359;
+	do{	
+		tmp_angle_rad = tmp_angle * PI / 180.0f;
+		x1 = 9.0f * cos(tmp_angle_rad);
+		y1 = 9.0f * sin(tmp_angle_rad);
+		x2 = 64.0f * cos(tmp_angle_rad);
+		y2 = 64.0f * sin(tmp_angle_rad);
+		ML_line(x1 + cam.cX, y1 + cam.cY, x2 + cam.cX, y2 + cam.cY, BLACK);
 
-	//defining the slope coefficient
-    coeff = sin(PI*tmp_angle / 180)/cos(PI * tmp_angle / 180);
-
-	//ML needs four coordinates in order to draw a line
-    x1 = -32/coeff;
-    y1 = -32;
-
-    x2 = 32/coeff;
-    y2 = 32;
-
-    if(abs(x1) > 64){
-        x1 = -64;
-        y1 = -64*coeff;
-
-        x2 = 64;
-        y2 = 64*coeff;
-    }
-	//drawing the line
-        ML_line(x1 + cam.cX, y1 + cam.cY, x2 + cam.cX, y2 + cam.cY, BLACK);
+		tmp_angle += (360 / nb_lines);
+		if(tmp_angle >= 360)tmp_angle = tmp_angle - 359;
+		i++;
+	}while(i < nb_lines);
 }

+ 1 - 1
draw_states.h

@@ -10,5 +10,5 @@ void draw_menu(Game_Data *data);
 void draw_game_over(Game_Data *data);
 
 void drawPlayer(Camera *cam, int player_angle, int nb_lines);
-void drawDiagonal(int nb, Camera cam, int nb_lines);
+void drawDiagonals(Camera cam, int nb_lines);
 #endif

+ 1 - 1
struct.h

@@ -4,7 +4,7 @@
 #include "stdlib.h"
 
 //constants
-#define FPS 60
+#define FPS 20
 #define FRAME_TIME 1/FPS
 #define PI 3.14159265
 #define SIN_60 0.866025404

+ 3 - 3
update_states.c

@@ -11,7 +11,7 @@ void update_game(Game_Data *data)
     if(data->list != NULL){ //if the linked list is not empty
             updateWalls(data->list, data->current_time - data->last_time); //update the linked list
 
-        if(isColliding(data->list, data->player_angle) == true){ //if the player and a wall collide
+        if(isColliding(data->list, data->player_angle, data->nb_lines) == true){ //if the player and a wall collide
             PrintMini(50, 0, "TOUCHE", MINI_OVER); //death handling
     	}
         data->list = removeWall(data->list, 0); //remove every wall whose distance to the center equals zero
@@ -54,5 +54,5 @@ void updateCamera(Camera *cam, unsigned int delta_time){
     cam->angle += cam->speed * delta_time / 2.;
 
     if(cam->angle >= 360)
-        cam->angle -= 359;
-}
+        cam->angle = cam->angle % 360;
+}

+ 7 - 7
wall.c

@@ -76,7 +76,7 @@ void updateWalls(Wall *list, unsigned int delta_time)
 	}while(tmp != NULL);
 }
 
-void drawWalls(Wall *list, Camera *cam)
+void drawWalls(Wall *list, Camera *cam, int nb_lines)
 {
 	Wall *tmp;
 	tmp = list;
@@ -85,12 +85,12 @@ void drawWalls(Wall *list, Camera *cam)
 		{
 			if(tmp->d + tmp->h< 64)
 			{
-
-				const float angle = PI * ((tmp->line)*60 +cam->angle) / 180;
+				const float angle =  ((360 / nb_lines) * tmp->line + cam->angle) * PI / 180;
 				const float cos1 = cos(angle);
-				const float cos2 = cos(angle + PI/3);
+				const float cos2 = cos(angle +  (360.0 / nb_lines) * (PI / 180));
 				const float sin1 = sin(angle);
-				const float sin2 = sin(angle + PI/3);
+				const float sin2 = sin(angle + (360.0 / nb_lines) * (PI / 180));
+
 				int i,j, x, y;
 
 				float dist = tmp->d - tmp->h + cam->zoom;
@@ -106,7 +106,7 @@ void drawWalls(Wall *list, Camera *cam)
 }
 
 //tests every Wall in the list
-bool isColliding(Wall *list, int player_angle)
+bool isColliding(Wall *list, int player_angle, int nb_lines)
 {
 	Wall *tmp;
 	tmp = list;
@@ -116,7 +116,7 @@ bool isColliding(Wall *list, int player_angle)
 		{
 			if(tmp-> d <= 8)//if the wall is close enough from the center of the screen
 			{	//and is on the same line than the player
-				if(tmp->line == (int)(player_angle/60)) //&& tmp->line * 60 + 60 > player_angle)
+				if(tmp->line == (int)(player_angle/ 360 / nb_lines))
 				{	//BOOM
 					return true;
 				}

+ 2 - 2
wall.h

@@ -10,9 +10,9 @@ Wall *addWall(Wall *list, int d, int h, int id, int line);//returns a new pointe
 Wall *removeWall(Wall *list, int d); //returns a new pointer to the first element
 
 //show the ll "list"
-void drawWalls(Wall *list, Camera *cam);
+void drawWalls(Wall *list, Camera *cam, int nb_lines);
 //updates the ll "list"
 void updateWalls(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);
+bool isColliding(Wall *list, int player_angle, int nb_lines);
 #endif