first, basic transition to C++
This commit is contained in:
parent
f2203cfa00
commit
4c2273d271
16
Makefile
16
Makefile
@ -6,7 +6,7 @@ GTK_CFLAGS=$(shell pkg-config --cflags $(GTK_PACKAGES))
|
|||||||
GTK_LIBS=$(shell pkg-config --libs $(GTK_PACKAGES))
|
GTK_LIBS=$(shell pkg-config --libs $(GTK_PACKAGES))
|
||||||
|
|
||||||
# PROFILING_CFLAGS=-pg
|
# PROFILING_CFLAGS=-pg
|
||||||
CFLAGS=-Wall -g -O2 $(PROFILING_CFLAGS) $(GTK_CFLAGS)
|
CXXFLAGS=-Wall -g -O2 $(PROFILING_CFLAGS) $(GTK_CFLAGS)
|
||||||
|
|
||||||
LIBS=$(GTK_LIBS) -lm
|
LIBS=$(GTK_LIBS) -lm
|
||||||
|
|
||||||
@ -14,13 +14,13 @@ PROGS=balls
|
|||||||
OBJS=balls.o c_index.o game.o gravity.o spaceship.o main.o
|
OBJS=balls.o c_index.o game.o gravity.o spaceship.o main.o
|
||||||
|
|
||||||
# dependencies (gcc -MM *.c)
|
# dependencies (gcc -MM *.c)
|
||||||
balls.o: balls.c game.h balls.h gravity.h
|
balls.o: balls.cc game.h balls.h gravity.h
|
||||||
c_index.o: c_index.c balls.h game.h c_index.h
|
c_index.o: c_index.cc balls.h game.h c_index.h
|
||||||
game.o: game.c game.h
|
game.o: game.cc game.h
|
||||||
gravity.o: gravity.c gravity.h game.h
|
gravity.o: gravity.cc gravity.h game.h
|
||||||
main.o: main.c game.h balls.h c_index.h gravity.h spaceship.h
|
main.o: main.cc game.h balls.h c_index.h gravity.h spaceship.h
|
||||||
spaceship.o: spaceship.c balls.h game.h
|
spaceship.o: spaceship.cc balls.h game.h
|
||||||
stats.o: stats.c
|
stats.o: stats.cc
|
||||||
|
|
||||||
.PHONY: run
|
.PHONY: run
|
||||||
run: balls
|
run: balls
|
||||||
|
@ -28,9 +28,9 @@ static void random_velocity(struct ball * p) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void balls_init_state () {
|
void balls_init_state () {
|
||||||
static const unsigned int border = 10;
|
static const int border = 10;
|
||||||
unsigned int w = width < 2*border ? 1 : width - 2*border;
|
int w = width < 2*border ? 1 : width - 2*border;
|
||||||
unsigned int h = height < 2*border ? 1 : height - 2*border;
|
int h = height < 2*border ? 1 : height - 2*border;
|
||||||
|
|
||||||
for (unsigned int i = 0; i < n_balls; ++i) {
|
for (unsigned int i = 0; i < n_balls; ++i) {
|
||||||
balls[i].x = border + rand() % w;
|
balls[i].x = border + rand() % w;
|
||||||
@ -141,7 +141,7 @@ static double random_color_component() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static struct ball_face * new_ball_face(unsigned int radius, cairo_surface_t * face, int rotation) {
|
static struct ball_face * new_ball_face(unsigned int radius, cairo_surface_t * face, int rotation) {
|
||||||
struct ball_face * f = malloc(sizeof(struct ball_face));
|
struct ball_face * f = (struct ball_face *)malloc(sizeof(struct ball_face));
|
||||||
if (!f)
|
if (!f)
|
||||||
return 0;
|
return 0;
|
||||||
if (face && rotation) {
|
if (face && rotation) {
|
||||||
@ -149,12 +149,12 @@ static struct ball_face * new_ball_face(unsigned int radius, cairo_surface_t * f
|
|||||||
} else {
|
} else {
|
||||||
f->rotations = 1;
|
f->rotations = 1;
|
||||||
}
|
}
|
||||||
f->c_faces = malloc(sizeof(cairo_surface_t *)*f->rotations);
|
f->c_faces = (cairo_surface_t **)malloc(sizeof(cairo_surface_t *)*f->rotations);
|
||||||
if (!f->c_faces) {
|
if (!f->c_faces) {
|
||||||
free(f);
|
free(f);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < f->rotations; ++i) {
|
for (unsigned int i = 0; i < f->rotations; ++i) {
|
||||||
f->c_faces[i] = gdk_window_create_similar_surface(gtk_widget_get_window(canvas),
|
f->c_faces[i] = gdk_window_create_similar_surface(gtk_widget_get_window(canvas),
|
||||||
CAIRO_CONTENT_COLOR_ALPHA,
|
CAIRO_CONTENT_COLOR_ALPHA,
|
||||||
2*radius, 2*radius);
|
2*radius, 2*radius);
|
||||||
@ -204,7 +204,7 @@ static void balls_init_faces () {
|
|||||||
}
|
}
|
||||||
if (face_surface) {
|
if (face_surface) {
|
||||||
faces_count = radius_max + 1 - radius_min;
|
faces_count = radius_max + 1 - radius_min;
|
||||||
faces = malloc(sizeof(struct ball_face *)*faces_count);
|
faces = (struct ball_face **)malloc(sizeof(struct ball_face *)*faces_count);
|
||||||
for (unsigned int i = 0; i < faces_count; ++i)
|
for (unsigned int i = 0; i < faces_count; ++i)
|
||||||
faces[i] = 0;
|
faces[i] = 0;
|
||||||
for(struct ball * b = balls; b != balls + n_balls; ++b) {
|
for(struct ball * b = balls; b != balls + n_balls; ++b) {
|
||||||
@ -216,7 +216,7 @@ static void balls_init_faces () {
|
|||||||
cairo_surface_destroy (face_surface);
|
cairo_surface_destroy (face_surface);
|
||||||
} else {
|
} else {
|
||||||
faces_count = n_balls;
|
faces_count = n_balls;
|
||||||
faces = malloc(sizeof(struct ball_face *)*faces_count);
|
faces = (struct ball_face **)malloc(sizeof(struct ball_face *)*faces_count);
|
||||||
for (unsigned int i = 0; i < n_balls; ++i)
|
for (unsigned int i = 0; i < n_balls; ++i)
|
||||||
balls[i].face = faces[i] = new_ball_face(balls[i].radius, 0, face_rotation);
|
balls[i].face = faces[i] = new_ball_face(balls[i].radius, 0, face_rotation);
|
||||||
}
|
}
|
||||||
@ -244,7 +244,7 @@ void balls_draw (cairo_t * cr) {
|
|||||||
static void balls_destroy_faces () {
|
static void balls_destroy_faces () {
|
||||||
if (!faces)
|
if (!faces)
|
||||||
return;
|
return;
|
||||||
for (int i = 0; i < faces_count; ++i) {
|
for (unsigned int i = 0; i < faces_count; ++i) {
|
||||||
if (faces[i]) {
|
if (faces[i]) {
|
||||||
if (faces[i]->c_faces) {
|
if (faces[i]->c_faces) {
|
||||||
for (unsigned int j = 0; j < faces[i]->rotations; ++j)
|
for (unsigned int j = 0; j < faces[i]->rotations; ++j)
|
||||||
@ -265,7 +265,7 @@ void balls_destroy () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void balls_init () {
|
void balls_init () {
|
||||||
balls = malloc(sizeof(struct ball)*n_balls);
|
balls = (struct ball *)malloc(sizeof(struct ball)*n_balls);
|
||||||
assert(balls);
|
assert(balls);
|
||||||
balls_init_state ();
|
balls_init_state ();
|
||||||
balls_init_faces ();
|
balls_init_faces ();
|
@ -96,7 +96,7 @@ static void c_index_insert(struct bt_node * t, struct bt_node * n, struct ball *
|
|||||||
|
|
||||||
void c_index_build() {
|
void c_index_build() {
|
||||||
c_index_init_node(c_index, balls);
|
c_index_init_node(c_index, balls);
|
||||||
for(int i = 1; i < n_balls; ++i)
|
for(unsigned int i = 1; i < n_balls; ++i)
|
||||||
c_index_insert(c_index, c_index + i, balls + i);
|
c_index_insert(c_index, c_index + i, balls + i);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -150,11 +150,11 @@ void c_index_check_collisions(void (*collision)(struct ball *, struct ball *)) {
|
|||||||
|
|
||||||
int c_index_init() {
|
int c_index_init() {
|
||||||
if (!c_index)
|
if (!c_index)
|
||||||
c_index = malloc(sizeof(struct bt_node) * n_balls);
|
c_index = (struct bt_node *) malloc(sizeof(struct bt_node) * n_balls);
|
||||||
if (!c_index)
|
if (!c_index)
|
||||||
return 0;
|
return 0;
|
||||||
if (!c_index_stack)
|
if (!c_index_stack)
|
||||||
c_index_stack = malloc(sizeof(struct bt_node *) * n_balls);
|
c_index_stack = (struct bt_node **) malloc(sizeof(struct bt_node *) * n_balls);
|
||||||
if (!c_index_stack)
|
if (!c_index_stack)
|
||||||
return 0;
|
return 0;
|
||||||
return 1;
|
return 1;
|
@ -11,8 +11,8 @@
|
|||||||
|
|
||||||
double delta = DEFAULT_DELTA; /* seconds */
|
double delta = DEFAULT_DELTA; /* seconds */
|
||||||
|
|
||||||
unsigned int width = DEFAULT_WIDTH;
|
int width = DEFAULT_WIDTH;
|
||||||
unsigned int height = DEFAULT_HEIGHT;
|
int height = DEFAULT_HEIGHT;
|
||||||
|
|
||||||
/* Graphics System
|
/* Graphics System
|
||||||
*/
|
*/
|
4
game.h
4
game.h
@ -10,8 +10,8 @@ extern double delta; /* simulation time delta in seconds */
|
|||||||
|
|
||||||
#define DEFAULT_DELTA 0.01
|
#define DEFAULT_DELTA 0.01
|
||||||
|
|
||||||
extern unsigned int width; /* game canvas width */
|
extern int width; /* game canvas width */
|
||||||
extern unsigned int height; /* game canvas height */
|
extern int height; /* game canvas height */
|
||||||
|
|
||||||
#define DEFAULT_WIDTH 800
|
#define DEFAULT_WIDTH 800
|
||||||
#define DEFAULT_HEIGHT 800
|
#define DEFAULT_HEIGHT 800
|
||||||
|
@ -19,10 +19,10 @@
|
|||||||
/* Trivial collision check
|
/* Trivial collision check
|
||||||
*/
|
*/
|
||||||
void check_collisions_simple () {
|
void check_collisions_simple () {
|
||||||
for(int i = 0; i < n_balls; ++i)
|
for(unsigned int i = 0; i < n_balls; ++i)
|
||||||
for(int j = i + 1; j < n_balls; ++j)
|
for(unsigned int j = i + 1; j < n_balls; ++j)
|
||||||
ball_elastic_collision(balls + i, balls + j);
|
ball_elastic_collision(balls + i, balls + j);
|
||||||
for(int j = 0; j < n_balls; ++j)
|
for(unsigned int j = 0; j < n_balls; ++j)
|
||||||
ball_elastic_collision(&spaceship, balls + j);
|
ball_elastic_collision(&spaceship, balls + j);
|
||||||
gravity_collisions (balls, balls + n_balls);
|
gravity_collisions (balls, balls + n_balls);
|
||||||
gravity_collisions (&spaceship, &spaceship + 1);
|
gravity_collisions (&spaceship, &spaceship + 1);
|
||||||
@ -31,7 +31,7 @@ void check_collisions_simple () {
|
|||||||
void check_collisions_with_index () {
|
void check_collisions_with_index () {
|
||||||
c_index_build();
|
c_index_build();
|
||||||
c_index_check_collisions(ball_elastic_collision);
|
c_index_check_collisions(ball_elastic_collision);
|
||||||
for(int j = 0; j < n_balls; ++j)
|
for(unsigned int j = 0; j < n_balls; ++j)
|
||||||
ball_elastic_collision(&spaceship, balls + j);
|
ball_elastic_collision(&spaceship, balls + j);
|
||||||
gravity_collisions (balls, balls + n_balls);
|
gravity_collisions (balls, balls + n_balls);
|
||||||
gravity_collisions (&spaceship, &spaceship + 1);
|
gravity_collisions (&spaceship, &spaceship + 1);
|
||||||
@ -42,7 +42,7 @@ void (*check_collisions)() = 0;
|
|||||||
void update_state () {
|
void update_state () {
|
||||||
if (check_collisions)
|
if (check_collisions)
|
||||||
check_collisions();
|
check_collisions();
|
||||||
for(int i = 0; i < n_balls; ++i)
|
for(unsigned int i = 0; i < n_balls; ++i)
|
||||||
ball_update_state(balls + i);
|
ball_update_state(balls + i);
|
||||||
spaceship_update_state();
|
spaceship_update_state();
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user