Skip to content

Commit e0d4d41

Browse files
Add dim shader
1 parent 8f32b6f commit e0d4d41

File tree

6 files changed

+87
-11
lines changed

6 files changed

+87
-11
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ ggez = "0.5"
1111
rand = "0.7"
1212
ggez-goodies = { git = "https://github.com/ggez/ggez-goodies" }
1313
nalgebra-glm = "0.9.0"
14+
gfx = "0.18.2"

resources/shaders/dim.basic.glslf

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#version 150 core
2+
3+
in vec2 a_Pos;
4+
in vec2 a_Uv;
5+
6+
in vec4 a_Src;
7+
in vec4 a_TCol1;
8+
in vec4 a_TCol2;
9+
in vec4 a_TCol3;
10+
in vec4 a_TCol4;
11+
in vec4 a_Color;
12+
13+
layout (std140) uniform Globals {
14+
mat4 u_MVP;
15+
};
16+
17+
out vec2 v_Uv;
18+
out vec4 v_Color;
19+
20+
void main() {
21+
v_Uv = a_Uv * a_Src.zw + a_Src.xy;
22+
v_Color = a_Color;
23+
mat4 instance_transform = mat4(a_TCol1, a_TCol2, a_TCol3, a_TCol4);
24+
vec4 position = instance_transform * vec4(a_Pos, 0.0, 1.0);
25+
26+
gl_Position = u_MVP * position;
27+
}

resources/shaders/dim.glslf

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#version 150 core
2+
3+
uniform sampler2D t_Texture;
4+
in vec2 v_Uv;
5+
in vec4 v_Color;
6+
out vec4 Target0;
7+
8+
layout (std140) uniform Globals {
9+
mat4 u_MVP;
10+
};
11+
12+
layout (std140) uniform Dim {
13+
float u_Rate;
14+
};
15+
16+
void main() {
17+
Target0 = texture(t_Texture, v_Uv) * v_Color * u_Rate;
18+
}

src/components/player.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl Player {
9191
if self.pos_x as i32 == l as i32 {
9292
self.lerp_to = None;
9393
} else {
94-
self.pos_x = lerp(self.pos_x, l, 0.4);
94+
self.pos_x = lerp(self.pos_x, l, 0.5);
9595
}
9696
}
9797

src/game.rs

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
use std::{io::Read, sync::Mutex};
22

3-
use ggez::{
4-
audio::{SoundSource, Source},
5-
event::KeyCode,
6-
graphics::{self, Color, DrawParam, Scale, Text},
7-
mint,
8-
nalgebra::Point2,
9-
timer, Context, GameResult,
10-
};
3+
use ggez::{Context, GameResult, audio::{SoundSource, Source}, event::KeyCode, graphics::{self, Color, DrawParam, Scale, Shader, Text}, mint, nalgebra::Point2, timer};
114
use ggez_goodies::{
125
camera::{Camera, CameraDraw},
136
nalgebra_glm::Vec2,
147
particle::{EmissionShape, ParticleSystem, ParticleSystemBuilder, Transition},
158
};
16-
use graphics::{Font, Image, TextFragment};
9+
use graphics::{Font, GlBackendSpec, Image, ShaderGeneric, TextFragment};
1710
use rand::Rng;
1811

1912
use crate::{
@@ -28,6 +21,14 @@ use crate::{
2821
Screen, HEIGHT, WIDTH,
2922
};
3023

24+
use gfx::*;
25+
26+
gfx_defines! {
27+
constant Dim {
28+
rate: f32 = "u_Rate",
29+
}
30+
}
31+
3132
pub struct Game {
3233
ground: Vec<Tile>,
3334
clouds: Vec<Cloud>,
@@ -52,6 +53,8 @@ pub struct Game {
5253
elapsed_shake: Option<(f32, Vec2, f32)>,
5354
tics: Option<i32>,
5455
particles: Vec<(ParticleSystem, f32, f32, i32)>,
56+
57+
dim_shader: ShaderGeneric<GlBackendSpec, Dim>,
5558
}
5659

5760
impl Game {
@@ -72,9 +75,21 @@ impl Game {
7275
let mut player = None;
7376

7477
let mut draw_pos = 0.;
75-
7678
let draw_inc = 64.;
7779

80+
let dim_constant = Dim {
81+
rate: 0.5,
82+
};
83+
84+
let dim_shader = Shader::new(
85+
ctx,
86+
"/shaders/dim.basic.glslf",
87+
"/shaders/dim.glslf",
88+
dim_constant,
89+
"Dim",
90+
None
91+
).unwrap();
92+
7893
for id in buffer.chars() {
7994
match id {
8095
'[' => {
@@ -181,10 +196,24 @@ impl Game {
181196
elapsed_shake: None,
182197
tics: None,
183198
particles: vec![],
199+
200+
dim_shader
184201
})
185202
}
186203

187204
pub fn draw(&mut self, ctx: &mut Context) -> GameResult<()> {
205+
if let Some(_t) = self.tics {
206+
let _lock = graphics::use_shader(ctx, &self.dim_shader);
207+
208+
self.inner_draw(ctx)
209+
}
210+
211+
else {
212+
self.inner_draw(ctx)
213+
}
214+
}
215+
216+
fn inner_draw(&mut self, ctx: &mut Context) -> GameResult<()> {
188217
graphics::clear(ctx, graphics::BLACK);
189218

190219
// Clouds

0 commit comments

Comments
 (0)