Skip to content

Commit

Permalink
Window position and size now saved and restored
Browse files Browse the repository at this point in the history
  • Loading branch information
fuelsoft committed Mar 9, 2020
1 parent acd275c commit 1e2d0b3
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 43 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
*.depend
*.cbp
*.layout
*.psd
16 changes: 11 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,25 @@ Then call the program by either dragging an image onto Viewer.exe or by running
As listed [here](https://www.libsdl.org/projects/SDL_image/docs/SDL_image.pdf#page=8&zoom=auto,-205,547).

#### Notes:
* Some file types can contain multiple resolutions/variations for an image (ICO, CUR, ...). SDL_image [documentation][1] states that "for files with multiple images, the first one found with the highest color count is chosen."
* Animated GIFs will only show the first frame as an SDL_image limitation. Additional external libraries will be required to view animations. TBA.
* Some file types can contain multiple resolutions/variations of an image (ICO, CUR, ...). SDL_image [documentation][1] states that "for files with multiple images, the first one found with the highest color count is chosen."
* Window size and position are stored in `setting.cfg` in the program folder. If this becomes broken somehow (window appears off-screen, etc.) it is safe to delete this file to restore defaults.

### TODO:
This list is maintained in main.cpp as I think of things - check there for an up to date copy.
* Remember window size and position
This list is maintained in `main.cpp` as I think of things - check there for an up to date copy.
* Add key to set zoom to 1:1 pixel ratio
* Add key to change sampling mode
* Filename in title
* 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
* Possible: Image deletion?
* Possible: Image rotation?
* Cursor icon state updates?

### Known Problems:
* ICO: Files stored as "NEW PNG" type do not load with SDL_image
* ICO: Files with partially transparent pixels do not render correctly
* GIF: Animated sequences only load the first frame

[1]: https://www.libsdl.org/projects/SDL_image/docs/SDL_image.pdf#page=21&zoom=auto,-205,720
5 changes: 2 additions & 3 deletions Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ NICK WILSON

/* PUBLIC */

Window::Window(int w, int h, bool visible) {
window = SDL_CreateWindow("Untitled Window", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, w, h, ((visible) ? SDL_WINDOW_SHOWN : SDL_WINDOW_HIDDEN) | SDL_WINDOW_RESIZABLE);
Window::Window(int w, int h, int x, int y, bool visible) {
window = SDL_CreateWindow("Untitled Window", x, y, w, h, ((visible) ? SDL_WINDOW_SHOWN : SDL_WINDOW_HIDDEN) | SDL_WINDOW_RESIZABLE);

if (window != NULL) {
surface = SDL_GetWindowSurface(window);
Expand Down
2 changes: 1 addition & 1 deletion Window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Window {

Window() {}

Window(int w, int h, bool visible = true);
Window(int w, int h, int x = SDL_WINDOWPOS_UNDEFINED, int y = SDL_WINDOWPOS_UNDEFINED, bool visible = true);

~Window();

Expand Down
92 changes: 58 additions & 34 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,47 +63,64 @@ bool MOUSE_CLICK_STATE_LEFT = false;
std::string PROGRAM_CWD;

const std::string FILENAME_SETTINGS = "settings.cfg";
const int settingDataSize = sizeof(WINDOW_X)
+ sizeof(WINDOW_Y)
+ sizeof(WINDOW_WIDTH)
const int settingDataSize = sizeof(WINDOW_X)
+ sizeof(WINDOW_Y)
+ sizeof(WINDOW_WIDTH)
+ sizeof(WINDOW_HEIGHT);

/* /// CODE /// */

void setWindowDefaults() {
WINDOW_WIDTH = WINDOW_WIDTH_DEFAULT;
WINDOW_HEIGHT = WINDOW_HEIGHT_DEFAULT;
}

/**
* readSettings - Load settings file if possible and recover window position and size.
*/
void readSettings() {
std::ifstream inputStream;
inputStream.open(PROGRAM_CWD + '\\' + FILENAME_SETTINGS, std::ifstream::in | std::ifstream::binary | std::ifstream::ate);
//if CWD is length 0, program was run from terminal and is CWD is unknown. Use default settings.
if (!PROGRAM_CWD.length()) {
std::cerr << LOG_NOTICE << "Run Viewer using full path to load settings file. Using defaults." << std::endl;
setWindowDefaults();
return;
}
std::ifstream inputStream(PROGRAM_CWD + '\\' + FILENAME_SETTINGS, std::ifstream::in | std::ifstream::binary | std::ifstream::ate);
if (inputStream.is_open()) {
if (inputStream.tellg() == settingDataSize) {
if (inputStream.tellg() >= settingDataSize) {
inputStream.seekg(std::ios_base::beg);
inputStream.read(reinterpret_cast<char *>(&WINDOW_X), sizeof(WINDOW_X));
inputStream.read(reinterpret_cast<char *>(&WINDOW_Y), sizeof(WINDOW_Y));
inputStream.read(reinterpret_cast<char *>(&WINDOW_WIDTH), sizeof(WINDOW_WIDTH));
inputStream.read(reinterpret_cast<char *>(&WINDOW_HEIGHT), sizeof(WINDOW_HEIGHT));
}
else {
std::cerr << "Incorrect settings data size!" << std::endl;

//window x,y will default to 0,0 which is fine.
WINDOW_WIDTH = WINDOW_WIDTH_DEFAULT;
WINDOW_HEIGHT = WINDOW_HEIGHT_DEFAULT;
std::cerr << LOG_WARNING << "Incorrect settings data size!" << std::endl;
//window x,y will default to 0,0 which is fine
setWindowDefaults();
}
inputStream.close();
}
else { //load defaults
std::cerr << "No settings file located!" << std::endl;

//window x,y will default to 0,0 which is fine.
WINDOW_WIDTH = WINDOW_WIDTH_DEFAULT;
WINDOW_HEIGHT = WINDOW_HEIGHT_DEFAULT;
std::cerr << LOG_NOTICE << "No settings file located!" << std::endl;
setWindowDefaults();
}
}

void writeSettings() {

//working directory wasn't set, don't write anything
if (!PROGRAM_CWD.length()) return;
std::ofstream outputStream(PROGRAM_CWD + '\\' + FILENAME_SETTINGS, std::ifstream::out | std::ifstream::binary | std::ifstream::trunc);
if (outputStream.is_open()) {
outputStream.write(reinterpret_cast<char *>(&WINDOW_X), sizeof(WINDOW_X));
outputStream.write(reinterpret_cast<char *>(&WINDOW_Y), sizeof(WINDOW_Y));
outputStream.write(reinterpret_cast<char *>(&WINDOW_WIDTH), sizeof(WINDOW_WIDTH));
outputStream.write(reinterpret_cast<char *>(&WINDOW_HEIGHT), sizeof(WINDOW_HEIGHT));
outputStream.close();
}
else {
std::cerr << LOG_ERROR << "Could not write to settings file!" << std::endl;
}
}

/**
Expand Down Expand Up @@ -192,15 +209,17 @@ void draw(Window* win, SDL_Texture* tile_texture, SDL_Texture* image_texture) {
*/
int main(int argc, char * argv[]) {
bool quit = false;
int windowDisplayIndex;

//pull current directory from first argument
PROGRAM_CWD = std::string(argv[0]);
PROGRAM_CWD.resize(PROGRAM_CWD.rfind('\\'));
//strip executable name if path exists
int pathLength = PROGRAM_CWD.rfind('\\');
if (pathLength > 0) PROGRAM_CWD.resize(pathLength);
else PROGRAM_CWD = "";

std::cout << PROGRAM_CWD << std::endl;

SDL_Event sdlEvent;
SDL_DisplayMode displayMode;
SDL_Surface* imageSurface;
SDL_Texture* imageTexture;

Expand All @@ -213,18 +232,9 @@ int main(int argc, char * argv[]) {
readSettings();

/* Create invisible application window */
Window win(WINDOW_WIDTH, WINDOW_HEIGHT);
Window win(WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_X, WINDOW_Y);
win.setTitle(APPLICATION_TITLE.c_str());

windowDisplayIndex = SDL_GetWindowDisplayIndex(win.window);
if (SDL_GetCurrentDisplayMode(windowDisplayIndex, &displayMode)) {
std::cerr << LOG_NOTICE << "Could not load display properties!" << std::endl;
return 1;
}

DISPLAY_WIDTH = displayMode.w;
DISPLAY_HEIGHT = displayMode.h;

//no file passed in
if (argc < 2) {
std::cerr << LOG_ERROR << "No filename provided!" << std::endl;
Expand Down Expand Up @@ -294,6 +304,9 @@ int main(int argc, char * argv[]) {
case SDL_WINDOWEVENT_CLOSE:
quit = true;
break;
case SDL_WINDOWEVENT_MOVED:
SDL_GetWindowPosition(win.window, &WINDOW_X, &WINDOW_Y);
break;
case SDL_WINDOWEVENT_SIZE_CHANGED:
SDL_GetWindowSize(win.window, &WINDOW_WIDTH, &WINDOW_HEIGHT);
draw(&win, TEXTURE_TRANSPARENCY, imageTexture);
Expand Down Expand Up @@ -378,16 +391,16 @@ int main(int argc, char * argv[]) {

}
}
std::this_thread::sleep_for(std::chrono::milliseconds(20));
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}

writeSettings();

return 0;
}

/* TODO:
* Remember window size and position
* Add key to set zoom to 1:1 pixel ratio
* Add key to change sampling mode
* Filename in title
* Possible: partial image metadata? PNG image data support from ProjectPNG?
* Navigate forward/backwards in current directory
Expand All @@ -401,5 +414,16 @@ int main(int argc, char * argv[]) {

/* KNOWN PROBLEMS:
* ICO: Files stored as "NEW PNG" type do not load with SDL_image
* ICO: Files with partial transparent pixels do not render correctly.
* ICO: Files with partial transparent pixels do not render correctly
* GIF: Animated sequences only load the first frame
*/

/* NOTES:
* - Working directory implementation will only work when program
* is run using it's full path (drag and drop, as default program),
* NOT directly from folder using terminal.
* This is not considered a high-priority fix due to complexity
* of doing it 'the right way' and minimal downside.
* It should happen infrequently and when it does, the program
* will just use the default settings.
*/

0 comments on commit 1e2d0b3

Please sign in to comment.