SDL的屏幕坐标是左上角为原点,需要纠正

This commit is contained in:
2025-06-19 14:19:29 +08:00
parent f812505a1e
commit fc851d2106
+34 -31
View File
@@ -52,8 +52,11 @@ TriangleVector g_allTriangles;
SDL_Window* g_window = NULL; SDL_Window* g_window = NULL;
SDL_Renderer* g_renderer = NULL; SDL_Renderer* g_renderer = NULL;
#define trans_x(x) ((float32_t)(x) * g_scale + g_lbCorner_x) #define trans_w2s_x(x) ((float32_t)(x) * g_scale + g_lbCorner_x)
#define trans_y(y) ((float32_t)(y) * g_scale + g_lbCorner_y) #define trans_w2s_y(y) ((float32_t)g_viewHeight - ((float32_t)(y) * g_scale + g_lbCorner_y))
#define trans_s2w_x(x) ((float32_t)(x - g_lbCorner_x) / g_scale)
#define trans_s2w_y(y) ((float32_t)(g_viewHeight - y - g_lbCorner_y) / g_scale)
void _onFramebufferSize(int32_t w, int32_t h) void _onFramebufferSize(int32_t w, int32_t h)
{ {
@@ -87,7 +90,7 @@ static void _drawGrid(void)
SDL_SetRenderDrawColorFloat(g_renderer, lineColor[level].x, lineColor[level].y, lineColor[level].z, SDL_ALPHA_OPAQUE_FLOAT); SDL_SetRenderDrawColorFloat(g_renderer, lineColor[level].x, lineColor[level].y, lineColor[level].z, SDL_ALPHA_OPAQUE_FLOAT);
SDL_RenderLine(g_renderer, trans_x(0), trans_y(i), trans_x(horLength), trans_y(i)); SDL_RenderLine(g_renderer, trans_w2s_x(0), trans_w2s_y(i), trans_w2s_x(horLength), trans_w2s_y(i));
} }
// Vertical lines // Vertical lines
@@ -100,7 +103,7 @@ static void _drawGrid(void)
if (level > 3 - g_nDrawGridLevel) continue; if (level > 3 - g_nDrawGridLevel) continue;
SDL_SetRenderDrawColorFloat(g_renderer, lineColor[level].x, lineColor[level].y, lineColor[level].z, SDL_ALPHA_OPAQUE_FLOAT); SDL_SetRenderDrawColorFloat(g_renderer, lineColor[level].x, lineColor[level].y, lineColor[level].z, SDL_ALPHA_OPAQUE_FLOAT);
SDL_RenderLine(g_renderer, trans_x(j), trans_y(0), trans_x(j), trans_y(verLength)); SDL_RenderLine(g_renderer, trans_w2s_x(j), trans_w2s_y(0), trans_w2s_x(j), trans_w2s_y(verLength));
} }
//cross //cross
@@ -114,13 +117,13 @@ static void _drawGrid(void)
for (int j = 0; j < horLength; j++) for (int j = 0; j < horLength; j++)
{ {
SDL_RenderLine(g_renderer, SDL_RenderLine(g_renderer,
trans_x(j + 0.5f - crossLength), trans_y(i + 0.5f - crossLength), trans_w2s_x(j + 0.5f - crossLength), trans_w2s_y(i + 0.5f - crossLength),
trans_x(j + 0.5f + crossLength), trans_y(i + 0.5f + crossLength) trans_w2s_x(j + 0.5f + crossLength), trans_w2s_y(i + 0.5f + crossLength)
); );
SDL_RenderLine(g_renderer, SDL_RenderLine(g_renderer,
trans_x(j + 0.5f - crossLength), trans_y(i + 0.5f + crossLength), trans_w2s_x(j + 0.5f - crossLength), trans_w2s_y(i + 0.5f + crossLength),
trans_x(j + 0.5f + crossLength), trans_y(i + 0.5f - crossLength) trans_w2s_x(j + 0.5f + crossLength), trans_w2s_y(i + 0.5f - crossLength)
); );
} }
} }
@@ -135,10 +138,10 @@ static void _drawTrianglePixel(const Triangle& triangle)
for (const auto& dot : triangle.pixels) for (const auto& dot : triangle.pixels)
{ {
SDL_FRect rect; SDL_FRect rect;
rect.x = trans_x(dot.first); rect.x = trans_w2s_x(dot.first);
rect.y = trans_y(dot.second); rect.y = trans_w2s_y(dot.second);
rect.w = g_scale; rect.w = g_scale;
rect.h = g_scale; rect.h = -g_scale;
SDL_RenderFillRect(g_renderer, &rect); SDL_RenderFillRect(g_renderer, &rect);
} }
@@ -149,10 +152,10 @@ void _drawTriangleBorder(const Triangle& triangle, const fVector3& color, float
SDL_SetRenderDrawColorFloat(g_renderer, color[0], color[1], color[2], SDL_ALPHA_OPAQUE_FLOAT); SDL_SetRenderDrawColorFloat(g_renderer, color[0], color[1], color[2], SDL_ALPHA_OPAQUE_FLOAT);
SDL_FPoint points[4] = { SDL_FPoint points[4] = {
{trans_x(triangle.p0.x), trans_y(triangle.p0.y)}, {trans_w2s_x(triangle.p0.x), trans_w2s_y(triangle.p0.y)},
{trans_x(triangle.p1.x), trans_y(triangle.p1.y)}, {trans_w2s_x(triangle.p1.x), trans_w2s_y(triangle.p1.y)},
{trans_x(triangle.p2.x), trans_y(triangle.p2.y)}, {trans_w2s_x(triangle.p2.x), trans_w2s_y(triangle.p2.y)},
{trans_x(triangle.p0.x), trans_y(triangle.p0.y)} {trans_w2s_x(triangle.p0.x), trans_w2s_y(triangle.p0.y)}
}; };
SDL_RenderLines(g_renderer, points, 4); SDL_RenderLines(g_renderer, points, 4);
} }
@@ -179,12 +182,12 @@ void _drawSegmentInAABB(const fVector2& p0, const fVector2& p1, float xMin, floa
float _ymax = (xMax - p0.x) * dy / dx + p0.y; float _ymax = (xMax - p0.x) * dy / dx + p0.y;
if (dx > 0) { if (dx > 0) {
SDL_RenderLine(g_renderer, trans_x(xMin), trans_y(_ymin), trans_x(p0.x), trans_y(p0.y) ); SDL_RenderLine(g_renderer, trans_w2s_x(xMin), trans_w2s_y(_ymin), trans_w2s_x(p0.x), trans_w2s_y(p0.y) );
SDL_RenderLine(g_renderer, trans_x(p1.x), trans_y(p1.y), trans_x(xMax), trans_y(_ymax) ); SDL_RenderLine(g_renderer, trans_w2s_x(p1.x), trans_w2s_y(p1.y), trans_w2s_x(xMax), trans_w2s_y(_ymax) );
} }
else { else {
SDL_RenderLine(g_renderer, trans_x(xMin), trans_y(_ymin), trans_x(p1.x), trans_y(p1.y)); SDL_RenderLine(g_renderer, trans_w2s_x(xMin), trans_w2s_y(_ymin), trans_w2s_x(p1.x), trans_w2s_y(p1.y));
SDL_RenderLine(g_renderer, trans_x(p0.x), trans_y(p0.y), trans_x(xMax), trans_y(_ymax)); SDL_RenderLine(g_renderer, trans_w2s_x(p0.x), trans_w2s_y(p0.y), trans_w2s_x(xMax), trans_w2s_y(_ymax));
} }
} }
else { else {
@@ -192,12 +195,12 @@ void _drawSegmentInAABB(const fVector2& p0, const fVector2& p1, float xMin, floa
float _xmax = (yMax - p0.y) * dx / dy + p0.x; float _xmax = (yMax - p0.y) * dx / dy + p0.x;
if (dy > 0) { if (dy > 0) {
SDL_RenderLine(g_renderer, trans_x(_xmin), trans_y(yMin), trans_x(p0.x), trans_y(p0.y)); SDL_RenderLine(g_renderer, trans_w2s_x(_xmin), trans_w2s_y(yMin), trans_w2s_x(p0.x), trans_w2s_y(p0.y));
SDL_RenderLine(g_renderer, trans_x(p1.x), trans_y(p1.y), trans_x(_xmax), trans_y(yMax)); SDL_RenderLine(g_renderer, trans_w2s_x(p1.x), trans_w2s_y(p1.y), trans_w2s_x(_xmax), trans_w2s_y(yMax));
} }
else { else {
SDL_RenderLine(g_renderer, trans_x(_xmin), trans_y(yMin), trans_x(p1.x), trans_y(p1.y)); SDL_RenderLine(g_renderer, trans_w2s_x(_xmin), trans_w2s_y(yMin), trans_w2s_x(p1.x), trans_w2s_y(p1.y));
SDL_RenderLine(g_renderer, trans_x(p0.x), trans_y(p0.y), trans_x(_xmax), trans_y(yMax)); SDL_RenderLine(g_renderer, trans_w2s_x(p0.x), trans_w2s_y(p0.y), trans_w2s_x(_xmax), trans_w2s_y(yMax));
} }
} }
} }
@@ -218,8 +221,8 @@ void _drawAction(void)
case MT_FIRST_LINE: case MT_FIRST_LINE:
{ {
SDL_RenderLine(g_renderer, SDL_RenderLine(g_renderer,
trans_x(g_currentTriangle.p0.x), trans_y(g_currentTriangle.p0.y), trans_w2s_x(g_currentTriangle.p0.x), trans_w2s_y(g_currentTriangle.p0.y),
trans_x(g_currentTriangle.p1.x), trans_y(g_currentTriangle.p1.y) trans_w2s_x(g_currentTriangle.p1.x), trans_w2s_y(g_currentTriangle.p1.y)
); );
} }
break; break;
@@ -247,13 +250,13 @@ void _drawMouseCursor(void)
const float32_t CursorLength = 10.f / g_scale; const float32_t CursorLength = 10.f / g_scale;
SDL_RenderLine(g_renderer, SDL_RenderLine(g_renderer,
trans_x(g_mousePosX - CursorLength), trans_y(g_mousePosY), trans_w2s_x(g_mousePosX - CursorLength), trans_w2s_y(g_mousePosY),
trans_x(g_mousePosX + CursorLength), trans_y(g_mousePosY) trans_w2s_x(g_mousePosX + CursorLength), trans_w2s_y(g_mousePosY)
); );
SDL_RenderLine(g_renderer, SDL_RenderLine(g_renderer,
trans_x(g_mousePosX), trans_y(g_mousePosY - CursorLength), trans_w2s_x(g_mousePosX), trans_w2s_y(g_mousePosY - CursorLength),
trans_x(g_mousePosX), trans_y(g_mousePosY + CursorLength) trans_w2s_x(g_mousePosX), trans_w2s_y(g_mousePosY + CursorLength)
); );
} }
} }
@@ -287,8 +290,8 @@ inline float _snapPosition(float pos)
void _onMouseMove(float32_t x, float32_t y) void _onMouseMove(float32_t x, float32_t y)
{ {
g_mousePosX = (float)(x - g_lbCorner_x) / g_scale; g_mousePosX = trans_s2w_x(x);
g_mousePosY = (float)(y - g_lbCorner_y) / g_scale; g_mousePosY = trans_s2w_y(y);
if (g_bSnapMode) { if (g_bSnapMode) {
g_mousePosX = _snapPosition(g_mousePosX); g_mousePosX = _snapPosition(g_mousePosX);