remove game object handles
This commit is contained in:
@@ -3,6 +3,7 @@ use cgmath::{Deg, Euler, Quaternion, vec3};
|
||||
use glyph_brush::{BrushAction, BrushError, GlyphBrush, GlyphBrushBuilder, GlyphVertex, Rectangle, Section, Text};
|
||||
use glyph_brush::ab_glyph::FontArc;
|
||||
use vulkano::format::Format;
|
||||
use vulkano::image::Dimensions;
|
||||
use vulkano::sampler::{Filter, SamplerAddressMode};
|
||||
use winit::event::Event;
|
||||
|
||||
@@ -11,7 +12,7 @@ use player::Player;
|
||||
|
||||
use crate::{config::LogConfig, vulkan};
|
||||
use crate::input::InputState;
|
||||
use crate::vulkan::{Game, Mesh, MeshHandle, TextVertex, Texture, Vertex, VulkanRenderer};
|
||||
use crate::vulkan::{Game, MeshHandle, TextVertex, Texture, Vertex, VulkanRenderer};
|
||||
use crate::vulkan::gameobject::{GameObject, GameObjectHandle, Updatable};
|
||||
use crate::vulkan::mesh::{self, CPUMesh, CPUVertex};
|
||||
use crate::vulkan::pipelines::vs::ty::ObjectUniformData;
|
||||
@@ -22,7 +23,7 @@ mod level;
|
||||
pub struct TestGame {
|
||||
pub input: InputState,
|
||||
pub player: Player,
|
||||
pub game_objects: Vec<GameObjectHandle>,
|
||||
pub game_objects: Vec<GameObject>,
|
||||
pub text_objects: Vec<TextObject>,
|
||||
pub log_config: LogConfig,
|
||||
pub texture_index_counter: usize,
|
||||
@@ -33,6 +34,10 @@ pub struct TestGame {
|
||||
}
|
||||
|
||||
impl Game for TestGame {
|
||||
fn get_game_objects(&self) -> &Vec<GameObject> {
|
||||
&self.game_objects
|
||||
}
|
||||
|
||||
fn on_window_event(self: &mut Self, event: &Event<()>) {
|
||||
self.input.on_window_event(event);
|
||||
}
|
||||
@@ -45,9 +50,11 @@ impl Game for TestGame {
|
||||
|
||||
// Component update
|
||||
let input = &self.input;
|
||||
let objs = &mut self.game_objects;
|
||||
let components = &mut self.components;
|
||||
|
||||
components.iter_mut().for_each(|component| {
|
||||
component.update(frame_time, &input, renderer);
|
||||
component.update(frame_time, &input, renderer, objs);
|
||||
});
|
||||
|
||||
// User interaction
|
||||
@@ -76,13 +83,13 @@ impl Game for TestGame {
|
||||
|
||||
if self.input.button_just_pressed("test") {
|
||||
// self.paused = !self.paused;
|
||||
self.text_objects[0].update_text("yeet", 100.0, renderer);
|
||||
self.text_objects[0].update_text(":)", 200.0, renderer, &mut self.game_objects);
|
||||
}
|
||||
|
||||
// Custom game object stuff
|
||||
let light_pos = vec3(2.0, 0.5, 2.0);
|
||||
if !self.paused {
|
||||
self.player.update(frame_time, &self.input, renderer);
|
||||
self.player.update(frame_time, &self.input, renderer, &mut self.game_objects);
|
||||
}
|
||||
|
||||
// End frame
|
||||
@@ -173,14 +180,13 @@ impl TestGame {
|
||||
}
|
||||
|
||||
pub fn add_game_object(&mut self, renderer: &mut VulkanRenderer, mesh: MeshHandle) -> GameObjectHandle {
|
||||
let obj = GameObject::new(mesh);
|
||||
let obj_handle = renderer.add_game_object(obj);
|
||||
self.game_objects.push(obj_handle);
|
||||
self.game_objects.last().unwrap().clone()
|
||||
let mut obj = GameObject::new(mesh);
|
||||
obj.init_descriptor_sets(renderer);
|
||||
self.game_objects.push(obj);
|
||||
self.game_objects.len() - 1
|
||||
}
|
||||
|
||||
fn convert_vertices(vertex_data: GlyphVertex) -> Vec<TextVertex> {
|
||||
// println!("VD: {:?}", vertex_data);
|
||||
let result = vec![
|
||||
TextVertex { position: [vertex_data.pixel_coords.min.x, vertex_data.pixel_coords.min.y, 0.], uv: [vertex_data.tex_coords.min.x, vertex_data.tex_coords.min.y] },
|
||||
TextVertex { position: [vertex_data.pixel_coords.min.x, vertex_data.pixel_coords.max.y, 0.], uv: [vertex_data.tex_coords.min.x, vertex_data.tex_coords.max.y] },
|
||||
@@ -208,9 +214,11 @@ impl TestGame {
|
||||
self.texture_index_counter += 1;
|
||||
}, Self::convert_vertices),
|
||||
self.texture_index_counter - 1,
|
||||
uploaded_texture.as_mut(),
|
||||
None,
|
||||
None,
|
||||
renderer);
|
||||
|
||||
|
||||
TextObject {
|
||||
brush: glyph_brush,
|
||||
texture: uploaded_texture.unwrap(),
|
||||
@@ -233,28 +241,20 @@ pub fn print_quat_as_euler(quat: Quaternion<f32>) {
|
||||
|
||||
pub fn update_text_texture(old_texture: Option<&mut Texture>, renderer: &mut VulkanRenderer, rect: Rectangle<u32>, text_data: &[u8]) -> (Option<Texture>, u32) {
|
||||
let size = u32::max(rect.width(), rect.height());
|
||||
let final_texture;
|
||||
let mut final_texture = None;
|
||||
|
||||
if let Some(old_tex) = old_texture {
|
||||
println!("rect: {:?}", rect);
|
||||
final_texture = renderer.update_texture(old_tex, text_data, [rect.width(), rect.height(), 1], [rect.min[0], rect.min[1], 0], renderer.device.clone());
|
||||
if let Some(_tex) = final_texture.clone() {
|
||||
println!("Updating texture size. TODO!");
|
||||
// renderer.game_data.textures.push(tex.clone());
|
||||
// go_handle.get_game_object_mut(renderer).unwrap().textures.texture_index = renderer.game_data.textures.len() - 1;
|
||||
// renderer.update_descriptor_set(&mut go_handle);
|
||||
}
|
||||
renderer.update_texture(old_tex, text_data, [rect.width(), rect.height(), 1], [rect.min[0], rect.min[1], 0], renderer.device.clone());
|
||||
} else {
|
||||
println!("Init size: {:?}", rect);
|
||||
let tex = renderer.upload_texture(text_data, size, size, Format::R8Unorm, Filter::Nearest, SamplerAddressMode::ClampToEdge, renderer.device.clone());
|
||||
renderer.game_data.textures.push(tex.clone());
|
||||
final_texture = Some(tex)
|
||||
final_texture = Some(tex);
|
||||
}
|
||||
|
||||
(final_texture, size)
|
||||
}
|
||||
|
||||
pub fn process_text_brush_data(result: Result<BrushAction<Vec<TextVertex>>, BrushError>, texture_index: usize, mesh_index: Option<usize>, renderer: &mut VulkanRenderer) -> Option<MeshHandle> {
|
||||
pub fn process_text_brush_data(result: Result<BrushAction<Vec<TextVertex>>, BrushError>, texture_index: usize, texture: Option<&mut Texture>, mesh_index: Option<usize>, game_object: Option<&mut GameObject>, renderer: &mut VulkanRenderer) -> Option<MeshHandle> {
|
||||
match result {
|
||||
Ok(BrushAction::Draw(quads)) => {
|
||||
let mut final_vertices = vec![];
|
||||
@@ -289,7 +289,11 @@ pub fn process_text_brush_data(result: Result<BrushAction<Vec<TextVertex>>, Brus
|
||||
}
|
||||
},
|
||||
Ok(BrushAction::ReDraw) => None,
|
||||
Err(BrushError::TextureTooSmall { suggested }) => { println!("texture too small, suggested: {:?}!", suggested); None },
|
||||
Err(BrushError::TextureTooSmall { suggested }) => {
|
||||
let size = Dimensions::Dim2d { width: suggested.0, height: suggested.1 };
|
||||
renderer.resize_texture(game_object.unwrap(), texture.unwrap(), size, renderer.device.clone());
|
||||
None
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -301,19 +305,20 @@ pub struct TextObject {
|
||||
}
|
||||
|
||||
impl TextObject {
|
||||
pub fn update_text(&mut self, new_text: &str, new_size: f32, renderer: &mut VulkanRenderer) {
|
||||
pub fn update_text(&mut self, new_text: &str, new_size: f32, renderer: &mut VulkanRenderer, game_objects: &mut Vec<GameObject>) {
|
||||
self.brush.queue(Section::default().add_text(Text::new(new_text).with_scale(new_size)));
|
||||
let mut old_texture = self.texture.clone();
|
||||
let mut new_texture = None;
|
||||
let mut new_size = None;
|
||||
|
||||
let mesh_index = self.game_object.get_game_object_mut(renderer).unwrap().mesh_index;
|
||||
let go = &mut game_objects[self.game_object];
|
||||
let mesh_index = go.mesh_index;
|
||||
|
||||
process_text_brush_data(self.brush.process_queued(|rect, text_data| {
|
||||
let (t, s) = update_text_texture(Some(&mut old_texture), renderer, rect, text_data);
|
||||
new_texture = t;
|
||||
new_size = Some(s);
|
||||
}, TestGame::convert_vertices), 420, Some(mesh_index), renderer);
|
||||
}, TestGame::convert_vertices), 420, Some(&mut self.texture), Some(mesh_index), Some(go), renderer);
|
||||
if let Some(tex) = new_texture {
|
||||
self.texture = tex;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user