1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#include "render_map.h"
4
5#include <base/str.h>
6
7#include <engine/graphics.h>
8#include <engine/map.h>
9#include <engine/shared/config.h>
10#include <engine/shared/datafile.h>
11#include <engine/shared/map.h>
12#include <engine/textrender.h>
13
14#include <generated/client_data.h>
15
16#include <game/mapitems.h>
17#include <game/mapitems_ex.h>
18
19#include <algorithm>
20#include <array>
21#include <chrono>
22#include <cmath>
23
24using namespace std::chrono_literals;
25
26int IEnvelopePointAccess::FindPointIndex(CFixedTime Time) const
27{
28 // binary search for the interval around Time
29 int Low = 0;
30 int High = NumPoints() - 2;
31 int FoundIndex = -1;
32
33 while(Low <= High)
34 {
35 int Mid = Low + (High - Low) / 2;
36 const CEnvPoint *pMid = GetPoint(Index: Mid);
37 const CEnvPoint *pNext = GetPoint(Index: Mid + 1);
38 if(Time >= pMid->m_Time && Time < pNext->m_Time)
39 {
40 FoundIndex = Mid;
41 break;
42 }
43 else if(Time < pMid->m_Time)
44 {
45 High = Mid - 1;
46 }
47 else
48 {
49 Low = Mid + 1;
50 }
51 }
52 return FoundIndex;
53}
54
55CMapBasedEnvelopePointAccess::CMapBasedEnvelopePointAccess(IMap *pMap)
56{
57 bool FoundBezierEnvelope = false;
58 int EnvelopeStart, EnvelopeNum;
59 pMap->GetType(Type: MAPITEMTYPE_ENVELOPE, pStart: &EnvelopeStart, pNum: &EnvelopeNum);
60 m_vpEnvelopes.reserve(n: EnvelopeNum);
61 for(int EnvelopeIndex = 0; EnvelopeIndex < EnvelopeNum; EnvelopeIndex++)
62 {
63 const CMapItemEnvelope *pEnvelope = static_cast<const CMapItemEnvelope *>(pMap->GetItem(Index: EnvelopeStart + EnvelopeIndex));
64 if(pEnvelope->m_Version >= CMapItemEnvelope::VERSION_TEEWORLDS_BEZIER)
65 {
66 FoundBezierEnvelope = true;
67 }
68 m_vpEnvelopes.push_back(x: pEnvelope);
69 }
70
71 if(FoundBezierEnvelope)
72 {
73 m_pPoints = nullptr;
74 m_pPointsBezier = nullptr;
75
76 int EnvPointStart, FakeEnvPointNum;
77 pMap->GetType(Type: MAPITEMTYPE_ENVPOINTS, pStart: &EnvPointStart, pNum: &FakeEnvPointNum);
78 if(FakeEnvPointNum > 0)
79 m_pPointsBezierUpstream = static_cast<CEnvPointBezier_upstream *>(pMap->GetItem(Index: EnvPointStart));
80 else
81 m_pPointsBezierUpstream = nullptr;
82
83 m_NumPointsMax = pMap->GetItemSize(Index: EnvPointStart) / sizeof(CEnvPointBezier_upstream);
84 }
85 else
86 {
87 int EnvPointStart, FakeEnvPointNum;
88 pMap->GetType(Type: MAPITEMTYPE_ENVPOINTS, pStart: &EnvPointStart, pNum: &FakeEnvPointNum);
89 if(FakeEnvPointNum > 0)
90 m_pPoints = static_cast<CEnvPoint *>(pMap->GetItem(Index: EnvPointStart));
91 else
92 m_pPoints = nullptr;
93
94 m_NumPointsMax = pMap->GetItemSize(Index: EnvPointStart) / sizeof(CEnvPoint);
95
96 int EnvPointBezierStart, FakeEnvPointBezierNum;
97 pMap->GetType(Type: MAPITEMTYPE_ENVPOINTS_BEZIER, pStart: &EnvPointBezierStart, pNum: &FakeEnvPointBezierNum);
98 const int NumPointsBezier = pMap->GetItemSize(Index: EnvPointBezierStart) / sizeof(CEnvPointBezier);
99 if(FakeEnvPointBezierNum > 0 && m_NumPointsMax == NumPointsBezier)
100 m_pPointsBezier = static_cast<CEnvPointBezier *>(pMap->GetItem(Index: EnvPointBezierStart));
101 else
102 m_pPointsBezier = nullptr;
103
104 m_pPointsBezierUpstream = nullptr;
105 }
106
107 SetPointsRange(StartPoint: 0, NumPoints: m_NumPointsMax);
108}
109
110const std::vector<const CMapItemEnvelope *> &CMapBasedEnvelopePointAccess::Envelopes() const
111{
112 return m_vpEnvelopes;
113}
114
115void CMapBasedEnvelopePointAccess::SetPointsRange(int StartPoint, int NumPoints)
116{
117 m_StartPoint = std::clamp(val: StartPoint, lo: 0, hi: m_NumPointsMax);
118 m_NumPoints = std::clamp(val: NumPoints, lo: 0, hi: m_NumPointsMax - m_StartPoint);
119}
120
121int CMapBasedEnvelopePointAccess::StartPoint() const
122{
123 return m_StartPoint;
124}
125
126int CMapBasedEnvelopePointAccess::NumPoints() const
127{
128 return m_NumPoints;
129}
130
131int CMapBasedEnvelopePointAccess::NumPointsMax() const
132{
133 return m_NumPointsMax;
134}
135
136const CEnvPoint *CMapBasedEnvelopePointAccess::GetPoint(int Index) const
137{
138 if(Index < 0 || Index >= m_NumPoints)
139 return nullptr;
140 if(m_pPoints != nullptr)
141 return &m_pPoints[Index + m_StartPoint];
142 if(m_pPointsBezierUpstream != nullptr)
143 return &m_pPointsBezierUpstream[Index + m_StartPoint];
144 return nullptr;
145}
146
147const CEnvPointBezier *CMapBasedEnvelopePointAccess::GetBezier(int Index) const
148{
149 if(Index < 0 || Index >= m_NumPoints)
150 return nullptr;
151 if(m_pPointsBezier != nullptr)
152 return &m_pPointsBezier[Index + m_StartPoint];
153 if(m_pPointsBezierUpstream != nullptr)
154 return &m_pPointsBezierUpstream[Index + m_StartPoint].m_Bezier;
155 return nullptr;
156}
157
158static float SolveBezier(float x, float p0, float p1, float p2, float p3)
159{
160 const double x3 = -p0 + 3.0 * p1 - 3.0 * p2 + p3;
161 const double x2 = 3.0 * p0 - 6.0 * p1 + 3.0 * p2;
162 const double x1 = -3.0 * p0 + 3.0 * p1;
163 const double x0 = p0 - x;
164
165 if(x3 == 0.0 && x2 == 0.0)
166 {
167 // linear
168 // a * t + b = 0
169 const double a = x1;
170 const double b = x0;
171
172 if(a == 0.0)
173 return 0.0f;
174 return -b / a;
175 }
176 else if(x3 == 0.0)
177 {
178 // quadratic
179 // t * t + b * t + c = 0
180 const double b = x1 / x2;
181 const double c = x0 / x2;
182
183 if(c == 0.0)
184 return 0.0f;
185
186 const double D = b * b - 4.0 * c;
187 const double SqrtD = std::sqrt(x: D);
188
189 const double t = (-b + SqrtD) / 2.0;
190
191 if(0.0 <= t && t <= 1.0001)
192 return t;
193 return (-b - SqrtD) / 2.0;
194 }
195 else
196 {
197 // cubic
198 // t * t * t + a * t * t + b * t * t + c = 0
199 const double a = x2 / x3;
200 const double b = x1 / x3;
201 const double c = x0 / x3;
202
203 // substitute t = y - a / 3
204 const double Substitute = a / 3.0;
205
206 // depressed form x^3 + px + q = 0
207 // cardano's method
208 const double p = b / 3.0 - a * a / 9.0;
209 const double q = (2.0 * a * a * a / 27.0 - a * b / 3.0 + c) / 2.0;
210
211 const double D = q * q + p * p * p;
212
213 if(D > 0.0)
214 {
215 // only one 'real' solution
216 const double s = std::sqrt(x: D);
217 return std::cbrt(x: s - q) - std::cbrt(x: s + q) - Substitute;
218 }
219 else if(D == 0.0)
220 {
221 // one single, one double solution or triple solution
222 const double s = std::cbrt(x: -q);
223 const double t = 2.0 * s - Substitute;
224
225 if(0.0 <= t && t <= 1.0001)
226 return t;
227 return (-s - Substitute);
228 }
229 else
230 {
231 // Casus irreducibilis ... ,_,
232 const double Phi = std::acos(x: -q / std::sqrt(x: -(p * p * p))) / 3.0;
233 const double s = 2.0 * std::sqrt(x: -p);
234
235 const double t1 = s * std::cos(x: Phi) - Substitute;
236
237 if(0.0 <= t1 && t1 <= 1.0001)
238 return t1;
239
240 const double t2 = -s * std::cos(x: Phi + pi / 3.0) - Substitute;
241
242 if(0.0 <= t2 && t2 <= 1.0001)
243 return t2;
244 return -s * std::cos(x: Phi - pi / 3.0) - Substitute;
245 }
246 }
247}
248
249void CRenderMap::Init(IGraphics *pGraphics, ITextRender *pTextRender)
250{
251 m_pGraphics = pGraphics;
252 m_pTextRender = pTextRender;
253}
254
255void CRenderMap::RenderEvalEnvelope(const IEnvelopePointAccess *pPoints, std::chrono::nanoseconds TimeNanos, ColorRGBA &Result, size_t Channels)
256{
257 const int NumPoints = pPoints->NumPoints();
258 if(NumPoints == 0)
259 {
260 return;
261 }
262
263 if(NumPoints == 1)
264 {
265 const CEnvPoint *pFirstPoint = pPoints->GetPoint(Index: 0);
266 for(size_t c = 0; c < Channels; c++)
267 {
268 Result[c] = fx2f(v: pFirstPoint->m_aValues[c]);
269 }
270 return;
271 }
272
273 const CEnvPoint *pLastPoint = pPoints->GetPoint(Index: NumPoints - 1);
274 const int64_t MaxPointTime = (int64_t)pLastPoint->m_Time.GetInternal() * std::chrono::nanoseconds(1ms).count();
275 if(MaxPointTime > 0) // TODO: remove this check when implementing a IO check for maps(in this case broken envelopes)
276 TimeNanos = std::chrono::nanoseconds(TimeNanos.count() % MaxPointTime);
277 else
278 TimeNanos = decltype(TimeNanos)::zero();
279
280 const double TimeMillis = TimeNanos.count() / (double)std::chrono::nanoseconds(1ms).count();
281
282 int FoundIndex = pPoints->FindPointIndex(Time: CFixedTime(TimeMillis));
283 if(FoundIndex == -1)
284 {
285 for(size_t c = 0; c < Channels; c++)
286 {
287 Result[c] = fx2f(v: pLastPoint->m_aValues[c]);
288 }
289 return;
290 }
291
292 const CEnvPoint *pCurrentPoint = pPoints->GetPoint(Index: FoundIndex);
293 const CEnvPoint *pNextPoint = pPoints->GetPoint(Index: FoundIndex + 1);
294
295 const CFixedTime Delta = pNextPoint->m_Time - pCurrentPoint->m_Time;
296 if(Delta <= CFixedTime(0))
297 {
298 for(size_t c = 0; c < Channels; c++)
299 {
300 Result[c] = fx2f(v: pCurrentPoint->m_aValues[c]);
301 }
302 return;
303 }
304
305 float a = (float)(TimeMillis - pCurrentPoint->m_Time.GetInternal()) / Delta.GetInternal();
306
307 switch(pCurrentPoint->m_Curvetype)
308 {
309 case CURVETYPE_STEP:
310 a = 0.0f;
311 break;
312
313 case CURVETYPE_SLOW:
314 a = a * a * a;
315 break;
316
317 case CURVETYPE_FAST:
318 a = 1.0f - a;
319 a = 1.0f - a * a * a;
320 break;
321
322 case CURVETYPE_SMOOTH:
323 a = -2.0f * a * a * a + 3.0f * a * a; // second hermite basis
324 break;
325
326 case CURVETYPE_BEZIER:
327 {
328 const CEnvPointBezier *pCurrentPointBezier = pPoints->GetBezier(Index: FoundIndex);
329 const CEnvPointBezier *pNextPointBezier = pPoints->GetBezier(Index: FoundIndex + 1);
330 if(pCurrentPointBezier == nullptr || pNextPointBezier == nullptr)
331 break; // fallback to linear
332 for(size_t c = 0; c < Channels; c++)
333 {
334 // monotonic 2d cubic bezier curve
335 const vec2 p0 = vec2(pCurrentPoint->m_Time.GetInternal(), fx2f(v: pCurrentPoint->m_aValues[c]));
336 const vec2 p3 = vec2(pNextPoint->m_Time.GetInternal(), fx2f(v: pNextPoint->m_aValues[c]));
337
338 const vec2 OutTang = vec2(pCurrentPointBezier->m_aOutTangentDeltaX[c].GetInternal(), fx2f(v: pCurrentPointBezier->m_aOutTangentDeltaY[c]));
339 const vec2 InTang = vec2(pNextPointBezier->m_aInTangentDeltaX[c].GetInternal(), fx2f(v: pNextPointBezier->m_aInTangentDeltaY[c]));
340
341 vec2 p1 = p0 + OutTang;
342 vec2 p2 = p3 + InTang;
343
344 // validate bezier curve
345 p1.x = std::clamp(val: p1.x, lo: p0.x, hi: p3.x);
346 p2.x = std::clamp(val: p2.x, lo: p0.x, hi: p3.x);
347
348 // solve x(a) = time for a
349 a = std::clamp(val: SolveBezier(x: TimeMillis, p0: p0.x, p1: p1.x, p2: p2.x, p3: p3.x), lo: 0.0f, hi: 1.0f);
350
351 // value = y(t)
352 Result[c] = bezier(p0: p0.y, p1: p1.y, p2: p2.y, p3: p3.y, amount: a);
353 }
354 return;
355 }
356
357 case CURVETYPE_LINEAR: [[fallthrough]];
358 default:
359 break;
360 }
361
362 for(size_t c = 0; c < Channels; c++)
363 {
364 const float v0 = fx2f(v: pCurrentPoint->m_aValues[c]);
365 const float v1 = fx2f(v: pNextPoint->m_aValues[c]);
366 Result[c] = v0 + (v1 - v0) * a;
367 }
368}
369
370void CRenderMap::ForceRenderQuads(const CQuad *pQuads, int NumQuads, const IEnvelopeEval *pEnvEval, float Alpha)
371{
372 Graphics()->TrianglesBegin();
373 float Conv = 1 / 255.0f;
374 for(int i = 0; i < NumQuads; i++)
375 {
376 const CQuad *pQuad = &pQuads[i];
377
378 ColorRGBA Color = ColorRGBA(1.0f, 1.0f, 1.0f, 1.0f);
379 pEnvEval->EnvelopeEval(TimeOffsetMillis: pQuad->m_ColorEnvOffset, EnvelopeIndex: pQuad->m_ColorEnv, Result&: Color, Channels: 4);
380
381 if(Color.a <= 0.0f)
382 continue;
383
384 Graphics()->QuadsSetSubsetFree(
385 x0: fx2f(v: pQuad->m_aTexcoords[0].x), y0: fx2f(v: pQuad->m_aTexcoords[0].y),
386 x1: fx2f(v: pQuad->m_aTexcoords[1].x), y1: fx2f(v: pQuad->m_aTexcoords[1].y),
387 x2: fx2f(v: pQuad->m_aTexcoords[2].x), y2: fx2f(v: pQuad->m_aTexcoords[2].y),
388 x3: fx2f(v: pQuad->m_aTexcoords[3].x), y3: fx2f(v: pQuad->m_aTexcoords[3].y));
389
390 ColorRGBA Position = ColorRGBA(0.0f, 0.0f, 0.0f, 0.0f);
391 pEnvEval->EnvelopeEval(TimeOffsetMillis: pQuad->m_PosEnvOffset, EnvelopeIndex: pQuad->m_PosEnv, Result&: Position, Channels: 3);
392 const vec2 Offset = vec2(Position.r, Position.g);
393 const float Rotation = Position.b / 180.0f * pi;
394
395 Graphics()->SetColor4(
396 TopLeft: ColorRGBA(pQuad->m_aColors[0].r, pQuad->m_aColors[0].g, pQuad->m_aColors[0].b, pQuad->m_aColors[0].a * Alpha).Multiply(Other: Color).Multiply(Factor: Conv),
397 TopRight: ColorRGBA(pQuad->m_aColors[1].r, pQuad->m_aColors[1].g, pQuad->m_aColors[1].b, pQuad->m_aColors[1].a * Alpha).Multiply(Other: Color).Multiply(Factor: Conv),
398 BottomLeft: ColorRGBA(pQuad->m_aColors[3].r, pQuad->m_aColors[3].g, pQuad->m_aColors[3].b, pQuad->m_aColors[3].a * Alpha).Multiply(Other: Color).Multiply(Factor: Conv),
399 BottomRight: ColorRGBA(pQuad->m_aColors[2].r, pQuad->m_aColors[2].g, pQuad->m_aColors[2].b, pQuad->m_aColors[2].a * Alpha).Multiply(Other: Color).Multiply(Factor: Conv));
400
401 const CPoint *pPoints;
402 CPoint aRotated[4];
403 if(Rotation == 0.0f)
404 {
405 pPoints = pQuad->m_aPoints;
406 }
407 else
408 {
409 std::copy_n(first: pQuad->m_aPoints, n: std::size(aRotated), result: aRotated);
410 RotatePoints(Center: pQuad->m_aPoints[4], pPoints: aRotated, NumPoints: std::size(aRotated), Rotation);
411 pPoints = aRotated;
412 }
413
414 IGraphics::CFreeformItem Freeform(
415 fx2f(v: pPoints[0].x) + Offset.x, fx2f(v: pPoints[0].y) + Offset.y,
416 fx2f(v: pPoints[1].x) + Offset.x, fx2f(v: pPoints[1].y) + Offset.y,
417 fx2f(v: pPoints[2].x) + Offset.x, fx2f(v: pPoints[2].y) + Offset.y,
418 fx2f(v: pPoints[3].x) + Offset.x, fx2f(v: pPoints[3].y) + Offset.y);
419 Graphics()->QuadsDrawFreeform(pArray: &Freeform, Num: 1);
420 }
421 Graphics()->TrianglesEnd();
422}
423
424void CRenderMap::RenderTileRectangle(int RectX, int RectY, int RectW, int RectH,
425 unsigned char IndexIn, unsigned char IndexOut,
426 float Scale, ColorRGBA Color)
427{
428 CScreenRect ScreenRect = Graphics()->GetScreen();
429
430 // calculate the final pixelsize for the tiles
431 float TilePixelSize = 1024 / 32.0f;
432 float FinalTileSize = Scale / ScreenRect.Width() * Graphics()->ScreenWidth();
433 float FinalTilesetScale = FinalTileSize / TilePixelSize;
434
435 if(Graphics()->HasTextureArraysSupport())
436 Graphics()->QuadsTex3DBegin();
437 else
438 Graphics()->QuadsBegin();
439 Graphics()->SetColor(Color);
440
441 int StartY = (int)(ScreenRect.m_TopLeft.y / Scale) - 1;
442 int StartX = (int)(ScreenRect.m_TopLeft.x / Scale) - 1;
443 int EndY = (int)(ScreenRect.m_BottomRight.y / Scale) + 1;
444 int EndX = (int)(ScreenRect.m_BottomRight.x / Scale) + 1;
445
446 // adjust the texture shift according to mipmap level
447 float TexSize = 1024.0f;
448 float Frac = (1.25f / TexSize) * (1 / FinalTilesetScale);
449 float Nudge = (0.5f / TexSize) * (1 / FinalTilesetScale);
450
451 for(int y = StartY; y < EndY; y++)
452 {
453 for(int x = StartX; x < EndX; x++)
454 {
455 unsigned char Index = (x >= RectX && x < RectX + RectW && y >= RectY && y < RectY + RectH) ? IndexIn : IndexOut;
456 if(Index)
457 {
458 int tx = Index % 16;
459 int ty = Index / 16;
460 int Px0 = tx * (1024 / 16);
461 int Py0 = ty * (1024 / 16);
462 int Px1 = Px0 + (1024 / 16) - 1;
463 int Py1 = Py0 + (1024 / 16) - 1;
464
465 float x0 = Nudge + Px0 / TexSize + Frac;
466 float y0 = Nudge + Py0 / TexSize + Frac;
467 float x1 = Nudge + Px1 / TexSize - Frac;
468 float y1 = Nudge + Py0 / TexSize + Frac;
469 float x2 = Nudge + Px1 / TexSize - Frac;
470 float y2 = Nudge + Py1 / TexSize - Frac;
471 float x3 = Nudge + Px0 / TexSize + Frac;
472 float y3 = Nudge + Py1 / TexSize - Frac;
473
474 if(Graphics()->HasTextureArraysSupport())
475 {
476 x0 = 0;
477 y0 = 0;
478 x1 = x0 + 1;
479 y1 = y0;
480 x2 = x0 + 1;
481 y2 = y0 + 1;
482 x3 = x0;
483 y3 = y0 + 1;
484 }
485
486 if(Graphics()->HasTextureArraysSupport())
487 {
488 Graphics()->QuadsSetSubsetFree(x0, y0, x1, y1, x2, y2, x3, y3, Index);
489 IGraphics::CQuadItem QuadItem(x * Scale, y * Scale, Scale, Scale);
490 Graphics()->QuadsTex3DDrawTL(pArray: &QuadItem, Num: 1);
491 }
492 else
493 {
494 Graphics()->QuadsSetSubsetFree(x0, y0, x1, y1, x2, y2, x3, y3);
495 IGraphics::CQuadItem QuadItem(x * Scale, y * Scale, Scale, Scale);
496 Graphics()->QuadsDrawTL(pArray: &QuadItem, Num: 1);
497 }
498 }
499 }
500 }
501
502 if(Graphics()->HasTextureArraysSupport())
503 Graphics()->QuadsTex3DEnd();
504 else
505 Graphics()->QuadsEnd();
506 Graphics()->MapScreen(ScreenRect);
507}
508
509void CRenderMap::RenderTile(int x, int y, unsigned char Index, float Scale, ColorRGBA Color)
510{
511 if(Graphics()->HasTextureArraysSupport())
512 Graphics()->QuadsTex3DBegin();
513 else
514 Graphics()->QuadsBegin();
515
516 CScreenRect ScreenRect = Graphics()->GetScreen();
517
518 // calculate the final pixelsize for the tiles
519 float TilePixelSize = 1024 / Scale;
520 float FinalTileSize = Scale / ScreenRect.Width() * Graphics()->ScreenWidth();
521 float FinalTilesetScale = FinalTileSize / TilePixelSize;
522
523 float TexSize = 1024.0f;
524 float Frac = (1.25f / TexSize) * (1 / FinalTilesetScale);
525 float Nudge = (0.5f / TexSize) * (1 / FinalTilesetScale);
526
527 int tx = Index % 16;
528 int ty = Index / 16;
529 int Px0 = tx * (1024 / 16);
530 int Py0 = ty * (1024 / 16);
531 int Px1 = Px0 + (1024 / 16) - 1;
532 int Py1 = Py0 + (1024 / 16) - 1;
533
534 float x0 = Nudge + Px0 / TexSize + Frac;
535 float y0 = Nudge + Py0 / TexSize + Frac;
536 float x1 = Nudge + Px1 / TexSize - Frac;
537 float y1 = Nudge + Py0 / TexSize + Frac;
538 float x2 = Nudge + Px1 / TexSize - Frac;
539 float y2 = Nudge + Py1 / TexSize - Frac;
540 float x3 = Nudge + Px0 / TexSize + Frac;
541 float y3 = Nudge + Py1 / TexSize - Frac;
542
543 if(Graphics()->HasTextureArraysSupport())
544 {
545 x0 = 0;
546 y0 = 0;
547 x1 = x0 + 1;
548 y1 = y0;
549 x2 = x0 + 1;
550 y2 = y0 + 1;
551 x3 = x0;
552 y3 = y0 + 1;
553 }
554
555 if(Graphics()->HasTextureArraysSupport())
556 {
557 Graphics()->QuadsSetSubsetFree(x0, y0, x1, y1, x2, y2, x3, y3, Index);
558 IGraphics::CQuadItem QuadItem(x, y, Scale, Scale);
559 Graphics()->QuadsTex3DDrawTL(pArray: &QuadItem, Num: 1);
560 }
561 else
562 {
563 Graphics()->QuadsSetSubsetFree(x0, y0, x1, y1, x2, y2, x3, y3);
564 IGraphics::CQuadItem QuadItem(x, y, Scale, Scale);
565 Graphics()->QuadsDrawTL(pArray: &QuadItem, Num: 1);
566 }
567
568 if(Graphics()->HasTextureArraysSupport())
569 Graphics()->QuadsTex3DEnd();
570 else
571 Graphics()->QuadsEnd();
572 Graphics()->MapScreen(ScreenRect);
573}
574
575void CRenderMap::RenderTilemap(const CTile *pTiles, int w, int h, float Scale, ColorRGBA Color, int RenderFlags)
576{
577 CScreenRect ScreenRect = Graphics()->GetScreen();
578
579 // calculate the final pixelsize for the tiles
580 float TilePixelSize = 1024 / 32.0f;
581 float FinalTileSize = Scale / ScreenRect.Width() * Graphics()->ScreenWidth();
582 float FinalTilesetScale = FinalTileSize / TilePixelSize;
583
584 if(Graphics()->HasTextureArraysSupport())
585 Graphics()->QuadsTex3DBegin();
586 else
587 Graphics()->QuadsBegin();
588 Graphics()->SetColor(Color);
589
590 const bool ExtendTiles = (RenderFlags & TILERENDERFLAG_EXTEND) != 0;
591 bool IgnoreOpaque;
592 bool RequireOpaque;
593 if(RenderFlags & LAYERRENDERFLAG_OPAQUE)
594 {
595 IgnoreOpaque = false;
596 RequireOpaque = true;
597 }
598 else if(RenderFlags & LAYERRENDERFLAG_TRANSPARENT)
599 {
600 IgnoreOpaque = false;
601 RequireOpaque = false;
602 }
603 else
604 {
605 IgnoreOpaque = true;
606 }
607
608 int StartY = (int)(ScreenRect.m_TopLeft.y / Scale) - 1;
609 int StartX = (int)(ScreenRect.m_TopLeft.x / Scale) - 1;
610 int EndY = (int)(ScreenRect.m_BottomRight.y / Scale) + 1;
611 int EndX = (int)(ScreenRect.m_BottomRight.x / Scale) + 1;
612 if(!ExtendTiles)
613 {
614 StartY = std::max(a: 0, b: StartY);
615 StartX = std::max(a: 0, b: StartX);
616 EndY = std::min(a: h, b: EndY);
617 EndX = std::min(a: w, b: EndX);
618 }
619
620 // adjust the texture shift according to mipmap level
621 float TexSize = 1024.0f;
622 float Frac = (1.25f / TexSize) * (1 / FinalTilesetScale);
623 float Nudge = (0.5f / TexSize) * (1 / FinalTilesetScale);
624
625 for(int y = StartY; y < EndY; y++)
626 {
627 for(int x = StartX; x < EndX; x++)
628 {
629 int mx = x;
630 int my = y;
631
632 if(ExtendTiles)
633 {
634 if(mx < 0)
635 mx = 0;
636 if(mx >= w)
637 mx = w - 1;
638 if(my < 0)
639 my = 0;
640 if(my >= h)
641 my = h - 1;
642 }
643
644 int c = mx + my * w;
645
646 unsigned char Index = pTiles[c].m_Index;
647 if(Index)
648 {
649 unsigned char Flags = pTiles[c].m_Flags;
650
651 if(IgnoreOpaque || ((Flags & TILEFLAG_OPAQUE) != 0) == RequireOpaque)
652 {
653 int tx = Index % 16;
654 int ty = Index / 16;
655 int Px0 = tx * (1024 / 16);
656 int Py0 = ty * (1024 / 16);
657 int Px1 = Px0 + (1024 / 16) - 1;
658 int Py1 = Py0 + (1024 / 16) - 1;
659
660 float x0 = Nudge + Px0 / TexSize + Frac;
661 float y0 = Nudge + Py0 / TexSize + Frac;
662 float x1 = Nudge + Px1 / TexSize - Frac;
663 float y1 = Nudge + Py0 / TexSize + Frac;
664 float x2 = Nudge + Px1 / TexSize - Frac;
665 float y2 = Nudge + Py1 / TexSize - Frac;
666 float x3 = Nudge + Px0 / TexSize + Frac;
667 float y3 = Nudge + Py1 / TexSize - Frac;
668
669 if(Graphics()->HasTextureArraysSupport())
670 {
671 x0 = 0;
672 y0 = 0;
673 x1 = x0 + 1;
674 y1 = y0;
675 x2 = x0 + 1;
676 y2 = y0 + 1;
677 x3 = x0;
678 y3 = y0 + 1;
679 }
680
681 if(Flags & TILEFLAG_XFLIP)
682 {
683 x0 = x2;
684 x1 = x3;
685 x2 = x3;
686 x3 = x0;
687 }
688
689 if(Flags & TILEFLAG_YFLIP)
690 {
691 y0 = y3;
692 y2 = y1;
693 y3 = y1;
694 y1 = y0;
695 }
696
697 if(Flags & TILEFLAG_ROTATE)
698 {
699 float Tmp = x0;
700 x0 = x3;
701 x3 = x2;
702 x2 = x1;
703 x1 = Tmp;
704 Tmp = y0;
705 y0 = y3;
706 y3 = y2;
707 y2 = y1;
708 y1 = Tmp;
709 }
710
711 if(Graphics()->HasTextureArraysSupport())
712 {
713 Graphics()->QuadsSetSubsetFree(x0, y0, x1, y1, x2, y2, x3, y3, Index);
714 IGraphics::CQuadItem QuadItem(x * Scale, y * Scale, Scale, Scale);
715 Graphics()->QuadsTex3DDrawTL(pArray: &QuadItem, Num: 1);
716 }
717 else
718 {
719 Graphics()->QuadsSetSubsetFree(x0, y0, x1, y1, x2, y2, x3, y3);
720 IGraphics::CQuadItem QuadItem(x * Scale, y * Scale, Scale, Scale);
721 Graphics()->QuadsDrawTL(pArray: &QuadItem, Num: 1);
722 }
723 }
724 }
725 x += pTiles[c].m_Skip;
726 }
727 }
728
729 if(Graphics()->HasTextureArraysSupport())
730 Graphics()->QuadsTex3DEnd();
731 else
732 Graphics()->QuadsEnd();
733 Graphics()->MapScreen(ScreenRect);
734}
735
736void CRenderMap::RenderTeleOverlay(const CTeleTile *pTele, int w, int h, float Scale, int OverlayRenderFlag, float Alpha)
737{
738 if(!(OverlayRenderFlag & OVERLAYRENDERFLAG_TEXT))
739 return;
740
741 CScreenRect ScreenRect = Graphics()->GetScreen();
742
743 int StartY = (int)(ScreenRect.m_TopLeft.y / Scale) - 1;
744 int StartX = (int)(ScreenRect.m_TopLeft.x / Scale) - 1;
745 int EndY = (int)(ScreenRect.m_BottomRight.y / Scale) + 1;
746 int EndX = (int)(ScreenRect.m_BottomRight.x / Scale) + 1;
747 if(EndX - StartX > Graphics()->ScreenWidth() / g_Config.m_GfxTextOverlay || EndY - StartY > Graphics()->ScreenHeight() / g_Config.m_GfxTextOverlay)
748 return; // its useless to render text at this distance
749
750 StartY = std::max(a: 0, b: StartY);
751 StartX = std::max(a: 0, b: StartX);
752 EndY = std::min(a: h, b: EndY);
753 EndX = std::min(a: w, b: EndX);
754
755 float Size = g_Config.m_ClTextEntitiesSize / 100.f;
756 char aBuf[16];
757
758 TextRender()->TextColor(r: 1.0f, g: 1.0f, b: 1.0f, a: Alpha);
759 for(int y = StartY; y < EndY; y++)
760 {
761 for(int x = StartX; x < EndX; x++)
762 {
763 int c = x + y * w;
764
765 unsigned char Index = pTele[c].m_Number;
766 if(Index && IsTeleTileNumberUsedAny(Index: pTele[c].m_Type))
767 {
768 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%d", Index);
769 // Auto-resize text to fit inside the tile
770 float ScaledWidth = TextRender()->TextWidth(Size: Size * Scale, pText: aBuf, StrLength: -1);
771 float Factor = std::clamp(val: Scale / ScaledWidth, lo: 0.0f, hi: 1.0f);
772 float LocalSize = Size * Factor;
773 float ToCenterOffset = (1 - LocalSize) / 2.f;
774 TextRender()->Text(x: (x + 0.5f) * Scale - (ScaledWidth * Factor) / 2.0f, y: (y + ToCenterOffset) * Scale, Size: LocalSize * Scale, pText: aBuf);
775 }
776 }
777 }
778 TextRender()->TextColor(Color: TextRender()->DefaultTextColor());
779 Graphics()->MapScreen(ScreenRect);
780}
781
782void CRenderMap::RenderSpeedupOverlay(const CSpeedupTile *pSpeedup, int w, int h, float Scale, int OverlayRenderFlag, float Alpha)
783{
784 CScreenRect ScreenRect = Graphics()->GetScreen();
785
786 int StartY = (int)(ScreenRect.m_TopLeft.y / Scale) - 1;
787 int StartX = (int)(ScreenRect.m_TopLeft.x / Scale) - 1;
788 int EndY = (int)(ScreenRect.m_BottomRight.y / Scale) + 1;
789 int EndX = (int)(ScreenRect.m_BottomRight.x / Scale) + 1;
790 if(EndX - StartX > Graphics()->ScreenWidth() / g_Config.m_GfxTextOverlay || EndY - StartY > Graphics()->ScreenHeight() / g_Config.m_GfxTextOverlay)
791 return; // its useless to render text at this distance
792
793 StartY = std::max(a: 0, b: StartY);
794 StartX = std::max(a: 0, b: StartX);
795 EndY = std::min(a: h, b: EndY);
796 EndX = std::min(a: w, b: EndX);
797
798 float Size = g_Config.m_ClTextEntitiesSize / 100.f;
799 float ToCenterOffset = (1 - Size) / 2.f;
800 char aBuf[16];
801
802 TextRender()->TextColor(r: 1.0f, g: 1.0f, b: 1.0f, a: Alpha);
803 for(int y = StartY; y < EndY; y++)
804 {
805 for(int x = StartX; x < EndX; x++)
806 {
807 int c = x + y * w;
808
809 int Force = (int)pSpeedup[c].m_Force;
810 int MaxSpeed = (int)pSpeedup[c].m_MaxSpeed;
811 int Type = (int)pSpeedup[c].m_Type;
812 int Angle = (int)pSpeedup[c].m_Angle;
813 if((Force && Type == TILE_SPEED_BOOST_OLD) || ((Force || MaxSpeed) && Type == TILE_SPEED_BOOST) || (OverlayRenderFlag & OVERLAYRENDERFLAG_EDITOR && (Type || Force || MaxSpeed || Angle)))
814 {
815 if(IsValidSpeedupTile(Index: Type))
816 {
817 // draw arrow
818 Graphics()->TextureSet(Texture: g_pData->m_aImages[IMAGE_SPEEDUP_ARROW].m_Id);
819 Graphics()->QuadsBegin();
820 Graphics()->SetColor(r: 1.0f, g: 1.0f, b: 1.0f, a: Alpha);
821 Graphics()->SelectSprite(Id: SPRITE_SPEEDUP_ARROW);
822 Graphics()->QuadsSetRotation(Angle: pSpeedup[c].m_Angle * (pi / 180.0f));
823 Graphics()->DrawSprite(x: x * Scale + 16, y: y * Scale + 16, Size: 35.0f);
824 Graphics()->QuadsEnd();
825
826 // draw force and max speed
827 if(OverlayRenderFlag & OVERLAYRENDERFLAG_TEXT)
828 {
829 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%d", Force);
830 TextRender()->Text(x: x * Scale, y: (y + 0.5f + ToCenterOffset / 2) * Scale, Size: Size * Scale / 2.f, pText: aBuf);
831 if(MaxSpeed)
832 {
833 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%d", MaxSpeed);
834 TextRender()->Text(x: x * Scale, y: (y + ToCenterOffset / 2) * Scale, Size: Size * Scale / 2.f, pText: aBuf);
835 }
836 }
837 }
838 else
839 {
840 // draw all three values
841 if(OverlayRenderFlag & OVERLAYRENDERFLAG_TEXT)
842 {
843 float LineSpacing = Size * Scale / 3.f;
844 float BaseY = (y + ToCenterOffset) * Scale;
845 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%d", Force);
846 TextRender()->Text(x: x * Scale, y: BaseY, Size: LineSpacing, pText: aBuf);
847 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%d", MaxSpeed);
848 TextRender()->Text(x: x * Scale, y: BaseY + LineSpacing, Size: LineSpacing, pText: aBuf);
849 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%d", Angle);
850 TextRender()->Text(x: x * Scale, y: BaseY + 2 * LineSpacing, Size: LineSpacing, pText: aBuf);
851 }
852 }
853 }
854 }
855 }
856 TextRender()->TextColor(Color: TextRender()->DefaultTextColor());
857 Graphics()->MapScreen(ScreenRect);
858}
859
860void CRenderMap::RenderSwitchOverlay(const CSwitchTile *pSwitch, int w, int h, float Scale, int OverlayRenderFlag, float Alpha)
861{
862 if(!(OverlayRenderFlag & OVERLAYRENDERFLAG_TEXT))
863 return;
864
865 CScreenRect ScreenRect = Graphics()->GetScreen();
866
867 int StartY = (int)(ScreenRect.m_TopLeft.y / Scale) - 1;
868 int StartX = (int)(ScreenRect.m_TopLeft.x / Scale) - 1;
869 int EndY = (int)(ScreenRect.m_BottomRight.y / Scale) + 1;
870 int EndX = (int)(ScreenRect.m_BottomRight.x / Scale) + 1;
871 if(EndX - StartX > Graphics()->ScreenWidth() / g_Config.m_GfxTextOverlay || EndY - StartY > Graphics()->ScreenHeight() / g_Config.m_GfxTextOverlay)
872 return; // its useless to render text at this distance
873
874 StartY = std::max(a: 0, b: StartY);
875 StartX = std::max(a: 0, b: StartX);
876 EndY = std::min(a: h, b: EndY);
877 EndX = std::min(a: w, b: EndX);
878
879 float Size = g_Config.m_ClTextEntitiesSize / 100.f;
880 float ToCenterOffset = (1 - Size) / 2.f;
881 char aBuf[16];
882
883 TextRender()->TextColor(r: 1.0f, g: 1.0f, b: 1.0f, a: Alpha);
884 for(int y = StartY; y < EndY; y++)
885 {
886 for(int x = StartX; x < EndX; x++)
887 {
888 int c = x + y * w;
889
890 unsigned char Index = pSwitch[c].m_Number;
891 if(Index && IsSwitchTileNumberUsed(Index: pSwitch[c].m_Type))
892 {
893 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%d", Index);
894 TextRender()->Text(x: x * Scale, y: (y + ToCenterOffset / 2) * Scale, Size: Size * Scale / 2.f, pText: aBuf);
895 }
896
897 unsigned char Delay = pSwitch[c].m_Delay;
898 if(Delay && IsSwitchTileDelayUsed(Index: pSwitch[c].m_Type))
899 {
900 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%d", Delay);
901 TextRender()->Text(x: x * Scale, y: (y + 0.5f + ToCenterOffset / 2) * Scale, Size: Size * Scale / 2.f, pText: aBuf);
902 }
903 }
904 }
905 TextRender()->TextColor(Color: TextRender()->DefaultTextColor());
906 Graphics()->MapScreen(ScreenRect);
907}
908
909void CRenderMap::RenderTuneOverlay(const CTuneTile *pTune, int w, int h, float Scale, int OverlayRenderFlag, float Alpha)
910{
911 if(!(OverlayRenderFlag & OVERLAYRENDERFLAG_TEXT))
912 return;
913
914 CScreenRect ScreenRect = Graphics()->GetScreen();
915
916 int StartY = (int)(ScreenRect.m_TopLeft.y / Scale) - 1;
917 int StartX = (int)(ScreenRect.m_TopLeft.x / Scale) - 1;
918 int EndY = (int)(ScreenRect.m_BottomRight.y / Scale) + 1;
919 int EndX = (int)(ScreenRect.m_BottomRight.x / Scale) + 1;
920 if(EndX - StartX > Graphics()->ScreenWidth() / g_Config.m_GfxTextOverlay || EndY - StartY > Graphics()->ScreenHeight() / g_Config.m_GfxTextOverlay)
921 return; // its useless to render text at this distance
922
923 StartY = std::max(a: 0, b: StartY);
924 StartX = std::max(a: 0, b: StartX);
925 EndY = std::min(a: h, b: EndY);
926 EndX = std::min(a: w, b: EndX);
927
928 float Size = g_Config.m_ClTextEntitiesSize / 200.f;
929 char aBuf[16];
930
931 TextRender()->TextColor(r: 1.0f, g: 1.0f, b: 1.0f, a: Alpha);
932 for(int y = StartY; y < EndY; y++)
933 {
934 for(int x = StartX; x < EndX; x++)
935 {
936 int c = x + y * w;
937
938 unsigned char Index = pTune[c].m_Number;
939 if(Index)
940 {
941 str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%d", Index);
942 // Auto-resize text to fit inside the tile
943 float ScaledWidth = TextRender()->TextWidth(Size: Size * Scale, pText: aBuf, StrLength: -1);
944 float Factor = std::clamp(val: Scale / ScaledWidth, lo: 0.0f, hi: 1.0f);
945 float LocalSize = Size * Factor;
946 float ToCenterOffset = (1 - LocalSize) / 2.f;
947 TextRender()->Text(x: (x + 0.5f) * Scale - (ScaledWidth * Factor) / 2.0f, y: (y + ToCenterOffset) * Scale, Size: LocalSize * Scale, pText: aBuf);
948 }
949 }
950 }
951 TextRender()->TextColor(Color: TextRender()->DefaultTextColor());
952 Graphics()->MapScreen(ScreenRect);
953}
954
955void CRenderMap::RenderTelemap(const CTeleTile *pTele, int w, int h, float Scale, ColorRGBA Color, bool ExtendTiles)
956{
957 CScreenRect ScreenRect = Graphics()->GetScreen();
958
959 // calculate the final pixelsize for the tiles
960 float TilePixelSize = 1024 / 32.0f;
961 float FinalTileSize = Scale / ScreenRect.Width() * Graphics()->ScreenWidth();
962 float FinalTilesetScale = FinalTileSize / TilePixelSize;
963
964 if(Graphics()->HasTextureArraysSupport())
965 Graphics()->QuadsTex3DBegin();
966 else
967 Graphics()->QuadsBegin();
968 Graphics()->SetColor(Color);
969
970 int StartY = (int)(ScreenRect.m_TopLeft.y / Scale) - 1;
971 int StartX = (int)(ScreenRect.m_TopLeft.x / Scale) - 1;
972 int EndY = (int)(ScreenRect.m_BottomRight.y / Scale) + 1;
973 int EndX = (int)(ScreenRect.m_BottomRight.x / Scale) + 1;
974 if(!ExtendTiles)
975 {
976 StartY = std::max(a: 0, b: StartY);
977 StartX = std::max(a: 0, b: StartX);
978 EndY = std::min(a: h, b: EndY);
979 EndX = std::min(a: w, b: EndX);
980 }
981
982 // adjust the texture shift according to mipmap level
983 float TexSize = 1024.0f;
984 float Frac = (1.25f / TexSize) * (1 / FinalTilesetScale);
985 float Nudge = (0.5f / TexSize) * (1 / FinalTilesetScale);
986
987 for(int y = StartY; y < EndY; y++)
988 for(int x = StartX; x < EndX; x++)
989 {
990 int mx = x;
991 int my = y;
992
993 if(ExtendTiles)
994 {
995 if(mx < 0)
996 mx = 0;
997 if(mx >= w)
998 mx = w - 1;
999 if(my < 0)
1000 my = 0;
1001 if(my >= h)
1002 my = h - 1;
1003 }
1004
1005 int c = mx + my * w;
1006
1007 unsigned char Index = pTele[c].m_Type;
1008 if(Index)
1009 {
1010 int tx = Index % 16;
1011 int ty = Index / 16;
1012 int Px0 = tx * (1024 / 16);
1013 int Py0 = ty * (1024 / 16);
1014 int Px1 = Px0 + (1024 / 16) - 1;
1015 int Py1 = Py0 + (1024 / 16) - 1;
1016
1017 float x0 = Nudge + Px0 / TexSize + Frac;
1018 float y0 = Nudge + Py0 / TexSize + Frac;
1019 float x1 = Nudge + Px1 / TexSize - Frac;
1020 float y1 = Nudge + Py0 / TexSize + Frac;
1021 float x2 = Nudge + Px1 / TexSize - Frac;
1022 float y2 = Nudge + Py1 / TexSize - Frac;
1023 float x3 = Nudge + Px0 / TexSize + Frac;
1024 float y3 = Nudge + Py1 / TexSize - Frac;
1025
1026 if(Graphics()->HasTextureArraysSupport())
1027 {
1028 x0 = 0;
1029 y0 = 0;
1030 x1 = x0 + 1;
1031 y1 = y0;
1032 x2 = x0 + 1;
1033 y2 = y0 + 1;
1034 x3 = x0;
1035 y3 = y0 + 1;
1036 }
1037
1038 if(Graphics()->HasTextureArraysSupport())
1039 {
1040 Graphics()->QuadsSetSubsetFree(x0, y0, x1, y1, x2, y2, x3, y3, Index);
1041 IGraphics::CQuadItem QuadItem(x * Scale, y * Scale, Scale, Scale);
1042 Graphics()->QuadsTex3DDrawTL(pArray: &QuadItem, Num: 1);
1043 }
1044 else
1045 {
1046 Graphics()->QuadsSetSubsetFree(x0, y0, x1, y1, x2, y2, x3, y3);
1047 IGraphics::CQuadItem QuadItem(x * Scale, y * Scale, Scale, Scale);
1048 Graphics()->QuadsDrawTL(pArray: &QuadItem, Num: 1);
1049 }
1050 }
1051 }
1052
1053 if(Graphics()->HasTextureArraysSupport())
1054 Graphics()->QuadsTex3DEnd();
1055 else
1056 Graphics()->QuadsEnd();
1057 Graphics()->MapScreen(ScreenRect);
1058}
1059
1060void CRenderMap::RenderSwitchmap(const CSwitchTile *pSwitchTile, int w, int h, float Scale, ColorRGBA Color, bool ExtendTiles)
1061{
1062 CScreenRect ScreenRect = Graphics()->GetScreen();
1063
1064 // calculate the final pixelsize for the tiles
1065 float TilePixelSize = 1024 / 32.0f;
1066 float FinalTileSize = Scale / ScreenRect.Width() * Graphics()->ScreenWidth();
1067 float FinalTilesetScale = FinalTileSize / TilePixelSize;
1068
1069 if(Graphics()->HasTextureArraysSupport())
1070 Graphics()->QuadsTex3DBegin();
1071 else
1072 Graphics()->QuadsBegin();
1073 Graphics()->SetColor(Color);
1074
1075 int StartY = (int)(ScreenRect.m_TopLeft.y / Scale) - 1;
1076 int StartX = (int)(ScreenRect.m_TopLeft.x / Scale) - 1;
1077 int EndY = (int)(ScreenRect.m_BottomRight.y / Scale) + 1;
1078 int EndX = (int)(ScreenRect.m_BottomRight.x / Scale) + 1;
1079 if(!ExtendTiles)
1080 {
1081 StartY = std::max(a: 0, b: StartY);
1082 StartX = std::max(a: 0, b: StartX);
1083 EndY = std::min(a: h, b: EndY);
1084 EndX = std::min(a: w, b: EndX);
1085 }
1086
1087 // adjust the texture shift according to mipmap level
1088 float TexSize = 1024.0f;
1089 float Frac = (1.25f / TexSize) * (1 / FinalTilesetScale);
1090 float Nudge = (0.5f / TexSize) * (1 / FinalTilesetScale);
1091
1092 for(int y = StartY; y < EndY; y++)
1093 for(int x = StartX; x < EndX; x++)
1094 {
1095 int mx = x;
1096 int my = y;
1097
1098 if(ExtendTiles)
1099 {
1100 if(mx < 0)
1101 mx = 0;
1102 if(mx >= w)
1103 mx = w - 1;
1104 if(my < 0)
1105 my = 0;
1106 if(my >= h)
1107 my = h - 1;
1108 }
1109
1110 int c = mx + my * w;
1111
1112 unsigned char Index = pSwitchTile[c].m_Type;
1113 if(Index)
1114 {
1115 if(Index == TILE_SWITCHTIMEDOPEN)
1116 Index = 8;
1117
1118 unsigned char Flags = pSwitchTile[c].m_Flags;
1119
1120 int tx = Index % 16;
1121 int ty = Index / 16;
1122 int Px0 = tx * (1024 / 16);
1123 int Py0 = ty * (1024 / 16);
1124 int Px1 = Px0 + (1024 / 16) - 1;
1125 int Py1 = Py0 + (1024 / 16) - 1;
1126
1127 float x0 = Nudge + Px0 / TexSize + Frac;
1128 float y0 = Nudge + Py0 / TexSize + Frac;
1129 float x1 = Nudge + Px1 / TexSize - Frac;
1130 float y1 = Nudge + Py0 / TexSize + Frac;
1131 float x2 = Nudge + Px1 / TexSize - Frac;
1132 float y2 = Nudge + Py1 / TexSize - Frac;
1133 float x3 = Nudge + Px0 / TexSize + Frac;
1134 float y3 = Nudge + Py1 / TexSize - Frac;
1135
1136 if(Graphics()->HasTextureArraysSupport())
1137 {
1138 x0 = 0;
1139 y0 = 0;
1140 x1 = x0 + 1;
1141 y1 = y0;
1142 x2 = x0 + 1;
1143 y2 = y0 + 1;
1144 x3 = x0;
1145 y3 = y0 + 1;
1146 }
1147
1148 if(Flags & TILEFLAG_XFLIP)
1149 {
1150 x0 = x2;
1151 x1 = x3;
1152 x2 = x3;
1153 x3 = x0;
1154 }
1155
1156 if(Flags & TILEFLAG_YFLIP)
1157 {
1158 y0 = y3;
1159 y2 = y1;
1160 y3 = y1;
1161 y1 = y0;
1162 }
1163
1164 if(Flags & TILEFLAG_ROTATE)
1165 {
1166 float Tmp = x0;
1167 x0 = x3;
1168 x3 = x2;
1169 x2 = x1;
1170 x1 = Tmp;
1171 Tmp = y0;
1172 y0 = y3;
1173 y3 = y2;
1174 y2 = y1;
1175 y1 = Tmp;
1176 }
1177
1178 if(Graphics()->HasTextureArraysSupport())
1179 {
1180 Graphics()->QuadsSetSubsetFree(x0, y0, x1, y1, x2, y2, x3, y3, Index);
1181 IGraphics::CQuadItem QuadItem(x * Scale, y * Scale, Scale, Scale);
1182 Graphics()->QuadsTex3DDrawTL(pArray: &QuadItem, Num: 1);
1183 }
1184 else
1185 {
1186 Graphics()->QuadsSetSubsetFree(x0, y0, x1, y1, x2, y2, x3, y3);
1187 IGraphics::CQuadItem QuadItem(x * Scale, y * Scale, Scale, Scale);
1188 Graphics()->QuadsDrawTL(pArray: &QuadItem, Num: 1);
1189 }
1190 }
1191 }
1192
1193 if(Graphics()->HasTextureArraysSupport())
1194 Graphics()->QuadsTex3DEnd();
1195 else
1196 Graphics()->QuadsEnd();
1197 Graphics()->MapScreen(ScreenRect);
1198}
1199
1200void CRenderMap::RenderTunemap(const CTuneTile *pTune, int w, int h, float Scale, ColorRGBA Color, bool ExtendTiles, CTuneColorMapper *pTuneColorMapper)
1201{
1202 CScreenRect ScreenRect = Graphics()->GetScreen();
1203
1204 // calculate the final pixelsize for the tiles
1205 float TilePixelSize = 1024 / 32.0f;
1206 float FinalTileSize = Scale / ScreenRect.Width() * Graphics()->ScreenWidth();
1207 float FinalTilesetScale = FinalTileSize / TilePixelSize;
1208
1209 if(Graphics()->HasTextureArraysSupport())
1210 Graphics()->QuadsTex3DBegin();
1211 else
1212 Graphics()->QuadsBegin();
1213 Graphics()->SetColor(Color);
1214
1215 int StartY = (int)(ScreenRect.m_TopLeft.y / Scale) - 1;
1216 int StartX = (int)(ScreenRect.m_TopLeft.x / Scale) - 1;
1217 int EndY = (int)(ScreenRect.m_BottomRight.y / Scale) + 1;
1218 int EndX = (int)(ScreenRect.m_BottomRight.x / Scale) + 1;
1219 if(!ExtendTiles)
1220 {
1221 StartY = std::max(a: 0, b: StartY);
1222 StartX = std::max(a: 0, b: StartX);
1223 EndY = std::min(a: h, b: EndY);
1224 EndX = std::min(a: w, b: EndX);
1225 }
1226
1227 // adjust the texture shift according to mipmap level
1228 float TexSize = 1024.0f;
1229 float Frac = (1.25f / TexSize) * (1 / FinalTilesetScale);
1230 float Nudge = (0.5f / TexSize) * (1 / FinalTilesetScale);
1231
1232 for(int y = StartY; y < EndY; y++)
1233 for(int x = StartX; x < EndX; x++)
1234 {
1235 int mx = x;
1236 int my = y;
1237
1238 if(ExtendTiles)
1239 {
1240 if(mx < 0)
1241 mx = 0;
1242 if(mx >= w)
1243 mx = w - 1;
1244 if(my < 0)
1245 my = 0;
1246 if(my >= h)
1247 my = h - 1;
1248 }
1249
1250 int c = mx + my * w;
1251
1252 const unsigned char Index = pTune[c].m_Type;
1253
1254 if(Index)
1255 {
1256 const unsigned char Number = pTune[c].m_Number;
1257
1258 if(Number == 0 || pTuneColorMapper == nullptr)
1259 Graphics()->SetColor(Color);
1260 else
1261 {
1262 uint8_t ColorIndex = pTuneColorMapper->TuneNumberToColorIndex(TuneNumber: Number);
1263 Graphics()->SetColor(pTuneColorMapper->TuneColorIndexToColor(TuneColorIndex: ColorIndex).Multiply(Other: Color));
1264 }
1265
1266 int tx = Index % 16;
1267 int ty = Index / 16;
1268 int Px0 = tx * (1024 / 16);
1269 int Py0 = ty * (1024 / 16);
1270 int Px1 = Px0 + (1024 / 16) - 1;
1271 int Py1 = Py0 + (1024 / 16) - 1;
1272
1273 float x0 = Nudge + Px0 / TexSize + Frac;
1274 float y0 = Nudge + Py0 / TexSize + Frac;
1275 float x1 = Nudge + Px1 / TexSize - Frac;
1276 float y1 = Nudge + Py0 / TexSize + Frac;
1277 float x2 = Nudge + Px1 / TexSize - Frac;
1278 float y2 = Nudge + Py1 / TexSize - Frac;
1279 float x3 = Nudge + Px0 / TexSize + Frac;
1280 float y3 = Nudge + Py1 / TexSize - Frac;
1281
1282 if(Graphics()->HasTextureArraysSupport())
1283 {
1284 x0 = 0;
1285 y0 = 0;
1286 x1 = x0 + 1;
1287 y1 = y0;
1288 x2 = x0 + 1;
1289 y2 = y0 + 1;
1290 x3 = x0;
1291 y3 = y0 + 1;
1292 }
1293
1294 if(Graphics()->HasTextureArraysSupport())
1295 {
1296 Graphics()->QuadsSetSubsetFree(x0, y0, x1, y1, x2, y2, x3, y3, Index);
1297 IGraphics::CQuadItem QuadItem(x * Scale, y * Scale, Scale, Scale);
1298 Graphics()->QuadsTex3DDrawTL(pArray: &QuadItem, Num: 1);
1299 }
1300 else
1301 {
1302 Graphics()->QuadsSetSubsetFree(x0, y0, x1, y1, x2, y2, x3, y3);
1303 IGraphics::CQuadItem QuadItem(x * Scale, y * Scale, Scale, Scale);
1304 Graphics()->QuadsDrawTL(pArray: &QuadItem, Num: 1);
1305 }
1306 }
1307 }
1308
1309 if(Graphics()->HasTextureArraysSupport())
1310 Graphics()->QuadsTex3DEnd();
1311 else
1312 Graphics()->QuadsEnd();
1313 Graphics()->MapScreen(ScreenRect);
1314}
1315
1316void CRenderMap::RenderDebugClip(float ClipX, float ClipY, float ClipW, float ClipH, ColorRGBA Color, float Zoom, const char *pLabel)
1317{
1318 Graphics()->TextureClear();
1319 Graphics()->LinesBegin();
1320 Graphics()->SetColor(Color);
1321 IGraphics::CLineItem aLineItems[] = {
1322 IGraphics::CLineItem(ClipX, ClipY, ClipX, ClipY + ClipH),
1323 IGraphics::CLineItem(ClipX + ClipW, ClipY, ClipX + ClipW, ClipY + ClipH),
1324 IGraphics::CLineItem(ClipX, ClipY, ClipX + ClipW, ClipY),
1325 IGraphics::CLineItem(ClipX, ClipY + ClipH, ClipX + ClipW, ClipY + ClipH),
1326 };
1327 Graphics()->LinesDraw(pArray: aLineItems, Num: std::size(aLineItems));
1328 Graphics()->LinesEnd();
1329
1330 TextRender()->TextColor(Color);
1331
1332 // clamp zoom and set line width, because otherwise the text can be partially clipped out
1333 TextRender()->Text(x: ClipX, y: ClipY, Size: std::min(a: 12.0f * Zoom, b: 20.0f), pText: pLabel, LineWidth: ClipW);
1334 TextRender()->TextColor(Color: TextRender()->DefaultTextColor());
1335}
1336
1337CTuneColorMapper::CTuneColorMapper()
1338{
1339 Reset();
1340}
1341
1342uint8_t CTuneColorMapper::TuneNumberToColorIndex(uint8_t TuneNumber)
1343{
1344 if(TuneNumber == 0)
1345 return 0;
1346
1347 uint8_t &TuneColorIndex = m_aTuneNumberToColorIndex[TuneNumber - 1];
1348 if(TuneColorIndex == 0)
1349 {
1350 TuneColorIndex = m_NextTuneNumberIndex + 1;
1351 ++m_NextTuneNumberIndex;
1352 }
1353 return TuneColorIndex;
1354}
1355
1356ColorRGBA CTuneColorMapper::TuneColorIndexToColor(uint8_t TuneColorIndex) const
1357{
1358 if(TuneColorIndex == 0)
1359 return ColorRGBA(1.0f, 1.0f, 1.0f);
1360
1361 float Hue = std::fmod(x: (TuneColorIndex - 1) * normalized_golden_angle, y: 1.0f);
1362 return color_cast<ColorRGBA>(hsl: ColorHSLA(Hue, 0.75f, 0.5f, 1.0f));
1363}
1364
1365void CTuneColorMapper::Reset()
1366{
1367 m_aTuneNumberToColorIndex.fill(u: 0);
1368 m_NextTuneNumberIndex = 0;
1369}
1370