Reformated main.cc and spaceship.cc

This commit is contained in:
Karma Riuk 2023-03-14 21:45:20 +01:00
parent af31bc7148
commit d0670178c0
2 changed files with 275 additions and 252 deletions

450
main.cc
View File

@ -1,103 +1,106 @@
#include <stdio.h>
#include <stdlib.h>
#include <assert.h> #include <assert.h>
#include <math.h>
#include <time.h>
#include <gdk/gdkkeysyms.h> #include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define DEFAULT_WIDTH 800 #define DEFAULT_WIDTH 800
#define DEFAULT_HEIGHT 800 #define DEFAULT_HEIGHT 800
#include "game.h"
#include "balls.h" #include "balls.h"
#include "c_index.h" #include "c_index.h"
#include "game.h"
#include "gravity.h" #include "gravity.h"
#include "polygons.h"
#include "spaceship.h" #include "spaceship.h"
/* Trivial collision check /* Trivial collision check
*/ */
void check_collisions_simple () { void check_collisions_simple() {
for(unsigned int i = 0; i < n_balls; ++i) for (unsigned int i = 0; i < n_balls; ++i)
for(unsigned int j = i + 1; j < n_balls; ++j) for (unsigned int j = i + 1; j < n_balls; ++j)
ball_ball_collision(balls + i, balls + j); ball_ball_collision(balls + i, balls + j);
for(unsigned int j = 0; j < n_balls; ++j) for (unsigned int j = 0; j < n_balls; ++j)
ball_ball_collision(&spaceship, balls + j); ball_ball_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);
} }
void check_collisions_with_index () { void check_collisions_with_index() {
c_index_build(); c_index_build();
c_index_check_collisions(ball_ball_collision); c_index_check_collisions(ball_ball_collision);
for(unsigned int j = 0; j < n_balls; ++j) for (unsigned int j = 0; j < n_balls; ++j)
ball_ball_collision(&spaceship, balls + j); ball_ball_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);
} }
void (*check_collisions)() = check_collisions_simple; void (*check_collisions)() = check_collisions_simple;
void update_state () { void update_state() {
if (check_collisions) if (check_collisions)
check_collisions(); check_collisions();
for(unsigned 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();
} }
/* Graphics System /* Graphics System
*/ */
void game_init () { void game_init() {
srand (time(NULL)); srand(time(NULL));
balls_init (); balls_init();
assert(c_index_init()); assert(c_index_init());
spaceship_init_state (); polygons_init_state();
// spaceship_init_state();
} }
void game_destroy () { void game_destroy() {
c_index_destroy (); c_index_destroy();
balls_destroy (); balls_destroy();
} }
static guint animation_timeout_id = 0; static guint animation_timeout_id = 0;
gboolean animation_timeout (gpointer user_data); gboolean animation_timeout(gpointer user_data);
static bool game_paused () { static bool game_paused() {
return animation_timeout_id == 0; return animation_timeout_id == 0;
} }
void game_animation_on_off () { void game_animation_on_off() {
if (animation_timeout_id == 0) { if (animation_timeout_id == 0) {
animation_timeout_id = g_timeout_add (delta * 1000, animation_timeout, canvas); animation_timeout_id =
g_timeout_add(delta * 1000, animation_timeout, canvas);
} else { } else {
g_source_remove (animation_timeout_id); g_source_remove(animation_timeout_id);
animation_timeout_id = 0; animation_timeout_id = 0;
} }
} }
struct param_controls { struct param_controls {
void (*draw) (cairo_t * cr); void (*draw)(cairo_t* cr);
gint (*keyboard_input) (GdkEventKey *event); gint (*keyboard_input)(GdkEventKey* event);
gboolean (*mouse_scroll) (GtkWidget *widget, GdkEvent *event, gpointer user_data); gboolean (*mouse_scroll)(
GtkWidget* widget, GdkEvent* event, gpointer user_data);
}; };
gint gravity_controls_keyboard_input (GdkEventKey *event) { gint gravity_controls_keyboard_input(GdkEventKey* event) {
switch(event->keyval) { switch (event->keyval) {
case GDK_KEY_Up: case GDK_KEY_Up:
gravity_change (0, -10); gravity_change(0, -10);
return TRUE; return TRUE;
case GDK_KEY_Down: case GDK_KEY_Down:
gravity_change (0, 10); gravity_change(0, 10);
return TRUE; return TRUE;
case GDK_KEY_Left: case GDK_KEY_Left:
gravity_change (-10, 0); gravity_change(-10, 0);
return TRUE; return TRUE;
case GDK_KEY_Right: case GDK_KEY_Right:
gravity_change (10, 0); gravity_change(10, 0);
return TRUE; return TRUE;
} }
return FALSE; return FALSE;
} }
@ -107,20 +110,20 @@ param_controls gravity_control = {
.keyboard_input = gravity_controls_keyboard_input, .keyboard_input = gravity_controls_keyboard_input,
}; };
gint restitution_coefficient_controls_keyboard_input (GdkEventKey *event) { gint restitution_coefficient_controls_keyboard_input(GdkEventKey* event) {
switch(event->keyval) { switch (event->keyval) {
case GDK_KEY_Up: case GDK_KEY_Up:
restitution_coefficient_change (0.01); restitution_coefficient_change(0.01);
return TRUE; return TRUE;
case GDK_KEY_Down: case GDK_KEY_Down:
restitution_coefficient_change (-0.01); restitution_coefficient_change(-0.01);
return TRUE; return TRUE;
case GDK_KEY_Left: case GDK_KEY_Left:
restitution_coefficient_change (-0.01); restitution_coefficient_change(-0.01);
return TRUE; return TRUE;
case GDK_KEY_Right: case GDK_KEY_Right:
restitution_coefficient_change (0.01); restitution_coefficient_change(0.01);
return TRUE; return TRUE;
} }
return FALSE; return FALSE;
} }
@ -131,119 +134,122 @@ param_controls restitution_coefficient_control = {
}; };
static param_controls* param_control = nullptr;
static param_controls * param_control = nullptr;
static guint param_control_timeout_id = 0; static guint param_control_timeout_id = 0;
static const guint param_control_active_ms = 3000; static const guint param_control_active_ms = 3000;
gboolean param_control_timeout (gpointer user_data) { gboolean param_control_timeout(gpointer user_data) {
param_control = nullptr; param_control = nullptr;
param_control_timeout_id = 0; param_control_timeout_id = 0;
gtk_widget_queue_draw(canvas); gtk_widget_queue_draw(canvas);
return FALSE; return FALSE;
} }
void param_control_activate (struct param_controls * ctrl) { void param_control_activate(struct param_controls* ctrl) {
if (param_control) if (param_control)
g_source_remove (param_control_timeout_id); g_source_remove(param_control_timeout_id);
param_control = ctrl; param_control = ctrl;
param_control_timeout_id = g_timeout_add (param_control_active_ms, param_control_timeout, canvas); param_control_timeout_id =
g_timeout_add(param_control_active_ms, param_control_timeout, canvas);
gtk_widget_queue_draw(canvas); gtk_widget_queue_draw(canvas);
} }
gboolean draw_frame (GtkWidget * widget, cairo_t *cr, gpointer data) { gboolean draw_frame(GtkWidget* widget, cairo_t* cr, gpointer data) {
cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0); cairo_set_source_rgba(cr, 0.0, 0.0, 0.0, 1.0);
cairo_paint(cr); cairo_paint(cr);
if (param_control) if (param_control)
param_control->draw (cr); param_control->draw(cr);
gravity_draw_visible_field (cr); gravity_draw_visible_field(cr);
balls_draw (cr); balls_draw(cr);
spaceship_draw (cr); spaceship_draw(cr);
polygons_draw(cr);
return FALSE; return FALSE;
} }
gint configure_event (GtkWidget *widget, GdkEventConfigure * event) { gint configure_event(GtkWidget* widget, GdkEventConfigure* event) {
if (width == event->width && height == event->height) if (width == event->width && height == event->height)
return FALSE; return FALSE;
width = gtk_widget_get_allocated_width (widget); width = gtk_widget_get_allocated_width(widget);
height = gtk_widget_get_allocated_height (widget); height = gtk_widget_get_allocated_height(widget);
for (ball * b = balls; b != balls + n_balls; ++b) for (ball* b = balls; b != balls + n_balls; ++b)
ball_reposition (b); ball_reposition(b);
ball_reposition (&spaceship); ball_reposition(&spaceship);
return TRUE; return TRUE;
} }
gint keyboard_input (GtkWidget *widget, GdkEventKey *event) { gint keyboard_input(GtkWidget* widget, GdkEventKey* event) {
if (event->type != GDK_KEY_PRESS) if (event->type != GDK_KEY_PRESS)
return FALSE; return FALSE;
if (param_control && param_control->keyboard_input (event)) { if (param_control && param_control->keyboard_input(event)) {
gtk_widget_queue_draw(canvas); gtk_widget_queue_draw(canvas);
g_source_remove (param_control_timeout_id); g_source_remove(param_control_timeout_id);
param_control_timeout_id = g_timeout_add (param_control_active_ms, param_control_timeout, canvas); param_control_timeout_id = g_timeout_add(
return TRUE; param_control_active_ms, param_control_timeout, canvas);
return TRUE;
} }
switch(event->keyval) { switch (event->keyval) {
case GDK_KEY_R: case GDK_KEY_R:
case GDK_KEY_r: case GDK_KEY_r:
param_control_activate (&restitution_coefficient_control); param_control_activate(&restitution_coefficient_control);
return TRUE; return TRUE;
case GDK_KEY_G: case GDK_KEY_G:
case GDK_KEY_g: case GDK_KEY_g:
param_control_activate (&gravity_control); param_control_activate(&gravity_control);
return TRUE; return TRUE;
case GDK_KEY_P: case GDK_KEY_P:
case GDK_KEY_p: case GDK_KEY_p:
game_animation_on_off (); game_animation_on_off();
return TRUE; return TRUE;
case GDK_KEY_Q: case GDK_KEY_Q:
case GDK_KEY_q: case GDK_KEY_q:
gtk_main_quit(); gtk_main_quit();
return TRUE; return TRUE;
} }
return FALSE; return FALSE;
} }
gboolean mouse_scroll (GtkWidget *widget, GdkEvent *event, gpointer user_data) { gboolean mouse_scroll(GtkWidget* widget, GdkEvent* event, gpointer user_data) {
if (event->type == GDK_SCROLL && ! game_paused ()) { if (event->type == GDK_SCROLL && !game_paused()) {
GdkEventScroll * e = (GdkEventScroll*) event; GdkEventScroll* e = (GdkEventScroll*) event;
switch (e->direction) { switch (e->direction) {
case GDK_SCROLL_SMOOTH: { case GDK_SCROLL_SMOOTH: {
double dx, dy; double dx, dy;
gdk_event_get_scroll_deltas (event, &dx, &dy); gdk_event_get_scroll_deltas(event, &dx, &dy);
spaceship_control (dx, dy); spaceship_control(dx, dy);
break; break;
} }
default: default:
break; break;
} }
} }
return TRUE; return TRUE;
} }
void destroy_window (void) { void destroy_window(void) {
gtk_main_quit(); gtk_main_quit();
} }
void print_usage (const char * progname) { void print_usage(const char* progname) {
fprintf(stderr, fprintf(stderr,
"usage: %s [options...]\n" "usage: %s [options...]\n"
"options:\n" "options:\n"
"\t<width>x<height>\n" "\t<width>x<height>\n"
"\tn=<number of balls>\n" "\tn=<number of balls>\n"
"\tfconst=<x-force>,<y-force> :: constant force field\n" "\tfconst=<x-force>,<y-force> :: constant force field\n"
"\tfnewt=<radius>,<g> :: radial, Newtonian force field\n" "\tfnewt=<radius>,<g> :: radial, Newtonian force field\n"
"\tfy=<y-force>\n" "\tfy=<y-force>\n"
"\tradius=<min-radius>-<max-radius>\n" "\tradius=<min-radius>-<max-radius>\n"
"\tv=<min-velocity>-<max-velocity>\n" "\tv=<min-velocity>-<max-velocity>\n"
"\tdelta=<frame-delta-time> (in seconds)\n" "\tdelta=<frame-delta-time> (in seconds)\n"
"\tface=<filename>\n" "\tface=<filename>\n"
"\tstats=<sample-count> :: rendering timing statitstics (0=disabled, default)\n" "\tstats=<sample-count> :: rendering timing statitstics (0=disabled, "
"\tcollisions=<C> :: n=no collisions, s=simple, i=index\n" "default)\n"
"\trestitution=<C_r> :: restitution coefficient C_r\n" "\tcollisions=<C> :: n=no collisions, s=simple, i=index\n"
"\t-r :: activate face rotation\n", "\trestitution=<C_r> :: restitution coefficient C_r\n"
progname); "\t-r :: activate face rotation\n",
progname);
} }
static unsigned int stats_sampling = 0; static unsigned int stats_sampling = 0;
@ -252,135 +258,145 @@ static unsigned int stats_update_samples = 0;
static guint64 stats_draw_usec = 0; static guint64 stats_draw_usec = 0;
static unsigned int stats_draw_samples = 0; static unsigned int stats_draw_samples = 0;
gboolean draw_event (GtkWidget *widget, cairo_t * cr, gpointer data) { gboolean draw_event(GtkWidget* widget, cairo_t* cr, gpointer data) {
if (stats_sampling > 0) { if (stats_sampling > 0) {
guint64 start = g_get_monotonic_time (); guint64 start = g_get_monotonic_time();
draw_frame (widget, cr, data); draw_frame(widget, cr, data);
stats_draw_usec += g_get_monotonic_time () - start; stats_draw_usec += g_get_monotonic_time() - start;
++stats_draw_samples; ++stats_draw_samples;
} else } else
draw_frame (widget, cr, data); draw_frame(widget, cr, data);
return FALSE; return FALSE;
} }
gboolean animation_timeout (gpointer user_data) { gboolean animation_timeout(gpointer user_data) {
if (stats_sampling > 0) { if (stats_sampling > 0) {
guint64 start = g_get_monotonic_time (); guint64 start = g_get_monotonic_time();
update_state(); update_state();
stats_update_usec += g_get_monotonic_time () - start; stats_update_usec += g_get_monotonic_time() - start;
if (++stats_update_samples == stats_sampling) { if (++stats_update_samples == stats_sampling) {
float uavg = 1.0*stats_update_usec / stats_update_samples; float uavg = 1.0 * stats_update_usec / stats_update_samples;
float davg = 1.0*stats_draw_usec / stats_draw_samples; float davg = 1.0 * stats_draw_usec / stats_draw_samples;
printf("\rupdate = %.0f us, draw = %.0f us, load = %.0f%% (%u update, %u draw) ", printf("\rupdate = %.0f us, draw = %.0f us, load = %.0f%% (%u "
uavg, davg, (uavg+davg)/(10000.0*delta), "update, %u draw) ",
stats_update_samples, stats_draw_samples); uavg,
fflush(stdout); davg,
stats_update_usec = 0; (uavg + davg) / (10000.0 * delta),
stats_update_samples = 0; stats_update_samples,
stats_draw_usec = 0; stats_draw_samples);
stats_draw_samples = 0; fflush(stdout);
} stats_update_usec = 0;
stats_update_samples = 0;
stats_draw_usec = 0;
stats_draw_samples = 0;
}
} else { } else {
update_state(); update_state();
} }
gtk_widget_queue_draw(canvas); gtk_widget_queue_draw(canvas);
return TRUE; return TRUE;
} }
int main (int argc, const char *argv[]) { int main(int argc, const char* argv[]) {
int w = DEFAULT_WIDTH; int w = DEFAULT_WIDTH;
int h = DEFAULT_HEIGHT; int h = DEFAULT_HEIGHT;
double fa, fb; double fa, fb;
for (int i = 1; i < argc; ++i) { for (int i = 1; i < argc; ++i) {
if (sscanf(argv[i], "%dx%d", &w, &h) == 2) if (sscanf(argv[i], "%dx%d", &w, &h) == 2)
continue; continue;
if (sscanf(argv[i], "n=%u", &n_balls) == 1) if (sscanf(argv[i], "n=%u", &n_balls) == 1)
continue; continue;
if (sscanf(argv[i], "fconst=%lf,%lf", &fa, &fb) == 2) { if (sscanf(argv[i], "fconst=%lf,%lf", &fa, &fb) == 2) {
gravity_constant_field (fa,fb); gravity_constant_field(fa, fb);
continue; continue;
} }
if (sscanf(argv[i], "fnewt=%lf,%lf", &fa, &fb) == 2) { if (sscanf(argv[i], "fnewt=%lf,%lf", &fa, &fb) == 2) {
gravity_newton_field (fa,fb); gravity_newton_field(fa, fb);
continue; continue;
} }
if (sscanf(argv[i], "radius=%u-%u", &radius_min, &radius_max) == 2) if (sscanf(argv[i], "radius=%u-%u", &radius_min, &radius_max) == 2)
continue; continue;
if (sscanf(argv[i], "v=%u-%u", &v_min, &v_max) == 2) if (sscanf(argv[i], "v=%u-%u", &v_min, &v_max) == 2)
continue; continue;
if (sscanf(argv[i], "delta=%lf", &delta) == 1) if (sscanf(argv[i], "delta=%lf", &delta) == 1)
continue; continue;
if (strncmp(argv[i], "face=", 5) == 0) { if (strncmp(argv[i], "face=", 5) == 0) {
face_filename = argv[i] + 5; face_filename = argv[i] + 5;
continue; continue;
} }
if (sscanf(argv[i], "stats=%u", &stats_sampling) == 1) if (sscanf(argv[i], "stats=%u", &stats_sampling) == 1)
continue; continue;
char collisions; char collisions;
if (sscanf(argv[i], "collisions=%c", &collisions) == 1) { if (sscanf(argv[i], "collisions=%c", &collisions) == 1) {
switch (collisions) { switch (collisions) {
case 'i': case 'i':
case 'I': case 'I':
check_collisions = check_collisions_with_index; check_collisions = check_collisions_with_index;
continue; continue;
case '0': case '0':
case 'N': case 'N':
case 'n': case 'n':
check_collisions = 0; check_collisions = 0;
continue; continue;
case 's': case 's':
case 'S': case 'S':
check_collisions = check_collisions_simple; check_collisions = check_collisions_simple;
continue; continue;
} }
} }
if (strcmp(argv[i], "-r") == 0) { if (strcmp(argv[i], "-r") == 0) {
face_rotation = 1; face_rotation = 1;
continue; continue;
} }
double c_r; double c_r;
if (sscanf(argv[i], "restitution=%lf", &c_r) == 1) { if (sscanf(argv[i], "restitution=%lf", &c_r) == 1) {
restitution_coefficient_set (c_r); restitution_coefficient_set(c_r);
continue; continue;
} }
print_usage(argv[0]); print_usage(argv[0]);
return 1; return 1;
} }
gtk_init(0, 0); gtk_init(0, 0);
GtkWidget * window = gtk_window_new (GTK_WINDOW_TOPLEVEL); GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), width, height); gtk_window_set_default_size(GTK_WINDOW(window), width, height);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_title(GTK_WINDOW(window), "Game"); gtk_window_set_title(GTK_WINDOW(window), "Game");
g_signal_connect(window, "destroy", G_CALLBACK(destroy_window), NULL); g_signal_connect(window, "destroy", G_CALLBACK(destroy_window), NULL);
g_signal_connect (G_OBJECT (window), "delete-event", G_CALLBACK(destroy_window), NULL); g_signal_connect(
g_signal_connect(G_OBJECT (window), "key-press-event", G_CALLBACK(keyboard_input), NULL); G_OBJECT(window), "delete-event", G_CALLBACK(destroy_window), NULL);
gtk_widget_set_events (window, GDK_EXPOSURE_MASK | GDK_SCROLL_MASK | GDK_SMOOTH_SCROLL_MASK | GDK_KEY_PRESS_MASK); g_signal_connect(
G_OBJECT(window), "key-press-event", G_CALLBACK(keyboard_input), NULL);
gtk_widget_set_events(window,
GDK_EXPOSURE_MASK | GDK_SCROLL_MASK | GDK_SMOOTH_SCROLL_MASK |
GDK_KEY_PRESS_MASK);
canvas = gtk_drawing_area_new (); canvas = gtk_drawing_area_new();
g_signal_connect (G_OBJECT (canvas), "configure-event", G_CALLBACK(configure_event), NULL); g_signal_connect(
g_signal_connect (G_OBJECT (canvas), "draw", G_CALLBACK (draw_event), NULL); G_OBJECT(canvas), "configure-event", G_CALLBACK(configure_event), NULL);
g_signal_connect (G_OBJECT (canvas), "scroll-event",G_CALLBACK(mouse_scroll), NULL); g_signal_connect(G_OBJECT(canvas), "draw", G_CALLBACK(draw_event), NULL);
gtk_widget_set_events (canvas, GDK_SCROLL_MASK | GDK_SMOOTH_SCROLL_MASK); g_signal_connect(
G_OBJECT(canvas), "scroll-event", G_CALLBACK(mouse_scroll), NULL);
gtk_widget_set_events(canvas, GDK_SCROLL_MASK | GDK_SMOOTH_SCROLL_MASK);
gtk_container_add (GTK_CONTAINER (window), canvas); gtk_container_add(GTK_CONTAINER(window), canvas);
game_animation_on_off (); game_animation_on_off();
gtk_widget_show_all(window); gtk_widget_show_all(window);
game_init (); game_init();
gtk_main(); gtk_main();
if (stats_sampling > 0) if (stats_sampling > 0)
printf("\n"); printf("\n");
game_destroy (); game_destroy();
return 0; return 0;
} }

View File

@ -1,17 +1,17 @@
#include <math.h>
#include <gtk/gtk.h>
#include "balls.h" #include "balls.h"
#include "game.h" #include "game.h"
#include <gtk/gtk.h>
#include <math.h>
struct ball spaceship; struct ball spaceship;
double spaceship_thrust = 0; double spaceship_thrust = 0;
int spaceship_thrust_countdown = 0; int spaceship_thrust_countdown = 0;
int spaceship_thrust_init = 50; int spaceship_thrust_init = 50;
void spaceship_init_state () { void spaceship_init_state() {
spaceship.position.x = width/2; spaceship.position.x = width / 2;
spaceship.position.y = height/2; spaceship.position.y = height / 2;
spaceship.radius = 30; spaceship.radius = 30;
spaceship.velocity.x = 0; spaceship.velocity.x = 0;
spaceship.velocity.y = 0; spaceship.velocity.y = 0;
@ -19,57 +19,64 @@ void spaceship_init_state () {
spaceship.v_angle = 0; spaceship.v_angle = 0;
} }
void spaceship_update_state () { void spaceship_update_state() {
if (spaceship_thrust > 0) { if (spaceship_thrust > 0) {
vec2d f { cos(spaceship.angle)*spaceship_thrust*4.0, vec2d f{cos(spaceship.angle) * spaceship_thrust * 4.0,
sin(spaceship.angle)*spaceship_thrust*4.0 }; sin(spaceship.angle) * spaceship_thrust * 4.0};
spaceship.position += delta*delta*f/2.0; spaceship.position += delta * delta * f / 2.0;
spaceship.velocity += delta*f; spaceship.velocity += delta * f;
if (spaceship_thrust_countdown > 0) if (spaceship_thrust_countdown > 0)
--spaceship_thrust_countdown; --spaceship_thrust_countdown;
else else
spaceship_thrust = 0; spaceship_thrust = 0;
} }
ball_update_state(&spaceship); ball_update_state(&spaceship);
} }
void spaceship_draw (cairo_t * cr) { void spaceship_draw(cairo_t* cr) {
static const double one_over_sqrt_2 = 0.70710678118654752440; static const double one_over_sqrt_2 = 0.70710678118654752440;
cairo_save(cr); cairo_save(cr);
cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 1.0); cairo_set_source_rgba(cr, 0.0, 0.0, 1.0, 1.0);
cairo_translate(cr, spaceship.position.x, spaceship.position.y); cairo_translate(cr, spaceship.position.x, spaceship.position.y);
cairo_rotate(cr, spaceship.angle); cairo_rotate(cr, spaceship.angle);
cairo_arc(cr, 0, 0, spaceship.radius, 0, 2*M_PI); cairo_arc(cr, 0, 0, spaceship.radius, 0, 2 * M_PI);
cairo_stroke(cr); cairo_stroke(cr);
cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 1.0); cairo_set_source_rgba(cr, 0.0, 1.0, 0.0, 1.0);
cairo_move_to (cr, 0, 0); cairo_move_to(cr, 0, 0);
cairo_line_to (cr, -one_over_sqrt_2*spaceship.radius, one_over_sqrt_2*spaceship.radius); cairo_line_to(cr,
cairo_line_to (cr, spaceship.radius, 0); -one_over_sqrt_2 * spaceship.radius,
cairo_line_to (cr, -one_over_sqrt_2*spaceship.radius, -one_over_sqrt_2*spaceship.radius); one_over_sqrt_2 * spaceship.radius);
cairo_line_to (cr, 0, 0); cairo_line_to(cr, spaceship.radius, 0);
cairo_line_to(cr,
-one_over_sqrt_2 * spaceship.radius,
-one_over_sqrt_2 * spaceship.radius);
cairo_line_to(cr, 0, 0);
cairo_stroke(cr); cairo_stroke(cr);
cairo_set_source_rgba(cr, 1.0, 0.5, 0.0, 1.0); cairo_set_source_rgba(cr, 1.0, 0.5, 0.0, 1.0);
for (unsigned int i = 0; i < spaceship_thrust; i += 5) { for (unsigned int i = 0; i < spaceship_thrust; i += 5) {
double d_angle = spaceship.radius/(spaceship.radius + 0.1*i)*0.25*M_PI*(1 - 0.99*i/spaceship_thrust); double d_angle = spaceship.radius / (spaceship.radius + 0.1 * i) *
cairo_set_source_rgba(cr, 1.0, 1.0*(1 - 0.5*i/spaceship_thrust), 0.0, 1.0); 0.25 * M_PI * (1 - 0.99 * i / spaceship_thrust);
cairo_arc(cr, 0, 0, spaceship.radius + i, M_PI - d_angle, M_PI + d_angle); cairo_set_source_rgba(
cairo_stroke(cr); cr, 1.0, 1.0 * (1 - 0.5 * i / spaceship_thrust), 0.0, 1.0);
if (d_angle > 0.05) cairo_arc(
d_angle = 0.7*d_angle; cr, 0, 0, spaceship.radius + i, M_PI - d_angle, M_PI + d_angle);
cairo_stroke(cr);
if (d_angle > 0.05)
d_angle = 0.7 * d_angle;
} }
cairo_restore(cr); cairo_restore(cr);
} }
void spaceship_control (double dx, double dy) { void spaceship_control(double dx, double dy) {
spaceship.angle -= dx/4; spaceship.angle -= dx / 4;
if (spaceship.angle < 0) if (spaceship.angle < 0)
spaceship.angle += 2*M_PI; spaceship.angle += 2 * M_PI;
else if (spaceship.angle > 2*M_PI) else if (spaceship.angle > 2 * M_PI)
spaceship.angle -= 2*M_PI; spaceship.angle -= 2 * M_PI;
spaceship_thrust += dy; spaceship_thrust += dy;
if (spaceship_thrust > 0) if (spaceship_thrust > 0)
spaceship_thrust_countdown = spaceship_thrust_init; spaceship_thrust_countdown = spaceship_thrust_init;
else else
spaceship_thrust = 0; spaceship_thrust = 0;
} }