From b940550b53bfc5be176bf90def9214d3cd976e90 Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 5 Mar 2020 01:05:39 -0800 Subject: [PATCH] Cleanup and logic fixes. Added escape to quit shortcut. Set background to draw before image is loaded to give illusion of progress. --- README.md | 3 ++- main.cpp | 41 +++++++++++++++++++++++------------------ 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 84b2f40..eaaf290 100644 --- a/README.md +++ b/README.md @@ -54,6 +54,7 @@ Then call the program by either dragging an image onto Viewer.exe or by running * Zoom is handled by hitting keypad +/- or scrolling. * Panning is handled by left-clicking and dragging the image. * Zoom and position can be reset by hitting keypad 0 (keypad zero). +* Quit by closing the window or hitting Escape. ### Supported formats: As listed [here](https://www.libsdl.org/projects/SDL_image/docs/SDL_image.pdf#page=8&zoom=auto,-205,547). @@ -68,10 +69,10 @@ This list is maintained in main.cpp as I think of things - check there for an up * Add key to set zoom to 1:1 pixel ratio * Add key to change sampling mode * Filename in title -* Transparent background texture * Possible: partial image metadata? PNG image data support from ProjectPNG? * Navigate forward/backwards in current directory * Animated GIFs * Zoom on mouse instead of window center +* Stop image from being moved off-screen [1]: https://www.libsdl.org/projects/SDL_image/docs/SDL_image.pdf#page=21&zoom=auto,-205,720 \ No newline at end of file diff --git a/main.cpp b/main.cpp index b58f60f..fa4db25 100644 --- a/main.cpp +++ b/main.cpp @@ -126,15 +126,15 @@ void drawTileTexture(Window* win, SDL_Texture* tileTexture) { } /** -* draw - Clear display, then draw tiles and image +* draw - Clear display, then draw tiles and image (if provided) * win > Target Window object * tileTexture > Texture as SDL_Texture to tile * imageTexture > Image as SDL_Texture */ void draw(Window* win, SDL_Texture* tile_texture, SDL_Texture* image_texture) { SDL_RenderClear(win->renderer); - drawTileTexture(win, tile_texture); - redrawImage(win, image_texture); + if (tile_texture) drawTileTexture(win, tile_texture); + if (image_texture) redrawImage(win, image_texture); SDL_RenderPresent(win->renderer); } @@ -186,6 +186,22 @@ int main(int argc, char * argv[]) { std::cout << LOG_NOTICE << "Sampling defaulting to nearest neighbour" << std::endl; } + //create checkerboard background texture for transparent images + SDL_Surface* transparency_tmp = SDL_CreateRGBSurface(0, TEXTURE_CHECKERBOARD_RESOLUTION, TEXTURE_CHECKERBOARD_RESOLUTION, 32, 0, 0, 0, 0); + uint32_t* pixeldata = (uint32_t*) transparency_tmp->pixels; + for (int h = 0; h < TEXTURE_CHECKERBOARD_RESOLUTION; h++) { + for (int w = 0; w < TEXTURE_CHECKERBOARD_RESOLUTION; w++) { + int p = TEXTURE_CHECKERBOARD_RESOLUTION / 2; + (*(pixeldata + h * TEXTURE_CHECKERBOARD_RESOLUTION + w)) = (((h < p) ^ (w < p)) ? COLOUR_CHECKERBOARD_LIGHT : COLOUR_CHECKERBOARD_DARK); + } + } + + TEXTURE_TRANSPARENCY = SDL_CreateTextureFromSurface(win.renderer, transparency_tmp); + SDL_FreeSurface(transparency_tmp); + + draw(&win, TEXTURE_TRANSPARENCY, nullptr); + + /* Moved after background draw to give illusion of progress while image is loaded */ //try loading image from filename imageSurface = IMG_Load(argv[1]); @@ -198,23 +214,10 @@ int main(int argc, char * argv[]) { IMAGE_HEIGHT = imageSurface->h; IMAGE_WIDTH = imageSurface->w; - //create hardware accelerated texture from image data and free surface + //create hardware accelerated texture from image data and free surface imageTexture = SDL_CreateTextureFromSurface(win.renderer, imageSurface); SDL_FreeSurface(imageSurface); - //create checkerboard background texture for transparent images - SDL_Surface* transparency_tmp = SDL_CreateRGBSurface(0, TEXTURE_CHECKERBOARD_RESOLUTION, TEXTURE_CHECKERBOARD_RESOLUTION, 32, 0, 0, 0, 0); - uint32_t* pixeldata = (uint32_t*) transparency_tmp->pixels; - for (int h = 0; h < TEXTURE_CHECKERBOARD_RESOLUTION; h++) { - for (int w = 0; w < TEXTURE_CHECKERBOARD_RESOLUTION; w++) { - int p = TEXTURE_CHECKERBOARD_RESOLUTION / 2; - (*(pixeldata + h * TEXTURE_CHECKERBOARD_RESOLUTION + w)) = (((h < p) ^ (w < p)) ? COLOUR_CHECKERBOARD_LIGHT : COLOUR_CHECKERBOARD_DARK); - } - } - - TEXTURE_TRANSPARENCY = SDL_CreateTextureFromSurface(win.renderer, transparency_tmp); - SDL_FreeSurface(transparency_tmp); - draw(&win, TEXTURE_TRANSPARENCY, imageTexture); int mouseX; @@ -259,6 +262,9 @@ int main(int argc, char * argv[]) { VIEWPORT_Y = 0; draw(&win, TEXTURE_TRANSPARENCY, imageTexture); break; + case SDLK_ESCAPE: + quit = true; + break; } break; case SDL_MOUSEWHEEL: @@ -330,7 +336,6 @@ int main(int argc, char * argv[]) { * Add key to set zoom to 1:1 pixel ratio * Add key to change sampling mode * Filename in title -* Transparent background texture * Possible: partial image metadata? PNG image data support from ProjectPNG? * Navigate forward/backwards in current directory * Animated GIFs -> https://stackoverflow.com/questions/36267833/c-sdl2-how-do-i-play-a-gif-in-sdl2/36410301#36410301