1 /*
2 * This file is part of EasyRPG Player.
3 *
4 * EasyRPG Player is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * EasyRPG Player 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 EasyRPG Player. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 // Headers
19 #include <memory>
20 #include <sstream>
21 #include <chrono>
22
23 #include "graphics.h"
24 #include "cache.h"
25 #include "player.h"
26 #include "fps_overlay.h"
27 #include "message_overlay.h"
28 #include "transition.h"
29 #include "scene.h"
30 #include "drawable_mgr.h"
31 #include "baseui.h"
32 #include "game_clock.h"
33
34 using namespace std::chrono_literals;
35
36 namespace Graphics {
37 void UpdateTitle();
38
39 std::shared_ptr<Scene> current_scene;
40
41 std::unique_ptr<MessageOverlay> message_overlay;
42 std::unique_ptr<FpsOverlay> fps_overlay;
43
44 std::string window_title_key;
45 }
46
Init()47 void Graphics::Init() {
48 Scene::Push(std::make_shared<Scene>());
49 UpdateSceneCallback();
50
51 message_overlay = std::make_unique<MessageOverlay>();
52 fps_overlay = std::make_unique<FpsOverlay>();
53 }
54
Quit()55 void Graphics::Quit() {
56 fps_overlay.reset();
57 message_overlay.reset();
58
59 Cache::Clear();
60
61 Scene::PopUntil(Scene::Null);
62 Scene::Pop();
63 }
64
Update()65 void Graphics::Update() {
66 BitmapRef disp = DisplayUi->GetDisplaySurface();
67 fps_overlay->SetDrawFps(DisplayUi->RenderFps());
68
69 //Update Graphics:
70 if (fps_overlay->Update()) {
71 UpdateTitle();
72 }
73 message_overlay->Update();
74 }
75
UpdateTitle()76 void Graphics::UpdateTitle() {
77 if (DisplayUi->IsFullscreen()) {
78 return;
79 }
80
81 #ifdef EMSCRIPTEN
82 return;
83 #else
84 std::string fps;
85 if (DisplayUi->ShowFpsOnTitle()) {
86 fps += fps_overlay->GetFpsString();
87 }
88
89 if (window_title_key == (Player::game_title + fps)) {
90 return;
91 }
92
93 std::stringstream title;
94 if (!Player::game_title.empty()) {
95 title << Player::game_title << " - ";
96 }
97 title << GAME_TITLE;
98
99 if (DisplayUi->ShowFpsOnTitle()) {
100 title << " - " << fps;
101 }
102
103 DisplayUi->SetTitle(title.str());
104
105 window_title_key = (Player::game_title + fps);
106 #endif
107 }
108
Draw(Bitmap & dst)109 void Graphics::Draw(Bitmap& dst) {
110 auto& transition = Transition::instance();
111
112 int min_z = std::numeric_limits<int>::min();
113 int max_z = std::numeric_limits<int>::max();
114 if (transition.IsActive()) {
115 min_z = transition.GetZ();
116 } else if (transition.IsErasedNotActive()) {
117 min_z = transition.GetZ() + 1;
118 dst.Clear();
119 }
120 LocalDraw(dst, min_z, max_z);
121 }
122
LocalDraw(Bitmap & dst,int min_z,int max_z)123 void Graphics::LocalDraw(Bitmap& dst, int min_z, int max_z) {
124 auto& drawable_list = DrawableMgr::GetLocalList();
125
126 if (!drawable_list.empty() && min_z == std::numeric_limits<int>::min()) {
127 current_scene->DrawBackground(dst);
128 }
129
130 drawable_list.Draw(dst, min_z, max_z);
131 }
132
UpdateSceneCallback()133 std::shared_ptr<Scene> Graphics::UpdateSceneCallback() {
134 auto prev_scene = current_scene;
135 current_scene = Scene::instance;
136
137 if (current_scene) {
138 if (prev_scene) {
139 prev_scene->Suspend(current_scene->type);
140 current_scene->TransferDrawablesFrom(*prev_scene);
141 }
142 DrawableMgr::SetLocalList(¤t_scene->GetDrawableList());
143 } else {
144 DrawableMgr::SetLocalList(nullptr);
145 }
146
147 return prev_scene;
148 }
149
GetMessageOverlay()150 MessageOverlay& Graphics::GetMessageOverlay() {
151 return *message_overlay;
152 }
153
154