Created
October 31, 2024 00:49
-
-
Save shizeeg/108192339744552a75a4f528fc0e680a to your computer and use it in GitHub Desktop.
an attempt to sketch smth in Odin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import "core:fmt" | |
| import ma "vendor:miniaudio" | |
| import rl "vendor:raylib" | |
| main :: proc() { | |
| // Initialize the audio engine | |
| engine_config := ma.engine_config_init() | |
| engine_config.sampleRate = 44100 | |
| engine_config.channels = 2 | |
| engine: ma.engine | |
| sounds: [2]ma.sound | |
| result := ma.engine_init(&engine_config, &engine) | |
| if result != .SUCCESS { | |
| fmt.println("Failed to initialize audio engine") | |
| return | |
| } | |
| defer { | |
| // delete(sounds) | |
| for &snd in sounds { | |
| ma.sound_uninit(&snd) | |
| } | |
| ma.engine_uninit(&engine) | |
| } | |
| for &snd, ndx in sounds { | |
| result = ma.sound_init_from_file( | |
| &engine, | |
| fmt.caprintf("snd/engine_%d.wav", ndx), | |
| 0, | |
| nil, | |
| nil, | |
| &snd, | |
| ) | |
| if result != .SUCCESS { | |
| fmt.printf("Failed to load engine_%d.wav", ndx) | |
| } | |
| } | |
| ma.sound_set_looping(&sounds[0], true) | |
| ma.sound_set_looping(&sounds[1], true) | |
| Sprite :: enum { | |
| PLR = 80, | |
| TNK_UP = 0, | |
| TNK_DOWN = 16 * 2, | |
| TNK_LEFT = 16 * 4, | |
| TNK_RIGHT = 16 * 6, | |
| } | |
| speed :: 40.0 | |
| rl.InitWindow(1280, 720, "Texture Drawing") | |
| rl.SetTargetFPS(60) | |
| // Load the texture | |
| tiles := rl.LoadTexture("img/tiles.png") | |
| defer rl.UnloadTexture(tiles) | |
| srcRect := rl.Rectangle{cast(f32)(Sprite.PLR + Sprite.TNK_UP), 64, 16, 16} | |
| tnkPos := rl.Vector2{0, 0} | |
| camera := rl.Camera2D { | |
| zoom = 2.0, | |
| target = rl.Vector2{0, srcRect.height}, | |
| offset = rl.Vector2 { | |
| cast(f32)(rl.GetScreenWidth() / 2) - srcRect.width * 4, | |
| cast(f32)(rl.GetScreenHeight()) - srcRect.height, | |
| }, | |
| } | |
| // Main loop | |
| toggle_frame := false | |
| time_frame: f32 | |
| tint := rl.WHITE | |
| for !rl.WindowShouldClose() { | |
| if rl.IsKeyDown(.UP) { | |
| if ma.sound_is_playing(&sounds[0]) { | |
| ma.sound_stop(&sounds[0]) | |
| ma.sound_start(&sounds[1]) | |
| } | |
| base_frame := cast(f32)Sprite.PLR | |
| srcRect.x = base_frame | |
| tnkPos.y -= speed * rl.GetFrameTime() | |
| if time_frame > 0.05 { | |
| toggle_frame = !toggle_frame | |
| time_frame = 0.0 | |
| } | |
| if toggle_frame { | |
| tint = rl.RED | |
| srcRect.x += srcRect.width | |
| } else {srcRect.x = base_frame;tint = rl.WHITE} | |
| time_frame += rl.GetFrameTime() | |
| } else if rl.IsKeyDown(.LEFT) { | |
| if ma.sound_is_playing(&sounds[0]) { | |
| ma.sound_stop(&sounds[0]) | |
| ma.sound_start(&sounds[1]) | |
| } | |
| base_frame := cast(f32)(Sprite.PLR + Sprite.TNK_LEFT) | |
| srcRect.x = base_frame | |
| tnkPos.x -= speed * rl.GetFrameTime() | |
| if toggle_frame { | |
| tint = rl.RED | |
| srcRect.x += srcRect.width | |
| } else {srcRect.x = base_frame;tint = rl.WHITE} | |
| if time_frame > 0.05 { | |
| toggle_frame = !toggle_frame | |
| time_frame = 0.0 | |
| } | |
| time_frame += rl.GetFrameTime() | |
| } else if rl.IsKeyDown(.DOWN) { | |
| if ma.sound_is_playing(&sounds[0]) { | |
| ma.sound_stop(&sounds[0]) | |
| ma.sound_start(&sounds[1]) | |
| } | |
| base_frame := cast(f32)(Sprite.PLR + Sprite.TNK_DOWN) | |
| srcRect.x = base_frame | |
| tnkPos.y += speed * rl.GetFrameTime() | |
| if toggle_frame { | |
| tint = rl.RED | |
| srcRect.x += srcRect.width | |
| } else {srcRect.x = base_frame;tint = rl.WHITE} | |
| if time_frame > 0.05 { | |
| toggle_frame = !toggle_frame | |
| time_frame = 0.0 | |
| } | |
| time_frame += rl.GetFrameTime() | |
| } else if rl.IsKeyDown(.RIGHT) { | |
| if ma.sound_is_playing(&sounds[0]) { | |
| ma.sound_stop(&sounds[0]) | |
| ma.sound_start(&sounds[1]) | |
| } | |
| base_frame := cast(f32)(Sprite.PLR + Sprite.TNK_RIGHT) | |
| srcRect.x = base_frame | |
| tnkPos.x += speed * rl.GetFrameTime() | |
| if toggle_frame { | |
| tint = rl.RED | |
| srcRect.x += srcRect.width | |
| } else {srcRect.x = base_frame;tint = rl.WHITE} | |
| if time_frame > 0.05 { | |
| toggle_frame = !toggle_frame | |
| time_frame = 0.0 | |
| } | |
| time_frame += rl.GetFrameTime() | |
| } else { | |
| if !ma.sound_is_playing(&sounds[0]) { | |
| ma.sound_stop(&sounds[1]) | |
| ma.sound_start(&sounds[0]) | |
| } | |
| } | |
| // Draw the texture | |
| rl.BeginDrawing() | |
| rl.ClearBackground(rl.BLANK) | |
| rl.DrawFPS(0, 0) | |
| rl.DrawText(rl.TextFormat("%f", time_frame), 100, 0, 10, rl.YELLOW) | |
| rl.BeginMode2D(camera) | |
| rl.DrawTextureRec(tiles, srcRect, tnkPos, rl.WHITE) | |
| rl.EndMode2D() | |
| rl.EndDrawing() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment