feat: image_viewer

This commit is contained in:
donkey 2025-07-30 00:31:45 +08:00
parent 967e53e485
commit 1bc53e28b6
1 changed files with 31 additions and 6 deletions

View File

@ -50,16 +50,41 @@ void OnScroll(swwWindow* o, float offset)
int main(int argc, char *argv[])
{
uint32_t img_w, img_h;
const uint32_t min_w = 256;
const uint32_t min_h = 256;
swwTexture* qrcode_tex = swwTexture_LoadFromFile("qrcode.png");
swwTexture_GetSize(qrcode_tex, &img_w, &img_h);
uint32_t img_w, img_h, w, h;
if (argc < 2) {
printf("Please provide a file path!");
return 0;
}
swwTexture* tex = swwTexture_LoadFromFile(argv[1]);
if (tex == NULL) {
printf("The file path %s is invalid!", argv[1]);
return 1;
}
swwTexture_GetSize(tex, &img_w, &img_h);
if (img_w < min_w) {
w = min_w;
} else {
w = img_w;
}
if (img_h < min_h) {
h = min_h;
} else {
h = img_h;
}
swwWindowCallback callback = {OnKey, OnButton, OnScroll};
swwFpsHelper fps;
swwApp_Initialize();
swwWindow* window = swwWindow_Create(TITLE, img_w, img_h);
swwWindow* window = swwWindow_Create(TITLE, w, h);
UserData ud = {img_w, img_h, NULL};
ud.renderer = swwRenderer_CreateAttachWindow(window, kSoftwareRenderer);
swwWindow_SetUserData(window, &ud);
@ -75,7 +100,7 @@ int main(int argc, char *argv[])
while (!swwApp_ShouldExit()) {
swwRenderer_ClearBlack(ud.renderer);
swwRenderer_DrawTexture(ud.renderer, qrcode_tex, (Point2i){0, 0});
swwRenderer_DrawTexture(ud.renderer, tex, (Point2i){(h - img_h) / 2, (w - img_w) / 2});
if (swwFpsHelper_Update(&fps, ud.renderer)) {
swwFpsHelper_SetTitleWithFps(&fps, window, TITLE);
@ -87,7 +112,7 @@ int main(int argc, char *argv[])
swwRateHelper_Sleep(&rh);
}
swwTexture_Destroy(qrcode_tex);
swwTexture_Destroy(tex);
swwRenderer_Destroy(ud.renderer);
swwWindow_Destroy(window);
swwApp_Cleanup();