Skip to content

Commit

Permalink
Cleanup and logic fixes.
Browse files Browse the repository at this point in the history
Added escape to quit shortcut.
Set background to draw before image is loaded to give illusion of progress.
  • Loading branch information
fuelsoft committed Mar 5, 2020
1 parent 8ef4ef7 commit b940550
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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
41 changes: 23 additions & 18 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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]);

Expand All @@ -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;
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b940550

Please sign in to comment.