/* ** game.c - The game ** ** 2014 - Frank Villaro-Dixon */ #include #include #include #include "game.h" #include "polygons.h" #include "obs.h" #include "SDL_prims.h" #include "SDL/SDL.h" union color { struct { unsigned char col[4]; } rgb; int col; }; int divide_color(int color, int divider) { union color col; col.col = color; for(int i = 0; i < 4; i++) { col.rgb.col[i] /= divider; } return col.col; } void change_game_color(struct s_game *g) { static int from_canal = 0; static int to_canal = 1; union color col; col.col = g->color; printf("%x - %d %d %d\n", col.col, col.rgb.col[0], col.rgb.col[1], col.rgb.col[2]); col.rgb.col[from_canal]--; col.rgb.col[to_canal]++; if(col.rgb.col[from_canal] > 255) { from_canal = to_canal; to_canal = (to_canal + rand()%2)%3; } g->color = col.col; puts("changed"); } 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))); } } //}}} int get_face_from_cursor(struct s_game *g) { return g->cursor_angle * g->polygon_type / ANGLE_MAX; } SDL_Point get_circle_vertice(int angle, int dist) { //{{{ SDL_Point p; p.x = cos((double)angle*2*M_PI/ANGLE_MAX) * dist; p.y = sin((double)angle*2*M_PI/ANGLE_MAX) * dist; OFFSET_POINT(p); 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 *= g->sexual_pulsation; // q.y *= g->sexual_pulsation; q.x += SCREEN_WIDTH/2; q.y += SCREEN_HEIGHT/2; return q; }//}}} void draw_cursor(struct s_game *g) { //{{{ SDL_Point p1, p2, p3; p1 = get_circle_vertice(g->cursor_angle, CURSOR_DIST); 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] = {to_screen_coords(g, p1), to_screen_coords(g, p2), to_screen_coords(g, p3)}; 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; default: return NULL; } } //}}} void draw_game(struct s_game *g) { //{{{ draw_background(g); draw_obstacles(g); draw_polygon(g); draw_cursor(g); } //}}} void get_keys(struct s_game *g) { //{{{ SDL_PumpEvents(); if(g->keys[SDL_QUIT]) exit(0); if(g->keys[SDLK_q]) exit(0); if(g->keys[SDLK_LEFT]) g->cursor_angle -= TO_DEG(CURSOR_DEG_FRAME); if(g->keys[SDLK_RIGHT]) g->cursor_angle += TO_DEG(CURSOR_DEG_FRAME); g->cursor_angle %= ANGLE_MAX; if(g->cursor_angle < 0) g->cursor_angle += ANGLE_MAX; } //}}} void init_game(struct s_game *g) { //{{{ g->polygon_type = SHAPE_HEXAGON; g->cursor_angle = TO_DEG(90); g->general_rotation = 0; g->sexual_pulsation = 1; g->color = 128; g->keys = SDL_GetKeyState(NULL); g->num_obs = NUM_OBS; g->counter = 0; init_obs(g); } //}}}