-
Notifications
You must be signed in to change notification settings - Fork 0
/
ghost_platform.h
249 lines (196 loc) · 6.38 KB
/
ghost_platform.h
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
#if !defined(GHOST_PLATFORM_H)
/*
NOTE():
GHOST_INTERNAL:
0 - Build for public release
1 - Build for developer only
GHOST_SLOW:
0 - Not slow code allowed!
1 - Slow code welcome.
*/
#ifdef __cplusplus
extern "C" {
#endif
//
// NOTE(): Compilers
//
#if !defined(COMPILER_MSVC)
#define COMPILER_MSVC 0
#endif
#if !defined(COMPILER_LLVM)
#define COMPILER_LLVM 0
#endif
#if !COMPILER_MSVC && !COMPILER_LLVM
#if _MSC_VER
#undef COMPILER_MSVC
#define COMPILER_MSVC 1
#else
// TODO(casey): Moar compilerz!!!
#undef COMPILER_LLVM
#define COMPILER_LLVM 1
#endif
#endif
#if COMPILER_MSVC
#include <intrin.h>
#endif
//
// NOTE(casey): Types
//
#include <stdint.h>
#include <stddef.h>
typedef int8_t int8;
typedef int16_t int16;
typedef int32_t int32;
typedef int64_t int64;
typedef int32 bool32;
typedef uint8_t uint8;
typedef uint16_t uint16;
typedef uint32_t uint32;
typedef uint64_t uint64;
typedef size_t memory_index;
typedef float real32;
typedef double real64;
#define internal static
#define local_persist static
#define global_variable static
#define Pi32 3.14159265359f
#if GHOST_SLOW
// TODO(casey): Complete assertion macro - don't worry everyone!
#define Assert(Expression) if(!(Expression)) {*(int *)0 = 0;}
#else
#define Assert(Expression)
#endif
#define Kilobytes(Value) ((Value)*1024LL)
#define Megabytes(Value) (Kilobytes(Value)*1024LL)
#define Gigabytes(Value) (Megabytes(Value)*1024LL)
#define Terabytes(Value) (Gigabytes(Value)*1024LL)
#define ArrayCount(Array) (sizeof(Array) / sizeof((Array)[0]))
// TODO(): swap, min, max ... macros???
inline uint32
SafeTruncateUInt64(uint64 Value)
{
// TODO(): Defines for maximum values
Assert(Value <= 0xFFFFFFFF);
uint32 Result = (uint32)Value;
return(Result);
}
typedef struct thread_context
{
int Placeholder;
} thread_context;
/*
NOTE(): Services that the platform layer provides to the game
*/
#if GHOST_INTERNAL
/* IMPORTANT():
These are NOT for doing anything in the shipping game - they are
blocking and the write doesn't protect against lost data!
*/
typedef struct debug_read_file_result
{
uint32 ContentsSize;
void *Contents;
} debug_read_file_result;
#define DEBUG_PLATFORM_FREE_FILE_MEMORY(name) void name(thread_context *Thread, void *Memory)
typedef DEBUG_PLATFORM_FREE_FILE_MEMORY(debug_platform_free_file_memory);
#define DEBUG_PLATFORM_READ_ENTIRE_FILE(name) debug_read_file_result name(thread_context *Thread, char *Filename)
typedef DEBUG_PLATFORM_READ_ENTIRE_FILE(debug_platform_read_entire_file);
#define DEBUG_PLATFORM_WRITE_ENTIRE_FILE(name) bool32 name(thread_context *Thread, char *Filename, uint32 MemorySize, void *Memory)
typedef DEBUG_PLATFORM_WRITE_ENTIRE_FILE(debug_platform_write_entire_file);
#endif
/*
NOTE(): Services that the game provides to the platform layer.
(this may expand in the future - sound on separate thread, etc.)
*/
// FOUR THINGS - timing, controller/keyboard input, bitmap buffer to use, sound buffer to use
// TODO(): In the future, rendering _specifically_ will become a three-tiered abstraction!!!
typedef struct game_offscreen_buffer
{
// NOTE(): Pixels are alwasy 32-bits wide, Memory Order BB GG RR XX
void *Memory;
int Width;
int Height;
int Pitch;
int BytesPerPixel;
} game_offscreen_buffer;
typedef struct game_sound_output_buffer
{
int SamplesPerSecond;
int SampleCount;
int16 *Samples;
} game_sound_output_buffer;
typedef struct game_button_state
{
int HalfTransitionCount;
bool32 EndedDown;
} game_button_state;
typedef struct game_controller_input
{
bool32 IsConnected;
bool32 IsAnalog;
real32 StickAverageX;
real32 StickAverageY;
union
{
game_button_state Buttons[17];
struct
{
game_button_state MoveUp;
game_button_state MoveDown;
game_button_state MoveLeft;
game_button_state MoveRight;
game_button_state ActionUp;
game_button_state ActionDown;
game_button_state ActionLeft;
game_button_state ActionRight;
game_button_state LeftShoulder;
game_button_state RightShoulder;
game_button_state Back;
game_button_state Start;
game_button_state Select;
game_button_state Load1;
game_button_state Load2;
game_button_state Load3;
game_button_state Load4;
// NOTE(): All buttons must be added above this line
game_button_state Terminator;
};
};
} game_controller_input;
typedef struct game_input
{
game_button_state MouseButtons[5];
int32 MouseX, MouseY, MouseZ;
real32 dtForFrame;
game_controller_input Controllers[2];
} game_input;
typedef struct game_memory
{
bool32 IsInitialized;
uint64 PermanentStorageSize;
void *PermanentStorage; // NOTE(): REQUIRED to be cleared to zero at startup
uint64 TransientStorageSize;
void *TransientStorage; // NOTE(): REQUIRED to be cleared to zero at startup
debug_platform_free_file_memory *DEBUGPlatformFreeFileMemory;
debug_platform_read_entire_file *DEBUGPlatformReadEntireFile;
debug_platform_write_entire_file *DEBUGPlatformWriteEntireFile;
} game_memory;
#define GAME_UPDATE_AND_RENDER(name) void name(thread_context *Thread, game_memory *Memory, game_input *Input, game_offscreen_buffer *Buffer)
typedef GAME_UPDATE_AND_RENDER(game_update_and_render);
// NOTE(): At the moment, this has to be a very fast function, it cannot be
// more than a millisecond or so.
// TODO(): Reduce the pressure on this function's performance by measuring it
// or asking about it, etc.
#define GAME_GET_SOUND_SAMPLES(name) void name(thread_context *Thread, game_memory *Memory, game_sound_output_buffer *SoundBuffer)
typedef GAME_GET_SOUND_SAMPLES(game_get_sound_samples);
inline game_controller_input *GetController(game_input *Input, int unsigned ControllerIndex)
{
Assert(ControllerIndex < ArrayCount(Input->Controllers));
game_controller_input *Result = &Input->Controllers[ControllerIndex];
return(Result);
}
#ifdef __cplusplus
}
#endif
#define GHOST_PLATFORM_H
#endif