From 4c2273d271c2bf6549a30a30a9de2d1e407be001 Mon Sep 17 00:00:00 2001 From: Antonio Carzaniga Date: Mon, 30 May 2022 15:59:45 +0200 Subject: [PATCH] first, basic transition to C++ --- Makefile | 16 ++++++++-------- balls.c => balls.cc | 20 ++++++++++---------- c_index.c => c_index.cc | 6 +++--- game.c => game.cc | 4 ++-- game.h | 4 ++-- gravity.c => gravity.cc | 0 main.c => main.cc | 10 +++++----- spaceship.c => spaceship.cc | 0 stats.c => stats.cc | 0 9 files changed, 30 insertions(+), 30 deletions(-) rename balls.c => balls.cc (91%) rename c_index.c => c_index.cc (94%) rename game.c => game.cc (77%) rename gravity.c => gravity.cc (100%) rename main.c => main.cc (97%) rename spaceship.c => spaceship.cc (100%) rename stats.c => stats.cc (100%) diff --git a/Makefile b/Makefile index af9a5b3..0c12939 100644 --- a/Makefile +++ b/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 diff --git a/balls.c b/balls.cc similarity index 91% rename from balls.c rename to balls.cc index 4d75d45..fc9d25b 100644 --- a/balls.c +++ b/balls.cc @@ -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 (); diff --git a/c_index.c b/c_index.cc similarity index 94% rename from c_index.c rename to c_index.cc index 61ee3f5..44678e5 100644 --- a/c_index.c +++ b/c_index.cc @@ -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; diff --git a/game.c b/game.cc similarity index 77% rename from game.c rename to game.cc index ab4a9f0..bb8b694 100644 --- a/game.c +++ b/game.cc @@ -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 */ diff --git a/game.h b/game.h index cba025d..f34c5ab 100644 --- a/game.h +++ b/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 diff --git a/gravity.c b/gravity.cc similarity index 100% rename from gravity.c rename to gravity.cc diff --git a/main.c b/main.cc similarity index 97% rename from main.c rename to main.cc index 37a1c12..e9108f5 100644 --- a/main.c +++ b/main.cc @@ -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(); } diff --git a/spaceship.c b/spaceship.cc similarity index 100% rename from spaceship.c rename to spaceship.cc diff --git a/stats.c b/stats.cc similarity index 100% rename from stats.c rename to stats.cc