From 87baa92b98dfcc6f2a0afa0a13e8986f3d4258c4 Mon Sep 17 00:00:00 2001 From: John-Mark Allen Date: Mon, 21 Jun 2021 22:29:25 +0100 Subject: [PATCH] Fix up texture use Previous `prepare_font_atlas` silently assumed a TrivialTextureMap. Also adds an impl of `TextureMap` for `imgui::Textures` as a further example. --- imgui-glow-renderer/src/lib.rs | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/imgui-glow-renderer/src/lib.rs b/imgui-glow-renderer/src/lib.rs index 3c63699..546db98 100644 --- a/imgui-glow-renderer/src/lib.rs +++ b/imgui-glow-renderer/src/lib.rs @@ -250,7 +250,7 @@ where pub fn initialize( gl: &G, imgui_context: &mut imgui::Context, - texture_map: T, + mut texture_map: T, shader_provider: S, context_state_manager: C, ) -> Result { @@ -286,7 +286,7 @@ where let mut context_state_manager = context_state_manager; context_state_manager.pre_init(gl, gl_version)?; - let font_atlas_texture = prepare_font_atlas(gl, imgui_context.fonts())?; + let font_atlas_texture = prepare_font_atlas(gl, imgui_context.fonts(), &mut texture_map)?; let mut shader_provider = shader_provider; shader_provider.initialize(gl, gl_version)?; @@ -620,16 +620,36 @@ where pub trait TextureMap { fn gl_texture(&self, imgui_texture: imgui::TextureId) -> Option; + + fn register(&mut self, gl_texture: glow::Texture) -> Option; } +/// Texture map where the imgui texture ID is simply the OpenGL texture ID #[derive(Default)] pub struct TrivialTextureMap(); impl TextureMap for TrivialTextureMap { + #[inline(always)] fn gl_texture(&self, imgui_texture: imgui::TextureId) -> Option { #[allow(clippy::cast_possible_truncation)] Some(imgui_texture.id() as _) } + + #[inline(always)] + fn register(&mut self, gl_texture: glow::Texture) -> Option { + Some(imgui::TextureId::new(gl_texture as _)) + } +} + +/// `Textures` from the `imgui` crate is a simple choice for a texture map +impl TextureMap for imgui::Textures { + fn gl_texture(&self, imgui_texture: imgui::TextureId) -> Option { + self.get(imgui_texture).cloned() + } + + fn register(&mut self, gl_texture: glow::Texture) -> Option { + Some(self.insert(gl_texture)) + } } pub trait ContextStateManager { @@ -1166,6 +1186,7 @@ pub enum InitError { Shader(ShaderError), CreateBufferObject(String), CreateTexture(String), + RegisterTexture, UserError(String), } @@ -1184,6 +1205,7 @@ impl Display for InitError { Self::Shader(error) => write!(f, "Shader initialisation error: {}", error), Self::CreateBufferObject(msg) => write!(f, "Error creating buffer object: {}", msg), Self::CreateTexture(msg) => write!(f, "Error creating texture object: {}", msg), + Self::RegisterTexture => write!(f, "Error registering texture in texture map"), Self::UserError(msg) => write!(f, "Initialization error: {}", msg), } } @@ -1197,9 +1219,10 @@ impl From for InitError { pub type RenderError = String; -fn prepare_font_atlas( +fn prepare_font_atlas( gl: &G, mut fonts: imgui::FontAtlasRefMut, + texture_map: &mut T, ) -> Result { #![allow(clippy::cast_possible_wrap)] @@ -1232,7 +1255,9 @@ fn prepare_font_atlas( ) } - fonts.tex_id = TextureId::new(gl_texture as _); + fonts.tex_id = texture_map + .register(gl_texture) + .ok_or(InitError::RegisterTexture)?; Ok(gl_texture) }