From e6d229ffb1aa84dc3a0ee6ac47d1fd2d9dc0a37a Mon Sep 17 00:00:00 2001 From: Frank Villaro-Dixon Date: Thu, 1 Jan 2015 04:34:12 +0100 Subject: [PATCH] aoauaou --- game.c | 74 ++++++++++++++++++++++++++++++++++++++++++---------------- game.h | 8 +++++-- main.c | 3 +++ 3 files changed, 63 insertions(+), 22 deletions(-) diff --git a/game.c b/game.c index 3578680..d9ab2a7 100644 --- a/game.c +++ b/game.c @@ -9,20 +9,19 @@ #include #include "game.h" #include "SDL_prims.h" +#include "SDL/SDL.h" SDL_Point -get_hexagon_vertice(int faceno, int dist) -{ +get_hexagon_vertice(int faceno, int dist) { //{{{ SDL_Point p1; p1.x = dist * cos((M_PI/3)*faceno); p1.y = dist * sin((M_PI/3)*faceno); - p1.x += SCREEN_WIDTH/2; - p1.y += SCREEN_HEIGHT/2; + OFFSET_POINT(p1); return p1; -} +} //}}} void draw_hexagon_side(struct s_game *g, int faceno, int dist, int col) { //{{{ SDL_Point p1, p2, p3, p4; @@ -36,40 +35,75 @@ draw_hexagon_side(struct s_game *g, int faceno, int dist, int col) { //{{{ SDL_FillPolygon(g->screen, points, 4, col); }//}}} void -draw_hexagon(struct s_game *g) { +draw_hexagon(struct s_game *g) { //{{{ int faces; #define DFT_COLOR 128 for(faces = 0; faces < SHAPE_HEXAGON; faces++) { - draw_hexagon_side(g, faces, 30, DFT_COLOR); + draw_hexagon_side(g, faces, 30, g->color); } +} //}}} + + +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; } - void -init_game(struct s_game *g) { +draw_cursor(struct s_game *g) { + SDL_Point p1, p2, p3; - g->polygon_type = SHAPE_HEXAGON; - g->cursor_angle = 0; + p1 = get_circle_vertice(g->cursor_angle, CURSOR_DIST+10); + p2 = get_circle_vertice(g->cursor_angle-TO_DEG(8), CURSOR_DIST); + p3 = get_circle_vertice(g->cursor_angle+TO_DEG(8), CURSOR_DIST); + SDL_Point pts[3] = {p1, p2, p3}; + + SDL_FillPolygon(g->screen, pts, 3, g->color); } void draw_game(struct s_game *g) { - switch(g->polygon_type) { case SHAPE_HEXAGON: draw_hexagon(g); break; } - /* - SDL_Point p1, p2, p3; - p1.x = 10; p1.y = 10; - p2.x = 10; p2.y = 20; - p3.x = 40; p3.y = 40; - SDL_Point points[3] = {p1, p2, p3} ; - SDL_FillPolygon(g->screen, points, 3, 0xffff0000); - */ + 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 -= 5; + if(g->keys[SDLK_RIGHT]) + g->cursor_angle += 5; + + g->cursor_angle %= ANGLE_MAX; +} + + +void +init_game(struct s_game *g) { //{{{ + g->polygon_type = SHAPE_HEXAGON; + g->cursor_angle = TO_DEG(90); + g->color = 128; + g->keys = SDL_GetKeyState(NULL); +} //}}} diff --git a/game.h b/game.h index 29d591f..fa8a2df 100644 --- a/game.h +++ b/game.h @@ -18,16 +18,20 @@ #define SHAPE_HEXAGON 6 #define SHAPE_THICK 5 +#define CURSOR_DIST 40 +#define ANGLE_MAX 3600 +#define OFFSET_POINT(p) {p.x += SCREEN_WIDTH/2; p.y += SCREEN_HEIGHT/2;} +#define TO_DEG(x) (x*ANGLE_MAX/360) struct s_game { #define CURSOR_POS_MAX 255 int cursor_angle; //relative to the polygon center's referential - int polygon_type; //Shape - + int color; SDL_Surface *screen; + Uint8 *keys; }; diff --git a/main.c b/main.c index c8d80e8..4c741c0 100644 --- a/main.c +++ b/main.c @@ -6,6 +6,7 @@ #include #include +#include "SDL/SDL.h" #include "game.h" #include "sdl.h" @@ -21,6 +22,8 @@ main(int argc, char *argv[]) while(1) { + SDL_FillRect(ga.screen, NULL, 0); + get_keys(&ga); draw_game(&ga); SDL_Flip(ga.screen); //get key