Add a CRT shader effect over the game.

This commit is contained in:
2026-09-29 07:53:43 +02:00
parent ce142a2d02
commit f345b0a5f4
+18 -14
View File
@@ -26,6 +26,7 @@ RESOLUTIONS := [?]Resolution{
Game :: struct { Game :: struct {
viewport: rl.RenderTexture2D, viewport: rl.RenderTexture2D,
bg_texture: rl.Texture2D, bg_texture: rl.Texture2D,
crt_shader: rl.Shader,
scene_stack: [dynamic]Scene, scene_stack: [dynamic]Scene,
should_close: bool, should_close: bool,
audio: Audio_Manager, audio: Audio_Manager,
@@ -65,8 +66,8 @@ game_init :: proc(game: ^Game) {
game.scene_stack = make([dynamic]Scene, 0, 8) game.scene_stack = make([dynamic]Scene, 0, 8)
game.viewport = rl.LoadRenderTexture(VIEWPORT_W, VIEWPORT_H) game.viewport = rl.LoadRenderTexture(VIEWPORT_W, VIEWPORT_H)
// Force nearest neighbor point filtering on the viewport texture // Look into texture filters
rl.SetTextureFilter(game.viewport.texture, .POINT) rl.SetTextureFilter(game.viewport.texture, .BILINEAR)
// Generate soft CRT background radial texture once on startup // Generate soft CRT background radial texture once on startup
bg_center := rl.Color{22, 28, 36, 255} bg_center := rl.Color{22, 28, 36, 255}
@@ -74,7 +75,8 @@ game_init :: proc(game: ^Game) {
bg_img := rl.GenImageGradientRadial(VIEWPORT_W, VIEWPORT_H, 0.3, bg_center, bg_corner) bg_img := rl.GenImageGradientRadial(VIEWPORT_W, VIEWPORT_H, 0.3, bg_center, bg_corner)
game.bg_texture = rl.LoadTextureFromImage(bg_img) game.bg_texture = rl.LoadTextureFromImage(bg_img)
rl.UnloadImage(bg_img) // Free CPU memory rl.UnloadImage(bg_img) // Free CPU memory
// rl.SetTextureFilter(game.bg_texture, .BILINEAR)
game.crt_shader = rl.LoadShader(nil, "assets/shaders/crt.fs")
game.should_close = false game.should_close = false
set_root_scene(game, make_main_menu()) set_root_scene(game, make_main_menu())
@@ -121,6 +123,7 @@ game_render :: proc(game: ^Game) {
game_quit :: proc(game: ^Game) { game_quit :: proc(game: ^Game) {
audio_manager_quit(&game.audio) audio_manager_quit(&game.audio)
rl.UnloadShader(game.crt_shader)
rl.UnloadTexture(game.bg_texture) rl.UnloadTexture(game.bg_texture)
rl.UnloadRenderTexture(game.viewport) rl.UnloadRenderTexture(game.viewport)
delete(GLOBAL_EVENTS) delete(GLOBAL_EVENTS)
@@ -156,15 +159,16 @@ game_set_resolution :: proc(game: ^Game, res_index: int) {
render_game_to_window :: proc(game: ^Game) { render_game_to_window :: proc(game: ^Game) {
texture := game.viewport.texture texture := game.viewport.texture
rl.DrawTexturePro( rl.BeginShaderMode(game.crt_shader)
texture, rl.DrawTexturePro(
// src: inverted y coordinate flips opengl texture buffer correctly texture,
rl.Rectangle{0, 0, f32(texture.width), -f32(texture.height)}, // src: inverted y coordinate flips opengl texture buffer correctly
// dst: scales viewport canvas to fill window resolution rl.Rectangle{0, 0, f32(texture.width), -f32(texture.height)},
rl.Rectangle{0, 0, f32(game.window_w), f32(game.window_h)}, // dst: scales viewport canvas to fill window resolution
rl.Vector2{}, rl.Rectangle{0, 0, f32(game.window_w), f32(game.window_h)},
0, rl.Vector2{},
rl.WHITE, 0,
) rl.WHITE,
)
rl.EndShaderMode()
} }