1 /* $Id: wall-0.c,v 1.5 2011/08/23 20:52:23 oohara Exp $ */
2 
3 #include <stdio.h>
4 /* malloc, rand */
5 #include <stdlib.h>
6 
7 #include "const.h"
8 #include "tenm_object.h"
9 #include "tenm_graphic.h"
10 #include "tenm_primitive.h"
11 #include "util.h"
12 #include "tenm_table.h"
13 #include "tenm_math.h"
14 
15 #include "wall-0.h"
16 
17 static int wall_0_move(tenm_object *my, double turn_per_frame);
18 static int wall_0_draw(tenm_object *my, int priority);
19 
20 tenm_object *
wall_0_new(double x)21 wall_0_new(double x)
22 {
23   double y = -29.0;
24   tenm_primitive **p = NULL;
25   tenm_object *new = NULL;
26   double *count_d = NULL;
27 
28   p = (tenm_primitive **) malloc(sizeof(tenm_primitive *) * 1);
29   if (p == NULL)
30   {
31     fprintf(stderr, "wall_0_new: malloc(p) failed\n");
32     return NULL;
33   }
34   p[0] = (tenm_primitive *) tenm_polygon_new(4,
35                                              x + 30.0, y - 30.0,
36                                              x + 30.0, y + 30.0,
37                                              x - 30.0, y + 30.0,
38                                              x - 30.0, y - 30.0);
39   if (p[0] == NULL)
40   {
41     fprintf(stderr, "wall_0_new: cannot set p[0]\n");
42     free(p);
43     return NULL;
44   }
45 
46   count_d = (double *) malloc(sizeof(double) * 2);
47   if (count_d == NULL)
48   {
49     fprintf(stderr, "wall_0_new: malloc(count_d) failed\n");
50     (p[0])->delete(p[0]);
51     free(p);
52     return NULL;
53   }
54 
55   /* list of count_d
56    * [0] speed x
57    * [1] speed y
58    */
59   count_d[0] = 0.0;
60   count_d[1] = 4.0;
61 
62   new = tenm_object_new("wall 0", ATTR_OBSTACLE | ATTR_OPAQUE, 0,
63                         1, x, y,
64                         0, NULL, 2, count_d, 1, p,
65                         (int (*)(tenm_object *, double))
66                         (&wall_0_move),
67                         (int (*)(tenm_object *, tenm_object *))
68                         NULL,
69                         (int (*)(tenm_object *, const tenm_object *))
70                         NULL,
71                         (int (*)(tenm_object *, int))
72                         (&wall_0_draw));
73 
74 
75   if (new == NULL)
76   {
77     fprintf(stderr, "wall_0_new: tenm_object_new failed\n");
78     if (count_d != NULL)
79       free(count_d);
80     (p[0])->delete(p[0]);
81     free(p);
82     return NULL;
83   }
84 
85   return new;
86 }
87 
88 static int
wall_0_move(tenm_object * my,double turn_per_frame)89 wall_0_move(tenm_object *my, double turn_per_frame)
90 {
91   double dx_temp;
92   double dy_temp;
93 
94   /* sanity check */
95   if (my == NULL)
96   {
97     fprintf(stderr, "wall_0_move: my is NULL\n");
98     return 0;
99   }
100   if (turn_per_frame <= 0.5)
101   {
102     fprintf(stderr, "wall_0_move: strange turn_per_frame (%f)\n",
103             turn_per_frame);
104     return 0;
105   }
106 
107   dx_temp = my->count_d[0] / turn_per_frame;
108   dy_temp = my->count_d[1] / turn_per_frame;
109 
110   my->x += dx_temp;
111   my->y += dy_temp;
112   if (my->mass != NULL)
113     tenm_move_mass(my->mass, dx_temp, dy_temp);
114 
115   if (my->y > ((double) WINDOW_HEIGHT) + 31.0)
116     return 1;
117 
118   return 0;
119 }
120 
121 static int
wall_0_draw(tenm_object * my,int priority)122 wall_0_draw(tenm_object *my, int priority)
123 {
124   int status = 0;
125   tenm_color color;
126 
127   /* sanity check */
128   if (my == NULL)
129   {
130     fprintf(stderr, "wall_0_draw: my is NULL\n");
131     return 0;
132   }
133 
134   if (priority != 0)
135     return 0;
136 
137   /* body */
138   color = tenm_map_color(95, 13, 68);
139 
140   if (tenm_draw_line(((int) (my->x - 30.0)),
141                      ((int) (my->y - 30.0)),
142                      ((int) (my->x + 30.0)),
143                      ((int) (my->y + 30.0)),
144                      1, color) != 0)
145     status = 1;
146   if (tenm_draw_line(((int) (my->x + 30.0)),
147                      ((int) (my->y - 30.0)),
148                      ((int) (my->x - 30.0)),
149                      ((int) (my->y + 30.0)),
150                      1, color) != 0)
151     status = 1;
152 
153   if (tenm_draw_line(((int) (my->x + 30.0)),
154                      ((int) (my->y - 30.0)),
155                      ((int) (my->x + 30.0)),
156                      ((int) (my->y + 30.0)),
157                      2, color) != 0)
158     status = 1;
159   if (tenm_draw_line(((int) (my->x + 30.0)),
160                      ((int) (my->y + 30.0)),
161                      ((int) (my->x - 30.0)),
162                      ((int) (my->y + 30.0)),
163                      2, color) != 0)
164     status = 1;
165   if (tenm_draw_line(((int) (my->x - 30.0)),
166                      ((int) (my->y + 30.0)),
167                      ((int) (my->x - 30.0)),
168                      ((int) (my->y - 30.0)),
169                      2, color) != 0)
170     status = 1;
171   if (tenm_draw_line(((int) (my->x - 30.0)),
172                      ((int) (my->y - 30.0)),
173                      ((int) (my->x + 30.0)),
174                      ((int) (my->y - 30.0)),
175                      2, color) != 0)
176     status = 1;
177 
178   return status;
179 }
180