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
|
/* ===========================================================================
* PEAK - Copyright @ Vasco Alves - See LICENSE at the end of file.
*
* Platform layer.
* It just works, don't think about it too much.
*
* =========================================================================== */
#ifndef PEAK_H
#define PEAK_H
#define PEAK_MAJOR "0"
#define PEAK_MINOR "1"
#define PEAK_PATCH "1"
/* CHANGE LOG
* 0.0.0 - @vasco - prototyping
* 0.1.0 - @vasco - linux x11 that automagically loads X11 DLL
* 0.1.1 - @vasco - fixed event handling on linux
* 0.1.2 - @vasco - better handling of windows, if multiple windows becomes necessary in the future
* 0.2.0 - @vasco - multiple windows, major-ish API changes.
* 0.3.0 - @vasco - web via emscripten
* 0.4.0 - @vasco - win32
*/
#include <assert.h>
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#if !( \
(defined(__STDC__) && __STDC__ == 1 && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)\
)
#error "Peak requires C99."
#endif
/*
* Detecting a platform does not mean it's supported.
* Feel free to use these macros if you need them.
*/
#if defined(__wasm__) || defined(__wasm32__) || defined(__wasm64__) || defined(__EMSCRIPTEN__)
#define PEAK_WEB
#elif defined(_WIN32) || defined(_WIN64) || defined(__WIN32__) || defined(__TOS_WIN__)
#define PEAK_WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#elif defined(__APPLE__) || defined(__MACH__)
#include <TargetConditionals.h>
#define PEAK_APPLE
#if TARGET_OS_IPHONE
#define PEAK_IOS
#else
#define PEAK_MACOS
#endif
#elif defined(__ANDROID__)
#define PEAK_ANDROID
#define PEAK_LINUX
#elif defined(__linux__)
#define PEAK_LINUX
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__bsdi__) || defined(__DragonFly__)
#define PEAK_BSD
#endif
#if defined(PEAK_LINUX) || defined(PEAK_BSD) || defined(PEAK_APPLE)
#define PEAK_UNIX
#if !defined(PEAK_APPLE) && !defined(_POSIX_C_SOURCE)
#define _POSIX_C_SOURCE 200112L
#endif
#endif
#ifdef PEAK_WEB
#define PEAK static inline
#else
#define PEAK extern
#endif
typedef enum {
PEAK_KEYMOD_NONE = 0,
PEAK_KEYMOD_ALT,
PEAK_KEYMOD_SHIFT,
PEAK_KEYMOD_CTRL,
PEAK_KEYMOD_CAPS,
} PeakKeyMod;
typedef enum {
PEAK_KEY_UNKNOWN = 0,
PEAK_KEY_UP, PEAK_KEY_DOWN, PEAK_KEY_LEFT, PEAK_KEY_RIGHT,
PEAK_KEY_SPACE, PEAK_KEY_ESCAPE, PEAK_KEY_ENTER,
PEAK_KEY_A, PEAK_KEY_B, PEAK_KEY_C, PEAK_KEY_D, PEAK_KEY_E, PEAK_KEY_F, PEAK_KEY_G, PEAK_KEY_H, PEAK_KEY_I,
PEAK_KEY_J, PEAK_KEY_K, PEAK_KEY_L, PEAK_KEY_M, PEAK_KEY_N, PEAK_KEY_O, PEAK_KEY_P, PEAK_KEY_Q, PEAK_KEY_R,
PEAK_KEY_S, PEAK_KEY_T, PEAK_KEY_U, PEAK_KEY_V, PEAK_KEY_W, PEAK_KEY_X, PEAK_KEY_Y, PEAK_KEY_Z,
} PeakKeyCode;
typedef enum {
PEAK_EVENT_NONE = 0,
PEAK_EVENT_KEY_DOWN,
PEAK_EVENT_KEY_UP,
PEAK_EVENT_WINDOW_CLOSE,
PEAK_EVENT_WINDOW_RESIZE,
PEAK_EVENT_POINTER,
PEAK_EVENT_POINTER_CONNECTED,
PEAK_EVENT_POINTER_DISCONNECTED,
PEAK_EVENT_LAST
} PeakEventType;
typedef enum {
PEAK_POINTER_MOVED = 0,
PEAK_POINTER_PRESSED,
PEAK_POINTER_RELEASED
} PeakPointerState;
typedef enum {
PEAK_POINTER_LEFT = 0,
PEAK_POINTER_RIGHT,
PEAK_POINTER_MIDDLE,
PEAK_POINTER_TOUCH,
} PeakPointerType;
typedef struct {
PeakEventType type;
union {
struct { PeakKeyCode key; PeakKeyMod mod; } key;
struct { uint32_t width, height; } resize;
struct { PeakPointerState state; PeakPointerType type; float x, y; } pointer;
};
} PeakEvent;
/* Implemented by the platform */
typedef struct peak_window_internal_t PeakWindowInternal;
typedef struct PeakWindow PeakWindow;
/* Public API */
PEAK int peak_init(void); // Initialize platform context and load necessary DLLs.
PEAK void peak_quit(void); // Close platform context.
PEAK PeakWindow peak_window_open(const char *name, uint32_t width, uint32_t height, uint32_t flags); // Open a window.
PEAK void peak_window_close(PeakWindow *window); // Close a window.
PEAK void peak_window_run(PeakWindow *win, int (*peak_tick)(PeakWindow *win, void *userdata), void *userdata); // Hijacking the main loop makes life easier on platforms like web
PEAK int peak_window_epoll(PeakWindow *win, PeakEvent *ev); // Poll a window for events.
PEAK uint32_t* peak_window_backbuffer(PeakWindow *win, size_t *width, size_t *height); // Get the windows backbuffer.
PEAK void peak_window_clear(PeakWindow *win, float r, float g, float b, float a);
PEAK void peak_window_present(PeakWindow *win);
#endif // PEAK_H
#ifdef PEAK_IMPLEMENTATION
#undef PEAK_IMPLEMENTATION
#if defined(PEAK_WIN32) || defined(PEAK_WEB)
#define PEAK_Q 64
typedef struct {
unsigned h, n;
PeakEvent e[PEAK_Q];
} PeakQ;
static void
peak_q_push(PeakQ *q, PeakEvent ev)
{
if (!q || q->n == PEAK_Q)
return;
q->e[(q->h + q->n++) % PEAK_Q] = ev;
}
static int
peak_q_pop(PeakQ *q, PeakEvent *ev)
{
if (!q || !q->n)
return 0;
*ev = q->e[q->h];
q->h = (q->h + 1) % PEAK_Q;
q->n--;
return 1;
}
#endif
#if defined(PEAK_WIN32)
#include "p_win32.c" // <REPLACE>
#elif defined(PEAK_LINUX)
#include "p_linux.c" // <REPLACE>
#elif defined(PEAK_WEB)
#include "p_emscripten.c" // <REPLACE>
#endif
/* We define PeakWindow only when we know the type of peak_window_internal_t */
struct PeakWindow {
PeakWindowInternal internal;
int (*tick)(struct PeakWindow *win, void *userdata);
void *userdata;
uint32_t *buffer;
uint32_t width;
uint32_t height;
uint32_t bufsize;
int running;
};
#include "peak.c" // <REPLACE>
#endif // PEAK_IMPLEMENTATION
/*
------------------------------------------------------------------------------
This software is available under 2 licenses -- choose whichever you prefer.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2026 Vasco Alves
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------
*/
|