API-breaking version of changes

This commit is contained in:
Cosmic Chip Socket 2020-06-29 17:23:52 -04:00
parent 4ab3aa6642
commit 7b31db0526

View File

@ -13,7 +13,6 @@ use glium::{
use imgui::internal::RawWrapper; use imgui::internal::RawWrapper;
use imgui::{DrawCmd, DrawCmdParams, DrawData, ImString, TextureId, Textures}; use imgui::{DrawCmd, DrawCmdParams, DrawData, ImString, TextureId, Textures};
use std::borrow::Cow; use std::borrow::Cow;
use std::collections::HashMap;
use std::error::Error; use std::error::Error;
use std::fmt; use std::fmt;
use std::rc::Rc; use std::rc::Rc;
@ -87,12 +86,17 @@ impl From<DrawError> for RendererError {
} }
} }
pub struct TextureEntry {
pub texture: Rc<Texture2d>,
pub mag_filter: MagnifySamplerFilter,
pub min_filter: MinifySamplerFilter,
}
pub struct Renderer { pub struct Renderer {
ctx: Rc<Context>, ctx: Rc<Context>,
program: Program, program: Program,
font_texture: Texture2d, font_texture: TextureEntry,
textures: Textures<Rc<Texture2d>>, textures: Textures<TextureEntry>,
filters: HashMap<TextureId, (MinifySamplerFilter, MagnifySamplerFilter)>,
} }
impl Renderer { impl Renderer {
@ -111,17 +115,16 @@ impl Renderer {
program, program,
font_texture, font_texture,
textures: Textures::new(), textures: Textures::new(),
filters: HashMap::new(),
}) })
} }
pub fn reload_font_texture(&mut self, ctx: &mut imgui::Context) -> Result<(), RendererError> { pub fn reload_font_texture(&mut self, ctx: &mut imgui::Context) -> Result<(), RendererError> {
self.font_texture = upload_font_texture(ctx.fonts(), &self.ctx)?; self.font_texture = upload_font_texture(ctx.fonts(), &self.ctx)?;
Ok(()) Ok(())
} }
pub fn textures(&mut self) -> &mut Textures<Rc<Texture2d>> { pub fn textures(&mut self) -> &mut Textures<TextureEntry> {
&mut self.textures &mut self.textures
} }
fn lookup_texture(&self, texture_id: TextureId) -> Result<&Texture2d, RendererError> { fn lookup_texture(&self, texture_id: TextureId) -> Result<&TextureEntry, RendererError> {
if texture_id.id() == usize::MAX { if texture_id.id() == usize::MAX {
Ok(&self.font_texture) Ok(&self.font_texture)
} else if let Some(texture) = self.textures.get(texture_id) { } else if let Some(texture) = self.textures.get(texture_id) {
@ -130,14 +133,6 @@ impl Renderer {
Err(RendererError::BadTexture(texture_id)) Err(RendererError::BadTexture(texture_id))
} }
} }
pub fn set_texture_filter(
&mut self,
texture_id: TextureId,
min_filter: MinifySamplerFilter,
mag_filter: MagnifySamplerFilter,
) {
self.filters.insert(texture_id, (min_filter, mag_filter));
}
pub fn render<T: Surface>( pub fn render<T: Surface>(
&mut self, &mut self,
target: &mut T, target: &mut T,
@ -198,15 +193,7 @@ impl Renderer {
&& clip_rect[2] >= 0.0 && clip_rect[2] >= 0.0
&& clip_rect[3] >= 0.0 && clip_rect[3] >= 0.0
{ {
let (min_filter, mag_filter) = self let entry = self.lookup_texture(texture_id)?;
.filters
.get(&texture_id)
.cloned()
.or(Some((
MinifySamplerFilter::Linear,
MagnifySamplerFilter::Linear,
)))
.expect("Bad texture filter");
target.draw( target.draw(
&vtx_buffer, &vtx_buffer,
@ -216,9 +203,9 @@ impl Renderer {
&self.program, &self.program,
&uniform! { &uniform! {
matrix: matrix, matrix: matrix,
tex: self.lookup_texture(texture_id)?.sampled() tex: entry.texture.sampled()
.minify_filter(min_filter) .minify_filter(entry.min_filter)
.magnify_filter(mag_filter) .magnify_filter(entry.mag_filter)
.wrap_function(SamplerWrapFunction::BorderClamp) .wrap_function(SamplerWrapFunction::BorderClamp)
}, },
&DrawParameters { &DrawParameters {
@ -251,7 +238,7 @@ impl Renderer {
fn upload_font_texture( fn upload_font_texture(
mut fonts: imgui::FontAtlasRefMut, mut fonts: imgui::FontAtlasRefMut,
ctx: &Rc<Context>, ctx: &Rc<Context>,
) -> Result<Texture2d, RendererError> { ) -> Result<TextureEntry, RendererError> {
let texture = fonts.build_rgba32_texture(); let texture = fonts.build_rgba32_texture();
let data = RawImage2d { let data = RawImage2d {
data: Cow::Borrowed(texture.data), data: Cow::Borrowed(texture.data),
@ -261,7 +248,11 @@ fn upload_font_texture(
}; };
let font_texture = Texture2d::with_mipmaps(ctx, data, MipmapsOption::NoMipmap)?; let font_texture = Texture2d::with_mipmaps(ctx, data, MipmapsOption::NoMipmap)?;
fonts.tex_id = TextureId::from(usize::MAX); fonts.tex_id = TextureId::from(usize::MAX);
Ok(font_texture) Ok(TextureEntry {
texture: Rc::new(font_texture),
mag_filter: MagnifySamplerFilter::Linear,
min_filter: MinifySamplerFilter::Linear,
})
} }
fn compile_default_program<F: Facade>(facade: &F) -> Result<Program, ProgramChooserCreationError> { fn compile_default_program<F: Facade>(facade: &F) -> Result<Program, ProgramChooserCreationError> {