Files
IAEngine/Engine/Src/Imp/CPP/Engine.cpp
Isuru Samarathunga 4380705f81 Fixes
2025-10-12 16:39:29 +05:30

93 lines
3.2 KiB
C++

// IAEngine: 2D Game Engine by IA
// Copyright (C) 2025 IASoft (PVT) LTD (oss@iasoft.dev)
//
// This program 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.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <IAEngine/Engine.hpp>
#include <IAEngine/UI.hpp>
#include <Renderer/Renderer.hpp>
#include <IACore/File.hpp>
#include <IAEngine/EngineLibraryInterface.hpp>
namespace ia::iae
{
EXTERN GameFunctionTable g_gameFunctions;
EXTERN SDL_Window *g_windowHandle;
SIZE_T g_resourceNameCounter = 1;
BOOL Engine::IsDebugMode()
{
#if defined(__DEBUG_MODE__)
return true;
#else
return false;
#endif
}
Direction Engine::GetVectorPointingDirection(IN Vec2 v)
{
STATIC CONSTEXPR Direction DIRECTION_MAP[] = {Direction::RIGHT, Direction::DOWN_RIGHT, Direction::DOWN, Direction::DOWN_LEFT,
Direction::LEFT, Direction::UP_LEFT, Direction::UP, Direction::UP_RIGHT};
if ((abs(v.x) <= FLOAT32_EPSILON) && (abs(v.y) <= FLOAT32_EPSILON))
return Direction::NONE;
auto angle = glm::degrees(atan2(v.y, v.x));
if (angle < 0)
angle += 360;
return DIRECTION_MAP[INT32((angle + 22.5) / 45) % 8];
}
VOID Engine::ResizeDisplay(IN INT32 newWidth, IN INT32 newHeight)
{
Renderer::WaitForGPUIdle();
SDL_SetWindowSize(g_windowHandle, newWidth, newHeight);
Renderer::OnScreenResize(newWidth, newHeight);
UI::OnScreenResize(newWidth, newHeight);
g_gameFunctions.OnResize(newWidth, newHeight);
}
Handle Engine::CreateImageFromFile(IN CONST String &name, IN CONST String &path, IN INT32 resizeToWidth,
IN INT32 resizeToHeight)
{
const auto data = File::ReadToVector(path.c_str());
const auto handle = CreateImage(name, data.data(), data.size());
return (resizeToWidth && resizeToHeight) ? ResizeImage(handle, resizeToWidth, resizeToHeight) : handle;
}
Handle Engine::CreateSoundFromFile(IN CONST String &name, IN CONST String &path)
{
const auto data = File::ReadToVector(path.c_str());
return CreateSound(name, data.data(), data.size());
}
RefPtr<Scene> Engine::CreateSceneFromFile(IN CONST String &path)
{
const auto data = File::ReadToString(path.c_str());
return Scene::Create(data);
}
Handle Engine::ResizeImage(IN CONST String &name, IN INT32 newWidth, IN INT32 newHeight)
{
return ResizeImage(GetImage(name), newWidth, newHeight);
}
String Engine::GetUniqueResourceName()
{
return BuildString("__res_", g_resourceNameCounter++);
}
} // namespace ia::iae