Skip to content

Commit

Permalink
Minor code improvements
Browse files Browse the repository at this point in the history
* fix file writing
* make more use of SDL
* add wannabe boss key
* forgot to delete translations before
  • Loading branch information
karwler committed Mar 20, 2019
1 parent f0c1c0d commit 11ab39e
Show file tree
Hide file tree
Showing 22 changed files with 148 additions and 229 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# VertiRead
A simple comic reader for Linux and Windows.
It's basically just an image viewer that shows all pictures in a directory/archive.
Currently supported file formats are JPG, PNG, TIFF, WEBP and archives.
Currently supported file formats are whatever SDL2_image and libarchive support.

Used libraries are SDL2, SDL2_image, SDL2_ttf, libarchive and by extension libjpeg, libpng, libtiff, libwebp, FreeType and zlib.
The CMakeLists.txt is written for at least CMake 3.10.2 with Clang, GCC or MSVC which need to support C++17.
Expand Down Expand Up @@ -30,3 +30,7 @@ The reader has a hidden side panel on the left.
The direction in which pictures in the reader are stacked can be set in the settings menu.
The program supports keyboard and controller bindings. DirectInput and XInput are handled separately. The bindings can be changed in the settings.
To reset certain settings, edit or delete the corresponding ini files in the settings directory or use the reset button in the settings menu to reset all settings.

## Supported files
- images: bmp, gif, jpg, lbm, pcx, png, pnm, svg, tga, tiff, webp, xcf, xpm, xv
- archives: 7z, ar, cab, cpio, empty, ISO9660, lha, lzh, mtree, pax, rar, raw, tar, xar, zip
23 changes: 0 additions & 23 deletions rsc/data/languages/Deutsch.ini

This file was deleted.

23 changes: 0 additions & 23 deletions rsc/data/languages/Polski.ini

This file was deleted.

23 changes: 0 additions & 23 deletions rsc/data/languages/Русский.ini

This file was deleted.

23 changes: 11 additions & 12 deletions src/engine/drawSys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,26 +134,26 @@ void DrawSys::drawWidgets() {

void DrawSys::drawPicture(const Picture* wgt) {
if (wgt->showBG)
drawRect(wgt->rect().getOverlap(wgt->frame()), wgt->color());
drawRect(wgt->rect().intersect(wgt->frame()), wgt->color());
if (wgt->tex)
drawImage(wgt->tex, wgt->texRect(), wgt->frame());
}

void DrawSys::drawCheckBox(const CheckBox* wgt) {
drawPicture(wgt); // draw background
drawRect(wgt->boxRect().getOverlap(wgt->frame()), wgt->boxColor()); // draw checkbox
drawRect(wgt->boxRect().intersect(wgt->frame()), wgt->boxColor()); // draw checkbox
}

void DrawSys::drawSlider(const Slider* wgt) {
Rect frame = wgt->frame();
drawPicture(wgt); // draw background
drawRect(wgt->barRect().getOverlap(frame), Color::dark); // draw bar
drawRect(wgt->sliderRect().getOverlap(frame), Color::light); // draw slider
drawRect(wgt->barRect().intersect(frame), Color::dark); // draw bar
drawRect(wgt->sliderRect().intersect(frame), Color::light); // draw slider
}

void DrawSys::drawProgressBar(const ProgressBar* wgt) {
drawRect(wgt->rect(), Color::normal); // draw background
drawRect(wgt->barRect().getOverlap(wgt->frame()), Color::light); // draw bar
drawRect(wgt->barRect().intersect(wgt->frame()), Color::light); // draw bar
}

void DrawSys::drawLabel(const Label* wgt) {
Expand Down Expand Up @@ -217,13 +217,12 @@ void DrawSys::drawImage(SDL_Texture* tex, const Rect& rect, const Rect& frame) {
}

SDL_Texture* DrawSys::renderText(const string& text, int height) {
if (text.empty())
return nullptr;

SDL_Surface* surf = TTF_RenderUTF8_Blended(fonts.getFont(height), text.c_str(), colors[uint8(Color::text)]);
SDL_Texture* tex = SDL_CreateTextureFromSurface(renderer, surf);
SDL_FreeSurface(surf);
return tex;
if (SDL_Surface* surf = TTF_RenderUTF8_Blended(fonts.getFont(height), text.c_str(), colors[uint8(Color::text)])) {
SDL_Texture* tex = SDL_CreateTextureFromSurface(renderer, surf);
SDL_FreeSurface(surf);
return tex;
}
return nullptr;
}

vector<Texture> DrawSys::loadTexturesDirectory(const string& drc) {
Expand Down
30 changes: 12 additions & 18 deletions src/engine/fileSys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ string FileSys::readTextFile(const string& file, bool printMessage) {
return text;
}

bool FileSys::writeTextFile(const std::string& file, const string& text) {
if (FILE* ofh = fopen(file.c_str(), defaultFrMode)) {
bool FileSys::writeTextFile(const string& file, const string& text) {
if (FILE* ofh = fopen(file.c_str(), defaultFwMode)) {
fwrite(text.c_str(), sizeof(char), text.length(), ofh);
fclose(ofh);
} else {
Expand All @@ -324,7 +324,7 @@ bool FileSys::writeTextFile(const std::string& file, const string& text) {
}

bool FileSys::writeTextFile(const string& file, const vector<string>& lines) {
if (FILE* ofh = fopen(file.c_str(), defaultFrMode)) {
if (FILE* ofh = fopen(file.c_str(), defaultFwMode)) {
for (const string& it : lines)
fputs(string(it + '\n').c_str(), ofh);
fclose(ofh);
Expand Down Expand Up @@ -594,24 +594,18 @@ int FileSys::moveContentThreaded(void* data) {
}

void FileSys::setWorkingDir() {
#ifdef _WIN32
wchar* buf = new wchar[MAX_PATH];
DWORD len = GetModuleFileNameW(nullptr, buf, MAX_PATH);
if (!len || len == MAX_PATH) {
delete[] buf;
buf = new wchar[pathMax];
len = GetModuleFileNameW(nullptr, buf, pathMax);
char* path = SDL_GetBasePath();
if (!path) {
std::cerr << SDL_GetError() << std::endl;
return;
}
while (len > 0 && buf[--len] != dsep); // terminate path stirng at last dsep
buf[len] = '\0';
if (!len || _wchdir(buf))
std::cerr << "failed to set working directory" << std::endl;
#ifdef _WIN32
if (_wchdir(stow(path).c_str()))
#else
char* buf = new char[PATH_MAX];
if (sizet len = sizet(readlink(linkExe, buf, PATH_MAX)); len > PATH_MAX || chdir(parentPath(string(buf, buf + len)).c_str()))
std::cerr << "failed to set working directory" << std::endl;
if (chdir(path))
#endif
delete[] buf;
std::cerr << "failed to set working directory" << std::endl;
SDL_free(path);
}

string FileSys::findFont(const string& font) {
Expand Down
16 changes: 7 additions & 9 deletions src/engine/fileSys.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,31 @@ enum FileType : uint8 {
FTYPE_ANY = 0xFF
};

inline FileType operator~(FileType a) {
inline constexpr FileType operator~(FileType a) {
return FileType(~uint8(a));
}

inline FileType operator&(FileType a, FileType b) {
inline constexpr FileType operator&(FileType a, FileType b) {
return FileType(uint8(a) & uint8(b));
}

inline FileType operator&=(FileType& a, FileType b) {
inline constexpr FileType operator&=(FileType& a, FileType b) {
return a = FileType(uint8(a) & uint8(b));
}

inline FileType operator^(FileType a, FileType b) {
inline constexpr FileType operator^(FileType a, FileType b) {
return FileType(uint8(a) ^ uint8(b));
}

inline FileType operator^=(FileType& a, FileType b) {
inline constexpr FileType operator^=(FileType& a, FileType b) {
return a = FileType(uint8(a) ^ uint8(b));
}

inline FileType operator|(FileType a, FileType b) {
inline constexpr FileType operator|(FileType a, FileType b) {
return FileType(uint8(a) | uint8(b));
}

inline FileType operator|=(FileType& a, FileType b) {
inline constexpr FileType operator|=(FileType& a, FileType b) {
return a = FileType(uint8(a) | uint8(b));
}

Expand Down Expand Up @@ -146,11 +146,9 @@ class FileSys {
#ifdef _WIN32
static const array<string, 22> takenFilenames;
static constexpr sizet drivesMax = 26;
static constexpr sizet pathMax = 32767;
static constexpr sizet fnameMax = 255;
#else
static constexpr sizet fnameMax = NAME_MAX;
static constexpr char linkExe[] = "/proc/self/exe";
#endif
static constexpr char fileThemes[] = "themes.ini";
static constexpr char fileSettings[] = "settings.ini";
Expand Down
28 changes: 15 additions & 13 deletions src/engine/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void Scene::tick(float dSec) {
}

void Scene::onMouseMove(const vec2i& mPos, const vec2i& mMov) {
setSelected(mPos, topLayout(mPos));
select = getSelected(mPos, topLayout(mPos));

if (capture)
capture->onDrag(mPos, mMov);
Expand All @@ -45,7 +45,7 @@ void Scene::onMouseDown(const vec2i& mPos, uint8 mBut, uint8 mCnt) {
capture = nullptr;
}

setSelected(mPos, topLayout(mPos)); // update in case selection has changed through keys while cursor remained at the old position
select = getSelected(mPos, topLayout(mPos)); // update in case selection has changed through keys while cursor remained at the old position
if (mCnt == 1) {
stamps[mBut] = ClickStamp(select, getSelectedScrollArea(), mPos);
if (stamps[mBut].area) // area goes first so widget can overwrite it's capture
Expand Down Expand Up @@ -115,15 +115,17 @@ void Scene::setPopup(Popup* newPopup, Widget* newCapture) {
onMouseMove(mousePos(), 0);
}

void Scene::setSelected(const vec2i& mPos, Layout* box) {
Rect frame = box->frame();
if (vector<Widget*>::const_iterator it = std::find_if(box->getWidgets().begin(), box->getWidgets().end(), [&frame, &mPos](const Widget* wi) -> bool { return wi->rect().getOverlap(frame).overlap(mPos); }); it != box->getWidgets().end()) {
if (Layout* lay = dynamic_cast<Layout*>(*it))
setSelected(mPos, lay);
else
select = (*it)->navSelectable() ? *it : box;
} else
select = box;
Widget* Scene::getSelected(const vec2i& mPos, Layout* box) {
for (;;) {
Rect frame = box->frame();
if (vector<Widget*>::const_iterator it = std::find_if(box->getWidgets().begin(), box->getWidgets().end(), [&frame, &mPos](const Widget* wi) -> bool { return wi->rect().intersect(frame).contain(mPos); }); it != box->getWidgets().end()) {
if (Layout* lay = dynamic_cast<Layout*>(*it))
box = lay;
else
return (*it)->navSelectable() ? *it : box;
} else
return box;
}
}

ScrollArea* Scene::getSelectedScrollArea() const {
Expand All @@ -141,8 +143,8 @@ bool Scene::overlayFocused(const vec2i& mPos) {
return false;

if (overlay->on)
return overlay->rect().overlap(mPos) ? true : overlay->on = false;
return overlay->actRect().overlap(mPos) ? overlay->on = true : false;
return overlay->rect().contain(mPos) ? true : overlay->on = false;
return overlay->actRect().contain(mPos) ? overlay->on = true : false;
}

void Scene::selectFirst() {
Expand Down
2 changes: 1 addition & 1 deletion src/engine/scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Scene {
bool cursorInClickRange(const vec2i& mPos, uint8 mBut);

private:
void setSelected(const vec2i& mPos, Layout* box);
Widget* getSelected(const vec2i& mPos, Layout* box);
ScrollArea* getSelectedScrollArea() const;
bool overlayFocused(const vec2i& mPos);
Layout* topLayout(const vec2i& mPos);
Expand Down
9 changes: 8 additions & 1 deletion src/engine/windowSys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void WindowSys::init() {
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMECONTROLLER))
throw std::runtime_error(string("Failed to initialize SDL:\n") + SDL_GetError());
if (TTF_Init())
throw std::runtime_error(string("Failed to initialize fonts:\n") + SDL_GetError());
throw std::runtime_error(string("Failed to initialize fonts:\n") + TTF_GetError());
SDL_StopTextInput(); // for some reason TextInput is on

int flags = IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG | IMG_INIT_TIF | IMG_INIT_WEBP);
Expand Down Expand Up @@ -193,6 +193,13 @@ void WindowSys::moveCursor(const vec2i& mov) {
SDL_WarpMouseInWindow(window, px + mov.x, py + mov.y);
}

void WindowSys::toggleOpacity() {
if (float val; !SDL_GetWindowOpacity(window, &val))
SDL_SetWindowOpacity(window, val < 1.f ? 1.f : 0.f);
else
SDL_MinimizeWindow(window);
}

void WindowSys::setFullscreen(bool on) {
sets->fullscreen = on;
SDL_SetWindowFullscreen(window, on ? SDL_GetWindowFlags(window) | SDL_WINDOW_FULLSCREEN_DESKTOP : SDL_GetWindowFlags(window) & uint32(~SDL_WINDOW_FULLSCREEN_DESKTOP));
Expand Down
1 change: 1 addition & 0 deletions src/engine/windowSys.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class WindowSys {
vec2i displayResolution() const;
void setWindowPos(const vec2i& pos);
void moveCursor(const vec2i& mov);
void toggleOpacity();
void setFullscreen(bool on);
void setResolution(const vec2i& res);
void setRenderer(const string& name);
Expand Down
4 changes: 4 additions & 0 deletions src/prog/progs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ void ProgState::eventHide() {
World::sets()->showHidden = !World::sets()->showHidden;
}

void ProgState::eventBoss() {
World::winSys()->toggleOpacity();
}

void ProgState::eventRefresh() {
World::scene()->resetLayouts();
}
Expand Down
1 change: 1 addition & 0 deletions src/prog/progs.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class ProgState {
virtual void eventPrevDir() {}
virtual void eventFullscreen();
virtual void eventHide();
void eventBoss();
void eventRefresh();
virtual void eventFileDrop(const string&) {}
virtual void eventClosing() {}
Expand Down
6 changes: 3 additions & 3 deletions src/utils/layouts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ void ScrollArea::onHold(const vec2i& mPos, uint8 mBut) {

if (mBut == SDL_BUTTON_LEFT) { // check scroll bar left click
World::scene()->capture = this;
if (draggingSlider = barRect().overlap(mPos)) {
if (draggingSlider = barRect().contain(mPos)) {
sizet di = direction.vertical();
if (int sp = sliderPos(), ss = sliderSize(); outRange(mPos[di], sp, sp + ss)) // if mouse outside of slider but inside bar
setSlider(mPos[di] - ss /2);
Expand Down Expand Up @@ -386,7 +386,7 @@ void ScrollArea::scrollToLimit(bool start) {
}

Rect ScrollArea::frame() const {
return parent ? rect().getOverlap(parent->frame()) : rect();
return parent ? rect().intersect(parent->frame()) : rect();
}

vec2i ScrollArea::wgtPosition(sizet id) const {
Expand Down Expand Up @@ -625,7 +625,7 @@ void ReaderBox::postInit() {
void ReaderBox::onMouseMove(const vec2i& mPos, const vec2i& mMov) {
Layout::onMouseMove(mPos, mMov);

countDown = World::scene()->cursorDisableable() && rect().overlap(mPos) && !showBar() && World::scene()->capture != this && cursorTimer > 0.f;
countDown = World::scene()->cursorDisableable() && rect().contain(mPos) && !showBar() && World::scene()->capture != this && cursorTimer > 0.f;
if (cursorTimer < menuHideTimeout) {
cursorTimer = menuHideTimeout;
SDL_ShowCursor(SDL_ENABLE);
Expand Down
Loading

0 comments on commit 11ab39e

Please sign in to comment.