Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add new SDL_Renderer APIs #1216

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn main() -> Result<(), String> {
.map_err(|e| e.to_string())?;
let texture_creator = canvas.texture_creator();

canvas.set_draw_color(sdl2::pixels::Color::RGBA(0, 0, 0, 255));
canvas.set_draw_color(sdl2::pixels::RColor::RGBA(0, 0, 0, 255));

let timer = sdl_context.timer()?;

Expand Down
4 changes: 2 additions & 2 deletions examples/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use sdl2::event::Event;
use sdl2::image::{InitFlag, LoadSurface};
use sdl2::keyboard::Keycode;
use sdl2::mouse::Cursor;
use sdl2::pixels::Color;
use sdl2::pixels::RColor;
use sdl2::rect::Rect;
use sdl2::surface::Surface;
use std::env;
Expand Down Expand Up @@ -35,7 +35,7 @@ pub fn run(png: &Path) -> Result<(), String> {
canvas.clear();
canvas.present();

canvas.set_draw_color(Color::RGBA(255, 255, 255, 255));
canvas.set_draw_color(RColor::RGBA(255, 255, 255, 255));

let mut events = sdl_context.event_pump()?;

Expand Down
4 changes: 2 additions & 2 deletions examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate sdl2;

use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::pixels::Color;
use sdl2::pixels::RColor;
use std::time::Duration;

pub fn main() -> Result<(), String> {
Expand All @@ -18,7 +18,7 @@ pub fn main() -> Result<(), String> {

let mut canvas = window.into_canvas().build().map_err(|e| e.to_string())?;

canvas.set_draw_color(Color::RGB(255, 0, 0));
canvas.set_draw_color(RColor::RGB(255, 0, 0));
canvas.clear();
canvas.present();
let mut event_pump = sdl_context.event_pump()?;
Expand Down
4 changes: 2 additions & 2 deletions examples/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate sdl2;

use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::pixels::Color;
use sdl2::pixels::RColor;
use std::time::Duration;

pub fn main() -> Result<(), String> {
Expand All @@ -18,7 +18,7 @@ pub fn main() -> Result<(), String> {

let mut canvas = window.into_canvas().build().map_err(|e| e.to_string())?;

canvas.set_draw_color(Color::RGB(255, 0, 0));
canvas.set_draw_color(RColor::RGB(255, 0, 0));
canvas.clear();
canvas.present();
let mut event_pump = sdl_context.event_pump()?;
Expand Down
20 changes: 10 additions & 10 deletions examples/game-of-life-unsafe-textures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use sdl2::keyboard::Keycode;
#[cfg(feature = "unsafe_textures")]
use sdl2::mouse::MouseButton;
#[cfg(feature = "unsafe_textures")]
use sdl2::pixels::Color;
use sdl2::pixels::RColor;
#[cfg(feature = "unsafe_textures")]
use sdl2::rect::{Point, Rect};
#[cfg(feature = "unsafe_textures")]
Expand Down Expand Up @@ -140,20 +140,20 @@ fn dummy_texture<'a>(canvas: &mut Canvas<Window>) -> Result<(Texture, Texture),
];
canvas
.with_multiple_texture_canvas(textures.iter(), |texture_canvas, user_context| {
texture_canvas.set_draw_color(Color::RGB(0, 0, 0));
texture_canvas.set_draw_color(RColor::RGB(0, 0, 0));
texture_canvas.clear();
match *user_context {
TextureColor::Yellow => {
for i in 0..SQUARE_SIZE {
for j in 0..SQUARE_SIZE {
if (i + j) % 4 == 0 {
texture_canvas.set_draw_color(Color::RGB(255, 255, 0));
texture_canvas.set_draw_color(RColor::RGB(255, 255, 0));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
}
if (i + j * 2) % 9 == 0 {
texture_canvas.set_draw_color(Color::RGB(200, 200, 0));
texture_canvas.set_draw_color(RColor::RGB(200, 200, 0));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
Expand All @@ -169,13 +169,13 @@ fn dummy_texture<'a>(canvas: &mut Canvas<Window>) -> Result<(Texture, Texture),
if (i + j) % 7 == 0 {
// this doesn't mean anything, there was some trial and error to find
// something that wasn't too ugly
texture_canvas.set_draw_color(Color::RGB(192, 192, 192));
texture_canvas.set_draw_color(RColor::RGB(192, 192, 192));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
}
if (i + j * 2) % 5 == 0 {
texture_canvas.set_draw_color(Color::RGB(64, 64, 64));
texture_canvas.set_draw_color(RColor::RGB(64, 64, 64));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
Expand All @@ -191,13 +191,13 @@ fn dummy_texture<'a>(canvas: &mut Canvas<Window>) -> Result<(Texture, Texture),
if (i + j) % 7 == 0 {
// this doesn't mean anything, there was some trial and serror to find
// something that wasn't too ugly
texture_canvas.set_draw_color(Color::RGB(192, 192, 192));
texture_canvas.set_draw_color(RColor::RGB(192, 192, 192));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
}
if (i + j * 2) % 5 == 0 {
texture_canvas.set_draw_color(Color::RGB(64, 64, 64));
texture_canvas.set_draw_color(RColor::RGB(64, 64, 64));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
Expand Down Expand Up @@ -239,7 +239,7 @@ pub fn main() -> Result<(), String> {
.map_err(|e| e.to_string())?;

println!("Using SDL_Renderer \"{}\"", canvas.info().name);
canvas.set_draw_color(Color::RGB(0, 0, 0));
canvas.set_draw_color(RColor::RGB(0, 0, 0));
// clears the canvas with the color we set in `set_draw_color`.
canvas.clear();
// However the canvas has not been updated to the window yet, everything has been processed to
Expand Down Expand Up @@ -294,7 +294,7 @@ pub fn main() -> Result<(), String> {
frame = 0;
}

canvas.set_draw_color(Color::RGB(0, 0, 0));
canvas.set_draw_color(RColor::RGB(0, 0, 0));
canvas.clear();
for (i, unit) in (&game).into_iter().enumerate() {
let i = i as u32;
Expand Down
20 changes: 10 additions & 10 deletions examples/game-of-life.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::game_of_life::{PLAYGROUND_HEIGHT, PLAYGROUND_WIDTH, SQUARE_SIZE};
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::mouse::MouseButton;
use sdl2::pixels::Color;
use sdl2::pixels::RColor;
use sdl2::rect::{Point, Rect};
use sdl2::render::{Canvas, Texture, TextureCreator};
use sdl2::video::{Window, WindowContext};
Expand Down Expand Up @@ -133,20 +133,20 @@ fn dummy_texture<'a>(
];
canvas
.with_multiple_texture_canvas(textures.iter(), |texture_canvas, user_context| {
texture_canvas.set_draw_color(Color::RGB(0, 0, 0));
texture_canvas.set_draw_color(RColor::RGB(0, 0, 0));
texture_canvas.clear();
match *user_context {
TextureColor::Yellow => {
for i in 0..SQUARE_SIZE {
for j in 0..SQUARE_SIZE {
if (i + j) % 4 == 0 {
texture_canvas.set_draw_color(Color::RGB(255, 255, 0));
texture_canvas.set_draw_color(RColor::RGB(255, 255, 0));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
}
if (i + j * 2) % 9 == 0 {
texture_canvas.set_draw_color(Color::RGB(200, 200, 0));
texture_canvas.set_draw_color(RColor::RGB(200, 200, 0));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
Expand All @@ -162,13 +162,13 @@ fn dummy_texture<'a>(
if (i + j) % 7 == 0 {
// this doesn't mean anything, there was some trial and error to find
// something that wasn't too ugly
texture_canvas.set_draw_color(Color::RGB(192, 192, 192));
texture_canvas.set_draw_color(RColor::RGB(192, 192, 192));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
}
if (i + j * 2) % 5 == 0 {
texture_canvas.set_draw_color(Color::RGB(64, 64, 64));
texture_canvas.set_draw_color(RColor::RGB(64, 64, 64));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
Expand All @@ -184,13 +184,13 @@ fn dummy_texture<'a>(
if (i + j) % 7 == 0 {
// this doesn't mean anything, there was some trial and serror to find
// something that wasn't too ugly
texture_canvas.set_draw_color(Color::RGB(192, 192, 192));
texture_canvas.set_draw_color(RColor::RGB(192, 192, 192));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
}
if (i + j * 2) % 5 == 0 {
texture_canvas.set_draw_color(Color::RGB(64, 64, 64));
texture_canvas.set_draw_color(RColor::RGB(64, 64, 64));
texture_canvas
.draw_point(Point::new(i as i32, j as i32))
.expect("could not draw point");
Expand Down Expand Up @@ -231,7 +231,7 @@ pub fn main() -> Result<(), String> {
.map_err(|e| e.to_string())?;

println!("Using SDL_Renderer \"{}\"", canvas.info().name);
canvas.set_draw_color(Color::RGB(0, 0, 0));
canvas.set_draw_color(RColor::RGB(0, 0, 0));
// clears the canvas with the color we set in `set_draw_color`.
canvas.clear();
// However the canvas has not been updated to the window yet, everything has been processed to
Expand Down Expand Up @@ -290,7 +290,7 @@ pub fn main() -> Result<(), String> {
frame = 0;
}

canvas.set_draw_color(Color::RGB(0, 0, 0));
canvas.set_draw_color(RColor::RGB(0, 0, 0));
canvas.clear();
for (i, unit) in (&game).into_iter().enumerate() {
let i = i as u32;
Expand Down
4 changes: 2 additions & 2 deletions examples/message-box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ extern crate sdl2;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::messagebox::*;
use sdl2::pixels::Color;
use sdl2::pixels::RColor;

pub fn main() -> Result<(), String> {
let sdl_context = sdl2::init()?;
Expand All @@ -18,7 +18,7 @@ pub fn main() -> Result<(), String> {

let mut canvas = window.into_canvas().build().map_err(|e| e.to_string())?;

canvas.set_draw_color(Color::RGB(255, 0, 0));
canvas.set_draw_color(RColor::RGB(255, 0, 0));
canvas.clear();
canvas.present();
let mut event_pump = sdl_context.event_pump()?;
Expand Down
12 changes: 6 additions & 6 deletions examples/no-renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate sdl2;

use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::pixels::Color;
use sdl2::pixels::RColor;
use sdl2::rect::Rect;
use sdl2::video::Window;
use std::time::Duration;
Expand All @@ -29,11 +29,11 @@ fn set_window_gradient(
let c: u8 = 255 - (i as u8);
let i = i as i32;
let color = match gradient {
Gradient::Red => Color::RGB(c, 0, 0),
Gradient::Cyan => Color::RGB(0, c, c),
Gradient::Green => Color::RGB(0, c, 0),
Gradient::Blue => Color::RGB(0, 0, c),
Gradient::White => Color::RGB(c, c, c),
Gradient::Red => RColor::RGB(c, 0, 0),
Gradient::Cyan => RColor::RGB(0, c, c),
Gradient::Green => RColor::RGB(0, c, 0),
Gradient::Blue => RColor::RGB(0, 0, c),
Gradient::White => RColor::RGB(c, c, c),
};
surface.fill_rect(Rect::new(i * 4, 0, 4, WINDOW_HEIGHT), color)?;
}
Expand Down
6 changes: 3 additions & 3 deletions examples/renderer-target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate sdl2;

use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::pixels::{Color, PixelFormatEnum};
use sdl2::pixels::{PixelFormatEnum, RColor};
use sdl2::rect::{Point, Rect};

fn main() -> Result<(), String> {
Expand Down Expand Up @@ -40,13 +40,13 @@ fn main() -> Result<(), String> {
canvas
.with_texture_canvas(&mut texture, |texture_canvas| {
texture_canvas.clear();
texture_canvas.set_draw_color(Color::RGBA(255, 0, 0, 255));
texture_canvas.set_draw_color(RColor::RGBA(255, 0, 0, 255));
texture_canvas
.fill_rect(Rect::new(0, 0, 400, 300))
.expect("could not fill rect");
})
.map_err(|e| e.to_string())?;
canvas.set_draw_color(Color::RGBA(0, 0, 0, 255));
canvas.set_draw_color(RColor::RGBA(0, 0, 0, 255));
let dst = Some(Rect::new(0, 0, 400, 300));
canvas.clear();
canvas.copy_ex(
Expand Down
4 changes: 2 additions & 2 deletions examples/window-properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ extern crate sdl2;

use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use sdl2::pixels::Color;
use sdl2::pixels::RColor;

pub fn main() -> Result<(), String> {
let sdl_context = sdl2::init()?;
Expand Down Expand Up @@ -51,7 +51,7 @@ pub fn main() -> Result<(), String> {
tick += 1;
}

canvas.set_draw_color(Color::RGB(0, 0, 0));
canvas.set_draw_color(RColor::RGB(0, 0, 0));
canvas.clear();
canvas.present();
}
Expand Down
7 changes: 7 additions & 0 deletions src/sdl2/gfx/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ impl ToColor for pixels::Color {
}
}

impl ToColor for pixels::RColor {
#[inline]
fn as_rgba(&self) -> (u8, u8, u8, u8) {
self.rgba()
}
}

impl ToColor for (u8, u8, u8, u8) {
#[inline]
fn as_rgba(&self) -> (u8, u8, u8, u8) {
Expand Down
Loading