Compare commits
9 Commits
609f7337da
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 7e7454278d | |||
| 346779060e | |||
| 175fe61e5c | |||
| f7a8d32865 | |||
| 9a283a2904 | |||
| a288d0961f | |||
| 8fd6cee223 | |||
| 851e1ee122 | |||
| fbd6d43e89 |
2
dependencies/cimgui
vendored
2
dependencies/cimgui
vendored
Submodule dependencies/cimgui updated: 5ba6ea3fc0...d94ad1b162
@@ -14,6 +14,14 @@ public class ExampleComponent : Component
|
|||||||
Console.WriteLine($"C# deltaTime {deltaTime} object id {Owner.Id}");
|
Console.WriteLine($"C# deltaTime {deltaTime} object id {Owner.Id}");
|
||||||
ImGui.Begin("C# WINDOW");
|
ImGui.Begin("C# WINDOW");
|
||||||
ImGui.Text("Hello from ExampleComponent!");
|
ImGui.Text("Hello from ExampleComponent!");
|
||||||
|
if (ImGui.Button("create GameObject"))
|
||||||
|
{
|
||||||
|
GameObject.Create();
|
||||||
|
}
|
||||||
|
if (ImGui.Button("GC Collect"))
|
||||||
|
{
|
||||||
|
GC.Collect();
|
||||||
|
}
|
||||||
ImGui.End();
|
ImGui.End();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -39,8 +39,9 @@ public class GameObject
|
|||||||
|
|
||||||
static public GameObject Create()
|
static public GameObject Create()
|
||||||
{
|
{
|
||||||
NativeMethods.createGameObject(out ulong id, out uint index);
|
NativeFunctions.createGameObject(out ulong id, out uint index);
|
||||||
var o = new GameObject(id, index);
|
var o = new GameObject(id, index);
|
||||||
|
Console.WriteLine($"C# created object with id {id}, index {index}");
|
||||||
return o;
|
return o;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,23 +50,29 @@ public class GameObject
|
|||||||
if(_isDestroyed)
|
if(_isDestroyed)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
_isDestroyed = NativeMethods.destroyGameObject(_index);
|
_isDestroyed = NativeFunctions.freeGameObject(_index);
|
||||||
if(!_isDestroyed)
|
if(!_isDestroyed)
|
||||||
throw new Exception($"Can't destroy GameObject({_id})");
|
throw new Exception($"Can't destroy GameObject({_id})");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
~GameObject()
|
||||||
|
{
|
||||||
|
// destroys object native part when managed part is garbage-collected
|
||||||
|
Destroy();
|
||||||
|
}
|
||||||
|
|
||||||
/// <param name="t">type derived from Component</param>
|
/// <param name="t">type derived from Component</param>
|
||||||
/// <returns>true if new component instance was created, false if component of the same tipe is already added to GameObject</returns>
|
/// <returns>true if new component instance was created, false if component of the same tipe is already added to GameObject</returns>
|
||||||
/// <exception cref="Exception"></exception>
|
/// <exception cref="Exception"></exception>
|
||||||
public bool TryCreateComponent(Type t)
|
public bool TryCreateComponent(Type t)
|
||||||
{
|
{
|
||||||
if(!t.IsSubclassOf(typeof(Component)))
|
if (!t.IsSubclassOf(typeof(Component)))
|
||||||
throw new Exception($"type {t.FullName} is not a derived class of {typeof(Component).FullName}");
|
throw new Exception($"type {t.FullName} is not a derived class of {typeof(Component).FullName}");
|
||||||
|
|
||||||
if(Components.ContainsKey(t))
|
if (Components.ContainsKey(t))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
Component component = (Component?)Activator.CreateInstance(t, this)
|
Component component = (Component?)Activator.CreateInstance(t, this)
|
||||||
?? throw new Exception($"can't create instance of class {t.FullName}");
|
?? throw new Exception($"can't create instance of class {t.FullName}");
|
||||||
Components.Add(t, component);
|
Components.Add(t, component);
|
||||||
return true;
|
return true;
|
||||||
@@ -78,7 +85,9 @@ public class GameObject
|
|||||||
|
|
||||||
private void InvokeUpdate(double deltaTime)
|
private void InvokeUpdate(double deltaTime)
|
||||||
{
|
{
|
||||||
foreach(var p in Components)
|
Console.WriteLine("C# InvokeUpdate");
|
||||||
|
Console.WriteLine($"id {_id}, index {_index}, destroyed {_isDestroyed}");
|
||||||
|
foreach (var p in Components)
|
||||||
{
|
{
|
||||||
p.Value.Update(deltaTime);
|
p.Value.Update(deltaTime);
|
||||||
}
|
}
|
||||||
|
|||||||
12
src-csharp/NativeFunctions.cs
Normal file
12
src-csharp/NativeFunctions.cs
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
|
||||||
|
namespace Ougge;
|
||||||
|
|
||||||
|
internal static class NativeFunctions
|
||||||
|
{
|
||||||
|
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||||
|
internal extern static void createGameObject(out ulong id, out uint index);
|
||||||
|
|
||||||
|
[MethodImpl(MethodImplOptions.InternalCall)]
|
||||||
|
internal extern static bool freeGameObject(uint index);
|
||||||
|
}
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
using System;
|
|
||||||
using System.Runtime.InteropServices;
|
|
||||||
|
|
||||||
namespace Ougge;
|
|
||||||
|
|
||||||
internal static class NativeMethods
|
|
||||||
{
|
|
||||||
[DllImport("__Internal")]
|
|
||||||
internal extern static bool destroyGameObject(uint index);
|
|
||||||
|
|
||||||
[DllImport("__Internal")]
|
|
||||||
internal extern static void createGameObject(out ulong id, out uint index);
|
|
||||||
}
|
|
||||||
146
src/Engine.cpp
146
src/Engine.cpp
@@ -1,26 +1,22 @@
|
|||||||
#include "Engine.hpp"
|
#include "Engine.hpp"
|
||||||
#include "gui/gui.hpp"
|
#include "common/UsefulException.hpp"
|
||||||
|
#include "common/ougge_format.hpp"
|
||||||
|
#include "common/time.hpp"
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
namespace ougge {
|
namespace ougge {
|
||||||
|
|
||||||
Engine::Engine()
|
IEngineModule::IEngineModule(Engine& engine)
|
||||||
: gameObjectPool(GAMEOBJECTPOOL_SIZE), textures(&resourceManager)
|
: engine(engine)
|
||||||
|
{}
|
||||||
|
|
||||||
|
void IEngineModule::beginFrame() {}
|
||||||
|
void IEngineModule::endFrame() {}
|
||||||
|
|
||||||
|
|
||||||
|
void Engine::startLoop()
|
||||||
{
|
{
|
||||||
}
|
|
||||||
|
|
||||||
void Engine::init(){
|
|
||||||
engineManagedAssembly = mono.loadAssembly("Ougge.dll");
|
|
||||||
gameObjectClass = engineManagedAssembly->getClass("Ougge", "GameObject");
|
|
||||||
gameObjectCtor = Mono::Method<void(u64, u32)>(gameObjectClass, ".ctor");
|
|
||||||
gameObjectInvokeUpdate = Mono::Method<void(f64)>(gameObjectClass, "InvokeUpdate");
|
|
||||||
gameObjectTryCreateComponent = Mono::Method<Mono::Bool(Mono::String)>(gameObjectClass, "TryCreateComponent_internal");
|
|
||||||
}
|
|
||||||
|
|
||||||
void Engine::openMainWindow(const std::string& window_title){
|
|
||||||
mainWindow.open(window_title, resourceManager);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Engine::startLoop(){
|
|
||||||
if(loop_running)
|
if(loop_running)
|
||||||
throw UsefulException("loop is running already");
|
throw UsefulException("loop is running already");
|
||||||
|
|
||||||
@@ -28,87 +24,79 @@ void Engine::startLoop(){
|
|||||||
loop_running=true;
|
loop_running=true;
|
||||||
// main loop
|
// main loop
|
||||||
while(loop_running){
|
while(loop_running){
|
||||||
mainWindow.pollEvents(&loop_running);
|
|
||||||
if(!loop_running)
|
|
||||||
break;
|
|
||||||
|
|
||||||
nsec_t update_time_ns = getMonotonicTimeNsec();
|
nsec_t update_time_ns = getMonotonicTimeNsec();
|
||||||
if(update_time_ns < prev_update_time_ns)
|
if(update_time_ns < prev_update_time_ns)
|
||||||
throw UsefulException("monotonic clock returned unexpected value");
|
throw UsefulException("monotonic clock returned unexpected value");
|
||||||
f64 delta_time_s = (f64)(update_time_ns - prev_update_time_ns) / 1e9;
|
f64 delta_time_s = (f64)(update_time_ns - prev_update_time_ns) / 1e9;
|
||||||
prev_update_time_ns = update_time_ns;
|
prev_update_time_ns = update_time_ns;
|
||||||
|
deltaTime = delta_time_s;
|
||||||
tryDrawFrame(delta_time_s);
|
|
||||||
|
tryDrawFrame();
|
||||||
|
|
||||||
nsec_t after_update_time_ns = getMonotonicTimeNsec();
|
nsec_t after_update_time_ns = getMonotonicTimeNsec();
|
||||||
nsec_t frame_delay_ns = (nsec_t)1e9 / mainWindow.fps_max - (after_update_time_ns - update_time_ns);
|
nsec_t frame_delay_ns = (nsec_t)1e9 / fps_max - (after_update_time_ns - update_time_ns);
|
||||||
if(frame_delay_ns > 0){
|
if(frame_delay_ns > 0){
|
||||||
SDL_Delay(frame_delay_ns / 1e6);
|
SDL_Delay(frame_delay_ns / 1e6);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mainWindow.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Engine::stopLoop(){
|
void Engine::stopLoop(){
|
||||||
loop_running = false;
|
loop_running = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Engine::tryDrawFrame(f64 deltaTime){
|
void Engine::handleModuleError(IEngineModule& module, const char* type, const char* method, const char* error){
|
||||||
static std::string error_message;
|
std::string error_message = ougge_format(
|
||||||
static bool draw_error_window = false;
|
"Catched %s at %s.%s(): %s",
|
||||||
try {
|
type, module.getName().c_str(), method, error);
|
||||||
mainWindow.beginFrame();
|
std::cerr<<error_message<<std::endl;
|
||||||
|
error_messages.push_back(error_message);
|
||||||
if(draw_error_window)
|
}
|
||||||
gui::drawErrorWindow(error_message, &draw_error_window);
|
|
||||||
else {
|
void Engine::tryDrawFrame(){
|
||||||
updateGameObjects(deltaTime);
|
auto it = modules.begin();
|
||||||
if(!updateCallback.isNull())
|
while(it != modules.end())
|
||||||
updateCallback(deltaTime);
|
{
|
||||||
|
IEngineModule& module = **it;
|
||||||
|
it++;
|
||||||
|
try {
|
||||||
|
module.beginFrame();
|
||||||
|
}
|
||||||
|
catch(const std::exception& e){
|
||||||
|
handleModuleError(module, "exception", "beginFrame", e.what());
|
||||||
|
}
|
||||||
|
catch(const char* cstr){
|
||||||
|
handleModuleError(module, "error message (const char*)", "beginFrame", cstr);
|
||||||
|
}
|
||||||
|
catch(const std::string& str){
|
||||||
|
handleModuleError(module, "error message (std::string)", "beginFrame", str.c_str());
|
||||||
|
}
|
||||||
|
catch(...){
|
||||||
|
handleModuleError(module, "unknown", "beginFrame", "unknown");
|
||||||
}
|
}
|
||||||
mainWindow.endFrame();
|
|
||||||
}
|
}
|
||||||
catch(const std::exception& e){
|
|
||||||
error_message = "Catched exception: " + std::string(e.what());
|
it = modules.end();
|
||||||
draw_error_window = true;
|
while(it != modules.begin())
|
||||||
std::cerr<<error_message<<std::endl;
|
{
|
||||||
}
|
it--;
|
||||||
catch(const char* cstr){
|
IEngineModule& module = **it;
|
||||||
error_message = "Catched error message (const char*): " + std::string(cstr);
|
try {
|
||||||
draw_error_window = true;
|
module.endFrame();
|
||||||
std::cerr<<error_message<<std::endl;
|
}
|
||||||
}
|
catch(const std::exception& e){
|
||||||
catch(const std::string& str){
|
handleModuleError(module, "exception", "endFrame", e.what());
|
||||||
error_message = "Catched error message (std::string): " + str;
|
}
|
||||||
draw_error_window = true;
|
catch(const char* cstr){
|
||||||
std::cerr<<error_message<<std::endl;
|
handleModuleError(module, "error message (const char*)", "endFrame", cstr);
|
||||||
}
|
}
|
||||||
catch(...){
|
catch(const std::string& str){
|
||||||
error_message = "Catched unknown error";
|
handleModuleError(module, "error message (std::string)", "endFrame", str.c_str());
|
||||||
draw_error_window = true;
|
}
|
||||||
std::cerr<<error_message<<std::endl;
|
catch(...){
|
||||||
|
handleModuleError(module, "unknown", "endFrame", "unknown");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Engine::updateGameObjects(f64 deltaTime){
|
|
||||||
for(auto pair : gameObjectPool){
|
|
||||||
gameObjectInvokeUpdate(pair.second.getObjectHandle().getObject(), deltaTime);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
game::GameObject& Engine::createGameObject(){
|
|
||||||
auto pair = gameObjectPool.emplace(game::GameObject(mono.createObject(gameObjectClass)));
|
|
||||||
game::GameObject& obj = pair.second;
|
|
||||||
gameObjectCtor(obj.getObjectHandle().getObject(), ++obj_id, pair.first);
|
|
||||||
return obj;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Engine::tryCreateComponent(game::GameObject& obj, const std::string& componentClassName){
|
|
||||||
Mono::String componentClassNameManaged = mono.createString(componentClassName);
|
|
||||||
Mono::Bool created = gameObjectTryCreateComponent(obj.getObjectHandle().getObject(), componentClassNameManaged);
|
|
||||||
return created.wide_bool;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
117
src/Engine.hpp
117
src/Engine.hpp
@@ -1,51 +1,106 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "common/function_shared_ptr.hpp"
|
#include <vector>
|
||||||
#include "mono/mono.hpp"
|
#include "common/std.hpp"
|
||||||
#include "game/GameObjectPool.hpp"
|
#include "common/UsefulException.hpp"
|
||||||
#include "gui/MainWindow.hpp"
|
#include "common/ougge_format.hpp"
|
||||||
#include "resources/textures.hpp"
|
#include "common/type_name_demangled.hpp"
|
||||||
|
#include <type_traits>
|
||||||
|
#include <memory>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
namespace ougge {
|
namespace ougge {
|
||||||
|
|
||||||
#define GAMEOBJECTPOOL_SIZE 64*1024
|
class Engine;
|
||||||
|
|
||||||
|
class IEngineModule {
|
||||||
|
public:
|
||||||
|
IEngineModule() = delete;
|
||||||
|
IEngineModule(const IEngineModule&) = delete;
|
||||||
|
IEngineModule& operator=(const IEngineModule&) = delete;
|
||||||
|
IEngineModule(IEngineModule&&) = delete;
|
||||||
|
IEngineModule& operator=(IEngineModule&&) = delete;
|
||||||
|
|
||||||
|
Engine& engine;
|
||||||
|
virtual const std::string& getName() = 0;
|
||||||
|
|
||||||
|
virtual void beginFrame();
|
||||||
|
virtual void endFrame();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
IEngineModule(Engine& engine);
|
||||||
|
};
|
||||||
|
|
||||||
using UpdateFunc_t = function_shared_ptr<void(f64)>;
|
|
||||||
|
|
||||||
class Engine {
|
class Engine {
|
||||||
|
std::vector<std::unique_ptr<IEngineModule>> modules;
|
||||||
bool loop_running = false;
|
bool loop_running = false;
|
||||||
|
|
||||||
game::GameObjectPool gameObjectPool;
|
|
||||||
u64 obj_id = 0;
|
|
||||||
MonoClass* gameObjectClass;
|
|
||||||
Mono::Method<void(u64, u32)> gameObjectCtor;
|
|
||||||
Mono::Method<void(f64)> gameObjectInvokeUpdate;
|
|
||||||
Mono::Method<Mono::Bool(Mono::String)> gameObjectTryCreateComponent;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
gui::MainWindow mainWindow;
|
u32 fps_max = 60;
|
||||||
UpdateFunc_t updateCallback;
|
f64 deltaTime;
|
||||||
|
std::vector<std::string> error_messages;
|
||||||
|
|
||||||
|
Engine() = default;
|
||||||
|
Engine(const Engine&) = delete;
|
||||||
|
Engine& operator=(const Engine&) = delete;
|
||||||
|
Engine(Engine&&) = delete;
|
||||||
|
Engine& operator=(Engine&&) = delete;
|
||||||
|
|
||||||
Mono::RuntimeJIT mono;
|
|
||||||
std::shared_ptr<Mono::Assembly> engineManagedAssembly;
|
|
||||||
|
|
||||||
resources::ResourceManager resourceManager;
|
|
||||||
resources::CacheStorage<resources::Texture> textures;
|
|
||||||
|
|
||||||
Engine();
|
|
||||||
void init();
|
|
||||||
void openMainWindow(const std::string& window_title);
|
|
||||||
// start game loop on the current thread
|
// start game loop on the current thread
|
||||||
void startLoop();
|
void startLoop();
|
||||||
void stopLoop();
|
void stopLoop();
|
||||||
|
|
||||||
game::GameObject& createGameObject();
|
//TODO: add std::enable_if
|
||||||
bool tryCreateComponent(game::GameObject& obj, const std::string& componentClassName);
|
template<typename ModuleT, typename... ConstructorArgsT>
|
||||||
|
ModuleT& createModule(ConstructorArgsT&&... args){
|
||||||
|
static_assert(std::is_base_of<IEngineModule, ModuleT>::value);
|
||||||
|
|
||||||
|
auto& module_type_name = ougge_type_name<ModuleT>();
|
||||||
|
//TODO: replace with some logger call
|
||||||
|
std::cout<<"Initializing module '"<<module_type_name<<"'"<<std::endl;
|
||||||
|
|
||||||
|
size_t module_index;
|
||||||
|
bool module_exists;
|
||||||
|
getOrCreateModuleIndex<ModuleT>(&module_index, &module_exists);
|
||||||
|
|
||||||
|
if(module_exists){
|
||||||
|
throw UsefulException(ougge_format("can't create second instance of module '%s'",
|
||||||
|
module_type_name.c_str()));
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO: std::forward(args)...
|
||||||
|
modules.push_back(std::make_unique<ModuleT>(*this, args...));
|
||||||
|
return *static_cast<ModuleT*>(modules[module_index].get());
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO: add std::enable_if
|
||||||
|
template<typename ModuleT>
|
||||||
|
ModuleT& getModule(){
|
||||||
|
static_assert(std::is_base_of<IEngineModule, ModuleT>::value);
|
||||||
|
|
||||||
|
size_t module_index;
|
||||||
|
bool module_exists;
|
||||||
|
getOrCreateModuleIndex<ModuleT>(&module_index, &module_exists);
|
||||||
|
if(!module_exists){
|
||||||
|
auto& module_type_name = ougge_type_name<ModuleT>();
|
||||||
|
throw UsefulException(ougge_format("engine has no module '%s'",
|
||||||
|
module_type_name.c_str()));
|
||||||
|
}
|
||||||
|
return *static_cast<ModuleT*>(modules[module_index].get());
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void tryDrawFrame(f64 deltaTime);
|
void tryDrawFrame();
|
||||||
void updateGameObjects(f64 deltaTime);
|
void handleModuleError(IEngineModule& module, const char *type, const char *method, const char *error);
|
||||||
};
|
|
||||||
|
|
||||||
|
//TODO: add std::enable_if
|
||||||
|
template<typename ModuleT>
|
||||||
|
void getOrCreateModuleIndex(size_t* out_index, bool* out_module_exists){
|
||||||
|
static size_t module_index = modules.size();
|
||||||
|
*out_index = module_index;
|
||||||
|
// if module_index == modules.size(), then the module hasn't been pushed to the vector yet
|
||||||
|
*out_module_exists = module_index != modules.size();
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,30 +0,0 @@
|
|||||||
#include "GameObject.hpp"
|
|
||||||
|
|
||||||
namespace ougge::game {
|
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream& s, Transform& t){
|
|
||||||
s<<"{ position: {x: "<<t.position.x<<", y: "<<t.position.y;
|
|
||||||
s<<"}, rotation: "<<t.rotation;
|
|
||||||
s<<", scale {x: "<<t.scale.x<<", y: "<<t.scale.y<<"} }";
|
|
||||||
return s;
|
|
||||||
}
|
|
||||||
|
|
||||||
GameObject::GameObject(Mono::Object managed_obj)
|
|
||||||
: object_handle(managed_obj)
|
|
||||||
{}
|
|
||||||
|
|
||||||
GameObject::GameObject(GameObject &&o) :
|
|
||||||
object_handle(std::move(o.object_handle)),
|
|
||||||
parent(o.parent),
|
|
||||||
transform(o.transform)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
GameObject& GameObject::operator=(GameObject &&o){
|
|
||||||
object_handle = std::move(o.object_handle);
|
|
||||||
parent = o.parent;
|
|
||||||
transform = o.transform;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,45 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <map>
|
|
||||||
#include <iostream>
|
|
||||||
#include "../common/math.hpp"
|
|
||||||
#include "../common/UsefulException.hpp"
|
|
||||||
#include "../mono/mono.hpp"
|
|
||||||
|
|
||||||
namespace ougge::game {
|
|
||||||
|
|
||||||
class GameObject;
|
|
||||||
|
|
||||||
struct Transform {
|
|
||||||
Vec2 scale = { 1, 1 };
|
|
||||||
Vec2 position = { 0, 0 };
|
|
||||||
angle_t rotation = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream& s, Transform& t);
|
|
||||||
|
|
||||||
|
|
||||||
class GameObject {
|
|
||||||
Mono::ObjectHandle object_handle;
|
|
||||||
GameObject* parent;
|
|
||||||
Transform transform;
|
|
||||||
|
|
||||||
public:
|
|
||||||
/// @warning Do not use this to create objects.
|
|
||||||
/// This constructor creates null values for GameObject arrays
|
|
||||||
/// GameObject* array = new GameObject[10];
|
|
||||||
/// array[0] = GameObject(initialized_mono_object_ptr)
|
|
||||||
GameObject() = default;
|
|
||||||
GameObject(Mono::Object managed_obj);
|
|
||||||
GameObject(const GameObject& o) = delete;
|
|
||||||
GameObject(GameObject&& o);
|
|
||||||
|
|
||||||
GameObject& operator=(GameObject&& o);
|
|
||||||
|
|
||||||
inline Mono::ObjectHandle& getObjectHandle() { return object_handle; }
|
|
||||||
inline GameObject* getParent() { return parent; }
|
|
||||||
inline void setParent(GameObject* p) { parent = p; }
|
|
||||||
inline Transform& getTransform() { return transform; }
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,130 +0,0 @@
|
|||||||
#include "GameObjectPool.hpp"
|
|
||||||
#include <bit>
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
namespace ougge::game {
|
|
||||||
|
|
||||||
GameObjectPool::GameObjectPool(u32 size)
|
|
||||||
{
|
|
||||||
useful_assert(size % 64 == 0, "size of GameObjectPool must be a multiple of 64");
|
|
||||||
this->size = size;
|
|
||||||
first_unused_index = 0;
|
|
||||||
buffer = new GameObject[size];
|
|
||||||
used_indices = new u64[size/64];
|
|
||||||
// std::memset(buffer, 0, size*sizeof(GameObject));
|
|
||||||
std::memset(used_indices, 0, size/8);
|
|
||||||
}
|
|
||||||
|
|
||||||
GameObjectPool::~GameObjectPool()
|
|
||||||
{
|
|
||||||
delete[] buffer;
|
|
||||||
delete[] used_indices;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool GameObjectPool::isIndexUsed(u32 index)
|
|
||||||
{
|
|
||||||
return ( used_indices[index/64] & (u64(1)<<(index%64)) ) != 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
u32 GameObjectPool::getNearestUnusedIndex(u32 startIndex)
|
|
||||||
{
|
|
||||||
if(startIndex >= size)
|
|
||||||
return -1;
|
|
||||||
if(!isIndexUsed(startIndex))
|
|
||||||
return startIndex;
|
|
||||||
|
|
||||||
u32 i = startIndex/64;
|
|
||||||
// mark previous bits as used
|
|
||||||
u64 u = used_indices[i] | ( (u64(1)<<startIndex%64) -1 );
|
|
||||||
while(u == u64(-1)){
|
|
||||||
i++;
|
|
||||||
if(i == size/64)
|
|
||||||
return -1;
|
|
||||||
u = used_indices[i];
|
|
||||||
}
|
|
||||||
u32 bitpos = std::countr_one(u);
|
|
||||||
if(bitpos == 64)
|
|
||||||
return -1;
|
|
||||||
u32 index = i*64 + bitpos;
|
|
||||||
return index;
|
|
||||||
}
|
|
||||||
|
|
||||||
u32 GameObjectPool::getNearestUsedIndex(u32 startIndex)
|
|
||||||
{
|
|
||||||
if(startIndex >= size)
|
|
||||||
return -1;
|
|
||||||
if(isIndexUsed(startIndex))
|
|
||||||
return startIndex;
|
|
||||||
|
|
||||||
u32 i = startIndex/64;
|
|
||||||
// mark previous bits as unused
|
|
||||||
u64 u = used_indices[i] & !( (u64(1)<<startIndex%64) -1 );
|
|
||||||
while(u == 0){
|
|
||||||
i++;
|
|
||||||
if(i == size/64)
|
|
||||||
return -1;
|
|
||||||
u = used_indices[i];
|
|
||||||
}
|
|
||||||
u32 bitpos = std::countr_zero(u);
|
|
||||||
if(bitpos == 64)
|
|
||||||
return -1;
|
|
||||||
u32 index = i*64 + bitpos;
|
|
||||||
return index;
|
|
||||||
}
|
|
||||||
|
|
||||||
GameObject& GameObjectPool::get(u32 index)
|
|
||||||
{
|
|
||||||
if(index >= size)
|
|
||||||
throw UsefulException(ougge_format("index %i is out of size %i", index, size));
|
|
||||||
if(!isIndexUsed(index))
|
|
||||||
throw UsefulException(ougge_format("there is no object at index %i", index));
|
|
||||||
return buffer[index];
|
|
||||||
}
|
|
||||||
|
|
||||||
std::pair<u32, GameObject&> GameObjectPool::emplace(GameObject&& new_obj)
|
|
||||||
{
|
|
||||||
u32 i = first_unused_index;
|
|
||||||
if(i == u32(-1))
|
|
||||||
throw UsefulException("can't put new GameObject to GameObjectPool because it's full");
|
|
||||||
|
|
||||||
buffer[i] = std::move(new_obj);
|
|
||||||
GameObject& r = buffer[i];
|
|
||||||
used_indices[i/64] |= u64(1)<<(i%64); // mark index bit as used
|
|
||||||
first_unused_index = getNearestUnusedIndex(i+1);
|
|
||||||
return std::pair<u32, GameObject&>(i, r);
|
|
||||||
}
|
|
||||||
|
|
||||||
void GameObjectPool::erase(u32 index)
|
|
||||||
{
|
|
||||||
if(index >= size)
|
|
||||||
throw UsefulException(ougge_format("index %i is out of size %i", index, size));
|
|
||||||
if(!isIndexUsed(index))
|
|
||||||
throw UsefulException(ougge_format("there is no object at index %i", index));
|
|
||||||
|
|
||||||
buffer[index] = GameObject();
|
|
||||||
used_indices[index/64] &= ~(u64(1)<<(index%64)); // mark index bit as unused
|
|
||||||
if(index < first_unused_index)
|
|
||||||
first_unused_index = index;
|
|
||||||
}
|
|
||||||
|
|
||||||
GameObjectPool::iterator::iterator(GameObjectPool* pool, u32 index)
|
|
||||||
: pool(pool), index(index)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
std::pair<u32, GameObject&> GameObjectPool::iterator::operator*()
|
|
||||||
{
|
|
||||||
if(index >= pool->size)
|
|
||||||
throw UsefulException("can't get value of end() iterator");
|
|
||||||
|
|
||||||
GameObject& r = pool->buffer[index];
|
|
||||||
return std::pair<u32, GameObject&>(index, r);
|
|
||||||
}
|
|
||||||
|
|
||||||
GameObjectPool::iterator& GameObjectPool::iterator::operator++()
|
|
||||||
{
|
|
||||||
index = pool->getNearestUsedIndex(index+1);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
#include "GameObject.hpp"
|
|
||||||
|
|
||||||
namespace ougge::game {
|
|
||||||
|
|
||||||
/*
|
|
||||||
Fixed array that stores deleted elements indices as bits in array of u64.
|
|
||||||
Fast emplace, erase and lookup.
|
|
||||||
|
|
||||||
------------------------[construct]------------------------
|
|
||||||
operation 'GameObjectPool::GameObjectPool()' took 1.0549 ms
|
|
||||||
operation 'other_collections_construct' took 0.0133 ms
|
|
||||||
-------------------------[emplace]-------------------------
|
|
||||||
operation 'GameObjectPool::emplace' took 8.0557 ms
|
|
||||||
operation 'vector::emplace_back' took 11.3735 ms
|
|
||||||
operation 'set::emplace' took 80.5633 ms
|
|
||||||
operation 'list::emplace_front' took 18.1442 ms
|
|
||||||
operation 'forward_list::emplace_front' took 11.5467 ms
|
|
||||||
--------------------------[erase]--------------------------
|
|
||||||
operation 'GameObjectPool::erase' took 0.2745 ms
|
|
||||||
operation 'vector::erase' took 15790.6 ms
|
|
||||||
operation 'set::erase' took 1.2697 ms
|
|
||||||
operation 'list::erase_after' took 0.93 ms
|
|
||||||
operation 'forward_list::erase_after' took 1.1127 ms
|
|
||||||
-------------------------[iterate]-------------------------
|
|
||||||
operation 'GameObjectPool::iterate' took 1.1166 ms
|
|
||||||
operation 'vector::iterate' took 0.8883 ms
|
|
||||||
operation 'set::iterate' took 2.8011 ms
|
|
||||||
operation 'list::iterate' took 2.0766 ms
|
|
||||||
operation 'forward_list::iterate' took 2.0823 ms
|
|
||||||
*/
|
|
||||||
|
|
||||||
class GameObjectPool {
|
|
||||||
GameObject* buffer;
|
|
||||||
u64* used_indices;
|
|
||||||
u32 size;
|
|
||||||
u32 first_unused_index;
|
|
||||||
|
|
||||||
bool isIndexUsed(u32 index);
|
|
||||||
u32 getNearestUnusedIndex(u32 startIndex);
|
|
||||||
u32 getNearestUsedIndex(u32 startIndex);
|
|
||||||
public:
|
|
||||||
|
|
||||||
///@param size must be a multiple of 64
|
|
||||||
GameObjectPool(u32 size);
|
|
||||||
~GameObjectPool();
|
|
||||||
GameObject& get(u32 index);
|
|
||||||
std::pair<u32, GameObject&> emplace(GameObject&& new_obj);
|
|
||||||
void erase(u32 index);
|
|
||||||
|
|
||||||
#pragma region iterator class
|
|
||||||
class iterator {
|
|
||||||
GameObjectPool* pool;
|
|
||||||
u32 index = 0;
|
|
||||||
|
|
||||||
public:
|
|
||||||
iterator(GameObjectPool* pool, u32 index);
|
|
||||||
std::pair<u32, GameObject&> operator*();
|
|
||||||
iterator& operator++();
|
|
||||||
inline bool operator!=(const iterator& o) const { return index != o.index; };
|
|
||||||
inline bool operator==(const iterator& o) const { return index == o.index; };
|
|
||||||
};
|
|
||||||
#pragma endregion
|
|
||||||
|
|
||||||
inline iterator begin() { return iterator(this, 0); }
|
|
||||||
inline iterator end() { return iterator(this, -1); }
|
|
||||||
|
|
||||||
friend class iterator;
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,17 +0,0 @@
|
|||||||
#include "mono.hpp"
|
|
||||||
|
|
||||||
namespace Mono {
|
|
||||||
|
|
||||||
Assembly::Assembly(MonoAssembly *ptr)
|
|
||||||
: ptr(ptr), image(mono_assembly_get_image(ptr))
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
MonoClass* Assembly::getClass(const std::string &name_space, const std::string &name){
|
|
||||||
auto c = mono_class_from_name(image, name_space.c_str(), name.c_str());
|
|
||||||
if(!c)
|
|
||||||
throw UsefulException(ougge_format("can't get class '%s.%s'", name_space.c_str(), name.c_str()));
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,81 +0,0 @@
|
|||||||
#include "mono.hpp"
|
|
||||||
#include <mono/metadata/appdomain.h>
|
|
||||||
|
|
||||||
namespace Mono {
|
|
||||||
|
|
||||||
template <> MonoClass* getClass<SByte>(){ return mono_get_sbyte_class(); }
|
|
||||||
template <> MonoClass* getClass<Byte>(){ return mono_get_byte_class(); }
|
|
||||||
template <> MonoClass* getClass<Short>(){ return mono_get_int16_class(); }
|
|
||||||
template <> MonoClass* getClass<UShort>(){ return mono_get_uint16_class(); }
|
|
||||||
template <> MonoClass* getClass<Int>(){ return mono_get_int32_class(); }
|
|
||||||
template <> MonoClass* getClass<UInt>(){ return mono_get_uint32_class(); }
|
|
||||||
template <> MonoClass* getClass<Long>(){ return mono_get_int64_class(); }
|
|
||||||
template <> MonoClass* getClass<ULong>(){ return mono_get_uint64_class(); }
|
|
||||||
template <> MonoClass* getClass<Float>(){ return mono_get_single_class(); }
|
|
||||||
template <> MonoClass* getClass<Double>(){ return mono_get_double_class(); }
|
|
||||||
template <> MonoClass* getClass<Bool>(){ return mono_get_boolean_class(); }
|
|
||||||
template <> MonoClass* getClass<Char>(){ return mono_get_char_class(); }
|
|
||||||
template <> MonoClass* getClass<String>(){ return mono_get_string_class(); }
|
|
||||||
template <> MonoClass* getClass<Object>(){ return mono_get_object_class(); }
|
|
||||||
template <> MonoClass* getClass<Void>(){ return mono_get_void_class(); }
|
|
||||||
|
|
||||||
|
|
||||||
void getMethodSignatureTypes(MonoMethod* mono_method,
|
|
||||||
MonoType** return_type,
|
|
||||||
std::vector<MonoType*>& argument_types)
|
|
||||||
{
|
|
||||||
auto signature = mono_method_signature(mono_method);
|
|
||||||
void* iter = nullptr;
|
|
||||||
*return_type = mono_signature_get_return_type(signature);
|
|
||||||
MonoType* pt = nullptr;
|
|
||||||
while( (pt = mono_signature_get_params(signature, &iter)) ){
|
|
||||||
argument_types.push_back(pt);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
MonoMethod* tryGetMonoMethod(MonoClass* target_class, const std::string& name,
|
|
||||||
MonoClass* return_class, MonoClass* arg_classes[], size_t arg_classes_size)
|
|
||||||
{
|
|
||||||
if(target_class == nullptr)
|
|
||||||
throw UsefulException("target_class is nullptr");
|
|
||||||
|
|
||||||
// iterate each method
|
|
||||||
void* iter = nullptr;
|
|
||||||
MonoMethod* m = nullptr;
|
|
||||||
std::vector<MonoType*> argument_types;
|
|
||||||
while( (m = mono_class_get_methods(target_class, &iter)) ){
|
|
||||||
// compare name
|
|
||||||
std::string m_name = mono_method_get_name(m);
|
|
||||||
if(m_name != name)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
argument_types.clear();
|
|
||||||
MonoType* return_type = nullptr;
|
|
||||||
getMethodSignatureTypes(m, &return_type, argument_types);
|
|
||||||
// compare argument count
|
|
||||||
if(argument_types.size() != arg_classes_size)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// compare return type
|
|
||||||
if(!mono_metadata_type_equal(return_type, mono_class_get_type(return_class)))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// compare argument types
|
|
||||||
bool argument_types_mismatch = false;
|
|
||||||
for(size_t i = 0; i < arg_classes_size; i++){
|
|
||||||
if(!mono_metadata_type_equal(argument_types[i], mono_class_get_type(arg_classes[i]))){
|
|
||||||
argument_types_mismatch = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(argument_types_mismatch)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
// passed all tests successfully
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return m;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,187 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include "../common/std.hpp"
|
|
||||||
#include "../common/UsefulException.hpp"
|
|
||||||
#include "../common/ougge_format.hpp"
|
|
||||||
#include <vector>
|
|
||||||
#include <type_traits>
|
|
||||||
#include <mono/metadata/class.h>
|
|
||||||
#include <mono/metadata/assembly.h>
|
|
||||||
#include <mono/metadata/object.h>
|
|
||||||
|
|
||||||
namespace Mono {
|
|
||||||
|
|
||||||
typedef i8 SByte;
|
|
||||||
typedef u8 Byte;
|
|
||||||
typedef i16 Short;
|
|
||||||
typedef u16 UShort;
|
|
||||||
typedef i32 Int;
|
|
||||||
typedef u32 UInt;
|
|
||||||
typedef i64 Long;
|
|
||||||
typedef u64 ULong;
|
|
||||||
typedef f32 Float;
|
|
||||||
typedef f64 Double;
|
|
||||||
typedef union { mono_bool wide_bool; } Bool; //USAGE: Bool t = {true};
|
|
||||||
typedef char16_t Char;
|
|
||||||
typedef MonoString* String;
|
|
||||||
/// @warning MonoObject can be moved in memory by GC in any time and raw pointer will be invalid.
|
|
||||||
/// Use ObjectHandle where it is possible.
|
|
||||||
typedef MonoObject* Object;
|
|
||||||
typedef void Void;
|
|
||||||
|
|
||||||
|
|
||||||
template<typename PrimitiveT>
|
|
||||||
MonoClass* getClass();
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
void* valueToVoidPtr(T& v){ return &v; }
|
|
||||||
template<>
|
|
||||||
inline void* valueToVoidPtr<Object>(Object& v){ return v; }
|
|
||||||
template<>
|
|
||||||
inline void* valueToVoidPtr<String>(String& v){ return v; }
|
|
||||||
|
|
||||||
template<typename T>
|
|
||||||
T valueFromMonoObject(MonoObject* o){
|
|
||||||
void* result_value_ptr = mono_object_unbox(o);
|
|
||||||
if(result_value_ptr == nullptr)
|
|
||||||
throw UsefulException("can't unbox method value");
|
|
||||||
return *((T*)result_value_ptr);
|
|
||||||
}
|
|
||||||
template<>
|
|
||||||
inline Object valueFromMonoObject<Object>(MonoObject* o){ return o; }
|
|
||||||
template<>
|
|
||||||
inline String valueFromMonoObject<String>(MonoObject* o){ return (String)((void*)o); }
|
|
||||||
|
|
||||||
void getMethodSignatureTypes(MonoMethod* mono_method,
|
|
||||||
MonoType** return_type,
|
|
||||||
std::vector<MonoType*>& argument_types);
|
|
||||||
|
|
||||||
/// searches for method `name` in `target_class` with return type `return_class` and argument types `arg_classes`
|
|
||||||
/// @return found method or nullptr
|
|
||||||
MonoMethod* tryGetMonoMethod(MonoClass* target_class, const std::string& name,
|
|
||||||
MonoClass* return_class, MonoClass* arg_classes[], size_t arg_classes_size);
|
|
||||||
|
|
||||||
|
|
||||||
template<typename SignatureT> class Method;
|
|
||||||
template<typename ReturnT, typename... ArgTypes>
|
|
||||||
class Method<ReturnT(ArgTypes...)>
|
|
||||||
{
|
|
||||||
MonoMethod* method_ptr;
|
|
||||||
|
|
||||||
public:
|
|
||||||
Method() { method_ptr = nullptr; }
|
|
||||||
|
|
||||||
/// all types must implement getClass<T>()
|
|
||||||
Method(MonoClass* target_class, const std::string& name){
|
|
||||||
static MonoClass* arg_classes[] { getClass<ArgTypes>()... };
|
|
||||||
static MonoClass* return_class { getClass<ReturnT>() };
|
|
||||||
method_ptr = tryGetMonoMethod(target_class, name, return_class, arg_classes, sizeof...(ArgTypes));
|
|
||||||
if(method_ptr == nullptr){
|
|
||||||
throw UsefulException(ougge_format("can't get method '%s' from class '%s'",
|
|
||||||
name.c_str(), mono_class_get_name(target_class)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ReturnT not is void
|
|
||||||
template<typename RT = ReturnT>
|
|
||||||
std::enable_if_t<!std::is_void<RT>::value, RT> operator()(MonoObject* class_instance, ArgTypes... args) const {
|
|
||||||
if(method_ptr == nullptr)
|
|
||||||
throw UsefulException("method_ptr is null");
|
|
||||||
|
|
||||||
void* arg_array[] = { valueToVoidPtr(args)..., nullptr };
|
|
||||||
MonoObject* ex = nullptr;
|
|
||||||
MonoObject* result = mono_runtime_invoke(method_ptr, class_instance, arg_array, &ex);
|
|
||||||
if(ex){
|
|
||||||
//TODO: call mono_trace_set_printerr_handler from mono/mono/utils/mono-logger.h
|
|
||||||
mono_print_unhandled_exception(ex);
|
|
||||||
throw UsefulException("Some C# exception occured");
|
|
||||||
}
|
|
||||||
return valueFromMonoObject<ReturnT>(result);
|
|
||||||
};
|
|
||||||
|
|
||||||
// ReturnT is void
|
|
||||||
template<typename RT = ReturnT>
|
|
||||||
std::enable_if_t<std::is_void<RT>::value, RT> operator()(MonoObject* class_instance, ArgTypes... args) const {
|
|
||||||
if(method_ptr == nullptr)
|
|
||||||
throw UsefulException("method_ptr is null");
|
|
||||||
|
|
||||||
void* arg_array[] = { valueToVoidPtr(args)..., nullptr };
|
|
||||||
MonoObject* ex = nullptr;
|
|
||||||
mono_runtime_invoke(method_ptr, class_instance, arg_array, &ex);
|
|
||||||
if(ex){
|
|
||||||
//TODO: call mono_trace_set_printerr_handler from mono/mono/utils/mono-logger.h
|
|
||||||
mono_print_unhandled_exception(ex);
|
|
||||||
throw UsefulException("Some C# exception occured");
|
|
||||||
}
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
class Assembly {
|
|
||||||
MonoAssembly* ptr;
|
|
||||||
MonoImage* image;
|
|
||||||
|
|
||||||
public:
|
|
||||||
Assembly(MonoAssembly* ptr);
|
|
||||||
Assembly(const Assembly&) = delete;
|
|
||||||
|
|
||||||
MonoClass* getClass(const std::string& name_space, const std::string& name);
|
|
||||||
MonoAssembly* getMonoAssembly() const { return ptr; }
|
|
||||||
MonoImage* getMonoImage() const { return image; }
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
///LINUX: `config.xml`, `mscorelib.dll`, `libmono-native.so` in `mono-libs` directory
|
|
||||||
///
|
|
||||||
///WINDOWS: `config.xml`, `mscorelib.dll` in `mono-libs` directory
|
|
||||||
class RuntimeJIT {
|
|
||||||
MonoDomain* domain;
|
|
||||||
public:
|
|
||||||
RuntimeJIT(const std::string& domain_name = "OuggeDomain");
|
|
||||||
RuntimeJIT(const RuntimeJIT&) = delete;
|
|
||||||
~RuntimeJIT();
|
|
||||||
|
|
||||||
inline MonoDomain* getDomain() { return domain; }
|
|
||||||
std::shared_ptr<Assembly> loadAssembly(const std::string& name);
|
|
||||||
|
|
||||||
inline Object createObject(MonoClass* klass) { return mono_object_new(domain, klass); }
|
|
||||||
inline String createString(const std::string& v) { return mono_string_new_len(domain, v.c_str(), v.size()); }
|
|
||||||
inline String createString(const char* v) { return mono_string_new(domain, v); }
|
|
||||||
};
|
|
||||||
|
|
||||||
/// @brief ObjectHandle can be used to store reliable reference to MonoObject.
|
|
||||||
/// MonoObject can be moved in memory by GC in any time and raw pointer will be invalid.
|
|
||||||
struct ObjectHandle {
|
|
||||||
u32 gc_handle;
|
|
||||||
|
|
||||||
inline ObjectHandle() : gc_handle(0) {}
|
|
||||||
|
|
||||||
inline ObjectHandle(Object obj) {
|
|
||||||
gc_handle = mono_gchandle_new(obj, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// implicitly create new ObjectHandle instead
|
|
||||||
inline ObjectHandle(const ObjectHandle& o) = delete;
|
|
||||||
|
|
||||||
inline ObjectHandle(ObjectHandle&& o) {
|
|
||||||
gc_handle = o.gc_handle;
|
|
||||||
o.gc_handle = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline ObjectHandle& operator=(ObjectHandle&& o) {
|
|
||||||
gc_handle = o.gc_handle;
|
|
||||||
o.gc_handle = 0;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline ~ObjectHandle() {
|
|
||||||
if(gc_handle)
|
|
||||||
mono_gchandle_free(gc_handle);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline Object getObject() const {
|
|
||||||
return mono_gchandle_get_target(gc_handle);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,30 +0,0 @@
|
|||||||
#include "mono.hpp"
|
|
||||||
#include <mono/jit/jit.h>
|
|
||||||
#include <mono/metadata/mono-config.h>
|
|
||||||
#include <mono/metadata/appdomain.h>
|
|
||||||
|
|
||||||
namespace Mono {
|
|
||||||
|
|
||||||
RuntimeJIT::RuntimeJIT(const std::string& domain_name){
|
|
||||||
mono_set_dirs("mono-libs", "mono-libs");
|
|
||||||
mono_set_assemblies_path("mono-libs");
|
|
||||||
mono_config_parse("mono-libs/config.xml");
|
|
||||||
|
|
||||||
domain = mono_jit_init(domain_name.c_str());
|
|
||||||
if(!domain)
|
|
||||||
throw UsefulException("can't initialize mono domain");
|
|
||||||
}
|
|
||||||
|
|
||||||
RuntimeJIT::~RuntimeJIT(){
|
|
||||||
// TODO: fix segfault on cleanup
|
|
||||||
// mono_jit_cleanup(domain);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::shared_ptr<Assembly> RuntimeJIT::loadAssembly(const std::string &name){
|
|
||||||
MonoAssembly* ptr = mono_domain_assembly_open(domain, name.c_str());
|
|
||||||
if(!ptr)
|
|
||||||
throw UsefulException(ougge_format("can't load assembly '%s'", name.c_str()));
|
|
||||||
return std::make_shared<Assembly>(ptr);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
#include "MemoryStream.hpp"
|
|
||||||
|
|
||||||
class MemoryStreamBuf : public std::streambuf {
|
|
||||||
public:
|
|
||||||
MemoryStreamBuf(void* p, const std::size_t n);
|
|
||||||
|
|
||||||
virtual std::istream::pos_type seekoff(
|
|
||||||
std::istream::off_type off,
|
|
||||||
std::ios_base::seekdir dir,
|
|
||||||
std::ios_base::openmode which);
|
|
||||||
};
|
|
||||||
|
|
||||||
MemoryStreamBuf::MemoryStreamBuf(void* _p, const std::size_t n){
|
|
||||||
char* p=(char*)_p;
|
|
||||||
setg(p, p, p + n);
|
|
||||||
setp(p, p + n);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::istream::pos_type MemoryStreamBuf::seekoff(
|
|
||||||
std::istream::off_type off,
|
|
||||||
std::ios_base::seekdir dir,
|
|
||||||
std::ios_base::openmode which)
|
|
||||||
{
|
|
||||||
if (dir == std::ios_base::cur)
|
|
||||||
gbump(off);
|
|
||||||
else if (dir == std::ios_base::end)
|
|
||||||
setg(eback(), egptr() + off, egptr());
|
|
||||||
else if (dir == std::ios_base::beg)
|
|
||||||
setg(eback(), eback() + off, egptr());
|
|
||||||
return gptr() - eback();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
MemoryStreamRead::MemoryStreamRead(const void* p, const std::size_t n)
|
|
||||||
: std::istream(new MemoryStreamBuf((void*)p, n))
|
|
||||||
{}
|
|
||||||
|
|
||||||
MemoryStreamRead::~MemoryStreamRead(){
|
|
||||||
delete rdbuf();
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
#include <iostream>
|
|
||||||
|
|
||||||
class MemoryStreamRead : public std::istream {
|
|
||||||
public:
|
|
||||||
MemoryStreamRead(const void* p, const std::size_t n);
|
|
||||||
|
|
||||||
virtual ~MemoryStreamRead();
|
|
||||||
};
|
|
||||||
@@ -1,64 +0,0 @@
|
|||||||
////////////////////////////////////////////////////////////////
|
|
||||||
// This file was generated by resource_embedder //
|
|
||||||
// https://timerix.ddns.net:3322/Timerix/resource_embedder //
|
|
||||||
////////////////////////////////////////////////////////////////
|
|
||||||
// USAGE: //
|
|
||||||
// Put it in a SOURCE file to define variables //
|
|
||||||
// #define EMBEDDED_RESOURCE_DEFINITION //
|
|
||||||
// #define EMBEDDED_RESOURCE_POSTFIX your_postfix //
|
|
||||||
// #include "../../src/Resources/embedded_resources.h" //
|
|
||||||
// //
|
|
||||||
// Put it in a HEADER file to declare external variables //
|
|
||||||
// #define EMBEDDED_RESOURCE_POSTFIX your_postfix //
|
|
||||||
// #include "../../src/Resources/embedded_resources.h" //
|
|
||||||
// //
|
|
||||||
// Then you can access embedded files through //
|
|
||||||
// EmbeddedResource_table_your_postfix. You can get table //
|
|
||||||
// content by index and put it into a hashtable or a map. //
|
|
||||||
////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
#if __cplusplus
|
|
||||||
extern "C" {
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
const char* path;
|
|
||||||
const char* data;
|
|
||||||
size_t size;
|
|
||||||
} EmbeddedResource;
|
|
||||||
|
|
||||||
#define RSCAT(A,B,C...) A##B##C
|
|
||||||
#ifdef EMBEDDED_RESOURCE_POSTFIX
|
|
||||||
#define _EmbeddedResource_table(P) \
|
|
||||||
RSCAT(EmbeddedResource_table_, P)
|
|
||||||
#define _EmbeddedResource_table_count(P) \
|
|
||||||
RSCAT(EmbeddedResource_table_, P, _count)
|
|
||||||
#else
|
|
||||||
#define _EmbeddedResource_table(P) \
|
|
||||||
EmbeddedResource_table
|
|
||||||
#define _EmbeddedResource_table_count(P) \
|
|
||||||
EmbeddedResource_table_count
|
|
||||||
#endif
|
|
||||||
extern const EmbeddedResource _EmbeddedResource_table(EMBEDDED_RESOURCE_POSTFIX)[];
|
|
||||||
extern const int _EmbeddedResource_table_count(EMBEDDED_RESOURCE_POSTFIX);
|
|
||||||
|
|
||||||
#ifdef EMBEDDED_RESOURCE_DEFINITION
|
|
||||||
const EmbeddedResource _EmbeddedResource_table(EMBEDDED_RESOURCE_POSTFIX)[]={
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
const int _EmbeddedResource_table_count(EMBEDDED_RESOURCE_POSTFIX)=0;
|
|
||||||
#endif // EMBEDDED_RESOURCE_DEFINITION
|
|
||||||
|
|
||||||
#undef _EmbeddedResource_table
|
|
||||||
#undef _EmbeddedResource_table_count
|
|
||||||
#undef EMBEDDED_RESOURCE_POSTFIX
|
|
||||||
#undef RSCAT
|
|
||||||
|
|
||||||
#if __cplusplus
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
#include "fonts.hpp"
|
|
||||||
|
|
||||||
namespace ougge::resources {
|
|
||||||
|
|
||||||
// select all glyphs from font
|
|
||||||
static const ImWchar glyph_ranges[] = {
|
|
||||||
0x0020, 0xFFFF, 0
|
|
||||||
};
|
|
||||||
|
|
||||||
ImFont* ImFont_LoadFromFile(const std::string& file_path, f32 font_size, f32 dpi){
|
|
||||||
ImGuiIO& io = ImGui::GetIO();
|
|
||||||
font_size *= dpi;
|
|
||||||
return io.Fonts->AddFontFromFileTTF(file_path.c_str(), font_size, nullptr, glyph_ranges);
|
|
||||||
}
|
|
||||||
|
|
||||||
ImFont* ImFont_LoadFromResource(ResourceFactory* res, f32 font_size, f32 dpi){
|
|
||||||
ImGuiIO& io = ImGui::GetIO();
|
|
||||||
font_size *= dpi;
|
|
||||||
ImFontConfig font_cfg = ImFontConfig();
|
|
||||||
std::sprintf(font_cfg.Name, "%s %ipx", res->path.c_str(), (i32)font_size);
|
|
||||||
|
|
||||||
char* font_data = new char[res->size];
|
|
||||||
res->openStream()->read(font_data, res->size);
|
|
||||||
|
|
||||||
return io.Fonts->AddFontFromMemoryTTF((void*)(font_data), res->size,
|
|
||||||
font_size, &font_cfg, glyph_ranges);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <imgui.h>
|
|
||||||
#include "../common/std.hpp"
|
|
||||||
#include "resources.hpp"
|
|
||||||
|
|
||||||
namespace ougge::resources {
|
|
||||||
|
|
||||||
ImFont* ImFont_LoadFromFile(const std::string& file_path, f32 font_size, f32 dpi);
|
|
||||||
|
|
||||||
ImFont* ImFont_LoadFromResource(ResourceFactory* res, f32 font_size, f32 dpi);
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -15,7 +15,7 @@ class UsefulException_ : public std::exception {
|
|||||||
public:
|
public:
|
||||||
UsefulException_(const std::string& msg, const std::string& _file, const std::string& _func, int line_n);
|
UsefulException_(const std::string& msg, const std::string& _file, const std::string& _func, int line_n);
|
||||||
|
|
||||||
virtual char const* what() const noexcept;
|
char const* what() const noexcept override;
|
||||||
};
|
};
|
||||||
|
|
||||||
#define useful_assert(EXPR, ERRMSG) if(!(EXPR)) throw UsefulException(ERRMSG);
|
#define useful_assert(EXPR, ERRMSG) if(!(EXPR)) throw UsefulException(ERRMSG);
|
||||||
|
|||||||
@@ -30,13 +30,12 @@ public:
|
|||||||
|
|
||||||
bool isNull() const { return func_ptr == nullptr; }
|
bool isNull() const { return func_ptr == nullptr; }
|
||||||
|
|
||||||
//TODO: make inline
|
|
||||||
|
|
||||||
// ReturnT is not void
|
// ReturnT is not void
|
||||||
template<typename RT = ReturnT>
|
template<typename RT = ReturnT>
|
||||||
std::enable_if_t<!std::is_void<RT>::value, RT> operator()(ArgTypes... args){
|
std::enable_if_t<!std::is_void<RT>::value, RT> operator()(ArgTypes... args){
|
||||||
if(func_ptr == nullptr)
|
if(func_ptr == nullptr)
|
||||||
throw UsefulException("function_shared_ptr is null");
|
throw UsefulException("function_shared_ptr is null");
|
||||||
|
//TODO: think about std::forward(args)...
|
||||||
return func_ptr->operator()(args...);
|
return func_ptr->operator()(args...);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ typedef double f64;
|
|||||||
/// anonymous pointer without specified freeMembers() func
|
/// anonymous pointer without specified freeMembers() func
|
||||||
typedef void* Pointer;
|
typedef void* Pointer;
|
||||||
|
|
||||||
#define nameof(V) #V
|
|
||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
#pragma comment(lib, "mincore_downlevel.lib") // Support OS older than SDK
|
#pragma comment(lib, "mincore_downlevel.lib") // Support OS older than SDK
|
||||||
|
|||||||
44
src/common/type_name_demangled.hpp
Normal file
44
src/common/type_name_demangled.hpp
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
// https://stackoverflow.com/a/53865723
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#ifndef _MSC_VER
|
||||||
|
#include <cxxabi.h>
|
||||||
|
#endif
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
template <class T>
|
||||||
|
std::string _ougge_type_name_demangled()
|
||||||
|
{
|
||||||
|
typedef typename std::remove_reference<T>::type TR;
|
||||||
|
const char* type_name_mangled = typeid(TR).name();
|
||||||
|
std::unique_ptr<char, void(*)(void*)> own
|
||||||
|
(
|
||||||
|
#ifndef _MSC_VER
|
||||||
|
abi::__cxa_demangle(type_name_mangled, nullptr, nullptr, nullptr),
|
||||||
|
#else
|
||||||
|
nullptr,
|
||||||
|
#endif
|
||||||
|
std::free
|
||||||
|
);
|
||||||
|
std::string r = own ? own.get() : type_name_mangled;
|
||||||
|
|
||||||
|
if (std::is_const<TR>::value)
|
||||||
|
r += " const";
|
||||||
|
if (std::is_volatile<TR>::value)
|
||||||
|
r += " volatile";
|
||||||
|
if (std::is_lvalue_reference<T>::value)
|
||||||
|
r += "&";
|
||||||
|
else if (std::is_rvalue_reference<T>::value)
|
||||||
|
r += "&&";
|
||||||
|
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// @return human-readable type name
|
||||||
|
template <class T>
|
||||||
|
const std::string& ougge_type_name(){
|
||||||
|
static std::string name = _ougge_type_name_demangled<T>();
|
||||||
|
return name;
|
||||||
|
}
|
||||||
@@ -94,17 +94,20 @@ std::pair<u32, GameObject&> GameObjectPool::emplace(GameObject&& new_obj)
|
|||||||
return std::pair<u32, GameObject&>(i, r);
|
return std::pair<u32, GameObject&>(i, r);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GameObjectPool::erase(u32 index)
|
bool GameObjectPool::erase(u32 index)
|
||||||
{
|
{
|
||||||
if(index >= size)
|
if(index >= size)
|
||||||
throw UsefulException(ougge_format("index %i is out of size %i", index, size));
|
throw UsefulException(ougge_format("index %i is out of size %i", index, size));
|
||||||
if(!isIndexUsed(index))
|
if(!isIndexUsed(index)){
|
||||||
throw UsefulException(ougge_format("there is no object at index %i", index));
|
// throw UsefulException(ougge_format("there is no object at index %i", index));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
buffer[index] = GameObject();
|
buffer[index] = GameObject();
|
||||||
used_indices[index/64] &= ~(u64(1)<<(index%64)); // mark index bit as unused
|
used_indices[index/64] &= ~(u64(1)<<(index%64)); // mark index bit as unused
|
||||||
if(index < first_unused_index)
|
if(index < first_unused_index)
|
||||||
first_unused_index = index;
|
first_unused_index = index;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
GameObjectPool::iterator::iterator(GameObjectPool* pool, u32 index)
|
GameObjectPool::iterator::iterator(GameObjectPool* pool, u32 index)
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ public:
|
|||||||
~GameObjectPool();
|
~GameObjectPool();
|
||||||
GameObject& get(u32 index);
|
GameObject& get(u32 index);
|
||||||
std::pair<u32, GameObject&> emplace(GameObject&& new_obj);
|
std::pair<u32, GameObject&> emplace(GameObject&& new_obj);
|
||||||
void erase(u32 index);
|
bool erase(u32 index);
|
||||||
|
|
||||||
#pragma region iterator class
|
#pragma region iterator class
|
||||||
class iterator {
|
class iterator {
|
||||||
|
|||||||
@@ -1,11 +0,0 @@
|
|||||||
#include "gui.hpp"
|
|
||||||
|
|
||||||
namespace ougge::gui {
|
|
||||||
|
|
||||||
void drawErrorWindow(const std::string& msg, bool* draw_error_window){
|
|
||||||
ImGui::Begin("ERROR", draw_error_window);
|
|
||||||
ImGui::Text("%s", msg.c_str());
|
|
||||||
ImGui::End();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,220 +0,0 @@
|
|||||||
#include <backends/imgui_impl_sdl2.h>
|
|
||||||
#include <backends/imgui_impl_sdlrenderer2.h>
|
|
||||||
#include <iostream>
|
|
||||||
#include "MainWindow.hpp"
|
|
||||||
#include "gui_exceptions.hpp"
|
|
||||||
#include "../common/ougge_format.hpp"
|
|
||||||
#include "../common/math.hpp"
|
|
||||||
|
|
||||||
namespace ougge::gui {
|
|
||||||
|
|
||||||
f32 MainWindow::getDPI(){
|
|
||||||
i32 w=0, h=0;
|
|
||||||
SDL_GetRendererOutputSize(sdl_renderer, &w, &h);
|
|
||||||
i32 sim_w=0, sim_h=0;
|
|
||||||
SDL_GetWindowSize(sdl_window, &sim_w, &sim_h);
|
|
||||||
f32 wdpi=(f32)w / sim_w;
|
|
||||||
f32 hdpi=(f32)h / sim_h;
|
|
||||||
f32 dpi=SDL_sqrtf(wdpi*wdpi + hdpi*hdpi);
|
|
||||||
return dpi;
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainWindow::open(const std::string& window_title, resources::ResourceManager& resourceManager){
|
|
||||||
SDL_TRY(SDL_Init(SDL_INIT_EVERYTHING));
|
|
||||||
SDL_version v;
|
|
||||||
SDL_GetVersion(&v);
|
|
||||||
std::cout<<ougge_format("SDL version: %u.%u.%u\n", v.major, v.minor, v.patch);
|
|
||||||
|
|
||||||
// From 2.0.18: Enable native IME.
|
|
||||||
#ifdef SDL_HINT_IME_SHOW_UI
|
|
||||||
SDL_SetHint(SDL_HINT_IME_SHOW_UI, "1");
|
|
||||||
#endif
|
|
||||||
|
|
||||||
SDL_WindowFlags window_flags = (SDL_WindowFlags)(
|
|
||||||
SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI
|
|
||||||
);
|
|
||||||
sdl_window = SDL_CreateWindow(window_title.c_str(),
|
|
||||||
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
|
||||||
1280, 720, window_flags);
|
|
||||||
if(sdl_window == nullptr)
|
|
||||||
throw SDLException();
|
|
||||||
|
|
||||||
SDL_RendererFlags renderer_flags = (SDL_RendererFlags)(
|
|
||||||
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC
|
|
||||||
);
|
|
||||||
sdl_renderer = SDL_CreateRenderer(sdl_window, -1, renderer_flags);
|
|
||||||
if(sdl_renderer == nullptr)
|
|
||||||
throw SDLException();
|
|
||||||
|
|
||||||
SDL_RendererInfo info;
|
|
||||||
SDL_GetRendererInfo(sdl_renderer, &info);
|
|
||||||
std::cout<<"Current SDL_Renderer driver: "<<info.name<<std::endl;
|
|
||||||
|
|
||||||
// Setup Dear ImGui context
|
|
||||||
IMGUI_CHECKVERSION();
|
|
||||||
ImGui::CreateContext();
|
|
||||||
ImGuiIO& io = ImGui::GetIO();
|
|
||||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
|
|
||||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
|
|
||||||
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; // Enable Docking
|
|
||||||
io.ConfigDockingTransparentPayload = true;
|
|
||||||
io.ConfigInputTextCursorBlink = false;
|
|
||||||
// io.ConfigViewportsNoTaskBarIcon = true;
|
|
||||||
io.ConfigWindowsMoveFromTitleBarOnly = true;
|
|
||||||
|
|
||||||
// Setup Platform/Renderer backends
|
|
||||||
if(!ImGui_ImplSDL2_InitForSDLRenderer(sdl_window, sdl_renderer))
|
|
||||||
throw SDLException();
|
|
||||||
if(!ImGui_ImplSDLRenderer2_Init(sdl_renderer))
|
|
||||||
throw SDLException();
|
|
||||||
|
|
||||||
// Setup Dear ImGui style
|
|
||||||
ImGui::StyleColorsDark();
|
|
||||||
|
|
||||||
resources::ResourceFactory* font_res = resourceManager.tryGetResource(default_font_path);
|
|
||||||
if(font_res == nullptr)
|
|
||||||
throw UsefulException("can't find default font resource");
|
|
||||||
f32 dpi = getDPI();
|
|
||||||
io.FontDefault = resources::ImFont_LoadFromResource(font_res, default_font_size, dpi);
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainWindow::close(){
|
|
||||||
ImGui_ImplSDLRenderer2_Shutdown();
|
|
||||||
ImGui_ImplSDL2_Shutdown();
|
|
||||||
ImGui::DestroyContext();
|
|
||||||
SDL_DestroyRenderer(sdl_renderer);
|
|
||||||
SDL_DestroyWindow(sdl_window);
|
|
||||||
SDL_Quit();
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainWindow::pollEvents(bool* loopRunning){
|
|
||||||
SDL_Event event;
|
|
||||||
while (SDL_PollEvent(&event)) {
|
|
||||||
ImGui_ImplSDL2_ProcessEvent(&event);
|
|
||||||
switch(event.type){
|
|
||||||
case SDL_QUIT: {
|
|
||||||
*loopRunning = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case SDL_WINDOWEVENT: {
|
|
||||||
if(event.window.event == SDL_WINDOWEVENT_CLOSE
|
|
||||||
&& event.window.windowID == SDL_GetWindowID(sdl_window))
|
|
||||||
{
|
|
||||||
*loopRunning = false;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
void MainWindow::beginFrame(){
|
|
||||||
// Start the Dear ImGui frame
|
|
||||||
ImGui_ImplSDLRenderer2_NewFrame();
|
|
||||||
ImGui_ImplSDL2_NewFrame();
|
|
||||||
ImGui::NewFrame();
|
|
||||||
|
|
||||||
ImGuiIO& io = ImGui::GetIO();
|
|
||||||
SDL_RenderSetScale(sdl_renderer, io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y);
|
|
||||||
SDL_SetRenderDrawColor(sdl_renderer,
|
|
||||||
(u8)(clear_color.x * 255),
|
|
||||||
(u8)(clear_color.y * 255),
|
|
||||||
(u8)(clear_color.z * 255),
|
|
||||||
(u8)(clear_color.w * 255));
|
|
||||||
SDL_RenderClear(sdl_renderer);
|
|
||||||
|
|
||||||
draw_bg_window();
|
|
||||||
draw_debug_window();
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainWindow::endFrame(){
|
|
||||||
ImGui::Render();
|
|
||||||
ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData(), sdl_renderer);
|
|
||||||
// Swap buffers
|
|
||||||
SDL_RenderPresent(sdl_renderer);
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainWindow::draw_bg_window(){
|
|
||||||
const ImGuiDockNodeFlags dockspace_flags =
|
|
||||||
ImGuiDockNodeFlags_PassthruCentralNode;
|
|
||||||
const ImGuiWindowFlags window_flags =
|
|
||||||
ImGuiWindowFlags_MenuBar |
|
|
||||||
ImGuiWindowFlags_NoDocking |
|
|
||||||
ImGuiWindowFlags_NoScrollbar |
|
|
||||||
ImGuiWindowFlags_NoScrollWithMouse |
|
|
||||||
ImGuiWindowFlags_NoBringToFrontOnFocus |
|
|
||||||
ImGuiWindowFlags_NoFocusOnAppearing |
|
|
||||||
ImGuiWindowFlags_NoMove |
|
|
||||||
ImGuiWindowFlags_NoResize |
|
|
||||||
ImGuiWindowFlags_NoCollapse |
|
|
||||||
ImGuiWindowFlags_NoTitleBar |
|
|
||||||
ImGuiWindowFlags_NoBackground;
|
|
||||||
// not dockable window that always bound to viewport
|
|
||||||
ImGuiViewport* viewport = ImGui::GetWindowViewport();
|
|
||||||
ImGui::SetNextWindowPos(viewport->Pos, ImGuiCond_Always);
|
|
||||||
ImGui::SetNextWindowSize(viewport->Size, ImGuiCond_Always);
|
|
||||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowRounding, 0.0f);
|
|
||||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
|
|
||||||
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
|
|
||||||
ImGui::Begin("bg_window", nullptr, window_flags);
|
|
||||||
ImGui::PopStyleVar(3);
|
|
||||||
|
|
||||||
// DockSpace
|
|
||||||
ImGuiID dockspace_id = ImGui::GetID("bg_dockspace");
|
|
||||||
ImGui::DockSpace(dockspace_id, ImVec2(0.0f, 0.0f), dockspace_flags);
|
|
||||||
|
|
||||||
// MenuBar
|
|
||||||
if(ImGui::BeginMainMenuBar()){
|
|
||||||
if(ImGui::BeginMenu("test")){
|
|
||||||
if(ImGui::MenuItem("throw exception")){
|
|
||||||
ImGui::EndMenu();
|
|
||||||
ImGui::EndMainMenuBar();
|
|
||||||
ImGui::End();
|
|
||||||
throw UsefulException("example exception");
|
|
||||||
}
|
|
||||||
if(ImGui::MenuItem("throw const char*")){
|
|
||||||
ImGui::EndMenu();
|
|
||||||
ImGui::EndMainMenuBar();
|
|
||||||
ImGui::End();
|
|
||||||
throw "cptr";
|
|
||||||
}
|
|
||||||
if(ImGui::MenuItem("throw std::string")){
|
|
||||||
ImGui::EndMenu();
|
|
||||||
ImGui::EndMainMenuBar();
|
|
||||||
ImGui::End();
|
|
||||||
throw std::string("str");
|
|
||||||
}
|
|
||||||
if(ImGui::MenuItem("throw unknown")){
|
|
||||||
ImGui::EndMenu();
|
|
||||||
ImGui::EndMainMenuBar();
|
|
||||||
ImGui::End();
|
|
||||||
throw 111;
|
|
||||||
}
|
|
||||||
ImGui::EndMenu();
|
|
||||||
}
|
|
||||||
ImGui::EndMainMenuBar();
|
|
||||||
}
|
|
||||||
|
|
||||||
ImGui::End();
|
|
||||||
}
|
|
||||||
|
|
||||||
void MainWindow::draw_debug_window(){
|
|
||||||
ImGuiIO& io = ImGui::GetIO();
|
|
||||||
if(ImGui::Begin("Debug Options", &show_debug_window)){
|
|
||||||
ImGui::ColorEdit3("clear_color", (float*)&clear_color);
|
|
||||||
ImGui::InputInt("fps_max", &fps_max);
|
|
||||||
ImGui::Text("Application average %.3f ms/frame (%.2f FPS)", 1000.0f / io.Framerate, io.Framerate);
|
|
||||||
ImGui::Checkbox("Demo Window", &show_demo_window);
|
|
||||||
ImGui::Checkbox("Metrics/Debug Window", &show_metrics_window);
|
|
||||||
ImGui::End();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (show_demo_window)
|
|
||||||
ImGui::ShowDemoWindow(&show_demo_window);
|
|
||||||
|
|
||||||
if (show_metrics_window)
|
|
||||||
ImGui::ShowMetricsWindow(&show_metrics_window);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,47 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <SDL.h>
|
|
||||||
#include <imgui.h>
|
|
||||||
#include "../common/std.hpp"
|
|
||||||
#include "../common/time.hpp"
|
|
||||||
#include "../resources/resources.hpp"
|
|
||||||
#include "../resources/fonts.hpp"
|
|
||||||
|
|
||||||
/// converts hex color to float vector
|
|
||||||
#define RGBAHexToF(R8,G8,B8,A8) ImVec4(((u8)35)/255.0f, ((u8)35)/255.0f, ((u8)50)/255.0f, ((u8)255)/255.0f)
|
|
||||||
/// converts float vector to hex color
|
|
||||||
#define RGBAFToHex(VEC4) {(u8)(VEC4.x*255), (u8)(VEC4.y*255), (u8)(VEC4.z*255), (u8)(VEC4.w*255)}
|
|
||||||
|
|
||||||
namespace ougge::gui {
|
|
||||||
|
|
||||||
#define default_font_path "fonts/DroidSans.ttf"
|
|
||||||
|
|
||||||
class MainWindow {
|
|
||||||
public:
|
|
||||||
i32 fps_max = 60;
|
|
||||||
f32 default_font_size = 14.0f;
|
|
||||||
ImVec4 clear_color = RGBAHexToF(35,35,50,255);
|
|
||||||
SDL_Window* sdl_window = nullptr;
|
|
||||||
SDL_Renderer* sdl_renderer = nullptr;
|
|
||||||
|
|
||||||
private:
|
|
||||||
bool show_debug_window = true;
|
|
||||||
bool show_demo_window = false;
|
|
||||||
bool show_metrics_window = false;
|
|
||||||
|
|
||||||
public:
|
|
||||||
void open(const std::string& window_title, resources::ResourceManager& resourceManager);
|
|
||||||
void close();
|
|
||||||
|
|
||||||
/// process io events happened since previous frame
|
|
||||||
void pollEvents(bool* loopRunning);
|
|
||||||
void beginFrame();
|
|
||||||
void endFrame();
|
|
||||||
|
|
||||||
f32 getDPI();
|
|
||||||
private:
|
|
||||||
void draw_debug_window();
|
|
||||||
void draw_bg_window();
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <SDL.h>
|
|
||||||
#include <imgui.h>
|
|
||||||
#include "gui_exceptions.hpp"
|
|
||||||
|
|
||||||
namespace ougge::gui {
|
|
||||||
|
|
||||||
void drawErrorWindow(const std::string& msg, bool* draw_error_window);
|
|
||||||
|
|
||||||
}
|
|
||||||
80
src/main.cpp
80
src/main.cpp
@@ -1,49 +1,67 @@
|
|||||||
#define SDL_MAIN_HANDLED
|
#define SDL_MAIN_HANDLED
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include "common/UsefulException.hpp"
|
|
||||||
#include "common/ougge_format.hpp"
|
|
||||||
#include "Engine.hpp"
|
#include "Engine.hpp"
|
||||||
#include "gui/gui.hpp"
|
#include "modules/MainWindowSDL2.hpp"
|
||||||
|
#include "modules/MonoGameObjectSystem.hpp"
|
||||||
|
#include "resources/textures.hpp"
|
||||||
|
|
||||||
using namespace ougge;
|
using namespace ougge;
|
||||||
|
|
||||||
void drawTutel(Engine& engine){
|
class TutelModule : public IEngineModule {
|
||||||
resources::Texture* tutel = engine.textures.tryGetOrCreate("tutel.png", engine.mainWindow.sdl_renderer);
|
resources::CacheStorage<resources::Texture> textures;
|
||||||
if(tutel == nullptr){
|
public:
|
||||||
throw new UsefulException("couldn't find resource 'tutel.png'");
|
TutelModule(Engine& engine, resources::ResourceManager& resourceManager) :
|
||||||
|
IEngineModule(engine),
|
||||||
|
textures(&resourceManager)
|
||||||
|
{
|
||||||
|
//TODO: add something like `assert(requireModule(MainWindow))`
|
||||||
}
|
}
|
||||||
resources::SDL_RenderCopyExF_Params params;
|
|
||||||
params.target_section = SDL_FRectConstruct(100, 100, 400, 400);
|
const std::string& getName() override {
|
||||||
static i32 si = 1;
|
return ougge_type_name<TutelModule>();
|
||||||
params.rotation_angle = M_PI_4 * (si++ / engine.mainWindow.fps_max);
|
}
|
||||||
tutel->render(params);
|
|
||||||
|
void beginFrame() override {
|
||||||
|
auto& mainWindow = engine.getModule<modules::MainWindowSDL2>();
|
||||||
|
resources::Texture* tutel = textures.tryGetOrCreate("tutel.png", mainWindow.sdl_renderer);
|
||||||
|
if(tutel == nullptr){
|
||||||
|
throw new UsefulException("couldn't find resource 'tutel.png'");
|
||||||
|
}
|
||||||
|
resources::SDL_RenderCopyExF_Params params;
|
||||||
|
params.target_section = SDL_FRectConstruct(100, 100, 400, 400);
|
||||||
|
static i32 si = 1;
|
||||||
|
params.rotation_angle = M_PI_4 * (si++ / engine.fps_max);
|
||||||
|
tutel->render(params);
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
void createExampleObject(Engine& engine){
|
||||||
|
std::cout<<"creating ExampleObject"<<std::endl;
|
||||||
|
auto& monoSystem = engine.getModule<modules::MonoGameObjectSystem>();
|
||||||
|
game::GameObject& exampleObj = monoSystem.createAndConstructGameObject();
|
||||||
|
std::string componentClassName = "Ougge.ExampleComponent";
|
||||||
|
if(!monoSystem.tryCreateComponent(exampleObj, componentClassName))
|
||||||
|
throw UsefulException(ougge_format("couldn't create component '%s'", componentClassName.c_str()));
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, const char** argv){
|
int main(int argc, const char** argv){
|
||||||
try {
|
try {
|
||||||
std::cout<<"initialized resource loader"<<std::endl;
|
std::cout<<"Initializing Engine"<<std::endl;
|
||||||
|
|
||||||
Engine engine;
|
Engine engine;
|
||||||
std::cout<<"initialized mono jit runtime"<<std::endl;
|
|
||||||
engine.init();
|
std::cout<<"Initializing ResourceManager"<<std::endl;
|
||||||
std::cout<<"initialized game engine"<<std::endl;
|
resources::ResourceManager resourceManager;
|
||||||
|
|
||||||
|
engine.createModule<modules::MainWindowSDL2>("ougge", resourceManager);
|
||||||
|
engine.createModule<modules::MonoGameObjectSystem>(64*1024);
|
||||||
|
createExampleObject(engine);
|
||||||
|
engine.createModule<TutelModule>(resourceManager);
|
||||||
|
|
||||||
game::GameObject& exampleObj = engine.createGameObject();
|
std::cout<<"main loop start"<<std::endl;
|
||||||
std::string componentClassName = "Ougge.ExampleComponent";
|
|
||||||
if(!engine.tryCreateComponent(exampleObj, componentClassName))
|
|
||||||
throw UsefulException(ougge_format("couldn't create component '%s'", componentClassName.c_str()));
|
|
||||||
|
|
||||||
std::cout<<"created ExampleObject"<<std::endl;
|
|
||||||
|
|
||||||
engine.updateCallback = [&engine](f64 deltaTime) -> void {
|
|
||||||
drawTutel(engine);
|
|
||||||
};
|
|
||||||
|
|
||||||
engine.openMainWindow("ougge");
|
|
||||||
std::cout<<"created sdl window"<<std::endl;
|
|
||||||
engine.startLoop();
|
engine.startLoop();
|
||||||
std::cout<<"sdl window has been cosed"<<std::endl;
|
std::cout<<"main loop stop"<<std::endl;
|
||||||
}
|
}
|
||||||
catch(const std::exception& e){
|
catch(const std::exception& e){
|
||||||
std::cerr<<"Catched exception: "<<e.what()<<std::endl;
|
std::cerr<<"Catched exception: "<<e.what()<<std::endl;
|
||||||
|
|||||||
@@ -1,14 +1,20 @@
|
|||||||
#include <backends/imgui_impl_sdl2.h>
|
#include <backends/imgui_impl_sdl2.h>
|
||||||
#include <backends/imgui_impl_sdlrenderer2.h>
|
#include <backends/imgui_impl_sdlrenderer2.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "MainWindow.hpp"
|
#include "MainWindowSDL2.hpp"
|
||||||
#include "gui_exceptions.hpp"
|
#include "../gui/gui_exceptions.hpp"
|
||||||
#include "../common/ougge_format.hpp"
|
#include "../common/ougge_format.hpp"
|
||||||
#include "../common/math.hpp"
|
#include "../common/math.hpp"
|
||||||
|
|
||||||
namespace ougge::gui {
|
using namespace ougge::gui;
|
||||||
|
|
||||||
f32 MainWindow::getDPI(){
|
namespace ougge::modules {
|
||||||
|
|
||||||
|
const std::string& MainWindowSDL2::getName() {
|
||||||
|
return ougge_type_name<MainWindowSDL2>();
|
||||||
|
}
|
||||||
|
|
||||||
|
f32 MainWindowSDL2::getDPI(){
|
||||||
i32 w=0, h=0;
|
i32 w=0, h=0;
|
||||||
SDL_GetRendererOutputSize(sdl_renderer, &w, &h);
|
SDL_GetRendererOutputSize(sdl_renderer, &w, &h);
|
||||||
i32 sim_w=0, sim_h=0;
|
i32 sim_w=0, sim_h=0;
|
||||||
@@ -19,7 +25,11 @@ f32 MainWindow::getDPI(){
|
|||||||
return dpi;
|
return dpi;
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::open(const std::string& window_title, resources::ResourceManager& resourceManager){
|
MainWindowSDL2::MainWindowSDL2(Engine& engine,
|
||||||
|
const std::string& window_title,
|
||||||
|
resources::ResourceManager& resourceManager)
|
||||||
|
: IEngineModule(engine)
|
||||||
|
{
|
||||||
SDL_TRY(SDL_Init(SDL_INIT_EVERYTHING));
|
SDL_TRY(SDL_Init(SDL_INIT_EVERYTHING));
|
||||||
SDL_version v;
|
SDL_version v;
|
||||||
SDL_GetVersion(&v);
|
SDL_GetVersion(&v);
|
||||||
@@ -78,7 +88,7 @@ void MainWindow::open(const std::string& window_title, resources::ResourceManage
|
|||||||
io.FontDefault = resources::ImFont_LoadFromResource(font_res, default_font_size, dpi);
|
io.FontDefault = resources::ImFont_LoadFromResource(font_res, default_font_size, dpi);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::close(){
|
MainWindowSDL2::~MainWindowSDL2(){
|
||||||
ImGui_ImplSDLRenderer2_Shutdown();
|
ImGui_ImplSDLRenderer2_Shutdown();
|
||||||
ImGui_ImplSDL2_Shutdown();
|
ImGui_ImplSDL2_Shutdown();
|
||||||
ImGui::DestroyContext();
|
ImGui::DestroyContext();
|
||||||
@@ -87,20 +97,22 @@ void MainWindow::close(){
|
|||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::pollEvents(bool* loopRunning){
|
void MainWindowSDL2::pollEvents(){
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
while (SDL_PollEvent(&event)) {
|
while (SDL_PollEvent(&event)) {
|
||||||
ImGui_ImplSDL2_ProcessEvent(&event);
|
ImGui_ImplSDL2_ProcessEvent(&event);
|
||||||
switch(event.type){
|
switch(event.type){
|
||||||
case SDL_QUIT: {
|
case SDL_QUIT: {
|
||||||
*loopRunning = false;
|
//TODO: log SDL_QUIT event
|
||||||
|
engine.stopLoop();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SDL_WINDOWEVENT: {
|
case SDL_WINDOWEVENT: {
|
||||||
if(event.window.event == SDL_WINDOWEVENT_CLOSE
|
if(event.window.event == SDL_WINDOWEVENT_CLOSE
|
||||||
&& event.window.windowID == SDL_GetWindowID(sdl_window))
|
&& event.window.windowID == SDL_GetWindowID(sdl_window))
|
||||||
{
|
{
|
||||||
*loopRunning = false;
|
//TODO: log SDL_WINDOWEVENT event
|
||||||
|
engine.stopLoop();
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -108,8 +120,10 @@ void MainWindow::pollEvents(bool* loopRunning){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MainWindowSDL2::beginFrame(){
|
||||||
|
// process events happend since previous frame
|
||||||
|
pollEvents();
|
||||||
|
|
||||||
void MainWindow::beginFrame(){
|
|
||||||
// Start the Dear ImGui frame
|
// Start the Dear ImGui frame
|
||||||
ImGui_ImplSDLRenderer2_NewFrame();
|
ImGui_ImplSDLRenderer2_NewFrame();
|
||||||
ImGui_ImplSDL2_NewFrame();
|
ImGui_ImplSDL2_NewFrame();
|
||||||
@@ -126,16 +140,17 @@ void MainWindow::beginFrame(){
|
|||||||
|
|
||||||
draw_bg_window();
|
draw_bg_window();
|
||||||
draw_debug_window();
|
draw_debug_window();
|
||||||
|
drawErrorWindow();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::endFrame(){
|
void MainWindowSDL2::endFrame(){
|
||||||
ImGui::Render();
|
ImGui::Render();
|
||||||
ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData(), sdl_renderer);
|
ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData(), sdl_renderer);
|
||||||
// Swap buffers
|
// Swap buffers
|
||||||
SDL_RenderPresent(sdl_renderer);
|
SDL_RenderPresent(sdl_renderer);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::draw_bg_window(){
|
void MainWindowSDL2::draw_bg_window(){
|
||||||
const ImGuiDockNodeFlags dockspace_flags =
|
const ImGuiDockNodeFlags dockspace_flags =
|
||||||
ImGuiDockNodeFlags_PassthruCentralNode;
|
ImGuiDockNodeFlags_PassthruCentralNode;
|
||||||
const ImGuiWindowFlags window_flags =
|
const ImGuiWindowFlags window_flags =
|
||||||
@@ -199,11 +214,11 @@ void MainWindow::draw_bg_window(){
|
|||||||
ImGui::End();
|
ImGui::End();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainWindow::draw_debug_window(){
|
void MainWindowSDL2::draw_debug_window(){
|
||||||
ImGuiIO& io = ImGui::GetIO();
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
if(ImGui::Begin("Debug Options", &show_debug_window)){
|
if(ImGui::Begin("Debug Options", &show_debug_window)){
|
||||||
ImGui::ColorEdit3("clear_color", (float*)&clear_color);
|
ImGui::ColorEdit3("clear_color", (float*)&clear_color);
|
||||||
ImGui::InputInt("fps_max", &fps_max);
|
ImGui::InputInt("fps_max", (int*)&engine.fps_max);
|
||||||
ImGui::Text("Application average %.3f ms/frame (%.2f FPS)", 1000.0f / io.Framerate, io.Framerate);
|
ImGui::Text("Application average %.3f ms/frame (%.2f FPS)", 1000.0f / io.Framerate, io.Framerate);
|
||||||
ImGui::Checkbox("Demo Window", &show_demo_window);
|
ImGui::Checkbox("Demo Window", &show_demo_window);
|
||||||
ImGui::Checkbox("Metrics/Debug Window", &show_metrics_window);
|
ImGui::Checkbox("Metrics/Debug Window", &show_metrics_window);
|
||||||
@@ -217,4 +232,16 @@ void MainWindow::draw_debug_window(){
|
|||||||
ImGui::ShowMetricsWindow(&show_metrics_window);
|
ImGui::ShowMetricsWindow(&show_metrics_window);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MainWindowSDL2::drawErrorWindow(){
|
||||||
|
if(engine.error_messages.size() < 1)
|
||||||
|
return;
|
||||||
|
ImGui::Begin("ERRORS");
|
||||||
|
if(ImGui::Button("Clear"))
|
||||||
|
engine.error_messages.clear();
|
||||||
|
for(auto& msg : engine.error_messages){
|
||||||
|
ImGui::TextWrapped("%s", msg.c_str());
|
||||||
|
}
|
||||||
|
ImGui::End();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -2,23 +2,23 @@
|
|||||||
|
|
||||||
#include <SDL.h>
|
#include <SDL.h>
|
||||||
#include <imgui.h>
|
#include <imgui.h>
|
||||||
|
#include <vector>
|
||||||
#include "../common/std.hpp"
|
#include "../common/std.hpp"
|
||||||
#include "../common/time.hpp"
|
|
||||||
#include "../resources/resources.hpp"
|
#include "../resources/resources.hpp"
|
||||||
#include "../resources/fonts.hpp"
|
#include "../resources/fonts.hpp"
|
||||||
|
#include "../Engine.hpp"
|
||||||
|
|
||||||
|
namespace ougge::modules {
|
||||||
|
|
||||||
/// converts hex color to float vector
|
/// converts hex color to float vector
|
||||||
#define RGBAHexToF(R8,G8,B8,A8) ImVec4(((u8)35)/255.0f, ((u8)35)/255.0f, ((u8)50)/255.0f, ((u8)255)/255.0f)
|
#define RGBAHexToF(R8,G8,B8,A8) ImVec4(((u8)35)/255.0f, ((u8)35)/255.0f, ((u8)50)/255.0f, ((u8)255)/255.0f)
|
||||||
/// converts float vector to hex color
|
/// converts float vector to hex color
|
||||||
#define RGBAFToHex(VEC4) {(u8)(VEC4.x*255), (u8)(VEC4.y*255), (u8)(VEC4.z*255), (u8)(VEC4.w*255)}
|
#define RGBAFToHex(VEC4) {(u8)(VEC4.x*255), (u8)(VEC4.y*255), (u8)(VEC4.z*255), (u8)(VEC4.w*255)}
|
||||||
|
|
||||||
namespace ougge::gui {
|
|
||||||
|
|
||||||
#define default_font_path "fonts/DroidSans.ttf"
|
#define default_font_path "fonts/DroidSans.ttf"
|
||||||
|
|
||||||
class MainWindow {
|
class MainWindowSDL2 : public IEngineModule {
|
||||||
public:
|
public:
|
||||||
i32 fps_max = 60;
|
|
||||||
f32 default_font_size = 14.0f;
|
f32 default_font_size = 14.0f;
|
||||||
ImVec4 clear_color = RGBAHexToF(35,35,50,255);
|
ImVec4 clear_color = RGBAHexToF(35,35,50,255);
|
||||||
SDL_Window* sdl_window = nullptr;
|
SDL_Window* sdl_window = nullptr;
|
||||||
@@ -30,18 +30,20 @@ private:
|
|||||||
bool show_metrics_window = false;
|
bool show_metrics_window = false;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void open(const std::string& window_title, resources::ResourceManager& resourceManager);
|
MainWindowSDL2(Engine& engine, const std::string& window_title,
|
||||||
void close();
|
resources::ResourceManager& resourceManager);
|
||||||
|
~MainWindowSDL2();
|
||||||
|
|
||||||
/// process io events happened since previous frame
|
const std::string& getName() override;
|
||||||
void pollEvents(bool* loopRunning);
|
void beginFrame() override;
|
||||||
void beginFrame();
|
void endFrame() override;
|
||||||
void endFrame();
|
|
||||||
|
|
||||||
f32 getDPI();
|
f32 getDPI();
|
||||||
private:
|
private:
|
||||||
|
void pollEvents();
|
||||||
void draw_debug_window();
|
void draw_debug_window();
|
||||||
void draw_bg_window();
|
void draw_bg_window();
|
||||||
|
void drawErrorWindow();
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
76
src/modules/MonoGameObjectSystem.cpp
Normal file
76
src/modules/MonoGameObjectSystem.cpp
Normal file
@@ -0,0 +1,76 @@
|
|||||||
|
#include "MonoGameObjectSystem.hpp"
|
||||||
|
|
||||||
|
namespace ougge::modules {
|
||||||
|
|
||||||
|
const std::string& MonoGameObjectSystem::getName() {
|
||||||
|
return ougge_type_name<MonoGameObjectSystem>();
|
||||||
|
}
|
||||||
|
|
||||||
|
MonoGameObjectSystem::MonoGameObjectSystem(Engine& engine, u32 max_game_objects) :
|
||||||
|
IEngineModule(engine),
|
||||||
|
gameObjectPool(max_game_objects)
|
||||||
|
{
|
||||||
|
engineManagedAssembly = mono.loadAssembly("Ougge.dll");
|
||||||
|
gameObjectClass = engineManagedAssembly->getClass("Ougge", "GameObject");
|
||||||
|
gameObjectCtor = Mono::Method<void(u64, u32)>(gameObjectClass, ".ctor");
|
||||||
|
gameObjectInvokeUpdate = Mono::Method<void(f64)>(gameObjectClass, "InvokeUpdate");
|
||||||
|
gameObjectTryCreateComponent = Mono::Method<Mono::Bool(Mono::String)>(gameObjectClass, "TryCreateComponent_internal");
|
||||||
|
registerNativeCallbacks();
|
||||||
|
}
|
||||||
|
|
||||||
|
// sets implementations for extern functions from src-csharp/NativeFunctions.cs
|
||||||
|
void MonoGameObjectSystem::registerNativeCallbacks(){
|
||||||
|
static MonoGameObjectSystem* this_static = this;
|
||||||
|
if(this_static != this)
|
||||||
|
throw new UsefulException("creation of more than one instance of MonoGameObjectSystem is not allowed");
|
||||||
|
|
||||||
|
auto createGameObject_callback = static_cast<void(*)(u64*, u32*)> (
|
||||||
|
[](u64* id_out, u32* index_out) -> void {
|
||||||
|
std::cout<<"createGameObject_callback"<<std::endl;
|
||||||
|
this_static->createGameObjectInPool(id_out, index_out);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
mono_add_internal_call("Ougge.NativeFunctions::createGameObject", (void*)createGameObject_callback);
|
||||||
|
|
||||||
|
auto freeGameObject_callback = static_cast<bool(*)(u32)>(
|
||||||
|
[](u32 index) -> bool {
|
||||||
|
std::cout<<"freeGameObject_callback"<<std::endl;
|
||||||
|
return this_static->gameObjectPool.erase(index);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
mono_add_internal_call("Ougge.NativeFunctions::freeGameObject", (void*)freeGameObject_callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MonoGameObjectSystem::beginFrame(){
|
||||||
|
for(auto pair : gameObjectPool){
|
||||||
|
|
||||||
|
auto obj = pair.second.getObjectHandle().getObject();
|
||||||
|
std::cout<<"updating obj: "<<obj<<std::endl;
|
||||||
|
gameObjectInvokeUpdate(obj, engine.deltaTime);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// is used in NativeFunctions.cs
|
||||||
|
game::GameObject& MonoGameObjectSystem::createGameObjectInPool(u64* id_out, u32* index_out){
|
||||||
|
auto pair = gameObjectPool.emplace(game::GameObject(mono.createObject(gameObjectClass)));
|
||||||
|
*id_out = ++obj_id;
|
||||||
|
*index_out = pair.first;
|
||||||
|
game::GameObject& obj = pair.second;
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
game::GameObject& MonoGameObjectSystem::createAndConstructGameObject(){
|
||||||
|
u64 obj_id;
|
||||||
|
u32 obj_index;
|
||||||
|
game::GameObject& obj = createGameObjectInPool(&obj_id, &obj_index);
|
||||||
|
gameObjectCtor(obj.getObjectHandle().getObject(), obj_id, obj_index);
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool MonoGameObjectSystem::tryCreateComponent(game::GameObject& obj, const std::string& componentClassName){
|
||||||
|
Mono::String componentClassNameManaged = mono.createString(componentClassName);
|
||||||
|
Mono::Bool created = gameObjectTryCreateComponent(obj.getObjectHandle().getObject(), componentClassNameManaged);
|
||||||
|
return created.wide_bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
35
src/modules/MonoGameObjectSystem.hpp
Normal file
35
src/modules/MonoGameObjectSystem.hpp
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "../Engine.hpp"
|
||||||
|
#include "../common/function_shared_ptr.hpp"
|
||||||
|
#include "../mono/mono.hpp"
|
||||||
|
#include "../game/GameObjectPool.hpp"
|
||||||
|
|
||||||
|
namespace ougge::modules {
|
||||||
|
|
||||||
|
class MonoGameObjectSystem : public IEngineModule {
|
||||||
|
Mono::RuntimeJIT mono;
|
||||||
|
game::GameObjectPool gameObjectPool;
|
||||||
|
u64 obj_id = 0;
|
||||||
|
MonoClass* gameObjectClass;
|
||||||
|
Mono::Method<void(u64, u32)> gameObjectCtor;
|
||||||
|
Mono::Method<void(f64)> gameObjectInvokeUpdate;
|
||||||
|
Mono::Method<Mono::Bool(Mono::String)> gameObjectTryCreateComponent;
|
||||||
|
|
||||||
|
public:
|
||||||
|
std::shared_ptr<Mono::Assembly> engineManagedAssembly;
|
||||||
|
|
||||||
|
MonoGameObjectSystem(Engine& engine, u32 max_game_objects);
|
||||||
|
|
||||||
|
const std::string& getName() override;
|
||||||
|
void beginFrame() override;
|
||||||
|
|
||||||
|
game::GameObject& createAndConstructGameObject();
|
||||||
|
bool tryCreateComponent(game::GameObject& obj, const std::string& componentClassName);
|
||||||
|
|
||||||
|
private:
|
||||||
|
void registerNativeCallbacks();
|
||||||
|
game::GameObject& createGameObjectInPool(u64* id_out, u32* index_out);
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
@@ -4,10 +4,10 @@ class MemoryStreamBuf : public std::streambuf {
|
|||||||
public:
|
public:
|
||||||
MemoryStreamBuf(void* p, const std::size_t n);
|
MemoryStreamBuf(void* p, const std::size_t n);
|
||||||
|
|
||||||
virtual std::istream::pos_type seekoff(
|
std::istream::pos_type seekoff(
|
||||||
std::istream::off_type off,
|
std::istream::off_type off,
|
||||||
std::ios_base::seekdir dir,
|
std::ios_base::seekdir dir,
|
||||||
std::ios_base::openmode which);
|
std::ios_base::openmode which) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
MemoryStreamBuf::MemoryStreamBuf(void* _p, const std::size_t n){
|
MemoryStreamBuf::MemoryStreamBuf(void* _p, const std::size_t n){
|
||||||
|
|||||||
@@ -23,6 +23,7 @@ public:
|
|||||||
|
|
||||||
using ResourceMap = std::unordered_map<std::string, ResourceFactory>;
|
using ResourceMap = std::unordered_map<std::string, ResourceFactory>;
|
||||||
|
|
||||||
|
//TODO: rewrite ResourceManager as a module
|
||||||
class ResourceManager {
|
class ResourceManager {
|
||||||
std::list<std::shared_ptr<ResourceMap>> _resourceMaps;
|
std::list<std::shared_ptr<ResourceMap>> _resourceMaps;
|
||||||
static std::shared_ptr<ResourceMap> _embeddedResourcesMap;
|
static std::shared_ptr<ResourceMap> _embeddedResourcesMap;
|
||||||
|
|||||||
Reference in New Issue
Block a user