screen rotation
This commit is contained in:
parent
4036e380eb
commit
916c80435b
4 changed files with 59 additions and 10 deletions
31
game.c
31
game.c
|
@ -17,7 +17,13 @@
|
|||
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,
|
||||
g->color * ((face%2)+1) / 4);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -38,6 +44,24 @@ get_circle_vertice(int angle, int dist) { //{{{
|
|||
|
||||
return p;
|
||||
} //}}}
|
||||
|
||||
SDL_Point
|
||||
to_screen_coords(struct s_game *g, SDL_Point p) {
|
||||
SDL_Point q;
|
||||
|
||||
p.x -= SCREEN_WIDTH/2;
|
||||
p.y -= SCREEN_HEIGHT/2;
|
||||
float angle = TO_RAD(g->general_rotation);
|
||||
|
||||
q.x = p.x * cos(angle) - p.y * sin(angle);
|
||||
q.y = p.x * sin(angle) + p.y * cos(angle);
|
||||
|
||||
q.x += SCREEN_WIDTH/2;
|
||||
q.y += SCREEN_HEIGHT/2;
|
||||
|
||||
return q;
|
||||
}
|
||||
|
||||
void
|
||||
draw_cursor(struct s_game *g) { //{{{
|
||||
SDL_Point p1, p2, p3;
|
||||
|
@ -46,7 +70,9 @@ draw_cursor(struct s_game *g) { //{{{
|
|||
p2 = get_circle_vertice(g->cursor_angle-TO_DEG(8), CURSOR_DIST-10);
|
||||
p3 = get_circle_vertice(g->cursor_angle+TO_DEG(8), CURSOR_DIST-10);
|
||||
|
||||
SDL_Point pts[3] = {p1, p2, p3};
|
||||
SDL_Point pts[3] = {to_screen_coords(g, p1),
|
||||
to_screen_coords(g, p2),
|
||||
to_screen_coords(g, p3)};
|
||||
|
||||
SDL_FillPolygon(g->screen, pts, 3, g->color);
|
||||
} //}}}
|
||||
|
@ -65,12 +91,12 @@ void
|
|||
|
||||
void
|
||||
draw_game(struct s_game *g) { //{{{
|
||||
draw_background(g);
|
||||
draw_polygon(g);
|
||||
draw_obstacles(g);
|
||||
draw_cursor(g);
|
||||
} //}}}
|
||||
|
||||
|
||||
void
|
||||
get_keys(struct s_game *g) { //{{{
|
||||
SDL_PumpEvents();
|
||||
|
@ -92,6 +118,7 @@ void
|
|||
init_game(struct s_game *g) { //{{{
|
||||
g->polygon_type = SHAPE_HEXAGON;
|
||||
g->cursor_angle = TO_DEG(90);
|
||||
g->general_rotation = 0;
|
||||
g->color = 128;
|
||||
g->keys = SDL_GetKeyState(NULL);
|
||||
|
||||
|
|
8
game.h
8
game.h
|
@ -8,10 +8,11 @@
|
|||
#define GAME_H
|
||||
|
||||
#include "SDL/SDL.h"
|
||||
#include "SDL_prims.h"
|
||||
|
||||
#define SCREEN_WIDTH 640
|
||||
#define SCREEN_HEIGHT 480
|
||||
#define SCREEN_DIAGONAL (sqrt(SCREEN_HEIGHT*SCREEN_HEIGHT + SCREEN_WIDTH*SCREEN_WIDTH)/2)
|
||||
#define SCREEN_DIAGONAL (sqrt(SCREEN_HEIGHT*SCREEN_HEIGHT + SCREEN_WIDTH*SCREEN_WIDTH))
|
||||
|
||||
|
||||
#define POLYGON_SIZE 30
|
||||
|
@ -26,6 +27,7 @@
|
|||
|
||||
#define OFFSET_POINT(p) {p.x += SCREEN_WIDTH/2; p.y += SCREEN_HEIGHT/2;}
|
||||
#define TO_DEG(x) (x*ANGLE_MAX/360)
|
||||
#define TO_RAD(x) (x * 2 * M_PI / ANGLE_MAX)
|
||||
|
||||
|
||||
struct s_obstacle {
|
||||
|
@ -39,6 +41,7 @@ struct s_obstacle {
|
|||
struct s_game {
|
||||
#define CURSOR_POS_MAX 255
|
||||
int cursor_angle; //relative to the polygon center's referential
|
||||
int general_rotation;
|
||||
int polygon_type; //Shape
|
||||
int color;
|
||||
int actual_speed;
|
||||
|
@ -65,6 +68,9 @@ 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);
|
||||
|
||||
void
|
||||
draw_game(struct s_game *g);
|
||||
|
||||
|
|
15
main.c
15
main.c
|
@ -25,9 +25,12 @@ start:
|
|||
struct s_obstacle a = create_obstacle(SHAPE_THICK, 2, 1);
|
||||
add_obstacle(&ga, a);
|
||||
|
||||
|
||||
int rotation = 0;
|
||||
|
||||
while(1) {
|
||||
SDL_FillRect(ga.screen, NULL, 0);
|
||||
if(!(ga.counter % 50)) {
|
||||
if(!(ga.counter % 30)) {
|
||||
add_random_obstacle(&ga);
|
||||
}
|
||||
update_obstacles(&ga);
|
||||
|
@ -45,6 +48,16 @@ start:
|
|||
SDL_Delay(5);
|
||||
|
||||
ga.counter++;
|
||||
|
||||
if(rand()%30 == 0) {
|
||||
rotation += rand()%20 - 10;
|
||||
if(rotation > 10)
|
||||
rotation = 10;
|
||||
if(rotation < -10)
|
||||
rotation = -10;
|
||||
}
|
||||
ga.general_rotation += rotation;
|
||||
ga.general_rotation %= ANGLE_MAX;
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
|
15
polygons.c
15
polygons.c
|
@ -14,7 +14,7 @@
|
|||
#include "SDL/SDL.h"
|
||||
|
||||
SDL_Point
|
||||
get_hexagon_vertice(int faceno, int dist) { //{{{
|
||||
get_hexagon_vertice(struct s_game *g, int faceno, int dist) { //{{{
|
||||
SDL_Point p1;
|
||||
|
||||
p1.x = dist * cos((M_PI/3)*faceno);
|
||||
|
@ -28,12 +28,15 @@ 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);
|
||||
p1 = get_hexagon_vertice(g, faceno, dist);
|
||||
p2 = get_hexagon_vertice(g, faceno+1, dist);
|
||||
p3 = get_hexagon_vertice(g, faceno+1, dist+thick);
|
||||
p4 = get_hexagon_vertice(g, faceno, dist+thick);
|
||||
|
||||
SDL_Point points[4] = {p1, p2, p3, p4} ;
|
||||
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);
|
||||
}//}}}
|
||||
void
|
||||
|
|
Loading…
Reference in a new issue