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

58
main.cc
View File

@ -1,19 +1,19 @@
#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
@ -54,7 +54,8 @@ 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() {
@ -71,7 +72,8 @@ static bool game_paused () {
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;
@ -81,7 +83,8 @@ void game_animation_on_off () {
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) {
@ -131,7 +134,6 @@ 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;
@ -147,7 +149,8 @@ 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);
} }
@ -159,6 +162,7 @@ gboolean draw_frame (GtkWidget * widget, cairo_t *cr, gpointer data) {
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;
} }
@ -181,7 +185,8 @@ gint keyboard_input (GtkWidget *widget, GdkEventKey *event) {
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(
param_control_active_ms, param_control_timeout, canvas);
return TRUE; return TRUE;
} }
switch (event->keyval) { switch (event->keyval) {
@ -239,7 +244,8 @@ void print_usage (const char * progname) {
"\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, "
"default)\n"
"\tcollisions=<C> :: n=no collisions, s=simple, i=index\n" "\tcollisions=<C> :: n=no collisions, s=simple, i=index\n"
"\trestitution=<C_r> :: restitution coefficient C_r\n" "\trestitution=<C_r> :: restitution coefficient C_r\n"
"\t-r :: activate face rotation\n", "\t-r :: activate face rotation\n",
@ -271,9 +277,13 @@ gboolean animation_timeout (gpointer user_data) {
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,
davg,
(uavg + davg) / (10000.0 * delta),
stats_update_samples,
stats_draw_samples);
fflush(stdout); fflush(stdout);
stats_update_usec = 0; stats_update_usec = 0;
stats_update_samples = 0; stats_update_samples = 0;
@ -356,15 +366,21 @@ int main (int argc, const char *argv[]) {
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_OBJECT(canvas), "configure-event", G_CALLBACK(configure_event), NULL);
g_signal_connect(G_OBJECT(canvas), "draw", G_CALLBACK(draw_event), NULL); g_signal_connect(G_OBJECT(canvas), "draw", G_CALLBACK(draw_event), NULL);
g_signal_connect (G_OBJECT (canvas), "scroll-event",G_CALLBACK(mouse_scroll), NULL); 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_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);

View File

@ -1,9 +1,9 @@
#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;
@ -44,16 +44,23 @@ void spaceship_draw (cairo_t * cr) {
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,
-one_over_sqrt_2 * spaceship.radius,
one_over_sqrt_2 * spaceship.radius);
cairo_line_to(cr, spaceship.radius, 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,
-one_over_sqrt_2 * spaceship.radius,
-one_over_sqrt_2 * spaceship.radius);
cairo_line_to(cr, 0, 0); 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(
cr, 1.0, 1.0 * (1 - 0.5 * i / spaceship_thrust), 0.0, 1.0);
cairo_arc(
cr, 0, 0, spaceship.radius + i, M_PI - d_angle, M_PI + d_angle);
cairo_stroke(cr); cairo_stroke(cr);
if (d_angle > 0.05) if (d_angle > 0.05)
d_angle = 0.7 * d_angle; d_angle = 0.7 * d_angle;