From ef9caac6018ad1a0565bcacbe5e69eee0563a0bc Mon Sep 17 00:00:00 2001 From: Nefrace Date: Sun, 21 Apr 2024 00:28:56 +0300 Subject: [PATCH] mini-refactoring --- src/main.odin | 65 +++++++++++++++++++++++----------------------- src/player.odin | 63 ++++++++++++++++++++------------------------ src/resloader.odin | 10 +++---- 3 files changed, 65 insertions(+), 73 deletions(-) diff --git a/src/main.odin b/src/main.odin index a03451f..b8c7f78 100644 --- a/src/main.odin +++ b/src/main.odin @@ -1,29 +1,25 @@ package main import "core:math" -import rl "vendor:raylib" +import "core:math/rand" +import rl "vendor:raylib" Vec3 :: [3]f32 Entity :: struct { - position : Vec3, - velocity : Vec3, - size : Vec3, + position: Vec3, + velocity: Vec3, + size: Vec3, } -PAL_INT :: [6]u32 { - 0x272744ff, - 0x494d7eff, - 0x8b6d9cff, - 0xc69fa5ff, - 0xf2d3abff, - 0xfbf5efff, -} +PAL_INT :: [6]u32{0x272744ff, 0x494d7eff, 0x8b6d9cff, 0xc69fa5ff, 0xf2d3abff, 0xfbf5efff} PAL := [6]rl.Color{} -CAMERA : ^rl.Camera3D -TEXTURES : Textures +CAMERA: ^rl.Camera3D +TEXTURES: Textures + +discard_shader: rl.Shader // See shader below. Required for drawing sprites main :: proc() { for v, i in PAL_INT { @@ -37,57 +33,60 @@ main :: proc() { defer unload_textures(&TEXTURES) discard_shader = rl.LoadShaderFromMemory(nil, alpha_discard) - + + // Floor checkerboard img := rl.GenImageChecked(64, 64, 8, 8, PAL[1], PAL[2]) defer rl.UnloadImage(img) tex := rl.LoadTextureFromImage(img) defer rl.UnloadTexture(tex) - rl.SetTextureFilter(tex, rl.TextureFilter.POINT) + // Floor mesh plane := rl.GenMeshPlane(32, 32, 16, 16) defer rl.UnloadMesh(plane) mat := rl.LoadMaterialDefault() rl.SetMaterialTexture(&mat, rl.MaterialMapIndex.ALBEDO, tex) + player_load_resources() defer player_unload_resources() player := player_create() - CAMERA = &(player.camera) - rl.DisableCursor() + CAMERA = &(player.camera) torches := [10]Torch{} for &v, i in torches { - torches[i] = Torch{position = Vec3{f32(i) * 2, 2, 0}} + torches[i] = Torch { + position = Vec3{rand.float32_range(-10, 10), 2, rand.float32_range(-10, 10)}, + } } + rl.DisableCursor() + for !rl.WindowShouldClose() { - using rl + using rl player_update(&player, GetFrameTime()) BeginDrawing() BeginMode3D(player.camera) - BeginShaderMode(discard_shader) + BeginShaderMode(discard_shader) - ClearBackground(PAL[0]) - rlPushMatrix() - rlRotatef(45, 0, 1, 0) - DrawMesh(plane, mat, rl.Matrix(1)) - rlPopMatrix() + ClearBackground(PAL[0]) + rlPushMatrix() + rlRotatef(45, 0, 1, 0) + DrawMesh(plane, mat, rl.Matrix(1)) + rlPopMatrix() - player_draw(&player) - for &v in torches { - torch_draw(&v) - } - + player_draw(&player) + for &v in torches { + torch_draw(&v) + } - EndShaderMode() + EndShaderMode() EndMode3D() EndDrawing() } } -discard_shader : rl.Shader alpha_discard :: ` #version 330 diff --git a/src/player.odin b/src/player.odin index b565e5f..5dd8017 100644 --- a/src/player.odin +++ b/src/player.odin @@ -1,29 +1,26 @@ package main -import "core:math" import "core:fmt" +import "core:math" import rl "vendor:raylib" Player :: struct { using entity: Entity, cameraOffset: Vec3, - direction: f32, - yaw: f32, - bob_timer: f64, + direction: f32, + yaw: f32, + bob_timer: f64, bob_position: Vec3, - bob_factor: f32, - camera: rl.Camera3D, + bob_factor: f32, + camera: rl.Camera3D, } -player_sword_tex : rl.Texture -player_sword_mesh : rl.Mesh -player_sword_mat : rl.Material +player_sword_tex: rl.Texture +player_sword_mesh: rl.Mesh +player_sword_mat: rl.Material player_load_resources :: proc() { player_sword_tex = TEXTURES["sword.png"] // rl.LoadTexture("sword.png") - fmt.println(TEXTURES) - fmt.println(player_sword_tex) - rl.SetTextureFilter(player_sword_tex, rl.TextureFilter.POINT) player_sword_mesh = rl.GenMeshPlane(1.5, 3.6, 1, 1) player_sword_mat = rl.LoadMaterialDefault() player_sword_mat.shader = discard_shader @@ -32,21 +29,22 @@ player_load_resources :: proc() { player_unload_resources :: proc() { rl.UnloadMaterial(player_sword_mat) - //rl.UnloadTexture(player_sword_tex) rl.UnloadMesh(player_sword_mesh) } player_create :: proc() -> Player { - return Player { - size = {0.5, 2, 0.5}, - cameraOffset = {0, 1.8, 0}, - camera = rl.Camera3D{ - up = {0, 1, 0}, - fovy = 80, - target = {0, 0, 10}, - projection = rl.CameraProjection.PERSPECTIVE, - }, - } + return( + Player { + size = {0.5, 2, 0.5}, + cameraOffset = {0, 1.8, 0}, + camera = rl.Camera3D { + up = {0, 1, 0}, + fovy = 80, + target = {0, 0, 10}, + projection = rl.CameraProjection.PERSPECTIVE, + }, + } \ + ) } player_update :: proc(using player: ^Player, delta: f32) { @@ -57,8 +55,8 @@ player_update :: proc(using player: ^Player, delta: f32) { direction -= mouseDelta.x * 0.003 yaw -= mouseDelta.y * 0.003 - yaw = Clamp(yaw, -math.PI/2+0.001, math.PI/2-0.001) - + yaw = Clamp(yaw, -math.PI / 2 + 0.001, math.PI / 2 - 0.001) + dir_right := direction + math.PI / 2 forward := Vec3{math.sin(direction), 0, math.cos(direction)} right := Vec3{math.sin(dir_right), 0, math.cos(dir_right)} @@ -78,13 +76,11 @@ player_update :: proc(using player: ^Player, delta: f32) { velocity.xz = Vector3MoveTowards(velocity, motion * 100, 100 * delta).xz position += velocity * delta - - //bob_position = Vec3{math.sin_f32(f32(bob_timer * math.PI * 2 + math.PI/4)), math.sin_f32(f32(bob_timer*math.PI * 4)), 0} * bob_factor * 0.3 - bob_position = Vec3{0, math.sin_f32(f32(bob_timer*math.PI * 4)), 0} * bob_factor * 0.3 - bob_position = Vector3RotateByAxisAngle(bob_position, Vec3{0,1,0}, direction) - + // camera positioning, targetting and bobbing + bob_position = Vec3{0, math.sin_f32(f32(bob_timer * math.PI * 4)), 0} * bob_factor * 0.3 + bob_position = Vector3RotateByAxisAngle(bob_position, Vec3{0, 1, 0}, direction) camera.position = position + cameraOffset + bob_position - target := Vec3{ + target := Vec3 { math.sin(direction) * math.cos(yaw), math.sin(yaw), math.cos(direction) * math.cos(yaw), @@ -95,21 +91,18 @@ player_update :: proc(using player: ^Player, delta: f32) { player_draw :: proc(using player: ^Player) { using rl - rlPushMatrix() offset := position + cameraOffset + (bob_position / 2) rlTranslatef(offset.x, offset.y, offset.z) rlRotatef(math.to_degrees(player.direction), 0, 1, 0) rlRotatef(math.to_degrees(-player.yaw), 1, 0, 0) rlTranslatef(-1, -0.3, 1) - rlTranslatef(math.sin_f32(f32(bob_timer * math.PI * 2 + math.PI/4)) * bob_factor * 0.3, 0, 0) + rlTranslatef(math.sin_f32(f32(bob_timer * math.PI * 2 + math.PI / 4)) * bob_factor * 0.3, 0, 0) rlRotatef(-90, 1, 0, 0) rlRotatef(-90, 0, 0, 1) rlRotatef(210, 0, 1, 0) rlScalef(0.3, 0.3, 0.3) DrawMesh(player_sword_mesh, player_sword_mat, rl.Matrix(1)) - // DrawCube({0, 0, 0}, 1,1,1, PAL[4]) - // DrawCube({0, 0, 0}, 1,1,1, PAL[4]) rlPopMatrix() } diff --git a/src/resloader.odin b/src/resloader.odin index 8f886a0..e2a723c 100644 --- a/src/resloader.odin +++ b/src/resloader.odin @@ -1,18 +1,18 @@ package main -import rl "vendor:raylib" -import "core:slice" import "core:path/filepath" +import "core:slice" +import rl "vendor:raylib" Textures :: map[string]rl.Texture -tex_filepaths : rl.FilePathList +tex_filepaths: rl.FilePathList load_textures :: proc() -> Textures { - result : Textures = make(map[string]rl.Texture) + result: Textures = make(map[string]rl.Texture) tex_filepaths := rl.LoadDirectoryFiles("./assets") - for i : u32 = 0; i < tex_filepaths.count; i += 1 { + for i: u32 = 0; i < tex_filepaths.count; i += 1 { item := tex_filepaths.paths[i] name := filepath.base(string(item)) result[name] = rl.LoadTexture(item)