rotagon/game.c

162 lines
3 KiB
C
Raw Normal View History

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>
2015-01-01 12:46:13 +00:00
#include <time.h>
2015-01-01 03:04:34 +00:00
#include "game.h"
2015-01-01 05:03:28 +00:00
#include "polygons.h"
2015-01-01 04:32:07 +00:00
#include "obs.h"
2015-01-01 03:04:34 +00:00
#include "SDL_prims.h"
2015-01-01 03:34:12 +00:00
#include "SDL/SDL.h"
2015-01-01 03:04:34 +00:00
2015-01-01 10:40:34 +00:00
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;
}
2015-01-01 05:19:10 +00:00
void
2015-01-01 10:40:34 +00:00
change_game_color(struct s_game *g) {
static int from_canal = 0;
static int to_canal = 1;
union color col;
col.col = g->color;
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;
}
void
draw_background(struct s_game *g) { //{{{
2015-01-01 05:19:10 +00:00
int face;
2015-01-01 06:01:41 +00:00
2015-01-01 05:19:10 +00:00
for(face = 0; face < g->polygon_type; face++) {
2015-01-02 13:28:11 +00:00
draw_polygon_side(g, face, POLYGON_SIZE, SCREEN_DIAGONAL,
divide_color(g->color, 2 + 2*(face%2)));
2015-01-01 05:19:10 +00:00
}
2015-01-01 10:40:34 +00:00
} //}}}
2015-01-01 05:19:10 +00:00
2015-01-01 04:57:59 +00:00
int
get_face_from_cursor(struct s_game *g) {
return g->cursor_angle * g->polygon_type / ANGLE_MAX;
}
2015-01-01 03:34:12 +00:00
SDL_Point
2015-01-01 04:32:07 +00:00
get_circle_vertice(int angle, int dist) { //{{{
2015-01-01 03:34:12 +00:00
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 04:32:07 +00:00
} //}}}
2015-01-01 06:01:41 +00:00
SDL_Point
2015-01-01 10:40:34 +00:00
to_screen_coords(struct s_game *g, SDL_Point p) { //{{{
2015-01-01 06:01:41 +00:00
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);
2015-01-01 06:37:18 +00:00
// q.x *= g->sexual_pulsation;
// q.y *= g->sexual_pulsation;
2015-01-01 06:01:41 +00:00
q.x += SCREEN_WIDTH/2;
q.y += SCREEN_HEIGHT/2;
return q;
2015-01-01 10:40:34 +00:00
}//}}}
2015-01-01 06:01:41 +00:00
2015-01-01 03:04:34 +00:00
void
2015-01-01 04:32:07 +00:00
draw_cursor(struct s_game *g) { //{{{
2015-01-01 03:34:12 +00:00
SDL_Point p1, p2, p3;
2015-01-01 03:04:34 +00:00
2015-01-01 04:57:59 +00:00
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);
2015-01-01 03:34:12 +00:00
2015-01-01 06:01:41 +00:00
SDL_Point pts[3] = {to_screen_coords(g, p1),
to_screen_coords(g, p2),
to_screen_coords(g, 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 04:32:07 +00:00
} //}}}
2015-01-01 05:19:10 +00:00
void
draw_game(struct s_game *g) { //{{{
2015-01-01 06:01:41 +00:00
draw_background(g);
2015-01-01 05:19:10 +00:00
draw_obstacles(g);
2015-01-01 06:37:18 +00:00
draw_polygon(g);
2015-01-01 03:34:12 +00:00
draw_cursor(g);
2015-01-01 05:19:10 +00:00
} //}}}
2015-01-01 03:04:34 +00:00
2015-01-01 03:34:12 +00:00
void
2015-01-01 04:32:07 +00:00
get_keys(struct s_game *g) { //{{{
2015-01-01 03:34:12 +00:00
SDL_PumpEvents();
if(g->keys[SDL_QUIT])
exit(0);
if(g->keys[SDLK_q])
exit(0);
if(g->keys[SDLK_LEFT])
2015-01-01 10:40:34 +00:00
g->cursor_angle -= TO_DEG(CURSOR_DEG_FRAME);
2015-01-01 03:34:12 +00:00
if(g->keys[SDLK_RIGHT])
2015-01-01 10:40:34 +00:00
g->cursor_angle += TO_DEG(CURSOR_DEG_FRAME);
2015-01-01 03:34:12 +00:00
g->cursor_angle %= ANGLE_MAX;
2015-01-01 04:57:59 +00:00
if(g->cursor_angle < 0)
g->cursor_angle += ANGLE_MAX;
2015-01-01 04:32:07 +00:00
} //}}}
2015-01-01 03:34:12 +00:00
void
init_game(struct s_game *g) { //{{{
g->polygon_type = SHAPE_HEXAGON;
g->cursor_angle = TO_DEG(90);
2015-01-01 06:01:41 +00:00
g->general_rotation = 0;
2015-01-01 06:37:18 +00:00
g->sexual_pulsation = 1;
2015-01-01 03:34:12 +00:00
g->color = 128;
g->keys = SDL_GetKeyState(NULL);
2015-01-01 04:32:07 +00:00
g->num_obs = NUM_OBS;
g->counter = 0;
2015-01-01 12:46:13 +00:00
clock_gettime(CLOCK_REALTIME, &g->start_time);
2015-01-01 04:32:07 +00:00
init_obs(g);
2015-01-01 03:34:12 +00:00
} //}}}