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
#![allow(dead_code)]
use std::fmt;
pub use rokol_ffi::gfx::sg_context_desc as SgContextDesc;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use crate::gfx as rg;
#[derive(Debug, Clone, Copy)]
#[repr(u32)]
pub enum ColorFormat {
Rgba8 = rg::PixelFormat::Rgba8 as u32,
Bgra8 = rg::PixelFormat::Bgra8 as u32,
}
impl ColorFormat {
pub fn to_ffi(self) -> rokol_ffi::gfx::sg_pixel_format {
match self {
Self::Rgba8 => rokol_ffi::gfx::sg_pixel_format::SG_PIXELFORMAT_RGBA8,
Self::Bgra8 => rokol_ffi::gfx::sg_pixel_format::SG_PIXELFORMAT_BGRA8,
}
}
}
#[derive(Debug, Clone, Copy)]
#[repr(u32)]
pub enum DepthFormat {
Depth = rg::PixelFormat::Depth as u32,
DepthStencil = rg::PixelFormat::DepthStencil as u32,
}
impl DepthFormat {
pub fn to_ffi(self) -> rokol_ffi::gfx::sg_pixel_format {
match self {
Self::Depth => rokol_ffi::gfx::sg_pixel_format::SG_PIXELFORMAT_DEPTH,
Self::DepthStencil => rokol_ffi::gfx::sg_pixel_format::SG_PIXELFORMAT_DEPTH_STENCIL,
}
}
}
#[derive(Debug, Clone)]
pub struct ResourceSettings {
pub color_format: ColorFormat,
pub depth_format: DepthFormat,
pub sample_count: u32,
}
impl Default for ResourceSettings {
fn default() -> Self {
Self {
color_format: ColorFormat::Rgba8,
depth_format: DepthFormat::Depth,
sample_count: 1,
}
}
}
impl ResourceSettings {
fn apply(&self, desc: &mut SgContextDesc) {
desc.color_format = self.color_format.to_ffi();
desc.depth_format = self.depth_format.to_ffi();
desc.sample_count = self.sample_count as i32;
}
#[cfg(rokol_gfx = "glcore33")]
fn create_context(&self) -> SgContextDesc {
let mut desc = SgContextDesc::default();
self.apply(&mut desc);
desc
}
pub fn init_gfx(&self) {
let desc = rokol_ffi::gfx::sg_desc {
context: self.create_context(),
..Default::default()
};
unsafe {
rokol_ffi::gfx::sg_setup(&desc as *const _);
}
}
}
pub struct WindowHandle {
pub sdl: sdl2::Sdl,
pub vid: sdl2::VideoSubsystem,
pub win: sdl2::video::Window,
#[cfg(rokol_gfx = "glcore33")]
pub gcx: sdl2::video::GLContext,
}
impl WindowHandle {
#[cfg(rokol_gfx = "glcore33")]
pub fn swap_window(&self) {
self.win.gl_swap_window();
}
}
impl fmt::Debug for WindowHandle {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("WindowHandle")
.field("sdl", &"<sdl2::Sdl>")
.field("vid", &self.vid)
.field("win", &"<sdl2::video::Window>")
.field("gcx", &"<sdl2::video::GLContext>")
.finish()
}
}
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Init {
pub title: String,
pub w: u32,
pub h: u32,
pub use_high_dpi: bool,
#[cfg_attr(feature = "serde", serde(skip))]
pub settings: ResourceSettings,
}
impl Default for Init {
fn default() -> Self {
Self {
title: "unnamed".to_string(),
w: 1280,
h: 720,
use_high_dpi: false,
settings: Default::default(),
}
}
}
impl Init {
#[cfg(rokol_gfx = "glcore33")]
pub fn init(
&self,
mut f: impl FnMut(&mut sdl2::video::WindowBuilder),
) -> Result<WindowHandle, String> {
let sdl = sdl2::init()?;
let vid = sdl.video()?;
{
let attr = vid.gl_attr();
attr.set_context_profile(sdl2::video::GLProfile::Core);
attr.set_context_version(3, 3);
}
let win = {
let mut b = vid.window(&self.title, self.w, self.h);
b.opengl();
if self.use_high_dpi {
b.allow_highdpi();
}
f(&mut b);
b.build().map_err(|e| e.to_string())?
};
let gcx = win.gl_create_context()?;
self.settings.init_gfx();
Ok(WindowHandle { sdl, vid, win, gcx })
}
}