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))
|
||||
|
||||
# PROFILING_CFLAGS=-pg
|
||||
CFLAGS=-Wall -g -O2 $(PROFILING_CFLAGS) $(GTK_CFLAGS)
|
||||
CXXFLAGS=-Wall -g -O2 $(PROFILING_CFLAGS) $(GTK_CFLAGS)
|
||||
|
||||
LIBS=$(GTK_LIBS) -lm
|
||||
|
||||
@ -14,13 +14,13 @@ PROGS=balls
|
||||
OBJS=balls.o c_index.o game.o gravity.o spaceship.o main.o
|
||||
|
||||
# dependencies (gcc -MM *.c)
|
||||
balls.o: balls.c game.h balls.h gravity.h
|
||||
c_index.o: c_index.c balls.h game.h c_index.h
|
||||
game.o: game.c game.h
|
||||
gravity.o: gravity.c gravity.h game.h
|
||||
main.o: main.c game.h balls.h c_index.h gravity.h spaceship.h
|
||||
spaceship.o: spaceship.c balls.h game.h
|
||||
stats.o: stats.c
|
||||
balls.o: balls.cc game.h balls.h gravity.h
|
||||
c_index.o: c_index.cc balls.h game.h c_index.h
|
||||
game.o: game.cc game.h
|
||||
gravity.o: gravity.cc gravity.h game.h
|
||||
main.o: main.cc game.h balls.h c_index.h gravity.h spaceship.h
|
||||
spaceship.o: spaceship.cc balls.h game.h
|
||||
stats.o: stats.cc
|
||||
|
||||
.PHONY: run
|
||||
run: balls
|
||||
|
@ -28,9 +28,9 @@ static void random_velocity(struct ball * p) {
|
||||
}
|
||||
|
||||
void balls_init_state () {
|
||||
static const unsigned int border = 10;
|
||||
unsigned int w = width < 2*border ? 1 : width - 2*border;
|
||||
unsigned int h = height < 2*border ? 1 : height - 2*border;
|
||||
static const int border = 10;
|
||||
int w = width < 2*border ? 1 : width - 2*border;
|
||||
int h = height < 2*border ? 1 : height - 2*border;
|
||||
|
||||
for (unsigned int i = 0; i < n_balls; ++i) {
|
||||
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) {
|
||||
struct ball_face * f = malloc(sizeof(struct ball_face));
|
||||
struct ball_face * f = (struct ball_face *)malloc(sizeof(struct ball_face));
|
||||
if (!f)
|
||||
return 0;
|
||||
if (face && rotation) {
|
||||
@ -149,12 +149,12 @@ static struct ball_face * new_ball_face(unsigned int radius, cairo_surface_t * f
|
||||
} else {
|
||||
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) {
|
||||
free(f);
|
||||
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),
|
||||
CAIRO_CONTENT_COLOR_ALPHA,
|
||||
2*radius, 2*radius);
|
||||
@ -204,7 +204,7 @@ static void balls_init_faces () {
|
||||
}
|
||||
if (face_surface) {
|
||||
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)
|
||||
faces[i] = 0;
|
||||
for(struct ball * b = balls; b != balls + n_balls; ++b) {
|
||||
@ -216,7 +216,7 @@ static void balls_init_faces () {
|
||||
cairo_surface_destroy (face_surface);
|
||||
} else {
|
||||
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)
|
||||
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 () {
|
||||
if (!faces)
|
||||
return;
|
||||
for (int i = 0; i < faces_count; ++i) {
|
||||
for (unsigned int i = 0; i < faces_count; ++i) {
|
||||
if (faces[i]) {
|
||||
if (faces[i]->c_faces) {
|
||||
for (unsigned int j = 0; j < faces[i]->rotations; ++j)
|
||||
@ -265,7 +265,7 @@ void balls_destroy () {
|
||||
}
|
||||
|
||||
void balls_init () {
|
||||
balls = malloc(sizeof(struct ball)*n_balls);
|
||||
balls = (struct ball *)malloc(sizeof(struct ball)*n_balls);
|
||||
assert(balls);
|
||||
balls_init_state ();
|
||||
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() {
|
||||
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);
|
||||
}
|
||||
|
||||
@ -150,11 +150,11 @@ void c_index_check_collisions(void (*collision)(struct ball *, struct ball *)) {
|
||||
|
||||
int c_index_init() {
|
||||
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)
|
||||
return 0;
|
||||
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)
|
||||
return 0;
|
||||
return 1;
|
@ -11,8 +11,8 @@
|
||||
|
||||
double delta = DEFAULT_DELTA; /* seconds */
|
||||
|
||||
unsigned int width = DEFAULT_WIDTH;
|
||||
unsigned int height = DEFAULT_HEIGHT;
|
||||
int width = DEFAULT_WIDTH;
|
||||
int height = DEFAULT_HEIGHT;
|
||||
|
||||
/* 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
|
||||
|
||||
extern unsigned int width; /* game canvas width */
|
||||
extern unsigned int height; /* game canvas height */
|
||||
extern int width; /* game canvas width */
|
||||
extern int height; /* game canvas height */
|
||||
|
||||
#define DEFAULT_WIDTH 800
|
||||
#define DEFAULT_HEIGHT 800
|
||||
|
@ -19,10 +19,10 @@
|
||||
/* Trivial collision check
|
||||
*/
|
||||
void check_collisions_simple () {
|
||||
for(int i = 0; i < n_balls; ++i)
|
||||
for(int j = i + 1; j < n_balls; ++j)
|
||||
for(unsigned int i = 0; i < n_balls; ++i)
|
||||
for(unsigned int j = i + 1; j < n_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);
|
||||
gravity_collisions (balls, balls + n_balls);
|
||||
gravity_collisions (&spaceship, &spaceship + 1);
|
||||
@ -31,7 +31,7 @@ void check_collisions_simple () {
|
||||
void check_collisions_with_index () {
|
||||
c_index_build();
|
||||
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);
|
||||
gravity_collisions (balls, balls + n_balls);
|
||||
gravity_collisions (&spaceship, &spaceship + 1);
|
||||
@ -42,7 +42,7 @@ void (*check_collisions)() = 0;
|
||||
void update_state () {
|
||||
if (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);
|
||||
spaceship_update_state();
|
||||
}
|
Loading…
Reference in New Issue
Block a user