2015-01-01 03:04:34 +00:00
|
|
|
/*
|
|
|
|
** game.c - The game
|
|
|
|
**
|
|
|
|
** 2014 - Frank Villaro-Dixon <Frank@Villaro-Dixon.eu>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include "game.h"
|
|
|
|
#include "SDL_prims.h"
|
2015-01-01 03:34:12 +00:00
|
|
|
#include "SDL/SDL.h"
|
2015-01-01 03:04:34 +00:00
|
|
|
|
|
|
|
SDL_Point
|
2015-01-01 03:34:12 +00:00
|
|
|
get_hexagon_vertice(int faceno, int dist) { //{{{
|
2015-01-01 03:04:34 +00:00
|
|
|
SDL_Point p1;
|
|
|
|
|
|
|
|
p1.x = dist * cos((M_PI/3)*faceno);
|
|
|
|
p1.y = dist * sin((M_PI/3)*faceno);
|
|
|
|
|
2015-01-01 03:34:12 +00:00
|
|
|
OFFSET_POINT(p1);
|
2015-01-01 03:04:34 +00:00
|
|
|
|
|
|
|
return p1;
|
2015-01-01 03:34:12 +00:00
|
|
|
} //}}}
|
2015-01-01 03:04:34 +00:00
|
|
|
void
|
|
|
|
draw_hexagon_side(struct s_game *g, int faceno, int dist, 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+SHAPE_THICK);
|
|
|
|
p4 = get_hexagon_vertice(faceno, dist+SHAPE_THICK);
|
|
|
|
|
|
|
|
SDL_Point points[4] = {p1, p2, p3, p4} ;
|
|
|
|
SDL_FillPolygon(g->screen, points, 4, col);
|
|
|
|
}//}}}
|
|
|
|
void
|
2015-01-01 03:34:12 +00:00
|
|
|
draw_hexagon(struct s_game *g) { //{{{
|
2015-01-01 03:04:34 +00:00
|
|
|
int faces;
|
|
|
|
|
|
|
|
#define DFT_COLOR 128
|
|
|
|
for(faces = 0; faces < SHAPE_HEXAGON; faces++) {
|
2015-01-01 03:34:12 +00:00
|
|
|
draw_hexagon_side(g, faces, 30, g->color);
|
2015-01-01 03:04:34 +00:00
|
|
|
}
|
2015-01-01 03:34:12 +00:00
|
|
|
} //}}}
|
2015-01-01 03:04:34 +00:00
|
|
|
|
2015-01-01 03:34:12 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2015-01-01 03:04:34 +00:00
|
|
|
void
|
2015-01-01 03:34:12 +00:00
|
|
|
draw_cursor(struct s_game *g) {
|
|
|
|
SDL_Point p1, p2, p3;
|
2015-01-01 03:04:34 +00:00
|
|
|
|
2015-01-01 03:34:12 +00:00
|
|
|
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};
|
2015-01-01 03:04:34 +00:00
|
|
|
|
2015-01-01 03:34:12 +00:00
|
|
|
SDL_FillPolygon(g->screen, pts, 3, g->color);
|
2015-01-01 03:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
draw_game(struct s_game *g) {
|
|
|
|
|
|
|
|
switch(g->polygon_type) {
|
|
|
|
case SHAPE_HEXAGON:
|
|
|
|
draw_hexagon(g);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-01-01 03:34:12 +00:00
|
|
|
draw_cursor(g);
|
2015-01-01 03:04:34 +00:00
|
|
|
}
|
|
|
|
|
2015-01-01 03:34:12 +00:00
|
|
|
|
|
|
|
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])
|
2015-01-01 03:45:30 +00:00
|
|
|
g->cursor_angle -= TO_DEG(3);
|
2015-01-01 03:34:12 +00:00
|
|
|
if(g->keys[SDLK_RIGHT])
|
2015-01-01 03:45:30 +00:00
|
|
|
g->cursor_angle += TO_DEG(3);
|
2015-01-01 03:34:12 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
} //}}}
|