2019-06-03 10:09:00 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
2021-12-11 21:32:48 +01:00
|
|
|
#include <assert.h>
|
2019-06-03 10:09:00 +02:00
|
|
|
|
2021-12-11 21:32:48 +01:00
|
|
|
#include "game.h"
|
2021-12-01 20:18:19 +01:00
|
|
|
#include "balls.h"
|
2021-12-11 21:32:48 +01:00
|
|
|
#include "gravity.h"
|
2019-06-03 10:09:00 +02:00
|
|
|
|
2019-08-20 16:15:29 +02:00
|
|
|
unsigned int radius_min = 5;
|
|
|
|
unsigned int radius_max = 10;
|
2019-06-03 10:09:00 +02:00
|
|
|
|
2019-08-20 16:15:29 +02:00
|
|
|
unsigned int v_max = 100;
|
|
|
|
unsigned int v_min = 0;
|
2019-06-03 10:09:00 +02:00
|
|
|
|
2019-12-18 21:18:21 +01:00
|
|
|
unsigned int v_angle_min = 0;
|
|
|
|
unsigned int v_angle_max = 100;
|
|
|
|
|
2019-06-03 10:09:00 +02:00
|
|
|
struct ball * balls = 0;
|
2019-06-04 10:13:02 +02:00
|
|
|
unsigned int n_balls = 50;
|
2019-06-03 10:09:00 +02:00
|
|
|
|
2021-12-11 21:32:48 +01:00
|
|
|
static void random_velocity(struct ball * p) {
|
2019-07-05 15:58:10 +02:00
|
|
|
double r2;
|
|
|
|
do {
|
|
|
|
p->v_x = v_min + rand() % (v_max + 1 - v_min);
|
|
|
|
p->v_y = v_min + rand() % (v_max + 1 - v_min);
|
|
|
|
r2 = p->v_x*p->v_x + p->v_y*p->v_y;
|
|
|
|
} while (r2 > v_max*v_max || r2 < v_min*v_min);
|
|
|
|
}
|
|
|
|
|
2019-06-03 22:24:06 +02:00
|
|
|
void balls_init_state () {
|
2019-06-03 10:09:00 +02:00
|
|
|
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;
|
2019-06-04 10:13:02 +02:00
|
|
|
|
2019-06-03 10:09:00 +02:00
|
|
|
for (unsigned int i = 0; i < n_balls; ++i) {
|
|
|
|
balls[i].x = border + rand() % w;
|
|
|
|
balls[i].y = border + rand() % h;
|
2019-07-05 15:58:10 +02:00
|
|
|
random_velocity(balls + i);
|
2019-06-05 14:51:02 +02:00
|
|
|
if (rand() % 2)
|
|
|
|
balls[i].v_x = -balls[i].v_x;
|
|
|
|
if (rand() % 2)
|
|
|
|
balls[i].v_y = -balls[i].v_y;
|
2019-06-04 10:13:02 +02:00
|
|
|
balls[i].radius = radius_min + rand() % (radius_max + 1 - radius_min);
|
2019-12-18 21:18:21 +01:00
|
|
|
unsigned int v_angle_360 = (v_angle_min + rand() % (v_angle_max + 1 - v_angle_min)) % 360;
|
|
|
|
balls[i].v_angle = 2*M_PI*v_angle_360/360;
|
|
|
|
balls[i].angle = (rand() % 360)*2*M_PI/360;
|
2019-06-03 22:24:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-19 10:45:55 +02:00
|
|
|
void ball_update_state (struct ball * p) {
|
2019-06-03 10:09:00 +02:00
|
|
|
p->x += delta*p->v_x + delta*delta*g_x/2.0;
|
|
|
|
p->v_x += delta*g_x;
|
|
|
|
|
|
|
|
p->y += delta*p->v_y + delta*delta*g_y/2.0;
|
|
|
|
p->v_y += delta*g_y;
|
|
|
|
|
2021-01-12 15:55:25 +01:00
|
|
|
if (p->x + p->radius > width) { /* right wall */
|
2019-06-03 22:24:06 +02:00
|
|
|
if (p->v_x > 0) {
|
|
|
|
p->x -= p->x + p->radius - width;
|
|
|
|
p->v_x = -p->v_x;
|
|
|
|
}
|
2021-01-12 15:55:25 +01:00
|
|
|
} else if (p->x < p->radius) { /* left wall */
|
2019-06-03 22:24:06 +02:00
|
|
|
if (p->v_x < 0) {
|
|
|
|
p->x += p->radius - p->x;
|
|
|
|
p->v_x = -p->v_x;
|
|
|
|
}
|
2019-06-03 10:09:00 +02:00
|
|
|
}
|
|
|
|
|
2021-01-12 15:55:25 +01:00
|
|
|
if (p->y + p->radius > height) { /* bottom wall */
|
2019-06-03 22:24:06 +02:00
|
|
|
if (p->v_y > 0) {
|
|
|
|
p->y -= p->y + p->radius - height;
|
|
|
|
p->v_y = -p->v_y;
|
|
|
|
}
|
2021-01-12 15:55:25 +01:00
|
|
|
} else if (p->y < p->radius) { /* top wall */
|
2019-06-03 22:24:06 +02:00
|
|
|
if (p->v_y < 0) {
|
|
|
|
p->y += p->radius - p->y;
|
|
|
|
p->v_y = -p->v_y;
|
|
|
|
}
|
2019-06-03 10:09:00 +02:00
|
|
|
}
|
2019-12-18 21:18:21 +01:00
|
|
|
p->angle += delta*p->v_angle;
|
2021-01-12 15:55:25 +01:00
|
|
|
while (p->angle >= 2*M_PI)
|
2019-12-18 21:18:21 +01:00
|
|
|
p->angle -= 2*M_PI;
|
2021-01-12 15:55:25 +01:00
|
|
|
while (p->angle < 0)
|
|
|
|
p->angle += 2*M_PI;
|
2019-06-03 10:09:00 +02:00
|
|
|
}
|
|
|
|
|
2021-12-11 21:32:48 +01:00
|
|
|
void ball_elastic_collision (struct ball * p, struct ball * q) {
|
|
|
|
double dx = q->x - p->x;
|
|
|
|
double dy = q->y - p->y;
|
|
|
|
double d2 = dx*dx + dy*dy;
|
|
|
|
double r = p->radius + q->radius;
|
|
|
|
if (d2 <= r*r) {
|
|
|
|
double dv_x = q->v_x - p->v_x;
|
|
|
|
double dv_y = q->v_y - p->v_y;
|
2019-08-20 16:15:29 +02:00
|
|
|
|
2021-12-11 21:32:48 +01:00
|
|
|
double mp = p->radius * p->radius;
|
|
|
|
double mq = q->radius * q->radius;
|
2019-08-19 10:45:55 +02:00
|
|
|
|
2021-12-11 21:32:48 +01:00
|
|
|
double f = dv_x*dx + dv_y*dy;
|
2019-08-20 16:15:29 +02:00
|
|
|
|
2021-12-11 21:32:48 +01:00
|
|
|
if (f < 0) {
|
|
|
|
f /= d2*(mp + mq);
|
|
|
|
p->v_x += 2*mq*f*dx;
|
|
|
|
p->v_y += 2*mq*f*dy;
|
2019-08-20 16:15:29 +02:00
|
|
|
|
2021-12-11 21:32:48 +01:00
|
|
|
q->v_x -= 2*mp*f*dx;
|
|
|
|
q->v_y -= 2*mp*f*dy;
|
|
|
|
}
|
|
|
|
}
|
2019-08-19 10:45:55 +02:00
|
|
|
}
|
|
|
|
|
2021-12-11 21:32:48 +01:00
|
|
|
void ball_reposition (struct ball * b) {
|
|
|
|
if (b->x < b->radius)
|
|
|
|
b->x = b->radius;
|
|
|
|
else if (b->x + b->radius > width)
|
|
|
|
b->x = width - b->radius;
|
|
|
|
if (b->y < b->radius)
|
|
|
|
b->y = b->radius;
|
|
|
|
else if (b->y + b->radius > height)
|
|
|
|
b->y = height - b->radius;
|
2019-07-28 15:03:27 +02:00
|
|
|
}
|
|
|
|
|
2019-07-29 22:45:32 +02:00
|
|
|
const char * face_filename = 0;
|
2021-01-03 13:05:23 +01:00
|
|
|
int face_rotation = 0;
|
2019-07-29 22:45:32 +02:00
|
|
|
|
2019-12-18 21:18:21 +01:00
|
|
|
static const double linear_rotation_unit = 2.0;
|
2019-07-29 22:45:32 +02:00
|
|
|
|
2021-12-11 21:32:48 +01:00
|
|
|
static unsigned int faces_count;
|
|
|
|
static struct ball_face ** faces;
|
2021-01-12 15:55:25 +01:00
|
|
|
|
|
|
|
struct ball_face {
|
|
|
|
unsigned int rotations;
|
|
|
|
cairo_surface_t ** c_faces;
|
|
|
|
};
|
|
|
|
|
2021-06-07 13:38:13 +02:00
|
|
|
static double random_color_component() {
|
|
|
|
return 1.0*(rand() % 200 + 56)/255;
|
|
|
|
};
|
|
|
|
|
2021-12-11 21:32:48 +01:00
|
|
|
static struct ball_face * new_ball_face(unsigned int radius, cairo_surface_t * face, int rotation) {
|
2021-01-12 15:55:25 +01:00
|
|
|
struct ball_face * f = malloc(sizeof(struct ball_face));
|
|
|
|
if (!f)
|
|
|
|
return 0;
|
2021-01-03 13:05:23 +01:00
|
|
|
if (face && rotation) {
|
2021-01-12 15:55:25 +01:00
|
|
|
f->rotations = 2*M_PI * radius / linear_rotation_unit;
|
2019-12-18 21:18:21 +01:00
|
|
|
} else {
|
2021-01-12 15:55:25 +01:00
|
|
|
f->rotations = 1;
|
2019-12-18 21:18:21 +01:00
|
|
|
}
|
2021-01-12 15:55:25 +01:00
|
|
|
f->c_faces = malloc(sizeof(cairo_surface_t *)*f->rotations);
|
|
|
|
if (!f->c_faces) {
|
|
|
|
free(f);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < f->rotations; ++i) {
|
2021-12-11 18:05:13 +01:00
|
|
|
f->c_faces[i] = gdk_window_create_similar_surface(gtk_widget_get_window(canvas),
|
2021-01-12 15:55:25 +01:00
|
|
|
CAIRO_CONTENT_COLOR_ALPHA,
|
|
|
|
2*radius, 2*radius);
|
|
|
|
assert(f->c_faces[i]);
|
|
|
|
cairo_t * ball_cr = cairo_create(f->c_faces[i]);
|
|
|
|
cairo_translate(ball_cr, radius, radius);
|
|
|
|
cairo_arc(ball_cr, 0.0, 0.0, radius, 0, 2 * M_PI);
|
2019-07-29 22:45:32 +02:00
|
|
|
cairo_clip(ball_cr);
|
2019-07-29 23:04:26 +02:00
|
|
|
|
2019-12-18 21:18:21 +01:00
|
|
|
if (face) {
|
|
|
|
int face_x_offset = cairo_image_surface_get_width (face) / 2;
|
|
|
|
int face_y_offset = cairo_image_surface_get_height (face) / 2;
|
2021-01-12 15:55:25 +01:00
|
|
|
cairo_rotate(ball_cr, i*2*M_PI/f->rotations);
|
|
|
|
cairo_scale (ball_cr, 1.0 * radius / face_x_offset, 1.0 * radius / face_y_offset);
|
2019-12-18 21:18:21 +01:00
|
|
|
cairo_set_source_surface(ball_cr, face, -face_x_offset, -face_y_offset);
|
2019-07-29 22:45:32 +02:00
|
|
|
cairo_paint(ball_cr);
|
2021-01-09 20:33:40 +01:00
|
|
|
} else {
|
2021-06-07 13:38:13 +02:00
|
|
|
cairo_pattern_t *pat;
|
|
|
|
pat = cairo_pattern_create_radial (-0.2*radius, -0.2*radius, 0.2*radius,
|
2021-06-08 21:00:31 +02:00
|
|
|
-0.2*radius, -0.2*radius, 1.3*radius);
|
2021-06-07 13:38:13 +02:00
|
|
|
double col_r = random_color_component();
|
|
|
|
double col_g = random_color_component();
|
|
|
|
double col_b = random_color_component();
|
|
|
|
cairo_pattern_add_color_stop_rgba (pat, 0, col_r, col_g, col_b, 1);
|
|
|
|
cairo_pattern_add_color_stop_rgba (pat, 1, col_r/3, col_g/3, col_b/3, 1);
|
|
|
|
cairo_set_source (ball_cr, pat);
|
|
|
|
cairo_arc (ball_cr, 0.0, 0.0, radius, 0, 2 * M_PI);
|
|
|
|
cairo_fill (ball_cr);
|
2019-07-29 22:45:32 +02:00
|
|
|
}
|
2021-01-12 15:55:25 +01:00
|
|
|
cairo_surface_flush(f->c_faces[i]);
|
2019-07-29 22:45:32 +02:00
|
|
|
cairo_destroy(ball_cr);
|
|
|
|
}
|
2021-01-12 15:55:25 +01:00
|
|
|
return f;
|
2019-12-18 21:18:21 +01:00
|
|
|
}
|
|
|
|
|
2021-12-11 21:32:48 +01:00
|
|
|
static void balls_init_faces () {
|
2019-12-18 21:18:21 +01:00
|
|
|
cairo_surface_t * face_surface = 0;
|
|
|
|
|
|
|
|
if (face_filename) {
|
|
|
|
face_surface = cairo_image_surface_create_from_png (face_filename);
|
|
|
|
if (cairo_surface_status(face_surface) != CAIRO_STATUS_SUCCESS) {
|
|
|
|
cairo_surface_destroy (face_surface);
|
|
|
|
face_surface = 0;
|
|
|
|
fprintf(stderr, "could not create surface from PNG file %s\n", face_filename);
|
|
|
|
}
|
|
|
|
}
|
2021-01-12 15:55:25 +01:00
|
|
|
if (face_surface) {
|
|
|
|
faces_count = radius_max + 1 - radius_min;
|
|
|
|
faces = 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) {
|
|
|
|
unsigned int r_idx = b->radius - radius_min;
|
|
|
|
if (!faces[r_idx])
|
|
|
|
faces[r_idx] = new_ball_face(b->radius, face_surface, face_rotation);
|
|
|
|
b->face = faces[r_idx];
|
|
|
|
}
|
2019-07-29 22:45:32 +02:00
|
|
|
cairo_surface_destroy (face_surface);
|
2021-01-12 15:55:25 +01:00
|
|
|
} else {
|
|
|
|
faces_count = n_balls;
|
|
|
|
faces = 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);
|
|
|
|
}
|
2019-07-29 19:25:45 +02:00
|
|
|
}
|
|
|
|
|
2021-12-11 21:32:48 +01:00
|
|
|
void balls_draw (cairo_t * cr) {
|
2021-12-03 00:29:18 +01:00
|
|
|
for (const struct ball * b = balls; b != balls + n_balls; ++b) {
|
2019-07-29 22:45:32 +02:00
|
|
|
cairo_save(cr);
|
2021-01-12 15:55:25 +01:00
|
|
|
cairo_translate(cr, b->x - b->radius, b->y - b->radius);
|
2019-12-18 21:18:21 +01:00
|
|
|
unsigned int face_id;
|
2021-01-12 15:55:25 +01:00
|
|
|
if (b->face->rotations == 1)
|
2019-12-18 21:18:21 +01:00
|
|
|
face_id = 0;
|
|
|
|
else {
|
2021-01-12 15:55:25 +01:00
|
|
|
face_id = b->face->rotations*b->angle/(2*M_PI);
|
|
|
|
assert(face_id < b->face->rotations);
|
|
|
|
if (face_id >= b->face->rotations)
|
|
|
|
face_id %= b->face->rotations;
|
2019-12-18 21:18:21 +01:00
|
|
|
}
|
2021-01-12 15:55:25 +01:00
|
|
|
cairo_set_source_surface(cr, b->face->c_faces[face_id], 0, 0);
|
2019-07-29 22:45:32 +02:00
|
|
|
cairo_paint(cr);
|
|
|
|
cairo_restore(cr);
|
2019-06-03 10:09:00 +02:00
|
|
|
}
|
2021-12-03 16:00:36 +01:00
|
|
|
}
|
|
|
|
|
2021-12-11 21:32:48 +01:00
|
|
|
static void balls_destroy_faces () {
|
|
|
|
if (!faces)
|
|
|
|
return;
|
|
|
|
for (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)
|
|
|
|
cairo_surface_destroy(faces[i]->c_faces[j]);
|
|
|
|
free(faces[i]->c_faces);
|
|
|
|
}
|
|
|
|
free(faces[i]);
|
2021-12-01 20:28:41 +01:00
|
|
|
}
|
|
|
|
}
|
2021-12-11 21:32:48 +01:00
|
|
|
free (faces);
|
|
|
|
faces = 0;
|
|
|
|
faces_count = 0;
|
2021-06-13 17:38:07 +02:00
|
|
|
}
|
2019-07-29 22:57:21 +02:00
|
|
|
|
2021-12-11 21:32:48 +01:00
|
|
|
void balls_destroy () {
|
|
|
|
balls_destroy_faces ();
|
|
|
|
free (balls);
|
2019-06-03 10:09:00 +02:00
|
|
|
}
|
|
|
|
|
2021-12-11 21:32:48 +01:00
|
|
|
void balls_init () {
|
2019-06-03 10:09:00 +02:00
|
|
|
balls = malloc(sizeof(struct ball)*n_balls);
|
|
|
|
assert(balls);
|
2021-12-11 21:32:48 +01:00
|
|
|
balls_init_state ();
|
|
|
|
balls_init_faces ();
|
2019-06-03 10:09:00 +02:00
|
|
|
}
|