]> git.cworth.org Git - akamaru/commitdiff
Add init functions for various objects, clean up model initializations.
authorKristian Høgsberg <krh@redhat.com>
Wed, 24 May 2006 18:30:06 +0000 (14:30 -0400)
committerKristian Høgsberg <krh@dinky.bitplanet.net>
Wed, 24 May 2006 18:30:06 +0000 (14:30 -0400)
Makefile
akamaru.c
akamaru.h
main.c

index c98106c323de14eabc4cb3b82e81c5ea98a7c7cb..dae1f08321e986eb98f9856914d7e5c06a6b54ff 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -8,5 +8,7 @@ objs = akamaru.o main.o
 
 $(target) : $(objs)
 
 
 $(target) : $(objs)
 
+$(objs) : akamaru.h
+
 clean :
        rm $(target) $(objs)
 clean :
        rm $(target) $(objs)
index 1d94281b269fc936dbeaa830ae44ab4e6e5fbcd1..d158f3e2da5b3728882025fb3ce9c3598def2aef 100644 (file)
--- a/akamaru.c
+++ b/akamaru.c
@@ -24,6 +24,34 @@ const double elasticity = 0.7;
 const double friction = 1;
 const double gravity = 20;
 
 const double friction = 1;
 const double gravity = 20;
 
+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;
+}
+
+void
+spring_init (Spring *spring, Object *a, Object *b, double length)
+{
+  spring->a = a;
+  spring->b = b;
+  spring->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;
+}
+
 void
 polygon_init (Polygon *p, int num_points, ...)
 {
 void
 polygon_init (Polygon *p, int num_points, ...)
 {
index bfec60f70e36f99926e78188c492430d58124f64..81eeb8545ab286219b727fde9e11265ab10db1d3 100644 (file)
--- a/akamaru.h
+++ b/akamaru.h
@@ -84,6 +84,11 @@ struct _Model {
   double theta;
 };
 
   double theta;
 };
 
+void object_init (Object *object, double x, double y, double mass);
+void offset_spring_init (OffsetSpring *spring,
+                        Object *a, Object *b, double dx, double dy);
+void spring_init (Spring *spring, Object *a, Object *b, double length);
+
 void polygon_init (Polygon *p, int num_points, ...);
 void polygon_init_diamond (Polygon *polygon, double x, double y);
 void polygon_init_rectangle (Polygon *polygon, double x0, double y0,
 void polygon_init (Polygon *p, int num_points, ...);
 void polygon_init_diamond (Polygon *polygon, double x, double y);
 void polygon_init_rectangle (Polygon *polygon, double x0, double y0,
diff --git a/main.c b/main.c
index c5b306035975b13392f851d705c9d610a4ae68d5..3cef5e98772520c59d4c63ba2dd31974bbc13294 100644 (file)
--- a/main.c
+++ b/main.c
@@ -49,11 +49,8 @@ model_init_snake (Model *model)
   model_init_polygons (model);
 
   for (i = 0; i < num_objects; i++) {
   model_init_polygons (model);
 
   for (i = 0; i < num_objects; i++) {
-    model->objects[i].position.x = random() % 200 + 20;
-    model->objects[i].position.y = random() % 200 + 20;
-    model->objects[i].previous_position.x = random() % 200 + 20;
-    model->objects[i].previous_position.y = random() % 200 + 20;
-    model->objects[i].mass = 1;
+    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];
 
     if (i + 1 < num_objects) {
       model->sticks[i * 2].a = &model->objects[i];
@@ -86,11 +83,7 @@ model_init_rope (Model *model)
   model_init_polygons (model);
 
   for (i = 0; i < num_objects; i++) {
   model_init_polygons (model);
 
   for (i = 0; i < num_objects; i++) {
-    model->objects[i].position.x = 200;
-    model->objects[i].position.y = 40 + i * stick_length;
-    model->objects[i].previous_position.x = 200;
-    model->objects[i].previous_position.y = 40 + i * stick_length;
-    model->objects[i].mass = 1;
+    object_init (&model->objects[i], 200, 40 + i * stick_length, 1);
 
     if (i + 1 < num_objects) {
       model->sticks[i].a = &model->objects[i];
 
     if (i + 1 < num_objects) {
       model->sticks[i].a = &model->objects[i];
@@ -133,11 +126,7 @@ model_init_curtain (Model *model)
       x = 200 + i * rope_offset;
       y = 40 + j * stick_length;
       index = i * num_rope_objects + j;
       x = 200 + i * rope_offset;
       y = 40 + j * stick_length;
       index = i * num_rope_objects + j;
-      model->objects[index].position.x = x;
-      model->objects[index].position.y = y;
-      model->objects[index].previous_position.x = x;
-      model->objects[index].previous_position.y = y;
-      model->objects[index].mass = 1;
+      object_init (&model->objects[index], x, y, 1);
 
       if (j + 1 < num_rope_objects) {
        stick_index = i * (num_rope_objects - 1) + j;
 
       if (j + 1 < num_rope_objects) {
        stick_index = i * (num_rope_objects - 1) + j;
@@ -185,11 +174,7 @@ model_init_grid (Model *model)
       x = 200 + i * rope_offset;
       y = 40 + j * string_length;
       index = i * num_rope_objects + j;
       x = 200 + i * rope_offset;
       y = 40 + j * string_length;
       index = i * num_rope_objects + j;
-      model->objects[index].position.x = x;
-      model->objects[index].position.y = y;
-      model->objects[index].previous_position.x = x;
-      model->objects[index].previous_position.y = y;
-      model->objects[index].mass = 1;
+      object_init (&model->objects[index], x, y, 1);
 
       if (i + 1 < num_ropes) {
        string_index = i * num_rope_objects + j;
 
       if (i + 1 < num_ropes) {
        string_index = i * num_rope_objects + j;
@@ -220,6 +205,7 @@ model_init_molecule (Model *model)
   const int num_springs = num_objects * 2;
   const int spring_length = 50;
   int i;
   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);
 
   memset (model, 0, sizeof *model);
   model->objects = g_new (Object, num_objects);
@@ -228,21 +214,17 @@ model_init_molecule (Model *model)
   model->num_springs = num_springs;
   model->k = 2;
 
   model->num_springs = num_springs;
   model->k = 2;
 
-  for (i = 0; i < num_objects; i++) {
-    model->objects[i].position.x = 200 + i * 20;
-    model->objects[i].position.y = 200;
-    model->objects[i].previous_position.x = 200 + i * 20;
-    model->objects[i].previous_position.y = 200;
-    model->objects[i].mass = 0;
-  }
+  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++) {
   for (i = 0; i < num_objects; i++) {
-    model->springs[i * 2].a = &model->objects[i];
-    model->springs[i * 2].b = &model->objects[(i + 1) % num_objects];
-    model->springs[i * 2].length = spring_length;
-    model->springs[i * 2 + 1].a = &model->objects[i];
-    model->springs[i * 2 + 1].b = &model->objects[(i + 2) % num_objects];
-    model->springs[i * 2 + 1].length = spring_length;
+    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);
   }
 }
 
   }
 }
 
@@ -255,7 +237,9 @@ model_init_wobbly (Model *model)
   const int num_offset_springs = (width - 1) * height + width * (height - 1);
   const int distance = 30;
   double x, y;
   const int num_offset_springs = (width - 1) * height + width * (height - 1);
   const int distance = 30;
   double x, y;
-  int i, j, object_index, spring_index;
+  int i, j;
+  Object *object;
+  OffsetSpring *spring;
 
   memset (model, 0, sizeof *model);
   model->objects = g_new (Object, num_objects);
 
   memset (model, 0, sizeof *model);
   model->objects = g_new (Object, num_objects);
@@ -266,35 +250,21 @@ model_init_wobbly (Model *model)
 
   model_init_polygons (model);
 
 
   model_init_polygons (model);
 
-  object_index = 0;
-  spring_index = 0;
+  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;
   for (i = 0; i < width; i++) {
     for (j = 0; j < height; j++) {
       x = 200 + i * distance;
       y = 40 + j * distance;
-      model->objects[object_index].position.x = x;
-      model->objects[object_index].position.y = y;
-      model->objects[object_index].previous_position.x = x;
-      model->objects[object_index].previous_position.y = y;
-      model->objects[object_index].mass = 0;
-
-      if (i + 1 < width) {
-       model->offset_springs[spring_index].a = &model->objects[object_index];
-       model->offset_springs[spring_index].b = &model->objects[object_index + height];
-       model->offset_springs[spring_index].dx = distance;
-       model->offset_springs[spring_index].dy = 0;
-       spring_index++;
-      }
+      object_init (object, x, y, 0);
+
+      if (i + 1 < width)
+       offset_spring_init (spring++, object, object + height, distance, 0);
       
       
-      if (j + 1 < height) {
-       model->offset_springs[spring_index].a = &model->objects[object_index];
-       model->offset_springs[spring_index].b = &model->objects[object_index + 1];
-       model->offset_springs[spring_index].dx = 0;
-       model->offset_springs[spring_index].dy = distance;
-       spring_index++;
-      }
+      if (j + 1 < height)
+       offset_spring_init (spring++, object, object + 1, 0, distance);
 
 
-      object_index++;
+      object++;
     }
   }
 }
     }
   }
 }