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
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
use libc::{c_void, c_int, c_float, uint32_t};
use std::ffi::{CStr, CString};
use std::marker::PhantomData;
use std::mem;
use std::ops::Deref;
use std::ptr;
use std::vec::Vec;
use rect::Rect;
use render::RendererBuilder;
use surface::Surface;
use pixels;
use Sdl;
use SdlResult;
use num::FromPrimitive;
use util::CStringExt;
use get_error;
use sys::video as ll;
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum GLProfile {
Core,
Compatibility,
GLES,
}
trait GLAttrTypeUtil {
fn to_gl_value(self) -> i32;
fn from_gl_value(value: i32) -> Self;
}
impl GLAttrTypeUtil for u8 {
fn to_gl_value(self) -> i32 { self as i32 }
fn from_gl_value(value: i32) -> u8 { value as u8 }
}
impl GLAttrTypeUtil for bool {
fn to_gl_value(self) -> i32 { if self { 1 } else { 0 } }
fn from_gl_value(value: i32) -> bool { value != 0 }
}
impl GLAttrTypeUtil for GLProfile {
fn to_gl_value(self) -> i32 {
use self::GLProfile::*;
match self {
Core => 1,
Compatibility => 2,
GLES => 4,
}
}
fn from_gl_value(value: i32) -> GLProfile {
use self::GLProfile::*;
match value {
1 => Core,
2 => Compatibility,
4 => GLES,
_ => panic!("unknown SDL_GLProfile value: {}", value)
}
}
}
macro_rules! attrs {
(
$(($attr_name:ident, $set_property:ident, $get_property:ident, $t:ty, $doc:expr)),*
) => (
$(
#[doc = "**Sets** the attribute: "]
#[doc = $doc]
#[inline]
pub fn $set_property(value: $t) {
gl_set_attribute!($attr_name, value.to_gl_value());
}
#[doc = "**Gets** the attribute: "]
#[doc = $doc]
#[inline]
pub fn $get_property() -> $t {
let value = gl_get_attribute!($attr_name);
GLAttrTypeUtil::from_gl_value(value)
}
)*
);
}
pub mod gl_attr {
use get_error;
use sys::video as ll;
use super::{GLProfile, GLAttrTypeUtil};
macro_rules! gl_set_attribute {
($attr:ident, $value:expr) => ({
let result = unsafe {
ll::SDL_GL_SetAttribute(ll::SDL_GLattr::$attr, $value)
};
if result != 0 {
panic!("couldn't set attribute {}: {}", stringify!($attr), get_error());
}
})
}
macro_rules! gl_get_attribute {
($attr:ident) => ({
let mut value = 0;
let result = unsafe {
ll::SDL_GL_GetAttribute(ll::SDL_GLattr::$attr, &mut value)
};
if result != 0 {
panic!("couldn't get attribute {}: {}", stringify!($attr), get_error());
}
value
})
}
attrs! {
(SDL_GL_RED_SIZE, set_red_size, red_size, u8,
"the minimum number of bits for the red channel of the color buffer; defaults to 3"),
(SDL_GL_GREEN_SIZE, set_green_size, green_size, u8,
"the minimum number of bits for the green channel of the color buffer; defaults to 3"),
(SDL_GL_BLUE_SIZE, set_blue_size, blue_size, u8,
"the minimum number of bits for the blue channel of the color buffer; defaults to 2"),
(SDL_GL_ALPHA_SIZE, set_alpha_size, alpha_size, u8,
"the minimum number of bits for the alpha channel of the color buffer; defaults to 0"),
(SDL_GL_BUFFER_SIZE, set_buffer_size, buffer_size, u8,
"the minimum number of bits for frame buffer size; defaults to 0"),
(SDL_GL_DOUBLEBUFFER, set_double_buffer, double_buffer, bool,
"whether the output is single or double buffered; defaults to double buffering on"),
(SDL_GL_DEPTH_SIZE, set_depth_size, depth_size, u8,
"the minimum number of bits in the depth buffer; defaults to 16"),
(SDL_GL_STENCIL_SIZE, set_stencil_size, stencil_size, u8,
"the minimum number of bits in the stencil buffer; defaults to 0"),
(SDL_GL_ACCUM_RED_SIZE, set_accum_red_size, accum_red_size, u8,
"the minimum number of bits for the red channel of the accumulation buffer; defaults to 0"),
(SDL_GL_ACCUM_GREEN_SIZE, set_accum_green_size, accum_green_size, u8,
"the minimum number of bits for the green channel of the accumulation buffer; defaults to 0"),
(SDL_GL_ACCUM_BLUE_SIZE, set_accum_blue_size, accum_blue_size, u8,
"the minimum number of bits for the blue channel of the accumulation buffer; defaults to 0"),
(SDL_GL_ACCUM_ALPHA_SIZE, set_accum_alpha_size, accum_alpha_size, u8,
"the minimum number of bits for the alpha channel of the accumulation buffer; defaults to 0"),
(SDL_GL_STEREO, set_stereo, stereo, bool,
"whether the output is stereo 3D; defaults to off"),
(SDL_GL_MULTISAMPLEBUFFERS, set_multisample_buffers, multisample_buffers, u8,
"the number of buffers used for multisample anti-aliasing; defaults to 0"),
(SDL_GL_MULTISAMPLESAMPLES, set_multisample_samples, multisample_samples, u8,
"the number of samples used around the current pixel used for multisample anti-aliasing; defaults to 0"),
(SDL_GL_ACCELERATED_VISUAL, set_accelerated_visual, accelerated_visual, bool,
"whether to require hardware acceleration; false to force software rendering; defaults to allow either"),
(SDL_GL_CONTEXT_MAJOR_VERSION, set_context_major_version, context_major_version, u8,
"OpenGL context major version"),
(SDL_GL_CONTEXT_MINOR_VERSION, set_context_minor_version, context_minor_version, u8,
"OpenGL context minor version"),
(SDL_GL_CONTEXT_PROFILE_MASK, set_context_profile, context_profile, GLProfile,
"type of GL context (Core, Compatibility, ES)"),
(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, set_share_with_current_context, share_with_current_context, bool,
"OpenGL context sharing; defaults to false"),
(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, set_framebuffer_srgb_compatible, framebuffer_srgb_compatible, bool,
"requests sRGB capable visual; defaults to false (>= SDL 2.0.1)")
}
#[inline]
pub fn set_context_version(major: u8, minor: u8) {
set_context_major_version(major);
set_context_minor_version(minor);
}
#[inline]
pub fn context_version() -> (u8, u8) {
(context_major_version(), context_minor_version())
}
pub struct ContextFlagsBuilder {
flags: i32
}
impl ContextFlagsBuilder {
#[inline]
pub fn set(&self) {
gl_set_attribute!(SDL_GL_CONTEXT_FLAGS, self.flags);
}
#[inline]
pub fn debug(&mut self) -> &mut ContextFlagsBuilder {
self.flags |= 0x0001;
self
}
#[inline]
pub fn forward_compatible(&mut self) -> &mut ContextFlagsBuilder {
self.flags |= 0x0002;
self
}
#[inline]
pub fn robust_access(&mut self) -> &mut ContextFlagsBuilder {
self.flags |= 0x0004;
self
}
#[inline]
pub fn reset_isolation(&mut self) -> &mut ContextFlagsBuilder {
self.flags |= 0x0008;
self
}
}
pub struct ContextFlags {
flags: i32
}
impl ContextFlags {
#[inline]
pub fn has_debug(&self) -> bool { self.flags & 0x0001 != 0 }
#[inline]
pub fn has_forward_compatible(&self) -> bool { self.flags & 0x0002 != 0 }
#[inline]
pub fn has_robust_access(&self) -> bool { self.flags & 0x0004 != 0 }
#[inline]
pub fn has_reset_isolation(&self) -> bool { self.flags & 0x0008 != 0 }
}
pub fn set_context_flags() -> ContextFlagsBuilder {
ContextFlagsBuilder {
flags: 0
}
}
pub fn context_flags() -> ContextFlags {
let flags = gl_get_attribute!(SDL_GL_CONTEXT_FLAGS);
ContextFlags {
flags: flags
}
}
}
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub struct DisplayMode {
pub format: u32,
pub w: i32,
pub h: i32,
pub refresh_rate: i32
}
impl DisplayMode {
pub fn new(format: u32, w: i32, h: i32, refresh_rate: i32) -> DisplayMode {
DisplayMode {
format: format,
w: w,
h: h,
refresh_rate: refresh_rate
}
}
pub fn from_ll(raw: &ll::SDL_DisplayMode) -> DisplayMode {
DisplayMode::new(
raw.format as u32,
raw.w as i32,
raw.h as i32,
raw.refresh_rate as i32
)
}
pub fn to_ll(&self) -> ll::SDL_DisplayMode {
ll::SDL_DisplayMode {
format: self.format as uint32_t,
w: self.w as c_int,
h: self.h as c_int,
refresh_rate: self.refresh_rate as c_int,
driverdata: ptr::null_mut()
}
}
}
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum FullscreenType {
FTOff = 0,
FTTrue = 0x00000001,
FTDesktop = 0x00001001,
}
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum WindowPos {
PosUndefined,
PosCentered,
Positioned(i32)
}
fn unwrap_windowpos (pos: WindowPos) -> ll::SDL_WindowPos {
match pos {
WindowPos::PosUndefined => ll::SDL_WINDOWPOS_UNDEFINED,
WindowPos::PosCentered => ll::SDL_WINDOWPOS_CENTERED,
WindowPos::Positioned(x) => x as ll::SDL_WindowPos
}
}
pub struct GLContext {
raw: ll::SDL_GLContext
}
impl Drop for GLContext {
fn drop(&mut self) {
unsafe {
ll::SDL_GL_DeleteContext(self.raw)
}
}
}
impl GLContext {
pub fn is_current(&self) -> bool {
let current_raw = unsafe { ll::SDL_GL_GetCurrentContext() };
self.raw == current_raw
}
}
pub struct Window {
raw: *mut ll::SDL_Window,
owned: bool
}
impl_raw_accessors!(
(GLContext, ll::SDL_GLContext),
(Window, *mut ll::SDL_Window)
);
impl_owned_accessors!(
(Window, owned)
);
impl_raw_constructor!(
(Window, Window (raw: *mut ll::SDL_Window, owned: bool))
);
impl Drop for Window {
fn drop(&mut self) {
if self.owned {
unsafe {
ll::SDL_DestroyWindow(self.raw);
}
}
}
}
pub struct WindowBuilder {
title: CString,
width: u32,
height: u32,
x: WindowPos,
y: WindowPos,
window_flags: u32,
_nosendsync: PhantomData<*mut ()>
}
impl WindowBuilder {
pub fn new(_sdl: &Sdl, title: &str, width: u32, height: u32) -> WindowBuilder {
WindowBuilder {
title: CString::new(title).remove_nul(),
width: width,
height: height,
x: WindowPos::PosUndefined,
y: WindowPos::PosUndefined,
window_flags: 0,
_nosendsync: PhantomData
}
}
pub fn build(&self) -> SdlResult<Window> {
unsafe {
if self.width >= (1<<31) || self.height >= (1<<31) {
Err(format!("Window is too large."))
} else {
let raw_width = self.width as c_int;
let raw_height = self.height as c_int;
let raw = ll::SDL_CreateWindow(
self.title.as_ptr(),
unwrap_windowpos(self.x),
unwrap_windowpos(self.y),
raw_width,
raw_height,
self.window_flags
);
if raw == ptr::null_mut() {
Err(get_error())
} else {
Ok(Window { raw: raw, owned: true })
}
}
}
}
pub fn get_window_flags(&self) -> u32 { self.window_flags }
pub fn set_window_flags(&mut self, flags: u32) -> &mut WindowBuilder {
self.window_flags = flags;
self
}
pub fn position(&mut self, x: i32, y: i32) -> &mut WindowBuilder {
self.x = WindowPos::Positioned(x);
self.y = WindowPos::Positioned(y);
self
}
pub fn position_centered(&mut self) -> &mut WindowBuilder {
self.x = WindowPos::PosCentered;
self.y = WindowPos::PosCentered;
self
}
pub fn fullscreen(&mut self) -> &mut WindowBuilder { self.window_flags |= ll::SDL_WindowFlags::SDL_WINDOW_FULLSCREEN as u32; self }
pub fn fullscreen_desktop(&mut self) -> &mut WindowBuilder { self.window_flags |= ll::SDL_WindowFlags::SDL_WINDOW_FULLSCREEN_DESKTOP as u32; self }
pub fn opengl(&mut self) -> & mut WindowBuilder { self.window_flags |= ll::SDL_WindowFlags::SDL_WINDOW_OPENGL as u32; self }
pub fn hidden(&mut self) -> &mut WindowBuilder { self.window_flags |= ll::SDL_WindowFlags::SDL_WINDOW_HIDDEN as u32; self }
pub fn borderless(&mut self) -> &mut WindowBuilder { self.window_flags |= ll::SDL_WindowFlags::SDL_WINDOW_BORDERLESS as u32; self }
pub fn resizable(&mut self) -> &mut WindowBuilder { self.window_flags |= ll::SDL_WindowFlags::SDL_WINDOW_RESIZABLE as u32; self }
pub fn minimized(&mut self) -> &mut WindowBuilder { self.window_flags |= ll::SDL_WindowFlags::SDL_WINDOW_MINIMIZED as u32; self }
pub fn maximized(&mut self) -> &mut WindowBuilder { self.window_flags |= ll::SDL_WindowFlags::SDL_WINDOW_MAXIMIZED as u32; self }
pub fn input_grabbed(&mut self) -> &mut WindowBuilder { self.window_flags |= ll::SDL_WindowFlags::SDL_WINDOW_INPUT_GRABBED as u32; self }
pub fn allow_highdpi(&mut self) -> &mut WindowBuilder { self.window_flags |= ll::SDL_WindowFlags::SDL_WINDOW_ALLOW_HIGHDPI as u32; self }
}
pub struct WindowProperties<'a> {
raw: *mut ll::SDL_Window,
_marker: PhantomData<&'a ()>
}
pub struct WindowPropertiesGetters<'a> {
window_properties: WindowProperties<'a>
}
impl<'a> Deref for WindowPropertiesGetters<'a> {
type Target = WindowProperties<'a>;
fn deref(&self) -> &WindowProperties<'a> {
&self.window_properties
}
}
impl Window {
pub fn renderer(self) -> RendererBuilder {
RendererBuilder::new(self)
}
pub fn properties<'a>(&'a mut self, _sdl: &'a Sdl) -> WindowProperties<'a> {
WindowProperties {
raw: self.raw,
_marker: PhantomData
}
}
pub fn properties_getters(&self) -> WindowPropertiesGetters {
WindowPropertiesGetters {
window_properties: WindowProperties {
raw: self.raw,
_marker: PhantomData
}
}
}
pub unsafe fn from_id(id: u32) -> SdlResult<Window> {
let raw = ll::SDL_GetWindowFromID(id);
if raw == ptr::null_mut() {
Err(get_error())
} else {
Ok(Window{ raw: raw, owned: false})
}
}
pub fn get_id(&self) -> u32 {
unsafe { ll::SDL_GetWindowID(self.raw) }
}
pub fn gl_create_context(&self) -> SdlResult<GLContext> {
let result = unsafe { ll::SDL_GL_CreateContext(self.raw) };
if result == ptr::null_mut() {
Err(get_error())
} else {
Ok(GLContext{ raw: result })
}
}
pub fn gl_set_context_to_current(&self) -> SdlResult<()> {
unsafe {
let context_raw = ll::SDL_GL_GetCurrentContext();
if context_raw.is_null() {
Err(get_error())
} else {
if ll::SDL_GL_MakeCurrent(self.raw, context_raw) == 0 {
Ok(())
} else {
Err(get_error())
}
}
}
}
pub fn gl_make_current(&self, context: &GLContext) -> SdlResult<()> {
unsafe {
if ll::SDL_GL_MakeCurrent(self.raw, context.raw) == 0 {
Ok(())
} else {
Err(get_error())
}
}
}
pub fn gl_swap_window(&self) {
unsafe { ll::SDL_GL_SwapWindow(self.raw) }
}
}
impl<'a> WindowProperties<'a> {
pub fn get_display_index(&self) -> SdlResult<i32> {
let result = unsafe { ll::SDL_GetWindowDisplayIndex(self.raw) };
if result < 0 {
return Err(get_error())
} else {
Ok(result as i32)
}
}
pub fn set_display_mode(&mut self, display_mode: Option<DisplayMode>) -> SdlResult<()> {
unsafe {
let result = ll::SDL_SetWindowDisplayMode(
self.raw,
match display_mode {
Some(ref mode) => &mode.to_ll(),
None => ptr::null()
}
);
if result < 0 {
Err(get_error())
} else {
Ok(())
}
}
}
pub fn get_display_mode(&self) -> SdlResult<DisplayMode> {
let mut dm = unsafe { mem::uninitialized() };
let result = unsafe {
ll::SDL_GetWindowDisplayMode(
self.raw,
&mut dm
) == 0
};
if result {
Ok(DisplayMode::from_ll(&dm))
} else {
Err(get_error())
}
}
pub fn get_window_pixel_format(&self) -> pixels::PixelFormatEnum {
unsafe{ FromPrimitive::from_u64(ll::SDL_GetWindowPixelFormat(self.raw) as u64).unwrap() }
}
pub fn get_window_flags(&self) -> u32 {
unsafe {
ll::SDL_GetWindowFlags(self.raw)
}
}
pub fn set_title(&mut self, title: &str) {
let title = CString::new(title).remove_nul();
unsafe { ll::SDL_SetWindowTitle(self.raw, title.as_ptr()); }
}
pub fn get_title(&self) -> &str {
use std::ffi::CStr;
use std::str;
unsafe {
let buf = ll::SDL_GetWindowTitle(self.raw);
str::from_utf8(CStr::from_ptr(buf).to_bytes()).unwrap()
}
}
pub fn set_icon(&mut self, icon: &Surface) {
unsafe { ll::SDL_SetWindowIcon(self.raw, icon.raw()) }
}
pub fn set_position(&mut self, x: WindowPos, y: WindowPos) {
unsafe { ll::SDL_SetWindowPosition(self.raw, unwrap_windowpos(x), unwrap_windowpos(y)) }
}
pub fn get_position(&self) -> (i32, i32) {
let mut x: c_int = 0;
let mut y: c_int = 0;
unsafe { ll::SDL_GetWindowPosition(self.raw, &mut x, &mut y) };
(x as i32, y as i32)
}
pub fn set_size(&mut self, w: i32, h: i32) {
unsafe { ll::SDL_SetWindowSize(self.raw, w as c_int, h as c_int) }
}
pub fn get_size(&self) -> (i32, i32) {
let mut w: c_int = 0;
let mut h: c_int = 0;
unsafe { ll::SDL_GetWindowSize(self.raw, &mut w, &mut h) };
(w as i32, h as i32)
}
pub fn get_drawable_size(&self) -> (i32, i32) {
let mut w: c_int = 0;
let mut h: c_int = 0;
unsafe { ll::SDL_GL_GetDrawableSize(self.raw, &mut w, &mut h) };
(w as i32, h as i32)
}
pub fn set_minimum_size(&mut self, w: i32, h: i32) {
unsafe { ll::SDL_SetWindowMinimumSize(self.raw, w as c_int, h as c_int) }
}
pub fn get_minimum_size(&self) -> (i32, i32) {
let mut w: c_int = 0;
let mut h: c_int = 0;
unsafe { ll::SDL_GetWindowMinimumSize(self.raw, &mut w, &mut h) };
(w as i32, h as i32)
}
pub fn set_maximum_size(&mut self, w: i32, h: i32) {
unsafe { ll::SDL_SetWindowMaximumSize(self.raw, w as c_int, h as c_int) }
}
pub fn get_maximum_size(&self) -> (i32, i32) {
let mut w: c_int = 0;
let mut h: c_int = 0;
unsafe { ll::SDL_GetWindowMaximumSize(self.raw, &mut w, &mut h) };
(w as i32, h as i32)
}
pub fn set_bordered(&mut self, bordered: bool) {
unsafe { ll::SDL_SetWindowBordered(self.raw, if bordered { 1 } else { 0 }) }
}
pub fn show(&mut self) {
unsafe { ll::SDL_ShowWindow(self.raw) }
}
pub fn hide(&mut self) {
unsafe { ll::SDL_HideWindow(self.raw) }
}
pub fn raise(&mut self) {
unsafe { ll::SDL_RaiseWindow(self.raw) }
}
pub fn maximize(&mut self) {
unsafe { ll::SDL_MaximizeWindow(self.raw) }
}
pub fn minimize(&mut self) {
unsafe { ll::SDL_MinimizeWindow(self.raw) }
}
pub fn restore(&mut self) {
unsafe { ll::SDL_RestoreWindow(self.raw) }
}
pub fn set_fullscreen(&mut self, fullscreen_type: FullscreenType) -> SdlResult<()> {
unsafe {
if ll::SDL_SetWindowFullscreen(self.raw, fullscreen_type as uint32_t) == 0 {
Ok(())
} else {
Err(get_error())
}
}
}
pub fn get_surface(&mut self) -> SdlResult<Surface> {
let raw = unsafe { ll::SDL_GetWindowSurface(self.raw) };
if raw == ptr::null_mut() {
Err(get_error())
} else {
unsafe { Ok(Surface::from_ll(raw, false)) }
}
}
pub fn update_surface(&self) -> SdlResult<()> {
unsafe {
if ll::SDL_UpdateWindowSurface(self.raw) == 0 {
Ok(())
} else {
Err(get_error())
}
}
}
pub fn update_surface_rects(&self, rects: &[Rect]) -> SdlResult<()> {
unsafe {
if ll::SDL_UpdateWindowSurfaceRects(self.raw, rects.as_ptr(), rects.len() as c_int) == 0 {
Ok(())
} else {
Err(get_error())
}
}
}
pub fn set_grab(&mut self, grabbed: bool) {
unsafe { ll::SDL_SetWindowGrab(self.raw, if grabbed { 1 } else { 0 }) }
}
pub fn get_grab(&self) -> bool {
unsafe { ll::SDL_GetWindowGrab(self.raw) == 1 }
}
pub fn set_brightness(&mut self, brightness: f64) -> SdlResult<()> {
unsafe {
if ll::SDL_SetWindowBrightness(self.raw, brightness as c_float) == 0 {
Ok(())
} else {
Err(get_error())
}
}
}
pub fn get_brightness(&self) -> f64 {
unsafe { ll::SDL_GetWindowBrightness(self.raw) as f64 }
}
pub fn set_gamma_ramp(&mut self, red: Option<&[u16; 256]>, green: Option<&[u16; 256]>, blue: Option<&[u16; 256]>) -> SdlResult<()> {
unsafe {
let unwrapped_red = match red {
Some(values) => values.as_ptr(),
None => ptr::null()
};
let unwrapped_green = match green {
Some(values) => values.as_ptr(),
None => ptr::null()
};
let unwrapped_blue = match blue {
Some(values) => values.as_ptr(),
None => ptr::null()
};
if ll::SDL_SetWindowGammaRamp(self.raw, unwrapped_red, unwrapped_green, unwrapped_blue) == 0 {
Ok(())
} else {
Err(get_error())
}
}
}
pub fn get_gamma_ramp(&self) -> SdlResult<(Vec<u16>, Vec<u16>, Vec<u16>)> {
let mut red: Vec<u16> = Vec::with_capacity(256);
let mut green: Vec<u16> = Vec::with_capacity(256);
let mut blue: Vec<u16> = Vec::with_capacity(256);
let result = unsafe {ll::SDL_GetWindowGammaRamp(self.raw, red.as_mut_ptr(), green.as_mut_ptr(), blue.as_mut_ptr()) == 0};
if result {
Ok((red, green, blue))
} else {
Err(get_error())
}
}
}
pub fn get_num_video_drivers() -> SdlResult<i32> {
let result = unsafe { ll::SDL_GetNumVideoDrivers() };
if result < 0 {
Err(get_error())
} else {
Ok(result as i32)
}
}
pub fn get_video_driver(id: i32) -> String {
unsafe {
let buf = ll::SDL_GetVideoDriver(id as c_int);
String::from_utf8_lossy(CStr::from_ptr(buf).to_bytes()).to_string()
}
}
pub fn video_init(name: &str) -> SdlResult<()> {
let name = try!(CString::new(name).unwrap_or_sdlresult());
let result = unsafe { ll::SDL_VideoInit(name.as_ptr()) };
if result == 0 {
Ok(())
} else {
Err(get_error())
}
}
pub fn video_quit() {
unsafe { ll::SDL_VideoQuit() }
}
pub fn get_current_video_driver() -> String {
unsafe {
let video = ll::SDL_GetCurrentVideoDriver();
String::from_utf8_lossy(CStr::from_ptr(video).to_bytes()).to_string()
}
}
pub fn get_num_video_displays() -> SdlResult<i32> {
let result = unsafe { ll::SDL_GetNumVideoDisplays() };
if result < 0 {
Err(get_error())
} else {
Ok(result as i32)
}
}
pub fn get_display_name(display_index: i32) -> String {
unsafe {
let display = ll::SDL_GetDisplayName(display_index as c_int);
String::from_utf8_lossy(CStr::from_ptr(display).to_bytes()).to_string()
}
}
pub fn get_display_bounds(display_index: i32) -> SdlResult<Rect> {
let mut out: Rect = Rect::new(0, 0, 0, 0);
let result = unsafe { ll::SDL_GetDisplayBounds(display_index as c_int, &mut out) == 0 };
if result {
Ok(out)
} else {
Err(get_error())
}
}
pub fn get_num_display_modes(display_index: i32) -> SdlResult<i32> {
let result = unsafe { ll::SDL_GetNumDisplayModes(display_index as c_int) };
if result < 0 {
Err(get_error())
} else {
Ok(result as i32)
}
}
pub fn get_display_mode(display_index: i32, mode_index: i32) -> SdlResult<DisplayMode> {
let mut dm = unsafe { mem::uninitialized() };
let result = unsafe { ll::SDL_GetDisplayMode(display_index as c_int, mode_index as c_int, &mut dm) == 0};
if result {
Ok(DisplayMode::from_ll(&dm))
} else {
Err(get_error())
}
}
pub fn get_desktop_display_mode(display_index: i32) -> SdlResult<DisplayMode> {
let mut dm = unsafe { mem::uninitialized() };
let result = unsafe { ll::SDL_GetDesktopDisplayMode(display_index as c_int, &mut dm) == 0};
if result {
Ok(DisplayMode::from_ll(&dm))
} else {
Err(get_error())
}
}
pub fn get_current_display_mode(display_index: i32) -> SdlResult<DisplayMode> {
let mut dm = unsafe { mem::uninitialized() };
let result = unsafe { ll::SDL_GetCurrentDisplayMode(display_index as c_int, &mut dm) == 0};
if result {
Ok(DisplayMode::from_ll(&dm))
} else {
Err(get_error())
}
}
pub fn get_closest_display_mode(display_index: i32, mode: &DisplayMode) -> SdlResult<DisplayMode> {
let input = mode.to_ll();
let mut dm = unsafe { mem::uninitialized() };
let result = unsafe { ll::SDL_GetClosestDisplayMode(display_index as c_int, &input, &mut dm) };
if result == ptr::null_mut() {
Err(get_error())
} else {
Ok(DisplayMode::from_ll(&dm))
}
}
pub fn is_screen_saver_enabled() -> bool {
unsafe { ll::SDL_IsScreenSaverEnabled() == 1 }
}
pub fn enable_screen_saver() {
unsafe { ll::SDL_EnableScreenSaver() }
}
pub fn disable_screen_saver() {
unsafe { ll::SDL_DisableScreenSaver() }
}
pub fn gl_load_library<P: AsRef<::std::path::Path>>(path: P) -> SdlResult<()> {
unsafe {
let path = CString::new(path.as_ref().to_str().unwrap()).unwrap();
if ll::SDL_GL_LoadLibrary(path.as_ptr()) == 0 {
Ok(())
} else {
Err(get_error())
}
}
}
pub fn gl_unload_library() {
unsafe { ll::SDL_GL_UnloadLibrary(); }
}
pub fn gl_get_proc_address(procname: &str) -> *const c_void {
match CString::new(procname) {
Ok(procname) => unsafe { ll::SDL_GL_GetProcAddress(procname.as_ptr()) },
Err(_) => ptr::null()
}
}
pub fn gl_extension_supported(extension: &str) -> bool {
match CString::new(extension) {
Ok(extension) => unsafe { ll::SDL_GL_ExtensionSupported(extension.as_ptr()) != 0 },
Err(_) => false
}
}
pub unsafe fn gl_get_current_window() -> SdlResult<Window> {
let raw = ll::SDL_GL_GetCurrentWindow();
if raw == ptr::null_mut() {
Err(get_error())
} else {
Ok(Window{ raw: raw, owned: false })
}
}
pub fn gl_release_current_context() -> SdlResult<()> {
let result = unsafe { ll::SDL_GL_MakeCurrent(ptr::null_mut(), ptr::null()) };
if result == 0 {
Ok(())
} else {
Err(get_error())
}
}
pub fn gl_set_swap_interval(interval: i32) -> bool {
unsafe { ll::SDL_GL_SetSwapInterval(interval as c_int) == 0 }
}
pub fn gl_get_swap_interval() -> i32 {
unsafe { ll::SDL_GL_GetSwapInterval() as i32 }
}