Check convex quads

This commit is contained in:
Miloslav Ciz 2025-02-01 19:57:33 +01:00
parent 19d3c1cbe5
commit 4cb558b85c
4 changed files with 31 additions and 5 deletions

View file

@ -60,7 +60,7 @@ static const char *LCR_internalDataFile =
":^s1A0 :fk110" // ramps before wall ":^s1A0 :fk110" // ramps before wall
":=s0B0 :fd910" // concrete wall ":=s0B0 :fd910" // concrete wall
":nu0j1 :ly0j1 :AA0j1 " ":nu0j1 :uy0j1 :AA0j1 "
":vw0m0" ":vw0m0"
":vx0m0J" ":vx0m0J"

View file

@ -3,5 +3,5 @@
clear clear
clear clear
gcc -std=c99 -g -Wall -Wextra -pedantic -O1 -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers `sdl2-config --libs --static-libs` -o game frontend_sdl.c && ./game gcc -std=c99 -g -Wall -Wextra -pedantic -O1 -Wall -Wextra -Wno-unused-parameter -Wno-missing-field-initializers `sdl2-config --libs --static-libs` -o game frontend_sdl.c && ./game $@

4
map.h
View file

@ -105,7 +105,7 @@
#define LCR_BLOCK_HILL '(' ///< curved "hill" #define LCR_BLOCK_HILL '(' ///< curved "hill"
#define LCR_BLOCK_BUMP '~' ///< small bump on the road #define LCR_BLOCK_BUMP '~' ///< small bump on the road
#define LCR_BLOCK_CORNER_CONVEX 'n' ///< curved corner (convex) #define LCR_BLOCK_CORNER_CONVEX 'n' ///< curved corner (convex)
#define LCR_BLOCK_CORNER_CONCAVE 'l' ///< curved corner (concave) #define LCR_BLOCK_CORNER_CONCAVE 'u' ///< curved corner (concave)
#define LCR_BLOCK_FULL_ACCEL '>' ///< full block with accelerator #define LCR_BLOCK_FULL_ACCEL '>' ///< full block with accelerator
#define LCR_BLOCK_BOTTOM_ACCEL 'z' ///< bottom half block with accelerator #define LCR_BLOCK_BOTTOM_ACCEL 'z' ///< bottom half block with accelerator
@ -450,7 +450,7 @@ uint8_t LCR_mapLoadFromStr(char (*getNextCharFunc)(void), const char *name)
c = getNextCharFunc(); c = getNextCharFunc();
if (c > '0' && c <= '3') if (c >= '0' && c <= '3')
{ {
mat = c - '0'; mat = c - '0';
c = getNextCharFunc(); c = getNextCharFunc();

View file

@ -252,6 +252,31 @@ void LCR_rendererDrawText(const char *text, int x, int y, uint16_t color,
} }
} }
/**
Guesses (not 100% accurately) if a quad is convex or not.
*/
int _LCR_rendererQuadLooksConvex(S3L_Unit quad[8])
{
S3L_Unit cx = (quad[0] + quad[2] + quad[4] + quad[6]) / 4;
S3L_Unit cy = (quad[1] + quad[3] + quad[5] + quad[7]) / 4;
S3L_Unit r = 0;
for (int i = 0; i < 8; i += 2)
{
r += S3L_abs(cx - quad[i]);
r += S3L_abs(cy - quad[i + 1]);
}
r = (3 * r) / 32;
for (int i = 0; i < 8; i += 2)
if (S3L_abs(cx - quad[i]) <= r && S3L_abs(cy - quad[i + 1]) <= r)
return 0;
return 1;
}
uint16_t _LCR_pixelColor = 0; /**< Holds pixel color for _LCR_pixelFunc3D. This uint16_t _LCR_pixelColor = 0; /**< Holds pixel color for _LCR_pixelFunc3D. This
is needed is texture subsampling is on. */ is needed is texture subsampling is on. */
@ -631,7 +656,8 @@ uint8_t _LCR_rendererCheckMapTriCover(const S3L_Index *t1,
points2D[13] = (4 * points2D[7] + 3 * points2D[9] + points2D[11]) / 8; points2D[13] = (4 * points2D[7] + 3 * points2D[9] + points2D[11]) / 8;
// first: does the triangle alone cover the other one? // first: does the triangle alone cover the other one?
if (_LCR_rendererQuadCoversTri(points2D + 6,points2D)) if (_LCR_rendererQuadLooksConvex(points2D +6) &&
_LCR_rendererQuadCoversTri(points2D + 6,points2D))
result |= 1 << j; result |= 1 << j;
else else
{ {