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 "common/config-manager.h"
24 #include "ags/shared/ac/common.h"
25 #include "ags/engine/ac/draw.h"
26 #include "ags/engine/ac/dynobj/cc_audio_channel.h"
27 #include "ags/engine/ac/game_setup.h"
28 #include "ags/shared/ac/game_setup_struct.h"
29 #include "ags/engine/ac/game_state.h"
30 #include "ags/engine/ac/global_debug.h"
31 #include "ags/engine/ac/global_translation.h"
32 #include "ags/engine/ac/mouse.h"
33 #include "ags/engine/ac/string.h"
34 #include "ags/engine/ac/system.h"
35 #include "ags/engine/ac/dynobj/script_system.h"
36 #include "ags/engine/debugging/debug_log.h"
37 #include "ags/shared/debugging/out.h"
38 #include "ags/engine/gfx/graphics_driver.h"
39 #include "ags/engine/main/config.h"
40 #include "ags/engine/main/graphics_mode.h"
41 #include "ags/engine/main/engine.h"
42 #include "ags/engine/main/main.h"
43 #include "ags/engine/media/audio/audio_system.h"
44 #include "ags/shared/util/string_compat.h"
45 #include "ags/shared/debugging/out.h"
46 #include "ags/engine/script/script_api.h"
47 #include "ags/engine/script/script_runtime.h"
48 #include "ags/engine/ac/dynobj/script_string.h"
49 #include "ags/ags.h"
50 #include "ags/globals.h"
51 #include "ags/events.h"
52
53 namespace AGS3 {
54
55 using namespace AGS::Shared;
56
System_HasInputFocus()57 bool System_HasInputFocus() {
58 return !_G(switched_away);
59 }
60
System_GetColorDepth()61 int System_GetColorDepth() {
62 return _GP(scsystem).coldepth;
63 }
64
System_GetOS()65 int System_GetOS() {
66 return _GP(scsystem).os;
67 }
68
69 // [IKM] 2014-09-21
70 // IMPORTANT NOTE on System.ScreenWidth and System.ScreenHeight:
71 // It appears that in AGS these properties were not defining actual window size
72 // in pixels, but rather game frame size, which could include black borders,
73 // in 'native' (unscaled) pixels. This was due the specifics of how graphics
74 // modes were implemented in previous versions.
75 //
76 // Quote from the old manual:
77 // "Returns the actual screen width that the game is running at. If a graphic
78 // filter is in use, the resolution returned will be that before any
79 // stretching by the filter has been applied. If widescreen side borders are
80 // enabled, the screen width reported will include the size of these borders."
81 //
82 // The key words are "the resolution returned will be that BEFORE any
83 // stretching by the filter has been applied".
84 //
85 // Since now the letterbox and pillarbox borders are handled by graphics
86 // renderer and are not part of the game anymore, these properties should
87 // return strictly native game size. This is required for backwards
88 // compatibility.
89 //
System_GetScreenWidth()90 int System_GetScreenWidth() {
91 return _GP(game).GetGameRes().Width;
92 }
93
System_GetScreenHeight()94 int System_GetScreenHeight() {
95 return _GP(game).GetGameRes().Height;
96 }
97
System_GetViewportHeight()98 int System_GetViewportHeight() {
99 return game_to_data_coord(_GP(play).GetMainViewport().GetHeight());
100 }
101
System_GetViewportWidth()102 int System_GetViewportWidth() {
103 return game_to_data_coord(_GP(play).GetMainViewport().GetWidth());
104 }
105
System_GetVersion()106 const char *System_GetVersion() {
107 return CreateNewScriptString(_G(EngineVersion).LongString.GetCStr());
108 }
109
System_GetHardwareAcceleration()110 int System_GetHardwareAcceleration() {
111 return _G(gfxDriver)->HasAcceleratedTransform() ? 1 : 0;
112 }
113
System_GetNumLock()114 int System_GetNumLock() {
115 return (::AGS::g_events->getModifierFlags() & Common::KBD_NUM) ? 1 : 0;
116 }
117
System_GetCapsLock()118 int System_GetCapsLock() {
119 return (::AGS::g_events->getModifierFlags() & Common::KBD_CAPS) ? 1 : 0;
120 }
121
System_GetScrollLock()122 int System_GetScrollLock() {
123 return (::AGS::g_events->getModifierFlags() & Common::KBD_SCRL) ? 1 : 0;
124 }
125
System_GetVsync()126 int System_GetVsync() {
127 return _GP(scsystem).vsync;
128 }
129
System_SetVsync(int newValue)130 void System_SetVsync(int newValue) {
131 if (ags_stricmp(_G(gfxDriver)->GetDriverID(), "D3D9") != 0)
132 _GP(scsystem).vsync = newValue;
133 }
134
System_GetWindowed()135 int System_GetWindowed() {
136 return _GP(scsystem).windowed;
137 }
138
System_SetWindowed(int windowed)139 void System_SetWindowed(int windowed) {
140 if (windowed != _GP(scsystem).windowed)
141 engine_try_switch_windowed_gfxmode();
142 }
143
System_GetSupportsGammaControl()144 int System_GetSupportsGammaControl() {
145 return _G(gfxDriver)->SupportsGammaControl();
146 }
147
System_GetGamma()148 int System_GetGamma() {
149 return _GP(play).gamma_adjustment;
150 }
151
System_SetGamma(int newValue)152 void System_SetGamma(int newValue) {
153 if ((newValue < 0) || (newValue > 200))
154 quitprintf("!System.Gamma: value must be between 0-200 (not %d)", newValue);
155
156 if (_GP(play).gamma_adjustment != newValue) {
157 debug_script_log("Gamma control set to %d", newValue);
158 _GP(play).gamma_adjustment = newValue;
159
160 if (_G(gfxDriver)->SupportsGammaControl())
161 _G(gfxDriver)->SetGamma(newValue);
162 }
163 }
164
System_GetAudioChannelCount()165 int System_GetAudioChannelCount() {
166 return MAX_SOUND_CHANNELS;
167 }
168
System_GetAudioChannels(int index)169 ScriptAudioChannel *System_GetAudioChannels(int index) {
170 if ((index < 0) || (index >= MAX_SOUND_CHANNELS))
171 quit("!System.AudioChannels: invalid sound channel index");
172
173 return &_G(scrAudioChannel)[index];
174 }
175
System_GetVolume()176 int System_GetVolume() {
177 return _GP(play).digital_master_volume;
178 }
179
System_SetVolume(int newvol)180 void System_SetVolume(int newvol) {
181 if ((newvol < 0) || (newvol > 100))
182 quit("!System.Volume: invalid volume - must be from 0-100");
183
184 _GP(play).digital_master_volume = newvol;
185
186 Audio::Mixer *mixer = ::AGS::g_vm->_mixer;
187 double percent = (double)newvol / 100.0;
188 int musicVol = static_cast<int>((double)ConfMan.getInt("music_volume") * percent);
189 int sfxVol = static_cast<int>((double)ConfMan.getInt("sfx_volume") * percent);
190
191 mixer->setVolumeForSoundType(Audio::Mixer::kMusicSoundType, musicVol);
192 mixer->setVolumeForSoundType(Audio::Mixer::kSFXSoundType, sfxVol);
193 }
194
System_GetRuntimeInfo()195 const char *System_GetRuntimeInfo() {
196 String runtimeInfo = GetRuntimeInfo();
197
198 return CreateNewScriptString(runtimeInfo.GetCStr());
199 }
200
System_GetRenderAtScreenResolution()201 int System_GetRenderAtScreenResolution() {
202 return _GP(usetup).RenderAtScreenRes;
203 }
204
System_SetRenderAtScreenResolution(int enable)205 void System_SetRenderAtScreenResolution(int enable) {
206 _GP(usetup).RenderAtScreenRes = enable != 0;
207 }
208
209 //=============================================================================
210 //
211 // Script API Functions
212 //
213 //=============================================================================
214
Sc_System_GetAudioChannelCount(const RuntimeScriptValue * params,int32_t param_count)215 RuntimeScriptValue Sc_System_GetAudioChannelCount(const RuntimeScriptValue *params, int32_t param_count) {
216 API_SCALL_INT(System_GetAudioChannelCount);
217 }
218
219 // ScriptAudioChannel* (int index)
Sc_System_GetAudioChannels(const RuntimeScriptValue * params,int32_t param_count)220 RuntimeScriptValue Sc_System_GetAudioChannels(const RuntimeScriptValue *params, int32_t param_count) {
221 API_SCALL_OBJ_PINT(ScriptAudioChannel, _GP(ccDynamicAudio), System_GetAudioChannels);
222 }
223
224 // int ()
Sc_System_GetCapsLock(const RuntimeScriptValue * params,int32_t param_count)225 RuntimeScriptValue Sc_System_GetCapsLock(const RuntimeScriptValue *params, int32_t param_count) {
226 API_SCALL_INT(System_GetCapsLock);
227 }
228
229 // int ()
Sc_System_GetColorDepth(const RuntimeScriptValue * params,int32_t param_count)230 RuntimeScriptValue Sc_System_GetColorDepth(const RuntimeScriptValue *params, int32_t param_count) {
231 API_SCALL_INT(System_GetColorDepth);
232 }
233
234 // int ()
Sc_System_GetGamma(const RuntimeScriptValue * params,int32_t param_count)235 RuntimeScriptValue Sc_System_GetGamma(const RuntimeScriptValue *params, int32_t param_count) {
236 API_SCALL_INT(System_GetGamma);
237 }
238
239 // void (int newValue)
Sc_System_SetGamma(const RuntimeScriptValue * params,int32_t param_count)240 RuntimeScriptValue Sc_System_SetGamma(const RuntimeScriptValue *params, int32_t param_count) {
241 API_SCALL_VOID_PINT(System_SetGamma);
242 }
243
244 // int ()
Sc_System_GetHardwareAcceleration(const RuntimeScriptValue * params,int32_t param_count)245 RuntimeScriptValue Sc_System_GetHardwareAcceleration(const RuntimeScriptValue *params, int32_t param_count) {
246 API_SCALL_INT(System_GetHardwareAcceleration);
247 }
248
Sc_System_GetHasInputFocus(const RuntimeScriptValue * params,int32_t param_count)249 RuntimeScriptValue Sc_System_GetHasInputFocus(const RuntimeScriptValue *params, int32_t param_count) {
250 API_SCALL_BOOL(System_HasInputFocus);
251 }
252
253 // int ()
Sc_System_GetNumLock(const RuntimeScriptValue * params,int32_t param_count)254 RuntimeScriptValue Sc_System_GetNumLock(const RuntimeScriptValue *params, int32_t param_count) {
255 API_SCALL_INT(System_GetNumLock);
256 }
257
258 // int ()
Sc_System_GetOS(const RuntimeScriptValue * params,int32_t param_count)259 RuntimeScriptValue Sc_System_GetOS(const RuntimeScriptValue *params, int32_t param_count) {
260 API_SCALL_INT(System_GetOS);
261 }
262
263 // int ()
Sc_System_GetScreenHeight(const RuntimeScriptValue * params,int32_t param_count)264 RuntimeScriptValue Sc_System_GetScreenHeight(const RuntimeScriptValue *params, int32_t param_count) {
265 API_SCALL_INT(System_GetScreenHeight);
266 }
267
268 // int ()
Sc_System_GetScreenWidth(const RuntimeScriptValue * params,int32_t param_count)269 RuntimeScriptValue Sc_System_GetScreenWidth(const RuntimeScriptValue *params, int32_t param_count) {
270 API_SCALL_INT(System_GetScreenWidth);
271 }
272
273 // int ()
Sc_System_GetScrollLock(const RuntimeScriptValue * params,int32_t param_count)274 RuntimeScriptValue Sc_System_GetScrollLock(const RuntimeScriptValue *params, int32_t param_count) {
275 API_SCALL_INT(System_GetScrollLock);
276 }
277
278 // int ()
Sc_System_GetSupportsGammaControl(const RuntimeScriptValue * params,int32_t param_count)279 RuntimeScriptValue Sc_System_GetSupportsGammaControl(const RuntimeScriptValue *params, int32_t param_count) {
280 API_SCALL_INT(System_GetSupportsGammaControl);
281 }
282
283 // const char *()
Sc_System_GetVersion(const RuntimeScriptValue * params,int32_t param_count)284 RuntimeScriptValue Sc_System_GetVersion(const RuntimeScriptValue *params, int32_t param_count) {
285 API_CONST_SCALL_OBJ(const char, _GP(myScriptStringImpl), System_GetVersion);
286 }
287
288 // int ()
Sc_System_GetViewportHeight(const RuntimeScriptValue * params,int32_t param_count)289 RuntimeScriptValue Sc_System_GetViewportHeight(const RuntimeScriptValue *params, int32_t param_count) {
290 API_SCALL_INT(System_GetViewportHeight);
291 }
292
293 // int ()
Sc_System_GetViewportWidth(const RuntimeScriptValue * params,int32_t param_count)294 RuntimeScriptValue Sc_System_GetViewportWidth(const RuntimeScriptValue *params, int32_t param_count) {
295 API_SCALL_INT(System_GetViewportWidth);
296 }
297
298 // int ()
Sc_System_GetVolume(const RuntimeScriptValue * params,int32_t param_count)299 RuntimeScriptValue Sc_System_GetVolume(const RuntimeScriptValue *params, int32_t param_count) {
300 API_SCALL_INT(System_GetVolume);
301 }
302
303 // void (int newvol)
Sc_System_SetVolume(const RuntimeScriptValue * params,int32_t param_count)304 RuntimeScriptValue Sc_System_SetVolume(const RuntimeScriptValue *params, int32_t param_count) {
305 API_SCALL_VOID_PINT(System_SetVolume);
306 }
307
308 // int ()
Sc_System_GetVsync(const RuntimeScriptValue * params,int32_t param_count)309 RuntimeScriptValue Sc_System_GetVsync(const RuntimeScriptValue *params, int32_t param_count) {
310 API_SCALL_INT(System_GetVsync);
311 }
312
313 // void (int newValue)
Sc_System_SetVsync(const RuntimeScriptValue * params,int32_t param_count)314 RuntimeScriptValue Sc_System_SetVsync(const RuntimeScriptValue *params, int32_t param_count) {
315 API_SCALL_VOID_PINT(System_SetVsync);
316 }
317
Sc_System_GetWindowed(const RuntimeScriptValue * params,int32_t param_count)318 RuntimeScriptValue Sc_System_GetWindowed(const RuntimeScriptValue *params, int32_t param_count) {
319 API_SCALL_INT(System_GetWindowed);
320 }
321
Sc_System_SetWindowed(const RuntimeScriptValue * params,int32_t param_count)322 RuntimeScriptValue Sc_System_SetWindowed(const RuntimeScriptValue *params, int32_t param_count) {
323 API_SCALL_VOID_PINT(System_SetWindowed);
324 }
325
326 // const char *()
Sc_System_GetRuntimeInfo(const RuntimeScriptValue * params,int32_t param_count)327 RuntimeScriptValue Sc_System_GetRuntimeInfo(const RuntimeScriptValue *params, int32_t param_count) {
328 API_CONST_SCALL_OBJ(const char, _GP(myScriptStringImpl), System_GetRuntimeInfo);
329 }
330
Sc_System_GetRenderAtScreenResolution(const RuntimeScriptValue * params,int32_t param_count)331 RuntimeScriptValue Sc_System_GetRenderAtScreenResolution(const RuntimeScriptValue *params, int32_t param_count) {
332 API_SCALL_INT(System_GetRenderAtScreenResolution);
333 }
334
Sc_System_SetRenderAtScreenResolution(const RuntimeScriptValue * params,int32_t param_count)335 RuntimeScriptValue Sc_System_SetRenderAtScreenResolution(const RuntimeScriptValue *params, int32_t param_count) {
336 API_SCALL_VOID_PINT(System_SetRenderAtScreenResolution);
337 }
338
Sc_System_SaveConfigToFile(const RuntimeScriptValue * params,int32_t param_count)339 RuntimeScriptValue Sc_System_SaveConfigToFile(const RuntimeScriptValue *params, int32_t param_count) {
340 API_SCALL_VOID(save_config_file);
341 }
342
Sc_System_Log(const RuntimeScriptValue * params,int32_t param_count)343 RuntimeScriptValue Sc_System_Log(const RuntimeScriptValue *params, int32_t param_count) {
344 API_SCALL_SCRIPT_SPRINTF_PURE(Sc_System_Log, 2);
345 Debug::Printf(kDbgGroup_Script, (MessageType)params[0].IValue, String::Wrapper(scsf_buffer));
346 return RuntimeScriptValue((int32_t)0);
347 }
348
349
RegisterSystemAPI()350 void RegisterSystemAPI() {
351 ccAddExternalStaticFunction("System::get_AudioChannelCount", Sc_System_GetAudioChannelCount);
352 ccAddExternalStaticFunction("System::geti_AudioChannels", Sc_System_GetAudioChannels);
353 ccAddExternalStaticFunction("System::get_CapsLock", Sc_System_GetCapsLock);
354 ccAddExternalStaticFunction("System::get_ColorDepth", Sc_System_GetColorDepth);
355 ccAddExternalStaticFunction("System::get_Gamma", Sc_System_GetGamma);
356 ccAddExternalStaticFunction("System::set_Gamma", Sc_System_SetGamma);
357 ccAddExternalStaticFunction("System::get_HardwareAcceleration", Sc_System_GetHardwareAcceleration);
358 ccAddExternalStaticFunction("System::get_HasInputFocus", Sc_System_GetHasInputFocus);
359 ccAddExternalStaticFunction("System::get_NumLock", Sc_System_GetNumLock);
360 ccAddExternalStaticFunction("System::get_OperatingSystem", Sc_System_GetOS);
361 ccAddExternalStaticFunction("System::get_RenderAtScreenResolution", Sc_System_GetRenderAtScreenResolution);
362 ccAddExternalStaticFunction("System::set_RenderAtScreenResolution", Sc_System_SetRenderAtScreenResolution);
363 ccAddExternalStaticFunction("System::get_RuntimeInfo", Sc_System_GetRuntimeInfo);
364 ccAddExternalStaticFunction("System::get_ScreenHeight", Sc_System_GetScreenHeight);
365 ccAddExternalStaticFunction("System::get_ScreenWidth", Sc_System_GetScreenWidth);
366 ccAddExternalStaticFunction("System::get_ScrollLock", Sc_System_GetScrollLock);
367 ccAddExternalStaticFunction("System::get_SupportsGammaControl", Sc_System_GetSupportsGammaControl);
368 ccAddExternalStaticFunction("System::get_Version", Sc_System_GetVersion);
369 ccAddExternalStaticFunction("SystemInfo::get_Version", Sc_System_GetVersion);
370 ccAddExternalStaticFunction("System::get_ViewportHeight", Sc_System_GetViewportHeight);
371 ccAddExternalStaticFunction("System::get_ViewportWidth", Sc_System_GetViewportWidth);
372 ccAddExternalStaticFunction("System::get_Volume", Sc_System_GetVolume);
373 ccAddExternalStaticFunction("System::set_Volume", Sc_System_SetVolume);
374 ccAddExternalStaticFunction("System::get_VSync", Sc_System_GetVsync);
375 ccAddExternalStaticFunction("System::set_VSync", Sc_System_SetVsync);
376 ccAddExternalStaticFunction("System::get_Windowed", Sc_System_GetWindowed);
377 ccAddExternalStaticFunction("System::set_Windowed", Sc_System_SetWindowed);
378
379 ccAddExternalStaticFunction("System::SaveConfigToFile", Sc_System_SaveConfigToFile);
380 ccAddExternalStaticFunction("System::Log^102", Sc_System_Log);
381 }
382
383 } // namespace AGS3
384