aboutsummaryrefslogtreecommitdiffstats
path: root/code/editor/editor_platform.h
blob: 340593649c4cf9ec90ea9bdc1c4ef94e43e14e35 (plain) (blame)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* date = December 14th 2025 5:30 pm */

#if !defined(PLATFORM_H)
#define PLATFORM_H

// TODO(luca): So that everyone has the same sense of what the "release" version is of the app maybe the sets of flags set in release mode should be defined?

// NOTE(luca): Adds some debug information and interactions that a normal user shouldn't be able to see.
#if !defined(EDITOR_INTERNAL)
# define EDITOR_INTERNAL 0
#endif

// NOTE(luca): Toggles the compiliation of the debug GUI in the platform layer, this is separate from EDITOR_INTERNAL because sometimes we want to test the "release" version of our app with our platform debug tools. 
#if !defined(EDITOR_PLATFORM_DEBUG_UI)
# define EDITOR_PLATFORM_DEBUG_UI 0
#endif

// NOTE(luca): For performance (specifically on my laptop).
#if !defined(EDITOR_HOT_RELOAD_SHADERS)
# define EDITOR_HOT_RELOAD_SHADERS 0
#endif

#if !defined(EDITOR_FORCE_X11)
# define EDITOR_FORCE_X11 0
#endif

#if !defined(EDITOR_FORCE_SMALL_RESOLUTION)
# define EDITOR_FORCE_SMALL_RESOLUTION 0
#endif

//~ Globals
#if EDITOR_INTERNAL
global_variable b32 IsEditorBuildInternal = true;
#else
global_variable b32 IsEditorBuildInternal = false;
#endif

global_variable str8 ExeDirPath;

//~ Types
typedef struct app_offscreen_buffer app_offscreen_buffer;
struct app_offscreen_buffer
{
    s32 Width;
    s32 Height;
    u8 *Pixels;
    s32 Pitch;
    s32 BytesPerPixel;
};

// TODO(luca): Metaprogram
enum platform_key
{
    PlatformKey_None = 0,
    PlatformKey_Tab,
    PlatformKey_Return,
    PlatformKey_Escape,
    
    PlatformKey_Delete,
    PlatformKey_BackSpace,
    PlatformKey_Insert,
    
    PlatformKey_F1,
    PlatformKey_F2,
    PlatformKey_F3,
    PlatformKey_F4,
    PlatformKey_F5,
    PlatformKey_F6,
    PlatformKey_F7,
    PlatformKey_F8,
    PlatformKey_F9,
    PlatformKey_F10,
    PlatformKey_F11,
    PlatformKey_F12,
    
    PlatformKey_Home,
    PlatformKey_End,
    PlatformKey_PageUp,
    PlatformKey_PageDown,
    
    PlatformKey_Up,
    PlatformKey_Down,
    PlatformKey_Left,
    PlatformKey_Right,
    
    PlatformKey_Shift,
    PlatformKey_Control,
    PlatformKey_Alt,
    
    PlatformKey_Count,
};
typedef enum platform_key platform_key;

enum platform_key_modifier
{
    PlatformKeyModifier_None    = 0,
    PlatformKeyModifier_Shift   = (1 << 0),
    PlatformKeyModifier_Control = (1 << 1),
    PlatformKeyModifier_Alt     = (1 << 2),
};
typedef enum platform_key_modifier platform_key_modifier;

#define PlatformKeyModifier_Any (PlatformKeyModifier_Control|PlatformKeyModifier_Shift|PlatformKeyModifier_Alt)

typedef struct app_text_button app_text_button;
struct app_text_button
{
    union
    {
        rune Codepoint;
        platform_key Symbol;
    };
    s32 Modifiers;
    b32 IsSymbol;
};

typedef struct app_button_state app_button_state;
struct app_button_state
{
    s32 Modifiers;
    s32 HalfTransitionCount;
    b32 EndedDown;
};

enum platform_cursor_shape
{
    PlatformCursorShape_Arrow = 0,
    PlatformCursorShape_None,
    PlatformCursorShape_Grab,
    PlatformCursorShape_ResizeHorizontal,
    PlatformCursorShape_ResizeVertical,
};
typedef enum platform_cursor_shape platform_cursor_shape;

enum platform_mouse_button_type
{
    PlatformMouseButton_Left = 0,
    PlatformMouseButton_Right,
    PlatformMouseButton_Middle,
    PlatformMouseButton_ScrollUp,
    PlatformMouseButton_ScrollDown,
    PlatformMouseButton_Count
};
typedef enum platform_mouse_button_type platform_mouse_button_type;

// NOTE(luca): If I want to support multiple players, I probably want to support multiple
// mice as well, so I may just want to duplicate this input struct.

typedef struct app_input app_input;
struct app_input
{
    
    struct
    {
        app_button_state Buttons[PlatformMouseButton_Count];
        s32 X, Y, Z;
        
        s32 StartX, StartY;
    } Mouse;
    
    union
    {
        app_button_state GameButtons[12];
        struct 
        {
            app_button_state ActionLeft;
            app_button_state ActionRight;
            app_button_state ActionUp;
            app_button_state ActionDown;
            app_button_state MoveLeft;
            app_button_state MoveRight;
            app_button_state MoveUp;
            app_button_state MoveDown;
        };
    };
    
    struct
    {
        u64 Count;
        // NOTE(luca): Who can press more than 256 keys in under one frame?
        app_text_button Buffer[256];
    } Text;
    
    // NOTE(luca): We can invalidate the input with this.
    b32 Consumed;
    
    f32 dtForFrame;
    
    b32 SkipRendering;
    
    b32 PlatformWindowIsFocused;
    b32 PlatformIsRecording;
    b32 PlatformIsStepping;
    
    // Communication with the platform layer
    s32 PlatformCursor;
    str8 PlatformClipboard;
    b32 PlatformSetClipboard;
};

typedef struct app_memory app_memory;
struct app_memory
{
    void *Memory;
    u64 MemorySize;
    
    str8 ExeDirPath;
#if EDITOR_INTERNAL
    b32 IsDebuggerAttached;
#endif
    b32 IsProfiling;
    b32 Reloaded;
    
    thread_context *ThreadCtx;
    
#if OS_WINDOWS
    f64 PerfCountFrequency;
#endif
    
    b32 Initialized;
};
#define UPDATE_AND_RENDER(Name) b32 Name(thread_context *Context, app_memory *Memory, app_offscreen_buffer *Buffer, app_input *Input)

typedef UPDATE_AND_RENDER(update_and_render);

UPDATE_AND_RENDER(UpdateAndRenderStub) { return false; }

typedef struct app_code app_code;
struct app_code
{
    update_and_render *UpdateAndRender;
    
    char *LibraryPath;
    u64 LibraryHandle;
    b32 Loaded;
    s64 LastWriteTime;
};

//~ API
typedef u64  P_context;

internal P_context P_ContextInit(arena *Arena, app_offscreen_buffer *Buffer, b32 *Running);
internal void      P_UpdateImage(P_context Context, app_offscreen_buffer *Buffer);
internal void      P_ProcessMessages(P_context Context, app_input *Input, app_offscreen_buffer *Buffer, b32 *Running);
internal void      P_LoadAppCode(arena *Arena, app_code *Code, app_memory *Memory);

//- Helpers 
internal inline b32 
WasPressed(app_button_state State)
{
    b32 Result = ((State.HalfTransitionCount > 1) || 
                  (State.HalfTransitionCount == 1 && State.EndedDown));
    return Result;
}

internal char *
PathFromExe(arena *Arena, str8 Path)
{
    char *Result = 0;
    
    u64 Size = ExeDirPath.Size + Path.Size + 1;
    
    Result = PushArray(Arena, char, Size);
    
    u64 At = 0;
    for EachIndex(Idx, ExeDirPath.Size)
    {
        Result[At] = (char)ExeDirPath.Data[Idx];
        At += 1;
    }
    for EachIndex(Idx, Path.Size)
    {
        Result[At] = (char)Path.Data[Idx];
        At += 1;
    }
    
    Result[Size - 1] = 0;
    
    return Result;
}

internal void 
ProcessKeyPress(app_button_state *ButtonState, b32 IsDown)
{
    if(ButtonState->EndedDown != IsDown)
    {
        ButtonState->EndedDown = IsDown;
        ButtonState->HalfTransitionCount += 1;
    }
}

#endif //PLATFORM_H