From a08bc07cbb7833f720277ca44d0ded657832d557 Mon Sep 17 00:00:00 2001 From: John Doty Date: Thu, 31 Aug 2023 20:39:07 -0700 Subject: [PATCH] [oden] Color and position for text --- game/log.ts | 8 +++++--- src/graphics.ts | 6 ++++-- src/lib.rs | 6 ++---- src/script/graphics.rs | 7 ++++--- src/text_shader.wgsl | 17 +++++++++++++---- types/graphics-core.d.ts | 2 +- 6 files changed, 29 insertions(+), 17 deletions(-) diff --git a/game/log.ts b/game/log.ts index 90b2ca5b..a988e2e7 100644 --- a/game/log.ts +++ b/game/log.ts @@ -1,4 +1,4 @@ -import { print } from "./graphics"; +import { color, print } from "./graphics"; const lines: string[] = []; @@ -8,8 +8,10 @@ export function log(...args: unknown[]) { } export function draw_log() { + color(1, 1, 1, 1); + let line_y = 3; for (const line of lines) { - print(line); - break; + print(3, line_y, line); + line_y += 8; } } diff --git a/src/graphics.ts b/src/graphics.ts index 4c999bcc..11a50ada 100644 --- a/src/graphics.ts +++ b/src/graphics.ts @@ -44,10 +44,12 @@ export function stroke(r: number, g: number, b: number, a: number = 1) { * separated by spaces. If you want them separated by something else, * format the string yourself. * + * @param x - The x coordinate of the upper-left corner of the text + * @param y - The y coordinate of the upper-left corner of the text * @param args - Arguments to print to the console. */ -export function print(...args: unknown[]) { - core.print(args.join(" ")); +export function print(x: number, y: number, ...args: unknown[]) { + core.print(args.join(" "), x, y); } /** diff --git a/src/lib.rs b/src/lib.rs index 40ed042e..e54ecec0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1237,13 +1237,11 @@ impl<'a> FrameBuilder<'a> { fn push_text(&mut self, pc: script::graphics::PrintCommand) { // println!("{}", pc.text); - let mut cursor_x = 0.0; - let cursor_y = 0.0; + let mut cursor_x = pc.pos[0]; + let cursor_y = pc.pos[1]; let font_id: u32 = 0; - // self.mode = ; // ? - let font = self.state.fonts.get_mut(&font_id).unwrap(); let mut glyphs = vec![]; for char in pc.text.chars() { diff --git a/src/script/graphics.rs b/src/script/graphics.rs index 011661c5..91418b91 100644 --- a/src/script/graphics.rs +++ b/src/script/graphics.rs @@ -8,6 +8,7 @@ use std::sync::Arc; #[derive(Debug)] pub struct PrintCommand { pub text: String, + pub pos: [f32; 2], } #[derive(Debug)] @@ -63,10 +64,10 @@ impl GraphicsImpl { } } - fn print(&self, text: String) -> () { + fn print(&self, text: String, x: f32, y: f32) -> () { let _ = self .sender - .send(GraphicsCommand::Print(PrintCommand { text })); + .send(GraphicsCommand::Print(PrintCommand { text, pos: [x, y] })); } fn cls(&self, r: f64, g: f64, b: f64) -> () { @@ -168,7 +169,7 @@ impl GraphicsAPI { let gfx = gfx.clone(); builder.export( "print", - ctx.new_fn(move |_: &ContextRef, t: String| gfx.print(t))?, + ctx.new_fn(move |_: &ContextRef, t: String, x: f32, y: f32| gfx.print(t, x, y))?, )?; } { diff --git a/src/text_shader.wgsl b/src/text_shader.wgsl index fe618525..5feb6e23 100644 --- a/src/text_shader.wgsl +++ b/src/text_shader.wgsl @@ -12,16 +12,19 @@ struct InstanceInput { @location(6) src_dims: vec2, @location(7) dest_top_left: vec2, @location(8) dest_dims: vec2, + @location(9) color: vec4, }; struct VertexOutput { @builtin(position) clip_position : vec4, @location(0) tex_coords : vec2, + @location(1) color: vec4, }; @vertex fn vs_main(vertex : VertexInput, instance : InstanceInput)->VertexOutput { var out : VertexOutput; out.tex_coords = instance.src_top_left + (vertex.tex_coords * instance.src_dims); + out.color = instance.color; let in_pos = instance.dest_top_left + (vec2f(vertex.position.x, vertex.position.y) * instance.dest_dims); @@ -38,16 +41,22 @@ struct VertexOutput { @group(1) @binding(1) var s_diffuse : sampler; @fragment fn fs_main(in : VertexOutput)->@location(0) vec4 { - // TODO: Should we be sampling here for the shader? - let tc = vec2(u32(in.tex_coords.x), u32(in.tex_coords.y)); let c = textureLoad(t_diffuse, tc, 0); + // If we were going to run this through the sampler it would look like + // this... but I don't know if that'w what we should do? Right now the + // direct load looks better? + // // let tc = in.tex_coords / vec2(textureDimensions(t_diffuse)); // let c = textureSample(t_diffuse, s_diffuse, tc); - - return vec4(c.r,c.r,c.r,c.r); + return vec4( + c.r * in.color.x, + c.r * in.color.y, + c.r * in.color.z, + c.r * in.color.a + ); //return vec4(1.0,1.0,1.0,1.0); } diff --git a/types/graphics-core.d.ts b/types/graphics-core.d.ts index a9d17c26..af53ae1c 100644 --- a/types/graphics-core.d.ts +++ b/types/graphics-core.d.ts @@ -2,7 +2,7 @@ // export function cls(r: number, g: number, b: number); -export function print(msg: string); +export function print(msg: string, x: number, y: number); export function color(r: number, g: number, b: number, a: number);