obstacles

This commit is contained in:
Frank Villaro-Dixon 2015-01-01 05:32:07 +01:00
parent 8e44c93d36
commit 9fd08a1145
7 changed files with 172 additions and 27 deletions

32
game.c
View file

@ -8,6 +8,7 @@
#include <stdlib.h>
#include <math.h>
#include "game.h"
#include "obs.h"
#include "SDL_prims.h"
#include "SDL/SDL.h"
@ -23,13 +24,13 @@ get_hexagon_vertice(int faceno, int dist) { //{{{
return p1;
} //}}}
void
draw_hexagon_side(struct s_game *g, int faceno, int dist, int col) { //{{{
draw_hexagon_side(struct s_game *g, int faceno, int dist, int thick, 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);
p3 = get_hexagon_vertice(faceno+1, dist+thick);
p4 = get_hexagon_vertice(faceno, dist+thick);
SDL_Point points[4] = {p1, p2, p3, p4} ;
SDL_FillPolygon(g->screen, points, 4, col);
@ -40,14 +41,13 @@ draw_hexagon(struct s_game *g) { //{{{
#define DFT_COLOR 128
for(faces = 0; faces < SHAPE_HEXAGON; faces++) {
draw_hexagon_side(g, faces, 30, g->color);
draw_hexagon_side(g, faces, POLYGON_SIZE, SHAPE_THICK, g->color);
}
} //}}}
SDL_Point
get_circle_vertice(int angle, int dist)
{
get_circle_vertice(int angle, int dist) { //{{{
SDL_Point p;
p.x = cos((double)angle*2*M_PI/ANGLE_MAX) * dist;
@ -56,9 +56,9 @@ get_circle_vertice(int angle, int dist)
OFFSET_POINT(p);
return p;
}
} //}}}
void
draw_cursor(struct s_game *g) {
draw_cursor(struct s_game *g) { //{{{
SDL_Point p1, p2, p3;
p1 = get_circle_vertice(g->cursor_angle, CURSOR_DIST+10);
@ -68,7 +68,8 @@ draw_cursor(struct s_game *g) {
SDL_Point pts[3] = {p1, p2, p3};
SDL_FillPolygon(g->screen, pts, 3, g->color);
}
} //}}}
void
draw_game(struct s_game *g) {
@ -76,6 +77,7 @@ draw_game(struct s_game *g) {
switch(g->polygon_type) {
case SHAPE_HEXAGON:
draw_hexagon(g);
draw_obstacles(g, draw_hexagon_side);
break;
}
@ -84,7 +86,7 @@ draw_game(struct s_game *g) {
void
get_keys(struct s_game *g) {
get_keys(struct s_game *g) { //{{{
SDL_PumpEvents();
if(g->keys[SDL_QUIT])
exit(0);
@ -97,13 +99,17 @@ get_keys(struct s_game *g) {
g->cursor_angle += TO_DEG(3);
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);
g->num_obs = NUM_OBS;
g->counter = 0;
init_obs(g);
} //}}}

25
game.h
View file

@ -13,6 +13,8 @@
#define SCREEN_HEIGHT 480
#define POLYGON_SIZE 30
#define SHAPE_SQUARE 4
#define SHAPE_PENTAGON 5
#define SHAPE_HEXAGON 6
@ -24,25 +26,31 @@
#define OFFSET_POINT(p) {p.x += SCREEN_WIDTH/2; p.y += SCREEN_HEIGHT/2;}
#define TO_DEG(x) (x*ANGLE_MAX/360)
struct s_obstacle {
int used;
int thick;
int face;
int distance;
int speed;
};
struct s_game {
#define CURSOR_POS_MAX 255
int cursor_angle; //relative to the polygon center's referential
int polygon_type; //Shape
int color;
int actual_speed;
int counter;
SDL_Surface *screen;
Uint8 *keys;
#define NUM_OBS 100
struct s_obstacle obs[NUM_OBS];
int num_obs;
};
struct s_obstacle {
int thick;
int angle;
int distance;
int speed;
};
void
@ -51,6 +59,9 @@ init_game(struct s_game *g);
void
draw_game(struct s_game *g);
void
get_keys(struct s_game *g);
#endif /* ndef GAME_H */

18
main.c
View file

@ -9,27 +9,35 @@
#include "SDL/SDL.h"
#include "game.h"
#include "sdl.h"
#include "obs.h"
int
main(int argc, char *argv[])
main()
{
struct s_game ga;
start:
init_game(&ga);
init_SDL(&ga);
struct s_obstacle a = create_obstacle(SHAPE_THICK, 2, 1);
add_obstacle(&ga, a);
while(1) {
SDL_FillRect(ga.screen, NULL, 0);
get_keys(&ga);
if(!(ga.counter % 50)) {
add_random_obstacle(&ga);
}
update_obstacles(&ga);
draw_game(&ga);
SDL_Flip(ga.screen);
//get key
//do calcs
//collisions
get_keys(&ga);
SDL_Delay(10);
ga.counter++;
}
return EXIT_SUCCESS;

86
obs.c Normal file
View file

@ -0,0 +1,86 @@
/*
** obs.c - <+DESC+>
**
** 2014 - Frank Villaro-Dixon <Frank@Villaro-Dixon.eu>
*/
#include <stdio.h>
#include <stdlib.h>
#include "game.h"
#include <math.h>
void
init_ob(struct s_obstacle *obs) { //{{{
obs->used = 0;
} //}}}
void
init_obs(struct s_game *g) { //{{{
int i;
for(i = 0; i < g->num_obs; i++) {
init_ob(&g->obs[i]);
}
} //}}}
void
add_obstacle(struct s_game *g, struct s_obstacle o) { //{{{
int i;
for(i = 0; i < g->num_obs; i++) {
if(!g->obs[i].used) {
g->obs[i] = o;
break;
}
}
} //}}}
struct s_obstacle
create_obstacle(int thick, int face, int speed) { //{{{
struct s_obstacle o;
o.used = 1;
o.thick = thick;
o.face = face;
o.distance = sqrt(SCREEN_WIDTH*SCREEN_WIDTH + SCREEN_HEIGHT*SCREEN_HEIGHT)/2;
o.speed = speed;
return o;
} //}}}
void
delete_obstacle(struct s_game *g, int obs) { //{{{
g->obs[obs].used = 0;
} //}}}
void
draw_obstacles(struct s_game *g,
void(*drawer)(struct s_game *, int faceno, int dist, int thick, int col)) { //{{{
int i;
for(i = 0; i < g->num_obs; i++) {
if(g->obs[i].used) {
struct s_obstacle o = g->obs[i];
drawer(g, o.face, o.distance, o.thick, g->color);
}
}
} //}}}
void
update_obstacles(struct s_game *g) { //{{{
int i;
for(i = 0; i < g->num_obs; i++) {
if(g->obs[i].used) {
g->obs[i].distance -= g->obs[i].speed;
if(g->obs[i].distance <= POLYGON_SIZE)
g->obs[i].used = 0;
}
}
} //}}}
void
add_random_obstacle(struct s_game *g) {
struct s_obstacle o;
o = create_obstacle(SHAPE_THICK, rand()%g->polygon_type, 1);
add_obstacle(g, o);
}

34
obs.h Normal file
View file

@ -0,0 +1,34 @@
/*
** obs.h - <+DESC+>
**
** 2014 - Frank Villaro-Dixon <Frank@Villaro-Dixon.eu>
*/
#ifndef OBS_H
#define OBS_H
void
init_obs(struct s_game *g);
void
add_obstacle(struct s_game *g, struct s_obstacle o);
struct s_obstacle
create_obstacle(int thick, int face, int speed);
void
delete_obstacle(struct s_game *g, int obs);
void
draw_obstacles(struct s_game *g,
void(*drawer)(struct s_game *, int faceno, int dist, int thick, int col));
void
update_obstacles(struct s_game *g);
void
add_random_obstacle(struct s_game *g);
#endif /* ndef OBS_H */

2
sdl.c
View file

@ -9,7 +9,7 @@
#include "SDL/SDL.h"
#include "game.h"
int
void
init_SDL(struct s_game *g)
{
/* initialize SDL */

2
sdl.h
View file

@ -7,7 +7,7 @@
#ifndef SDL_H
#define SDL_H
int
void
init_SDL(struct s_game *g);
#endif /* ndef SDL_H */