summaryrefslogtreecommitdiffstats
path: root/src/overlay.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/overlay.cpp')
-rw-r--r--src/overlay.cpp140
1 files changed, 140 insertions, 0 deletions
diff --git a/src/overlay.cpp b/src/overlay.cpp
new file mode 100644
index 0000000..2445d95
--- /dev/null
+++ b/src/overlay.cpp
@@ -0,0 +1,140 @@
1// overlay.cpp — the single ISurface access layer.
2#include "hl2h.h"
3#include <ctime>
4
5ISurface *g_surface = nullptr;
6
7// ---------------------------------------------------------------------------
8// per-frame draw budget
9// ---------------------------------------------------------------------------
10
11static bool g_drawn_this_frame = false;
12static double g_frame_mark_at = 0.0;
13
14static double overlay_now() {
15 timespec ts;
16 clock_gettime(CLOCK_MONOTONIC, &ts);
17 return (double)ts.tv_sec + (double)ts.tv_nsec * 1e-9;
18}
19
20void overlay_frame_mark() {
21 g_drawn_this_frame = false;
22 g_frame_mark_at = overlay_now();
23}
24
25bool overlay_should_draw() {
26 if (g_drawn_this_frame && overlay_now() - g_frame_mark_at < 0.10)
27 return false;
28 g_drawn_this_frame = true;
29 return true;
30}
31
32void overlay_color(int r, int g, int b, int a) {
33 if (!g_surface)
34 return;
35 using Fn = void (*)(void *, int, int, int, int);
36 vfunc<Fn>(g_surface, SURF_DrawSetColor)(g_surface, r, g, b, a);
37}
38
39void overlay_fill(int x0, int y0, int x1, int y1) {
40 if (!g_surface)
41 return;
42 using Fn = void (*)(void *, int, int, int, int);
43 vfunc<Fn>(g_surface, SURF_DrawFilledRect)(g_surface, x0, y0, x1, y1);
44}
45
46void overlay_outline_rect(int x0, int y0, int x1, int y1) {
47 if (!g_surface)
48 return;
49 using Fn = void (*)(void *, int, int, int, int);
50 vfunc<Fn>(g_surface, SURF_DrawOutlinedRect)(g_surface, x0, y0, x1, y1);
51}
52
53void overlay_line(int x0, int y0, int x1, int y1) {
54 if (!g_surface)
55 return;
56 using Fn = void (*)(void *, int, int, int, int);
57 vfunc<Fn>(g_surface, SURF_DrawLine)(g_surface, x0, y0, x1, y1);
58}
59
60void overlay_text_font(HFont f) {
61 if (!g_surface)
62 return;
63 using Fn = void (*)(void *, HFont);
64 vfunc<Fn>(g_surface, SURF_DrawSetTextFont)(g_surface, f);
65}
66
67void overlay_text_color(int r, int g, int b, int a) {
68 if (!g_surface)
69 return;
70 using Fn = void (*)(void *, int, int, int, int);
71 vfunc<Fn>(g_surface, SURF_DrawSetTextColor)(g_surface, r, g, b, a);
72}
73
74void overlay_text_pos(int x, int y) {
75 if (!g_surface)
76 return;
77 using Fn = void (*)(void *, int, int);
78 vfunc<Fn>(g_surface, SURF_DrawSetTextPos)(g_surface, x, y);
79}
80
81void overlay_print_text(const wchar_t *text, int count, int draw_type) {
82 if (!g_surface || !text || count <= 0)
83 return;
84 using Fn = void (*)(void *, const wchar_t *, int, int);
85 vfunc<Fn>(g_surface, SURF_DrawPrintText)(g_surface, text, count, draw_type);
86}
87
88HFont overlay_create_font() {
89 if (!g_surface)
90 return 0;
91 using Fn = HFont (*)(void *);
92 return vfunc<Fn>(g_surface, SURF_CreateFont)(g_surface);
93}
94
95bool overlay_add_glyph_set(HFont f, const char *name, int tall, int weight,
96 int blur, int scanlines, int flags, int range_min,
97 int range_max) {
98 if (!g_surface || !name)
99 return false;
100 using Fn = bool (*)(void *, HFont, const char *, int, int, int, int, int, int,
101 int);
102 return vfunc<Fn>(g_surface, SURF_AddGlyphSetToFont)(
103 g_surface, f, name, tall, weight, blur, scanlines, flags, range_min,
104 range_max);
105}
106
107void overlay_screen_size(int &w, int &h) {
108 if (!g_surface)
109 return;
110 using Fn = void (*)(void *, int &, int &);
111 vfunc<Fn>(g_surface, SURF_GetScreenSize)(g_surface, w, h);
112}
113
114void overlay_text_size(HFont f, const wchar_t *text, int &w, int &h) {
115 if (!g_surface || !text)
116 return;
117 using Fn = void (*)(void *, HFont, const wchar_t *, int &, int &);
118 vfunc<Fn>(g_surface, SURF_GetTextSize)(g_surface, f, text, w, h);
119}
120
121int overlay_embedded_panel() {
122 if (!g_surface)
123 return 0;
124 using Fn = int (*)(void *);
125 return vfunc<Fn>(g_surface, SURF_GetEmbeddedPanel)(g_surface);
126}
127
128void overlay_push_make_current(int panel, bool use_insets) {
129 if (!g_surface)
130 return;
131 using Fn = void (*)(void *, int, bool);
132 vfunc<Fn>(g_surface, SURF_PushMakeCurrent)(g_surface, panel, use_insets);
133}
134
135void overlay_pop_make_current(int panel) {
136 if (!g_surface)
137 return;
138 using Fn = void (*)(void *, int);
139 vfunc<Fn>(g_surface, SURF_PopMakeCurrent)(g_surface, panel);
140}