|
|
楼主 |
发表于 2025-12-26 12:34:58
|
显示全部楼层
本帖最后由 zzz19760225 于 2025-12-26 15:36 编辑
续上
- // 绘制媒体内容(允许多个共存)
- if (current_playback & PLAYBACK_IMAGE && current_image) {
- int win_w, win_h;
- SDL_GetRendererOutputSize(renderer, &win_w, &win_h);
- float scale = fminf((float)win_w / image_w, (float)win_h / image_h) * 0.9f;
- int draw_w = (int)(image_w * scale);
- int draw_h = (int)(image_h * scale);
- SDL_Rect dst = {(win_w - draw_w)/2, (win_h - draw_h)/2, draw_w, draw_h};
- SDL_SetRenderDrawColor(renderer, 0, 0, 0, 180);
- SDL_RenderFillRect(renderer, NULL);
- SDL_RenderCopy(renderer, current_image, NULL, &dst);
- }
- else if (current_playback & PLAYBACK_GIF && gif_playing && gif_frames) {
- Uint32 now = SDL_GetTicks();
- if (now - gif_last_time >= (Uint32)gif_frames[gif_current_frame].delay_ms) {
- gif_last_time = now;
- gif_current_frame = (gif_current_frame + 1) % gif_frame_count;
- }
- if (gif_frames[gif_current_frame].texture) {
- int win_w, win_h;
- SDL_GetRendererOutputSize(renderer, &win_w, &win_h);
- SDL_SetRenderDrawColor(renderer, 0, 0, 0, 180);
- SDL_RenderFillRect(renderer, NULL);
- // 居中缩放 GIF(可选,也可全屏)
- int img_w, img_h;
- SDL_QueryTexture(gif_frames[gif_current_frame].texture, NULL, NULL, &img_w, &img_h);
- float scale = fminf((float)win_w / img_w, (float)win_h / img_h) * 0.9f;
- int draw_w = (int)(img_w * scale);
- int draw_h = (int)(img_h * scale);
- SDL_Rect dst = {(win_w - draw_w)/2, (win_h - draw_h)/2, draw_w, draw_h};
- SDL_RenderCopy(renderer, gif_frames[gif_current_frame].texture, NULL, &dst);
- }
- }
- SDL_RenderPresent(renderer);
- }
- char* get_exe_name() {
- static char path[256];
- ssize_t len = readlink("/proc/self/exe", path, sizeof(path) - 1);
- if (len != -1) {
- path[len] = '\0';
- return basename(path);
- }
- return "生活游戏";
- }
- // ========================
- // 主函数
- // ========================
- int main(int argc, char *argv[]) {
- setlocale(LC_ALL, "");
- if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) goto fail;
- if (TTF_Init() < 0) goto fail;
- int img_flags = IMG_INIT_PNG | IMG_INIT_JPG;
- if ((IMG_Init(img_flags) & img_flags) != img_flags) goto fail;
- if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) goto fail;
- int desktop_w, desktop_h;
- get_desktop_workarea(&desktop_w, &desktop_h);
- window = SDL_CreateWindow("生活游戏", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
- desktop_w, desktop_h, SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);
- if (!window) goto fail;
- renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
- if (!renderer) goto fail;
- font = TTF_OpenFont("/usr/share/fonts/truetype/wqy/wqy-microhei.ttc", FONT_SIZE);
- if (!font) font = TTF_OpenFont("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", FONT_SIZE);
- if (!font) font = TTF_OpenFont(NULL, FONT_SIZE); // fallback
- if (!font) goto fail;
- SDL_GetWindowSize(window, ¤t_page.window_w, ¤t_page.window_h);
- char exe[256];
- strcpy(exe, get_exe_name());
- char init_file[300];
- snprintf(init_file, sizeof(init_file), "%s.txt", exe);
- if (!file_exists(init_file)) {
- fprintf(stderr, "初始文件不存在: %s\n", init_file);
- goto fail;
- }
- if (load_page(init_file) != 0) goto fail;
- layout_page();
- int running = 1;
- SDL_Event e;
- while (running) {
- while (SDL_PollEvent(&e)) {
- if (e.type == SDL_QUIT) {
- running = 0;
- } else if (e.type == SDL_WINDOWEVENT) {
- if (e.window.event == SDL_WINDOWEVENT_RESIZED ||
- e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
- current_page.window_w = e.window.data1;
- current_page.window_h = e.window.data2;
- layout_page();
- }
- } else if (e.type == SDL_MOUSEWHEEL) {
- scroll_y -= e.wheel.y * 30;
- int max_scroll = current_page.total_height - current_page.window_h + 20;
- if (max_scroll < 0) max_scroll = 0;
- if (scroll_y < 0) scroll_y = 0;
- if (scroll_y > max_scroll) scroll_y = max_scroll;
- } else if (e.type == SDL_MOUSEBUTTONDOWN) {
- if (current_playback != PLAYBACK_NONE) {
- // 点击任意位置关闭当前媒体
- cleanup_playback();
- } else {
- int mx = e.button.x, my = e.button.y;
- for (int i = 0; i < current_page.button_count; i++) {
- SDL_Rect log = current_page.buttons[i].logical_rect;
- SDL_Rect scr = {log.x, log.y - scroll_y, log.w, log.h};
- if (mx >= scr.x && mx <= scr.x + scr.w &&
- my >= scr.y && my <= scr.y + scr.h) {
- const char* btn_text = current_page.buttons[i].text;
- // === 新逻辑:支持 GIF + 音频 联动 ===
- cleanup_playback(); // 清空前一个媒体
- // 尝试加载 GIF
- char gif_path[300];
- snprintf(gif_path, sizeof(gif_path), "%s.gif", btn_text);
- if (file_exists(gif_path)) {
- start_gif_playback(gif_path);
- }
- // 尝试加载音频(mp3/wav/ogg)
- const char* audio_exts[] = {"mp3", "wav", "ogg"};
- char audio_path[300];
- int audio_found = 0;
- for (int k = 0; k < 3; k++) {
- snprintf(audio_path, sizeof(audio_path), "%s.%s", btn_text, audio_exts[k]);
- if (file_exists(audio_path)) {
- start_audio_playback(audio_path);
- audio_found = 1;
- break;
- }
- }
- // 如果没有媒体,则尝试作为文本页面
- if (!file_exists(gif_path) && !audio_found) {
- char txt_path[300];
- snprintf(txt_path, sizeof(txt_path), "%s.txt", btn_text);
- if (file_exists(txt_path)) {
- if (load_page(txt_path) == 0) {
- SDL_GetWindowSize(window, ¤t_page.window_w, ¤t_page.window_h);
- layout_page();
- }
- }
- }
- break;
- }
- }
- }
- } else if (e.type == SDL_KEYDOWN) {
- if (e.key.keysym.sym == SDLK_ESCAPE && current_playback != PLAYBACK_NONE) {
- cleanup_playback();
- }
- }
- }
- render();
- SDL_Delay(16);
- }
- fail:
- cleanup_playback();
- if (font) TTF_CloseFont(font);
- if (renderer) SDL_DestroyRenderer(renderer);
- if (window) SDL_DestroyWindow(window);
- if (current_music) {
- Mix_FreeMusic(current_music);
- }
- Mix_CloseAudio();
- Mix_Quit();
- IMG_Quit();
- TTF_Quit();
- SDL_Quit();
- return 0;
- }
复制代码 不断修改,当处理完gif动画播放,原本的图片显示不见了。需要人工或功能全组合可选。 |
|