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
//! [`WindowBackend`]: the generic [`Backend`](retroglyph_core::Backend)
//! implementation for windowed presenters.
use cratePresenter;
use Backend;
use Event;
use ;
use Tile;
use VecDeque;
use Duration;
/// A [`Backend`] built from a [`Presenter`] plus an input event queue.
///
/// `Backend` fuses input and output, which does not fit a window: some event
/// loop owns input, while a per-renderer surface owns output. `WindowBackend`
/// reunites the two so [`Terminal`](retroglyph_core::Terminal) gets the full
/// `Backend` it needs, while renderer crates implement only [`Presenter`]:
///
/// ```text
/// event loop.push_event(e) ──> VecDeque<Event> ──> app.poll_event()
/// │
/// v
/// Terminal<WindowBackend<P>>
/// │
/// draw / flush / resize v
/// ◄──────────────────── WindowBackend
/// │
/// v
/// P: Presenter (output)
/// ```
///
/// With the `winit` feature enabled, `winit::run_windowed` and
/// `winit::run_app` own the event loop, call `push_event` as winit events
/// are translated, and call [`Presenter::present`] once per frame; callers
/// never touch `WindowBackend` directly. With `winit` disabled,
/// `retroglyph-window` exports no event loop at all: a caller driving its
/// own loop (SDL2, tao, a custom driver) constructs
/// `WindowBackend::new(presenter)` itself, calls `push_event` for each
/// translated input event, and calls `Terminal::present` (which drives
/// `Presenter::flush`) plus `presenter_mut().present()` once per frame.
///
/// # Example: driving without `winit`
///
/// ```rust
/// use retroglyph_core::{Backend, Event, Pos, Size, Terminal, Tile};
/// use retroglyph_window::{Presenter, WindowBackend, WindowHandle};
/// use std::sync::Arc;
/// use std::time::Duration;
///
/// struct NullPresenter;
///
/// impl Presenter for NullPresenter {
/// type Error = core::convert::Infallible;
/// type SurfaceError = core::convert::Infallible;
///
/// fn draw<'a, I>(&mut self, _content: I) -> Result<(), Self::Error>
/// where
/// I: Iterator<Item = (Pos, &'a Tile, Option<&'a str>)>,
/// {
/// Ok(())
/// }
///
/// fn draw_layers<'a, I>(&mut self, _content: I) -> Result<(), Self::Error>
/// where
/// I: Iterator<Item = (u8, Pos, &'a Tile, Option<&'a str>)>,
/// {
/// Ok(())
/// }
///
/// fn flush(&mut self) -> Result<(), Self::Error> {
/// Ok(())
/// }
///
/// fn size(&self) -> Size {
/// Size { width: 4, height: 2 }
/// }
///
/// fn clear(&mut self) -> Result<(), Self::Error> {
/// Ok(())
/// }
///
/// fn resize(&mut self, _size: Size) {}
///
/// fn init_surface(&mut self, _window: Arc<dyn WindowHandle>) -> Result<(), Self::SurfaceError> {
/// Ok(())
/// }
///
/// fn resize_surface(&mut self, _width: u32, _height: u32) {}
///
/// fn present(&mut self) -> Result<(), Self::SurfaceError> {
/// Ok(())
/// }
///
/// fn cell_size(&self) -> (u32, u32) {
/// (8, 16)
/// }
/// }
///
/// // A caller driving its own loop (SDL2, tao, a hand-rolled driver) builds
/// // `WindowBackend` directly -- no `winit` feature required.
/// let backend = WindowBackend::new(NullPresenter);
/// let mut term = Terminal::new(backend);
///
/// // The loop pushes each translated input event onto the queue...
/// term.backend_mut().push_event(Event::FocusGained);
///
/// // ...and the app drains it through the normal `Terminal` polling API,
/// // which never blocks for `WindowBackend`.
/// while term.poll(Duration::ZERO).is_some() {}
///
/// // Once per frame: `Terminal::present` diffs the grid and drives
/// // `Presenter::flush`, then the caller drives `Presenter::present` itself
/// // to push pixels to the window.
/// term.present().unwrap();
/// term.backend_mut().presenter_mut().present().unwrap();
/// ```
///
/// [`poll_event`](Backend::poll_event) never blocks: frame timing is owned by
/// the event loop, not by input waits.