-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcSnow.cpp
365 lines (293 loc) · 10.1 KB
/
cSnow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
///////////////////////////////////////////////////////
//*-------------------------------------------------*//
//| Part of Project One (https://www.maus-games.at) |//
//*-------------------------------------------------*//
//| Copyright (c) 2010 Martin Mauersics |//
//| Released under the zlib License |//
//*-------------------------------------------------*//
///////////////////////////////////////////////////////
#include "main.h"
// ****************************************************************
// constructor
cSnow::cSnow()noexcept
: m_fVisibility (0.0f)
, m_fDelay (0.0f)
, m_bDirty (true)
{
//
m_pSnowMap = Core::Manager::Resource->LoadNew<coreTexture>();
m_piSnowData = ALIGNED_NEW(coreUint8, SNOW_SIZE * SNOW_SIZE, ALIGNMENT_PAGE);
STATIC_ASSERT(SNOW_SIZE * SNOW_SIZE == ALIGNMENT_PAGE)
//
this->__Reset(CORE_RESOURCE_RESET_INIT);
// load object resources
this->DefineProgram("effect_snow_program");
this->DefineTexture(0u, m_pSnowMap);
this->DefineTexture(1u, "menu_background_black.png");
// set object properties
this->SetSize (coreVector2(1.0f,1.0f));
this->SetAlpha(0.0f);
}
// ****************************************************************
// destructor
cSnow::~cSnow()
{
//
ALIGNED_DELETE(m_piSnowData)
// free resources
this->DefineTexture(0u, NULL);
Core::Manager::Resource->Free(&m_pSnowMap);
}
// ****************************************************************
//
void cSnow::Render()
{
if(!m_fVisibility) return;
//
if(m_bDirty)
{
m_bDirty = false;
//
m_pSnowMap->Invalidate(0u);
m_pSnowMap->Modify(0u, 0u, SNOW_SIZE, SNOW_SIZE, SNOW_SIZE * SNOW_SIZE, m_piSnowData);
}
//
this->coreFullscreen::Render();
}
// ****************************************************************
//
void cSnow::Move()
{
if(!m_fVisibility) return;
//
if(m_fDelay) m_fVisibility = MAX0(m_fVisibility - m_fDelay * TIME);
this->SetAlpha(BLENDH3(m_fVisibility));
//
this->coreFullscreen::Move();
}
// ****************************************************************
//
void cSnow::Enable()
{
//
ASSERT(!m_fVisibility)
m_fVisibility = 1.0f;
//
m_fDelay = 0.0f;
//
this->DrawAll(SNOW_TYPE_REMOVE);
}
// ****************************************************************
//
void cSnow::Disable(const coreFloat fDelay)
{
//
m_fDelay = fDelay ? fDelay : FRAMERATE_MAX;
}
// ****************************************************************
//
coreUintW cSnow::DrawPoint(const coreVector2 vPosition, const coreFloat fSize, const eSnowType eType)
{
coreUintW iHit = 0u;
//
const coreVector2 vSnap = cSnow::__SnapPosition(vPosition);
//
const coreUintW iFromX = cSnow::__GetMapIndex(vSnap.x - (fSize + 1.0f));
const coreUintW iToX = cSnow::__GetMapIndex(vSnap.x + (fSize + 1.0f));
const coreUintW iFromY = cSnow::__GetMapIndex(vSnap.y - (fSize + 1.0f));
const coreUintW iToY = cSnow::__GetMapIndex(vSnap.y + (fSize + 1.0f));
ASSERT((iFromX < SNOW_SIZE) && (iToX < SNOW_SIZE) && (iFromY < SNOW_SIZE) && (iToY < SNOW_SIZE))
//
for(coreUintW j = iFromY; j <= iToY; ++j)
{
for(coreUintW i = iFromX; i <= iToX; ++i)
{
//
coreUint8& iByte = m_piSnowData[j * SNOW_SIZE + i];
if((eType == SNOW_TYPE_INVERT) || (eType ? !iByte : iByte))
{
//
const coreFloat fPosX = cSnow::__GetMapValue(i);
const coreFloat fPosY = cSnow::__GetMapValue(j);
//
if((coreVector2(fPosX, fPosY) - vSnap).LengthSq() < POW2(fSize))
{
iByte = (eType == SNOW_TYPE_INVERT) ? ~iByte : (eType ? 0xFFu : 0x00u);
iHit += 1u;
}
}
}
}
//
if(iHit) m_bDirty = true;
return iHit;
}
// ****************************************************************
//
coreUintW cSnow::DrawLine(const coreVector2 vPosition, const coreFloat fSize, const coreBool bHorizontal, const eSnowType eType)
{
coreUintW iHit = 0u;
//
const coreVector2 vSnap = cSnow::__SnapPosition(vPosition);
//
const coreUintW iFromX = bHorizontal ? (0u) : cSnow::__GetMapIndex(vSnap.x - fSize);
const coreUintW iToX = bHorizontal ? (SNOW_SIZE - 1u) : cSnow::__GetMapIndex(vSnap.x + fSize);
const coreUintW iFromY = !bHorizontal ? (0u) : cSnow::__GetMapIndex(vSnap.y - fSize);
const coreUintW iToY = !bHorizontal ? (SNOW_SIZE - 1u) : cSnow::__GetMapIndex(vSnap.y + fSize);
ASSERT((iFromX < SNOW_SIZE) && (iToX < SNOW_SIZE) && (iFromY < SNOW_SIZE) && (iToY < SNOW_SIZE))
//
for(coreUintW j = iFromY; j <= iToY; ++j)
{
for(coreUintW i = iFromX; i <= iToX; ++i)
{
//
coreUint8& iByte = m_piSnowData[j * SNOW_SIZE + i];
if((eType == SNOW_TYPE_INVERT) || (eType ? !iByte : iByte))
{
iByte = (eType == SNOW_TYPE_INVERT) ? ~iByte : (eType ? 0xFFu : 0x00u);
iHit += 1u;
}
}
}
//
if(iHit) m_bDirty = true;
return iHit;
}
// ****************************************************************
//
coreUintW cSnow::DrawRay(const coreVector2 vPosition, const coreVector2 vDirection, const eSnowType eType)
{
coreUintW iHit = 0u;
//
coreVector2 vCurrent = coreVector2(cSnow::__GetMapIndexFloat(vPosition.x), cSnow::__GetMapIndexFloat(vPosition.y));
//
ASSERT(vDirection.IsNormalized())
const coreVector2 vStep = vDirection * ABS(RCP(IsHorizontal(vDirection) ? vDirection.x : vDirection.y));
//
while((vCurrent.x >= 0.0f) && (vCurrent.x < I_TO_F(SNOW_SIZE)) &&
(vCurrent.y >= 0.0f) && (vCurrent.y < I_TO_F(SNOW_SIZE)))
{
//
const coreUintW i = F_TO_UI(vCurrent.x);
const coreUintW j = F_TO_UI(vCurrent.y);
ASSERT((i < SNOW_SIZE) && (j < SNOW_SIZE))
//
coreUint8& iByte = m_piSnowData[j * SNOW_SIZE + i];
if((eType == SNOW_TYPE_INVERT) || (eType ? !iByte : iByte))
{
iByte = (eType == SNOW_TYPE_INVERT) ? ~iByte : (eType ? 0xFFu : 0x00u);
iHit += 1u;
}
//
vCurrent += vStep;
}
//
if(iHit) m_bDirty = true;
return iHit;
}
// ****************************************************************
//
void cSnow::DrawAll(const eSnowType eType)
{
if(eType == SNOW_TYPE_INVERT)
{
STATIC_ASSERT(coreMath::IsAligned(SNOW_SIZE * SNOW_SIZE, 8u))
//
coreUint64* piSnowData64 = r_cast<coreUint64*>(m_piSnowData);
for(coreUintW i = 0u; i < SNOW_SIZE * SNOW_SIZE / 8u; ++i)
{
piSnowData64[i] = ~piSnowData64[i];
}
}
else
{
//
std::memset(m_piSnowData, eType ? 0xFFu : 0x00u, SNOW_SIZE * SNOW_SIZE);
}
//
m_bDirty = true;
}
// ****************************************************************
//
coreBool cSnow::TestCollision(const coreVector2 vPosition)const
{
ASSERT(m_fVisibility)
//
const coreUintW iX = cSnow::__GetMapIndex(vPosition.x);
const coreUintW iY = cSnow::__GetMapIndex(vPosition.y);
//
return (m_piSnowData[iY * SNOW_SIZE + iX] != 0x00u);
}
// ****************************************************************
//
coreBool cSnow::TestCollision(const coreVector2 vPosition, const coreVector2 vDirection, coreFloat* OUTPUT pfDistance)const
{
ASSERT(pfDistance)
//
coreVector2 vCurrent = coreVector2(cSnow::__GetMapIndexFloat(vPosition.x), cSnow::__GetMapIndexFloat(vPosition.y));
//
ASSERT(vDirection.IsNormalized())
const coreVector2 vStep = vDirection * ABS(RCP(IsHorizontal(vDirection) ? vDirection.x : vDirection.y));
//
while((vCurrent.x >= 0.0f) && (vCurrent.x < I_TO_F(SNOW_SIZE)) &&
(vCurrent.y >= 0.0f) && (vCurrent.y < I_TO_F(SNOW_SIZE)))
{
//
const coreUintW i = F_TO_UI(vCurrent.x);
const coreUintW j = F_TO_UI(vCurrent.y);
ASSERT((i < SNOW_SIZE) && (j < SNOW_SIZE))
//
if(m_piSnowData[j * SNOW_SIZE + i] != 0x00u)
{
//
const coreFloat fPosX = cSnow::__GetMapValue(i);
const coreFloat fPosY = cSnow::__GetMapValue(j);
//
(*pfDistance) = (coreVector2(fPosX, fPosY) - vPosition).Length();
return true;
}
//
vCurrent += vStep;
}
//
return false;
}
// ****************************************************************
//
coreBool cSnow::AnyData()const
{
return std::memchr(m_piSnowData, 0xFFu, SNOW_SIZE * SNOW_SIZE);
}
coreBool cSnow::AllData()const
{
return !std::memchr(m_piSnowData, 0x00u, SNOW_SIZE * SNOW_SIZE);
}
// ****************************************************************
// reset with the resource manager
void cSnow::__Reset(const coreResourceReset eInit)
{
if(eInit) {m_pSnowMap->Create(SNOW_SIZE, SNOW_SIZE, CORE_TEXTURE_SPEC_R8, CORE_TEXTURE_MODE_DEFAULT); m_bDirty = true;}
else {m_pSnowMap->Unload();}
}
// ****************************************************************
//
coreFloat cSnow::__GetMapIndexFloat(const coreFloat fValue)
{
STATIC_ASSERT(FOREGROUND_AREA.x == FOREGROUND_AREA.y)
return CLAMP((fValue + (FOREGROUND_AREA.x * 1.1f)) / (FOREGROUND_AREA.x * 2.2f), 0.0f, 1.0f - CORE_MATH_PRECISION) * I_TO_F(SNOW_SIZE);
}
coreUintW cSnow::__GetMapIndex(const coreFloat fValue)
{
return F_TO_UI(cSnow::__GetMapIndexFloat(fValue));
}
coreFloat cSnow::__GetMapValue(const coreUintW iIndex)
{
STATIC_ASSERT(FOREGROUND_AREA.x == FOREGROUND_AREA.y)
return ((I_TO_F(iIndex) + 0.5f) / I_TO_F(SNOW_SIZE)) * (FOREGROUND_AREA.x * 2.2f) - (FOREGROUND_AREA.x * 1.1f);
}
// ****************************************************************
//
coreVector2 cSnow::__SnapPosition(const coreVector2 vPosition)
{
return (((((vPosition / (FOREGROUND_AREA * 1.1f)) * 0.5f + 0.5f) * I_TO_F(SNOW_SIZE)).Processed(ROUND) / I_TO_F(SNOW_SIZE)) * 2.0f - 1.0f) * (FOREGROUND_AREA * 1.1f);
}