diff --git a/sww/source/platform/fb.c b/sww/source/platform/fb.c index e773045..3ece5ab 100644 --- a/sww/source/platform/fb.c +++ b/sww/source/platform/fb.c @@ -318,6 +318,9 @@ void swwApp_Initialize() exit(4); } + g_app.xres = vinfo.xres; + g_app.yres = vinfo.yres; + g_app.fb = (uint8_t*)mmap(0, g_app.xres * g_app.yres * 4, PROT_READ | PROT_WRITE, MAP_SHARED, g_app.fbfd, 0); if (g_app.fb == (void*)-1) { @@ -325,9 +328,6 @@ void swwApp_Initialize() exit(5); } - g_app.xres = vinfo.xres; - g_app.yres = vinfo.yres; - int events[EVENT_SOURCE_MAX_SIZE] = {-1}; int num = ScanDevices(events, EVENT_SOURCE_MAX_SIZE); @@ -431,7 +431,8 @@ void swwWindow_Present(swwWindow* o) /* Start position: [y][x] */ uint8_t* start = g_app.fb + (((o->y * g_app.xres) + o->x) << 2); uint8_t* buf = o->surface->buffer; - size_t line_size = o->o->surface->width << 2; + size_t line_size = o->surface->width << 2; + int height = (int)o->surface->height; // VSYNC int zero = 0; @@ -442,7 +443,7 @@ void swwWindow_Present(swwWindow* o) } /* Copy the window buffer to framebuffer line by line */ - for (; i < o->height; ++i, start += (g_app.xres << 2), buf += line_size) { + for (; i < height; ++i, start += (g_app.xres << 2), buf += line_size) { memcpy(start, buf, line_size); } } diff --git a/sww/test/canvas_wave.c b/sww/test/canvas_wave.c index bdfe40c..97a701f 100644 --- a/sww/test/canvas_wave.c +++ b/sww/test/canvas_wave.c @@ -190,14 +190,13 @@ void CanvasWave(swwWindow* o) int main(int argc, char* argv[]) { - uint32_t w, h; + uint32_t w = 512, h = 512; swwWindowCallback callback = {OnKey, OnButton, OnScroll}; swwFpsHelper fps; swwApp_Initialize(); swwWindow* window = swwWindow_Create(TITLE, w, h); - swwWindow_GetSize(window, &w, &h); UserData ud = {w, h, NULL}; ud.renderer = swwRenderer_CreateAttachWindow(window, kSoftwareRenderer); swwWindow_SetUserData(window, &ud); diff --git a/sww/test/image_viewer.c b/sww/test/image_viewer.c index 95de70f..050cdba 100644 --- a/sww/test/image_viewer.c +++ b/sww/test/image_viewer.c @@ -51,9 +51,7 @@ void OnScroll(swwWindow* o, float offset) int main(int argc, char *argv[]) { uint32_t img_w, img_h; - uint32_t w, h; - // TODO: Read the image file, get the width and height swwTexture* qrcode_tex = swwTexture_LoadFromFile("qrcode.png"); swwTexture_GetSize(qrcode_tex, &img_w, &img_h); @@ -62,16 +60,12 @@ int main(int argc, char *argv[]) swwApp_Initialize(); swwWindow* window = swwWindow_Create(TITLE, img_w, img_h); - swwWindow_GetSize(window, &w, &h); - UserData ud = {w, h, NULL}; + UserData ud = {img_w, img_h, NULL}; ud.renderer = swwRenderer_CreateAttachWindow(window, kSoftwareRenderer); swwWindow_SetUserData(window, &ud); swwWindow_SetCallback(window, &callback); - int ww2 = (w - img_w) / 2; - int hh2 = (h - img_h) / 2; - swwRateHelper rh; swwRenderer_EnablePerfMonitor(ud.renderer, 1); @@ -81,7 +75,7 @@ int main(int argc, char *argv[]) while (!swwApp_ShouldExit()) { swwRenderer_ClearBlack(ud.renderer); - swwRenderer_DrawTexture(ud.renderer, qrcode_tex, (Point2i){ww2, hh2}); + swwRenderer_DrawTexture(ud.renderer, qrcode_tex, (Point2i){0, 0}); if (swwFpsHelper_Update(&fps, ud.renderer)) { swwFpsHelper_SetTitleWithFps(&fps, window, TITLE); diff --git a/sww/test/shapes.c b/sww/test/shapes.c index 5e38be7..1a0669c 100644 --- a/sww/test/shapes.c +++ b/sww/test/shapes.c @@ -130,8 +130,7 @@ void HideSphere(swwRenderer* o, int cx, int cy, float r, int alpha, int beta, in int main() { - int w, h; - int ow = 512, oh = 512; + int w = 512, h = 512; int cw, ch; swwRateHelper rh; swwFpsHelper fps; @@ -139,8 +138,7 @@ int main() swwWindowCallback callback = {OnKey, OnButton, OnScroll}; swwApp_Initialize(); - swwWindow* window = swwWindow_Create(TITLE, ow, oh); - swwWindow_GetSize(window, &w, &h); + swwWindow* window = swwWindow_Create(TITLE, w, h); UserData ud = {w, h, 0, NULL}; ud.renderer = swwRenderer_CreateAttachWindow(window, kSoftwareRenderer); swwWindow_SetUserData(window, &ud); @@ -150,10 +148,9 @@ int main() cw = w / 2; ch = h / 2; - int x = 320, y = 240; // 初始位置 - int dx = 2, dy = 2; // 初始速度 + int x = 320, y = 240; + int dx = 2, dy = 2; - // 小球半径 int radius = 100; int alpha = 45; int beta = 30; @@ -195,22 +192,21 @@ int main() swwRenderer_DrawLine(ud.renderer, (Point2i){20, 120}, (Point2i){200, 120}, kOrange); } break; case 7: { - // 绘制小球 HideSphere(ud.renderer, x, y, radius, alpha, beta, 1, ball_c); - // 移动小球 + x += dx; y += dy; + alpha = x; beta = y; - // 碰撞检测 if (x + radius >= ud.w || x - radius <= 0) { - dx = -dx; // 水平方向反弹 + dx = -dx; ball_c = Color_RandomRGB; } if (y + radius >= ud.h || y - radius <= 0) { - dy = -dy; // 垂直方向反弹 + dy = -dy; ball_c = Color_RandomRGB; } } break; @@ -218,8 +214,8 @@ int main() const int n = 12; for (float f = 0; f < 2 * M_PI; f += 2 * M_PI / n) { for (float g = 0; g < 2 * M_PI; g += 2 * M_PI / n) { - Point2i begin = {(sinf(f) + 1) * ow * 0.5, (cosf(f) + 1) * oh * 0.5}; - Point2i end = {(sinf(g) + 1) * ow * 0.5, (cosf(g) + 1) * oh * 0.5}; + Point2i begin = {(sinf(f) + 1) * w * 0.5, (cosf(f) + 1) * h * 0.5}; + Point2i end = {(sinf(g) + 1) * w * 0.5, (cosf(g) + 1) * h * 0.5}; swwRenderer_DrawLine(ud.renderer, begin, end, kOrange); } } diff --git a/sww/test/starfield.c b/sww/test/starfield.c index 9fad8e1..32fb98d 100644 --- a/sww/test/starfield.c +++ b/sww/test/starfield.c @@ -75,8 +75,7 @@ typedef struct int main() { - const uint32_t win_w = 512, win_h = 512; - uint32_t w, h; + const uint32_t w = 512, h = 512; uint32_t img_w, img_h; int cw, ch; swwWindowCallback callback = {OnKey, OnButton, OnScroll}; @@ -84,17 +83,13 @@ int main() swwRateHelper rh; swwApp_Initialize(); - swwWindow* window = swwWindow_Create(TITLE, win_w, win_h); - swwWindow_GetSize(window, &w, &h); + swwWindow* window = swwWindow_Create(TITLE, w, h); UserData ud = {w, h, 0, NULL}; ud.renderer = swwRenderer_CreateAttachWindow(window, kSoftwareRenderer); swwWindow_SetUserData(window, &ud); swwWindow_SetCallback(window, &callback); - int ww2 = (w - win_w) / 2; - int hh2 = (h - win_h) / 2; - const int center_x = w / 2; const int center_y = h / 2; diff --git a/sww/test/texture.c b/sww/test/texture.c index 23f32d1..af5d7d3 100644 --- a/sww/test/texture.c +++ b/sww/test/texture.c @@ -84,24 +84,22 @@ int CompareDepth(const void* a, const void* b) int main() { - const uint32_t win_w = 512, win_h = 512; - uint32_t w, h; + uint32_t w = 512, h = 512; uint32_t img_w, img_h; int cw, ch; swwWindowCallback callback = {OnKey, OnButton, OnScroll}; swwFpsHelper fps; swwApp_Initialize(); - swwWindow* window = swwWindow_Create(TITLE, win_w, win_h); - swwWindow_GetSize(window, &w, &h); + swwWindow* window = swwWindow_Create(TITLE, w, h); UserData ud = {w, h, 0, NULL}; ud.renderer = swwRenderer_CreateAttachWindow(window, kSoftwareRenderer); swwWindow_SetUserData(window, &ud); swwWindow_SetCallback(window, &callback); - int ww2 = (w - win_w) / 2; - int hh2 = (h - win_h) / 2; + int ww2 = w / 2; + int hh2 = h / 2; const int center_x = w / 2; const int center_y = h / 2; @@ -161,7 +159,6 @@ int main() const float cos_theta = cos(angle); const float sin_theta = sin(angle); - // 计算列变换(带缩放因子) int valid_columns = 0; uint32_t x = 0; for (; x < img_w; x++) { @@ -186,10 +183,9 @@ int main() qsort(columns, valid_columns, sizeof(ColumnInfo), CompareDepth); - // 改进的渲染逻辑 for (int i = 0; i < valid_columns; i++) { const ColumnInfo* col = &columns[i]; - if (col->screen_x < 0 || col->screen_x >= win_w) + if (col->screen_x < 0 || col->screen_x >= w) continue; // 计算垂直缩放范围 @@ -199,7 +195,7 @@ int main() int sy = y_start; for (; sy < y_end; sy++) { - if (sy < 0 || sy >= win_h) + if (sy < 0 || sy >= h) continue; // 逆向计算原始Y坐标(带抗锯齿)