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
|
use bezier::Bezier;
use sdl2::event::Event;
use sdl2::keyboard::Keycode;
use gl::types::*;
fn main() {
let mut curve = Bezier::new();
let sdl_context = sdl2::init().unwrap();
let video_subsystem = sdl_context.video().unwrap();
let gl_attr = video_subsystem.gl_attr();
gl_attr.set_context_profile(sdl2::video::GLProfile::Core);
gl_attr.set_context_version(4, 1);
let window_w: u32 = 800;
let window_h: u32 = 600;
let window = video_subsystem
.window("bezier", window_w, window_h)
.position_centered()
.opengl()
.build()
.unwrap();
let _gl_context = window.gl_create_context().unwrap();
gl::load_with(|s| video_subsystem.gl_get_proc_address(s) as *const std::os::raw::c_void);
let vertices: [f32; 18] = [
1.0, 1.0, 0.0, 1.0, -1.0, 0.0, -1.0, 1.0, 0.0, 1.0, -1.0, 0.0, -1.0, -1.0, 0.0, -1.0, 1.0, 0.0,
];
let mut vbo: GLuint = 0;
let mut vao: GLuint = 0;
unsafe {
gl::Viewport(0, 0, window_w as i32, window_h as i32);
gl::GenVertexArrays(1, &mut vao);
gl::BindVertexArray(vao);
gl::GenBuffers(1, &mut vbo);
gl::BindBuffer(gl::ARRAY_BUFFER, vbo);
gl::BufferData(
gl::ARRAY_BUFFER,
(vertices.len() * std::mem::size_of::<f32>()) as GLsizeiptr,
vertices.as_ptr() as *const GLvoid,
gl::STATIC_DRAW,
);
gl::VertexAttribPointer(
0,
3,
gl::FLOAT,
gl::FALSE,
(3 * std::mem::size_of::<f32>()) as GLint,
std::ptr::null(),
);
gl::EnableVertexAttribArray(0);
}
let mut event_pump = sdl_context.event_pump().unwrap();
'running: loop {
let x = event_pump.mouse_state().x();
let y = event_pump.mouse_state().y();
let left_click = event_pump.mouse_state().left();
for event in event_pump.poll_iter() {
match event {
Event::Quit { .. }
| Event::KeyDown {
keycode: Some(Keycode::Escape),
..
} => break 'running,
Event::KeyDown {
keycode: Some(Keycode::A),
..
} => {
curve.push(x, window_h as i32 - y);
curve.draw();
},
Event::MouseMotion { .. } => {
if left_click {
curve.grab_closer(x as f32, (window_h as i32 - y) as f32);
curve.draw();
}
}
_ => {}
}
}
unsafe {
gl::Clear(gl::COLOR_BUFFER_BIT);
gl::DrawArrays(gl::TRIANGLES, 0, 6);
}
window.gl_swap_window();
}
}
|