DCDecomp
A decompilation of Dark Cloud for the PlayStation 2
Loading...
Searching...
No Matches
effectmacro.cpp
1#include "effectmacro.hpp"
2
3#include <libvu0.h>
4
5#include <cstdlib>
6
7#include "effect.hpp"
8#include "effectgroup.hpp"
9#include "frame.hpp"
10#include "mglib.hpp"
11#include "rect.hpp"
12#include "texture.hpp"
13
14sceVu0FVECTOR wind_dir = {0.0f, 0.0f, 0.0f, 0.0f};
15int effect_count;
16CTexture *smoke_tex;
17CTexture *sibuki_tex;
18CTexture *hamon_tex;
19
20void EffectMacroStep(float *wind) {
21 sceVu0CopyVector(wind_dir, wind);
22 effect_count++;
23 smoke_tex = TexManager.GetTexture("smoke", -1);
24 sibuki_tex = TexManager.GetTexture("shibuki", -1);
25 hamon_tex = TexManager.GetTexture("hamon", -1);
26}
27
28void EffectSmoke(CEffectGroup *group, float *position, float size, int period) {
29 CEffectParam effect;
30
31 // One puff every `period` frames, so a caller can thin its own smoke.
32 if (effect_count % period != 0) {
33 return;
34 }
35
36 effect.texel.x = effect.texel.y = effect.texel.width = effect.texel.height = 0;
37 effect.Initialize();
38 sceVu0CopyVector(effect.position, position);
39
40 // Half the puffs drift, on a sine of their own in x and z.
41 if (rand() % 2 != 0) {
43 effect.position_oscillation_scale[0] = 0.3f * (float) rand() / 2.1474836e9f;
44 effect.position_oscillation_rate[0] = PI / (30.0f + (float) (rand() * 20) / 2.1474836e9f);
45 effect.position_oscillation_scale[2] = 0.3f * (float) rand() / 2.1474836e9f;
46 effect.position_oscillation_rate[2] = PI / (30.0f + (float) (rand() * 20) / 2.1474836e9f);
47 }
48
49 effect.opacity_mode = EFFECT_OPACITY_FADE_OUT;
50 effect.render_flags = 1;
51 effect.opacity = 0.1f;
52 sceVu0ScaleVector(effect.velocity, effect.velocity, 0.4f);
53 effect.velocity[1] += 0.2f + 0.2f * (float) rand() / 2.1474836e9f;
54 effect.scale_velocity[0] = 0.02f;
55 effect.scale_velocity[1] = 0.02f;
56 effect.scale[0] = size;
57 effect.scale[1] = size;
58 effect.lifetime = 0x78;
59 effect.texture = smoke_tex;
60
61 // The four puff shapes are the quarters of the texture.
62 switch (rand() % 4) {
63 case 0:
64 effect.texel = CRect_i_(0, 0, 0x40, 0x40);
65 break;
66 case 1:
67 effect.texel = CRect_i_(0x40, 0, 0x40, 0x40);
68 break;
69 case 2:
70 effect.texel = CRect_i_(0, 0x40, 0x40, 0x40);
71 break;
72 case 3:
73 effect.texel = CRect_i_(0x40, 0x40, 0x40, 0x40);
74 break;
75 }
76
77 effect.width = 10.0f;
78 effect.height = 10.0f;
79 group->EnterEffect(&effect);
80}
81
82void EffectWaterSpray(CEffectGroup *group, float *position, float *extent, int period, int phase) {
83 CEffectParam effect;
84
85 if ((effect_count + phase) % period != 0) {
86 return;
87 }
88
89 effect.texel.x = effect.texel.y = effect.texel.width = effect.texel.height = 0;
90 effect.Initialize();
91 sceVu0CopyVector(effect.position, position);
92 effect.opacity_mode = EFFECT_OPACITY_FADE_OUT;
93 effect.render_flags = 1;
94 effect.opacity = 0.5f;
95
96 float spread_x = 0.2f * extent[0];
97 float spread_z = 0.2f * extent[2];
98 float rise = 0.1f * extent[1] * (1.0f + (float) rand() / 2.1474836e9f);
99 float width_grown = rise + 0.5f * (extent[0] + extent[2]);
100 float height_grown = extent[1] + rise;
101
102 effect.velocity[0] = spread_x * (float) rand() / 2.1474836e9f - 0.5f * spread_x;
103 effect.velocity[2] = (spread_x * (float) rand() / 2.1474836e9f - 0.5f * spread_z) - 0.5f;
104 effect.velocity[1] = 1.0f + 0.5f * (float) rand() / 2.1474836e9f;
105 effect.acceleration[1] = -0.03f;
106 effect.scale_velocity[0] = width_grown / 30.0f;
107 effect.scale_velocity[1] = height_grown / 30.0f;
108 effect.scale[0] = 0.2f * width_grown;
109 effect.scale[1] = 0.2f * height_grown;
110 effect.lifetime = 0x28;
111 effect.texture = sibuki_tex;
112 effect.texel = CRect_i_(0, 0, 0x40, 0x40);
113 effect.width = 10.0f;
114 effect.height = 10.0f;
115 group->EnterEffect(&effect);
116}
117
118void EffectHamon(CEffectGroup *group, float *position, float size) {
119 CEffectParam effect;
120
121 effect.texel.x = effect.texel.y = effect.texel.width = effect.texel.height = 0;
122 effect.Initialize();
123 sceVu0CopyVector(effect.position, position);
124 effect.opacity_mode = EFFECT_OPACITY_FADE_OUT;
125 effect.render_flags = 1;
126 effect.opacity = 1.0f;
127 effect.draw_mode = 1;
128 effect.lifetime = 0x28;
129 // The ring starts at nothing and spreads for its whole life.
130 effect.scale_velocity[0] = 0.025f;
131 effect.scale_velocity[1] = 0.025f;
132 effect.scale_velocity[2] = 0.025f;
133 effect.scale[0] = 0.0f;
134 effect.scale[1] = 0.0f;
135 effect.scale[2] = 0.0f;
136 effect.texture = hamon_tex;
137 effect.texel = CRect_i_(0, 0, 0x20, 0x20);
138 effect.width = size;
139 effect.height = size;
140 group->EnterEffect(&effect);
141}
142
143void DepthOfField(float *focus, int level, int alpha, int blur) {
144 static float rd[21][15];
145 sceGsTex0 frame;
146 sceGsTex0 image;
147 sceGsTest test;
148 sceGsZbuf zbuffer;
149 int phase;
150 int i;
151 int j;
152 int k;
153
154 MGGetFBuffTex(&frame);
155 image = *(sceGsTex0 *) &TexManager.GetTexture("frame_image", -1)->tex0;
156 frame.PSM = SCE_GS_PSMCT24;
157 MGStretchMoveImage(&frame, CRect_i_(0, 0, 0x2800, (SCREEN_HALF_HEIGHT << 4)), &image, CRect_i_(0, 0, 0x1400, (SCREEN_HALF_HEIGHT << 4)));
158 MGStretchMoveImage(&image, CRect_i_(0, 0, 0x1400, (SCREEN_HALF_HEIGHT << 4)), &image, CRect_i_(0x1400, 0, 0xA00, (SCREEN_HALF_HEIGHT << 4)));
159
160 sceVu0FVECTOR depth_point[4] = {
161 {0.0f, 0.0f, focus[0], 1.0f},
162 {0.0f, 0.0f, focus[0] + 30.0f, 1.0f},
163 {0.0f, 0.0f, focus[1], 1.0f},
164 {0.0f, 0.0f, focus[1] + 20.0f, 1.0f},
165 };
166 int screen[4][4];
167
168 for (i = 0; i < 4; i++) {
169 sceVu0ApplyMatrix(depth_point[i], mgRenderInfo.screen, depth_point[i]);
170 depth_point[i][2] /= depth_point[i][3];
171 screen[i][2] = (int) depth_point[i][2];
172 }
173
174 sceVif1PkCnt(Vif1Packet, 0);
175 sceVif1PkOpenDirectCode(Vif1Packet, 0);
176 sceVif1PkOpenGifTag(Vif1Packet, *(u_long128 *) &GiftagAD);
177 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_TEXFLUSH, 0);
178 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_TEX1_1, 0x61);
179 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_TEX0_1, *(u_long *) &image);
180 test = mgPixelTest;
181 test.bits.ate = 0;
182 test.bits.aref = 0;
183 test.bits.atst = 1;
184 test.bits.zte = 1;
185 test.bits.ztst = 2;
186 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_TEST_1, *(u_long *) &test);
187 zbuffer = mgZBuffer;
188 zbuffer.bits.zmsk = 1;
189 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_ZBUF_1, *(u_long *) &zbuffer);
190 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_RGBAQ, SCE_GS_SET_RGBAQ(0x80, 0x80, 0x80, 0x80, 0));
191 sceVif1PkCloseGifTag(Vif1Packet);
192 sceVif1PkCloseDirectCode(Vif1Packet);
193
194 for (phase = 0; phase < 1; phase++) {
195 sceVif1PkCnt(Vif1Packet, 0);
196 sceVif1PkOpenDirectCode(Vif1Packet, 0);
197 sceVif1PkOpenGifTag(Vif1Packet, *(u_long128 *) &GiftagAD);
198
199 for (k = 0; k < 8; k++) {
200 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_PRIM, 0x15C);
201
202 for (j = 0; j < 41; j++) {
203 int x = (j << 8) + 0x6C00;
204 int v = k << 9;
205 int y = v + GS_Y_OFFSET;
206 int u = j << 7;
207 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_RGBAQ, SCE_GS_SET_RGBAQ(0x80, 0x80, 0x80, alpha, 0));
208 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_UV, SCE_GS_SET_UV(u, v));
209 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_XYZF2, SCE_GS_SET_XYZF2(x, y, screen[(j + phase) % 2][2], 0));
210 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_UV, SCE_GS_SET_UV(u, v + 0x200));
211 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_XYZF2, SCE_GS_SET_XYZF2(x, y + 0x200, screen[(j + phase) % 2][2], 0));
212 }
213 }
214
215 sceVif1PkCloseGifTag(Vif1Packet);
216 sceVif1PkCloseDirectCode(Vif1Packet);
217 }
218
219 sceVif1PkCnt(Vif1Packet, 0);
220 sceVif1PkOpenDirectCode(Vif1Packet, 0);
221 sceVif1PkOpenGifTag(Vif1Packet, *(u_long128 *) &GiftagAD);
222
223 if (level >= 2) {
224 if (blur > 0) {
225 for (i = 0; i < 21; i++) {
226 for (j = 0; j < 15; j++) {
227 if (blur > 0) {
228 rd[i][j] += 0.2f * ((float) blur * ((float) rand() / 2147483648.0f - 0.5f));
229
230 if (rd[i][j] < 0.0f) {
231 rd[i][j] = 0.0f;
232 }
233
234 if (rd[i][j] > (float) blur) {
235 rd[i][j] = (float) blur;
236 }
237 } else {
238 rd[i][j] = 0.0f;
239 }
240 }
241 }
242 }
243
244 if (blur > 0) {
245 alpha = 0x80;
246 }
247
248#ifdef PAL
249 for (j = 0; j < 15; j++) {
250#else
251 for (j = 0; j < 14; j++) {
252#endif
253 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_PRIM, 0x15C);
254 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_RGBAQ, SCE_GS_SET_RGBAQ(0x80, 0x80, 0x80, alpha, 0));
255
256 for (i = 0; i < 21; i++) {
257 int x = (i << 9) + 0x6C00;
258 int v = j << 8;
259 int y = v + GS_Y_OFFSET;
260 int y_bottom = y + 0x100;
261 int u = (i << 7) + 0x1400;
262 int v_bottom = v + 0x100;
263
264 if (blur > 0) {
265 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_UV, SCE_GS_SET_UV(u, v));
266 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_XYZF2, SCE_GS_SET_XYZF2(x - (int) rd[i][j], y, screen[2 + i % 2][2], 0));
267 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_UV, SCE_GS_SET_UV(u, v_bottom));
268 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_XYZF2, SCE_GS_SET_XYZF2(x - (int) rd[i][j + 1], y_bottom, screen[2 + i % 2][2], 0));
269 } else {
270 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_UV, SCE_GS_SET_UV(u, v));
271 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_XYZF2, SCE_GS_SET_XYZF2(x, y, screen[2 + i % 2][2], 0));
272 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_UV, SCE_GS_SET_UV(u, v_bottom));
273 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_XYZF2, SCE_GS_SET_XYZF2(x, y_bottom, screen[2 + i % 2][2], 0));
274 }
275 }
276 }
277 }
278
279 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_TEXFLUSH, 0);
280 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_TEST_1, *(u_long *) &mgPixelTest);
281 sceVif1PkAddGsAD(Vif1Packet, SCE_GS_ZBUF_1, *(u_long *) &mgZBuffer);
282 sceVif1PkCloseGifTag(Vif1Packet);
283 sceVif1PkCloseDirectCode(Vif1Packet);
284}
sceVu0FVECTOR position
Definition op_a.cpp:149
int draw_mode
Definition effect.hpp:44
float scale_velocity[4]
Definition effect.hpp:54
int position_oscillation_flags
Definition op_a.cpp:145
float height
Definition op_a.cpp:147
float acceleration[4]
Definition effect.hpp:50
CRect< int > texel
Definition op_a.cpp:164
float width
Definition effect.hpp:42
float position_oscillation_rate[4]
Definition effect.hpp:52
float position_oscillation_scale[4]
Definition effect.hpp:51
float scale[4]
Definition effect.hpp:53
int render_flags
Definition op_a.cpp:161
float velocity[4]
Definition effect.hpp:49
float opacity
Definition op_a.cpp:162
int opacity_mode
Definition op_a.cpp:160
int lifetime
Definition op_a.cpp:144
CTexture * texture
Definition op_a.cpp:163
T y
Definition op_a.cpp:58
T x
Definition op_a.cpp:57
u_long tex0
Definition texture.hpp:57
sceVu0FMATRIX screen