window/
window.rs

1// Copyright 2015 The Gfx-rs Developers.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14extern crate gfx;
15extern crate gfx_window_sdl;
16extern crate sdl2;
17
18use gfx::Device;
19use sdl2::event::Event;
20use sdl2::keyboard::Keycode;
21use gfx::format::{Rgba8, DepthStencil};
22
23const CLEAR_COLOR: [f32; 4] = [0.1, 0.2, 0.3, 1.0];
24
25pub fn main() {
26    let sdl_context = sdl2::init().unwrap();
27    let video = sdl_context.video().unwrap();
28    // Request opengl core 3.2 for example:
29    video.gl_attr().set_context_profile(sdl2::video::GLProfile::Core);
30    video.gl_attr().set_context_version(3, 2);
31    let builder = video.window("SDL Window", 1024, 768);
32    let (window, _gl_context, mut device, mut factory, main_color, _main_depth) =
33        gfx_window_sdl::init::<Rgba8, DepthStencil>(&video, builder).unwrap();
34
35    let mut encoder: gfx::Encoder<_, _> = factory.create_command_buffer().into();
36
37    let mut events = sdl_context.event_pump().unwrap();
38
39    let mut running = true;
40    while running {
41        // handle events
42        for event in events.poll_iter() {
43            match event {
44                Event::Quit { .. } |
45                Event::KeyUp { keycode: Some(Keycode::Escape), .. } => {
46                    running = false;
47                }
48                _ => {}
49            }
50        }
51
52        // draw a frame
53        encoder.clear(&main_color, CLEAR_COLOR);
54        // <- draw actual stuff here
55        encoder.flush(&mut device);
56        window.gl_swap_window();
57        device.cleanup();
58    }
59}