Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SDL.h>
- #include <SDL_image.h>
- #include <iostream>
- #include <algorithm>
- #if defined(_WIN32) && defined(_DEBUG)
- #define ZLIB_WINAPI
- #endif
- #include <zlib.h>
- #include <contrib/minizip/unzip.h>
- #include <contrib/minizip/zip.h>
- #include <string>
- using namespace std;
- Uint8* bufferB = NULL;
- SDL_RWops* readfromarchive(string archive, string filename, Uint8* buffer)
- {
- unzFile data;
- unz_file_info info;
- SDL_RWops* rw = NULL;
- data = unzOpen(archive.c_str());
- unzLocateFile(data, filename.c_str(), 1);
- unzGetCurrentFileInfo(data, &info, NULL, 0, NULL, 0, NULL, 0);
- //unzOpenCurrentFile(data);
- unzOpenCurrentFilePassword(data, "test");
- buffer = (Uint8*)malloc(info.uncompressed_size);
- unzReadCurrentFile(data, buffer, info.uncompressed_size);
- rw = SDL_RWFromMem(buffer, info.uncompressed_size);
- unzClose(data);
- return rw;
- }
- int main(int arc, char ** argv)
- {
- SDL_Window* pWindow = NULL;
- SDL_Event event;
- SDL_Renderer *pRenderer = NULL;
- SDL_Texture* pTexture = NULL;
- SDL_Surface* pSprite = NULL;
- SDL_Rect dest;
- SDL_RWops* rw = NULL;
- gzFile file = NULL;
- bool cont = true;
- int CurrentTime = 0, seconds = 0, PassedTime = 0;
- if (SDL_Init(SDL_INIT_EVERYTHING) != 0)//SDL_INIT_VIDEO
- {
- cout << "SDL could not initialize! SDL_Error: " << SDL_GetError() << endl;
- }
- else
- {
- pWindow = SDL_CreateWindow("SDL2 Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN);
- }
- pRenderer = SDL_CreateRenderer(pWindow, -1, SDL_RENDERER_ACCELERATED | IMG_INIT_PNG);
- if (pRenderer)
- {
- pSprite = IMG_LoadPNG_RW(readfromarchive("resource/deck/deckpngpass.zip", "bg.png", bufferB)); //not working :(
- //pSprite = IMG_LoadTyped_RW(SDL_RWFromFile(filename.c_str(), "r"), 1, "png"); //works with notes at the end
- //pSprite = IMG_Load("resource/deck/bg.png"); //works
- /*rw = SDL_RWFromFile("resource/deck/grid.bmp", "rb");
- pSprite = SDL_LoadBMP_RW(rw, 1);//works*/
- //pSprite = SDL_LoadBMP_RW(readfromarchive("resource/deck/deck.zip", "grid.bmp", bufferB), 1);//working :D
- if (pSprite)
- {
- pTexture = SDL_CreateTextureFromSurface(pRenderer, pSprite);
- //pTexture = IMG_LoadTexture_RW(pRenderer, readfromarchive("resource/deck/deck.zip", "grid.bmp", bufferB), 1); //working
- if (pTexture)
- {
- dest = {0, 0, 800, 600};
- SDL_RenderCopy(pRenderer, pTexture, NULL, &dest);
- SDL_RenderPresent(pRenderer);
- }
- else
- {
- cout << "Couldn't display image : " << SDL_GetError() << endl;
- }
- }
- else
- {
- cout << "Sprite loading error : " << SDL_GetError() <<endl;
- }
- }
- else
- {
- cout << "Couldn't create renderer : " << SDL_GetError() << endl;
- }
- while (cont != 0)
- {
- while (SDL_PollEvent(&event))
- {
- //SDL_FlushEvent(SDL_QUIT);
- switch (event.type)
- {
- case SDL_KEYDOWN:
- if (event.key.keysym.scancode == SDL_SCANCODE_ESCAPE)
- {
- cout << "Quit" << endl;
- cont = 0;
- }
- break;
- //case SDL_WINDOWEVENT:
- case SDL_QUIT:
- cont = false;
- break;
- default:
- break;
- }
- }
- SDL_RenderClear(pRenderer);
- SDL_RenderCopy(pRenderer, pTexture, NULL, &dest);
- SDL_RenderPresent(pRenderer);
- CurrentTime = SDL_GetTicks();
- if (CurrentTime - PassedTime > 1000)
- {
- seconds++;
- cout << seconds << endl;
- PassedTime = CurrentTime;
- }
- }
- SDL_DestroyTexture(pTexture);
- SDL_DestroyRenderer(pRenderer);
- SDL_FreeSurface(pSprite);
- free(bufferB);
- SDL_FreeRW(rw);
- return 0;
- }
- //NOTE for png : 1rst image drawn should NOT be transparant or it display black screen
- // 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