def __init__(self, output, default_font, program=None): self.output = output self.default_font = default_font self.stream = VertexStream(fmt) self.textures = TextureCache() if program is None: self.program = Program.load(in_module('shaders/flat.glsl')) else: self.program = program
class Renderer(object): def __init__(self, output, default_font, program=None): self.output = output self.default_font = default_font self.stream = VertexStream(fmt) self.textures = TextureCache() if program is None: self.program = Program.load(in_module('shaders/flat.glsl')) else: self.program = program def bind(self): self.output.bind() self.program.use() self.program.uniform2f('resolution', (self.output.width, self.output.height)) glViewport(0, 0, self.output.width, self.output.height) self.stream.vbo.bind() fmt.use(self.program) def unbind(self): fmt.enduse(self.program) self.stream.vbo.unbind() self.program.enduse() self.output.unbind() def rectangle(self, rect, image=image_empty, color=white, gradient=None): if isinstance(image, Patch9) and gradient is None: patch9 = image image = image.image texture = self.textures.get(image) texture.bind() patch9_cell_flat(self.stream, patch9, 0, 0, rect, color) patch9_cell_flat(self.stream, patch9, 1, 0, rect, color) patch9_cell_flat(self.stream, patch9, 2, 0, rect, color) patch9_cell_flat(self.stream, patch9, 0, 1, rect, color) patch9_cell_flat(self.stream, patch9, 1, 1, rect, color) patch9_cell_flat(self.stream, patch9, 2, 1, rect, color) patch9_cell_flat(self.stream, patch9, 0, 2, rect, color) patch9_cell_flat(self.stream, patch9, 1, 2, rect, color) patch9_cell_flat(self.stream, patch9, 2, 2, rect, color) self.stream.flush() texture.unbind() elif isinstance(image, Patch9): patch9 = image image = image.image texture = self.textures.get(image) texture.bind() patch9_cell_gradient(self.stream, 0, 0, rect, gradient) patch9_cell_gradient(self.stream, 1, 0, rect, gradient) patch9_cell_gradient(self.stream, 2, 0, rect, gradient) patch9_cell_gradient(self.stream, 0, 1, rect, gradient) patch9_cell_gradient(self.stream, 1, 1, rect, gradient) patch9_cell_gradient(self.stream, 2, 1, rect, gradient) patch9_cell_gradient(self.stream, 0, 2, rect, gradient) patch9_cell_gradient(self.stream, 1, 2, rect, gradient) patch9_cell_gradient(self.stream, 2, 2, rect, gradient) self.stream.flush() texture.unbind() else: if gradient is None: gradient = color, color, color, color texture = self.textures.get(image) texture.bind() quad_gradient(self.stream, rect, (0,1,1,0), gradient) self.stream.flush() texture.unbind() def text(self, pos, text, font=None, color=white): font = self.default_font if font is None else font texture = self.textures.get(font.image) texture.bind() characters_flat(self.stream, font, pos, text, color) self.stream.flush() texture.unbind()