SDL2笔记02_加载一批png图片和加载gif

1. 加载一批png图片显示

  1. 首先用ffmpeg.exe准备一批png格式的图片

       .\ffmpeg.exe -ss 59:07.000 -t 7 -r 24 -i G:\Xubuntu_Work_Space\From_Xubuntu\下载\[迅雷下载www.2tu.cc]穿越时空的少女.BD1280高清日语中字.rmvb -y -f image2  C:\Users\majiao\Desktop\heyzo1031\2m%04d.png
    
  2. cmakelists.txt里需要链接SDL2SDL_Image,如下

         cmake_minimum_required(VERSION 3.19)
         project(main)
         set(SDL2_DIR G:/Xubuntu_Work_Space/From_Xubuntu/codeTest_2019_2_21/SDL2/win/SDL2-devel-2.0.14-mingw/SDL2-2.0.14/x86_64-w64-mingw32)
         set(SDL2_IMAGE_DIR G:/Xubuntu_Work_Space/From_Xubuntu/codeTest_2019_2_21/SDL2/win/SDL2_image-devel-2.0.5-mingw/SDL2_image-2.0.5/x86_64-w64-mingw32)
    
         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lmingw32")
         set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
    
         include_directories(
                 ${SDL2_DIR}/include/SDL2
                 ${SDL2_IMAGE_DIR}/include/SDL2
         )
         link_directories(
                 ${SDL2_DIR}/lib
                 ${SDL2_IMAGE_DIR}/lib
         )
    
         set(SOURCE_FILES main.cpp)
         add_executable(main ${SOURCE_FILES})
    
         target_link_libraries(main mingw32 SDL2main SDL2 SDL2_image)
    
    
  3. 写下main.cpp

    • 使用了C++11chrono
     //main.c
    
     #include <Windows.h>
     #include <vector>
     #include <string>
     #include <chrono>
    
     using namespace std;
     using namespace chrono;
     #include "SDL.h"
     #include "SDL_image.h"
    
     vector<SDL_Texture *> MyLoadImage(SDL_Renderer *renderer);
     template< typename... Args >
     std::string string_sprintf( const char* format, Args... args ) {
         int length = std::snprintf( nullptr, 0, format, args... );
         char* buf = new char[length + 1];
         std::snprintf( buf, length + 1, format, args... );
    
         std::string str( buf );
         delete[] buf;
         return str;
     }
    
     int main(int argc, char** argv) {
         int ret = SDL_Init(SDL_INIT_EVERYTHING);
         if (ret != 0) {
             return -1;
         }
    
         SDL_Window *pWin = SDL_CreateWindow("Game", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 491, 0);
         if (pWin == NULL) {
             return -2;
         }
    
         SDL_Renderer *pRenderer = SDL_CreateRenderer(pWin, -1, SDL_RENDERER_ACCELERATED);
         if (pRenderer == NULL) {
             SDL_DestroyWindow(pWin);
             return -3;
         }
    
         int quit = 0;
         SDL_Event evt;
    
         SDL_Texture *sdlTexture = IMG_LoadTexture(pRenderer, "G:\\Xubuntu_Work_Space\\From_Windows\\img\\pixiv\\not_h\\39700353_p0.jpg");
         vector<SDL_Texture*> pngTextures = MyLoadImage(pRenderer);
         int now = -1,
             n = pngTextures.size(),
             len = 1000/23;  // 23 FPS
         while (!quit) {
             if (SDL_PollEvent(&evt)) {
                 if (evt.type == SDL_QUIT) {
                     quit = 1;
                 }
                 /**else {
                     //清除背景
                     SDL_SetRenderDrawColor(pRenderer, 0, 0, 0, 255);
                     SDL_RenderClear(pRenderer);
                     //TODO:渲染图像
                     SDL_RenderCopy(pRenderer, pngTextures[(++now)%n], NULL, NULL);
                     //显示图像
                     SDL_RenderPresent(pRenderer);
                     SDL_Log("渲染%d\n", now%n);
                 }*/
             } else {
                 auto start = system_clock::now();
                 //清除背景
                 SDL_SetRenderDrawColor(pRenderer, 0, 0, 0, 255);
                 SDL_RenderClear(pRenderer);
    
                 //TODO:渲染图像
                 SDL_RenderCopy(pRenderer, pngTextures[(++now)%n], NULL, NULL);
    
                 //显示图像
                 SDL_RenderPresent(pRenderer);
                 auto end   = system_clock::now();
                 auto duration = duration_cast<milliseconds>(end - start);
                 SDL_Log("else 渲染%d   %lldms\n", now%n, duration.count());
                 SDL_Delay(len-duration.count());
             }
         }
    
         if (pRenderer) {
             SDL_DestroyRenderer(pRenderer);
         }
    
         if (pWin) {
             SDL_DestroyWindow(pWin);
         }
    
         SDL_Quit();
    
         return 0;
     }
    
     vector<SDL_Texture *> MyLoadImage(SDL_Renderer* renderer) {
         vector<SDL_Texture *> vec;
         int lef = 0, rig = 168;
         for (int i=1; i<=rig; i++) {
             string filePath = string_sprintf("C:\\Users\\majiao\\Desktop\\heyzo1031\\2m%04d.png", i);
             SDL_Log(filePath.c_str());
             vec.push_back(IMG_LoadTexture(renderer, filePath.c_str()));
         }
         return vec;
     }
    
    
  4. 运行后,600多MB的内存消耗,和2.5%的GPU消耗。。。。太拉跨了,不过毕竟是写着玩的东西,就不计较这些了
    SDL2笔记02_加载一批png图片和加载gif

2. 加载GIF

上一篇:[转]微软商店 打开就显示无法加载该页面 代码0x80131500?


下一篇:音视频技术应用(8)-SDL渲染RGB数据到Qt的控件,并能够通过定时器刷新