Skip to content

Instantly share code, notes, and snippets.

@icefoxen
Created February 2, 2017 19:20
Show Gist options
  • Save icefoxen/de847b996d9bdb77c9305b3798f095f9 to your computer and use it in GitHub Desktop.
Save icefoxen/de847b996d9bdb77c9305b3798f095f9 to your computer and use it in GitHub Desktop.
[package]
name = "minimal"
version = "0.1.0"
[dependencies]
sdl2-sys = "0.27"
sdl2 = "0.27"
gfx = "0.14"
gfx_core = "*"
gfx_device_gl = "0.12"
gfx_window_sdl = "0.4"
#[macro_use]
extern crate gfx;
extern crate gfx_core;
extern crate gfx_device_gl;
extern crate gfx_window_sdl;
extern crate sdl2;
use std::fmt;
use std::path;
use std::collections::BTreeMap;
use std::io::Read;
use sdl2::pixels;
use sdl2::render;
use sdl2::surface;
// This is all placeholder for now just to get us going,
// taken from the instancing example.
const QUAD_VERTICES: [Vertex; 4] = [Vertex { position: [-0.5, 0.5] },
Vertex { position: [-0.5, -0.5] },
Vertex { position: [0.5, -0.5] },
Vertex { position: [0.5, 0.5] }];
const QUAD_INDICES: [u16; 6] = [0, 1, 2, 2, 3, 0];
pub type ColorFormat = gfx::format::Rgba8;
pub type DepthFormat = gfx::format::DepthStencil;
gfx_defines!{
vertex Vertex {
position: [f32; 2] = "a_Position",
}
// color format: 0xRRGGBBAA
vertex Instance {
translate: [f32; 2] = "a_Translate",
color: u32 = "a_Color",
}
constant Locals {
scale: f32 = "u_Scale",
}
pipeline pipe {
vertex: gfx::VertexBuffer<Vertex> = (),
instance: gfx::InstanceBuffer<Instance> = (),
scale: gfx::Global<f32> = "u_Scale",
locals: gfx::ConstantBuffer<Locals> = "Locals",
out: gfx::RenderTarget<ColorFormat> = "Target0",
}
}
// This works fine unless I uncomment color_view or depth_view
pub struct GraphicsContext {
background: pixels::Color,
foreground: pixels::Color,
window: sdl2::video::Window,
gl_context: sdl2::video::GLContext,
device: gfx_device_gl::Device,
factory: gfx_device_gl::Factory,
//color_view: gfx::handle::RenderTargetView<gfx_device_gl::Resources, gfx::format::Srgba8>,
//depth_view: gfx::handle::DepthStencilView<gfx_device_gl::Resources, gfx::format::DepthStencil>,
}
// This compiles fine until I try to use it, see new_context2() below
pub struct GraphicsContextGeneric<R, F, C, D> where R: gfx::Resources, F: gfx::Factory<R>, C: gfx::CommandBuffer<R>, D: gfx::Device<Resources=R, CommandBuffer=C> {
background: pixels::Color,
foreground: pixels::Color,
window: sdl2::video::Window,
gl_context: sdl2::video::GLContext,
device: Box<D>,
factory: Box<F>,
color_view: gfx::handle::RenderTargetView<R, gfx::format::Srgba8>,
depth_view: gfx::handle::DepthStencilView<R, gfx::format::DepthStencil>,
}
// GL only
pub type GraphicsContext2 = GraphicsContextGeneric<gfx_device_gl::Resources, gfx_device_gl::Factory, gfx_device_gl::CommandBuffer, gfx_device_gl::Device>;
pub fn new_context(video: sdl2::VideoSubsystem, window_title: &str, screen_width: u32, screen_height: u32) -> GraphicsContext {
let window_builder = &mut video.window(window_title, screen_width, screen_height);
let (mut window, mut gl_context, mut device, mut factory, color_view, depth_view) =
gfx_window_sdl::init(window_builder);
GraphicsContext {
background: pixels::Color::RGB(0u8, 0u8, 255u8),
foreground: pixels::Color::RGB(255, 255, 255),
window: window,
gl_context: gl_context,
device: device,
factory: factory,
//color_view: color_view,
//depth_view: depth_view,
}
}
// This gives trait-not-found errors
// pub fn new_context2(video: sdl2::VideoSubsystem, window_title: &str, screen_width: u32, screen_height: u32) -> GraphicsContext2 {
// let window_builder = &mut video.window(window_title, screen_width, screen_height);
// let (mut window, mut gl_context, mut device, mut factory, color_view, depth_view) =
// gfx_window_sdl::init(window_builder);
// GraphicsContext2 {
// background: pixels::Color::RGB(0u8, 0u8, 255u8),
// foreground: pixels::Color::RGB(255, 255, 255),
// window: window,
// gl_context: gl_context,
// device: device,
// factory: factory,
// color_view: color_view,
// depth_view: depth_view,
// }
// }
fn main() {
println!("Hello world!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment