mini-refactoring

This commit is contained in:
Nefrace 2024-04-21 00:28:56 +03:00
parent cd412f8249
commit ef9caac601
3 changed files with 65 additions and 73 deletions

View File

@ -1,29 +1,25 @@
package main package main
import "core:math" import "core:math"
import rl "vendor:raylib" import "core:math/rand"
import rl "vendor:raylib"
Vec3 :: [3]f32 Vec3 :: [3]f32
Entity :: struct { Entity :: struct {
position : Vec3, position: Vec3,
velocity : Vec3, velocity: Vec3,
size : Vec3, size: Vec3,
} }
PAL_INT :: [6]u32 { PAL_INT :: [6]u32{0x272744ff, 0x494d7eff, 0x8b6d9cff, 0xc69fa5ff, 0xf2d3abff, 0xfbf5efff}
0x272744ff,
0x494d7eff,
0x8b6d9cff,
0xc69fa5ff,
0xf2d3abff,
0xfbf5efff,
}
PAL := [6]rl.Color{} PAL := [6]rl.Color{}
CAMERA : ^rl.Camera3D CAMERA: ^rl.Camera3D
TEXTURES : Textures TEXTURES: Textures
discard_shader: rl.Shader // See shader below. Required for drawing sprites
main :: proc() { main :: proc() {
for v, i in PAL_INT { for v, i in PAL_INT {
@ -37,57 +33,60 @@ main :: proc() {
defer unload_textures(&TEXTURES) defer unload_textures(&TEXTURES)
discard_shader = rl.LoadShaderFromMemory(nil, alpha_discard) discard_shader = rl.LoadShaderFromMemory(nil, alpha_discard)
// Floor checkerboard
img := rl.GenImageChecked(64, 64, 8, 8, PAL[1], PAL[2]) img := rl.GenImageChecked(64, 64, 8, 8, PAL[1], PAL[2])
defer rl.UnloadImage(img) defer rl.UnloadImage(img)
tex := rl.LoadTextureFromImage(img) tex := rl.LoadTextureFromImage(img)
defer rl.UnloadTexture(tex) defer rl.UnloadTexture(tex)
rl.SetTextureFilter(tex, rl.TextureFilter.POINT)
// Floor mesh
plane := rl.GenMeshPlane(32, 32, 16, 16) plane := rl.GenMeshPlane(32, 32, 16, 16)
defer rl.UnloadMesh(plane) defer rl.UnloadMesh(plane)
mat := rl.LoadMaterialDefault() mat := rl.LoadMaterialDefault()
rl.SetMaterialTexture(&mat, rl.MaterialMapIndex.ALBEDO, tex) rl.SetMaterialTexture(&mat, rl.MaterialMapIndex.ALBEDO, tex)
player_load_resources() player_load_resources()
defer player_unload_resources() defer player_unload_resources()
player := player_create() player := player_create()
CAMERA = &(player.camera)
rl.DisableCursor() CAMERA = &(player.camera)
torches := [10]Torch{} torches := [10]Torch{}
for &v, i in torches { 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() { for !rl.WindowShouldClose() {
using rl using rl
player_update(&player, GetFrameTime()) player_update(&player, GetFrameTime())
BeginDrawing() BeginDrawing()
BeginMode3D(player.camera) BeginMode3D(player.camera)
BeginShaderMode(discard_shader) BeginShaderMode(discard_shader)
ClearBackground(PAL[0]) ClearBackground(PAL[0])
rlPushMatrix() rlPushMatrix()
rlRotatef(45, 0, 1, 0) rlRotatef(45, 0, 1, 0)
DrawMesh(plane, mat, rl.Matrix(1)) DrawMesh(plane, mat, rl.Matrix(1))
rlPopMatrix() rlPopMatrix()
player_draw(&player) player_draw(&player)
for &v in torches { for &v in torches {
torch_draw(&v) torch_draw(&v)
} }
EndShaderMode() EndShaderMode()
EndMode3D() EndMode3D()
EndDrawing() EndDrawing()
} }
} }
discard_shader : rl.Shader
alpha_discard :: ` alpha_discard :: `
#version 330 #version 330

View File

@ -1,29 +1,26 @@
package main package main
import "core:math"
import "core:fmt" import "core:fmt"
import "core:math"
import rl "vendor:raylib" import rl "vendor:raylib"
Player :: struct { Player :: struct {
using entity: Entity, using entity: Entity,
cameraOffset: Vec3, cameraOffset: Vec3,
direction: f32, direction: f32,
yaw: f32, yaw: f32,
bob_timer: f64, bob_timer: f64,
bob_position: Vec3, bob_position: Vec3,
bob_factor: f32, bob_factor: f32,
camera: rl.Camera3D, camera: rl.Camera3D,
} }
player_sword_tex : rl.Texture player_sword_tex: rl.Texture
player_sword_mesh : rl.Mesh player_sword_mesh: rl.Mesh
player_sword_mat : rl.Material player_sword_mat: rl.Material
player_load_resources :: proc() { player_load_resources :: proc() {
player_sword_tex = TEXTURES["sword.png"] // rl.LoadTexture("sword.png") 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_mesh = rl.GenMeshPlane(1.5, 3.6, 1, 1)
player_sword_mat = rl.LoadMaterialDefault() player_sword_mat = rl.LoadMaterialDefault()
player_sword_mat.shader = discard_shader player_sword_mat.shader = discard_shader
@ -32,21 +29,22 @@ player_load_resources :: proc() {
player_unload_resources :: proc() { player_unload_resources :: proc() {
rl.UnloadMaterial(player_sword_mat) rl.UnloadMaterial(player_sword_mat)
//rl.UnloadTexture(player_sword_tex)
rl.UnloadMesh(player_sword_mesh) rl.UnloadMesh(player_sword_mesh)
} }
player_create :: proc() -> Player { player_create :: proc() -> Player {
return Player { return(
size = {0.5, 2, 0.5}, Player {
cameraOffset = {0, 1.8, 0}, size = {0.5, 2, 0.5},
camera = rl.Camera3D{ cameraOffset = {0, 1.8, 0},
up = {0, 1, 0}, camera = rl.Camera3D {
fovy = 80, up = {0, 1, 0},
target = {0, 0, 10}, fovy = 80,
projection = rl.CameraProjection.PERSPECTIVE, target = {0, 0, 10},
}, projection = rl.CameraProjection.PERSPECTIVE,
} },
} \
)
} }
player_update :: proc(using player: ^Player, delta: f32) { 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 direction -= mouseDelta.x * 0.003
yaw -= mouseDelta.y * 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 dir_right := direction + math.PI / 2
forward := Vec3{math.sin(direction), 0, math.cos(direction)} forward := Vec3{math.sin(direction), 0, math.cos(direction)}
right := Vec3{math.sin(dir_right), 0, math.cos(dir_right)} 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 velocity.xz = Vector3MoveTowards(velocity, motion * 100, 100 * delta).xz
position += velocity * delta position += velocity * delta
// camera positioning, targetting and bobbing
//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 = 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)
bob_position = Vector3RotateByAxisAngle(bob_position, Vec3{0,1,0}, direction)
camera.position = position + cameraOffset + bob_position camera.position = position + cameraOffset + bob_position
target := Vec3{ target := Vec3 {
math.sin(direction) * math.cos(yaw), math.sin(direction) * math.cos(yaw),
math.sin(yaw), math.sin(yaw),
math.cos(direction) * math.cos(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) { player_draw :: proc(using player: ^Player) {
using rl using rl
rlPushMatrix() rlPushMatrix()
offset := position + cameraOffset + (bob_position / 2) offset := position + cameraOffset + (bob_position / 2)
rlTranslatef(offset.x, offset.y, offset.z) rlTranslatef(offset.x, offset.y, offset.z)
rlRotatef(math.to_degrees(player.direction), 0, 1, 0) rlRotatef(math.to_degrees(player.direction), 0, 1, 0)
rlRotatef(math.to_degrees(-player.yaw), 1, 0, 0) rlRotatef(math.to_degrees(-player.yaw), 1, 0, 0)
rlTranslatef(-1, -0.3, 1) 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, 1, 0, 0)
rlRotatef(-90, 0, 0, 1) rlRotatef(-90, 0, 0, 1)
rlRotatef(210, 0, 1, 0) rlRotatef(210, 0, 1, 0)
rlScalef(0.3, 0.3, 0.3) rlScalef(0.3, 0.3, 0.3)
DrawMesh(player_sword_mesh, player_sword_mat, rl.Matrix(1)) 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() rlPopMatrix()
} }

View File

@ -1,18 +1,18 @@
package main package main
import rl "vendor:raylib"
import "core:slice"
import "core:path/filepath" import "core:path/filepath"
import "core:slice"
import rl "vendor:raylib"
Textures :: map[string]rl.Texture Textures :: map[string]rl.Texture
tex_filepaths : rl.FilePathList tex_filepaths: rl.FilePathList
load_textures :: proc() -> Textures { load_textures :: proc() -> Textures {
result : Textures = make(map[string]rl.Texture) result: Textures = make(map[string]rl.Texture)
tex_filepaths := rl.LoadDirectoryFiles("./assets") 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] item := tex_filepaths.paths[i]
name := filepath.base(string(item)) name := filepath.base(string(item))
result[name] = rl.LoadTexture(item) result[name] = rl.LoadTexture(item)