From 2a9d85645cd704ad4748e60e4467a2561ef16b4d Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Fri, 22 Jan 2010 07:08:29 +1300 Subject: [PATCH] Indent to 8 columns rather than 2 --- akamaru.c | 596 ++++++++++++++-------------- dock.c | 340 ++++++++-------- main.c | 1120 ++++++++++++++++++++++++++--------------------------- 3 files changed, 1028 insertions(+), 1028 deletions(-) diff --git a/akamaru.c b/akamaru.c index 76f0058..fd3c468 100644 --- a/akamaru.c +++ b/akamaru.c @@ -1,4 +1,4 @@ -/* -*- mode: c; c-basic-offset: 2 -*- +/* -*- mode: c; c-basic-offset: 8 -*- * See: * * http://en.wikipedia.org/wiki/Verlet_integration @@ -27,227 +27,227 @@ const double gravity = 50; void object_init (Object *object, double x, double y, double mass) { - object->position.x = x; - object->position.y = y; - object->previous_position.x = x; - object->previous_position.y = y; - object->mass = mass; + object->position.x = x; + object->position.y = y; + object->previous_position.x = x; + object->previous_position.y = y; + object->mass = mass; } void spring_init (Spring *spring, Object *a, Object *b, double length) { - spring->a = a; - spring->b = b; - spring->length = length; + spring->a = a; + spring->b = b; + spring->length = length; } void stick_init (Stick *stick, Object *a, Object *b, double length) { - stick->a = a; - stick->b = b; - stick->length = length; + stick->a = a; + stick->b = b; + stick->length = length; } void string_init (String *string, Object *a, Object *b, double length) { - string->a = a; - string->b = b; - string->length = length; + string->a = a; + string->b = b; + string->length = length; } void offset_spring_init (OffsetSpring *spring, Object *a, Object *b, double dx, double dy) { - spring->a = a; - spring->b = b; - spring->dx = dx; - spring->dy = dy; + spring->a = a; + spring->b = b; + spring->dx = dx; + spring->dy = dy; } void spacer_init (Spacer *spacer, Object *a, Object *b, double length) { - spacer->a = a; - spacer->b = b; - spacer->length = length; + spacer->a = a; + spacer->b = b; + spacer->length = length; } void anchor_init (Anchor *anchor, Object *object, double x, double y) { - anchor->object = object; - anchor->x = x; - anchor->y = y; + anchor->object = object; + anchor->x = x; + anchor->y = y; } void polygon_init (Polygon *p, int enclosing, int num_points, ...) { - double dx, dy, length; - int i, j; - va_list ap; - - /* Polygons are defined counter-clock-wise in a coordinate system - * with the y-axis pointing down. */ - - va_start (ap, num_points); - p->num_points = num_points; - p->points = g_new (Point, num_points); - p->enclosing = enclosing; - - for (i = 0; i < num_points; i++) { - p->points[i].x = va_arg (ap, double); - p->points[i].y = va_arg (ap, double); - } - va_end (ap); + double dx, dy, length; + int i, j; + va_list ap; + + /* Polygons are defined counter-clock-wise in a coordinate system + * with the y-axis pointing down. */ + + va_start (ap, num_points); + p->num_points = num_points; + p->points = g_new (Point, num_points); + p->enclosing = enclosing; + + for (i = 0; i < num_points; i++) { + p->points[i].x = va_arg (ap, double); + p->points[i].y = va_arg (ap, double); + } + va_end (ap); - p->normals = g_new (Vector, p->num_points); - /* Compute outward pointing normals. p->normals[i] is the normal - * for the edged between p->points[i] and p->points[i + 1]. */ - for (i = 0; i < p->num_points; i++) { - j = (i + 1) % p->num_points; - dx = p->points[j].x - p->points[i].x; - dy = p->points[j].y - p->points[i].y; - length = sqrt (dx * dx + dy * dy); - p->normals[i].x = -dy / length; - p->normals[i].y = dx / length; - } + p->normals = g_new (Vector, p->num_points); + /* Compute outward pointing normals. p->normals[i] is the normal + * for the edged between p->points[i] and p->points[i + 1]. */ + for (i = 0; i < p->num_points; i++) { + j = (i + 1) % p->num_points; + dx = p->points[j].x - p->points[i].x; + dy = p->points[j].y - p->points[i].y; + length = sqrt (dx * dx + dy * dy); + p->normals[i].x = -dy / length; + p->normals[i].y = dx / length; + } } void polygon_init_diamond (Polygon *polygon, double x, double y) { - return polygon_init (polygon, FALSE, 5, - x, y, - x + 10, y + 40, - x + 90, y + 40, - x + 100, y, - x + 50, y - 20); + return polygon_init (polygon, FALSE, 5, + x, y, + x + 10, y + 40, + x + 90, y + 40, + x + 100, y, + x + 50, y - 20); } void polygon_init_rectangle (Polygon *polygon, double x0, double y0, double x1, double y1) { - return polygon_init (polygon, FALSE, 4, x0, y0, x0, y1, x1, y1, x1, y0); + return polygon_init (polygon, FALSE, 4, x0, y0, x0, y1, x1, y1, x1, y0); } void polygon_init_enclosing_rectangle (Polygon *polygon, double x0, double y0, - double x1, double y1) + double x1, double y1) { - return polygon_init (polygon, TRUE, 4, x0, y0, x0, y1, x1, y1, x1, y0); + return polygon_init (polygon, TRUE, 4, x0, y0, x0, y1, x1, y1, x1, y0); } void model_fini (Model *model) { - int i; - - g_free (model->objects); - g_free (model->sticks); - g_free (model->strings); - for (i = 0; i < model->num_offsets; i++) - g_free (model->offsets[i].objects); - g_free (model->springs); - g_free (model->offset_springs); - g_free (model->spacers); - for (i = 0; i < model->num_polygons; i++) - g_free (model->polygons[i].points); - g_free (model->polygons); - - memset (model, 0, sizeof *model); + int i; + + g_free (model->objects); + g_free (model->sticks); + g_free (model->strings); + for (i = 0; i < model->num_offsets; i++) + g_free (model->offsets[i].objects); + g_free (model->springs); + g_free (model->offset_springs); + g_free (model->spacers); + for (i = 0; i < model->num_polygons; i++) + g_free (model->polygons[i].points); + g_free (model->polygons); + + memset (model, 0, sizeof *model); } static void model_accumulate_forces (Model *model) { - int i; - double x, y, dx, dy, distance, displacement; - Point middle; - Vector u, v; - - for (i = 0; i < model->num_objects; i++) { - /* Gravity */ - model->objects[i].force.x = 0; - model->objects[i].force.y = gravity * model->objects[i].mass; - - /* Friction */ - v.x = model->objects[i].position.x - model->objects[i].previous_position.x; - v.y = model->objects[i].position.y - model->objects[i].previous_position.y; - model->objects[i].force.x -= v.x * friction; - model->objects[i].force.y -= v.y * friction; - } - - for (i = 0; i < model->num_springs; i++) { - x = model->springs[i].a->position.x; - y = model->springs[i].a->position.y; - dx = model->springs[i].b->position.x - x; - dy = model->springs[i].b->position.y - y; - distance = sqrt (dx * dx + dy * dy); - u.x = dx / distance; - u.y = dy / distance; - displacement = distance - model->springs[i].length; - model->springs[i].a->force.x += u.x * model->k * displacement; - model->springs[i].a->force.y += u.y * model->k * displacement; - model->springs[i].b->force.x -= u.x * model->k * displacement; - model->springs[i].b->force.y -= u.y * model->k * displacement; - } - - for (i = 0; i < model->num_offset_springs; i++) { - middle.x = - (model->offset_springs[i].a->position.x + - model->offset_springs[i].b->position.x) / 2; - middle.y = - (model->offset_springs[i].a->position.y + - model->offset_springs[i].b->position.y) / 2; - - x = middle.x - model->offset_springs[i].dx / 2; - y = middle.y - model->offset_springs[i].dy / 2; - - dx = x - model->offset_springs[i].a->position.x; - dy = y - model->offset_springs[i].a->position.y; - - model->offset_springs[i].a->force.x += dx * model->k; - model->offset_springs[i].a->force.y += dy * model->k; - model->offset_springs[i].b->force.x -= dx * model->k; - model->offset_springs[i].b->force.y -= dy * model->k; - } - - for (i = 0; i < model->num_objects; i++) { - double f = - model->objects[i].force.x * model->objects[i].force.x + - model->objects[i].force.y * model->objects[i].force.y; - - if (f > 100000000) - abort(); - } + int i; + double x, y, dx, dy, distance, displacement; + Point middle; + Vector u, v; + + for (i = 0; i < model->num_objects; i++) { + /* Gravity */ + model->objects[i].force.x = 0; + model->objects[i].force.y = gravity * model->objects[i].mass; + + /* Friction */ + v.x = model->objects[i].position.x - model->objects[i].previous_position.x; + v.y = model->objects[i].position.y - model->objects[i].previous_position.y; + model->objects[i].force.x -= v.x * friction; + model->objects[i].force.y -= v.y * friction; + } + + for (i = 0; i < model->num_springs; i++) { + x = model->springs[i].a->position.x; + y = model->springs[i].a->position.y; + dx = model->springs[i].b->position.x - x; + dy = model->springs[i].b->position.y - y; + distance = sqrt (dx * dx + dy * dy); + u.x = dx / distance; + u.y = dy / distance; + displacement = distance - model->springs[i].length; + model->springs[i].a->force.x += u.x * model->k * displacement; + model->springs[i].a->force.y += u.y * model->k * displacement; + model->springs[i].b->force.x -= u.x * model->k * displacement; + model->springs[i].b->force.y -= u.y * model->k * displacement; + } + + for (i = 0; i < model->num_offset_springs; i++) { + middle.x = + (model->offset_springs[i].a->position.x + + model->offset_springs[i].b->position.x) / 2; + middle.y = + (model->offset_springs[i].a->position.y + + model->offset_springs[i].b->position.y) / 2; + + x = middle.x - model->offset_springs[i].dx / 2; + y = middle.y - model->offset_springs[i].dy / 2; + + dx = x - model->offset_springs[i].a->position.x; + dy = y - model->offset_springs[i].a->position.y; + + model->offset_springs[i].a->force.x += dx * model->k; + model->offset_springs[i].a->force.y += dy * model->k; + model->offset_springs[i].b->force.x -= dx * model->k; + model->offset_springs[i].b->force.y -= dy * model->k; + } + + for (i = 0; i < model->num_objects; i++) { + double f = + model->objects[i].force.x * model->objects[i].force.x + + model->objects[i].force.y * model->objects[i].force.y; + + if (f > 100000000) + abort(); + } } static void model_integrate (Model *model, double step) { - double x, y; - Object *o; - int i; - - for (i = 0; i < model->num_objects; i++) { - o = &model->objects[i]; - x = o->position.x; - y = o->position.y; + double x, y; + Object *o; + int i; + + for (i = 0; i < model->num_objects; i++) { + o = &model->objects[i]; + x = o->position.x; + y = o->position.y; - o->position.x = - x + (x - o->previous_position.x) + o->force.x * step * step; - o->position.y = - y + (y - o->previous_position.y) + o->force.y * step * step; - - o->previous_position.x = x; - o->previous_position.y = y; - } + o->position.x = + x + (x - o->previous_position.x) + o->force.x * step * step; + o->position.y = + y + (y - o->previous_position.y) + o->force.y * step * step; + + o->previous_position.x = x; + o->previous_position.y = y; + } } /* The square root in the distance computation for the string and @@ -267,205 +267,205 @@ static inline double estimate_distance (double dx, double dy, double r) { #ifdef APPROXIMATE_SQUARE_ROOTS - return (r + (dx * dx + dy * dy) / r) / 2; + return (r + (dx * dx + dy * dy) / r) / 2; #else - return sqrt (dx * dx + dy * dy); + return sqrt (dx * dx + dy * dy); #endif } static int polygon_contains_point (Polygon *polygon, Point *point) { - int i; - double dx, dy; + int i; + double dx, dy; - for (i = 0; i < polygon->num_points; i++) { - dx = point->x - polygon->points[i].x; - dy = point->y - polygon->points[i].y; + for (i = 0; i < polygon->num_points; i++) { + dx = point->x - polygon->points[i].x; + dy = point->y - polygon->points[i].y; - if (polygon->normals[i].x * dx + polygon->normals[i].y * dy >= 0) - return polygon->enclosing; - } + if (polygon->normals[i].x * dx + polygon->normals[i].y * dy >= 0) + return polygon->enclosing; + } - return !polygon->enclosing; + return !polygon->enclosing; } static void polygon_reflect_object (Polygon *polygon, Object *object) { - int i, edge; - double d, distance; - Vector *n; - - distance = -1000; - for (i = 0; i < polygon->num_points; i++) { - d = polygon->normals[i].x * (object->position.x - polygon->points[i].x) + - polygon->normals[i].y * (object->position.y - polygon->points[i].y); - - if (d > distance) { - distance = d; - edge = i; - n = &polygon->normals[i]; - } - } - - object->position.x -= (1 + elasticity) * distance * n->x; - object->position.y -= (1 + elasticity) * distance * n->y; - - distance = - n->x * (object->previous_position.x - polygon->points[edge].x) + - n->y * (object->previous_position.y - polygon->points[edge].y); - - object->previous_position.x -= (1 + elasticity) * distance * n->x; - object->previous_position.y -= (1 + elasticity) * distance * n->y; + int i, edge; + double d, distance; + Vector *n; + + distance = -1000; + for (i = 0; i < polygon->num_points; i++) { + d = polygon->normals[i].x * (object->position.x - polygon->points[i].x) + + polygon->normals[i].y * (object->position.y - polygon->points[i].y); + + if (d > distance) { + distance = d; + edge = i; + n = &polygon->normals[i]; + } + } + + object->position.x -= (1 + elasticity) * distance * n->x; + object->position.y -= (1 + elasticity) * distance * n->y; + + distance = + n->x * (object->previous_position.x - polygon->points[edge].x) + + n->y * (object->previous_position.y - polygon->points[edge].y); + + object->previous_position.x -= (1 + elasticity) * distance * n->x; + object->previous_position.y -= (1 + elasticity) * distance * n->y; } static void model_constrain_polygon (Model *model, Polygon *polygon) { - int i; + int i; - for (i = 0; i < model->num_objects; i++) { - if (polygon_contains_point (polygon, &model->objects[i].position)) - polygon_reflect_object (polygon, &model->objects[i]); - } + for (i = 0; i < model->num_objects; i++) { + if (polygon_contains_point (polygon, &model->objects[i].position)) + polygon_reflect_object (polygon, &model->objects[i]); + } } static void model_constrain_anchor (Model *model, Anchor *anchor) { - anchor->object->position.x = anchor->x; - anchor->object->position.y = anchor->y; - anchor->object->previous_position.x = anchor->x; - anchor->object->previous_position.y = anchor->y; + anchor->object->position.x = anchor->x; + anchor->object->position.y = anchor->y; + anchor->object->previous_position.x = anchor->x; + anchor->object->previous_position.y = anchor->y; } static void model_constrain_offset (Model *model, Offset *offset) { - double x, y; - int i; - - x = 0; - y = 0; - for (i = 0; i < offset->num_objects; i++) { - x += offset->objects[i]->position.x; - y += offset->objects[i]->position.y; - } - - x = x / offset->num_objects - offset->dx * (offset->num_objects - 1) / 2; - y = y / offset->num_objects - offset->dy * (offset->num_objects - 1) / 2; + double x, y; + int i; + + x = 0; + y = 0; + for (i = 0; i < offset->num_objects; i++) { + x += offset->objects[i]->position.x; + y += offset->objects[i]->position.y; + } + + x = x / offset->num_objects - offset->dx * (offset->num_objects - 1) / 2; + y = y / offset->num_objects - offset->dy * (offset->num_objects - 1) / 2; - for (i = 0; i < offset->num_objects; i++) { - offset->objects[i]->position.x = x + offset->dx * i; - offset->objects[i]->position.y = y + offset->dy * i; - } + for (i = 0; i < offset->num_objects; i++) { + offset->objects[i]->position.x = x + offset->dx * i; + offset->objects[i]->position.y = y + offset->dy * i; + } } static void model_constrain (Model *model) { - double dx, dy, x, y, distance, fraction; - int i; - - if (model->mouse_anchor.object != NULL) - model_constrain_anchor (model, &model->mouse_anchor); - for (i = 0; i < model->num_anchors; i++) - model_constrain_anchor (model, &model->anchors[i]); - - /* String constraints. */ - for (i = 0; i < model->num_strings; i++) { - x = model->strings[i].a->position.x; - y = model->strings[i].a->position.y; - dx = model->strings[i].b->position.x - x; - dy = model->strings[i].b->position.y - y; - distance = estimate_distance (dx, dy, model->strings[i].length); - if (distance < model->strings[i].length) - continue; - fraction = (distance - model->strings[i].length) / distance / 2; - model->strings[i].a->position.x = x + dx * fraction; - model->strings[i].a->position.y = y + dy * fraction; - model->strings[i].b->position.x = x + dx * (1 - fraction); - model->strings[i].b->position.y = y + dy * (1 - fraction); - } - - /* Spacer constraints. */ - for (i = 0; i < model->num_spacers; i++) { - x = model->spacers[i].a->position.x; - y = model->spacers[i].a->position.y; - dx = model->spacers[i].b->position.x - x; - dy = model->spacers[i].b->position.y - y; - distance = estimate_distance (dx, dy, model->spacers[i].length); - if (distance > model->spacers[i].length) - continue; - fraction = (distance - model->spacers[i].length) / distance / 2; - model->spacers[i].a->position.x = x + dx * fraction; - model->spacers[i].a->position.y = y + dy * fraction; - model->spacers[i].b->position.x = x + dx * (1 - fraction); - model->spacers[i].b->position.y = y + dy * (1 - fraction); - } - - /* Stick constraints. */ - for (i = 0; i < model->num_sticks; i++) { - x = model->sticks[i].a->position.x; - y = model->sticks[i].a->position.y; - dx = model->sticks[i].b->position.x - x; - dy = model->sticks[i].b->position.y - y; - distance = estimate_distance (dx, dy, model->sticks[i].length); - fraction = (distance - model->sticks[i].length) / distance / 2; - model->sticks[i].a->position.x = x + dx * fraction; - model->sticks[i].a->position.y = y + dy * fraction; - model->sticks[i].b->position.x = x + dx * (1 - fraction); - model->sticks[i].b->position.y = y + dy * (1 - fraction); - } - - /* Offset constraints. */ - for (i = 0; i < model->num_offsets; i++) - model_constrain_offset (model, &model->offsets[i]); - - /* Polygon constraints. */ - for (i = 0; i < model->num_polygons; i++) - model_constrain_polygon (model, &model->polygons[i]); + double dx, dy, x, y, distance, fraction; + int i; + + if (model->mouse_anchor.object != NULL) + model_constrain_anchor (model, &model->mouse_anchor); + for (i = 0; i < model->num_anchors; i++) + model_constrain_anchor (model, &model->anchors[i]); + + /* String constraints. */ + for (i = 0; i < model->num_strings; i++) { + x = model->strings[i].a->position.x; + y = model->strings[i].a->position.y; + dx = model->strings[i].b->position.x - x; + dy = model->strings[i].b->position.y - y; + distance = estimate_distance (dx, dy, model->strings[i].length); + if (distance < model->strings[i].length) + continue; + fraction = (distance - model->strings[i].length) / distance / 2; + model->strings[i].a->position.x = x + dx * fraction; + model->strings[i].a->position.y = y + dy * fraction; + model->strings[i].b->position.x = x + dx * (1 - fraction); + model->strings[i].b->position.y = y + dy * (1 - fraction); + } + + /* Spacer constraints. */ + for (i = 0; i < model->num_spacers; i++) { + x = model->spacers[i].a->position.x; + y = model->spacers[i].a->position.y; + dx = model->spacers[i].b->position.x - x; + dy = model->spacers[i].b->position.y - y; + distance = estimate_distance (dx, dy, model->spacers[i].length); + if (distance > model->spacers[i].length) + continue; + fraction = (distance - model->spacers[i].length) / distance / 2; + model->spacers[i].a->position.x = x + dx * fraction; + model->spacers[i].a->position.y = y + dy * fraction; + model->spacers[i].b->position.x = x + dx * (1 - fraction); + model->spacers[i].b->position.y = y + dy * (1 - fraction); + } + + /* Stick constraints. */ + for (i = 0; i < model->num_sticks; i++) { + x = model->sticks[i].a->position.x; + y = model->sticks[i].a->position.y; + dx = model->sticks[i].b->position.x - x; + dy = model->sticks[i].b->position.y - y; + distance = estimate_distance (dx, dy, model->sticks[i].length); + fraction = (distance - model->sticks[i].length) / distance / 2; + model->sticks[i].a->position.x = x + dx * fraction; + model->sticks[i].a->position.y = y + dy * fraction; + model->sticks[i].b->position.x = x + dx * (1 - fraction); + model->sticks[i].b->position.y = y + dy * (1 - fraction); + } + + /* Offset constraints. */ + for (i = 0; i < model->num_offsets; i++) + model_constrain_offset (model, &model->offsets[i]); + + /* Polygon constraints. */ + for (i = 0; i < model->num_polygons; i++) + model_constrain_polygon (model, &model->polygons[i]); } void model_step (Model *model, double delta_t) { - int i; + int i; - model_accumulate_forces (model); - model_integrate (model, delta_t); - for (i = 0; i < 20; i++) - model_constrain (model); + model_accumulate_forces (model); + model_integrate (model, delta_t); + for (i = 0; i < 20; i++) + model_constrain (model); - model->theta += delta_t; + model->theta += delta_t; } static double object_distance (Object *object, double x, double y) { - double dx, dy; + double dx, dy; - dx = object->position.x - x; - dy = object->position.y - y; + dx = object->position.x - x; + dy = object->position.y - y; - return sqrt (dx*dx + dy*dy); + return sqrt (dx*dx + dy*dy); } Object * model_find_nearest (Model *model, double x, double y) { - Object *object; - double distance, min_distance; - int i; - - for (i = 0; i < model->num_objects; i++) { - distance = object_distance (&model->objects[i], x, y); - if (i == 0 || distance < min_distance) { - min_distance = distance; - object = &model->objects[i]; - } - } - - return object; + Object *object; + double distance, min_distance; + int i; + + for (i = 0; i < model->num_objects; i++) { + distance = object_distance (&model->objects[i], x, y); + if (i == 0 || distance < min_distance) { + min_distance = distance; + object = &model->objects[i]; + } + } + + return object; } diff --git a/dock.c b/dock.c index f462406..c90c3d9 100644 --- a/dock.c +++ b/dock.c @@ -1,4 +1,4 @@ -/* -*- mode: c; c-basic-offset: 2 -*- +/* -*- mode: c; c-basic-offset: 8 -*- */ #include @@ -16,220 +16,220 @@ typedef struct Closure Closure; struct Closure { - Model model; - int num_icons; - GdkWindow **windows; - int drag_offset_x, drag_offset_y; - int spacing; - int height; + Model model; + int num_icons; + GdkWindow **windows; + int drag_offset_x, drag_offset_y; + int spacing; + int height; }; static gint timeout_callback (gpointer data) { - Closure *closure = data; - int i; + Closure *closure = data; + int i; - for (i = 0; i < closure->num_icons; i++) { - gdk_window_move (closure->windows[i], - closure->model.objects[i + 1].position.x + 0.5, - closure->model.objects[i + 1].position.y + 0.5); - } + for (i = 0; i < closure->num_icons; i++) { + gdk_window_move (closure->windows[i], + closure->model.objects[i + 1].position.x + 0.5, + closure->model.objects[i + 1].position.y + 0.5); + } - for (i = 0; i < 4; i++) - model_step (&closure->model, 0.03); + for (i = 0; i < 4; i++) + model_step (&closure->model, 0.03); - return TRUE; + return TRUE; } static GdkWindow * create_window (GdkScreen *screen, int x, int y, int width, int height) { - GdkWindowAttr attributes; - gint attributes_mask; - - attributes.wclass = GDK_INPUT_OUTPUT; - attributes.visual = gdk_screen_get_rgba_visual (screen); - attributes.colormap = gdk_screen_get_rgba_colormap (screen); - attributes.window_type = GDK_WINDOW_TEMP; - - attributes.x = x; - attributes.y = y; - attributes.width = width; - attributes.height = height; - attributes.event_mask = - GDK_EXPOSURE_MASK | - GDK_BUTTON_PRESS_MASK | - GDK_BUTTON_RELEASE_MASK | - GDK_ENTER_NOTIFY_MASK | - GDK_LEAVE_NOTIFY_MASK | - GDK_POINTER_MOTION_MASK | - GDK_POINTER_MOTION_HINT_MASK; - - attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP; + GdkWindowAttr attributes; + gint attributes_mask; + + attributes.wclass = GDK_INPUT_OUTPUT; + attributes.visual = gdk_screen_get_rgba_visual (screen); + attributes.colormap = gdk_screen_get_rgba_colormap (screen); + attributes.window_type = GDK_WINDOW_TEMP; + + attributes.x = x; + attributes.y = y; + attributes.width = width; + attributes.height = height; + attributes.event_mask = + GDK_EXPOSURE_MASK | + GDK_BUTTON_PRESS_MASK | + GDK_BUTTON_RELEASE_MASK | + GDK_ENTER_NOTIFY_MASK | + GDK_LEAVE_NOTIFY_MASK | + GDK_POINTER_MOTION_MASK | + GDK_POINTER_MOTION_HINT_MASK; + + attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP; - return gdk_window_new (gdk_screen_get_root_window (screen), - &attributes, attributes_mask); + return gdk_window_new (gdk_screen_get_root_window (screen), + &attributes, attributes_mask); } static void model_init_dock (Model *model, int num_items, int width, int height, int spacing) { - const int num_objects = num_items + 1; - const int num_spacers = (num_objects - 1) * (num_objects - 2) / 2; - const int num_springs = num_objects - 1; - const int spread = spacing + 20; - int i, j, left_edge; - Object *object; - Spring *spring; - Spacer *spacer; - - memset (model, 0, sizeof *model); - model->objects = g_new (Object, num_objects); - model->num_objects = num_objects; - model->springs = g_new (Spring, num_springs); - model->num_springs = num_springs; - model->spacers = g_new (Spacer, num_spacers); - model->num_spacers = num_spacers; - model->anchors = g_new (Anchor, 1); - model->num_anchors = 1; - model->k = 0.1; - - model->polygons = g_new (Polygon, 1); - model->num_polygons = 1; - polygon_init_enclosing_rectangle (&model->polygons[0], - 0, 0, width - 50, height - 50); - - model->anchors[0].x = width / 2; - model->anchors[0].y = height - 50; - model->anchors[0].object = &model->objects[0]; - - object_init (&model->objects[0], - model->anchors[0].x, model->anchors[0].y, 0); - - object = &model->objects[1]; - spring = model->springs; - spacer = model->spacers; - left_edge = (width - (num_items - 1) * spread) / 2; - - for (i = 1; i < num_objects; i++, object++) { - object_init (&model->objects[i], - left_edge + (i - 1) * spread, height - 100, 1); - spring_init (spring++, &model->objects[0], object, spacing); - for (j = 1; j < num_objects - i; j++) { - spacer_init (spacer++, object, object + j, spacing); - } - } + const int num_objects = num_items + 1; + const int num_spacers = (num_objects - 1) * (num_objects - 2) / 2; + const int num_springs = num_objects - 1; + const int spread = spacing + 20; + int i, j, left_edge; + Object *object; + Spring *spring; + Spacer *spacer; + + memset (model, 0, sizeof *model); + model->objects = g_new (Object, num_objects); + model->num_objects = num_objects; + model->springs = g_new (Spring, num_springs); + model->num_springs = num_springs; + model->spacers = g_new (Spacer, num_spacers); + model->num_spacers = num_spacers; + model->anchors = g_new (Anchor, 1); + model->num_anchors = 1; + model->k = 0.1; + + model->polygons = g_new (Polygon, 1); + model->num_polygons = 1; + polygon_init_enclosing_rectangle (&model->polygons[0], + 0, 0, width - 50, height - 50); + + model->anchors[0].x = width / 2; + model->anchors[0].y = height - 50; + model->anchors[0].object = &model->objects[0]; + + object_init (&model->objects[0], + model->anchors[0].x, model->anchors[0].y, 0); + + object = &model->objects[1]; + spring = model->springs; + spacer = model->spacers; + left_edge = (width - (num_items - 1) * spread) / 2; + + for (i = 1; i < num_objects; i++, object++) { + object_init (&model->objects[i], + left_edge + (i - 1) * spread, height - 100, 1); + spring_init (spring++, &model->objects[0], object, spacing); + for (j = 1; j < num_objects - i; j++) { + spacer_init (spacer++, object, object + j, spacing); + } + } } static GdkFilterReturn window_event (GdkXEvent *xevent, GdkEvent *event, gpointer data) { - Closure *closure = data; - GdkModifierType state; - XEvent *ev = (XEvent *) xevent; - int x, y, i; - Object *object; - - switch (ev->type) { - case ButtonPress: - closure->drag_offset_x = ev->xbutton.x; - closure->drag_offset_y = ev->xbutton.y; - for (i = 0; i < closure->num_icons; i++) { - if (closure->windows[i] == event->any.window) { - object = &closure->model.objects[i + 1]; - closure->model.mouse_anchor.x = object->position.x; - closure->model.mouse_anchor.y = object->position.y; - closure->model.mouse_anchor.object = object; - } - } - break; - - case ButtonRelease: - closure->model.mouse_anchor.object = NULL; - break; - - case MotionNotify: - gdk_window_get_pointer (gdk_get_default_root_window(), &x, &y, &state); - closure->model.mouse_anchor.x = x - closure->drag_offset_x; - closure->model.mouse_anchor.y = y - closure->drag_offset_y; - if (closure->model.mouse_anchor.y > closure->height) - closure->model.mouse_anchor.y = closure->height; - break; - - default: - break; - } - - return GDK_FILTER_CONTINUE; + Closure *closure = data; + GdkModifierType state; + XEvent *ev = (XEvent *) xevent; + int x, y, i; + Object *object; + + switch (ev->type) { + case ButtonPress: + closure->drag_offset_x = ev->xbutton.x; + closure->drag_offset_y = ev->xbutton.y; + for (i = 0; i < closure->num_icons; i++) { + if (closure->windows[i] == event->any.window) { + object = &closure->model.objects[i + 1]; + closure->model.mouse_anchor.x = object->position.x; + closure->model.mouse_anchor.y = object->position.y; + closure->model.mouse_anchor.object = object; + } + } + break; + + case ButtonRelease: + closure->model.mouse_anchor.object = NULL; + break; + + case MotionNotify: + gdk_window_get_pointer (gdk_get_default_root_window(), &x, &y, &state); + closure->model.mouse_anchor.x = x - closure->drag_offset_x; + closure->model.mouse_anchor.y = y - closure->drag_offset_y; + if (closure->model.mouse_anchor.y > closure->height) + closure->model.mouse_anchor.y = closure->height; + break; + + default: + break; + } + + return GDK_FILTER_CONTINUE; } static const char *icons[] = { - "svg/applications-office.svg", - "svg/camera-video.svg", - "svg/email.svg", - "svg/firefox-logo.svg", - "svg/gnome-dev-disc-dvdrom.svg", - "svg/gnome-terminal.svg", - "svg/help-browser.svg", - "svg/internet-group-chat.svg" + "svg/applications-office.svg", + "svg/camera-video.svg", + "svg/email.svg", + "svg/firefox-logo.svg", + "svg/gnome-dev-disc-dvdrom.svg", + "svg/gnome-terminal.svg", + "svg/help-browser.svg", + "svg/internet-group-chat.svg" }; int main (int argc, char *argv[]) { - Closure closure; - GdkScreen *screen; - const int num_icons = G_N_ELEMENTS (icons); - int x, y, width, height, i; - RsvgHandle *handle; - RsvgDimensionData dimension; - cairo_t *cr; - const int spacing = 50; + Closure closure; + GdkScreen *screen; + const int num_icons = G_N_ELEMENTS (icons); + int x, y, width, height, i; + RsvgHandle *handle; + RsvgDimensionData dimension; + cairo_t *cr; + const int spacing = 50; - gtk_init (&argc, &argv); + gtk_init (&argc, &argv); - rsvg_init (); + rsvg_init (); - screen = gdk_screen_get_default (); - width = gdk_screen_get_width (screen); - height = gdk_screen_get_height (screen); + screen = gdk_screen_get_default (); + width = gdk_screen_get_width (screen); + height = gdk_screen_get_height (screen); - closure.spacing = spacing; - closure.height = height - 50; - closure.num_icons = num_icons; - closure.windows = g_new (GdkWindow *, num_icons); + closure.spacing = spacing; + closure.height = height - 50; + closure.num_icons = num_icons; + closure.windows = g_new (GdkWindow *, num_icons); - model_init_dock (&closure.model, num_icons, width, height, spacing); + model_init_dock (&closure.model, num_icons, width, height, spacing); - for (i = 0; i < num_icons; i++) { + for (i = 0; i < num_icons; i++) { - handle = rsvg_handle_new_from_file (icons[i], NULL); - rsvg_handle_get_dimensions (handle, &dimension); + handle = rsvg_handle_new_from_file (icons[i], NULL); + rsvg_handle_get_dimensions (handle, &dimension); - x = closure.model.objects[i + 1].position.x; - y = closure.model.objects[i + 1].position.y; - closure.windows[i] = - create_window (screen, x, y, dimension.width, dimension.height); + x = closure.model.objects[i + 1].position.x; + y = closure.model.objects[i + 1].position.y; + closure.windows[i] = + create_window (screen, x, y, dimension.width, dimension.height); - gdk_window_show (closure.windows[i]); + gdk_window_show (closure.windows[i]); - cr = gdk_cairo_create (closure.windows[i]); - cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR); - cairo_paint (cr); - cairo_set_operator (cr, CAIRO_OPERATOR_OVER); - rsvg_handle_render_cairo (handle, cr); - rsvg_handle_free (handle); - cairo_destroy (cr); + cr = gdk_cairo_create (closure.windows[i]); + cairo_set_operator (cr, CAIRO_OPERATOR_CLEAR); + cairo_paint (cr); + cairo_set_operator (cr, CAIRO_OPERATOR_OVER); + rsvg_handle_render_cairo (handle, cr); + rsvg_handle_free (handle); + cairo_destroy (cr); - gdk_window_add_filter (closure.windows[i], window_event, &closure); - } + gdk_window_add_filter (closure.windows[i], window_event, &closure); + } - g_timeout_add (20, timeout_callback, &closure); + g_timeout_add (20, timeout_callback, &closure); - gtk_main (); + gtk_main (); - rsvg_term (); + rsvg_term (); - return 0; + return 0; } diff --git a/main.c b/main.c index 15fbce4..a6d298f 100644 --- a/main.c +++ b/main.c @@ -1,4 +1,4 @@ -/* -*- mode: c; c-basic-offset: 2 -*- +/* -*- mode: c; c-basic-offset: 8 -*- * To compile: * * gcc -Wall -g $(pkg-config --cflags --libs gtk+-2.0 cairo) \ @@ -19,294 +19,294 @@ static void model_init_polygons (Model *model) { - const int num_polygons = 5; - const double ground_level = 500; + const int num_polygons = 5; + const double ground_level = 500; - model->polygons = g_new (Polygon, num_polygons); - polygon_init_diamond (&model->polygons[0], 250, 300); - polygon_init_diamond (&model->polygons[1], 400, 150); - polygon_init_rectangle (&model->polygons[2], -100, 200, 200, 250); - polygon_init_rectangle (&model->polygons[3], -200, ground_level, - 1200, ground_level + 400); + model->polygons = g_new (Polygon, num_polygons); + polygon_init_diamond (&model->polygons[0], 250, 300); + polygon_init_diamond (&model->polygons[1], 400, 150); + polygon_init_rectangle (&model->polygons[2], -100, 200, 200, 250); + polygon_init_rectangle (&model->polygons[3], -200, ground_level, + 1200, ground_level + 400); - polygon_init_rectangle (&model->polygons[4], 300, 320, 400, 350); + polygon_init_rectangle (&model->polygons[4], 300, 320, 400, 350); - model->num_polygons = num_polygons; + model->num_polygons = num_polygons; } static void model_init_snake (Model *model) { - const int num_objects = 20; - const int num_sticks = num_objects * 2 - 3; - int i; - - memset (model, 0, sizeof *model); - model->objects = g_new (Object, num_objects); - model->num_objects = num_objects; - model->sticks = g_new (Stick, num_sticks); - model->num_sticks = num_sticks; - model_init_polygons (model); - - for (i = 0; i < num_objects; i++) { - object_init (&model->objects[i], - random() % 200 + 20, random() % 200 + 20, 1); - - if (i + 1 < num_objects) { - model->sticks[i * 2].a = &model->objects[i]; - model->sticks[i * 2].b = &model->objects[i + 1]; - model->sticks[i * 2].length = random() % 20 + 20; - } - if (i + 2 < num_objects) { - model->sticks[i * 2 + 1].a = &model->objects[i]; - model->sticks[i * 2 + 1].b = &model->objects[i + 2]; - model->sticks[i * 2 + 1].length = random() % 20 + 20; - } - } + const int num_objects = 20; + const int num_sticks = num_objects * 2 - 3; + int i; + + memset (model, 0, sizeof *model); + model->objects = g_new (Object, num_objects); + model->num_objects = num_objects; + model->sticks = g_new (Stick, num_sticks); + model->num_sticks = num_sticks; + model_init_polygons (model); + + for (i = 0; i < num_objects; i++) { + object_init (&model->objects[i], + random() % 200 + 20, random() % 200 + 20, 1); + + if (i + 1 < num_objects) { + model->sticks[i * 2].a = &model->objects[i]; + model->sticks[i * 2].b = &model->objects[i + 1]; + model->sticks[i * 2].length = random() % 20 + 20; + } + if (i + 2 < num_objects) { + model->sticks[i * 2 + 1].a = &model->objects[i]; + model->sticks[i * 2 + 1].b = &model->objects[i + 2]; + model->sticks[i * 2 + 1].length = random() % 20 + 20; + } + } } static void model_init_rope (Model *model) { - const int num_objects = 20; - const int num_sticks = num_objects - 1; - const int stick_length = 10; - int i; - - memset (model, 0, sizeof *model); - model->objects = g_new (Object, num_objects); - model->num_objects = num_objects; - model->sticks = g_new (Stick, num_sticks); - model->num_sticks = num_sticks; - model_init_polygons (model); - - for (i = 0; i < num_objects; i++) { - object_init (&model->objects[i], 200, 40 + i * stick_length, 1); - - if (i + 1 < num_objects) { - model->sticks[i].a = &model->objects[i]; - model->sticks[i].b = &model->objects[i + 1]; - model->sticks[i].length = stick_length; - } - } + const int num_objects = 20; + const int num_sticks = num_objects - 1; + const int stick_length = 10; + int i; + + memset (model, 0, sizeof *model); + model->objects = g_new (Object, num_objects); + model->num_objects = num_objects; + model->sticks = g_new (Stick, num_sticks); + model->num_sticks = num_sticks; + model_init_polygons (model); + + for (i = 0; i < num_objects; i++) { + object_init (&model->objects[i], 200, 40 + i * stick_length, 1); + + if (i + 1 < num_objects) { + model->sticks[i].a = &model->objects[i]; + model->sticks[i].b = &model->objects[i + 1]; + model->sticks[i].length = stick_length; + } + } } static void model_init_curtain (Model *model) { - const int num_ropes = 5; - const int num_rope_objects = 15; - const int num_objects = num_ropes * num_rope_objects; - const int num_sticks = num_ropes * (num_rope_objects - 1); - const int stick_length = 10; - const int rope_offset = 30; - double x, y; - int i, j, index, stick_index; - - memset (model, 0, sizeof *model); - model->objects = g_new (Object, num_objects); - model->num_objects = num_objects; - model->sticks = g_new (Stick, num_sticks); - model->num_sticks = num_sticks; - model->offsets = g_new (Offset, 1); - model->num_offsets = 1; - model_init_polygons (model); - - model->offsets[0].num_objects = num_ropes; - model->offsets[0].objects = g_new (Object *, num_ropes); - model->offsets[0].dx = rope_offset; - model->offsets[0].dy = 0; - - for (i = 0; i < num_ropes; i++) { - for (j = 0; j < num_rope_objects; j++) { - x = 200 + i * rope_offset; - y = 40 + j * stick_length; - index = i * num_rope_objects + j; - object_init (&model->objects[index], x, y, 1); - - if (j + 1 < num_rope_objects) { - stick_index = i * (num_rope_objects - 1) + j; - model->sticks[stick_index].a = &model->objects[index]; - model->sticks[stick_index].b = &model->objects[index + 1]; - model->sticks[stick_index].length = stick_length; - } - } - - model->offsets[0].objects[i] = &model->objects[i * num_rope_objects]; - } + const int num_ropes = 5; + const int num_rope_objects = 15; + const int num_objects = num_ropes * num_rope_objects; + const int num_sticks = num_ropes * (num_rope_objects - 1); + const int stick_length = 10; + const int rope_offset = 30; + double x, y; + int i, j, index, stick_index; + + memset (model, 0, sizeof *model); + model->objects = g_new (Object, num_objects); + model->num_objects = num_objects; + model->sticks = g_new (Stick, num_sticks); + model->num_sticks = num_sticks; + model->offsets = g_new (Offset, 1); + model->num_offsets = 1; + model_init_polygons (model); + + model->offsets[0].num_objects = num_ropes; + model->offsets[0].objects = g_new (Object *, num_ropes); + model->offsets[0].dx = rope_offset; + model->offsets[0].dy = 0; + + for (i = 0; i < num_ropes; i++) { + for (j = 0; j < num_rope_objects; j++) { + x = 200 + i * rope_offset; + y = 40 + j * stick_length; + index = i * num_rope_objects + j; + object_init (&model->objects[index], x, y, 1); + + if (j + 1 < num_rope_objects) { + stick_index = i * (num_rope_objects - 1) + j; + model->sticks[stick_index].a = &model->objects[index]; + model->sticks[stick_index].b = &model->objects[index + 1]; + model->sticks[stick_index].length = stick_length; + } + } + + model->offsets[0].objects[i] = &model->objects[i * num_rope_objects]; + } } static void model_init_grid (Model *model) { - const int num_ropes = 8; - const int num_rope_objects = 8; - const int num_objects = num_ropes * num_rope_objects; - const int num_strings = num_ropes * (num_rope_objects - 1) + - (num_ropes - 1) * num_rope_objects; - const int string_length = 20; - const int rope_offset = 20; - Object *object; - String *string; - int i, j; - - memset (model, 0, sizeof *model); - model->objects = g_new (Object, num_objects); - model->num_objects = num_objects; - model->strings = g_new (String, num_strings); - model->num_strings = num_strings; - model->offsets = g_new (Offset, 1); - model->num_offsets = 1; - model_init_polygons (model); - - model->offsets[0].num_objects = num_ropes; - model->offsets[0].objects = g_new (Object *, num_ropes); - model->offsets[0].dx = rope_offset; - model->offsets[0].dy = 0; - - object = model->objects; - string = model->strings; - for (i = 0; i < num_ropes; i++) { - for (j = 0; j < num_rope_objects; j++) { - object_init (object, 200 + i * rope_offset, 40 + j * string_length, 1); - - if (i + 1 < num_ropes) - string_init (string++, - object, object + num_rope_objects, string_length); - if (j + 1 < num_rope_objects) - string_init (string++, object, object + 1, string_length); - object++; - } - - model->offsets[0].objects[i] = &model->objects[i * num_rope_objects]; - } + const int num_ropes = 8; + const int num_rope_objects = 8; + const int num_objects = num_ropes * num_rope_objects; + const int num_strings = num_ropes * (num_rope_objects - 1) + + (num_ropes - 1) * num_rope_objects; + const int string_length = 20; + const int rope_offset = 20; + Object *object; + String *string; + int i, j; + + memset (model, 0, sizeof *model); + model->objects = g_new (Object, num_objects); + model->num_objects = num_objects; + model->strings = g_new (String, num_strings); + model->num_strings = num_strings; + model->offsets = g_new (Offset, 1); + model->num_offsets = 1; + model_init_polygons (model); + + model->offsets[0].num_objects = num_ropes; + model->offsets[0].objects = g_new (Object *, num_ropes); + model->offsets[0].dx = rope_offset; + model->offsets[0].dy = 0; + + object = model->objects; + string = model->strings; + for (i = 0; i < num_ropes; i++) { + for (j = 0; j < num_rope_objects; j++) { + object_init (object, 200 + i * rope_offset, 40 + j * string_length, 1); + + if (i + 1 < num_ropes) + string_init (string++, + object, object + num_rope_objects, string_length); + if (j + 1 < num_rope_objects) + string_init (string++, object, object + 1, string_length); + object++; + } + + model->offsets[0].objects[i] = &model->objects[i * num_rope_objects]; + } } static void model_init_molecule (Model *model) { - const int num_objects = 8; - const int num_springs = num_objects * 2; - const int spring_length = 50; - int i; - Spring *spring; - - memset (model, 0, sizeof *model); - model->objects = g_new (Object, num_objects); - model->num_objects = num_objects; - model->springs = g_new (Spring, num_springs); - model->num_springs = num_springs; - model->k = 2; - - for (i = 0; i < num_objects; i++) - object_init (&model->objects[i], 200 + i * 20, 200, 0); - - spring = model->springs; - for (i = 0; i < num_objects; i++) { - spring_init (spring++, &model->objects[i], - &model->objects[(i + 1) % num_objects], - spring_length); - spring_init (spring++, &model->objects[i], - &model->objects[(i + 2) % num_objects], - spring_length); - } + const int num_objects = 8; + const int num_springs = num_objects * 2; + const int spring_length = 50; + int i; + Spring *spring; + + memset (model, 0, sizeof *model); + model->objects = g_new (Object, num_objects); + model->num_objects = num_objects; + model->springs = g_new (Spring, num_springs); + model->num_springs = num_springs; + model->k = 2; + + for (i = 0; i < num_objects; i++) + object_init (&model->objects[i], 200 + i * 20, 200, 0); + + spring = model->springs; + for (i = 0; i < num_objects; i++) { + spring_init (spring++, &model->objects[i], + &model->objects[(i + 1) % num_objects], + spring_length); + spring_init (spring++, &model->objects[i], + &model->objects[(i + 2) % num_objects], + spring_length); + } } static void model_init_wobbly (Model *model) { - const int width = 8, height = 8; - const int num_objects = width * height; - const int num_offset_springs = (width - 1) * height + width * (height - 1); - const int distance = 30; - double x, y; - int i, j; - Object *object; - OffsetSpring *spring; - - memset (model, 0, sizeof *model); - model->objects = g_new (Object, num_objects); - model->num_objects = num_objects; - model->offset_springs = g_new (OffsetSpring, num_offset_springs); - model->num_offset_springs = num_offset_springs; - model->k = 4.5; - - model_init_polygons (model); - - object = model->objects; - spring = model->offset_springs; - for (i = 0; i < width; i++) { - for (j = 0; j < height; j++) { - x = 200 + i * distance; - y = 40 + j * distance; - object_init (object, x, y, 0); - - if (i + 1 < width) - offset_spring_init (spring++, object, object + height, distance, 0); + const int width = 8, height = 8; + const int num_objects = width * height; + const int num_offset_springs = (width - 1) * height + width * (height - 1); + const int distance = 30; + double x, y; + int i, j; + Object *object; + OffsetSpring *spring; + + memset (model, 0, sizeof *model); + model->objects = g_new (Object, num_objects); + model->num_objects = num_objects; + model->offset_springs = g_new (OffsetSpring, num_offset_springs); + model->num_offset_springs = num_offset_springs; + model->k = 4.5; + + model_init_polygons (model); + + object = model->objects; + spring = model->offset_springs; + for (i = 0; i < width; i++) { + for (j = 0; j < height; j++) { + x = 200 + i * distance; + y = 40 + j * distance; + object_init (object, x, y, 0); + + if (i + 1 < width) + offset_spring_init (spring++, object, object + height, distance, 0); - if (j + 1 < height) - offset_spring_init (spring++, object, object + 1, 0, distance); + if (j + 1 < height) + offset_spring_init (spring++, object, object + 1, 0, distance); - object++; - } - } + object++; + } + } } static void model_init_dock (Model *model) { - const int num_objects = 8; - const int num_spacers = (num_objects - 1) * (num_objects - 2) / 2; - const int num_springs = num_objects - 1; - const int distance = 30; - double x, y; - int i, j; - Object *object; - Spring *spring; - Spacer *spacer; - - memset (model, 0, sizeof *model); - model->objects = g_new (Object, num_objects); - model->num_objects = num_objects; - model->springs = g_new (Spring, num_springs); - model->num_springs = num_springs; - model->spacers = g_new (Spacer, num_spacers); - model->num_spacers = num_spacers; - model->anchors = g_new (Anchor, 1); - model->num_anchors = 1; - model->k = 0.1; - - model->polygons = g_new (Polygon, 1); - model->num_polygons = 1; - polygon_init_enclosing_rectangle (&model->polygons[0], 10, 10, 700, 500); - - model->anchors[0].x = 300; - model->anchors[0].y = 300; - model->anchors[0].object = &model->objects[0]; - - object = model->objects; - spring = model->springs; - spacer = model->spacers; - for (i = 0; i < num_objects; i++, object++) { - x = 200 + i * distance / 3; - y = 40; - object_init (object, x, y, 1); - if (i == 0) - continue; - spring_init (spring++, - &model->objects[0], - &model->objects[i], - distance); - for (j = 1; j < num_objects - i; j++) { - spacer_init (spacer++, object, object + j, distance); - } - } + const int num_objects = 8; + const int num_spacers = (num_objects - 1) * (num_objects - 2) / 2; + const int num_springs = num_objects - 1; + const int distance = 30; + double x, y; + int i, j; + Object *object; + Spring *spring; + Spacer *spacer; + + memset (model, 0, sizeof *model); + model->objects = g_new (Object, num_objects); + model->num_objects = num_objects; + model->springs = g_new (Spring, num_springs); + model->num_springs = num_springs; + model->spacers = g_new (Spacer, num_spacers); + model->num_spacers = num_spacers; + model->anchors = g_new (Anchor, 1); + model->num_anchors = 1; + model->k = 0.1; + + model->polygons = g_new (Polygon, 1); + model->num_polygons = 1; + polygon_init_enclosing_rectangle (&model->polygons[0], 10, 10, 700, 500); + + model->anchors[0].x = 300; + model->anchors[0].y = 300; + model->anchors[0].object = &model->objects[0]; + + object = model->objects; + spring = model->springs; + spacer = model->spacers; + for (i = 0; i < num_objects; i++, object++) { + x = 200 + i * distance / 3; + y = 40; + object_init (object, x, y, 1); + if (i == 0) + continue; + spring_init (spring++, + &model->objects[0], + &model->objects[i], + distance); + for (j = 1; j < num_objects - i; j++) { + spacer_init (spacer++, object, object + j, distance); + } + } } typedef struct _Color Color; struct _Color { - double red, green, blue; + double red, green, blue; }; static void @@ -314,24 +314,24 @@ draw_sticks (cairo_t *cr, Model *model, Color *color) { - int i; - - cairo_set_source_rgba (cr, color->red, color->green, color->blue, 1); - cairo_new_path (cr); - cairo_set_line_width (cr, 2); - cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND); - cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); - - for (i = 0; i < model->num_sticks; i++) { - cairo_move_to (cr, - model->sticks[i].a->position.x, - model->sticks[i].a->position.y); - cairo_line_to (cr, - model->sticks[i].b->position.x, - model->sticks[i].b->position.y); - } - - cairo_stroke (cr); + int i; + + cairo_set_source_rgba (cr, color->red, color->green, color->blue, 1); + cairo_new_path (cr); + cairo_set_line_width (cr, 2); + cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND); + cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); + + for (i = 0; i < model->num_sticks; i++) { + cairo_move_to (cr, + model->sticks[i].a->position.x, + model->sticks[i].a->position.y); + cairo_line_to (cr, + model->sticks[i].b->position.x, + model->sticks[i].b->position.y); + } + + cairo_stroke (cr); } static void @@ -339,24 +339,24 @@ draw_strings (cairo_t *cr, Model *model, Color *color) { - int i; - - cairo_set_source_rgba (cr, color->red, color->green, color->blue, 1); - cairo_new_path (cr); - cairo_set_line_width (cr, 1); - cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND); - cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); - - for (i = 0; i < model->num_strings; i++) { - cairo_move_to (cr, - model->strings[i].a->position.x, - model->strings[i].a->position.y); - cairo_line_to (cr, - model->strings[i].b->position.x, - model->strings[i].b->position.y); - } - - cairo_stroke (cr); + int i; + + cairo_set_source_rgba (cr, color->red, color->green, color->blue, 1); + cairo_new_path (cr); + cairo_set_line_width (cr, 1); + cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND); + cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); + + for (i = 0; i < model->num_strings; i++) { + cairo_move_to (cr, + model->strings[i].a->position.x, + model->strings[i].a->position.y); + cairo_line_to (cr, + model->strings[i].b->position.x, + model->strings[i].b->position.y); + } + + cairo_stroke (cr); } static void @@ -364,22 +364,22 @@ draw_offsets (cairo_t *cr, Model *model, Color *color) { - int i, j; - - cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.5); - cairo_new_path (cr); - cairo_set_line_width (cr, 4); - cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND); - cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); - - for (i = 0; i < model->num_offsets; i++) { - for (j = 0; j < model->offsets[i].num_objects; j++) { - cairo_line_to (cr, - model->offsets[i].objects[j]->position.x, - model->offsets[i].objects[j]->position.y); - } - cairo_stroke (cr); - } + int i, j; + + cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.5); + cairo_new_path (cr); + cairo_set_line_width (cr, 4); + cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND); + cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); + + for (i = 0; i < model->num_offsets; i++) { + for (j = 0; j < model->offsets[i].num_objects; j++) { + cairo_line_to (cr, + model->offsets[i].objects[j]->position.x, + model->offsets[i].objects[j]->position.y); + } + cairo_stroke (cr); + } } @@ -388,24 +388,24 @@ draw_springs (cairo_t *cr, Model *model, Color *color) { - int i; - - cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4); - cairo_new_path (cr); - cairo_set_line_width (cr, 2); - cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND); - cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); - - for (i = 0; i < model->num_springs; i++) { - cairo_move_to (cr, - model->springs[i].a->position.x, - model->springs[i].a->position.y); - cairo_line_to (cr, - model->springs[i].b->position.x, - model->springs[i].b->position.y); - } - - cairo_stroke (cr); + int i; + + cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4); + cairo_new_path (cr); + cairo_set_line_width (cr, 2); + cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND); + cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); + + for (i = 0; i < model->num_springs; i++) { + cairo_move_to (cr, + model->springs[i].a->position.x, + model->springs[i].a->position.y); + cairo_line_to (cr, + model->springs[i].b->position.x, + model->springs[i].b->position.y); + } + + cairo_stroke (cr); } static void @@ -413,65 +413,65 @@ draw_offset_springs (cairo_t *cr, Model *model, Color *color) { - int i; - - cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4); - cairo_new_path (cr); - cairo_set_line_width (cr, 2); - cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND); - cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); - - for (i = 0; i < model->num_offset_springs; i++) { - cairo_move_to (cr, - model->offset_springs[i].a->position.x, - model->offset_springs[i].a->position.y); - cairo_line_to (cr, - model->offset_springs[i].b->position.x, - model->offset_springs[i].b->position.y); - } - - cairo_stroke (cr); + int i; + + cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4); + cairo_new_path (cr); + cairo_set_line_width (cr, 2); + cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND); + cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); + + for (i = 0; i < model->num_offset_springs; i++) { + cairo_move_to (cr, + model->offset_springs[i].a->position.x, + model->offset_springs[i].a->position.y); + cairo_line_to (cr, + model->offset_springs[i].b->position.x, + model->offset_springs[i].b->position.y); + } + + cairo_stroke (cr); } static void draw_polygons (cairo_t *cr, Model *model, Color *color) { - Polygon *p; - int i, j; + Polygon *p; + int i, j; - for (i = 0; i < model->num_polygons; i++) { - p = &model->polygons[i]; + for (i = 0; i < model->num_polygons; i++) { + p = &model->polygons[i]; - for (j = 0; j < p->num_points; j++) - cairo_line_to (cr, p->points[j].x, p->points[j].y); - cairo_close_path (cr); - - if (p->enclosing) { - cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.1); - cairo_fill_preserve (cr); - } - - cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4); - if (p->enclosing) - cairo_stroke (cr); - else - cairo_fill (cr); - } + for (j = 0; j < p->num_points; j++) + cairo_line_to (cr, p->points[j].x, p->points[j].y); + cairo_close_path (cr); + + if (p->enclosing) { + cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.1); + cairo_fill_preserve (cr); + } + + cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4); + if (p->enclosing) + cairo_stroke (cr); + else + cairo_fill (cr); + } } static void draw_objects (cairo_t *cr, Model *model, Color *color) { - int i; - - cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4); - for (i = 0; i < model->num_objects; i++) { - cairo_arc (cr, model->objects[i].position.x, - model->objects[i].position.y, - 3, 0, 2*M_PI); - cairo_fill (cr); - } + int i; + + cairo_set_source_rgba (cr, color->red, color->green, color->blue, 0.4); + for (i = 0; i < model->num_objects; i++) { + cairo_arc (cr, model->objects[i].position.x, + model->objects[i].position.y, + 3, 0, 2*M_PI); + cairo_fill (cr); + } } static Color blue = { 0, 0, 1 }; @@ -481,33 +481,33 @@ static Color black = { 0, 0, 0 }; typedef struct _Closure Closure; struct _Closure { - GtkWidget *drawing_area; - GtkWidget *fps_label; - Model *model; - int frame_count; - int i; - struct timeval start; + GtkWidget *drawing_area; + GtkWidget *fps_label; + Model *model; + int frame_count; + int i; + struct timeval start; }; static void draw_model (GtkWidget *widget, Model *model) { - cairo_t *cr; + cairo_t *cr; - cr = gdk_cairo_create (widget->window); + cr = gdk_cairo_create (widget->window); - cairo_set_source_rgb (cr, 1, 1, 1); - cairo_paint (cr); + cairo_set_source_rgb (cr, 1, 1, 1); + cairo_paint (cr); - draw_polygons (cr, model, &blue); - draw_sticks (cr, model, &black); - draw_strings (cr, model, &green); - draw_springs (cr, model, &black); - draw_offsets (cr, model, &blue); - draw_offset_springs (cr, model, &blue); - draw_objects (cr, model, &red); + draw_polygons (cr, model, &blue); + draw_sticks (cr, model, &black); + draw_strings (cr, model, &green); + draw_springs (cr, model, &black); + draw_offsets (cr, model, &blue); + draw_offset_springs (cr, model, &blue); + draw_objects (cr, model, &red); - cairo_destroy (cr); + cairo_destroy (cr); } static gboolean @@ -515,11 +515,11 @@ expose_event (GtkWidget *widget, GdkEventExpose *event, gpointer data) { - Closure *closure = data; + Closure *closure = data; - draw_model (widget, closure->model); + draw_model (widget, closure->model); - return TRUE; + return TRUE; } static gboolean @@ -527,17 +527,17 @@ button_press_event (GtkWidget *widget, GdkEventButton *event, gpointer data) { - Closure *closure = data; + Closure *closure = data; - if (event->button != 1) - return TRUE; + if (event->button != 1) + return TRUE; - closure->model->mouse_anchor.x = event->x; - closure->model->mouse_anchor.y = event->y; - closure->model->mouse_anchor.object = - model_find_nearest (closure->model, event->x, event->y); + closure->model->mouse_anchor.x = event->x; + closure->model->mouse_anchor.y = event->y; + closure->model->mouse_anchor.object = + model_find_nearest (closure->model, event->x, event->y); - return TRUE; + return TRUE; } static gboolean @@ -545,14 +545,14 @@ button_release_event (GtkWidget *widget, GdkEventButton *event, gpointer data) { - Closure *closure = data; + Closure *closure = data; - if ((event->state & GDK_BUTTON1_MASK) == 0) - return TRUE; + if ((event->state & GDK_BUTTON1_MASK) == 0) + return TRUE; - closure->model->mouse_anchor.object = NULL; + closure->model->mouse_anchor.object = NULL; - return TRUE; + return TRUE; } static gboolean @@ -560,16 +560,16 @@ motion_notify_event (GtkWidget *widget, GdkEventMotion *event, gpointer data) { - Closure *closure = data; - int x, y; - GdkModifierType state; + Closure *closure = data; + int x, y; + GdkModifierType state; - gdk_window_get_pointer (event->window, &x, &y, &state); + gdk_window_get_pointer (event->window, &x, &y, &state); - closure->model->mouse_anchor.x = x + 0.5; - closure->model->mouse_anchor.y = y + 0.5; + closure->model->mouse_anchor.x = x + 0.5; + closure->model->mouse_anchor.y = y + 0.5; - return TRUE; + return TRUE; } typedef void (*ModelInitFunc) (Model *model); @@ -577,213 +577,213 @@ typedef void (*ModelInitFunc) (Model *model); static void model_changed (GtkComboBox *combo, gpointer user_data) { - Closure *closure = user_data; - GtkTreeIter iter; - GtkTreeModel *tree_model; - ModelInitFunc init; - char *name; + Closure *closure = user_data; + GtkTreeIter iter; + GtkTreeModel *tree_model; + ModelInitFunc init; + char *name; - tree_model = gtk_combo_box_get_model (combo); - if (!gtk_combo_box_get_active_iter (combo, &iter)) - return; + tree_model = gtk_combo_box_get_model (combo); + if (!gtk_combo_box_get_active_iter (combo, &iter)) + return; - gtk_tree_model_get (tree_model, &iter, 0, &name, 1, &init, -1); + gtk_tree_model_get (tree_model, &iter, 0, &name, 1, &init, -1); - model_fini (closure->model); - (*init) (closure->model); + model_fini (closure->model); + (*init) (closure->model); } static GtkTreeModel * create_model_store (void) { - static struct { - const char *name; - ModelInitFunc init; - } models[] = { - { "Rope", model_init_rope }, - { "Snake", model_init_snake }, - { "Curtain", model_init_curtain }, - { "Grid", model_init_grid }, - { "Molecule", model_init_molecule }, - { "Wobbly", model_init_wobbly }, - { "Dock", model_init_dock } - }; - - GtkTreeIter iter; - GtkTreeStore *store; - gint i; - - store = gtk_tree_store_new (2, G_TYPE_STRING, G_TYPE_POINTER); - - for (i = 0; i < G_N_ELEMENTS(models); i++) { - gtk_tree_store_append (store, &iter, NULL); - gtk_tree_store_set (store, &iter, - 0, models[i].name, 1, models[i].init, -1); - } + static struct { + const char *name; + ModelInitFunc init; + } models[] = { + { "Rope", model_init_rope }, + { "Snake", model_init_snake }, + { "Curtain", model_init_curtain }, + { "Grid", model_init_grid }, + { "Molecule", model_init_molecule }, + { "Wobbly", model_init_wobbly }, + { "Dock", model_init_dock } + }; + + GtkTreeIter iter; + GtkTreeStore *store; + gint i; + + store = gtk_tree_store_new (2, G_TYPE_STRING, G_TYPE_POINTER); + + for (i = 0; i < G_N_ELEMENTS(models); i++) { + gtk_tree_store_append (store, &iter, NULL); + gtk_tree_store_set (store, &iter, + 0, models[i].name, 1, models[i].init, -1); + } - return GTK_TREE_MODEL (store); + return GTK_TREE_MODEL (store); } static GtkWidget * create_model_combo (Closure *closure) { - GtkWidget *hbox; - GtkWidget *combo, *label; - GtkTreeModel *store; - GtkCellRenderer *renderer; + GtkWidget *hbox; + GtkWidget *combo, *label; + GtkTreeModel *store; + GtkCellRenderer *renderer; - hbox = gtk_hbox_new (FALSE, 8); + hbox = gtk_hbox_new (FALSE, 8); - label = gtk_label_new_with_mnemonic ("_Model:"); - gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0); + label = gtk_label_new_with_mnemonic ("_Model:"); + gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0); - store = create_model_store (); - combo = gtk_combo_box_new_with_model (store); - gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0); - g_object_unref (store); + store = create_model_store (); + combo = gtk_combo_box_new_with_model (store); + gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0); + g_object_unref (store); - renderer = gtk_cell_renderer_text_new (); - gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE); - gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer, - "text", 0, - NULL); + renderer = gtk_cell_renderer_text_new (); + gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), renderer, TRUE); + gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), renderer, + "text", 0, + NULL); - gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo); - gtk_box_pack_start (GTK_BOX (hbox), combo, FALSE, FALSE, 0); - g_signal_connect (combo, "changed", - G_CALLBACK (model_changed), closure); + gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo); + gtk_box_pack_start (GTK_BOX (hbox), combo, FALSE, FALSE, 0); + g_signal_connect (combo, "changed", + G_CALLBACK (model_changed), closure); - label = gtk_label_new ("Frames per second: 0"); - gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0); + label = gtk_label_new ("Frames per second: 0"); + gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0); - closure->fps_label = label; + closure->fps_label = label; - return hbox; + return hbox; } static void create_window (Closure *closure) { - GtkWidget *window; - GtkWidget *frame; - GtkWidget *vbox; - GtkWidget *da; - GtkWidget *model_combo; + GtkWidget *window; + GtkWidget *frame; + GtkWidget *vbox; + GtkWidget *da; + GtkWidget *model_combo; - window = gtk_window_new (GTK_WINDOW_TOPLEVEL); - gtk_window_set_title (GTK_WINDOW (window), "赤丸"); + window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_window_set_title (GTK_WINDOW (window), "赤丸"); - g_signal_connect (window, "destroy", - G_CALLBACK (gtk_main_quit), &window); + g_signal_connect (window, "destroy", + G_CALLBACK (gtk_main_quit), &window); - gtk_container_set_border_width (GTK_CONTAINER (window), 8); + gtk_container_set_border_width (GTK_CONTAINER (window), 8); - vbox = gtk_vbox_new (FALSE, 8); - gtk_container_set_border_width (GTK_CONTAINER (vbox), 8); - gtk_container_add (GTK_CONTAINER (window), vbox); + vbox = gtk_vbox_new (FALSE, 8); + gtk_container_set_border_width (GTK_CONTAINER (vbox), 8); + gtk_container_add (GTK_CONTAINER (window), vbox); - /* - * Create the drawing area - */ + /* + * Create the drawing area + */ - frame = gtk_frame_new (NULL); - gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN); - gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0); + frame = gtk_frame_new (NULL); + gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_IN); + gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0); - da = gtk_drawing_area_new (); - /* set a minimum size */ - gtk_widget_set_size_request (da, 200, 200); + da = gtk_drawing_area_new (); + /* set a minimum size */ + gtk_widget_set_size_request (da, 200, 200); - gtk_container_add (GTK_CONTAINER (frame), da); + gtk_container_add (GTK_CONTAINER (frame), da); - /* Signals used to handle backing pixmap */ + /* Signals used to handle backing pixmap */ - g_signal_connect (da, "expose_event", - G_CALLBACK (expose_event), closure); + g_signal_connect (da, "expose_event", + G_CALLBACK (expose_event), closure); - /* Event signals */ + /* Event signals */ - g_signal_connect (da, "motion_notify_event", - G_CALLBACK (motion_notify_event), closure); - g_signal_connect (da, "button_press_event", - G_CALLBACK (button_press_event), closure); - g_signal_connect (da, "button_release_event", - G_CALLBACK (button_release_event), closure); - - /* Ask to receive events the drawing area doesn't normally - * subscribe to - */ - gtk_widget_set_events (da, gtk_widget_get_events (da) - | GDK_LEAVE_NOTIFY_MASK - | GDK_BUTTON_PRESS_MASK - | GDK_BUTTON_RELEASE_MASK - | GDK_POINTER_MOTION_MASK - | GDK_POINTER_MOTION_HINT_MASK); - - model_combo = create_model_combo (closure); - gtk_box_pack_start (GTK_BOX (vbox), model_combo, FALSE, FALSE, 0); - - closure->drawing_area = da; + g_signal_connect (da, "motion_notify_event", + G_CALLBACK (motion_notify_event), closure); + g_signal_connect (da, "button_press_event", + G_CALLBACK (button_press_event), closure); + g_signal_connect (da, "button_release_event", + G_CALLBACK (button_release_event), closure); + + /* Ask to receive events the drawing area doesn't normally + * subscribe to + */ + gtk_widget_set_events (da, gtk_widget_get_events (da) + | GDK_LEAVE_NOTIFY_MASK + | GDK_BUTTON_PRESS_MASK + | GDK_BUTTON_RELEASE_MASK + | GDK_POINTER_MOTION_MASK + | GDK_POINTER_MOTION_HINT_MASK); + + model_combo = create_model_combo (closure); + gtk_box_pack_start (GTK_BOX (vbox), model_combo, FALSE, FALSE, 0); + + closure->drawing_area = da; } static gint timeout_callback (gpointer data) { - Closure *closure = data; - - model_step (closure->model, 0.2); - - closure->i++; - if (closure->i == 1) { - gtk_widget_queue_draw (closure->drawing_area); - closure->i = 0; - closure->frame_count++; - } - - if (closure->frame_count == 200) { - struct timeval end, elapsed; - double total; - char text[50]; - - closure->frame_count = 0; - gettimeofday (&end, NULL); - if (closure->start.tv_usec > end.tv_usec) { - end.tv_usec += 1000000; - end.tv_sec--; - } - - elapsed.tv_usec = end.tv_usec - closure->start.tv_usec; - elapsed.tv_sec = end.tv_sec - closure->start.tv_sec; - - total = elapsed.tv_sec + ((double) elapsed.tv_usec / 1e6); - if (total < 0) { - total = 0; - } - closure->start = end; - snprintf (text, sizeof text, "Frames per second: %.2f", 200 / total); - gtk_label_set_text (GTK_LABEL (closure->fps_label), text); - } - - return TRUE; + Closure *closure = data; + + model_step (closure->model, 0.2); + + closure->i++; + if (closure->i == 1) { + gtk_widget_queue_draw (closure->drawing_area); + closure->i = 0; + closure->frame_count++; + } + + if (closure->frame_count == 200) { + struct timeval end, elapsed; + double total; + char text[50]; + + closure->frame_count = 0; + gettimeofday (&end, NULL); + if (closure->start.tv_usec > end.tv_usec) { + end.tv_usec += 1000000; + end.tv_sec--; + } + + elapsed.tv_usec = end.tv_usec - closure->start.tv_usec; + elapsed.tv_sec = end.tv_sec - closure->start.tv_sec; + + total = elapsed.tv_sec + ((double) elapsed.tv_usec / 1e6); + if (total < 0) { + total = 0; + } + closure->start = end; + snprintf (text, sizeof text, "Frames per second: %.2f", 200 / total); + gtk_label_set_text (GTK_LABEL (closure->fps_label), text); + } + + return TRUE; } int main (int argc, char *argv[]) { - Closure closure; - Model model; - - gtk_init (&argc, &argv); - model_init_rope (&model); - create_window (&closure); - closure.i = 0; - gtk_widget_show_all (gtk_widget_get_toplevel (closure.drawing_area)); - closure.model = &model; - closure.frame_count = 0; - gettimeofday (&closure.start, NULL); - g_timeout_add (40, timeout_callback, &closure); - gtk_main (); - - return 0; + Closure closure; + Model model; + + gtk_init (&argc, &argv); + model_init_rope (&model); + create_window (&closure); + closure.i = 0; + gtk_widget_show_all (gtk_widget_get_toplevel (closure.drawing_area)); + closure.model = &model; + closure.frame_count = 0; + gettimeofday (&closure.start, NULL); + g_timeout_add (40, timeout_callback, &closure); + gtk_main (); + + return 0; } -- 2.43.0