wingl 0.1.2

A minimal opengl windows for the win32 api.
Documentation
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
extern crate alloc;

use core::{cell::RefCell, ffi::{c_ulong, c_ushort, CStr}, marker::PhantomData};
use alloc::{rc::Rc, boxed::Box, collections::VecDeque, ffi::CString};

use windows::{core::PCSTR, Win32::{Foundation::*, UI::WindowsAndMessaging::*}};
use windows::Win32::System::LibraryLoader::{GetModuleHandleA, LoadLibraryA};
use windows::Win32::Graphics::Gdi::{HBRUSH, HDC, GetDC, ValidateRect};
use windows::System::VirtualKey;

use crate::{set_window_long, get_window_long};

/// Fenster Builder
#[derive(Default)]
pub struct WindowBuilder<'a>
{
	title: Option<&'a CStr>,
	position: Position,
	size: Size,
	paint_func: Option<PaintFn>
}

impl<'a> WindowBuilder<'a>
{
	pub fn new() -> Self
	{
		Self
		{
			position: Position::new(CW_USEDEFAULT, CW_USEDEFAULT),
			..Default::default()
		}
	}

	/// Setzt einen Fenstertitel. Der Titel muss null terminiert sein, da die Windows API das so erwartet.
	/// * lpWindowName
	pub fn with_title(mut self, title: &'a CStr) -> Self
	{
		self.title = Some(title);
		self
	}

	/// Legt die Größe des Fensters fest.
	/// * nWidth, nHeight
	pub fn with_size(mut self, size: Size) -> Self
	{
		self.size = size;
		self
	}

	/// Zeichenfunktion, die aufgerufen wird, wenn das Fenster aus dem Event Loop heraus neu gezeichnet werden soll.
	/// Das geschied z.B: wenn ein Bereich des Fensters invalidiert wird oder beim skalieren mit der Maus, da hier der Event-Loop nicht verlassen wird.
	/// 
	/// Die Funktion wird unmittelbar vor SwapBuffers aufgerufen.
	pub fn with_paint_func(mut self, paint_func: PaintFn) -> Self
	{
		self.paint_func = Some(paint_func);
		self
	}

	/// Legt die Position des Fensters fest.
	/// * x, y
	pub fn with_position(mut self, position: Position) -> Self
	{
		self.position = position;
		self
	}

	/// Erstellt ein Fenster mit den angegebenen Eigenschaften.
	pub fn build(self) -> windows::core::Result<Window<NotCurrent>>
	{
		Window::create(self)
	}
}

pub type PaintFn = Box<dyn Fn(&Box<ContextTarget>)>;

/// Ein Windows Fenster mit Opengl Kontext.
pub struct Window<S: CurrentState>
{
	handle: HWND,
	hdc: HDC,
	data: *mut ContextTarget,
	gl_context: GLContext,
	events: Rc<RefCell<VecDeque<Event>>>,
	_marker: PhantomData<S>
}

impl Window<NotCurrent>
{
	/// Macht den Grafikkontext des Fensters zum aktuellen Kontext.
	pub fn make_current(mut self) -> Result<Window<PossibleCurrent>, windows::core::Error>
	{
		self.gl_context.make_current(self.hdc)?;
		unsafe { core::mem::transmute(self) }
	}

	fn PixelFormatDescriptor() -> PIXELFORMATDESCRIPTOR
	{
		PIXELFORMATDESCRIPTOR
		{
			dwFlags: PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
			iPixelType: PFD_TYPE_RGBA,
			cColorBits: 32,
			iLayerType: PFD_MAIN_PLANE,
			..Default::default()
		}
	}
}

impl<S: CurrentState> Window<S>
{
	fn create(builder: WindowBuilder) -> windows::core::Result<Self>
	{
		unsafe { SetProcessDPIAware(); }
		let size: Size = builder.size;
		let gl_context = GLContext::new();

		let (handle, hdc, data) = unsafe { Self::create_window(size, builder)? };

		Ok(Self
		{
			handle,
			hdc,
			data,
			events: unsafe { (*data).events.clone() },
			gl_context,
			_marker: Default::default()
		})
	}

	unsafe fn create_window(size: Size, builder: WindowBuilder) -> windows::core::Result<(HWND, HDC, *mut ContextTarget)>
	{
		let instance = GetModuleHandleA(None)?;
		let window_class = PCSTR::from_raw(c"Wingl Window".as_ptr() as *const u8);

		let wc = WNDCLASSA
		{
			//hIcon: LoadIconA(instance, PCSTR::from_raw("IDI_ICON".as_ptr()))?,
			hCursor: LoadCursorW(None, IDC_ARROW)?,
			hInstance: instance.into(),
			lpszClassName: window_class,

			style: CS_OWNDC | CS_HREDRAW | CS_VREDRAW, // Neuzeichnen, bei Verschieben oder Skalieren
			lpfnWndProc: Some(Window::<S>::event_callback),
			hbrBackground: HBRUSH(2), // Schwarz
			..Default::default()
		};

		let atom = RegisterClassA(&wc);
		debug_assert!(atom != 0);

		let mut rect = RECT { left: 0, top: 0, right: size.width as i32, bottom: size.height as i32 };
		AdjustWindowRect(&mut rect, WS_OVERLAPPEDWINDOW, None)?;

		let title = PCSTR::from_raw(builder.title.map_or(core::ptr::null(), |t|t.as_ptr() as *const u8));
		let handle: HWND = CreateWindowExA(
			WINDOW_EX_STYLE::default(),
			window_class,
			title,
			WS_OVERLAPPEDWINDOW | WS_VISIBLE,
			builder.position.x,
			builder.position.y,
			rect.right - rect.left,
			rect.bottom - rect.top,
			None,
			None,
			instance,
			None,
		);
		
		let mut data = Box::new(ContextTarget::default());

		let hdc = GetDC(handle);
		data.hdc = hdc;
		data.window_size = size;
		data.paint_func = builder.paint_func;
		
		let data = Box::into_raw(data);
		set_window_long(handle, GWL_USERDATA, data as isize);

		let pfd = Window::PixelFormatDescriptor();
		let pixel_format = ChoosePixelFormat(hdc, &pfd);
		assert!(SetPixelFormat(hdc, pixel_format, &pfd) == true);

		Ok((handle, hdc, data))
	}

	/// Fordert eine bestimmte OpenGL Version für den Kontext an.
	/// Das ist notwendig für Versionen größer `1.1`
	pub fn request_gl_version(&mut self, version: (i32, i32)) -> bool
	{
		self.gl_context.request_gl_version(self.hdc, version)
	}

	/// Führt den Event Loop aus und terminiert sobald keine Events mehr vorhanden sind.
	/// Der große Vorteil dieser Methode ist, dass das ausführende Programm wieder die volle Kontrolle über den Thread hat.
	/// Die Verarbeitung der Events erfolg durch `next`.
	pub fn poll(&mut self) -> bool
	{
		unsafe
		{
			let mut msg = MSG::default();
			//PostMessageA(self.handle, *USER_EVENT_MSG_ID, None, None).expect("Message failed");
			while PeekMessageA(&mut msg, None, 0, 0, PM_REMOVE).into()
			{
				//TranslateMessage(&msg);
				DispatchMessageA(&msg);
			}
		}
		true
	}

	/// Gibt das nächste Event aus dem Polling zurück, falls vorhanden.
	pub fn next(&mut self) -> Option<Event>
	{
		self.events.borrow_mut().pop_front()
	}

	/// OpenGL: SwapBuffers
	pub fn swap_buffers(&self)
	{
		unsafe { SwapBuffers(self.hdc) };
	}

	/// Gibt die Größe des Fensters zurück.
	pub fn size(&self) -> Size
	{
		unsafe { (*self.data).window_size }
	}
	
	/// Setzt die Größe des Fensters.
	pub fn resize(&mut self, size: Size)
	{
		let mut w = size.width as i32;
		let mut h = size.height as i32;
		let mut rect = RECT { top: 0, left: 0, right: w, bottom: h };
		unsafe
		{
			if AdjustWindowRect(&mut rect, WS_OVERLAPPEDWINDOW, None).is_ok()
			{
				w = rect.right - rect.left;
				h = rect.bottom - rect.top;
				SetWindowPos(self.handle, None, 0, 0, w, h, SWP_NOMOVE).expect("Resize failed");
			}
		}
	}

	/// Gibt den Fensterhandle zurück.
	pub fn handle(&self) -> HWND
	{
		self.handle
	}

	/// Setzt die Zeichenfunktion, die aufgerufen wird, wenn das Fenster aus dem Event Loop heraus neu gezeichnet werden soll.
	/// Das geschied z.B: wenn ein Bereich des Fensters invalidiert wird oder beim skalieren mit der Maus, da hier der Event-Loop nicht verlassen wird.
	/// 
	/// Die Funktion wird unmittelbar vor SwapBuffers aufgerufen.
	pub fn set_paint_callback(&mut self, paint_func: PaintFn)
	{
		unsafe 
		{
			(*self.data).paint_func = Some(paint_func);
		}
	}

	unsafe extern "system" fn event_callback(window: HWND, msg: u32, wparam: WPARAM, lparam: LPARAM) -> LRESULT
	{
		let data_ptr = get_window_long(window, GWL_USERDATA) as *mut ContextTarget;
		if data_ptr.is_null()
		{
			return DefWindowProcA(window, msg, wparam, lparam);
		}
		let mut context_target = Box::from_raw(data_ptr);

		let result = match msg
		{
			WM_PAINT => {
				ValidateRect(window, None);
				if let Some(f) = &context_target.paint_func
				{
					f(&context_target);
					SwapBuffers(context_target.hdc);
				}
				LRESULT(0)
			},
			WM_CLOSE => {
				let _ = DestroyWindow(window);
				LRESULT(0)
			}
			WM_NCDESTROY => {
				set_window_long(window, GWL_USERDATA, 0);
				LRESULT(0)
			}
			WM_DESTROY => {
				PostQuitMessage(0);
				context_target.send(Event::Exit);
				LRESULT(0)
			},
			WM_KEYDOWN => {
				context_target.send(Event::KeyPressed(VirtualKey(wparam.0 as i32)));
				LRESULT(0)
			}
			WM_KEYUP => {
				context_target.send(Event::KeyReleased(VirtualKey(wparam.0 as i32)));
				LRESULT(0)
			}
			WM_WINDOWPOSCHANGED => {
				let new_pos = lparam.0 as *const WINDOWPOS;
				let mut w = (*new_pos).cx;
				let mut h = (*new_pos).cy;
				let mut rect = RECT::default();
				if AdjustWindowRect(&mut rect, WS_OVERLAPPEDWINDOW, None).is_ok()
				{
					w -= rect.right - rect.left;
					h -= rect.bottom - rect.top;
				}
				let size = Size::new(w as i16, h as i16);
				context_target.window_size = size;
				context_target.send(Event::Resized(size));
				LRESULT(0)
			}
			WM_NCCREATE => LRESULT(1),
			WM_CREATE => LRESULT(0),
			_ => DefWindowProcA(window, msg, wparam, lparam),
		};
		let _ = Box::into_raw(context_target);
		result
	}
}

impl Window<PossibleCurrent>
{
	/// Gibt den aktuellen OpenGL Kontext zurück.
	pub fn gl_context(&self) -> &GLContext
	{
		&self.gl_context
	}
}

impl<S: CurrentState> Drop for Window<S>
{
	fn drop(&mut self)
	{
		unsafe
		{
			if let Some(handle) = self.gl_context.current_handle
			{
				wglDeleteContext(handle);
			}
			drop(Box::from_raw(self.data));
		}
	}
}

#[derive(Default, Debug, PartialEq, Clone, Copy)]
pub struct Size
{
	pub width: i16,
	pub height: i16,
}

impl Size
{
	pub fn new(width: i16, height: i16) -> Size
	{
		Self { width, height }
	}
}

#[derive(Default, Debug, PartialEq, Clone, Copy)]
pub struct Position
{
	pub x: i32,
	pub y: i32,
}

impl Position
{
	pub fn new(x: i32, y: i32) -> Position
	{
		Self { x, y }
	}
}

/// Die Kontext Daten, für den Event Loop.
#[repr(C)]
#[derive(Default)]
pub struct ContextTarget
{
	hdc: HDC,
	window_size: Size,
	events: Rc<RefCell<VecDeque<Event>>>,
	paint_func: Option<PaintFn>,
}

impl ContextTarget
{
	/// Sendet ein Event an die Event Queue.
	pub fn send(&self, ev: Event)
	{
		self.events.borrow_mut().push_back(ev);
	}

	/// Gibt den Device Context zurück.
	pub fn GetDC(&self) -> HDC
	{
		self.hdc
	}

	/// Gibt die Größe des Fensters zurück.
	pub fn size(&self) -> Size
	{
		self.window_size
	}
}

/// Minimalset an Fenster Events
#[derive(Debug, PartialEq)]
pub enum Event
{
	KeyPressed(VirtualKey),
	KeyReleased(VirtualKey),
	Resized(Size),
	Exit
}

pub trait CurrentState{}
pub struct NotCurrent;
pub struct PossibleCurrent;
impl CurrentState for NotCurrent{}
impl CurrentState for PossibleCurrent{}

/// Opengl Kontext Wrapper
pub struct GLContext
{
	opengl32: HMODULE,
	current_handle: Option<HANDLE>
}

impl GLContext
{
	fn new() -> Self
	{
		unsafe
		{
			let opengl32 = LoadLibraryA(PCSTR(c"opengl32.dll".as_ptr() as *const u8)).unwrap();
			Self
			{
				opengl32,
				current_handle: None
			}
		}
	}

	/// Gibt zurück, ob der Wrapper einen Kontext hat.
	pub fn is_current(&self) -> bool
	{
		self.current_handle.is_some()
	}

	/// Erstellt den Kontext und macht ihn zum aktuellen Kontext.
	fn make_current(&mut self, hdc: HDC) -> Result<(), windows::core::Error>
	{
		unsafe
		{
			let gl_context = wglCreateContext(hdc);
			if !wglMakeCurrent(hdc, gl_context).as_bool()
			{
				return GetLastError();
			}
			
			self.current_handle = Some(gl_context);
		}
		Ok(())
	}

	/// Fordert eine bestimmte OpenGL Version für den Kontext an.
	/// Das ist notwendig für Versionen größer `1.1`
	pub fn request_gl_version(&mut self, hdc: HDC, version: (i32, i32)) -> bool
	{
		unsafe
		{
			if let Some(handle) = self.current_handle
			{
				//let wglChoosePixelFormatARB: wglChoosePixelFormatARB_t = core::mem::transmute(self.get_proc_address(&"wglChoosePixelFormatARB"));
				let wglCreateContextAttribsARB: wglCreateContextAttribsARB_t = core::mem::transmute(self.get_proc_address(&"wglCreateContextAttribsARB"));

				if let Some(create) = wglCreateContextAttribsARB
				{
					let attributs = [
						WGL_CONTEXT_MAJOR_VERSION_ARB, version.0,
						WGL_CONTEXT_MINOR_VERSION_ARB, version.1,
						0];
					let gl_context = create(hdc, handle, attributs.as_ptr());
					assert!(wglMakeCurrent(hdc, gl_context).as_bool() == true);
					wglDeleteContext(handle);
					self.current_handle = Some(gl_context);
					return true;
				}
			}
		}
		false
	}

	/// Ruft die Adresse einer exportierten Funktion ab.
	/// 
	/// Wird von der GL Binding Library verwendet, um die OpenGL Funktionen zu laden.
	/// ```ignore
	/// gl::load_with(|ptr| gl_context.get_proc_address(ptr) as *const _);
	/// ```
	pub fn get_proc_address(&self, addr: &str) -> *const core::ffi::c_void
	{
		let addr = CString::new(addr.as_bytes()).unwrap();
		let addr = addr.as_ptr();
	
		unsafe
		{
			let p = wglGetProcAddress(addr) as *const core::ffi::c_void;
			if !p.is_null() { return p; }

			GetProcAddress(self.opengl32, addr) as *const _
		}
	}
}

#[repr(C)]
pub struct PIXELFORMATDESCRIPTOR
{
	pub nSize: WORD,
	pub nVersion: WORD,
	pub dwFlags: DWORD,
	pub iPixelType: BYTE,
	pub cColorBits: BYTE,
	pub cRedBits: BYTE,
	pub cRedShift: BYTE,
	pub cGreenBits: BYTE,
	pub cGreenShift: BYTE,
	pub cBlueBits: BYTE,
	pub cBlueShift: BYTE,
	pub cAlphaBits: BYTE,
	pub cAlphaShift: BYTE,
	pub cAccumBits: BYTE,
	pub cAccumRedBits: BYTE,
	pub cAccumGreenBits: BYTE,
	pub cAccumBlueBits: BYTE,
	pub cAccumAlphaBits: BYTE,
	pub cDepthBits: BYTE,
	pub cStencilBits: BYTE,
	pub cAuxBuffers: BYTE,
	pub iLayerType: BYTE,
	pub bReserved: BYTE,
	pub dwLayerMask: DWORD,
	pub dwVisibleMask: DWORD,
	pub dwDamageMask: DWORD,
}

impl Default for PIXELFORMATDESCRIPTOR
{
	#[inline]
	fn default() -> Self
	{
		let mut out: Self = unsafe { core::mem::zeroed() };
		out.nSize = core::mem::size_of::<Self>() as WORD;
		out.nVersion = 1;
		out
	}
}

/// [`PIXELFORMATDESCRIPTOR`] pixel type
pub const PFD_TYPE_RGBA: u8 = 0;
/// [`PIXELFORMATDESCRIPTOR`] pixel type
pub const PFD_TYPE_COLORINDEX: u8 = 1;

/// [`PIXELFORMATDESCRIPTOR`] layer type
pub const PFD_MAIN_PLANE: u8 = 0;
/// [`PIXELFORMATDESCRIPTOR`] layer type
pub const PFD_OVERLAY_PLANE: u8 = 1;
/// [`PIXELFORMATDESCRIPTOR`] layer type
pub const PFD_UNDERLAY_PLANE: u8 = u8::MAX /* was (-1) */;

pub const PFD_DOUBLEBUFFER: u32 = 0x00000001;
pub const PFD_STEREO: u32 = 0x00000002;
pub const PFD_DRAW_TO_WINDOW: u32 = 0x00000004;
pub const PFD_DRAW_TO_BITMAP: u32 = 0x00000008;
pub const PFD_SUPPORT_GDI: u32 = 0x00000010;
pub const PFD_SUPPORT_OPENGL: u32 = 0x00000020;
pub const PFD_GENERIC_FORMAT: u32 = 0x00000040;
pub const PFD_NEED_PALETTE: u32 = 0x00000080;
pub const PFD_NEED_SYSTEM_PALETTE: u32 = 0x00000100;
pub const PFD_SWAP_EXCHANGE: u32 = 0x00000200;
pub const PFD_SWAP_COPY: u32 = 0x00000400;
pub const PFD_SWAP_LAYER_BUFFERS: u32 = 0x00000800;
pub const PFD_GENERIC_ACCELERATED: u32 = 0x00001000;
pub const PFD_SUPPORT_DIRECTDRAW: u32 = 0x00002000;
pub const PFD_DIRECT3D_ACCELERATED: u32 = 0x00004000;
pub const PFD_SUPPORT_COMPOSITION: u32 = 0x00008000;

/// use with [`ChoosePixelFormat`] only
pub const PFD_DEPTH_DONTCARE: u32 = 0x20000000;
/// use with [`ChoosePixelFormat`] only
pub const PFD_DOUBLEBUFFER_DONTCARE: u32 = 0x40000000;
/// use with [`ChoosePixelFormat`] only
pub const PFD_STEREO_DONTCARE: u32 = 0x80000000;

type c_char = i8;
type c_int = i32;
type BYTE = u8;
type WORD = c_ushort;
type DWORD = c_ulong;


pub type HGLRC = HANDLE;
/// Pointer to an ANSI string.
pub type LPCSTR = *const c_char;
/// Pointer to a procedure of unknown type.
pub type PROC = *mut core::ffi::c_void;

const WGL_CONTEXT_MAJOR_VERSION_ARB: i32 = 0x2091;
const WGL_CONTEXT_MINOR_VERSION_ARB: i32 = 0x2092;

#[link(name = "Opengl32")]
extern "system" 
{
	/// [`wglCreateContext`](https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-wglcreatecontext)
	pub fn wglCreateContext(Arg1: HDC) -> HGLRC;

	/// [`wglDeleteContext`](https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-wgldeletecontext)
	pub fn wglDeleteContext(Arg1: HGLRC) -> BOOL;

	/// [`wglMakeCurrent`](https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-wglmakecurrent)
	pub fn wglMakeCurrent(hdc: HDC, hglrc: HGLRC) -> BOOL;

	/// [`wglGetProcAddress`](https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-wglgetprocaddress)
	pub fn wglGetProcAddress(Arg1: LPCSTR) -> PROC;
}

pub type FARPROC = *mut core::ffi::c_void;

#[link(name = "kernel32", kind = "dylib")]
extern "system"
{
	/// [`GetProcAddress`](https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-getprocaddress)
	pub fn GetProcAddress(hModule: HMODULE, lpProcName: LPCSTR) -> FARPROC;
}

#[link(name = "Gdi32")]
extern "system"
{
	/// [`ChoosePixelFormat`](https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-choosepixelformat)
	pub fn ChoosePixelFormat(hdc: HDC, ppfd: *const PIXELFORMATDESCRIPTOR) -> c_int;

	/// [`SetPixelFormat`](https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-setpixelformat)
	pub fn SetPixelFormat(hdc: HDC, format: c_int, ppfd: *const PIXELFORMATDESCRIPTOR) -> BOOL;

	/// [`SwapBuffers`](https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-swapbuffers)
	pub fn SwapBuffers(Arg1: HDC) -> BOOL;
}

/// Type for [wglCreateContextAttribsARB](https://www.khronos.org/registry/OpenGL/extensions/ARB/WGL_ARB_create_context.txt)
type wglCreateContextAttribsARB_t = Option<unsafe extern "system" fn(hDC: HDC, hShareContext: HGLRC, attribList: *const c_int) -> HGLRC>;