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
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
//! 2D accelerated rendering
//!
//! Official C documentation: https://wiki.libsdl.org/CategoryRender
//! # Introduction
//!
//! This module contains functions for 2D accelerated rendering.
//!
//! This API supports the following features:
//!
//! * single pixel points
//! * single pixel lines
//! * filled rectangles
//! * texture images
//! * All of these may be drawn in opaque, blended, or additive modes.
//!
//! The texture images can have an additional color tint or alpha modulation
//! applied to them, and may also be stretched with linear interpolation,
//! rotated or flipped/mirrored.
//!
//! For advanced functionality like particle effects or actual 3D you should use
//! SDL's OpenGL/Direct3D support or one of the many available 3D engines.
//!
//! This API is not designed to be used from multiple threads, see
//! [this bug](http://bugzilla.libsdl.org/show_bug.cgi?id=1995) for details.
//!
//! ---
//!
//! None of the draw methods in `RenderDrawer` are expected to fail.
//! If they do, a panic is raised and the program is aborted.

use Sdl;
use video::{Window, WindowProperties, WindowPropertiesGetters};
use surface;
use surface::Surface;
use pixels;
use pixels::PixelFormatEnum;
use get_error;
use SdlResult;
use std::mem;
use std::ptr;
use libc::{c_int, uint32_t, c_double, c_void};
use rect::Point;
use rect::Rect;
use std::cell::UnsafeCell;
use std::ffi::CStr;
use num::FromPrimitive;
use std::vec::Vec;
use std::rc::Rc;

use sys::render as ll;

#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum TextureAccess {
    Static = ll::SDL_TEXTUREACCESS_STATIC as isize,
    Streaming = ll::SDL_TEXTUREACCESS_STREAMING as isize,
    Target = ll::SDL_TEXTUREACCESS_TARGET as isize
}

impl FromPrimitive for TextureAccess {
    fn from_i64(n: i64) -> Option<TextureAccess> {
        use self::TextureAccess::*;

        Some( match n as ll::SDL_TextureAccess {
            ll::SDL_TEXTUREACCESS_STATIC    => Static,
            ll::SDL_TEXTUREACCESS_STREAMING => Streaming,
            ll::SDL_TEXTUREACCESS_TARGET    => Target,
            _                               => return None,
        })
    }

    fn from_u64(n: u64) -> Option<TextureAccess> { FromPrimitive::from_i64(n as i64) }
}

/// A structure that contains information on the capabilities of a render driver
/// or the current render context.
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub struct RendererInfo {
    pub name: String,
    pub flags: u32,
    pub texture_formats: Vec<pixels::PixelFormatEnum>,
    pub max_texture_width: i32,
    pub max_texture_height: i32
}

#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum BlendMode {
    None = ll::SDL_BLENDMODE_NONE as isize,
    Blend = ll::SDL_BLENDMODE_BLEND as isize,
    Add = ll::SDL_BLENDMODE_ADD as isize,
    Mod = ll::SDL_BLENDMODE_MOD as isize
}

impl FromPrimitive for BlendMode {
    fn from_i64(n: i64) -> Option<BlendMode> {
        use self::BlendMode::*;

        Some( match n as ll::SDL_BlendMode {
            ll::SDL_BLENDMODE_NONE  => None,
            ll::SDL_BLENDMODE_BLEND => Blend,
            ll::SDL_BLENDMODE_ADD   => Add,
            ll::SDL_BLENDMODE_MOD   => Mod,
            _                       => return Option::None,
        })
    }

    fn from_u64(n: u64) -> Option<BlendMode> { FromPrimitive::from_i64(n as i64) }
}

impl RendererInfo {
    pub unsafe fn from_ll(info: &ll::SDL_RendererInfo) -> RendererInfo {
        let texture_formats: Vec<pixels::PixelFormatEnum> = info.texture_formats[0..(info.num_texture_formats as usize)].iter().map(|&format| {
            FromPrimitive::from_i64(format as i64).unwrap()
        }).collect();

        RendererInfo {
            name: String::from_utf8_lossy(CStr::from_ptr(info.name).to_bytes()).to_string(),
            flags: info.flags,
            texture_formats: texture_formats,
            max_texture_width: info.max_texture_width as i32,
            max_texture_height: info.max_texture_height as i32
        }
    }
}

pub enum RendererParent<'a> {
    Surface(Surface<'a>),
    Window(Window)
}

/// 2D rendering context
pub struct Renderer<'a> {
    raw: *mut ll::SDL_Renderer,
    parent: Option<RendererParent<'a>>,
    is_alive: Rc<UnsafeCell<bool>>
}

impl<'a> Drop for Renderer<'a> {
    fn drop(&mut self) {
        unsafe {
            *self.is_alive.get() = false;
            ll::SDL_DestroyRenderer(self.raw);
        };
    }
}

/// The type that allows you to build Window-based renderers.
///
/// By default, the renderer builder will prioritize for a hardware-accelerated renderer.
pub struct RendererBuilder {
    window: Window,
    index: i32,
    renderer_flags: u32
}

impl RendererBuilder {
    /// Initializes a new `RendererBuilder`.
    pub fn new(window: Window) -> RendererBuilder {
        RendererBuilder {
            window: window,
            // -1 means to initialize the first rendering driver supporting the renderer flags
            index: -1,
            // no flags gives priority to available SDL_RENDERER_ACCELERATED renderers
            renderer_flags: 0
        }
    }

    /// Builds the renderer.
    pub fn build(self) -> SdlResult<Renderer<'static>> {
        let raw = unsafe {
            ll::SDL_CreateRenderer(self.window.raw(), self.index, self.renderer_flags)
        };

        if raw.is_null() {
            Err(get_error())
        } else {
            unsafe {
                Ok(Renderer::from_ll(raw, RendererParent::Window(self.window)))
            }
        }
    }

    /// Sets the index of the rendering driver to initialize.
    pub fn index(mut self, index: i32) -> RendererBuilder {
        self.index = index;
        self
    }

    /// Set the renderer to a software fallback.
    pub fn software(mut self) -> RendererBuilder {
        self.renderer_flags |= ll::SDL_RENDERER_SOFTWARE as u32;
        self
    }

    /// Set the renderer to use hardware acceleration.
    pub fn accelerated(mut self) -> RendererBuilder {
        self.renderer_flags |= ll::SDL_RENDERER_ACCELERATED as u32;
        self
    }

    /// Synchronize renderer present with the refresh rate.
    pub fn present_vsync(mut self) -> RendererBuilder {
        self.renderer_flags |= ll::SDL_RENDERER_PRESENTVSYNC as u32;
        self
    }

    /// Set the renderer to support rendering to a texture.
    pub fn target_texture(mut self) -> RendererBuilder {
        self.renderer_flags |= ll::SDL_RENDERER_TARGETTEXTURE as u32;
        self
    }
}

impl<'a> Renderer<'a> {
    /// Creates a 2D software rendering context for a surface.
    pub fn from_surface(surface: surface::Surface<'a>) -> SdlResult<Renderer<'a>> {
        let raw_renderer = unsafe { ll::SDL_CreateSoftwareRenderer(surface.raw()) };
        if raw_renderer != ptr::null_mut() {
            unsafe {
                Ok(Renderer::from_ll(raw_renderer, RendererParent::Surface(surface)))
            }
        } else {
            Err(get_error())
        }
    }

    /// Gets information about the rendering context.
    pub fn get_info(&self) -> RendererInfo {
        unsafe {
            let mut renderer_info_raw = mem::uninitialized();
            if ll::SDL_GetRendererInfo(self.raw, &mut renderer_info_raw) != 0 {
                // Should only fail on an invalid renderer
                panic!();
            } else {
                RendererInfo::from_ll(&renderer_info_raw)
            }
        }
    }

    /// Gets the window or surface the rendering context was created from.
    #[inline]
    pub fn get_parent(&self) -> &RendererParent { self.parent.as_ref().unwrap() }

    #[inline]
    pub fn get_parent_as_window(&self) -> Option<&Window> {
        match self.get_parent() {
            &RendererParent::Window(ref window) => Some(window),
            _ => None
        }
    }

    #[inline]
    pub fn get_parent_as_surface(&self) -> Option<&Surface> {
        match self.get_parent() {
            &RendererParent::Surface(ref surface) => Some(surface),
            _ => None
        }
    }

    /// Accesses the Window properties, such as the position, size and title of a Window.
    /// Returns None if the renderer is not associated with a Window.
    pub fn window_properties<'b>(&'b mut self, sdl: &'b Sdl) -> Option<WindowProperties<'b>>
    {
        match self.parent.as_mut() {
            Some(&mut RendererParent::Window(ref mut window)) => Some(window.properties(sdl)),
            _ => None
        }
    }

    /// Accesses the Window getters, such as the position, size and title of a Window.
    /// Returns None if the renderer is not associated with a Window.
    pub fn window_properties_getters(&self) -> Option<WindowPropertiesGetters>
    {
        match self.parent.as_ref() {
            Some(&RendererParent::Window(ref window)) => Some(window.properties_getters()),
            _ => None
        }
    }

    #[inline]
    pub fn unwrap_parent(mut self) -> RendererParent<'a> {
        use std::mem;
        mem::replace(&mut self.parent, None).unwrap()
    }

    #[inline]
    pub fn unwrap_parent_as_window(self) -> Option<Window> {
        match self.unwrap_parent() {
            RendererParent::Window(window) => Some(window),
            _ => None
        }
    }

    #[inline]
    pub fn unwrap_parent_as_surface(self) -> Option<Surface<'a>> {
        match self.unwrap_parent() {
            RendererParent::Surface(surface) => Some(surface),
            _ => None
        }
    }

    /// Provides drawing methods for the renderer.
    ///
    /// # Examples
    /// ```no_run
    /// use sdl2::render::Renderer;
    /// use sdl2::rect::Rect;
    ///
    /// fn test_draw(renderer: &mut Renderer) {
    ///     let mut drawer = renderer.drawer();
    ///     drawer.clear();
    ///     drawer.draw_rect(Rect::new(50, 50, 150, 175));
    ///     drawer.present();
    /// }
    /// ```
    pub fn drawer(&mut self) -> RenderDrawer {
        RenderDrawer::new(self.raw, &self.is_alive)
    }

    /// Unwraps the window or surface the rendering context was created from.
    pub unsafe fn raw(&self) -> *mut ll::SDL_Renderer { self.raw }

    pub unsafe fn from_ll(raw: *mut ll::SDL_Renderer, parent: RendererParent)
    -> Renderer
    {
        Renderer {
            raw: raw,
            parent: Some(parent),
            is_alive: Rc::new(UnsafeCell::new(true))
        }
    }
}

/// Texture-creating methods for the renderer
impl<'a> Renderer<'a> {
    /// Creates a texture for a rendering context.
    ///
    /// `size` is the width and height of the texture.
    pub fn create_texture(&self, format: pixels::PixelFormatEnum, access: TextureAccess, size: (i32, i32)) -> SdlResult<Texture> {
        let (width, height) = size;

        // If the pixel format is YUV 4:2:0 and planar, the width and height must
        // be multiples-of-two. See issue #334 for details.
        match format {
            PixelFormatEnum::YV12 | PixelFormatEnum::IYUV => {
                if width % 2 != 0 || height % 2 != 0 {
                    return Err(format!("The width and height must be multiples-of-two for planar YUV 4:2:0 pixel formats"));
                }
            },
            _ => ()
        }

        let result = unsafe { ll::SDL_CreateTexture(self.raw, format as uint32_t, access as c_int, width as c_int, height as c_int) };
        if result == ptr::null_mut() {
            Err(get_error())
        } else {
            unsafe { Ok(Texture::from_ll(self, result)) }
        }
    }

    /// Shorthand for `create_texture(format, TextureAccess::Static, size)`
    pub fn create_texture_static(&self, format: pixels::PixelFormatEnum, size: (i32, i32)) -> SdlResult<Texture> {
        self.create_texture(format, TextureAccess::Static, size)
    }

    /// Shorthand for `create_texture(format, TextureAccess::Streaming, size)`
    pub fn create_texture_streaming(&self, format: pixels::PixelFormatEnum, size: (i32, i32)) -> SdlResult<Texture> {
        self.create_texture(format, TextureAccess::Streaming, size)
    }

    /// Shorthand for `create_texture(format, TextureAccess::Target, size)`
    pub fn create_texture_target(&self, format: pixels::PixelFormatEnum, size: (i32, i32)) -> SdlResult<Texture> {
        self.create_texture(format, TextureAccess::Target, size)
    }

    /// Creates a texture from an existing surface.
    /// # Remarks
    /// The access hint for the created texture is `TextureAccess::Static`.
    pub fn create_texture_from_surface(&self, surface: &surface::Surface) -> SdlResult<Texture> {
        let result = unsafe { ll::SDL_CreateTextureFromSurface(self.raw, surface.raw()) };
        if result == ptr::null_mut() {
            Err(get_error())
        } else {
            unsafe { Ok(Texture::from_ll(self, result)) }
        }
    }
}

/// Drawing functionality for the render context.
pub struct RenderDrawer<'renderer> {
    raw: *mut ll::SDL_Renderer,
    is_renderer_alive: &'renderer Rc<UnsafeCell<bool>>
}

/// Render target methods for the drawer
impl<'renderer> RenderDrawer<'renderer> {
    fn new<'l>(raw: *mut ll::SDL_Renderer, is_renderer_alive: &'l Rc<UnsafeCell<bool>>) -> RenderDrawer<'l> {
        RenderDrawer {
            raw: raw,
            is_renderer_alive: is_renderer_alive
        }
    }

    /// Determine whether a window supports the use of render targets.
    pub fn render_target_supported(&self) -> bool {
        unsafe { ll::SDL_RenderTargetSupported(self.raw) == 1 }
    }

    /// Gets the render target handle.
    ///
    /// Returns `None` if the window does not support the use of render targets.
    pub fn render_target(&mut self) -> Option<RenderTarget> {
        if self.render_target_supported() {
            Some(RenderTarget {
                raw: self.raw,
                is_renderer_alive: self.is_renderer_alive
            })
        } else {
            None
        }
    }
}

/// Drawing methods
impl<'renderer> RenderDrawer<'renderer> {
    /// Sets the color used for drawing operations (Rect, Line and Clear).
    pub fn set_draw_color(&mut self, color: pixels::Color) {
        let ret = match color {
            pixels::Color::RGB(r, g, b) => {
                unsafe { ll::SDL_SetRenderDrawColor(self.raw, r, g, b, 255) }
            },
            pixels::Color::RGBA(r, g, b, a) => {
                unsafe { ll::SDL_SetRenderDrawColor(self.raw, r, g, b, a)  }
            }
        };
        // Should only fail on an invalid renderer
        if ret != 0 { panic!(get_error()) }
    }

    /// Gets the color used for drawing operations (Rect, Line and Clear).
    pub fn get_draw_color(&self) -> pixels::Color {
        let (mut r, mut g, mut b, mut a) = (0, 0, 0, 0);
        let ret = unsafe { ll::SDL_GetRenderDrawColor(self.raw, &mut r, &mut g, &mut b, &mut a) };
        // Should only fail on an invalid renderer
        if ret != 0 { panic!(get_error()) }
        else { pixels::Color::RGBA(r, g, b, a) }
    }

    /// Sets the blend mode used for drawing operations (Fill and Line).
    pub fn set_blend_mode(&mut self, blend: BlendMode) {
        let ret = unsafe { ll::SDL_SetRenderDrawBlendMode(self.raw, FromPrimitive::from_i64(blend as i64).unwrap()) };
        // Should only fail on an invalid renderer
        if ret != 0 { panic!(get_error()) }
    }

    /// Gets the blend mode used for drawing operations.
    pub fn get_blend_mode(&self) -> BlendMode {
        let mut blend = 0;
        let ret = unsafe { ll::SDL_GetRenderDrawBlendMode(self.raw, &mut blend) };
        // Should only fail on an invalid renderer
        if ret != 0 { panic!(get_error()) }
        else { FromPrimitive::from_i64(blend as i64).unwrap() }
    }

    /// Clears the current rendering target with the drawing color.
    pub fn clear(&mut self) {
        let ret = unsafe { ll::SDL_RenderClear(self.raw) };
        if ret != 0 { panic!("Could not clear: {}", get_error()) }
    }

    /// Updates the screen with any rendering performed since the previous call.
    ///
    /// SDL's rendering functions operate on a backbuffer; that is, calling a
    /// rendering function such as `draw_line()` does not directly put a line on
    /// the screen, but rather updates the backbuffer.
    /// As such, you compose your entire scene and present the composed
    /// backbuffer to the screen as a complete picture.
    pub fn present(&mut self) {
        unsafe { ll::SDL_RenderPresent(self.raw) }
    }

    /// Gets the output size of a rendering context.
    pub fn get_output_size(&self) -> SdlResult<(i32, i32)> {
        let mut width = 0;
        let mut height = 0;

        let result = unsafe { ll::SDL_GetRendererOutputSize(self.raw, &mut width, &mut height) == 0 };

        if result {
            Ok((width as i32, height as i32))
        } else {
            Err(get_error())
        }
    }

    /// Sets a device independent resolution for rendering.
    pub fn set_logical_size(&mut self, width: i32, height: i32) {
        let ret = unsafe { ll::SDL_RenderSetLogicalSize(self.raw, width as c_int, height as c_int) };
        if ret != 0 { panic!("Could not set logical size: {}", get_error()) }
    }

    /// Gets device independent resolution for rendering.
    pub fn get_logical_size(&self) -> (i32, i32) {
        let mut width = 0;
        let mut height = 0;

        unsafe { ll::SDL_RenderGetLogicalSize(self.raw, &mut width, &mut height) };

        (width as i32, height as i32)
    }

    /// Sets the drawing area for rendering on the current target.
    pub fn set_viewport(&mut self, rect: Option<Rect>) {
        let ptr = match rect {
            Some(ref rect) => rect as *const _,
            None => ptr::null()
        };
        let ret = unsafe { ll::SDL_RenderSetViewport(self.raw, ptr) };
        if ret != 0 { panic!("Could not set viewport: {}", get_error()) }
    }

    /// Gets the drawing area for the current target.
    pub fn get_viewport(&self) -> Rect {
        let mut rect = unsafe { mem::uninitialized() };
        unsafe { ll::SDL_RenderGetViewport(self.raw, &mut rect) };
        rect
    }

    /// Sets the clip rectangle for rendering on the specified target.
    pub fn set_clip_rect(&mut self, rect: Option<Rect>) {
        let ret = unsafe {
            ll::SDL_RenderSetClipRect(
                self.raw,
                match rect {
                    Some(ref rect) => rect as *const _,
                    None => ptr::null()
                }
            )
        };
        if ret != 0 { panic!("Could not set clip rect: {}", get_error()) }
    }

    /// Gets the clip rectangle for the current target.
    pub fn get_clip_rect(&self) -> Rect {
        let mut rect = unsafe { mem::uninitialized() };
        unsafe { ll::SDL_RenderGetClipRect(self.raw, &mut rect) };
        rect
    }

    /// Sets the drawing scale for rendering on the current target.
    pub fn set_scale(&mut self, scale_x: f32, scale_y: f32) {
        let ret = unsafe { ll::SDL_RenderSetScale(self.raw, scale_x, scale_y) };
        // Should only fail on an invalid renderer
        if ret != 0 { panic!(get_error()) }
    }

    /// Gets the drawing scale for the current target.
    pub fn get_scale(&self) -> (f32, f32) {
        let mut scale_x = 0.0;
        let mut scale_y = 0.0;
        unsafe { ll::SDL_RenderGetScale(self.raw, &mut scale_x, &mut scale_y) };
        (scale_x, scale_y)
    }

    /// Draws a point on the current rendering target.
    /// # Panics
    /// Panics if drawing fails for any reason (e.g. driver failure)
    pub fn draw_point(&mut self, point: Point) {
        unsafe {
            if ll::SDL_RenderDrawPoint(self.raw, point.x, point.y) != 0 {
                panic!("Error drawing point: {}", get_error())
            }
        }
    }

    /// Draws multiple points on the current rendering target.
    /// # Panics
    /// Panics if drawing fails for any reason (e.g. driver failure)
    pub fn draw_points(&mut self, points: &[Point]) {
        unsafe {
            if ll::SDL_RenderDrawPoints(self.raw, points.as_ptr(), points.len() as c_int) != 0 {
                panic!("Error drawing points: {}", get_error())
            }
        }
    }

    // Draws a line on the current rendering target.
    /// # Panics
    /// Panics if drawing fails for any reason (e.g. driver failure)
    pub fn draw_line(&mut self, start: Point, end: Point) {
        unsafe {
            if ll::SDL_RenderDrawLine(self.raw, start.x, start.y, end.x, end.y) != 0 {
                panic!("Error drawing line: {}", get_error())
            }
        }
    }

    /// Draws a series of connected lines on the current rendering target.
    /// # Panics
    /// Panics if drawing fails for any reason (e.g. driver failure)
    pub fn draw_lines(&mut self, points: &[Point]) {
        unsafe {
            if ll::SDL_RenderDrawLines(self.raw, points.as_ptr(), points.len() as c_int) != 0 {
                panic!("Error drawing lines: {}", get_error())
            }
        }
    }

    /// Draws a rectangle on the current rendering target.
    /// # Panics
    /// Panics if drawing fails for any reason (e.g. driver failure)
    pub fn draw_rect(&mut self, rect: Rect) {
        unsafe {
            if ll::SDL_RenderDrawRect(self.raw, &rect) != 0 {
                panic!("Error drawing rect: {}", get_error())
            }
        }
    }

    /// Draws some number of rectangles on the current rendering target.
    /// # Panics
    /// Panics if drawing fails for any reason (e.g. driver failure)
    pub fn draw_rects(&mut self, rects: &[Rect]) {
        unsafe {
            if ll::SDL_RenderDrawRects(self.raw, rects.as_ptr(), rects.len() as c_int) != 0 {
                panic!("Error drawing rects: {}", get_error())
            }
        }
    }

    /// Fills a rectangle on the current rendering target with the drawing
    /// color.
    /// # Panics
    /// Panics if drawing fails for any reason (e.g. driver failure)
    pub fn fill_rect(&mut self, rect: Rect) {
        unsafe {
            if ll::SDL_RenderFillRect(self.raw, &rect) != 0 {
                panic!("Error filling rect: {}", get_error())
            }
        }
    }

    /// Fills some number of rectangles on the current rendering target with
    /// the drawing color.
    /// # Panics
    /// Panics if drawing fails for any reason (e.g. driver failure)
    pub fn fill_rects(&mut self, rects: &[Rect]) {
        unsafe {
            if ll::SDL_RenderFillRects(self.raw, rects.as_ptr(), rects.len() as c_int) != 0 {
                panic!("Error filling rects: {}", get_error())
            }
        }
    }

    /// Copies a portion of the texture to the current rendering target.
    ///
    /// * If `src` is `None`, the entire texture is copied.
    /// * If `dst` is `None`, the texture will be stretched to fill the given
    ///   rectangle.
    ///
    /// # Panics
    /// Panics if drawing fails for any reason (e.g. driver failure),
    /// or if the provided texture does not belong to the renderer.
    pub fn copy(&mut self, texture: &Texture, src: Option<Rect>, dst: Option<Rect>) {
        texture.check_renderer();

        let ret = unsafe {
            ll::SDL_RenderCopy(
                self.raw,
                texture.raw,
                match src {
                    Some(ref rect) => rect as *const _,
                    None => ptr::null()
                },
                match dst {
                    Some(ref rect) => rect as *const _,
                    None => ptr::null()
                }
            )
        };

        if ret != 0 {
            panic!("Error copying texture: {}", get_error())
        }
    }

    /// Copies a portion of the texture to the current rendering target,
    /// optionally rotating it by angle around the given center and also
    /// flipping it top-bottom and/or left-right.
    ///
    /// * If `src` is `None`, the entire texture is copied.
    /// * If `dst` is `None`, the texture will be stretched to fill the given
    ///   rectangle.
    /// * If `center` is `None`, rotation will be done around the center point
    ///   of `dst`, or `src` if `dst` is None.
    ///
    /// # Panics
    /// Panics if drawing fails for any reason (e.g. driver failure),
    /// if the provided texture does not belong to the renderer,
    /// or if the driver does not support RenderCopyEx.
    pub fn copy_ex(&mut self, texture: &Texture, src: Option<Rect>, dst: Option<Rect>, angle: f64, center: Option<Point>, (flip_horizontal, flip_vertical): (bool, bool)) {
        texture.check_renderer();

        let flip = match (flip_horizontal, flip_vertical) {
            (false, false) => ll::SDL_FLIP_NONE,
            (true, false) => ll::SDL_FLIP_HORIZONTAL,
            (false, true) => ll::SDL_FLIP_VERTICAL,
            (true, true) => ll::SDL_FLIP_HORIZONTAL | ll::SDL_FLIP_VERTICAL,
        };

        let ret = unsafe {
            ll::SDL_RenderCopyEx(
                self.raw,
                texture.raw,
                match src {
                    Some(ref rect) => rect as *const _,
                    None => ptr::null()
                },
                match dst {
                    Some(ref rect) => rect as *const _,
                    None => ptr::null()
                },
                angle as c_double,
                match center {
                    Some(ref point) => point as *const _,
                    None => ptr::null()
                },
                flip
            )
        };

        if ret != 0 {
            panic!("Error copying texture (ex): {}", get_error())
        }
    }

    /// Reads pixels from the current rendering target.
    /// # Remarks
    /// WARNING: This is a very slow operation, and should not be used frequently.
    pub fn read_pixels(&self, rect: Option<Rect>, format: pixels::PixelFormatEnum) -> SdlResult<Vec<u8>> {
        unsafe {
            let (actual_rect, w, h) = match rect {
                Some(ref rect) => (rect as *const _, rect.w as usize, rect.h as usize),
                None => {
                    let (w, h) = try!(self.get_output_size());
                    (ptr::null(), w as usize, h as usize)
                }
            };

            let pitch = w * format.byte_size_per_pixel(); // calculated pitch
            let size = format.byte_size_of_pixels(w * h);
            let mut pixels = Vec::with_capacity(size);
            pixels.set_len(size);

            // Pass the interior of `pixels: Vec<u8>` to SDL
            let ret = {
                ll::SDL_RenderReadPixels(self.raw, actual_rect, format as uint32_t, pixels.as_mut_ptr() as *mut c_void, pitch as c_int)
            };

            if ret == 0 {
                Ok(pixels)
            } else {
                Err(get_error())
            }
        }
    }
}

/// A handle for getting/setting the render target of the render context.
///
/// # Example
/// ```no_run
/// use sdl2::pixels::{Color, PixelFormatEnum};
/// use sdl2::rect::Rect;
/// use sdl2::render::{RenderDrawer, Texture};
///
/// // Draw a red rectangle to a new texture
/// fn draw_to_texture(drawer: &mut RenderDrawer) -> Texture {
///     drawer.render_target()
///         .expect("This platform doesn't support render targets")
///         .create_and_set(PixelFormatEnum::RGBA8888, 512, 512);
///
///     // Start drawing
///     drawer.clear();
///     drawer.set_draw_color(Color::RGB(255, 0, 0));
///     drawer.fill_rect(Rect::new(100, 100, 256, 256));
///
///     let texture: Option<Texture> = drawer.render_target().unwrap().reset().unwrap();
///     texture.unwrap()
/// }
/// ```
pub struct RenderTarget<'renderer> {
    raw: *mut ll::SDL_Renderer,
    is_renderer_alive: &'renderer Rc<UnsafeCell<bool>>
}

impl<'renderer> RenderTarget<'renderer> {
    /// Resets the render target to the default render target.
    ///
    /// The old render target is returned if the function is successful.
    pub fn reset(&mut self) -> SdlResult<Option<Texture>> {
        unsafe {
            let old_texture_raw = ll::SDL_GetRenderTarget(self.raw);

            if ll::SDL_SetRenderTarget(self.raw, ptr::null_mut()) == 0 {
                Ok(match old_texture_raw.is_null() {
                    true => None,
                    false => Some(Texture {
                        raw: old_texture_raw,
                        is_renderer_alive: self.is_renderer_alive.clone()
                    })
                })
            } else {
                Err(get_error())
            }
        }
    }

    /// Sets the render target to the provided texture.
    /// The texture must be created with the texture access: `sdl2::render::TextureAccess::Target`.
    ///
    /// The old render target is returned if the function is successful.
    pub fn set(&mut self, texture: Texture) -> SdlResult<Option<Texture>> {
        texture.check_renderer();

        unsafe {
            let old_texture_raw = ll::SDL_GetRenderTarget(self.raw);

            if ll::SDL_SetRenderTarget(self.raw, texture.raw) == 0 {
                texture.forget();
                Ok(match old_texture_raw.is_null() {
                    true => None,
                    false => Some(Texture {
                        raw: old_texture_raw,
                        is_renderer_alive: self.is_renderer_alive.clone()
                    })
                })
            } else {
                Err(get_error())
            }
        }
    }

    /// Creates a new texture and sets it as the render target.
    ///
    /// The old render target is returned if the function is successful.
    pub fn create_and_set(&mut self, format: pixels::PixelFormatEnum, width: i32, height: i32) -> SdlResult<Option<Texture>> {
        let new_texture_raw = unsafe {
            let access = ll::SDL_TEXTUREACCESS_TARGET;
            ll::SDL_CreateTexture(self.raw, format as uint32_t, access as c_int, width as c_int, height as c_int)
        };

        if new_texture_raw == ptr::null_mut() {
            Err(get_error())
        } else {
            unsafe {
                let old_texture_raw = ll::SDL_GetRenderTarget(self.raw);

                if ll::SDL_SetRenderTarget(self.raw, new_texture_raw) == 0 {
                    Ok(match old_texture_raw.is_null() {
                        true => None,
                        false => Some(Texture {
                            raw: old_texture_raw,
                            is_renderer_alive: self.is_renderer_alive.clone()
                        })
                    })
                } else {
                    Err(get_error())
                }
            }
        }
    }
}

#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub struct TextureQuery {
    pub format: pixels::PixelFormatEnum,
    pub access: TextureAccess,
    pub width: i32,
    pub height: i32
}

/// A texture for a rendering context.
///
/// Every Texture is owned by a Renderer.
/// If a Texture is accessed after the corresponding Renderer is dropped, then
/// the program will panic (clarification: will not crash).
///
/// A Texture can be safely dropped before or after the Renderer is dropped.
pub struct Texture {
    raw: *mut ll::SDL_Texture,
    is_renderer_alive: Rc<UnsafeCell<bool>>
}

impl Drop for Texture {
    fn drop(&mut self) {
        unsafe {
            if *self.is_renderer_alive.get() {
                ll::SDL_DestroyTexture(self.raw);
            }
        }
    }
}

impl Texture {
    #[inline]
    fn check_renderer(&self) {
        let alive = unsafe { *self.is_renderer_alive.get() };
        if !alive {
            panic!("renderer has been destroyed; cannot use Texture");
        }
    }

    /// Doesn't free the Texture, but decrements its `is_renderer_alive` box.
    fn forget(self) {
        unsafe {
            let _is_renderer_alive: Rc<UnsafeCell<bool>> = mem::transmute_copy(&self.is_renderer_alive);
            mem::forget(self);

            // is_renderer_alive gets deref'd
        }
    }

    /// Queries the attributes of the texture.
    pub fn query(&self) -> TextureQuery {
        self.check_renderer();

        let mut format = 0;
        let mut access = 0;
        let mut width = 0;
        let mut height = 0;

        let ret = unsafe { ll::SDL_QueryTexture(self.raw, &mut format, &mut access, &mut width, &mut height) };
        // Should only fail on an invalid texture
        if ret != 0 {
            panic!(get_error())
        } else {
            TextureQuery {
               format: FromPrimitive::from_i64(format as i64).unwrap(),
               access: FromPrimitive::from_i64(access as i64).unwrap(),
               width: width as i32,
               height: height as i32
            }
        }
    }

    /// Sets an additional color value multiplied into render copy operations.
    pub fn set_color_mod(&mut self, red: u8, green: u8, blue: u8) {
        self.check_renderer();

        let ret = unsafe { ll::SDL_SetTextureColorMod(self.raw, red, green, blue) };

        if ret != 0 {
            panic!("Error setting color mod: {}", get_error())
        }
    }

    /// Gets the additional color value multiplied into render copy operations.
    pub fn get_color_mod(&self) -> (u8, u8, u8) {
        self.check_renderer();

        let (mut r, mut g, mut b) = (0, 0, 0);
        let ret = unsafe { ll::SDL_GetTextureColorMod(self.raw, &mut r, &mut g, &mut b) };

        // Should only fail on an invalid texture
        if ret != 0 { panic!(get_error()) }
        else { (r, g, b) }
    }

    /// Sets an additional alpha value multiplied into render copy operations.
    pub fn set_alpha_mod(&mut self, alpha: u8) {
        self.check_renderer();

        let ret = unsafe { ll::SDL_SetTextureAlphaMod(self.raw, alpha) };

        if ret != 0 {
            panic!("Error setting alpha mod: {}", get_error())
        }
    }

    /// Gets the additional alpha value multiplied into render copy operations.
    pub fn get_alpha_mod(&self) -> u8 {
        self.check_renderer();

        let mut alpha = 0;
        let ret = unsafe { ll::SDL_GetTextureAlphaMod(self.raw, &mut alpha) };

        // Should only fail on an invalid texture
        if ret != 0 { panic!(get_error()) }
        else { alpha }
    }

    /// Sets the blend mode for a texture, used by `RenderDrawer::copy()`.
    pub fn set_blend_mode(&mut self, blend: BlendMode) {
        self.check_renderer();

        let ret = unsafe { ll::SDL_SetTextureBlendMode(self.raw, FromPrimitive::from_i64(blend as i64).unwrap()) };

        if ret != 0 {
            panic!("Error setting blend: {}", get_error())
        }
    }

    /// Gets the blend mode used for texture copy operations.
    pub fn get_blend_mode(&self) -> BlendMode {
        self.check_renderer();

        let mut blend = 0;
        let ret = unsafe { ll::SDL_GetTextureBlendMode(self.raw, &mut blend) };

        // Should only fail on an invalid texture
        if ret != 0 { panic!(get_error()) }
        else { FromPrimitive::from_i64(blend as i64).unwrap() }
    }

    /// Updates the given texture rectangle with new pixel data.
    ///
    /// `pitch` is the number of bytes in a row of pixel data, including padding
    /// between lines
    ///
    /// * If `rect` is `None`, the entire texture is updated.
    pub fn update(&mut self, rect: Option<Rect>, pixel_data: &[u8], pitch: i32) -> SdlResult<()> {
        self.check_renderer();

        let ret = unsafe {
            let rect_raw_ptr = match rect {
                Some(ref rect) => rect as *const _,
                None => ptr::null()
            };

            // Check if the rectangle's position or size is odd, and if the pitch is odd.
            // This needs to be done in case the texture's pixel format is planar YUV.
            // See issue #334 for details.
            let rect_is_odd = match rect {
                Some(r) => (r.x % 2 != 0) || (r.y % 2 != 0) || (r.w % 2 != 0) || (r.h % 2 != 0),
                None => false
            };
            let pitch_is_odd = pitch % 2 != 0;

            if rect_is_odd || pitch_is_odd {
                // Query the texture's format
                match self.query() {
                    TextureQuery { format: PixelFormatEnum::YV12, .. } |
                    TextureQuery { format: PixelFormatEnum::IYUV, .. } => {
                        return Err(format!("The rectangle dimensions and pitch must be multiples-of-two for planar YUV 4:2:0 pixel formats"));
                    },
                    _ => ()
                }
            }

            ll::SDL_UpdateTexture(self.raw, rect_raw_ptr, pixel_data.as_ptr() as *const _, pitch as c_int)
        };

        if ret == 0 { Ok(()) }
        else { Err(get_error()) }
    }

    /// Updates a rectangle within a planar YV12 or IYUV texture with new pixel data.
    pub fn update_yuv(&mut self, rect: Option<Rect>, y_plane: &[u8], y_pitch: i32, u_plane: &[u8], u_pitch: i32, v_plane: &[u8], v_pitch: i32) -> SdlResult<()> {
        self.check_renderer();

        let rect_raw_ptr = match rect {
            Some(ref rect) => rect as *const _,
            None => ptr::null()
        };

        let rect_is_odd = match rect {
            Some(r) => (r.x % 2 != 0) || (r.y % 2 != 0) || (r.w % 2 != 0) || (r.h % 2 != 0),
            None => false
        };

        if rect_is_odd {
            return Err(format!("The rectangle dimensions must be multiples-of-two for planar YUV 4:2:0 pixel formats"));
        }

        // We need the height in order to check the array slice lengths.
        // Checking the lengths can prevent buffer overruns in SDL_UpdateYUVTexture.
        let height = match rect {
            Some(r) => r.h,
            None => self.query().height
        };

        let wrong_length =
            (y_plane.len() != (y_pitch * height) as usize) ||
            (u_plane.len() != (u_pitch * height/2) as usize) ||
            (v_plane.len() != (v_pitch * height/2) as usize);

        if wrong_length {
            return Err(format!("One or more of the plane lengths is not correct (should be pitch * height)."));
        }

        unsafe {
            let result = ll::SDL_UpdateYUVTexture(
                self.raw,
                rect_raw_ptr,
                y_plane.as_ptr(),
                y_pitch,
                u_plane.as_ptr(),
                u_pitch,
                v_plane.as_ptr(),
                v_pitch
            );

            if result == 0 { Ok(()) }
            else { Err(get_error()) }
        }
    }

    /// Locks the texture for **write-only** pixel access.
    /// The texture must have been created with streaming access.
    ///
    /// `F` is a function that is passed the write-only texture buffer,
    /// and the pitch of the texture (size of a row in bytes).
    /// # Remarks
    /// As an optimization, the pixels made available for editing don't
    /// necessarily contain the old texture data.
    /// This is a write-only operation, and if you need to keep a copy of the
    /// texture data you should do that at the application level.
    pub fn with_lock<F, R>(&mut self, rect: Option<Rect>, func: F) -> SdlResult<R>
    where F: FnOnce(&mut [u8], usize) -> R
    {
        self.check_renderer();

        // Call to SDL to populate pixel data
        let loaded = unsafe {
            let q = self.query();
            let mut pixels = ptr::null_mut();
            let mut pitch = 0;

            let (rect_raw_ptr, height) = match rect {
                Some(ref rect) => (rect as *const _, rect.h as usize),
                None => (ptr::null(), q.height as usize)
            };

            let ret = ll::SDL_LockTexture(self.raw, rect_raw_ptr, &mut pixels, &mut pitch);
            if ret == 0 {
                let size = q.format.byte_size_from_pitch_and_height(pitch as usize, height);
                Ok( (::std::slice::from_raw_parts_mut(pixels as *mut u8, size ), pitch) )
            } else {
                Err(get_error())
            }
        };

        match loaded {
            Ok((interior, pitch)) => {
                let result;
                unsafe {
                    result = func(interior, pitch as usize);
                    ll::SDL_UnlockTexture(self.raw);
                }
                Ok(result)
            }
            Err(e) => Err(e),
        }
    }

    /// Binds an OpenGL/ES/ES2 texture to the current
    /// context for use with when rendering OpenGL primitives directly.
    pub unsafe fn gl_bind_texture(&mut self) -> (f32, f32) {
        self.check_renderer();

        let mut texw = 0.0;
        let mut texh = 0.0;

        if ll::SDL_GL_BindTexture(self.raw, &mut texw, &mut texh) == 0 {
            (texw, texh)
        } else {
            panic!("OpenGL texture binding not supported");
        }
    }

    /// Unbinds an OpenGL/ES/ES2 texture from the current context.
    pub unsafe fn gl_unbind_texture(&mut self) {
        self.check_renderer();

        if ll::SDL_GL_UnbindTexture(self.raw) != 0 {
            panic!("OpenGL texture unbinding not supported");
        }
    }

    /// Binds and unbinds an OpenGL/ES/ES2 texture from the current context.
    pub fn gl_with_bind<R, F: FnOnce(f32, f32) -> R>(&mut self, f: F) -> R {
        self.check_renderer();

        unsafe {
            let mut texw = 0.0;
            let mut texh = 0.0;

            if ll::SDL_GL_BindTexture(self.raw, &mut texw, &mut texh) == 0 {
                let return_value = f(texw, texh);

                if ll::SDL_GL_UnbindTexture(self.raw) == 0 {
                    return_value
                } else {
                    // This should never happen...
                    panic!();
                }
            } else {
                panic!("OpenGL texture binding not supported");
            }
        }
    }

    pub unsafe fn from_ll(renderer: &Renderer, raw: *mut ll::SDL_Texture) -> Texture {
        Texture {
            raw: raw,
            is_renderer_alive: renderer.is_alive.clone()
        }
    }

    pub unsafe fn raw(&self) -> *mut ll::SDL_Texture { self.raw }
}


pub fn get_num_render_drivers() -> SdlResult<i32> {
    let result = unsafe { ll::SDL_GetNumRenderDrivers() };
    if result > 0 {
        Ok(result as i32)
    } else {
        Err(get_error())
    }
}

pub fn get_render_driver_info(index: i32) -> SdlResult<RendererInfo> {
    let mut out = unsafe { mem::uninitialized() };
    let result = unsafe { ll::SDL_GetRenderDriverInfo(index as c_int, &mut out) == 0 };
    if result {
        unsafe { Ok(RendererInfo::from_ll(&out)) }
    } else {
        Err(get_error())
    }
}

/*
    //TODO: Figure out how to support this with our current struct format
    pub fn SDL_GetRenderer(window: *SDL_Window) -> *SDL_Renderer;
*/