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

Improved interop for StepTimer with a new Event C API #486

Merged
merged 10 commits into from
Jan 4, 2023
46 changes: 46 additions & 0 deletions include/NovelRT.Interop/Utilities/NrtEvent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright © Matt Jones and Contributors. Licensed under the MIT Licence (MIT). See LICENCE.md in the repository root
// for more information.
#ifndef NOVELRT_INTEROP_UTILITIES_NRTEVENT_H
#define NOVELRT_INTEROP_UTILITIES_NRTEVENT_H

#include "../NrtTypedefs.h"

#ifdef __cplusplus
extern "C"
{
#endif

// Event<>
NrtUtilitiesEventHandle Nrt_Event_Create();

NrtResult Nrt_Event_AddEventHandler(NrtUtilitiesEventHandle event,
void (*handler)(void*),
void* context,
NrtAtom* outputEventHandlerId);

NrtResult Nrt_Event_RemoveEventHandler(NrtUtilitiesEventHandle event, NrtAtom eventHandlerId);

NrtResult Nrt_Event_Invoke(NrtUtilitiesEventHandle event);

NrtResult Nrt_Event_Destroy(NrtUtilitiesEventHandle event);

// Event<NovelRT::Timing::Timestamp>
NrtUtilitiesEventWithTimestampHandle Nrt_EventWithTimestamp_Create();

NrtResult Nrt_EventWithTimestamp_AddEventHandler(NrtUtilitiesEventWithTimestampHandle event,
void (*handler)(NrtTimestamp, void*),
void* context,
NrtAtom* outputEventHandlerId);

NrtResult Nrt_EventWithTimestamp_RemoveEventHandler(NrtUtilitiesEventWithTimestampHandle event,
NrtAtom eventHandlerId);

NrtResult Nrt_EventWithTimestamp_Invoke(NrtUtilitiesEventWithTimestampHandle event, NrtTimestamp timestamp);

NrtResult Nrt_EventWithTimestamp_Destroy(NrtUtilitiesEventWithTimestampHandle event);

#ifdef __cplusplus
}
#endif

#endif // NOVELRT_INTEROP_UTILITIES_NRTEVENT_H
capnkenny marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions include/NovelRT/Timing/StepTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// This is based on the StepTimer provided in the DirectX ToolKit
// Original code is available under the MIT Licence

#include "../Utilities/Event.h"
RubyNova marked this conversation as resolved.
Show resolved Hide resolved
#include "Timestamp.h"

#ifndef NOVELRT_TIMING_STEPTIMER_H
Expand Down
1 change: 1 addition & 0 deletions include/NovelRT/Utilities/Event.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "../Atom.h"
#include <functional>
#include <vector>
DynamicField marked this conversation as resolved.
Show resolved Hide resolved

#ifndef NOVELRT_UTILITIES_EVENT_H
#define NOVELRT_UTILITIES_EVENT_H
Expand Down
4 changes: 3 additions & 1 deletion src/NovelRT.Interop/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ set(INTEROP_SOURCES
Timing/NrtTimestamp.cpp

Utilities/NrtMisc.cpp
Utilities/NrtEvent.cpp

Windowing/NrtIWindowingDevice.cpp)
Windowing/NrtIWindowingDevice.cpp
)

add_library(Interop SHARED ${INTEROP_SOURCES})
add_dependencies(Interop Engine)
Expand Down
222 changes: 105 additions & 117 deletions src/NovelRT.Interop/Timing/NrtStepTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,150 +3,138 @@

#include <NovelRT.Interop/NrtErrorHandling.h>
#include <NovelRT.Interop/Timing/NrtStepTimer.h>
#include <NovelRT/NovelRT.h>
#include <NovelRT/Timing/StepTimer.h>
#include <NovelRT/Utilities/Event.h>

#ifdef __cplusplus
extern "C"
NrtResult Nrt_StepTimer_create(uint32_t targetFrameRate, double maxSecondDelta, NrtStepTimerHandle* output)
{
#endif

NrtResult Nrt_StepTimer_create(uint32_t targetFrameRate, double maxSecondDelta, NrtStepTimerHandle* output)
if (output == nullptr)
{
if (output == nullptr)
{
Nrt_setErrMsgIsNullArgumentProvidedInternal();
return NRT_FAILURE_NULL_ARGUMENT_PROVIDED;
}

NovelRT::Timing::StepTimer timer = NovelRT::Timing::StepTimer(targetFrameRate, maxSecondDelta);
*output = reinterpret_cast<NrtStepTimerHandle>(&timer);
return NRT_SUCCESS;
Nrt_setErrMsgIsNullArgumentProvidedInternal();
return NRT_FAILURE_NULL_ARGUMENT_PROVIDED;
}

uint64_t Nrt_StepTimer_getElapsedTicks(NrtStepTimerHandle timer)
{
NovelRT::Timing::StepTimer time = reinterpret_cast<NovelRT::Timing::StepTimer&>(timer);
return time.getElapsedTicks();
}
auto* timer = new NovelRT::Timing::StepTimer(targetFrameRate, maxSecondDelta);
*output = reinterpret_cast<NrtStepTimerHandle>(timer);
return NRT_SUCCESS;
}

uint64_t Nrt_StepTimer_getTotalTicks(NrtStepTimerHandle timer)
{
NovelRT::Timing::StepTimer time = reinterpret_cast<NovelRT::Timing::StepTimer&>(timer);
return time.getTotalTicks();
}
uint64_t Nrt_StepTimer_getElapsedTicks(NrtStepTimerHandle timer)
{
auto* time = reinterpret_cast<NovelRT::Timing::StepTimer*>(timer);
return time->getElapsedTicks();
}

NrtTimestamp Nrt_StepTimer_getElapsedTime(NrtStepTimerHandle timer)
{
NovelRT::Timing::Timestamp* time = new NovelRT::Timing::Timestamp(0);
*time = reinterpret_cast<NovelRT::Timing::StepTimer&>(timer).getElapsedTime();
return reinterpret_cast<NrtTimestamp&>(*time);
}
uint64_t Nrt_StepTimer_getTotalTicks(NrtStepTimerHandle timer)
{
auto* time = reinterpret_cast<NovelRT::Timing::StepTimer*>(timer);
return time->getTotalTicks();
}

NrtTimestamp Nrt_StepTimer_getTotalTime(NrtStepTimerHandle timer)
{
NovelRT::Timing::Timestamp* time = new NovelRT::Timing::Timestamp(0);
*time = reinterpret_cast<NovelRT::Timing::StepTimer&>(timer).getTotalTime();
return reinterpret_cast<NrtTimestamp&>(*time);
}
NrtTimestamp Nrt_StepTimer_getElapsedTime(NrtStepTimerHandle timer)
{
NovelRT::Timing::Timestamp timestamp = reinterpret_cast<NovelRT::Timing::StepTimer&>(timer).getElapsedTime();
return reinterpret_cast<NrtTimestamp&>(timestamp);
}

uint64_t Nrt_StepTimer_getTargetElapsedTicks(NrtStepTimerHandle timer)
{
NovelRT::Timing::StepTimer time = reinterpret_cast<NovelRT::Timing::StepTimer&>(timer);
return time.targetElapsedTicks();
}
NrtTimestamp Nrt_StepTimer_getTotalTime(NrtStepTimerHandle timer)
{
NovelRT::Timing::Timestamp timestamp = reinterpret_cast<NovelRT::Timing::StepTimer&>(timer).getTotalTime();
return reinterpret_cast<NrtTimestamp&>(timestamp);
}

NrtResult Nrt_StepTimer_setTargetElapsedTicks(NrtStepTimerHandle timer, uint64_t input)
{
if (timer == nullptr)
{
Nrt_setErrMsgIsNullInstanceProvidedInternal();
return NRT_FAILURE_NULL_INSTANCE_PROVIDED;
}

NovelRT::Timing::StepTimer time = reinterpret_cast<NovelRT::Timing::StepTimer&>(timer);
time.targetElapsedTicks() = input;
return NRT_SUCCESS;
}
uint64_t Nrt_StepTimer_getTargetElapsedTicks(NrtStepTimerHandle timer)
{
auto* time = reinterpret_cast<NovelRT::Timing::StepTimer*>(timer);
return time->targetElapsedTicks();
}

NrtTimestamp Nrt_StepTimer_getTargetElapsedTime(NrtStepTimerHandle timer)
NrtResult Nrt_StepTimer_setTargetElapsedTicks(NrtStepTimerHandle timer, uint64_t input)
{
if (timer == nullptr)
{
NovelRT::Timing::Timestamp* time = new NovelRT::Timing::Timestamp(0);
*time = reinterpret_cast<NovelRT::Timing::StepTimer&>(timer).getTargetElapsedTime();
return reinterpret_cast<NrtTimestamp&>(*time);
Nrt_setErrMsgIsNullArgumentProvidedInternal();
return NRT_FAILURE_NULL_INSTANCE_PROVIDED;
}

NrtResult Nrt_StepTimer_setTargetElapsedTime(NrtStepTimerHandle timer, NrtTimestamp target)
{
if (timer == nullptr)
{
Nrt_setErrMsgIsNullInstanceProvidedInternal();
return NRT_FAILURE_NULL_INSTANCE_PROVIDED;
}

NovelRT::Timing::StepTimer time = reinterpret_cast<NovelRT::Timing::StepTimer&>(timer);
time.setTargetElapsedTime(NovelRT::Timing::Timestamp(target));
return NRT_SUCCESS;
}
auto* time = reinterpret_cast<NovelRT::Timing::StepTimer*>(timer);
time->targetElapsedTicks() = input;
return NRT_SUCCESS;
}

uint32_t Nrt_StepTimer_getFrameCount(NrtStepTimerHandle timer)
{
NovelRT::Timing::StepTimer time = reinterpret_cast<NovelRT::Timing::StepTimer&>(timer);
return time.getFrameCount();
}
NrtTimestamp Nrt_StepTimer_getTargetElapsedTime(NrtStepTimerHandle timer)
{
NovelRT::Timing::Timestamp timestamp = reinterpret_cast<NovelRT::Timing::StepTimer&>(timer).getTargetElapsedTime();
return reinterpret_cast<NrtTimestamp&>(timestamp);
}

uint32_t Nrt_StepTimer_getFramesPerSecond(NrtStepTimerHandle timer)
NrtResult Nrt_StepTimer_setTargetElapsedTime(NrtStepTimerHandle timer, NrtTimestamp target)
{
if (timer == nullptr)
{
NovelRT::Timing::StepTimer time = reinterpret_cast<NovelRT::Timing::StepTimer&>(timer);
return time.getFramesPerSecond();
Nrt_setErrMsgIsNullArgumentProvidedInternal();
return NRT_FAILURE_NULL_INSTANCE_PROVIDED;
}

NrtBool Nrt_StepTimer_getIsFixedTimeStep(NrtStepTimerHandle timer)
{
NovelRT::Timing::StepTimer time = reinterpret_cast<NovelRT::Timing::StepTimer&>(timer);
return time.isFixedTimeStep() ? NRT_TRUE : NRT_FALSE;
}
auto* time = reinterpret_cast<NovelRT::Timing::StepTimer*>(timer);
time->setTargetElapsedTime(NovelRT::Timing::Timestamp(target));
return NRT_SUCCESS;
}

uint32_t Nrt_StepTimer_getFrameCount(NrtStepTimerHandle timer)
{
auto* time = reinterpret_cast<NovelRT::Timing::StepTimer*>(timer);
return time->getFrameCount();
}

NrtResult Nrt_StepTimer_setIsFixedTimeStep(NrtStepTimerHandle timer, int32_t input)
uint32_t Nrt_StepTimer_getFramesPerSecond(NrtStepTimerHandle timer)
{
auto* time = reinterpret_cast<NovelRT::Timing::StepTimer*>(timer);
return time->getFramesPerSecond();
}

NrtBool Nrt_StepTimer_getIsFixedTimeStep(NrtStepTimerHandle timer)
{
auto* time = reinterpret_cast<NovelRT::Timing::StepTimer*>(timer);
return time->isFixedTimeStep() ? NRT_TRUE : NRT_FALSE;
}

NrtResult Nrt_StepTimer_setIsFixedTimeStep(NrtStepTimerHandle timer, int32_t input)
{
if (timer == nullptr)
{
if (timer == nullptr)
{
Nrt_setErrMsgIsNullInstanceProvidedInternal();
return NRT_FAILURE_NULL_INSTANCE_PROVIDED;
}

NovelRT::Timing::StepTimer time = reinterpret_cast<NovelRT::Timing::StepTimer&>(timer);
time.isFixedTimeStep() = input;
return NRT_SUCCESS;
Nrt_setErrMsgIsNullArgumentProvidedInternal();
return NRT_FAILURE_NULL_INSTANCE_PROVIDED;
}

NrtResult Nrt_StepTimer_resetElapsedTime(NrtStepTimerHandle timer)
auto* time = reinterpret_cast<NovelRT::Timing::StepTimer*>(timer);
time->isFixedTimeStep() = input;
return NRT_SUCCESS;
}

NrtResult Nrt_StepTimer_resetElapsedTime(NrtStepTimerHandle timer)
{
if (timer == nullptr)
{
if (timer == nullptr)
{
Nrt_setErrMsgIsNullInstanceProvidedInternal();
return NRT_FAILURE_NULL_INSTANCE_PROVIDED;
}

NovelRT::Timing::StepTimer time = reinterpret_cast<NovelRT::Timing::StepTimer&>(timer);
time.resetElapsedTime();
return NRT_SUCCESS;
Nrt_setErrMsgIsNullArgumentProvidedInternal();
return NRT_FAILURE_NULL_INSTANCE_PROVIDED;
}

NrtResult Nrt_StepTimer_tick(NrtStepTimerHandle timer, NrtUtilitiesEventWithTimestampHandle event)
auto* time = reinterpret_cast<NovelRT::Timing::StepTimer*>(timer);
time->resetElapsedTime();
return NRT_SUCCESS;
}

NrtResult Nrt_StepTimer_tick(NrtStepTimerHandle timer, NrtUtilitiesEventWithTimestampHandle event)
{
if (event == nullptr)
{
if (event == nullptr)
{
Nrt_setErrMsgIsNullInstanceProvidedInternal();
return NRT_FAILURE_NULL_INSTANCE_PROVIDED;
}

NovelRT::Timing::StepTimer time = reinterpret_cast<NovelRT::Timing::StepTimer&>(timer);
NovelRT::Utilities::Event<NovelRT::Timing::Timestamp> eventWithTimestamp =
reinterpret_cast<NovelRT::Utilities::Event<NovelRT::Timing::Timestamp>&>(event);
time.tick(eventWithTimestamp);
return NRT_SUCCESS;
Nrt_setErrMsgIsNullArgumentProvidedInternal();
return NRT_FAILURE_NULL_INSTANCE_PROVIDED;
}

#ifdef __cplusplus
auto* time = reinterpret_cast<NovelRT::Timing::StepTimer*>(timer);
auto* eventWithTimestamp = reinterpret_cast<NovelRT::Utilities::Event<NovelRT::Timing::Timestamp>*>(event);
time->tick(*eventWithTimestamp);
return NRT_SUCCESS;
}
#endif
Loading