summaryrefslogtreecommitdiff
path: root/tools/graphics-app-framework/window.h
blob: e6f886f4260626600b8072fd0e6d1177aba41ae3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// window.h
#pragma once

#include <stdint.h>

namespace gfx {

struct Window;

enum class KeyCode
{
    Unknown,

    // TODO: extend this to cover at least a standard US-English keyboard

    A, B, C, D, E, F, G, H, I, J,
    K, L, M, N, O, P, Q, R, S, T,
    U, V, W, X, Y, Z,

    Space,
};

enum class EventCode : uint32_t
{
    MouseDown,
    MouseUp,
    MouseMoved,
    KeyDown,
    KeyUp,
};

struct Event
{
    EventCode   code;
    Window*     window;
    union
    {
        struct
        {
            float x;
            float y;
        } mouse;

        KeyCode key;
    } u;
};

typedef void (*EventHandler)(Event const&);

struct WindowDesc
{
    char const* title = nullptr;
    void* userData = nullptr;
    int width = 0;
    int height = 0;
    EventHandler eventHandler = nullptr;
};

Window* createWindow(WindowDesc const& desc);
void showWindow(Window* window);

void* getPlatformWindowHandle(Window* window);
void* getUserData(Window* window);

/// Opaque state provided by platform for a running application.
typedef struct ApplicationContext ApplicationContext;

/// User-defined application entry-point function.
typedef void(*ApplicationFunc)(ApplicationContext* context);

/// Dispatch any pending events for application.
///
/// @returns `true` if application should keep running.
bool dispatchEvents(ApplicationContext* context);

/// Exit the application with a given result code
void exitApplication(ApplicationContext* context, int resultCode);

/// Log a message to an appropriate logging destination.
void log(char const* message, ...);

/// Report an error to an appropriate logging destination.
int reportError(char const* message, ...);

uint64_t getCurrentTime();

uint64_t getTimerFrequency();

/// Run an application given the specified callback and command-line arguments.
int runApplication(
    ApplicationFunc     func,
    int                 argc,
    char const* const*  argv);

#define GFX_CONSOLE_MAIN(APPLICATION_ENTRY)  \
    int main(int argc, char** argv) {       \
        return gfx::runApplication(&(APPLICATION_ENTRY), argc, argv); \
    }

#ifdef _WIN32

int runWindowsApplication(
    ApplicationFunc     func,
    void*               instance,
    int                 showCommand);

#define GFX_UI_MAIN(APPLICATION_ENTRY)   \
    int __stdcall WinMain(              \
        void*   instance,               \
        void*   /* prevInstance */,     \
        void*   /* commandLine */,      \
        int     showCommand) {          \
        return gfx::runWindowsApplication(&(APPLICATION_ENTRY), instance, showCommand); \
    }

#else

#define GFX_UI_MAIN(APPLICATION_ENTRY) GFX_CONSOLE_MAIN(APPLICATION_ENTRY)

#endif

} // gfx