Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dmSDK: Added possibility to load collection proxies and spawn factories from C++ #8667

Merged
merged 14 commits into from
May 20, 2024
32 changes: 30 additions & 2 deletions engine/engine/src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
// Copyright 2009-2014 Ragnar Svensson, Christian Murray
// Licensed under the Defold License version 1.0 (the "License"); you may not use
// this file except in compliance with the License.
//
//
// You may obtain a copy of the License, together with FAQs at
// https://www.defold.com/license
//
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
Expand Down Expand Up @@ -274,6 +274,20 @@ namespace dmEngine

void Delete(HEngine engine)
{
{
dmExtension::Params params;
params.m_ConfigFile = engine->m_Config;
params.m_ResourceFactory = engine->m_Factory;
if (engine->m_SharedScriptContext) {
params.m_L = dmScript::GetLuaState(engine->m_SharedScriptContext);
} else {
params.m_L = dmScript::GetLuaState(engine->m_GOScriptContext);
}
dmExtension::Event event;
event.m_Event = dmExtension::EVENT_ID_ENGINE_DELETE;
dmExtension::DispatchEvent( &params, &event );
}
Comment on lines +277 to +289
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New feature:
Since the extension system isn't aware of the game loop as such, I introduced two new events EVENT_ID_ENGINE_DELETE and EVENT_ID_ENGINE_INITIALIZED so that the extension can act upon it.

I.e. this helps with things like: don't load things before the game has been initialized, and don't delete your things too late.


if (engine->m_MainCollection)
dmResource::Release(engine->m_Factory, engine->m_MainCollection);
dmGameObject::PostUpdate(engine->m_Register);
Expand Down Expand Up @@ -1392,6 +1406,20 @@ namespace dmEngine
dmEngineService::InitProfiler(engine->m_EngineService, engine->m_Factory, engine->m_Register);
}

{
dmExtension::Params params;
params.m_ConfigFile = engine->m_Config;
params.m_ResourceFactory = engine->m_Factory;
if (engine->m_SharedScriptContext) {
params.m_L = dmScript::GetLuaState(engine->m_SharedScriptContext);
} else {
params.m_L = dmScript::GetLuaState(engine->m_GOScriptContext);
}
dmExtension::Event event;
event.m_Event = dmExtension::EVENT_ID_ENGINE_INITIALIZED;
dmExtension::DispatchEvent( &params, &event );
}

engine->m_PreviousFrameTime = dmTime::GetTime();

return true;
Expand Down
7 changes: 5 additions & 2 deletions engine/extension/src/dmsdk/extension/extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
// Copyright 2009-2014 Ragnar Svensson, Christian Murray
// Licensed under the Defold License version 1.0 (the "License"); you may not use
// this file except in compliance with the License.
//
//
// You may obtain a copy of the License, together with FAQs at
// https://www.defold.com/license
//
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
Expand Down Expand Up @@ -118,6 +118,9 @@ namespace dmExtension
EVENT_ID_DEACTIVATEAPP,
EVENT_ID_ICONIFYAPP,
EVENT_ID_DEICONIFYAPP,

EVENT_ID_ENGINE_INITIALIZED,// After Init() has been run, before first Step()
EVENT_ID_ENGINE_DELETE, // At the top of the Delete() of the engine
};

/*# extra callback enumeration
Expand Down
8 changes: 4 additions & 4 deletions engine/gameobject/src/dmsdk/gameobject/component.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
// Copyright 2009-2014 Ragnar Svensson, Christian Murray
// Licensed under the Defold License version 1.0 (the "License"); you may not use
// this file except in compliance with the License.
//
//
// You may obtain a copy of the License, together with FAQs at
// https://www.defold.com/license
//
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
Expand Down Expand Up @@ -217,9 +217,9 @@ namespace dmGameObject
struct ComponentGetParams
{
/// Component world
void* m_World;
dmGameObject::HComponentWorld m_World;
/// User data storage pointer
uintptr_t* m_UserData;
dmGameObject::HComponentInternal m_UserData;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding more type safety.

};

/*#
Expand Down
77 changes: 74 additions & 3 deletions engine/gameobject/src/dmsdk/gameobject/gameobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
// Copyright 2009-2014 Ragnar Svensson, Christian Murray
// Licensed under the Defold License version 1.0 (the "License"); you may not use
// this file except in compliance with the License.
//
//
// You may obtain a copy of the License, together with FAQs at
// https://www.defold.com/license
//
//
// Unless required by applicable law or agreed to in writing, software distributed
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
// CONDITIONS OF ANY KIND, either express or implied. See the License for the
Expand Down Expand Up @@ -86,6 +86,34 @@ namespace dmGameObject
*/
typedef struct CollectionHandle* HCollection;

/*#
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved from internal header.

* Handle to a list of properties (gameobject_props.h)
* @typedef
* @name HPropertyContainer
*/
typedef struct PropertyContainer* HPropertyContainer;

/*#
* Opaque handle to component instance
* @typedef
* @name HComponent
*/
typedef void* HComponent;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A HComponent, is intended to be a fully resolved pointer, to be used with our c++ sdk's.


/*#
* Opaque handle to internal representation of a component instance
* @typedef
* @name HComponentInternal
*/
typedef uintptr_t HComponentInternal;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A HComponentInternal, may be anything that the component type needs (e.g. a uin16_t handle). It needs to be resolved (handled by our component extensions) to get an actual pointer.


/*#
* Opaque handle to a component world
* @typedef
* @name HComponentWorld
*/
typedef void* HComponentWorld;

typedef void* HCollectionDesc;

/*#
Expand Down Expand Up @@ -473,6 +501,22 @@ namespace dmGameObject
*/
dmMessage::HSocket GetMessageSocket(HCollection collection);

/*# spawn a new game object
* Spawns a new gameobject instance. The actual creation is performed after the update is completed.
* @name Spawn
* @param collection [type: HCollection] Gameobject collection
* @param prototype [type: HPrototype] Prototype
* @param prototype_name [type: const char*] Prototype file name (.goc)
* @param id [type: dmhash_t] Id of the spawned instance
* @param properties [type: HPropertyContainer] Container with override properties
* @param position [type: dmVMath::Vector3] Position of the spawed object
* @param rotation [type: dmVMath::Quat] Rotation of the spawned object
* @param scale [type: dmVMath::Vector3] Scale of the spawned object
* return instance [type: HInstance] the spawned instance, 0 at failure
*/
HInstance Spawn(HCollection collection, HPrototype prototype, const char* prototype_name, dmhash_t id,
HPropertyContainer properties, const dmVMath::Point3& position, const dmVMath::Quat& rotation, const dmVMath::Vector3& scale);

/*#
* Retrieve a collection from the specified instance
* @name GetCollection
Expand Down Expand Up @@ -560,6 +604,9 @@ namespace dmGameObject
*/
Result GetComponentId(HInstance instance, uint16_t component_index, dmhash_t* component_id);

// Get the component, component type and its world
Result GetComponent(HInstance instance, dmhash_t component_id, uint32_t* component_type, HComponent* component, HComponentWorld* out_world);

/*# set position
* Set gameobject instance position
* @name SetPosition
Expand Down Expand Up @@ -728,6 +775,31 @@ namespace dmGameObject
*/
HInstance GetParent(HInstance instance);

/*#
* Get the component type index
* @name GetComponentTypeIndex
* @param collection Collection handle
* @param type_hash [type:dhmash_t] The hashed name of the registered component type (e.g. dmHashString("guic"))
* @return type_index [type:uint32_t] The component type index. 0xFFFFFFFF if not found
*/
uint32_t GetComponentTypeIndex(HCollection collection, dmhash_t type_hash);

/*#
* Retrieve the world in the collection connected to the supplied component
* @param collection Collection handle
* @param component_type_index index of the component type
* @return world [type:void*] The pointer to the world, 0x0 if not found
*/
HComponentWorld GetWorld(HCollection collection, uint32_t component_type_index);

/*#
* Retrieve the context for a component type
* @param collection Collection handle
* @param component_type_index index of the component type
* @return context [type:void*] The pointer to the context, 0x0 if not found
*/
void* GetContext(HCollection collection, uint32_t component_type_index);


// These functions are used for profiling functionality

Expand Down Expand Up @@ -976,4 +1048,3 @@ namespace dmGameObject
}

#endif // DMSDK_GAMEOBJECT_H

Loading
Loading