diff --git a/CMakeLists.txt b/CMakeLists.txt index 99eafd0..ebb8752 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,6 +31,7 @@ set(DinguxCommander_SRCS main.cpp panel.cpp resourceManager.cpp + screen.cpp sdlutils.cpp viewer.cpp window.cpp @@ -72,13 +73,17 @@ target_link_libraries(${BIN_TARGET} PRIVATE ) if(TARGET_PLATFORM STREQUAL "retrofw") + target_compile_definitions(${BIN_TARGET} PRIVATE TARGET_PLATFORM_RETROFW) include(CMake/retrofw_defs.cmake) elseif(TARGET_PLATFORM STREQUAL "rg350") + target_compile_definitions(${BIN_TARGET} PRIVATE TARGET_PLATFORM_RG350) include(CMake/rg350_defs.cmake) endif() foreach( def_name + SCREEN_WIDTH + SCREEN_HEIGHT PPU_X PPU_Y RES_DIR diff --git a/commander.cpp b/commander.cpp index 7e87078..5a9559f 100755 --- a/commander.cpp +++ b/commander.cpp @@ -2,6 +2,7 @@ #include #include "commander.h" #include "resourceManager.h" +#include "screen.h" #include "sdlutils.h" #include "def.h" #include "dialog.h" @@ -13,38 +14,38 @@ #define SPLITTER_LINE_W 1 #define X_LEFT 1 -#define X_RIGHT SCREEN_WIDTH / 2 + SPLITTER_LINE_W + 1 +#define X_RIGHT screen.w / 2 + SPLITTER_LINE_W + 1 namespace { SDL_Surface *DrawBackground() { - SDL_Surface *bg = SDL_utils::createSurface(SCREEN_WIDTH * PPU_X, SCREEN_HEIGHT * PPU_Y); + SDL_Surface *bg = SDL_utils::createSurface(screen.w * screen.ppu_x, screen.h * screen.ppu_y); // Stripes - const int stripes_h = SCREEN_HEIGHT - HEADER_H - FOOTER_H; - SDL_Rect rect = SDL_utils::Rect(0, 0, SCREEN_WIDTH * PPU_X, SCREEN_HEIGHT * PPU_Y); + const int stripes_h = screen.h - HEADER_H - FOOTER_H; + SDL_Rect rect = SDL_utils::Rect(0, 0, screen.w * screen.ppu_x, screen.h * screen.ppu_y); const Uint32 bg_colors[2] = {SDL_MapRGB(bg->format, COLOR_BG_1), SDL_MapRGB(bg->format, COLOR_BG_2)}; const std::size_t num_lines = (stripes_h - 1) / LINE_HEIGHT + 1; for (std::size_t i = 0; i < num_lines; ++i) { - rect.y = (Y_LIST + i * LINE_HEIGHT) * PPU_Y; + rect.y = (Y_LIST + i * LINE_HEIGHT) * screen.ppu_y; SDL_FillRect(bg, &rect, bg_colors[i % 2]); } // Top and bottom bars const auto bar_color = SDL_MapRGB(bg->format, COLOR_TITLE_BG); - rect = SDL_utils::Rect(0, 0, static_cast(bg->w), Y_LIST * PPU_Y); + rect = SDL_utils::Rect(0, 0, static_cast(bg->w), Y_LIST * screen.ppu_y); SDL_FillRect(bg, &rect, bar_color); - rect.y = bg->h - FOOTER_H * PPU_Y; + rect.y = bg->h - FOOTER_H * screen.ppu_y; SDL_FillRect(bg, &rect, bar_color); // Line in the middle - rect = SDL_utils::Rect(SCREEN_WIDTH / 2 * PPU_X, 0, SPLITTER_LINE_W * PPU_X, Y_LIST * PPU_Y); + rect = SDL_utils::Rect(screen.w / 2 * screen.ppu_x, 0, SPLITTER_LINE_W * screen.ppu_x, Y_LIST * screen.ppu_y); SDL_FillRect(bg, &rect, bg_colors[0]); rect.y = rect.h; - rect.h = stripes_h * PPU_Y; + rect.h = stripes_h * screen.ppu_y; SDL_FillRect(bg, &rect, bar_color); rect.y += rect.h; - rect.h = FOOTER_H * PPU_Y; + rect.h = FOOTER_H * screen.ppu_y; SDL_FillRect(bg, &rect, bg_colors[0]); return bg; diff --git a/def.h b/def.h index a6f5bdd..d64b847 100755 --- a/def.h +++ b/def.h @@ -48,13 +48,13 @@ #define FOOTER_H 13 #define FOOTER_PADDING_TOP 1 -#define FOOTER_Y (SCREEN_HEIGHT - FOOTER_H) +#define FOOTER_Y (screen.h - FOOTER_H) #define Y_LIST HEADER_H #define LINE_HEIGHT 15 -#define NB_VISIBLE_LINES ((SCREEN_HEIGHT - FOOTER_H - HEADER_H - 1) / LINE_HEIGHT + 1) -#define NB_FULLY_VISIBLE_LINES ((SCREEN_HEIGHT - FOOTER_H - HEADER_H) / LINE_HEIGHT) +#define NB_VISIBLE_LINES ((screen.h - FOOTER_H - HEADER_H - 1) / LINE_HEIGHT + 1) +#define NB_FULLY_VISIBLE_LINES ((screen.h - FOOTER_H - HEADER_H) / LINE_HEIGHT) // Dialogs #define DIALOG_BORDER 2 diff --git a/dialog.cpp b/dialog.cpp index 46bad1d..d90b5d4 100755 --- a/dialog.cpp +++ b/dialog.cpp @@ -1,5 +1,6 @@ #include #include "dialog.h" +#include "screen.h" #include "sdlutils.h" #include "resourceManager.h" #include "def.h" @@ -98,38 +99,38 @@ void CDialog::init(void) if (m_linesImg.back()->w > l_width) l_width = m_linesImg.back()->w; } - l_width /= PPU_X; + l_width /= screen.ppu_x; // Cursor width l_cursorWidth = l_width + 2 * DIALOG_MARGIN; - if (l_cursorWidth > SCREEN_WIDTH - 2 * DIALOG_BORDER) - l_cursorWidth = SCREEN_WIDTH - 2 * DIALOG_BORDER; + if (l_cursorWidth > screen.w - 2 * DIALOG_BORDER) + l_cursorWidth = screen.w - 2 * DIALOG_BORDER; // Line clip m_clip.h = m_linesImg.front()->h; - m_clip.w = (l_cursorWidth - DIALOG_MARGIN - 1) * PPU_X; + m_clip.w = (l_cursorWidth - DIALOG_MARGIN - 1) * screen.ppu_x; // Adjust image width l_width = l_width + 2 * DIALOG_MARGIN + 2 * DIALOG_BORDER; - if (l_width > SCREEN_WIDTH) - l_width = SCREEN_WIDTH; + if (l_width > screen.w) + l_width = screen.w; // Create dialog image const int m_image_h = m_lines.size() * LINE_HEIGHT + 2 * DIALOG_BORDER; - m_image = SDL_utils::createImage(l_width * PPU_X, m_image_h * PPU_Y, SDL_MapRGB(Globals::g_screen->format, COLOR_BORDER)); + m_image = SDL_utils::createImage(l_width * screen.ppu_x, m_image_h * screen.ppu_y, SDL_MapRGB(Globals::g_screen->format, COLOR_BORDER)); { SDL_Rect l_rect; - l_rect.x = DIALOG_BORDER * PPU_X; - l_rect.y = (DIALOG_BORDER + m_nbTitle * LINE_HEIGHT) * PPU_Y; - l_rect.w = m_image->w - 2 * DIALOG_BORDER * PPU_X; - l_rect.h = (m_image_h - 2 * DIALOG_BORDER - m_nbTitle * LINE_HEIGHT) * PPU_Y; + l_rect.x = DIALOG_BORDER * screen.ppu_x; + l_rect.y = (DIALOG_BORDER + m_nbTitle * LINE_HEIGHT) * screen.ppu_y; + l_rect.w = m_image->w - 2 * DIALOG_BORDER * screen.ppu_x; + l_rect.h = (m_image_h - 2 * DIALOG_BORDER - m_nbTitle * LINE_HEIGHT) * screen.ppu_y; SDL_FillRect(m_image, &l_rect, SDL_MapRGB(m_image->format, COLOR_BG_1)); } // Create cursor image - m_cursor1 = SDL_utils::createImage(l_cursorWidth * PPU_X, LINE_HEIGHT * PPU_Y, SDL_MapRGB(Globals::g_screen->format, COLOR_CURSOR_1)); - m_cursor2 = SDL_utils::createImage(l_cursorWidth * PPU_X, LINE_HEIGHT * PPU_Y, SDL_MapRGB(Globals::g_screen->format, COLOR_CURSOR_2)); + m_cursor1 = SDL_utils::createImage(l_cursorWidth * screen.ppu_x, LINE_HEIGHT * screen.ppu_y, SDL_MapRGB(Globals::g_screen->format, COLOR_CURSOR_1)); + m_cursor2 = SDL_utils::createImage(l_cursorWidth * screen.ppu_x, LINE_HEIGHT * screen.ppu_y, SDL_MapRGB(Globals::g_screen->format, COLOR_CURSOR_2)); // Adjust dialog coordinates if (!m_x) - m_x = (SCREEN_WIDTH - m_image->w / PPU_X) / 2; + m_x = (screen.w - m_image->w / screen.ppu_x) / 2; if (!m_y) { - m_y = (SCREEN_HEIGHT - m_image_h) / 2; + m_y = (screen.h - m_image_h) / 2; } else { diff --git a/keyboard.cpp b/keyboard.cpp index ac21488..ac67ea8 100755 --- a/keyboard.cpp +++ b/keyboard.cpp @@ -1,5 +1,6 @@ #include #include "keyboard.h" +#include "screen.h" #include "sdlutils.h" #include "resourceManager.h" #include "def.h" @@ -29,55 +30,55 @@ CKeyboard::CKeyboard(const std::string &p_inputText): { SDL_Rect l_rect; // Create keyboard image - m_imageKeyboard = SDL_utils::createImage(265 * PPU_X, 84 * PPU_Y, SDL_MapRGB(Globals::g_screen->format, COLOR_BORDER)); - l_rect.x = 2 * PPU_X; - l_rect.y = 2 * PPU_Y; - l_rect.w = 261 * PPU_X; - l_rect.h = 80 * PPU_Y; + m_imageKeyboard = SDL_utils::createImage(265 * screen.ppu_x, 84 * screen.ppu_y, SDL_MapRGB(Globals::g_screen->format, COLOR_BORDER)); + l_rect.x = 2 * screen.ppu_x; + l_rect.y = 2 * screen.ppu_y; + l_rect.w = 261 * screen.ppu_x; + l_rect.h = 80 * screen.ppu_y; SDL_FillRect(m_imageKeyboard, &l_rect, SDL_MapRGB(m_imageKeyboard->format, COLOR_BG_2)); // Keys for (unsigned int l_y = 0; l_y < 3; ++l_y) { for (unsigned int l_x = 0; l_x < 13; ++l_x) { - l_rect.x = (3 + 20 * l_x) * PPU_X; - l_rect.y = (3 + 20 * l_y) * PPU_Y; - l_rect.w = 19 * PPU_X; - l_rect.h = 18 * PPU_Y; + l_rect.x = (3 + 20 * l_x) * screen.ppu_x; + l_rect.y = (3 + 20 * l_y) * screen.ppu_y; + l_rect.w = 19 * screen.ppu_x; + l_rect.h = 18 * screen.ppu_y; SDL_FillRect(m_imageKeyboard, &l_rect, SDL_MapRGB(m_imageKeyboard->format, COLOR_BORDER)); - l_rect.x += 1 * PPU_X; - l_rect.y += 1 * PPU_Y; - l_rect.w -= 2 * PPU_X; - l_rect.h -= 2 * PPU_Y; + l_rect.x += 1 * screen.ppu_x; + l_rect.y += 1 * screen.ppu_y; + l_rect.w -= 2 * screen.ppu_x; + l_rect.h -= 2 * screen.ppu_y; SDL_FillRect(m_imageKeyboard, &l_rect, SDL_MapRGB(m_imageKeyboard->format, COLOR_BG_1)); } } // Buttons Cancel and OK - l_rect.x = 3 * PPU_X; - l_rect.y = 63 * PPU_Y; - l_rect.w = 129 * PPU_X; - l_rect.h = 18 * PPU_Y; + l_rect.x = 3 * screen.ppu_x; + l_rect.y = 63 * screen.ppu_y; + l_rect.w = 129 * screen.ppu_x; + l_rect.h = 18 * screen.ppu_y; SDL_FillRect(m_imageKeyboard, &l_rect, SDL_MapRGB(m_imageKeyboard->format, COLOR_BORDER)); - l_rect.x = 133 * PPU_X; + l_rect.x = 133 * screen.ppu_x; SDL_FillRect(m_imageKeyboard, &l_rect, SDL_MapRGB(m_imageKeyboard->format, COLOR_BORDER)); - l_rect.w -= 2 * PPU_X; - l_rect.h -= 2 * PPU_Y; - l_rect.y += 1 * PPU_Y; - l_rect.x = 4 * PPU_X; + l_rect.w -= 2 * screen.ppu_x; + l_rect.h -= 2 * screen.ppu_y; + l_rect.y += 1 * screen.ppu_y; + l_rect.x = 4 * screen.ppu_x; SDL_FillRect(m_imageKeyboard, &l_rect, SDL_MapRGB(m_imageKeyboard->format, COLOR_BG_1)); - l_rect.x = 134 * PPU_X; + l_rect.x = 134 * screen.ppu_x; SDL_FillRect(m_imageKeyboard, &l_rect, SDL_MapRGB(m_imageKeyboard->format, COLOR_BG_1)); // Create text field image - m_textField = SDL_utils::createImage(265 * PPU_X, 19 * PPU_Y, SDL_MapRGB(Globals::g_screen->format, COLOR_BORDER)); - l_rect.x = 2 * PPU_X; - l_rect.y = 2 * PPU_Y; - l_rect.w = 261 * PPU_X; - l_rect.h = 15 * PPU_Y; + m_textField = SDL_utils::createImage(265 * screen.ppu_x, 19 * screen.ppu_y, SDL_MapRGB(Globals::g_screen->format, COLOR_BORDER)); + l_rect.x = 2 * screen.ppu_x; + l_rect.y = 2 * screen.ppu_y; + l_rect.w = 261 * screen.ppu_x; + l_rect.h = 15 * screen.ppu_y; SDL_FillRect(m_textField, &l_rect, SDL_MapRGB(m_imageKeyboard->format, COLOR_BG_1)); } // Create footer - m_footer = SDL_utils::createImage(SCREEN_WIDTH * PPU_X, FOOTER_H * PPU_Y, SDL_MapRGB(Globals::g_screen->format, COLOR_BORDER)); - SDL_utils::applyText(SCREEN_WIDTH >> 1, 1, m_footer, m_font, "A-Input B-Cancel START-OK L/R-Change Y-Backspace X-Space", Globals::g_colorTextTitle, {COLOR_TITLE_BG}, SDL_utils::T_TEXT_ALIGN_CENTER); + m_footer = SDL_utils::createImage(screen.w * screen.ppu_x, FOOTER_H * screen.ppu_y, SDL_MapRGB(Globals::g_screen->format, COLOR_BORDER)); + SDL_utils::applyText(screen.w >> 1, 1, m_footer, m_font, "A-Input B-Cancel START-OK L/R-Change Y-Backspace X-Space", Globals::g_colorTextTitle, {COLOR_TITLE_BG}, SDL_utils::T_TEXT_ALIGN_CENTER); } CKeyboard::~CKeyboard(void) @@ -159,10 +160,10 @@ void CKeyboard::render(const bool p_focus) const l_rect.x = KB_X + 4 + (m_selected == 40) * 130; l_rect.y = KB_Y + 64; } - l_rect.x *= PPU_X; - l_rect.y *= PPU_Y; - l_rect.w *= PPU_X; - l_rect.h *= PPU_Y; + l_rect.x *= screen.ppu_x; + l_rect.y *= screen.ppu_y; + l_rect.w *= screen.ppu_x; + l_rect.h *= screen.ppu_y; SDL_FillRect(Globals::g_screen, &l_rect, SDL_MapRGB(Globals::g_screen->format, COLOR_CURSOR_1)); } // Draw keys text diff --git a/main.cpp b/main.cpp index f03c809..45a8fda 100755 --- a/main.cpp +++ b/main.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -5,6 +6,7 @@ #include #include #include "def.h" +#include "screen.h" #include "sdlutils.h" #include "resourceManager.h" #include "commander.h" @@ -18,6 +20,21 @@ const SDL_Color Globals::g_colorTextDir = {COLOR_TEXT_DIR}; const SDL_Color Globals::g_colorTextSelected = {COLOR_TEXT_SELECTED}; std::vector Globals::g_windows; +namespace { + +SDL_Surface *SetVideoMode(int width, int height, int bpp, std::uint32_t flags) { + fprintf(stderr, "Setting video mode %dx%d bpp=%u flags=0x%08X\n", width, height, bpp, flags); + fflush(stderr); + auto *result = SDL_SetVideoMode(width, height, bpp, flags); + const auto ¤t = *SDL_GetVideoInfo(); + fprintf(stderr, "Video mode is now %dx%d bpp=%u flags=0x%08X\n", + current.current_w, current.current_h, current.vfmt->BitsPerPixel, SDL_GetVideoSurface()->flags); + fflush(stderr); + return result; +} + +} // namespace + int main(int argc, char** argv) { // Avoid crash due to the absence of mouse @@ -35,7 +52,35 @@ int main(int argc, char** argv) SDL_ShowCursor(SDL_DISABLE); // Screen - ScreenSurface = SDL_SetVideoMode(SCREEN_WIDTH * PPU_X, SCREEN_HEIGHT * PPU_Y, SCREEN_BPP, SURFACE_FLAGS); + const auto &best = *SDL_GetVideoInfo(); + fprintf(stderr, "Best video mode reported as: %dx%d bpp=%d hw_available=%u\n", + best.current_w, best.current_h, best.vfmt->BitsPerPixel, best.hw_available); + + // Detect non 320x240/480 screens. +#if defined(TARGET_PLATFORM_RETROFW) || defined(TARGET_PLATFORM_RG350) + if (best.current_w >= SCREEN_WIDTH * 2) + { + // E.g. 640x480. Upscale to the smaller of the two. + double scale = std::min( + best.current_w / static_cast(SCREEN_WIDTH), + best.current_h / static_cast(SCREEN_HEIGHT)); + scale = std::min(scale, 2.0); + screen.ppu_x = screen.ppu_y = scale; + screen.w = best.current_w / scale; + screen.h = best.current_h / scale; + screen.actual_w = best.current_w; + screen.actual_h = best.current_h; + } + else if (best.current_w != SCREEN_WIDTH) + { + // E.g. RS07 with 480x272 screen. + screen.actual_w = screen.w = best.current_w; + screen.actual_h = screen.h = best.current_h; + screen.ppu_x = screen.ppu_y = 1; + } +#endif + ScreenSurface = SetVideoMode(screen.actual_w, screen.actual_h, SCREEN_BPP, SURFACE_FLAGS); + Globals::g_screen = ScreenSurface; if (Globals::g_screen == NULL) { diff --git a/panel.cpp b/panel.cpp index 01d9b30..ff5dcc7 100755 --- a/panel.cpp +++ b/panel.cpp @@ -2,13 +2,14 @@ #include #include "panel.h" #include "resourceManager.h" +#include "screen.h" #include "sdlutils.h" #include "fileutils.h" namespace { -constexpr int PANEL_SIZE = SCREEN_WIDTH / 2 - 2; -constexpr int NAME_SIZE = PANEL_SIZE - 18; -constexpr int CONTENTS_H = SCREEN_HEIGHT - HEADER_H - FOOTER_H; +#define PANEL_SIZE (screen.w / 2 - 2) +#define NAME_SIZE (PANEL_SIZE - 18) +#define CONTENTS_H (screen.h - HEADER_H - FOOTER_H) } // namespace CPanel::CPanel(const std::string &p_path, const Sint16 p_x): @@ -47,7 +48,7 @@ CPanel::~CPanel(void) void CPanel::render(const bool p_active) const { // Draw panel - const Sint16 l_x = m_x + m_iconDir->w / PPU_X + 2; + const Sint16 l_x = m_x + m_iconDir->w / screen.ppu_x + 2; const unsigned int l_nbTotal = m_fileLister.getNbTotal(); Sint16 l_y = Y_LIST; SDL_Surface *l_surfaceTmp = NULL; @@ -55,11 +56,11 @@ void CPanel::render(const bool p_active) const SDL_Rect l_rect; // Current dir l_surfaceTmp = SDL_utils::renderText(m_font, m_currentPath, Globals::g_colorTextTitle, {COLOR_TITLE_BG}); - if (l_surfaceTmp->w > PANEL_SIZE * PPU_X) + if (l_surfaceTmp->w > PANEL_SIZE * screen.ppu_x) { - l_rect.x = l_surfaceTmp->w - PANEL_SIZE * PPU_X; + l_rect.x = l_surfaceTmp->w - PANEL_SIZE * screen.ppu_x; l_rect.y = 0; - l_rect.w = PANEL_SIZE * PPU_X; + l_rect.w = PANEL_SIZE * screen.ppu_x; l_rect.h = l_surfaceTmp->h; SDL_utils::applySurface(m_x, HEADER_PADDING_TOP, l_surfaceTmp, Globals::g_screen, &l_rect); } @@ -68,7 +69,7 @@ void CPanel::render(const bool p_active) const SDL_utils::applySurface(m_x, HEADER_PADDING_TOP, l_surfaceTmp, Globals::g_screen); } SDL_FreeSurface(l_surfaceTmp); - SDL_Rect clip_contents_rect = SDL_utils::Rect(0, Y_LIST * PPU_Y, SCREEN_WIDTH * PPU_X, CONTENTS_H * PPU_Y); + SDL_Rect clip_contents_rect = SDL_utils::Rect(0, Y_LIST * screen.ppu_y, screen.w * screen.ppu_x, CONTENTS_H * screen.ppu_y); // Content SDL_SetClipRect(Globals::g_screen, &clip_contents_rect); // Draw cursor @@ -120,11 +121,11 @@ void CPanel::render(const bool p_active) const l_bg = kLineBg[(l_i - m_camera) % 2]; } l_surfaceTmp = SDL_utils::renderText(m_font, m_fileLister[l_i].m_name, *l_color, l_bg); - if (l_surfaceTmp->w > NAME_SIZE * PPU_X) + if (l_surfaceTmp->w > NAME_SIZE * screen.ppu_x) { l_rect.x = 0; l_rect.y = 0; - l_rect.w = NAME_SIZE * PPU_X; + l_rect.w = NAME_SIZE * screen.ppu_x; l_rect.h = l_surfaceTmp->h; SDL_utils::applySurface(l_x, l_y + 2, l_surfaceTmp, Globals::g_screen, &l_rect); } diff --git a/resourceManager.cpp b/resourceManager.cpp index 895fec6..ef8c14b 100755 --- a/resourceManager.cpp +++ b/resourceManager.cpp @@ -4,6 +4,7 @@ #include #include "resourceManager.h" #include "def.h" +#include "screen.h" #include "sdlutils.h" namespace { @@ -16,10 +17,10 @@ SDL_Surface *LoadIcon(const char *path) { return nullptr; } SDL_Surface *scaled = nullptr; - if ((PPU_X == 1 || PPU_X == 2) && (PPU_Y == 1 || PPU_Y == 2)) { - scaled = shrinkSurface(img, 2 / PPU_X, 2 / PPU_Y); + if ((screen.ppu_x == 1 || screen.ppu_x == 2) && (screen.ppu_y == 1 || screen.ppu_y == 2)) { + scaled = shrinkSurface(img, 2 / screen.ppu_x, 2 / screen.ppu_y); } else { - scaled = zoomSurface(img, PPU_X / 2, PPU_Y / 2, SMOOTHING_ON); + scaled = zoomSurface(img, screen.ppu_x / 2, screen.ppu_y / 2, SMOOTHING_ON); } SDL_FreeSurface(img); SDL_Surface *display = SDL_DisplayFormatAlpha(scaled); @@ -45,8 +46,8 @@ CResourceManager::CResourceManager(void) : m_surfaces[T_SURFACE_FILE_INSTALLABLE_PACKAGE] = LoadIcon(RES_DIR "file-ipk.png"); m_surfaces[T_SURFACE_FILE_PACKAGE] = LoadIcon(RES_DIR "file-opk.png"); m_surfaces[T_SURFACE_UP] = LoadIcon(RES_DIR "up.png"); - m_surfaces[T_SURFACE_CURSOR1] = SDL_utils::createImage(SCREEN_WIDTH / 2 * PPU_X, LINE_HEIGHT * PPU_Y, SDL_MapRGB(Globals::g_screen->format, COLOR_CURSOR_1)); - m_surfaces[T_SURFACE_CURSOR2] = SDL_utils::createImage(SCREEN_WIDTH / 2 * PPU_X, LINE_HEIGHT * PPU_Y, SDL_MapRGB(Globals::g_screen->format, COLOR_CURSOR_2)); + m_surfaces[T_SURFACE_CURSOR1] = SDL_utils::createImage(screen.w / 2 * screen.ppu_x, LINE_HEIGHT * screen.ppu_y, SDL_MapRGB(Globals::g_screen->format, COLOR_CURSOR_1)); + m_surfaces[T_SURFACE_CURSOR2] = SDL_utils::createImage(screen.w / 2 * screen.ppu_x, LINE_HEIGHT * screen.ppu_y, SDL_MapRGB(Globals::g_screen->format, COLOR_CURSOR_2)); // Load font m_font = SDL_utils::loadFont(RES_DIR "wy_scorpio.ttf", 8); } diff --git a/screen.cpp b/screen.cpp new file mode 100644 index 0000000..0f0d2b1 --- /dev/null +++ b/screen.cpp @@ -0,0 +1,3 @@ +#include "screen.h" + +Screen screen; diff --git a/screen.h b/screen.h new file mode 100644 index 0000000..ea35376 --- /dev/null +++ b/screen.h @@ -0,0 +1,23 @@ +#ifndef _SCREEN_H_ +#define _SCREEN_H_ + +#include +#include "def.h" + +struct Screen { + // Logical width and height. + decltype(SDL_Rect().w) w = SCREEN_WIDTH; + decltype(SDL_Rect().h) h = SCREEN_HEIGHT; + + // Scaling factors. + float ppu_x = PPU_X; + float ppu_y = PPU_Y; + + // Actual width and height. + decltype(SDL_Rect().w) actual_w = SCREEN_WIDTH * PPU_X; + decltype(SDL_Rect().h) actual_h = SCREEN_HEIGHT * PPU_Y; +}; + +extern Screen screen; + +#endif // _SCREEN_H_ diff --git a/sdlutils.cpp b/sdlutils.cpp index f8e093a..0b830ca 100755 --- a/sdlutils.cpp +++ b/sdlutils.cpp @@ -8,6 +8,7 @@ #include "def.h" #include "fileutils.h" #include "resourceManager.h" +#include "screen.h" extern SDL_Surface *ScreenSurface; @@ -35,8 +36,8 @@ SDL_Surface *SDL_utils::loadImageToFit(const std::string &p_filename, int fit_w, target_h = std::min(l_img->h, fit_h); target_w = target_h * aspect_ratio; } - target_w *= PPU_X; - target_h *= PPU_Y; + target_w *= screen.ppu_x; + target_h *= screen.ppu_y; SDL_Surface *l_img2 = zoomSurface(l_img, static_cast(target_w) / l_img->w, static_cast(target_h) / l_img->h, SMOOTHING_ON); SDL_FreeSurface(l_img); @@ -52,8 +53,8 @@ void SDL_utils::applySurface(const Sint16 p_x, const Sint16 p_y, SDL_Surface* p_ // Rectangle to hold the offsets SDL_Rect l_offset; // Set offsets - l_offset.x = p_x * PPU_X; - l_offset.y = p_y * PPU_Y; + l_offset.x = p_x * screen.ppu_x; + l_offset.y = p_y * screen.ppu_y; //Blit the surface SDL_BlitSurface(p_source, p_clip, p_destination, &l_offset); } @@ -61,7 +62,7 @@ void SDL_utils::applySurface(const Sint16 p_x, const Sint16 p_y, SDL_Surface* p_ TTF_Font *SDL_utils::loadFont(const std::string &p_font, const int p_size) { INHIBIT(std::cout << "SDL_utils::loadFont(" << p_font << ", " << p_size << ")" << std::endl;) - TTF_Font *l_font = TTF_OpenFontDPI(p_font.c_str(), p_size, 72 * PPU_X, 72 * PPU_Y); + TTF_Font *l_font = TTF_OpenFontDPI(p_font.c_str(), p_size, 72 * screen.ppu_x, 72 * screen.ppu_y); if (l_font == NULL) std::cerr << "SDL_utils::loadFont: " << SDL_GetError() << std::endl; return l_font; @@ -81,10 +82,10 @@ void SDL_utils::applyText(Sint16 p_x, Sint16 p_y, SDL_Surface* p_destination, TT applySurface(p_x, p_y, l_text, p_destination); break; case T_TEXT_ALIGN_RIGHT: - applySurface(p_x - l_text->w / PPU_X, p_y, l_text, p_destination); + applySurface(p_x - l_text->w / screen.ppu_x, p_y, l_text, p_destination); break; case T_TEXT_ALIGN_CENTER: - applySurface(p_x - l_text->w / 2 / PPU_X, p_y, l_text, p_destination); + applySurface(p_x - l_text->w / 2 / screen.ppu_x, p_y, l_text, p_destination); break; default: break; @@ -137,17 +138,17 @@ void SDL_utils::pleaseWait(void) { SDL_Surface *l_surfaceTmp = renderText(CResourceManager::instance().getFont(), "Please wait...", Globals::g_colorTextNormal, {COLOR_BG_1}); SDL_Rect l_rect = Rect( - (SCREEN_WIDTH * PPU_X - (l_surfaceTmp->w + (2 * DIALOG_MARGIN + 2 * DIALOG_BORDER) * PPU_X)) / 2, - (SCREEN_HEIGHT * PPU_Y - (l_surfaceTmp->h + 9)) / 2, - l_surfaceTmp->w + (2 * DIALOG_MARGIN + 2 * DIALOG_BORDER) * PPU_X, - l_surfaceTmp->h + 4 * PPU_Y); + (screen.w * screen.ppu_x - (l_surfaceTmp->w + (2 * DIALOG_MARGIN + 2 * DIALOG_BORDER) * screen.ppu_x)) / 2, + (screen.h * screen.ppu_y - (l_surfaceTmp->h + 9)) / 2, + l_surfaceTmp->w + (2 * DIALOG_MARGIN + 2 * DIALOG_BORDER) * screen.ppu_x, + l_surfaceTmp->h + 4 * screen.ppu_y); SDL_FillRect(Globals::g_screen, &l_rect, SDL_MapRGB(Globals::g_screen->format, COLOR_BORDER)); - l_rect.x += DIALOG_BORDER * PPU_X; - l_rect.y += DIALOG_BORDER * PPU_Y; - l_rect.w -= 2 * DIALOG_BORDER * PPU_X; - l_rect.h -= 2 * DIALOG_BORDER * PPU_Y; + l_rect.x += DIALOG_BORDER * screen.ppu_x; + l_rect.y += DIALOG_BORDER * screen.ppu_y; + l_rect.w -= 2 * DIALOG_BORDER * screen.ppu_x; + l_rect.h -= 2 * DIALOG_BORDER * screen.ppu_y; SDL_FillRect(Globals::g_screen, &l_rect, SDL_MapRGB(Globals::g_screen->format, COLOR_BG_1)); - applySurface((SCREEN_WIDTH - l_surfaceTmp->w / PPU_X) / 2, (SCREEN_HEIGHT - l_surfaceTmp->h / PPU_Y) / 2, l_surfaceTmp, Globals::g_screen); + applySurface((screen.w - l_surfaceTmp->w / screen.ppu_x) / 2, (screen.h - l_surfaceTmp->h / screen.ppu_y) / 2, l_surfaceTmp, Globals::g_screen); SDL_FreeSurface(l_surfaceTmp); //SDL_Flip(Globals::g_screen); //SDL_Flip(Globals::g_screen); diff --git a/viewer.cpp b/viewer.cpp index bde0154..7f8fa99 100755 --- a/viewer.cpp +++ b/viewer.cpp @@ -47,9 +47,9 @@ CViewer::CViewer(const std::string &p_fileName): m_image(nullptr) { // Create background image - m_background = SDL_utils::createImage(SCREEN_WIDTH * PPU_X, SCREEN_HEIGHT * PPU_Y, SDL_MapRGB(Globals::g_screen->format, COLOR_BG_1)); + m_background = SDL_utils::createImage(screen.w * screen.ppu_x, screen.h * screen.ppu_y, SDL_MapRGB(Globals::g_screen->format, COLOR_BG_1)); { - SDL_Rect l_rect = SDL_utils::Rect(0, 0, SCREEN_WIDTH * PPU_X, HEADER_H * PPU_Y); + SDL_Rect l_rect = SDL_utils::Rect(0, 0, screen.w * screen.ppu_x, HEADER_H * screen.ppu_y); SDL_FillRect(m_background, &l_rect, SDL_MapRGB(m_background->format, COLOR_BORDER)); } // Print title @@ -71,7 +71,7 @@ CViewer::CViewer(const std::string &p_fileName): SDL_FreeSurface(l_surfaceTmp); // Read file - m_image = SDL_utils::loadImageToFit(m_fileName, SCREEN_WIDTH, SCREEN_HEIGHT - Y_LIST); + m_image = SDL_utils::loadImageToFit(m_fileName, screen.w, screen.h - Y_LIST); if (m_image != nullptr) { m_mode = IMAGE; @@ -83,13 +83,13 @@ CViewer::CViewer(const std::string &p_fileName): SDL_MapRGB(m_background->format, 155, 155, 155), }; int i = 0; - for (int y = Y_LIST; y < SCREEN_HEIGHT; y += kTransparentBgRectSize, ++i) { - for (int x = 0; x < SCREEN_WIDTH; x += kTransparentBgRectSize, ++i) { + for (int y = Y_LIST; y < screen.h; y += kTransparentBgRectSize, ++i) { + for (int x = 0; x < screen.w; x += kTransparentBgRectSize, ++i) { SDL_Rect rect = { - static_cast(x * PPU_X), - static_cast(y * PPU_Y), - static_cast(kTransparentBgRectSize * PPU_X), - static_cast(kTransparentBgRectSize * PPU_Y)}; + static_cast(x * screen.ppu_x), + static_cast(y * screen.ppu_y), + static_cast(kTransparentBgRectSize * screen.ppu_x), + static_cast(kTransparentBgRectSize * screen.ppu_y)}; SDL_FillRect(m_background, &rect, colors[i % 2]); } } @@ -101,7 +101,7 @@ CViewer::CViewer(const std::string &p_fileName): // Init clip rect m_clip.x = 0; m_clip.y = 0; - m_clip.w = (SCREEN_WIDTH - 2 * VIEWER_MARGIN) * PPU_X; + m_clip.w = (screen.w - 2 * VIEWER_MARGIN) * screen.ppu_x; std::ifstream l_file(m_fileName.c_str()); @@ -137,7 +137,7 @@ void CViewer::render(const bool p_focus) const SDL_utils::applySurface(0, 0, m_background, Globals::g_screen); if (m_mode == IMAGE) { - SDL_utils::applySurface((SCREEN_WIDTH - m_image->w / PPU_X) / 2, Y_LIST + (SCREEN_HEIGHT - Y_LIST - m_image->h / PPU_Y) / 2, m_image, Globals::g_screen); + SDL_utils::applySurface((screen.w - m_image->w / screen.ppu_x) / 2, Y_LIST + (screen.h - Y_LIST - m_image->h / screen.ppu_y) / 2, m_image, Globals::g_screen); } else if (m_mode == TEXT) { @@ -266,8 +266,8 @@ bool CViewer::moveLeft(void) bool l_ret(false); if (m_clip.x > 0) { - if (m_clip.x > VIEWER_X_STEP * PPU_X) - m_clip.x -= VIEWER_X_STEP * PPU_X; + if (m_clip.x > VIEWER_X_STEP * screen.ppu_x) + m_clip.x -= VIEWER_X_STEP * screen.ppu_x; else m_clip.x = 0; l_ret = true; @@ -277,6 +277,6 @@ bool CViewer::moveLeft(void) bool CViewer::moveRight(void) { - m_clip.x += VIEWER_X_STEP * PPU_X; + m_clip.x += VIEWER_X_STEP * screen.ppu_x; return true; } diff --git a/viewer.h b/viewer.h index 479ee5d..a59b5e0 100755 --- a/viewer.h +++ b/viewer.h @@ -7,11 +7,12 @@ #include #include +#include "screen.h" #include "window.h" #define VIEWER_LINE_HEIGHT 13 #define VIEWER_Y_LIST 18 -#define VIEWER_NB_LINES ((SCREEN_HEIGHT - VIEWER_Y_LIST - 1) / VIEWER_LINE_HEIGHT + 1) +#define VIEWER_NB_LINES ((screen.h - VIEWER_Y_LIST - 1) / VIEWER_LINE_HEIGHT + 1) #define VIEWER_MARGIN 1 #define VIEWER_X_STEP 32 #define VIEWER_SIZE_MAX 16777216 // = 16 MB