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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
extern crate gl;
use self::gl::types::*;
use std::collections::HashMap;
use std::ffi::CString;
use std::ptr;
use std::str;
use crate::stage::Stage;
use crate::actor::Actor;
use crate::actor::EventHandler;
const VERTEX_SHADER_SOURCE: &str = r#"
#version 330 core
layout(location = 0) in vec4 a_position;
layout(location = 1) in vec2 a_texCoord;
uniform mat4 transform;
out vec2 v_texCoord;
void main() {
gl_Position = transform * a_position;
v_texCoord = a_texCoord;
}
"#;
const FRAGMENT_SHADER_SOURCE: &str = r#"
#version 330 core
out vec4 outColor;
uniform vec4 color;
in vec2 v_texCoord;
uniform sampler2D s_texture;
void main() {
if (v_texCoord[0] == 0.0f)
outColor = color;
else {
outColor = texture(s_texture, v_texCoord);
}
}
"#;
pub fn render(name:String) {
println!("Render {}", name);
}
pub struct Play<'a> {
name : String,
stage_list: Vec<Stage<'a>>,
shader_program: GLuint,
stage_map: HashMap<String, usize>
}
impl<'a> Play<'a> {
pub fn new(name: String) -> Self {
Play {
name : name,
stage_list: Vec::new(),
shader_program: 0,
stage_map: HashMap::new()
}
}
pub fn initialize(&mut self) {
self.compile_shader();
}
pub fn new_actor(name: String, w: u32, h: u32,
event_handler: Option<Box<dyn EventHandler + 'a>>) -> Actor {
Actor::new(name, w, h, event_handler)
}
pub fn add_new_actor_to_stage(&mut self, stage_name: &String, actor: Actor<'a>) {
match self.stage_map.get(stage_name) {
Some(&index) => {
self.stage_list[index].add_actor(actor);
},
_ => println!("Can't find the stage with the given name: {}", stage_name)
}
}
pub fn set_stage_needs_layout(&mut self, stage_name: &String) {
match self.stage_map.get(stage_name) {
Some(&index) => {
self.stage_list[index].set_needs_layout();
},
_ => println!("Can't find the stage with the given name: {}", stage_name)
}
}
pub fn set_visible_stage(&mut self, name: &String, visible: bool) {
match self.stage_map.get(name) {
Some(&index) => {
self.stage_list[index].set_visible(visible);
},
_ => println!("Can't find the stage with the given name: {}", name)
}
}
fn compile_shader(&mut self) {
unsafe {
let vertex_shader = gl::CreateShader(gl::VERTEX_SHADER);
let c_str_vert = CString::new(VERTEX_SHADER_SOURCE.as_bytes()).unwrap();
gl::ShaderSource(vertex_shader, 1, &c_str_vert.as_ptr(), ptr::null());
gl::CompileShader(vertex_shader);
let mut success = gl::FALSE as GLint;
let mut info_log = Vec::with_capacity(512);
info_log.set_len(512 - 1);
gl::GetShaderiv(vertex_shader, gl::COMPILE_STATUS, &mut success);
if success != gl::TRUE as GLint {
gl::GetShaderInfoLog(vertex_shader, 512, ptr::null_mut(), info_log.as_mut_ptr() as *mut GLchar);
let s = str::from_utf8(&info_log);
match s {
Err(_) => { println!("Failed to decode using"); }
Ok(s) => { println!("Decoded with to '{}'", s); }
}
}
let fragment_shader = gl::CreateShader(gl::FRAGMENT_SHADER);
let c_str_frag = CString::new(FRAGMENT_SHADER_SOURCE.as_bytes()).unwrap();
gl::ShaderSource(fragment_shader, 1, &c_str_frag.as_ptr(), ptr::null());
gl::CompileShader(fragment_shader);
gl::GetShaderiv(fragment_shader, gl::COMPILE_STATUS, &mut success);
if success != gl::TRUE as GLint {
gl::GetShaderInfoLog(fragment_shader, 512, ptr::null_mut(), info_log.as_mut_ptr() as *mut GLchar);
println!("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n{}", str::from_utf8(&info_log).unwrap());
}
self.shader_program = gl::CreateProgram();
gl::AttachShader(self.shader_program, vertex_shader);
gl::AttachShader(self.shader_program, fragment_shader);
gl::LinkProgram(self.shader_program);
gl::GetProgramiv(self.shader_program, gl::LINK_STATUS, &mut success);
if success != gl::TRUE as GLint {
gl::GetProgramInfoLog(self.shader_program, 512, ptr::null_mut(), info_log.as_mut_ptr() as *mut GLchar);
println!("ERROR::SHADER::PROGRAM::COMPILATION_FAILED\n{}", str::from_utf8(&info_log).unwrap());
}
gl::DeleteShader(vertex_shader);
gl::DeleteShader(fragment_shader);
}
}
pub fn add_stage(&mut self, mut stage: Stage<'a>) -> String {
stage.initialize();
let stage_name = stage.name.to_string();
self.stage_list.push(stage);
self.stage_map.insert(stage_name.to_string(), self.stage_list.len() - 1);
stage_name
}
pub fn handle_input(&mut self, key: usize) {
for stage in self.stage_list.iter_mut() {
stage.handle_input(key);
}
}
pub fn render(&mut self) {
unsafe {
gl::ClearColor(0.2, 0.3, 0.3, 1.0);
gl::Clear(gl::COLOR_BUFFER_BIT);
for stage in self.stage_list.iter_mut() {
stage.render(self.shader_program);
}
}
}
}