1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #include "ags/shared/ac/common.h"
24 #include "ags/engine/ac/draw.h"
25 #include "ags/shared/ac/game_setup_struct.h"
26 #include "ags/engine/ac/game_state.h"
27 #include "ags/engine/ac/global_game.h"
28 #include "ags/engine/ac/global_screen.h"
29 #include "ags/engine/ac/screen.h"
30 #include "ags/engine/ac/dynobj/script_viewport.h"
31 #include "ags/engine/ac/dynobj/script_user_object.h"
32 #include "ags/engine/script/script_runtime.h"
33 #include "ags/engine/platform/base/ags_platform_driver.h"
34 #include "ags/plugins/ags_plugin.h"
35 #include "ags/plugins/plugin_engine.h"
36 #include "ags/shared/gfx/bitmap.h"
37 #include "ags/engine/gfx/graphics_driver.h"
38 #include "ags/globals.h"
39
40 namespace AGS3 {
41
42 using namespace AGS::Shared;
43 using namespace AGS::Engine;
44
my_fade_in(PALETTE p,int speed)45 void my_fade_in(PALETTE p, int speed) {
46 if (_GP(game).color_depth > 1) {
47 set_palette(p);
48
49 _GP(play).screen_is_faded_out = 0;
50
51 if (_GP(play).no_hicolor_fadein) {
52 return;
53 }
54 }
55
56 _G(gfxDriver)->FadeIn(speed, p, _GP(play).fade_to_red, _GP(play).fade_to_green, _GP(play).fade_to_blue);
57 }
58
current_fade_out_effect()59 void current_fade_out_effect() {
60 if (pl_run_plugin_hooks(AGSE_TRANSITIONOUT, 0))
61 return;
62
63 // get the screen transition type
64 int theTransition = _GP(play).fade_effect;
65 // was a temporary transition selected? if so, use it
66 if (_GP(play).next_screen_transition >= 0)
67 theTransition = _GP(play).next_screen_transition;
68 const bool ignore_transition = _GP(play).screen_tint > 0;
69
70 if ((theTransition == FADE_INSTANT) || ignore_transition) {
71 if (!_GP(play).keep_screen_during_instant_transition)
72 set_palette_range(_G(black_palette), 0, 255, 0);
73 } else if (theTransition == FADE_NORMAL) {
74 my_fade_out(5);
75 } else if (theTransition == FADE_BOXOUT) {
76 _G(gfxDriver)->BoxOutEffect(true, get_fixed_pixel_size(16), 1000 / GetGameSpeed());
77 _GP(play).screen_is_faded_out = 1;
78 } else {
79 get_palette(_G(old_palette));
80 const Rect &viewport = _GP(play).GetMainViewport();
81 _G(saved_viewport_bitmap) = CopyScreenIntoBitmap(viewport.GetWidth(), viewport.GetHeight());
82 }
83 }
84
prepare_screen_for_transition_in()85 IDriverDependantBitmap *prepare_screen_for_transition_in() {
86 if (_G(saved_viewport_bitmap) == nullptr)
87 quit("Crossfade: buffer is null attempting transition");
88
89 _G(saved_viewport_bitmap) = ReplaceBitmapWithSupportedFormat(_G(saved_viewport_bitmap));
90 const Rect &viewport = _GP(play).GetMainViewport();
91 if (_G(saved_viewport_bitmap)->GetHeight() < viewport.GetHeight()) {
92 Bitmap *enlargedBuffer = BitmapHelper::CreateBitmap(_G(saved_viewport_bitmap)->GetWidth(), viewport.GetHeight(), _G(saved_viewport_bitmap)->GetColorDepth());
93 enlargedBuffer->Blit(_G(saved_viewport_bitmap), 0, 0, 0, (viewport.GetHeight() - _G(saved_viewport_bitmap)->GetHeight()) / 2, _G(saved_viewport_bitmap)->GetWidth(), _G(saved_viewport_bitmap)->GetHeight());
94 delete _G(saved_viewport_bitmap);
95 _G(saved_viewport_bitmap) = enlargedBuffer;
96 } else if (_G(saved_viewport_bitmap)->GetHeight() > viewport.GetHeight()) {
97 Bitmap *clippedBuffer = BitmapHelper::CreateBitmap(_G(saved_viewport_bitmap)->GetWidth(), viewport.GetHeight(), _G(saved_viewport_bitmap)->GetColorDepth());
98 clippedBuffer->Blit(_G(saved_viewport_bitmap), 0, (_G(saved_viewport_bitmap)->GetHeight() - viewport.GetHeight()) / 2, 0, 0, _G(saved_viewport_bitmap)->GetWidth(), _G(saved_viewport_bitmap)->GetHeight());
99 delete _G(saved_viewport_bitmap);
100 _G(saved_viewport_bitmap) = clippedBuffer;
101 }
102 IDriverDependantBitmap *ddb = _G(gfxDriver)->CreateDDBFromBitmap(_G(saved_viewport_bitmap), false);
103 return ddb;
104 }
105
106 //=============================================================================
107 //
108 // Screen script API.
109 //
110 //=============================================================================
111
Screen_GetScreenWidth()112 int Screen_GetScreenWidth() {
113 return _GP(game).GetGameRes().Width;
114 }
115
Screen_GetScreenHeight()116 int Screen_GetScreenHeight() {
117 return _GP(game).GetGameRes().Height;
118 }
119
Screen_GetAutoSizeViewport()120 bool Screen_GetAutoSizeViewport() {
121 return _GP(play).IsAutoRoomViewport();
122 }
123
Screen_SetAutoSizeViewport(bool on)124 void Screen_SetAutoSizeViewport(bool on) {
125 _GP(play).SetAutoRoomViewport(on);
126 }
127
Screen_GetViewport()128 ScriptViewport *Screen_GetViewport() {
129 return _GP(play).GetScriptViewport(0);
130 }
131
Screen_GetViewportCount()132 int Screen_GetViewportCount() {
133 return _GP(play).GetRoomViewportCount();
134 }
135
Screen_GetAnyViewport(int index)136 ScriptViewport *Screen_GetAnyViewport(int index) {
137 return _GP(play).GetScriptViewport(index);
138 }
139
Screen_ScreenToRoomPoint(int scrx,int scry)140 ScriptUserObject *Screen_ScreenToRoomPoint(int scrx, int scry) {
141 data_to_game_coords(&scrx, &scry);
142
143 VpPoint vpt = _GP(play).ScreenToRoom(scrx, scry);
144 if (vpt.second < 0)
145 return nullptr;
146
147 game_to_data_coords(vpt.first.X, vpt.first.Y);
148 return ScriptStructHelpers::CreatePoint(vpt.first.X, vpt.first.Y);
149 }
150
Screen_RoomToScreenPoint(int roomx,int roomy)151 ScriptUserObject *Screen_RoomToScreenPoint(int roomx, int roomy) {
152 data_to_game_coords(&roomx, &roomy);
153 Point pt = _GP(play).RoomToScreen(roomx, roomy);
154 game_to_data_coords(pt.X, pt.Y);
155 return ScriptStructHelpers::CreatePoint(pt.X, pt.Y);
156 }
157
Sc_Screen_GetScreenHeight(const RuntimeScriptValue * params,int32_t param_count)158 RuntimeScriptValue Sc_Screen_GetScreenHeight(const RuntimeScriptValue *params, int32_t param_count) {
159 API_SCALL_INT(Screen_GetScreenHeight);
160 }
161
Sc_Screen_GetScreenWidth(const RuntimeScriptValue * params,int32_t param_count)162 RuntimeScriptValue Sc_Screen_GetScreenWidth(const RuntimeScriptValue *params, int32_t param_count) {
163 API_SCALL_INT(Screen_GetScreenWidth);
164 }
165
Sc_Screen_GetAutoSizeViewport(const RuntimeScriptValue * params,int32_t param_count)166 RuntimeScriptValue Sc_Screen_GetAutoSizeViewport(const RuntimeScriptValue *params, int32_t param_count) {
167 API_SCALL_BOOL(Screen_GetAutoSizeViewport);
168 }
169
Sc_Screen_SetAutoSizeViewport(const RuntimeScriptValue * params,int32_t param_count)170 RuntimeScriptValue Sc_Screen_SetAutoSizeViewport(const RuntimeScriptValue *params, int32_t param_count) {
171 API_SCALL_VOID_PBOOL(Screen_SetAutoSizeViewport);
172 }
173
Sc_Screen_GetViewport(const RuntimeScriptValue * params,int32_t param_count)174 RuntimeScriptValue Sc_Screen_GetViewport(const RuntimeScriptValue *params, int32_t param_count) {
175 API_SCALL_OBJAUTO(ScriptViewport, Screen_GetViewport);
176 }
177
Sc_Screen_GetViewportCount(const RuntimeScriptValue * params,int32_t param_count)178 RuntimeScriptValue Sc_Screen_GetViewportCount(const RuntimeScriptValue *params, int32_t param_count) {
179 API_SCALL_INT(Screen_GetViewportCount);
180 }
181
Sc_Screen_GetAnyViewport(const RuntimeScriptValue * params,int32_t param_count)182 RuntimeScriptValue Sc_Screen_GetAnyViewport(const RuntimeScriptValue *params, int32_t param_count) {
183 API_SCALL_OBJAUTO_PINT(ScriptViewport, Screen_GetAnyViewport);
184 }
185
Sc_Screen_ScreenToRoomPoint(const RuntimeScriptValue * params,int32_t param_count)186 RuntimeScriptValue Sc_Screen_ScreenToRoomPoint(const RuntimeScriptValue *params, int32_t param_count) {
187 API_SCALL_OBJAUTO_PINT2(ScriptUserObject, Screen_ScreenToRoomPoint);
188 }
189
Sc_Screen_RoomToScreenPoint(const RuntimeScriptValue * params,int32_t param_count)190 RuntimeScriptValue Sc_Screen_RoomToScreenPoint(const RuntimeScriptValue *params, int32_t param_count) {
191 API_SCALL_OBJAUTO_PINT2(ScriptUserObject, Screen_RoomToScreenPoint);
192 }
193
RegisterScreenAPI()194 void RegisterScreenAPI() {
195 ccAddExternalStaticFunction("Screen::get_Height", Sc_Screen_GetScreenHeight);
196 ccAddExternalStaticFunction("Screen::get_Width", Sc_Screen_GetScreenWidth);
197 ccAddExternalStaticFunction("Screen::get_AutoSizeViewportOnRoomLoad", Sc_Screen_GetAutoSizeViewport);
198 ccAddExternalStaticFunction("Screen::set_AutoSizeViewportOnRoomLoad", Sc_Screen_SetAutoSizeViewport);
199 ccAddExternalStaticFunction("Screen::get_Viewport", Sc_Screen_GetViewport);
200 ccAddExternalStaticFunction("Screen::get_ViewportCount", Sc_Screen_GetViewportCount);
201 ccAddExternalStaticFunction("Screen::geti_Viewports", Sc_Screen_GetAnyViewport);
202 ccAddExternalStaticFunction("Screen::ScreenToRoomPoint", Sc_Screen_ScreenToRoomPoint);
203 ccAddExternalStaticFunction("Screen::RoomToScreenPoint", Sc_Screen_RoomToScreenPoint);
204 }
205
206 } // namespace AGS3
207