OpenRCT2/src/openrct2/Context.cpp

172 lines
4.4 KiB
C++
Raw Normal View History

#pragma region Copyright (c) 2014-2016 OpenRCT2 Developers
/*****************************************************************************
* OpenRCT2, an open source clone of Roller Coaster Tycoon 2.
*
* OpenRCT2 is the work of many authors, a full list can be found in contributors.md
* For more information, visit https://github.com/OpenRCT2/OpenRCT2
*
* OpenRCT2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* A full copy of the GNU General Public License can be found in licence.txt
*****************************************************************************/
#pragma endregion
#include <exception>
2017-03-26 04:36:22 +02:00
#include "core/Memory.hpp"
2017-03-25 18:42:14 +01:00
#include "config/Config.h"
#include "Context.h"
#include "OpenRCT2.h"
#include "ui/UiContext.h"
using namespace OpenRCT2;
using namespace OpenRCT2::Ui;
namespace OpenRCT2
{
class Context : public IContext
{
private:
IUiContext * const _uiContext;
public:
// Singleton of Context.
// Remove this when GetContext() is no longer called so that
// multiple instances can be created in parallel
static Context * Instance;
public:
Context(IUiContext * uiContext)
: _uiContext(uiContext)
{
Instance = this;
}
~Context() override
{
Instance = nullptr;
}
IUiContext * GetUiContext() override
{
return _uiContext;
}
sint32 RunOpenRCT2(int argc, char * * argv) override
{
core_init();
int runGame = cmdline_run((const char * *)argv, argc);
if (runGame == 1)
{
openrct2_launch();
}
return gExitCode;
}
};
Context * Context::Instance = nullptr;
IContext * CreateContext()
{
return new Context(nullptr);
}
IContext * CreateContext(IUiContext * uiContext)
{
return new Context(uiContext);
}
IContext * GetContext()
{
return Context::Instance;
}
}
extern "C"
{
void context_setcurrentcursor(sint32 cursor)
{
GetContext()->GetUiContext()->SetCursor((CURSOR_ID)cursor);
}
2017-03-25 18:42:14 +01:00
void context_hide_cursor()
{
GetContext()->GetUiContext()->SetCursorVisible(false);
}
void context_show_cursor()
{
GetContext()->GetUiContext()->SetCursorVisible(true);
}
void context_get_cursor_position(sint32 * x, sint32 * y)
{
GetContext()->GetUiContext()->GetCursorPosition(x, y);
}
void context_get_cursor_position_scaled(sint32 * x, sint32 * y)
{
context_get_cursor_position(x, y);
// Compensate for window scaling.
*x = (sint32)ceilf(*x / gConfigGeneral.window_scale);
*y = (sint32)ceilf(*y / gConfigGeneral.window_scale);
}
void context_set_cursor_position(sint32 x, sint32 y)
{
GetContext()->GetUiContext()->SetCursorPosition(x, y);
}
const CursorState * context_get_cursor_state()
{
return GetContext()->GetUiContext()->GetCursorState();
}
const uint8 * context_get_keys_state()
{
return GetContext()->GetUiContext()->GetKeysState();
}
const uint8 * context_get_keys_pressed()
{
return GetContext()->GetUiContext()->GetKeysPressed();
}
2017-03-26 04:36:22 +02:00
TextInputSession * context_start_text_input(utf8 * buffer, size_t maxLength)
2017-03-25 18:42:14 +01:00
{
2017-03-26 04:36:22 +02:00
return GetContext()->GetUiContext()->StartTextInput(buffer, maxLength);
2017-03-25 18:42:14 +01:00
}
void context_stop_text_input()
{
GetContext()->GetUiContext()->StopTextInput();
}
bool context_is_input_active()
{
return GetContext()->GetUiContext()->IsTextInputActive();
}
2017-03-26 04:36:22 +02:00
void context_trigger_resize()
{
return GetContext()->GetUiContext()->TriggerResize();
}
void context_set_fullscreen_mode(sint32 mode)
{
return GetContext()->GetUiContext()->SetFullscreenMode((FULLSCREEN_MODE)mode);
}
sint32 context_get_resolutions(Resolution * * outResolutions)
{
auto resolutions = GetContext()->GetUiContext()->GetFullscreenResolutions();
sint32 count = (sint32)resolutions.size();
*outResolutions = Memory::AllocateArray<Resolution>(count);
Memory::CopyArray(*outResolutions, resolutions.data(), count);
return count;
}
}