diff --git a/game/main.ts b/game/main.ts index fd199e3b..767b8f38 100644 --- a/game/main.ts +++ b/game/main.ts @@ -21,7 +21,9 @@ export function draw() { if (bot_sprite != undefined) { // ...it gets resolved here? use_texture(bot_sprite); - spr(10, 10, 32, 32, 0, 0); + + let y = (since_start() * 10) % 240; + spr(10, y, 32, 32, 0, 0); } // print("FRAME TIME:", since_last_frame()); } diff --git a/src/lib.rs b/src/lib.rs index 223c8b80..f147ad69 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -416,26 +416,27 @@ impl State { GraphicsCommand::Sprite(sc) => { vertices.push(Vertex { position: [sc.x, sc.y, 0.0], + tex_coords: [sc.u, sc.v], + }); + vertices.push(Vertex { + position: [sc.x, sc.y + sc.h, 0.0], tex_coords: [sc.u, sc.v + sc.sh], }); vertices.push(Vertex { position: [sc.x + sc.w, sc.y, 0.0], - tex_coords: [sc.u + sc.sw, sc.v + sc.sh], + tex_coords: [sc.u + sc.sw, sc.v], }); + vertices.push(Vertex { position: [sc.x, sc.y + sc.h, 0.0], - tex_coords: [sc.u, sc.v], - }); - vertices.push(Vertex { - position: [sc.x, sc.y + sc.h, 0.0], - tex_coords: [sc.u, sc.v], - }); - vertices.push(Vertex { - position: [sc.x + sc.w, sc.y, 0.0], - tex_coords: [sc.u + sc.sw, sc.v + sc.sh], + tex_coords: [sc.u, sc.v + sc.sh], }); vertices.push(Vertex { position: [sc.x + sc.w, sc.y + sc.h, 0.0], + tex_coords: [sc.u + sc.sw, sc.v + sc.sh], + }); + vertices.push(Vertex { + position: [sc.x + sc.w, sc.y, 0.0], tex_coords: [sc.u + sc.sw, sc.v], }); } diff --git a/src/shader.wgsl b/src/shader.wgsl deleted file mode 100644 index a1ab6a39..00000000 --- a/src/shader.wgsl +++ /dev/null @@ -1,77 +0,0 @@ -// Vertex shader - -struct ScreenUniform { - resolution : vec2f, -}; -@group(1) @binding(0) // 1. - var screen : ScreenUniform; - -struct VertexInput { - @location(0) position : vec3, @location(1) tex_coords : vec2, -}; - -struct VertexOutput { - @builtin(position) clip_position : vec4, - @location(0) tex_coords : vec2, -}; - -const RES = vec2f(320.0, 240.0); // The logical resolution of the screen. - -@vertex fn vs_main(model : VertexInput)->VertexOutput { - var out : VertexOutput; - out.tex_coords = model.tex_coords; - - let RES_AR = RES.x / RES.y; // The aspect ratio of the logical screen. - - // the actual resolution of the screen. - let screen_ar = screen.resolution.x / screen.resolution.y; - - // Compute the difference in resolution ... correctly? - // - // nudge is the amount to add to the logical resolution so that the pixels - // stay the same size but we respect the aspect ratio of the screen. (So - // there's more of them in either the x or y direction.) - var nudge = vec2f(0.0); - if (screen_ar > RES_AR) { - nudge.x = (RES.y * screen_ar) - RES.x; - } else { - nudge.y = (RES.x / screen_ar) - RES.y; - } - var new_logical_resolution = RES + nudge; - - // Now we can convert the incoming position to clip space, in the new screen. - let in_pos = vec2f(model.position.x, model.position.y); - let centered = in_pos + (nudge / 2.0); - let position = (2.0 * centered / new_logical_resolution) - 1.0; - - out.clip_position = vec4f(position, model.position.z, 1.0); - return out; -} - -// Fragment shader.... - -@group(0) @binding(0) var t_diffuse : texture_2d; -@group(0) @binding(1) var s_diffuse : sampler; - -@fragment fn fs_main(in : VertexOutput)->@location(0) vec4 { - // The "screen" is centered in the window, so anything outside of the - // screen borders should be black. But *where are they*? - let RES_AR = RES.x / RES.y; // The aspect ratio of the logical screen. - let screen_ar = screen.resolution.x / screen.resolution.y; - var black_mod = 1.0; - if (screen_ar > RES_AR) { - // Wider than tall, bars are on the left and right. - let active_width = screen.resolution.y * RES_AR; - let half_delta = (screen.resolution.x - active_width) / 2.0; - if (in.clip_position.x < half_delta || - in.clip_position.x > half_delta + active_width) { - black_mod = 0.0; - } - } else { - // Taller than wide, bars are on top and bottom. - } - - let dims = vec2f(textureDimensions(t_diffuse)); - - return black_mod * textureSample(t_diffuse, s_diffuse, in.tex_coords / dims); -} diff --git a/src/sprite_shader.wgsl b/src/sprite_shader.wgsl index a1ab6a39..e98d1b87 100644 --- a/src/sprite_shader.wgsl +++ b/src/sprite_shader.wgsl @@ -42,9 +42,10 @@ const RES = vec2f(320.0, 240.0); // The logical resolution of the screen. // Now we can convert the incoming position to clip space, in the new screen. let in_pos = vec2f(model.position.x, model.position.y); let centered = in_pos + (nudge / 2.0); - let position = (2.0 * centered / new_logical_resolution) - 1.0; + var position = (2.0 * centered / new_logical_resolution) - 1.0; + position.y = -position.y; - out.clip_position = vec4f(position, model.position.z, 1.0); + out.clip_position = vec4f(position.x, position.y, model.position.z, 1.0); return out; }