Skip to content

Instantly share code, notes, and snippets.

Created August 18, 2016 19:49
Show Gist options
  • Save anonymous/7a6ac71d99a9c1fe162c20fa1739c94c to your computer and use it in GitHub Desktop.
Save anonymous/7a6ac71d99a9c1fe162c20fa1739c94c to your computer and use it in GitHub Desktop.
Shared via Rust Playground
#![allow(dead_code)]
#![allow(unreachable_code)]
#![allow(unused_variables)]
#![allow(non_snake_case)]
extern crate libc;
extern crate sdl2;
extern crate sdl2_sys;
extern crate gl;
extern crate nanovg;
use libc::{c_int, c_float, uint32_t, c_char};
use std::cell::Cell; // for glfw error count
use nanovg::Context;
use sdl2::pixels::Color;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
/// evaluate the expression, then check for GL error.
macro_rules! glcheck {
($e: expr) => (
{
$e;
assert_eq!(unsafe {gl::GetError()}, 0);
}
)
}
fn init_gl() {
glcheck!(unsafe {gl::FrontFace(gl::CCW)});
glcheck!(unsafe {gl::Enable(gl::DEPTH_TEST)});
glcheck!(unsafe {gl::Enable(gl::SCISSOR_TEST)});
glcheck!(unsafe {gl::DepthFunc(gl::LEQUAL)});
glcheck!(unsafe {gl::FrontFace(gl::CCW)});
glcheck!(unsafe {gl::Enable(gl::CULL_FACE)});
glcheck!(unsafe {gl::CullFace(gl::BACK)});
}
static mut blowup: bool = false;
static mut screenshot: bool = false;
static mut premult: bool = false;
fn main()
{
let sdl_context = sdl2::init().unwrap();
let video_subsystem = sdl_context.video().unwrap();
let window = video_subsystem.window("rust-sdl2 demo: Video", 800, 600)
.position_centered()
.opengl()
.build()
.unwrap();
let mut renderer = window.renderer().build().unwrap();
renderer.set_draw_color(Color::RGB(255, 0, 0));
renderer.clear();
renderer.present();
let mut event_pump = sdl_context.event_pump().unwrap();
glcheck!(gl::load_with(|name| video_subsystem.gl_get_proc_address(name) as *const _));
init_gl();
let vg: nanovg::Context = nanovg::Context::create_gl3(nanovg::ANTIALIAS | nanovg::STENCIL_STROKES);
'running: loop {
let t: f64 = unsafe {sdl2_sys::sdl::SDL_GetTicks() as f64};
let (mx, my) = (0, 0);//window.get_cursor_pos(); // (f64,f64)
let (winWidth, winHeight) = renderer.window().unwrap().size(); // (i32,i32)
let (fbWidth, fbHeight) = renderer.window().unwrap().size();
// Calculate pixel ration for hi-dpi devices.
let pxRatio = fbWidth as f32 / winWidth as f32;
// Update and render
glcheck!(unsafe {gl::Viewport(0, 0, fbWidth as i32, fbHeight as i32)});
if unsafe {premult} {
glcheck!(unsafe {gl::ClearColor(0.0, 0.0, 0.0, 0.0)});
} else {
glcheck!(unsafe {gl::ClearColor(0.3, 0.3, 0.32, 1.0)});
}
glcheck!(unsafe {gl::Clear(gl::COLOR_BUFFER_BIT|gl::DEPTH_BUFFER_BIT|gl::STENCIL_BUFFER_BIT)});
glcheck!(unsafe {gl::Enable(gl::BLEND)});
glcheck!(unsafe {gl::BlendFunc(gl::SRC_ALPHA, gl::ONE_MINUS_SRC_ALPHA)});
glcheck!(unsafe {gl::Enable(gl::CULL_FACE)});
glcheck!(unsafe {gl::Disable(gl::DEPTH_TEST)});
vg.begin_frame(winWidth as u32, winHeight as u32, pxRatio as f32);
vg.end_frame();
unsafe {gl::Enable(gl::DEPTH_TEST);}
unsafe {
if screenshot {
screenshot = false;
}
}
renderer.window().unwrap().gl_swap_window();
for event in event_pump.poll_iter() {
match event {
Event::Quit {..} | Event::KeyDown { keycode: Some(Keycode::Escape), .. } => {
break 'running
},
_ => {}
}
}
}
}
//// think linebreaks api needs some love
//fn test_linebreaks(vg:Context) {
// let x=0.0; let y=0.0;
// let width = 120.0;
//
// let text = "This is longer chunk of text.\n \n Would have used lorem ipsum but she was busy jumping over the lazy dog with the fox and all the men who came to the aid of the party.";
// //let text = "01234 6789 1234 6789 1234 6789 123456789012345678901234567890123456789012345678901234567890123456789";
//
//
// // The text break API can be used to fill a large buffer of rows,
// // or to iterate over the text just few lines (or just one) at a time.
// // The "next" variable of the last returned item tells where to continue.
// let mut start: uint = 0; // byte pos in utf8 'text' str
// let end: uint = text.len(); // exclusive
// 'chunks: loop {
// let text = text.slice(start, end);
//
//println!("{}", text);
//
// let rows = vg.text_break_lines(text, width, 3);
// let nrows = rows.len();
// if nrows == 0 { break 'chunks; }
// for i in range(0, nrows) {
// let row = &rows[i];
// let line = text.slice(row.start_index(), row.end_index());
//
//println!("i: {} st: {}, en: {} \t {} \tnext: {}", i, row.start_index(), row.end_index(), line, row.next_index());
//
// }
// // Keep going...
// start += rows[nrows-1].next_index();
// }
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment