blah/include/blah/app.h

165 lines
3.8 KiB
C
Raw Normal View History

2020-08-26 15:38:01 +08:00
#pragma once
#include <blah/common.h>
#include <blah/numerics/spatial.h>
2020-08-26 15:38:01 +08:00
namespace Blah
{
2021-03-21 17:08:28 +08:00
// Application Event Functions
using AppEventFn = Func<void()>;
2021-03-21 17:08:28 +08:00
// Application Logging Functions
using AppLogFn = Func<void(const char* message, Log::Category category)>;
2021-03-21 17:08:28 +08:00
// Application Configuration
2020-08-26 15:38:01 +08:00
struct Config
{
2021-03-21 17:08:28 +08:00
// Application name.
// This has no default and must be set.
2020-08-26 15:38:01 +08:00
const char* name;
2021-03-21 17:08:28 +08:00
// Starting width, in pixels.
// Depending on the OS DPI, the true window size may be a multiple of this.
// This has no default and must be set.
2020-08-26 15:38:01 +08:00
int width;
2021-03-21 17:08:28 +08:00
// Starting height, in pixels.
// Depending on the OS DPI, the true window size may be a multiple of this.
// This has no default and must be set.
2020-08-26 15:38:01 +08:00
int height;
2021-03-21 17:08:28 +08:00
// maximum updates to run before "giving up" and reducing frame rate.
// this avoids the 'spiral of death'.
// defaults to 5.
2020-08-26 15:38:01 +08:00
int max_updates;
2021-03-21 17:08:28 +08:00
// target framerate.
// defaults to 60.
2020-08-26 15:38:01 +08:00
int target_framerate;
2021-03-21 17:08:28 +08:00
// Callback on application startup
// Defaults to nothing.
AppEventFn on_startup;
2021-03-21 17:08:28 +08:00
// Callback on application shutdown
// Defaults to nothing.
AppEventFn on_shutdown;
2021-03-21 17:08:28 +08:00
// Callback on application update
// Defaults to nothing.
AppEventFn on_update;
2021-03-21 17:08:28 +08:00
// Callback on application render
// Defaults to nothing.
AppEventFn on_render;
2021-03-21 17:08:28 +08:00
// Callback when the user has requested the application close.
// For example, pressing the Close button, ALT+F4, etc
2021-03-21 17:08:28 +08:00
// By default this calls `App::exit()`
AppEventFn on_exit_request;
2021-03-21 17:08:28 +08:00
// Callback when the application logs info/warning/errors
// Defaults to printf.
AppLogFn on_log;
2020-08-26 15:38:01 +08:00
2021-03-21 17:08:28 +08:00
// Default config setup
2020-08-26 15:38:01 +08:00
Config();
};
2021-03-21 17:08:28 +08:00
// Renderer the Application is using
2020-12-27 06:44:48 +08:00
enum class Renderer
{
None = -1,
OpenGL,
D3D11,
Metal,
Count
};
2021-03-21 17:08:28 +08:00
// Features available on the current Renderer
2020-12-27 06:44:48 +08:00
struct RendererFeatures
{
2021-03-21 17:08:28 +08:00
// Whether Mesh Instancing is available
2020-12-27 06:44:48 +08:00
bool instancing = false;
2021-03-21 17:08:28 +08:00
// Whether the Texture origin is the bottom left.
// This is true for OpenGL.
2020-12-27 06:44:48 +08:00
bool origin_bottom_left = false;
2021-03-21 17:08:28 +08:00
// Maximum Texture Size available
2020-12-27 06:44:48 +08:00
int max_texture_size = 0;
};
// Forward declare Target for the BackBuffer
class Target;
using TargetRef = Ref<Target>;
2021-03-21 17:08:28 +08:00
// Application
2020-08-26 15:38:01 +08:00
namespace App
{
// Runs the application
bool run(const Config* config);
// Exits the application.
// This only signals for the application to close, it will not stop
// until the current update and render calls are finished.
2020-08-26 15:38:01 +08:00
void exit();
// Gets the config data used to run the application
const Config& config();
2020-08-26 15:38:01 +08:00
// Gets the working path
const char* path();
// Gets the user path
const char* user_path();
2021-05-10 10:46:08 +08:00
// Gets the Window Title
const char* get_title();
// Sets the Window Title
void set_title(const char* title);
// Gets the Window Position
Point get_position();
// Sets the Window Position
void set_position(Point point);
// Gets the Window Size
Point get_size();
// Sets the Window Size
void set_size(Point point);
2020-08-26 15:38:01 +08:00
// Gets the width of the window
int width();
// Gets the height of the window
int height();
// Gets the drawable width of the window, in pixels.
// This may differ from the width when on platforms with High DPI Displays.
2020-08-26 15:38:01 +08:00
int draw_width();
// Gets the drawable height of the window, in pixels.
// This may differ from the height when on platforms with High DPI Displays.
2020-08-26 15:38:01 +08:00
int draw_height();
// Gets the content scale based on the platform.
// macOS is usually 2.0, other platforms vary.
2020-08-26 15:38:01 +08:00
float content_scale();
// Toggles fullscreen if supported on the platform.
// Otherwise this function does nothing.
2020-08-26 15:38:01 +08:00
void fullscreen(bool enabled);
2020-12-27 06:44:48 +08:00
// Returns the Rendering API in use
Renderer renderer();
// Retrieves the Renderer Features
const RendererFeatures& renderer_features();
// Reference to the window's back buffer
extern const TargetRef backbuffer;
2020-08-26 15:38:01 +08:00
}
}