Skip to content

Commit

Permalink
Import and namespace cleanup, add simple about window.
Browse files Browse the repository at this point in the history
  • Loading branch information
SkylerRankin committed Dec 28, 2024
1 parent ae0abbb commit 3779bc0
Show file tree
Hide file tree
Showing 13 changed files with 313 additions and 273 deletions.
21 changes: 8 additions & 13 deletions src/application.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
#include <chrono>
#include <ctime>
#include <filesystem>
#include <format>
#include <fstream>
#include <iostream>
#include "application.h"
#include "cache.h"
#include "ui.h"

Application::Application(GLFWwindow* window) : window(window) {
loadConfig(config);
Config::loadConfig(config);
preloadNextImageCount = config.cacheForwardPreload;
preloadPreviousImageCount = config.cacheBackwardPreload;

Expand Down Expand Up @@ -64,7 +59,7 @@ Application::Application(GLFWwindow* window) : window(window) {
};

ui->onSaveGroup = [this](int i) -> void {
ImageGroup& group = groups.at(i);
auto& group = groups.at(i);
for (int id : group.ids) {
cache->getImage(id)->saved = true;
cache->getImage(id)->skipped = false;
Expand All @@ -74,7 +69,7 @@ Application::Application(GLFWwindow* window) : window(window) {
};

ui->onSkipGroup = [this](int i) -> void {
ImageGroup& group = groups.at(i);
auto& group = groups.at(i);
for (int id : group.ids) {
cache->getImage(id)->skipped = true;
cache->getImage(id)->saved = false;
Expand All @@ -84,7 +79,7 @@ Application::Application(GLFWwindow* window) : window(window) {
};

ui->onResetGroup = [this](int i) -> void {
ImageGroup& group = groups.at(i);
auto& group = groups.at(i);
for (int id : group.ids) {
cache->getImage(id)->skipped = false;
cache->getImage(id)->saved = false;
Expand All @@ -93,9 +88,9 @@ Application::Application(GLFWwindow* window) : window(window) {
group.savedCount = 0;
};

ui->onConfigUpdate = [this](Config& updateConfig) -> void {
ui->onConfigUpdate = [this](Config::Config& updateConfig) -> void {
config.update(updateConfig);
saveConfig(config);
Config::saveConfig(config);
cache->updateCapacity(config.cacheCapacity);
ui->onDirectoryClosed();
};
Expand Down Expand Up @@ -213,7 +208,7 @@ void Application::toggleSkipImage(int id) {
Image* image = cache->getImage(id);

// TODO: have a mapping from image id to group index to avoid this linear search
for (ImageGroup& group : groups) {
for (auto& group : groups) {
if (std::find(group.ids.begin(), group.ids.end(), id) != group.ids.end()) {
if (image->skipped) {
group.skippedCount--;
Expand All @@ -234,7 +229,7 @@ void Application::toggleSkipImage(int id) {
void Application::toggleSaveImage(int id) {
Image* image = cache->getImage(id);

for (ImageGroup& group : groups) {
for (auto& group : groups) {
if (std::find(group.ids.begin(), group.ids.end(), id) != group.ids.end()) {
if (image->saved) {
group.savedCount--;
Expand Down
16 changes: 8 additions & 8 deletions src/cache.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include <filesystem>
#include <iostream>
#include <thread>
#include <fstream>
#include <iostream>
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include <stb_image_resize2.h>
#include "glCommon.h"
#include "cache.h"
#include <thread>
#include <TinyEXIF.h>
#include "cache.h"
#include "glCommon.h"

namespace fs = std::filesystem;

Expand All @@ -30,7 +30,7 @@ ImageCache::ImageCache(int capacity) : cacheCapacity(capacity) {
}

// Create the PBOs usable for texture uploads
//TODO: is this an appropriate number of PBOs given the number of threads?
// TODO: is this an appropriate number of PBOs given the number of threads?
int pboCount = imageLoadThreads;
for (int i = 0; i < pboCount; i++) {
PBOQueueEntry entry{
Expand Down Expand Up @@ -113,7 +113,7 @@ void ImageCache::frameUpdate() {
processPendingPBOQueue();
processPendingImageQueue();
processTextureQueue();
assert(textureIds.size() <= cacheCapacity && std::format("Number of in-use textures ({}) has exceeded cache capacity ({}).", textureIds.size(), cacheCapacity).c_str());
assert(textureIds.size() <= cacheCapacity && "Number of in-use textures has exceeded cache capacity.");
}

void ImageCache::updateCapacity(int capacity) {
Expand All @@ -138,11 +138,11 @@ const std::map<int, Image>& ImageCache::getImages() const {
return images;
}

bool ImageCache::previewLoadingComplete() {
bool ImageCache::previewLoadingComplete() const {
return previewsLoaded.size() == images.size();
}

float ImageCache::getPreviewLoadProgress() {
float ImageCache::getPreviewLoadProgress() const {
return previewsLoaded.size() / (float)images.size();
}

Expand Down
120 changes: 62 additions & 58 deletions src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,78 +4,82 @@
#include "config.h"
#include "version.h"

namespace fs = std::filesystem;

static const char* configFilename = "cormorant.config";
static char* executablePath = nullptr;

std::string trim(std::string s) {
int start = 0;
while (start < s.size() - 1 && std::isspace(s.at(start))) {
start++;
namespace Config {
namespace {
const char* configFilename = "cormorant.config";
char* executablePath = nullptr;
}

size_t end = s.size() - 1;
while (end >= start && std::isspace(s.at(end))) {
end--;
}
namespace fs = std::filesystem;

return s.substr(start, end - start + 1);
}
std::string trim(std::string s) {
int start = 0;
while (start < s.size() - 1 && std::isspace(s.at(start))) {
start++;
}

void setExecutablePath() {
int pathLength = wai_getExecutablePath(nullptr, 0, nullptr);
executablePath = new char[pathLength + 1];
wai_getExecutablePath(executablePath, pathLength, nullptr);
executablePath[pathLength] = '\0';
}
size_t end = s.size() - 1;
while (end >= start && std::isspace(s.at(end))) {
end--;
}

void loadConfig(Config& config) {
if (executablePath == nullptr) {
setExecutablePath();
return s.substr(start, end - start + 1);
}

fs::path configPath = fs::path(executablePath).parent_path().append(configFilename);
if (fs::exists(configPath)) {
std::ifstream stream(configPath);
std::string line;
while (std::getline(stream, line)) {
size_t equalsIndex = line.find("=");
if (equalsIndex != std::string::npos) {
std::string key = trim(line.substr(0, equalsIndex));
std::string value = trim(line.substr(equalsIndex + 1));
std::optional<int> intValue = std::nullopt;
try {
intValue.emplace(std::stoi(value));
} catch (...) {}
void setExecutablePath() {
int pathLength = wai_getExecutablePath(nullptr, 0, nullptr);
executablePath = new char[pathLength + 1];
wai_getExecutablePath(executablePath, pathLength, nullptr);
executablePath[pathLength] = '\0';
}

void loadConfig(Config& config) {
if (executablePath == nullptr) {
setExecutablePath();
}

if (key == "cacheCapacity" && intValue.has_value()) {
config.cacheCapacity = intValue.value();
} else if (key == "cacheBackwardPreload" && intValue.has_value()) {
config.cacheBackwardPreload = intValue.value();
} else if (key == "cacheForwardPreload" && intValue.has_value()) {
config.cacheForwardPreload = intValue.value();
fs::path configPath = fs::path(executablePath).parent_path().append(configFilename);
if (fs::exists(configPath)) {
std::ifstream stream(configPath);
std::string line;
while (std::getline(stream, line)) {
size_t equalsIndex = line.find("=");
if (equalsIndex != std::string::npos) {
std::string key = trim(line.substr(0, equalsIndex));
std::string value = trim(line.substr(equalsIndex + 1));
std::optional<int> intValue = std::nullopt;
try {
intValue.emplace(std::stoi(value));
} catch (...) {}

if (key == "cacheCapacity" && intValue.has_value()) {
config.cacheCapacity = intValue.value();
} else if (key == "cacheBackwardPreload" && intValue.has_value()) {
config.cacheBackwardPreload = intValue.value();
} else if (key == "cacheForwardPreload" && intValue.has_value()) {
config.cacheForwardPreload = intValue.value();
}
}
}
}

stream.close();
stream.close();
}
}
}

void saveConfig(const Config& config) {
if (executablePath == nullptr) {
setExecutablePath();
}
void saveConfig(const Config& config) {
if (executablePath == nullptr) {
setExecutablePath();
}

fs::path configPath = fs::path(executablePath).parent_path().append(configFilename);
std::ofstream stream(configPath);
if (!stream.is_open()) return;
fs::path configPath = fs::path(executablePath).parent_path().append(configFilename);
std::ofstream stream(configPath);
if (!stream.is_open()) return;

stream << "version = " << VERSION << std::endl;
stream << "cacheCapacity = " << config.cacheCapacity << std::endl;
stream << "cacheBackwardPreload = " << config.cacheBackwardPreload << std::endl;
stream << "cacheForwardPreload = " << config.cacheForwardPreload << std::endl;
stream << "version = " << VERSION << std::endl;
stream << "cacheCapacity = " << config.cacheCapacity << std::endl;
stream << "cacheBackwardPreload = " << config.cacheBackwardPreload << std::endl;
stream << "cacheForwardPreload = " << config.cacheForwardPreload << std::endl;

stream.close();
stream.close();
}
}
Loading

0 comments on commit 3779bc0

Please sign in to comment.