-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
99 lines (84 loc) · 3.64 KB
/
main.cpp
File metadata and controls
99 lines (84 loc) · 3.64 KB
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
// This project is licensed under the GPL-3.0 License. See the LICENSE file for details, or check out <gnu.org>.
// Copyright (C) 2026 mxreal64
#include <SDL3/SDL.h>
#include <SDL3_ttf/SDL_ttf.h>
import delta.types;
import delta.renderer;
import delta.app;
import std;
int main() {
if (!SDL_Init(SDL_INIT_VIDEO) || !TTF_Init()) {
std::println(std::cerr, "Hardware backend context initialization failure.");
return 1;
}
SDL_Window* window = SDL_CreateWindow("delta", WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_BORDERLESS);
SDL_Renderer* renderer = SDL_CreateRenderer(window, nullptr);
TTF_Font* font_reg = TTF_OpenFont("/usr/share/fonts/noto/NotoSans-Regular.ttf", 24);
if (!font_reg || !renderer || !window) {
std::println(std::cerr, "Subsystem resource allocation failed.");
return 1;
}
DeltaApp app;
MuxCanvasRenderer canvas(renderer, font_reg);
SDL_Event event;
SDL_StartTextInput(window);
// Track state to completely avoid redundant layout compilation or GPU redraws
bool needs_redraw = true;
while (app.is_running()) {
// 1. BLOCKED OS WAIT STATE (0% CPU) hopefully
// This stays asleep until an OS input event occurs, OR 500ms ticks down
// (The 500ms timeout provides the exact heartbeats needed to animate the cursor)
if (SDL_WaitEventTimeout(&event, 500)) {
do {
// Filter the events coming out of the queue to skip redundant calculations
switch (event.type) {
case SDL_EVENT_QUIT:
case SDL_EVENT_TEXT_INPUT:
case SDL_EVENT_KEY_DOWN:
case SDL_EVENT_KEY_UP:
case SDL_EVENT_MOUSE_BUTTON_DOWN:
case SDL_EVENT_MOUSE_BUTTON_UP:
case SDL_EVENT_MOUSE_MOTION:
case SDL_EVENT_WINDOW_EXPOSED:
case SDL_EVENT_WINDOW_PIXEL_SIZE_CHANGED:
{
auto res = app.handle_event(event, font_reg);
if (!res) {
std::println(std::cerr, "App module error: {}", static_cast<int>(res.error()));
}
needs_redraw = true;
}
break;
default:
break;
}
} while (SDL_PollEvent(&event)); // Cleanly drain the remaining packed system frames
} else {
// 2. CURSOR ANIMATION TIMEOUT PULSE
// The 500ms timeout was triggered with zero incoming input events.
// This wakes up the app just enough to toggle the cursor visibility state.
needs_redraw = true;
}
// 3. LAZY RE-RENDERING PIPELINE
// Only run your visual layout compiler and GPU texture pipelines if a change occurred!
if (needs_redraw && app.is_running()) {
auto render_res = canvas.render_layout_blueprint(
app.get_layout(),
app.get_buffer().size(),
app.get_selection()
);
if (!render_res) {
std::println(std::cerr, "Renderer failure: {}", static_cast<int>(render_res.error()));
break;
}
SDL_RenderPresent(renderer);
needs_redraw = false; // System state synchronized. Return to sleep.
}
}
TTF_CloseFont(font_reg);
SDL_StopTextInput(window);
SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window);
TTF_Quit(); SDL_Quit();
return 0;
}
// yup