1 /*
2  * Copyright (C) 2002-2020 by the Widelands Development Team
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  *
18  */
19 
20 #ifndef WL_GRAPHIC_GRAPHIC_H
21 #define WL_GRAPHIC_GRAPHIC_H
22 
23 #include <memory>
24 
25 #include <SDL_video.h>
26 
27 #include "graphic/image_cache.h"
28 #include "graphic/style_manager.h"
29 #include "notifications/note_ids.h"
30 #include "notifications/notifications.h"
31 
32 class AnimationManager;
33 class RenderTarget;
34 class Screen;
35 
36 // A graphics card must at least support this size for texture for Widelands to
37 // run.
38 constexpr int kMinimumSizeForTextures = 2048;
39 
40 // Will be send whenever the resolution changes.
41 struct GraphicResolutionChanged {
42 	CAN_BE_SENT_AS_NOTE(NoteId::GraphicResolutionChanged)
43 
44 	// Old width and height in pixels.
45 	int old_width;
46 	int old_height;
47 
48 	// New width and height in pixels.
49 	int new_width;
50 	int new_height;
51 };
52 
53 /**
54  * This class is a kind of Swiss Army knife for your graphics need.
55  * It initializes the graphic system and provides access to
56  * resolutions. It owns an Animation, Image and Surface cache. It
57  * also offers functionality to save a screenshot.
58  */
59 class Graphic {
60 public:
61 	// Creates a new Graphic object. Must call initialize before first use.
62 	Graphic();
63 	~Graphic();
64 
65 	// Initializes with the given resolution if fullscreen is false, otherwise a
66 	// window that fills the screen. The 'trace_gl' parameter gets passed on to
67 	// 'Gl::initialize'.
68 	enum class TraceGl { kNo, kYes };
69 	void
70 	initialize(const TraceGl& trace_gl, int window_mode_w, int window_mode_height, bool fullscreen);
71 
72 	// Gets and sets the resolution.
73 	void change_resolution(int w, int h);
74 	int get_xres();
75 	int get_yres();
76 
77 	// Changes the window to be fullscreen or not.
78 	bool fullscreen();
79 	void set_fullscreen(bool);
80 
81 	RenderTarget* get_render_target();
82 	void refresh();
get_sdlwindow()83 	SDL_Window* get_sdlwindow() {
84 		return sdl_window_;
85 	}
86 
87 	int max_texture_size_for_font_rendering() const;
88 
images()89 	ImageCache& images() const {
90 		return *image_cache_.get();
91 	}
animations()92 	AnimationManager& animations() const {
93 		return *animation_manager_.get();
94 	}
styles()95 	StyleManager& styles() const {
96 		return *style_manager_.get();
97 	}
98 
99 	// Requests a screenshot being taken on the next frame.
100 	void screenshot(const std::string& fname);
101 
102 private:
103 	// Called when the resolution (might) have changed.
104 	void resolution_changed(int old_w = 0, int old_h = 0);
105 
106 	// The height & width of the window should we be in window mode.
107 	int window_mode_width_ = 0;
108 	int window_mode_height_ = 0;
109 
110 	/// This is the main screen Surface.
111 	/// A RenderTarget for this can be retrieved with get_render_target()
112 	std::unique_ptr<Screen> screen_;
113 
114 	/// This saves a copy of the screen SDL_Surface. This is needed for
115 	/// opengl rendering as the SurfaceOpenGL does not use it. It allows
116 	/// manipulation the screen context.
117 	SDL_Window* sdl_window_ = nullptr;
118 	SDL_GLContext gl_context_;
119 
120 	/// The maximum width or height a texture can have.
121 	int max_texture_size_ = kMinimumSizeForTextures;
122 
123 	/// A RenderTarget for screen_. This is initialized during init()
124 	std::unique_ptr<RenderTarget> render_target_;
125 
126 	/// Non-volatile cache of independent images.
127 	std::unique_ptr<ImageCache> image_cache_;
128 
129 	/// This holds all animations.
130 	std::unique_ptr<AnimationManager> animation_manager_;
131 
132 	/// This holds all GUI styles.
133 	std::unique_ptr<StyleManager> style_manager_;
134 
135 	/// Screenshot filename. If a screenshot is requested, this will be set to
136 	/// the requested filename. On the next frame the screenshot will be written
137 	/// out and this will be clear()ed again.
138 	std::string screenshot_filename_;
139 };
140 
141 extern Graphic* g_gr;
142 
143 #endif  // end of include guard: WL_GRAPHIC_GRAPHIC_H
144