Advertisement
Combreal

loadPngFromPswdProtectedZip.cpp

May 5th, 2019
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.64 KB | None | 0 0
  1. #include <SDL.h>
  2. #include <SDL_image.h>
  3. #include <iostream>
  4. #include <algorithm>
  5. #if defined(_WIN32) && defined(_DEBUG)
  6. #define ZLIB_WINAPI
  7. #endif
  8. #include <zlib.h>
  9. #include <contrib/minizip/unzip.h>
  10. #include <contrib/minizip/zip.h>
  11. #include <string>
  12. using namespace std;
  13.  
  14. Uint8*  bufferB = NULL;
  15.  
  16. SDL_RWops* readfromarchive(string archive, string filename, Uint8*  buffer)
  17. {
  18.     unzFile data;
  19.     unz_file_info   info;
  20.     SDL_RWops* rw = NULL;
  21.     data = unzOpen(archive.c_str());
  22.     unzLocateFile(data, filename.c_str(), 1);
  23.     unzGetCurrentFileInfo(data, &info, NULL, 0, NULL, 0, NULL, 0);
  24.     //unzOpenCurrentFile(data);
  25.     unzOpenCurrentFilePassword(data, "test");
  26.     buffer = (Uint8*)malloc(info.uncompressed_size);
  27.     unzReadCurrentFile(data, buffer, info.uncompressed_size);
  28.     rw = SDL_RWFromMem(buffer, info.uncompressed_size);
  29.     unzClose(data);
  30.     return rw;
  31. }
  32.  
  33. int main(int arc, char ** argv)
  34. {
  35.     SDL_Window* pWindow = NULL;
  36.     SDL_Event event;
  37.     SDL_Renderer *pRenderer = NULL;
  38.     SDL_Texture* pTexture = NULL;
  39.     SDL_Surface* pSprite = NULL;
  40.     SDL_Rect dest;
  41.     SDL_RWops* rw = NULL;
  42.     gzFile file = NULL;
  43.     bool cont = true;
  44.     int CurrentTime = 0, seconds = 0, PassedTime = 0;
  45.     if (SDL_Init(SDL_INIT_EVERYTHING) != 0)//SDL_INIT_VIDEO
  46.     {
  47.         cout << "SDL could not initialize! SDL_Error: " << SDL_GetError() << endl;
  48.     }
  49.     else
  50.     {
  51.         pWindow = SDL_CreateWindow("SDL2 Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN);
  52.     }
  53.     pRenderer = SDL_CreateRenderer(pWindow, -1, SDL_RENDERER_ACCELERATED | IMG_INIT_PNG);
  54.     if (pRenderer)
  55.     {  
  56.         pSprite = IMG_LoadPNG_RW(readfromarchive("resource/deck/deckpngpass.zip", "bg.png", bufferB)); //not working :(
  57.         //pSprite = IMG_LoadTyped_RW(SDL_RWFromFile(filename.c_str(), "r"), 1, "png"); //works with notes at the end
  58.         //pSprite = IMG_Load("resource/deck/bg.png"); //works
  59.         /*rw = SDL_RWFromFile("resource/deck/grid.bmp", "rb");
  60.         pSprite = SDL_LoadBMP_RW(rw, 1);//works*/
  61.         //pSprite = SDL_LoadBMP_RW(readfromarchive("resource/deck/deck.zip", "grid.bmp", bufferB), 1);//working :D
  62.         if (pSprite)
  63.         {
  64.             pTexture = SDL_CreateTextureFromSurface(pRenderer, pSprite);
  65.             //pTexture = IMG_LoadTexture_RW(pRenderer, readfromarchive("resource/deck/deck.zip", "grid.bmp", bufferB), 1); //working
  66.             if (pTexture)
  67.             {
  68.                 dest = {0, 0, 800, 600};
  69.                 SDL_RenderCopy(pRenderer, pTexture, NULL, &dest);
  70.                 SDL_RenderPresent(pRenderer);
  71.             }
  72.             else
  73.             {
  74.                 cout << "Couldn't display image : " << SDL_GetError() << endl;
  75.             }
  76.         }
  77.         else
  78.         {
  79.             cout << "Sprite loading error : " << SDL_GetError() <<endl;
  80.         }
  81.     }
  82.     else
  83.     {
  84.         cout << "Couldn't create renderer : " << SDL_GetError() << endl;
  85.     }
  86.     while (cont != 0)
  87.     {
  88.         while (SDL_PollEvent(&event))
  89.         {
  90.             //SDL_FlushEvent(SDL_QUIT);
  91.             switch (event.type)
  92.             {
  93.             case SDL_KEYDOWN:
  94.                 if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE)
  95.                 {
  96.                     cout << "Quit" << endl;
  97.                     cont = 0;
  98.                 }
  99.                 break;
  100.             //case SDL_WINDOWEVENT:
  101.             case SDL_QUIT:
  102.                 cont = false;
  103.                 break;
  104.             default:
  105.                 break;
  106.             }
  107.         }
  108.         SDL_RenderClear(pRenderer);
  109.         SDL_RenderCopy(pRenderer, pTexture, NULL, &dest);
  110.         SDL_RenderPresent(pRenderer);
  111.         CurrentTime = SDL_GetTicks();
  112.         if (CurrentTime - PassedTime > 1000)
  113.         {
  114.             seconds++;
  115.             cout << seconds << endl;
  116.             PassedTime = CurrentTime;
  117.         }
  118.     }
  119.     SDL_DestroyTexture(pTexture);
  120.     SDL_DestroyRenderer(pRenderer);
  121.     SDL_FreeSurface(pSprite);
  122.     free(bufferB);
  123.     SDL_FreeRW(rw);
  124.     return 0;
  125. }
  126.  
  127. //NOTE for png : 1rst image drawn should NOT be transparant or it display black screen
  128. //               have to first display plain picture bg then proceed (example : first bg picture then grid picture)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement