- more c++-ification
[supertux.git] / src / world.cpp
1 //
2 // C Implementation: world
3 //
4 // Description:
5 //
6 //
7 // Author: Tobias Glaesser <tobi.web@gmx.de>, (C) 2004
8 //
9 // Copyright: See COPYING file that comes with this distribution
10 //
11 //
12
13 #include <stdlib.h>
14 #include <string.h>
15 #include "globals.h"
16 #include "scene.h"
17 #include "screen.h"
18 #include "defines.h"
19 #include "world.h"
20
21 texture_type img_distro[4];
22
23 void bouncy_distro_init(bouncy_distro_type* pbouncy_distro, float x, float y)
24 {
25   pbouncy_distro->base.x = x;
26   pbouncy_distro->base.y = y;
27   pbouncy_distro->base.ym = -2;
28 }
29
30 void bouncy_distro_action(bouncy_distro_type* pbouncy_distro)
31 {
32   pbouncy_distro->base.y = pbouncy_distro->base.y + pbouncy_distro->base.ym * frame_ratio;
33
34   pbouncy_distro->base.ym += 0.1 * frame_ratio;
35
36   if (pbouncy_distro->base.ym >= 0)
37     bouncy_distros.erase(static_cast<std::vector<bouncy_distro_type>::iterator>(pbouncy_distro));
38 }
39
40 void bouncy_distro_draw(bouncy_distro_type* pbouncy_distro)
41 {
42   texture_draw(&img_distro[0],
43                pbouncy_distro->base.x - scroll_x,
44                pbouncy_distro->base.y);
45 }
46
47 void broken_brick_init(broken_brick_type* pbroken_brick, float x, float y, float xm, float ym)
48 {
49   pbroken_brick->base.x = x;
50   pbroken_brick->base.y = y;
51   pbroken_brick->base.xm = xm;
52   pbroken_brick->base.ym = ym;
53   timer_init(&pbroken_brick->timer, true);
54   timer_start(&pbroken_brick->timer,200);
55 }
56
57 void broken_brick_action(broken_brick_type* pbroken_brick)
58 {
59   pbroken_brick->base.x = pbroken_brick->base.x + pbroken_brick->base.xm * frame_ratio;
60   pbroken_brick->base.y = pbroken_brick->base.y + pbroken_brick->base.ym * frame_ratio;
61
62   if (!timer_check(&pbroken_brick->timer))
63     broken_bricks.erase(static_cast<std::vector<broken_brick_type>::iterator>(pbroken_brick));
64 }
65
66 void broken_brick_draw(broken_brick_type* pbroken_brick)
67 {
68   SDL_Rect src, dest;
69   src.x = rand() % 16;
70   src.y = rand() % 16;
71   src.w = 16;
72   src.h = 16;
73
74   dest.x = (int)(pbroken_brick->base.x - scroll_x);
75   dest.y = (int)pbroken_brick->base.y;
76   dest.w = 16;
77   dest.h = 16;
78
79   texture_draw_part(&img_brick[0],src.x,src.y,dest.x,dest.y,dest.w,dest.h);
80 }
81
82 void bouncy_brick_init(bouncy_brick_type* pbouncy_brick, float x, float y)
83 {
84   pbouncy_brick->base.x = x;
85   pbouncy_brick->base.y = y;
86   pbouncy_brick->offset = 0;
87   pbouncy_brick->offset_m = -BOUNCY_BRICK_SPEED;
88   pbouncy_brick->shape = gettileid(x, y);
89 }
90
91 void bouncy_brick_action(bouncy_brick_type* pbouncy_brick)
92 {
93
94   pbouncy_brick->offset = (pbouncy_brick->offset +
95                            pbouncy_brick->offset_m * frame_ratio);
96
97   /* Go back down? */
98
99   if (pbouncy_brick->offset < -BOUNCY_BRICK_MAX_OFFSET)
100     pbouncy_brick->offset_m = BOUNCY_BRICK_SPEED;
101
102
103   /* Stop bouncing? */
104
105   if (pbouncy_brick->offset >= 0)
106     bouncy_bricks.erase(static_cast<std::vector<bouncy_brick_type>::iterator>(pbouncy_brick));
107 }
108
109 void bouncy_brick_draw(bouncy_brick_type* pbouncy_brick)
110 {
111   int s;
112   SDL_Rect dest;
113   
114   if (pbouncy_brick->base.x >= scroll_x - 32 &&
115       pbouncy_brick->base.x <= scroll_x + screen->w)
116     {
117       dest.x = (int)(pbouncy_brick->base.x - scroll_x);
118       dest.y = (int)pbouncy_brick->base.y;
119       dest.w = 32;
120       dest.h = 32;
121
122       // FIXME: overdrawing hack to clean the tile from the screen to
123       // paint it later at on offseted position
124       if(current_level.bkgd_image[0] == '\0')
125         {
126           fillrect(pbouncy_brick->base.x - scroll_x, pbouncy_brick->base.y,
127                    32,32,current_level.bkgd_red,current_level.bkgd_green,
128                    current_level.bkgd_blue,0);
129         }
130       else
131         {
132           s = (int)scroll_x / 30;
133           texture_draw_part(&img_bkgd,dest.x + s,dest.y,dest.x,dest.y,dest.w,dest.h);
134         }
135
136       drawshape(pbouncy_brick->base.x - scroll_x,
137                 pbouncy_brick->base.y + pbouncy_brick->offset,
138                 pbouncy_brick->shape);
139     }
140 }
141
142 void floating_score_init(floating_score_type* pfloating_score, float x, float y, int s)
143 {
144   pfloating_score->base.x = x;
145   pfloating_score->base.y = y - 16;
146   timer_init(&pfloating_score->timer,true);
147   timer_start(&pfloating_score->timer,1000);
148   pfloating_score->value = s;
149 }
150
151 void floating_score_action(floating_score_type* pfloating_score)
152 {
153   pfloating_score->base.y = pfloating_score->base.y - 2 * frame_ratio;
154
155   if(!timer_check(&pfloating_score->timer))
156     floating_scores.erase(static_cast<std::vector<floating_score_type>::iterator>(pfloating_score));
157 }
158
159 void floating_score_draw(floating_score_type* pfloating_score)
160 {
161   char str[10];
162   sprintf(str, "%d", pfloating_score->value);
163   text_draw(&gold_text, str, (int)pfloating_score->base.x + 16 - strlen(str) * 8, (int)pfloating_score->base.y, 1);
164 }
165
166 /* EOF */
167