diff --git a/game.c b/game.c index 24c97e4..dd2e8a3 100644 --- a/game.c +++ b/game.c @@ -58,13 +58,10 @@ change_game_color(struct s_game *g) { void draw_background(struct s_game *g) { //{{{ int face; - void (*drawer)(struct s_game *, int faceno, int dist, int thick, int col); - - drawer = get_polygon_drawer(g); for(face = 0; face < g->polygon_type; face++) { - drawer(g, face, POLYGON_SIZE, SCREEN_DIAGONAL, - divide_color(g->color, 2 + 2*(face%2))); + draw_polygon_side(g, face, POLYGON_SIZE, SCREEN_DIAGONAL, + divide_color(g->color, 2 + 2*(face%2))); } } //}}} @@ -120,22 +117,6 @@ draw_cursor(struct s_game *g) { //{{{ SDL_FillPolygon(g->screen, pts, 3, g->color); } //}}} - -void -(*get_polygon_drawer(struct s_game *g)) -(struct s_game *, int faceno, int dist, int thick, int col) { //{{{ - switch(g->polygon_type) { - case SHAPE_HEXAGON: - return draw_hexagon_side; - case SHAPE_SQUARE: - return draw_square_side; - case SHAPE_TRIANGLE: - return draw_triangle_side; - default: - return NULL; - } -} //}}} - void draw_game(struct s_game *g) { //{{{ draw_background(g); diff --git a/game.h b/game.h index de654f6..96db1e7 100644 --- a/game.h +++ b/game.h @@ -83,11 +83,6 @@ init_game(struct s_game *g); int get_face_from_cursor(struct s_game *g); -//ugly ! -void -(*get_polygon_drawer(struct s_game *g)) -(struct s_game *, int faceno, int dist, int thick, int col); - SDL_Point to_screen_coords(struct s_game *g, SDL_Point p); diff --git a/obs.c b/obs.c index e147869..e35bf83 100644 --- a/obs.c +++ b/obs.c @@ -8,6 +8,7 @@ #include #include "game.h" #include "col.h" +#include "polygons.h" #include void @@ -63,13 +64,10 @@ draw_obstacles(struct s_game *g) { //{{{ int i; - void(*drawer)(struct s_game *, int faceno, int dist, int thick, int col); - drawer = get_polygon_drawer(g); - for(i = 0; i < g->num_obs; i++) { if(g->obs[i].used) { struct s_obstacle o = g->obs[i]; - drawer(g, o.face, o.distance, o.thick, g->color); + draw_polygon_side(g, o.face, o.distance, o.thick, g->color); } } } //}}} diff --git a/polygons.c b/polygons.c index 1ff8147..4db8863 100644 --- a/polygons.c +++ b/polygons.c @@ -13,6 +13,9 @@ #include "SDL_prims.h" #include "SDL/SDL.h" + + + SDL_Point get_hexagon_vertice(int faceno, int dist) { //{{{ SDL_Point p1; @@ -24,22 +27,6 @@ get_hexagon_vertice(int faceno, int dist) { //{{{ return p1; } //}}} -void -draw_hexagon_side(struct s_game *g, int faceno, int dist, int thick, int col) { //{{{ - SDL_Point p1, p2, p3, p4; - - p1 = get_hexagon_vertice(faceno, dist); - p2 = get_hexagon_vertice(faceno+1, dist); - p3 = get_hexagon_vertice(faceno+1, dist+thick); - p4 = get_hexagon_vertice(faceno, dist+thick); - - SDL_Point points[4] = {to_screen_coords(g, p1), - to_screen_coords(g, p2), - to_screen_coords(g, p3), - to_screen_coords(g, p4)} ; - SDL_FillPolygon(g->screen, points, 4, col); -}//}}} - SDL_Point get_square_vertice(int faceno, int dist) { //{{{ SDL_Point p1; @@ -51,22 +38,6 @@ get_square_vertice(int faceno, int dist) { //{{{ return p1; } //}}} -void -draw_square_side(struct s_game *g, int faceno, int dist, int thick, int col) { //{{{ - SDL_Point p1, p2, p3, p4; - - p1 = get_square_vertice(faceno, dist); - p2 = get_square_vertice(faceno+1, dist); - p3 = get_square_vertice(faceno+1, dist+thick); - p4 = get_square_vertice(faceno, dist+thick); - - SDL_Point points[4] = {to_screen_coords(g, p1), - to_screen_coords(g, p2), - to_screen_coords(g, p3), - to_screen_coords(g, p4)} ; - SDL_FillPolygon(g->screen, points, 4, col); -}//}}} - SDL_Point get_triangle_vertice(int faceno, int dist) { //{{{ SDL_Point p1; @@ -78,14 +49,34 @@ get_triangle_vertice(int faceno, int dist) { //{{{ return p1; } //}}} + void -draw_triangle_side(struct s_game *g, int faceno, int dist, int thick, int col) { //{{{ +draw_polygon_side(struct s_game *g, int faceno, int dist, int thick, int col) { //{{{ + + SDL_Point (*get_poly_vertice)(int faceno, int dist); SDL_Point p1, p2, p3, p4; - p1 = get_triangle_vertice(faceno, dist); - p2 = get_triangle_vertice(faceno+1, dist); - p3 = get_triangle_vertice(faceno+1, dist+thick); - p4 = get_triangle_vertice(faceno, dist+thick); + /* Depending on the polygon */ + switch(g->polygon_type) { + case SHAPE_HEXAGON: + get_poly_vertice = get_hexagon_vertice; + break; + case SHAPE_SQUARE: + get_poly_vertice = get_square_vertice; + break; + case SHAPE_TRIANGLE: + get_poly_vertice = get_triangle_vertice; + break; + default: + fprintf(stderr, "Unknown SHAPE @ %s", __FILE__); + exit(EXIT_FAILURE); + } + + + p1 = get_poly_vertice(faceno, dist); + p2 = get_poly_vertice(faceno+1, dist); + p3 = get_poly_vertice(faceno+1, dist+thick); + p4 = get_poly_vertice(faceno, dist+thick); SDL_Point points[4] = {to_screen_coords(g, p1), to_screen_coords(g, p2), @@ -98,16 +89,13 @@ void draw_polygon(struct s_game *g) { //{{{ int faces; - void (*drawer)(struct s_game *, int faceno, int dist, int thick, int col); - drawer = get_polygon_drawer(g); - for(faces = 0; faces < SHAPE_HEXAGON; faces++) { - drawer(g, faces, 0, POLYGON_SIZE, 0); + draw_polygon_side(g, faces, 0, POLYGON_SIZE, 0); } for(faces = 0; faces < SHAPE_HEXAGON; faces++) { - drawer(g, faces, POLYGON_SIZE, POLYGON_THICK, g->color); + draw_polygon_side(g, faces, POLYGON_SIZE, POLYGON_THICK, g->color); } } //}}} diff --git a/polygons.h b/polygons.h index 2c19393..bf57ad5 100644 --- a/polygons.h +++ b/polygons.h @@ -11,13 +11,7 @@ void draw_polygon(struct s_game *g); void -draw_hexagon_side(struct s_game *g, int faceno, int dist, int thick, int col); - -void -draw_square_side(struct s_game *g, int faceno, int dist, int thick, int col); - -void -draw_triangle_side(struct s_game *g, int faceno, int dist, int thick, int col); +draw_polygon_side(struct s_game *g, int faceno, int dist, int thick, int col); #endif /* ndef POLYGONS_H */