Dawn/src/dawn/host/DawnHost.hpp
2022-10-19 19:44:47 -07:00

91 lines
3.3 KiB
C++

// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "dawnlibs.hpp"
#include "game/DawnGame.hpp"
/** Implies the host initialized successfully */
#define DAWN_HOST_INIT_RESULT_SUCCESS 0
/** Implies that the update was successful, and the loop should continue */
#define DAWN_HOST_UPDATE_RESULT_SUCCESS 0
/** Implies that the update was successful, but the loop should not continue */
#define DAWN_HOST_UPDATE_RESULT_EXIT 1
/** Implies that the host started successfully */
#define DAWN_HOST_START_RESULT_SUCCESS 0
/** Implies that the host started successfully, and then finished everything */
#define DAWN_HOST_START_RESULT_EXIT_SUCCESS 1
namespace Dawn {
/**
* DawnHostData is a custom forwarder that allows any host to define what it
* will need access to, data-wise. For example, GLFW+GLAD uses this to hold
* the window handle during the hosts' session, so that it can destroy it
* safely when the host is unloaded.
*/
class DawnHostData;
class DawnHost :
public std::enable_shared_from_this<DawnHost>
{
public:
std::unique_ptr<DawnHostData> data;
/**
* Construct a new DawnHost. Hosts are set by the various dawn platform
* libraries to be handled in different ways depending on the exact
* system. For example, Windows can support both GLFW+GLAD or SDL and may
* opt to invoke a DawnHost for either of these scenarios.
*/
DawnHost();
/**
* Request to initialize the host. Hosts can initialize themselves pretty
* much however they want, but they must return a status code, in cases
* where some hosts may not be responsible for their own invokation.
*
* @param game Game instance that this host is running for.
* @return A status code, where DAWN_HOST_INIT_RESULT_SUCCESS is success.
*/
int32_t init(DawnGame &game);
/**
* Request to start the main loop. This method may not exist and may not
* need to exist depending on the host. For that reason this is marked as
* virtual and is up to the main caller to know how this start method
* needs to be called. Start should request update() to be invoked if it
* does begin the main thread.
*
* @param game Game instance that this host is running for.
* @return A status code, refer to DAWN_HOST_START_RESULT_{} definitions.
*/
virtual int32_t start(DawnGame &game);
/**
* Requests the host to perform a main-thread update.
*
* @param game Game instance that this host is running for.
* @param delta How much time has passed (in seconds) since the last tick.
* @return A status code, refer to DAWN_HOST_UPDATE_RESULT_{} definitions.
*/
int32_t update(DawnGame &game, float_t delta);
/**
* Request the host to be unloaded. This is a bit different from dispose
* as we are likely to unload the host just after the game is unloaded,
* but before the parent program has requested memory freeing.
*
* @param game Game instance that this host is running for.
*/
void unload(DawnGame &game);
/**
* Destroy (and unload) all of the DawnHost data from memory.
*/
~DawnHost();
};
}