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
|
/* ===========================================================================
* PODIUM - Kitchen Sink - Copyright (c) 2026 Vasco Alves
* ---------------------------------------------------------------------------
* To stand on the shoulders of giants... Giants must stand still!"
* ---------------------------------------------------------------------------
* PREFIX: P_ (types) or p_ (functions & variables)
* ---------------------------------------------------------------------------
* COMPILE TIME FLAGS
* ._________________________________________________________________________,
* | Name | Description |
* |-----------------|-------------------------------------------------------|
* | PODIUM_WIN32 | Used to explicitly compile for Windows |
* | PODIUM_LINUX | Used to explicitly compile for Linux and FreeBSD. |
* '-------------------------------------------------------------------------'
* MODULES
* ._________________________________________________________________________,
* | Name | Description |
* |-----------------|-------------------------------------------------------|
* | P_MODULE_VULKAN | Gives access to vulkan related utilities. |
* | P_MODULE_STRING | Include string manipulation utilities. |
* | P_MODULE_MATH | Trigonometry and linear algebra functions. |
* | P_MODULE_ALLOC | Memory allocators: Arena, Stack & Pool. |
* '-------------------------------------------------------------------------'
* =========================================================================== */
#ifndef _PODIUM_H_
#define _PODIUM_H_
#define PODIUM_MAJOR "0" // needs more revisions and use to reach 1.0 status
#define PODIUM_MINOR "2" // vulkan module
#define PODIUM_PATCH "2" // gamepad
#define POAPI static inline
#define PODEF static inline
/*
* Auto-detect platforms.
*/
/* Windows */
#if defined(_WIN32) || defined(_WIN64) || defined(__WIN32__) || defined(__TOS_WIN__)
#ifndef PODIUM_WIN32
#define PODIUM_WIN32
#endif
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
/* Apple */
#elif defined(__APPLE__) || defined(__MACH__)
#include <TargetConditionals.h>
#ifndef PODIUM_APPLE
#define PODIUM_APPLE
#endif
#if TARGET_OS_IPHONE
#define PODIUM_IOS
#else
#define PODIUM_MACOS
#endif
/* Linux / BSD */
#elif defined(__linux__) || defined(__FreeBSD__)
#ifndef PODIUM_LINUX
#define PODIUM_LINUX
#endif
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200112L
#endif
#endif
#include <stdarg.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h> /* memcpy */
#include <stdio.h> /* vsnprintf */
/*
* NOTE: The <REPLACE> comments are used by the build
* script to know what includes to inline into the
* final single header file!
*/
#include "p_types.h" // <REPLACE>
#include "p_log.h" // <REPLACE>
#include "p_ds.h" // <REPLACE>
/*
* Main Platform Types (Keyboard, Events, Context, etc.)
*/
typedef enum {
P_KEYMOD_ALT = 0,
P_KEYMOD_SHIFT,
P_KEYMOD_CTRL,
P_KEYMOD_CAPS,
} P_KeyMod;
typedef enum {
P_KEY_UNKNOWN = 0,
P_KEY_UP, P_KEY_DOWN, P_KEY_LEFT, P_KEY_RIGHT,
P_KEY_SPACE, P_KEY_ESCAPE, P_KEY_ENTER,
P_KEY_A, P_KEY_B, P_KEY_C, P_KEY_D, P_KEY_E, P_KEY_F, P_KEY_G, P_KEY_H, P_KEY_I,
P_KEY_J, P_KEY_K, P_KEY_L, P_KEY_M, P_KEY_N, P_KEY_O, P_KEY_P, P_KEY_Q, P_KEY_R,
P_KEY_S, P_KEY_T, P_KEY_U, P_KEY_V, P_KEY_W, P_KEY_X, P_KEY_Y, P_KEY_Z,
} P_KeyCode;
/* Platform event type */
typedef enum {
P_EVENT_NONE = 0,
P_EVENT_KEY_DOWN,
P_EVENT_KEY_UP,
P_EVENT_WINDOW_CLOSE,
P_EVENT_WINDOW_RESIZE,
P_EVENT_POINTER,
P_EVENT_CONTROLLER_CONNECTED,
P_EVENT_CONTROLLER_DISCONNECTED,
} P_EvenType;
typedef enum {
P_POINTER_MOVED = 0,
P_POINTER_PRESSED,
P_POINTER_RELEASED
} P_PointerState;
typedef struct {
P_EvenType type;
union {
struct {P_KeyCode key; P_KeyMod mod; } key;
struct { u32 width, height; } resize;
struct { P_PointerState state; u32 button; u32 x, y; } pointer;
};
} P_Event;
/*
* Platform dependent functions to be implemented by each layer.
*/
POAPI u64 p_get_time(void); // Get time in nanoseconds.
POAPI void p_sleep_ns(i64 ns); // Sleep for nanoseconds.
POAPI void p_stdout(void *msg, usize bytes); // Print buffer to stdout.
PODEF void p_log_printf(P_Log_Level level, const char* src, ...); // Print buffer to stdout.
POAPI void* p_file_alloc(const char *path, unsigned long *buf_size);
POAPI bool p_file_exists(const char *path); // Check if file exists.
POAPI usize p_file_size(const char *path); // Check file size.
POAPI void p_file_load(const char *path, void *buf_ptr, unsigned long buf_size); // Load file to buffer.
PODEF void p_file_write(const char *path, void *buf_ptr, unsigned long buf_size); // Write buffer to file.
typedef struct P_Window_Impl P_Window;
POAPI bool p_window_open(P_Window *win, int width, int height, const char *title); // Opens a window. Returns false on failure.
POAPI void p_window_close(P_Window *win); // Close window.
POAPI bool p_window_is_open(P_Window *win); // Check if window is open.
POAPI void p_window_size(P_Window *win, int *window_width, int *window_height); // Get window size.
POAPI void p_window_draw(P_Window *win, u32 *pixels, int width, int height); // Draw directly to the window's buffer. Normally much slower than using the GPU.
POAPI bool p_window_poll_event(P_Window *win, P_Event *ev); // Returns true while there are events to poll. Write event data to a pointer.
POAPI void p_audio_init(const int sample_rate, const int channels, const char *name, const char *desc);
POAPI void p_audio_quit();
POAPI void p_audio_write(const i16 *samples, usize count);
/*
* The Vulkan Module is platform specific
*/
#ifdef P_MODULE_VULKAN
#include <vulkan/vulkan.h>
POAPI char** p_vulkan_get_extensions();
POAPI bool p_vulkan_create_surface(P_Window *window, VkInstance instance, const VkAllocationCallbacks* allocator, VkSurfaceKHR* out_surface);
#endif
/*
* The Remaining Modules are PLATFORM INDEPENDENT!
*/
#ifdef P_MODULE_STRING
#include <p_string.h> // <REPLACE>
#endif
#ifdef P_MODULE_MATH
#include <p_math.h> // <REPLACE>
#endif
#ifdef P_MODULE_ALLOC
#include <p_allocators.h> // <REPLACE>
#endif
#endif /* _PODIUM_H_ */
#ifdef PODIUM_IMPLEMENTATION
#undef PODIUM_IMPLEMENTATION
#if defined(PODIUM_SDL)
/* #include "p_platform_sdl.c" */
#elif defined(PODIUM_WIN32)
#include "p_platform_win32.c" // <REPLACE>
#elif defined(PODIUM_LINUX)
#include "p_platform_linux.c" // <REPLACE>
#endif /* linux */
POAPI void* p_file_alloc(const char *path, unsigned long *buf_size) {
if (p_file_exists(path) == false) {
return NULL;
}
usize file_size = p_file_size(path);
*buf_size = file_size;
void *ptr = malloc(file_size);
if (ptr) {
p_file_load(path, ptr, file_size); // Load file to buffer.
}
return ptr;
}
/*
* Import Module Code
*/
#ifdef P_MODULE_STRING
#include <p_string.c> // <REPLACE>
#endif
#ifdef P_MODULE_MATH
#include <p_math.c> // <REPLACE>
#endif
#ifdef P_MODULE_ALLOC
#include <p_allocators.c> // <REPLACE>
#endif
#define P_MAX_PRINTF_SIZE 1024
PODEF void
p_log_printf(P_Log_Level level, const char* src, ...)
{
static char out_message[P_MAX_PRINTF_SIZE];
static u32 offset = P_PREFIX_LEN + 1;
memcpy(out_message, p_prefix[level], P_PREFIX_LEN); /* prefixes have a fixed len */
out_message[P_PREFIX_LEN] = ' '; /* write space */
__builtin_va_list arg_ptr;
va_start(arg_ptr, src);
int len = vsnprintf(out_message+offset, P_MAX_PRINTF_SIZE-offset, src, arg_ptr); // append message
va_end(arg_ptr);
if (len < 0) len = 0;
usize total_len = offset + len;
if (total_len > P_MAX_PRINTF_SIZE) {
total_len = P_MAX_PRINTF_SIZE;
}
p_stdout(out_message, total_len);
p_stdout("\n", 1);
}
#endif // PODIUM_IMPLEMENTATION
|