Skip to content

Commit

Permalink
Update ScanLine effect to remove jump and scale the same for differen…
Browse files Browse the repository at this point in the history
…t screen sizes
  • Loading branch information
dylanlangston committed Jan 3, 2024
1 parent 542a334 commit 6dfd983
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
11 changes: 7 additions & 4 deletions src/Asteroids.zig
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ pub inline fn main() void {
var view = Shared.View.ViewLocator.Build(current_view);
view.init();

var scanlinePosition: f32 = 0;
var scanlinePosition: f32 = -10;

Shared.Log.Info("Begin Game Loop");
while (!raylib.windowShouldClose()) {
Expand All @@ -90,13 +90,16 @@ pub inline fn main() void {
defer current_view = new_view;

// Draw scan lines
const screenHeight: f32 = @floatFromInt(raylib.getScreenHeight());
const screenTick = screenHeight / 240;
scanlinePosition += raylib.getFrameTime() * 5;
if (scanlinePosition > 10) scanlinePosition = 0;
if (scanlinePosition > 0) scanlinePosition -= 10;
//raylib.drawLine(0, @intFromFloat(screenTick * (scanlinePosition + 10)), raylib.getScreenWidth(), @intFromFloat(screenTick * (scanlinePosition + 10)), Shared.Color.Red.Base);
Shared.Shader.DrawTexture(.ScanLine, .ScanLines, raylib.Rectangle.init(
0,
-10 + scanlinePosition,
screenTick * scanlinePosition,
@as(f32, @floatFromInt(raylib.getScreenWidth())),
@as(f32, @floatFromInt(raylib.getScreenHeight() + 20)),
screenHeight + (screenTick * 20),
));

if (Shared.Settings.GetSettings().Debug) {
Expand Down
7 changes: 4 additions & 3 deletions src/Shaders/ScanLines.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ varying vec2 fragTexCoord;
// Input uniform values
uniform sampler2D texture0;

// NOTE: Add here your custom variables

float frequency = 120.0;
uniform float screenHeight;

float frequency = 4.0;

void main()
{
// Scanlines method 2
float globalPos = (fragTexCoord.y) * frequency;
float globalPos = (fragTexCoord.y) * (screenHeight / frequency);
float wavePos = cos((fract(globalPos) - 0.5)*3.14);

vec4 color = texture2D(texture0, fragTexCoord);
Expand Down
6 changes: 6 additions & 0 deletions src/Shared.zig
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,12 @@ pub const Shared = struct {
loaded_settings.?.CurrentResolution.Width,
loaded_settings.?.CurrentResolution.Height,
);

// Update shader scanlines
const shader = Shared.Shader.Get(.ScanLines);
const screenHeight: f32 = @floatFromInt(raylib.getScreenHeight());
const screenHeightLoc = raylib.getShaderLocation(shader, "screenHeight");
raylib.setShaderValue(shader, screenHeightLoc, &screenHeight, @intFromEnum(raylib.ShaderUniformDataType.shader_uniform_float));
}

if (original_settings.UserLocale != loaded_settings.?.UserLocale) {
Expand Down

0 comments on commit 6dfd983

Please sign in to comment.