fix: fixed framebuffer

This commit is contained in:
donkey 2025-07-19 18:48:22 +08:00
parent dabfa89eb7
commit 36e341d0b4
6 changed files with 27 additions and 46 deletions

View File

@ -318,6 +318,9 @@ void swwApp_Initialize()
exit(4); 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.fb = (uint8_t*)mmap(0, g_app.xres * g_app.yres * 4, PROT_READ | PROT_WRITE, MAP_SHARED,
g_app.fbfd, 0); g_app.fbfd, 0);
if (g_app.fb == (void*)-1) { if (g_app.fb == (void*)-1) {
@ -325,9 +328,6 @@ void swwApp_Initialize()
exit(5); exit(5);
} }
g_app.xres = vinfo.xres;
g_app.yres = vinfo.yres;
int events[EVENT_SOURCE_MAX_SIZE] = {-1}; int events[EVENT_SOURCE_MAX_SIZE] = {-1};
int num = ScanDevices(events, EVENT_SOURCE_MAX_SIZE); int num = ScanDevices(events, EVENT_SOURCE_MAX_SIZE);
@ -431,7 +431,8 @@ void swwWindow_Present(swwWindow* o)
/* Start position: <fb4pixels>[y][x] */ /* Start position: <fb4pixels>[y][x] */
uint8_t* start = g_app.fb + (((o->y * g_app.xres) + o->x) << 2); uint8_t* start = g_app.fb + (((o->y * g_app.xres) + o->x) << 2);
uint8_t* buf = o->surface->buffer; 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 // VSYNC
int zero = 0; int zero = 0;
@ -442,7 +443,7 @@ void swwWindow_Present(swwWindow* o)
} }
/* Copy the window buffer to framebuffer line by line */ /* 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); memcpy(start, buf, line_size);
} }
} }

View File

@ -190,14 +190,13 @@ void CanvasWave(swwWindow* o)
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
uint32_t w, h; uint32_t w = 512, h = 512;
swwWindowCallback callback = {OnKey, OnButton, OnScroll}; swwWindowCallback callback = {OnKey, OnButton, OnScroll};
swwFpsHelper fps; swwFpsHelper fps;
swwApp_Initialize(); swwApp_Initialize();
swwWindow* window = swwWindow_Create(TITLE, w, h); swwWindow* window = swwWindow_Create(TITLE, w, h);
swwWindow_GetSize(window, &w, &h);
UserData ud = {w, h, NULL}; UserData ud = {w, h, NULL};
ud.renderer = swwRenderer_CreateAttachWindow(window, kSoftwareRenderer); ud.renderer = swwRenderer_CreateAttachWindow(window, kSoftwareRenderer);
swwWindow_SetUserData(window, &ud); swwWindow_SetUserData(window, &ud);

View File

@ -51,9 +51,7 @@ void OnScroll(swwWindow* o, float offset)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
uint32_t img_w, img_h; 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* qrcode_tex = swwTexture_LoadFromFile("qrcode.png");
swwTexture_GetSize(qrcode_tex, &img_w, &img_h); swwTexture_GetSize(qrcode_tex, &img_w, &img_h);
@ -62,16 +60,12 @@ int main(int argc, char *argv[])
swwApp_Initialize(); swwApp_Initialize();
swwWindow* window = swwWindow_Create(TITLE, img_w, img_h); swwWindow* window = swwWindow_Create(TITLE, img_w, img_h);
swwWindow_GetSize(window, &w, &h); UserData ud = {img_w, img_h, NULL};
UserData ud = {w, h, NULL};
ud.renderer = swwRenderer_CreateAttachWindow(window, kSoftwareRenderer); ud.renderer = swwRenderer_CreateAttachWindow(window, kSoftwareRenderer);
swwWindow_SetUserData(window, &ud); swwWindow_SetUserData(window, &ud);
swwWindow_SetCallback(window, &callback); swwWindow_SetCallback(window, &callback);
int ww2 = (w - img_w) / 2;
int hh2 = (h - img_h) / 2;
swwRateHelper rh; swwRateHelper rh;
swwRenderer_EnablePerfMonitor(ud.renderer, 1); swwRenderer_EnablePerfMonitor(ud.renderer, 1);
@ -81,7 +75,7 @@ int main(int argc, char *argv[])
while (!swwApp_ShouldExit()) { while (!swwApp_ShouldExit()) {
swwRenderer_ClearBlack(ud.renderer); 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)) { if (swwFpsHelper_Update(&fps, ud.renderer)) {
swwFpsHelper_SetTitleWithFps(&fps, window, TITLE); swwFpsHelper_SetTitleWithFps(&fps, window, TITLE);

View File

@ -130,8 +130,7 @@ void HideSphere(swwRenderer* o, int cx, int cy, float r, int alpha, int beta, in
int main() int main()
{ {
int w, h; int w = 512, h = 512;
int ow = 512, oh = 512;
int cw, ch; int cw, ch;
swwRateHelper rh; swwRateHelper rh;
swwFpsHelper fps; swwFpsHelper fps;
@ -139,8 +138,7 @@ int main()
swwWindowCallback callback = {OnKey, OnButton, OnScroll}; swwWindowCallback callback = {OnKey, OnButton, OnScroll};
swwApp_Initialize(); swwApp_Initialize();
swwWindow* window = swwWindow_Create(TITLE, ow, oh); swwWindow* window = swwWindow_Create(TITLE, w, h);
swwWindow_GetSize(window, &w, &h);
UserData ud = {w, h, 0, NULL}; UserData ud = {w, h, 0, NULL};
ud.renderer = swwRenderer_CreateAttachWindow(window, kSoftwareRenderer); ud.renderer = swwRenderer_CreateAttachWindow(window, kSoftwareRenderer);
swwWindow_SetUserData(window, &ud); swwWindow_SetUserData(window, &ud);
@ -150,10 +148,9 @@ int main()
cw = w / 2; cw = w / 2;
ch = h / 2; ch = h / 2;
int x = 320, y = 240; // 初始位置 int x = 320, y = 240;
int dx = 2, dy = 2; // 初始速度 int dx = 2, dy = 2;
// 小球半径
int radius = 100; int radius = 100;
int alpha = 45; int alpha = 45;
int beta = 30; int beta = 30;
@ -195,22 +192,21 @@ int main()
swwRenderer_DrawLine(ud.renderer, (Point2i){20, 120}, (Point2i){200, 120}, kOrange); swwRenderer_DrawLine(ud.renderer, (Point2i){20, 120}, (Point2i){200, 120}, kOrange);
} break; } break;
case 7: { case 7: {
// 绘制小球
HideSphere(ud.renderer, x, y, radius, alpha, beta, 1, ball_c); HideSphere(ud.renderer, x, y, radius, alpha, beta, 1, ball_c);
// 移动小球
x += dx; x += dx;
y += dy; y += dy;
alpha = x; alpha = x;
beta = y; beta = y;
// 碰撞检测
if (x + radius >= ud.w || x - radius <= 0) { if (x + radius >= ud.w || x - radius <= 0) {
dx = -dx; // 水平方向反弹 dx = -dx;
ball_c = Color_RandomRGB; ball_c = Color_RandomRGB;
} }
if (y + radius >= ud.h || y - radius <= 0) { if (y + radius >= ud.h || y - radius <= 0) {
dy = -dy; // 垂直方向反弹 dy = -dy;
ball_c = Color_RandomRGB; ball_c = Color_RandomRGB;
} }
} break; } break;
@ -218,8 +214,8 @@ int main()
const int n = 12; const int n = 12;
for (float f = 0; f < 2 * M_PI; f += 2 * M_PI / n) { 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) { 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 begin = {(sinf(f) + 1) * w * 0.5, (cosf(f) + 1) * h * 0.5};
Point2i end = {(sinf(g) + 1) * ow * 0.5, (cosf(g) + 1) * oh * 0.5}; Point2i end = {(sinf(g) + 1) * w * 0.5, (cosf(g) + 1) * h * 0.5};
swwRenderer_DrawLine(ud.renderer, begin, end, kOrange); swwRenderer_DrawLine(ud.renderer, begin, end, kOrange);
} }
} }

View File

@ -75,8 +75,7 @@ typedef struct
int main() int main()
{ {
const uint32_t win_w = 512, win_h = 512; const uint32_t w = 512, h = 512;
uint32_t w, h;
uint32_t img_w, img_h; uint32_t img_w, img_h;
int cw, ch; int cw, ch;
swwWindowCallback callback = {OnKey, OnButton, OnScroll}; swwWindowCallback callback = {OnKey, OnButton, OnScroll};
@ -84,17 +83,13 @@ int main()
swwRateHelper rh; swwRateHelper rh;
swwApp_Initialize(); swwApp_Initialize();
swwWindow* window = swwWindow_Create(TITLE, win_w, win_h); swwWindow* window = swwWindow_Create(TITLE, w, h);
swwWindow_GetSize(window, &w, &h);
UserData ud = {w, h, 0, NULL}; UserData ud = {w, h, 0, NULL};
ud.renderer = swwRenderer_CreateAttachWindow(window, kSoftwareRenderer); ud.renderer = swwRenderer_CreateAttachWindow(window, kSoftwareRenderer);
swwWindow_SetUserData(window, &ud); swwWindow_SetUserData(window, &ud);
swwWindow_SetCallback(window, &callback); 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_x = w / 2;
const int center_y = h / 2; const int center_y = h / 2;

View File

@ -84,24 +84,22 @@ int CompareDepth(const void* a, const void* b)
int main() int main()
{ {
const uint32_t win_w = 512, win_h = 512; uint32_t w = 512, h = 512;
uint32_t w, h;
uint32_t img_w, img_h; uint32_t img_w, img_h;
int cw, ch; int cw, ch;
swwWindowCallback callback = {OnKey, OnButton, OnScroll}; swwWindowCallback callback = {OnKey, OnButton, OnScroll};
swwFpsHelper fps; swwFpsHelper fps;
swwApp_Initialize(); swwApp_Initialize();
swwWindow* window = swwWindow_Create(TITLE, win_w, win_h); swwWindow* window = swwWindow_Create(TITLE, w, h);
swwWindow_GetSize(window, &w, &h);
UserData ud = {w, h, 0, NULL}; UserData ud = {w, h, 0, NULL};
ud.renderer = swwRenderer_CreateAttachWindow(window, kSoftwareRenderer); ud.renderer = swwRenderer_CreateAttachWindow(window, kSoftwareRenderer);
swwWindow_SetUserData(window, &ud); swwWindow_SetUserData(window, &ud);
swwWindow_SetCallback(window, &callback); swwWindow_SetCallback(window, &callback);
int ww2 = (w - win_w) / 2; int ww2 = w / 2;
int hh2 = (h - win_h) / 2; int hh2 = h / 2;
const int center_x = w / 2; const int center_x = w / 2;
const int center_y = h / 2; const int center_y = h / 2;
@ -161,7 +159,6 @@ int main()
const float cos_theta = cos(angle); const float cos_theta = cos(angle);
const float sin_theta = sin(angle); const float sin_theta = sin(angle);
// 计算列变换(带缩放因子)
int valid_columns = 0; int valid_columns = 0;
uint32_t x = 0; uint32_t x = 0;
for (; x < img_w; x++) { for (; x < img_w; x++) {
@ -186,10 +183,9 @@ int main()
qsort(columns, valid_columns, sizeof(ColumnInfo), CompareDepth); qsort(columns, valid_columns, sizeof(ColumnInfo), CompareDepth);
// 改进的渲染逻辑
for (int i = 0; i < valid_columns; i++) { for (int i = 0; i < valid_columns; i++) {
const ColumnInfo* col = &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; continue;
// 计算垂直缩放范围 // 计算垂直缩放范围
@ -199,7 +195,7 @@ int main()
int sy = y_start; int sy = y_start;
for (; sy < y_end; sy++) { for (; sy < y_end; sy++) {
if (sy < 0 || sy >= win_h) if (sy < 0 || sy >= h)
continue; continue;
// 逆向计算原始Y坐标带抗锯齿 // 逆向计算原始Y坐标带抗锯齿