@@ -21,6 +21,59 @@ void ImageViewer::init()
21
21
filename_, screen.w , screen.h ) };
22
22
ok_ = (image_ != nullptr );
23
23
if (!ok_) return ;
24
+
25
+ const auto &fonts = CResourceManager::instance ().getFonts ();
26
+ bool show_title = image_->h <= screen.actual_h - Y_LIST * screen.ppu_y ;
27
+
28
+ // Create background image
29
+ background_ = SDLSurfaceUniquePtr { SDL_utils::createImage (screen.actual_w ,
30
+ screen.actual_h , SDL_MapRGB (screen.surface ->format , COLOR_BG_1)) };
31
+ if (show_title) {
32
+ SDL_Rect rect = SDL_utils::Rect (
33
+ 0 , 0 , screen.actual_w , HEADER_H * screen.ppu_y );
34
+ SDL_FillRect (background_.get (), &rect,
35
+ SDL_MapRGB (background_->format , COLOR_BORDER));
36
+ }
37
+ // Print title
38
+ if (show_title) {
39
+ SDLSurfaceUniquePtr tmp { SDL_utils::renderText (
40
+ fonts, filename_, Globals::g_colorTextTitle, { COLOR_TITLE_BG }) };
41
+ if (tmp->w > background_->w - 2 * VIEWER_MARGIN) {
42
+ SDL_Rect rect;
43
+ rect.x = tmp->w - (background_->w - 2 * VIEWER_MARGIN);
44
+ rect.y = 0 ;
45
+ rect.w = background_->w - 2 * VIEWER_MARGIN;
46
+ rect.h = tmp->h ;
47
+ SDL_utils::applyPpuScaledSurface (VIEWER_MARGIN * screen.ppu_x ,
48
+ HEADER_PADDING_TOP * screen.ppu_y , tmp.get (), background_.get (),
49
+ &rect);
50
+ } else {
51
+ SDL_utils::applyPpuScaledSurface (VIEWER_MARGIN * screen.ppu_x ,
52
+ HEADER_PADDING_TOP * screen.ppu_y , tmp.get (),
53
+ background_.get ());
54
+ }
55
+ }
56
+
57
+ // Transparency grid background.
58
+ constexpr int kTransparentBgRectSize = 10 ;
59
+ const Uint32 colors[2 ] = {
60
+ SDL_MapRGB (background_->format , COLOR_BG_1),
61
+ SDL_MapRGB (background_->format , COLOR_BG_2),
62
+ };
63
+ int j = 0 ;
64
+ const int rect_w = static_cast <int >(kTransparentBgRectSize * screen.ppu_x );
65
+ const int rect_h = static_cast <int >(kTransparentBgRectSize * screen.ppu_y );
66
+ for (int j = 0 , y = show_title ? Y_LIST * screen.ppu_y : 0 ; y < screen.actual_h ; y += rect_h, ++j) {
67
+ for (int i = 0 , x = 0 ; x < screen.actual_w ; x += rect_w, ++i) {
68
+ SDL_Rect rect = SDL_utils::makeRect (x, y, rect_w, rect_h);
69
+ SDL_FillRect (background_.get (), &rect, colors[(i + j) % 2 ]);
70
+ }
71
+ }
72
+
73
+ SDL_Rect frame_left_rect = SDL_utils::makeRect (screen.actual_w - 1 , 0 , 1 , screen.actual_h );
74
+ SDL_Rect frame_bottom_rect = SDL_utils::makeRect (0 , screen.actual_h - 1 , screen.actual_w , 1 );
75
+ SDL_FillRect (background_.get (), &frame_left_rect, 0 );
76
+ SDL_FillRect (background_.get (), &frame_bottom_rect, 0 );
24
77
}
25
78
26
79
void ImageViewer::onResize ()
@@ -31,8 +84,14 @@ void ImageViewer::onResize()
31
84
32
85
void ImageViewer::render (const bool focused) const
33
86
{
34
- SDL_FillRect (screen.surface , NULL , 0 );
35
- SDL_utils::applyPpuScaledSurface (0 , 0 , image_.get (), screen.surface );
87
+ SDL_utils::applyPpuScaledSurface (0 , 0 , background_.get (), screen.surface );
88
+ int pos_y = image_->h <= screen.actual_h - Y_LIST * screen.ppu_y ?
89
+ (Y_LIST * screen.ppu_y
90
+ + (screen.actual_h - Y_LIST * screen.ppu_y - image_->h ) / 2 )
91
+ : (screen.actual_h - image_->h ) / 2 ;
92
+ SDL_utils::applyPpuScaledSurface ((screen.actual_w - image_->w ) / 2 ,
93
+ pos_y,
94
+ image_.get (), screen.surface );
36
95
}
37
96
38
97
// Key press management
0 commit comments