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 <string>
20 #include <lcf/data.h>
21 #include <lcf/rpg/terrain.h>
22 #include "cache.h"
23 #include "background.h"
24 #include "bitmap.h"
25 #include "main_data.h"
26 #include <lcf/reader_util.h>
27 #include "output.h"
28 #include "drawable_mgr.h"
29 #include "game_screen.h"
30
Background(const std::string & name)31 Background::Background(const std::string& name) : Drawable(Priority_Background)
32 {
33 DrawableMgr::Register(this);
34
35 if (!name.empty()) {
36 FileRequestAsync* request = AsyncHandler::RequestFile("Backdrop", name);
37 request->SetGraphicFile(true);
38 request_id = request->Bind(&Background::OnBackgroundGraphicReady, this);
39 request->Start();
40 }
41 }
42
Background(int terrain_id)43 Background::Background(int terrain_id) : Drawable(Priority_Background)
44 {
45 DrawableMgr::Register(this);
46
47 const lcf::rpg::Terrain* terrain = lcf::ReaderUtil::GetElement(lcf::Data::terrains, terrain_id);
48
49 if (!terrain) {
50 Output::Warning("Background: Invalid terrain ID {}", terrain_id);
51 return;
52 }
53
54 // Either background or frame
55 if (terrain->background_type == lcf::rpg::Terrain::BGAssociation_background && !terrain->background_name.empty()) {
56 FileRequestAsync* request = AsyncHandler::RequestFile("Backdrop", terrain->background_name);
57 request->SetGraphicFile(true);
58 request_id = request->Bind(&Background::OnBackgroundGraphicReady, this);
59 request->Start();
60 return;
61 }
62
63 // Frame
64 if (!terrain->background_a_name.empty()) {
65 FileRequestAsync* request = AsyncHandler::RequestFile("Frame", terrain->background_a_name);
66 request->SetGraphicFile(true);
67 request_id = request->Bind(&Background::OnBackgroundGraphicReady, this);
68 request->Start();
69
70 bg_hscroll = terrain->background_a_scrollh ? terrain->background_a_scrollh_speed : 0;
71 bg_vscroll = terrain->background_a_scrollv ? terrain->background_a_scrollv_speed : 0;
72 }
73
74 if (terrain->background_b && !terrain->background_b_name.empty()) {
75 FileRequestAsync* request = AsyncHandler::RequestFile("Frame", terrain->background_b_name);
76 request->SetGraphicFile(true);
77 request_id = request->Bind(&Background::OnForegroundFrameGraphicReady, this);
78 request->Start();
79
80 fg_hscroll = terrain->background_b_scrollh ? terrain->background_b_scrollh_speed : 0;
81 fg_vscroll = terrain->background_b_scrollv ? terrain->background_b_scrollv_speed : 0;
82 }
83 }
84
OnBackgroundGraphicReady(FileRequestResult * result)85 void Background::OnBackgroundGraphicReady(FileRequestResult* result) {
86 if (result->directory == "Backdrop") {
87 bg_bitmap = Cache::Backdrop(result->file);
88 }
89 else if (result->directory == "Frame") {
90 bg_bitmap = Cache::Frame(result->file, false);
91 }
92 }
93
OnForegroundFrameGraphicReady(FileRequestResult * result)94 void Background::OnForegroundFrameGraphicReady(FileRequestResult* result) {
95 fg_bitmap = Cache::Frame(result->file);
96 }
97
Update(int & rate,int & value)98 void Background::Update(int& rate, int& value) {
99 int step =
100 (rate > 0) ? 2 << rate :
101 (rate < 0) ? 2 << -rate :
102 0;
103 value += step;
104 }
105
Update()106 void Background::Update() {
107 Update(bg_hscroll, bg_x);
108 Update(bg_vscroll, bg_y);
109 Update(fg_hscroll, fg_x);
110 Update(fg_vscroll, fg_y);
111 }
112
Scale(int x)113 int Background::Scale(int x) {
114 return x > 0 ? x / 64 : -(-x / 64);
115 }
116
Draw(Bitmap & dst)117 void Background::Draw(Bitmap& dst) {
118 Rect dst_rect = dst.GetRect();
119
120 dst_rect.x += Main_Data::game_screen->GetShakeOffsetX();
121 dst_rect.y += Main_Data::game_screen->GetShakeOffsetY();
122
123 if (bg_bitmap)
124 dst.TiledBlit(-Scale(bg_x), -Scale(bg_y), bg_bitmap->GetRect(), *bg_bitmap, dst_rect, 255);
125
126 if (fg_bitmap)
127 dst.TiledBlit(-Scale(fg_x), -Scale(fg_y), fg_bitmap->GetRect(), *fg_bitmap, dst_rect, 255);
128
129 if (tone_effect != Tone()) {
130 dst.ToneBlit(0, 0, dst, dst.GetRect(), tone_effect, Opacity::Opaque());
131 }
132 }
133