This commit is contained in:
Frank Villaro-Dixon 2015-01-01 04:34:12 +01:00
parent 7100267c41
commit e6d229ffb1
3 changed files with 63 additions and 22 deletions

74
game.c
View file

@ -9,20 +9,19 @@
#include <math.h>
#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);
} //}}}

8
game.h
View file

@ -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;
};

3
main.c
View file

@ -6,6 +6,7 @@
#include <stdio.h>
#include <stdlib.h>
#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