Compare commits
45 Commits
249109f117
...
debug/worl
| Author | SHA1 | Date | |
|---|---|---|---|
|
d9957eead1
|
|||
|
c4cf8f532b
|
|||
|
8a360df98a
|
|||
|
3a84947750
|
|||
|
3e527f473b
|
|||
|
ab8301f475
|
|||
|
08cbfaeb71
|
|||
|
2f52bb3097
|
|||
|
a9612b2d57
|
|||
| 139b14ed71 | |||
|
c721839dfa
|
|||
| 24e9b472c3 | |||
| b09bdc6209 | |||
|
64a84e3a8d
|
|||
|
319bb441ed
|
|||
|
3eb8683ce3
|
|||
|
eb3c81cd04
|
|||
|
635e7b1c1e
|
|||
|
b8f47b8f61
|
|||
|
e20e537e97
|
|||
|
98095c907f
|
|||
|
8dc0ac1d3e
|
|||
|
222575b318
|
|||
| 9a46a802e0 | |||
| 628a6c5ade | |||
| c8dfcd857e | |||
|
394366480b
|
|||
|
8f148b6894
|
|||
|
7306c8fba4
|
|||
|
5cabf845b6
|
|||
|
1d70f3ae20
|
|||
|
5d48ed9c19
|
|||
|
1f97d81133
|
|||
|
1757fc4b96
|
|||
|
3b8c50a361
|
|||
|
2c67896cf2
|
|||
|
175f4da59b
|
|||
|
50efa6d8c0
|
|||
|
ab96fa3eeb
|
|||
|
55c1adba40
|
|||
|
dd316664f6
|
|||
|
e4c1bc0a1c
|
|||
|
187359f747
|
|||
|
b1a805cea8
|
|||
|
7d55b16c8e
|
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
build/
|
||||
code/compile_commands.json
|
||||
code/.cache
|
||||
.vscode
|
||||
|
||||
BIN
assets/barrel.png
Normal file
|
After Width: | Height: | Size: 590 B |
BIN
assets/can.png
Normal file
|
After Width: | Height: | Size: 168 B |
BIN
assets/candle.png
Normal file
|
After Width: | Height: | Size: 176 B |
BIN
assets/clock.png
Normal file
|
After Width: | Height: | Size: 584 B |
BIN
assets/house.png
Normal file
|
After Width: | Height: | Size: 3.3 KiB |
BIN
assets/house_int.png
Normal file
|
After Width: | Height: | Size: 3.7 KiB |
BIN
assets/hunter.png
Normal file
|
After Width: | Height: | Size: 730 B |
BIN
assets/log_pile.png
Normal file
|
After Width: | Height: | Size: 519 B |
BIN
assets/nightstand.png
Normal file
|
After Width: | Height: | Size: 244 B |
BIN
assets/npc_00.png
Normal file
|
After Width: | Height: | Size: 801 B |
BIN
assets/npc_front_base_black.png
Normal file
|
After Width: | Height: | Size: 732 B |
BIN
assets/npc_front_base_white.png
Normal file
|
After Width: | Height: | Size: 737 B |
BIN
assets/npc_front_eyes_blue.png
Normal file
|
After Width: | Height: | Size: 232 B |
BIN
assets/npc_front_eyes_brown.png
Normal file
|
After Width: | Height: | Size: 207 B |
BIN
assets/npc_front_eyes_closed.png
Normal file
|
After Width: | Height: | Size: 127 B |
BIN
assets/npc_front_eyes_green.png
Normal file
|
After Width: | Height: | Size: 217 B |
BIN
assets/npc_front_face_glasses.png
Normal file
|
After Width: | Height: | Size: 157 B |
BIN
assets/npc_front_face_moustache.png
Normal file
|
After Width: | Height: | Size: 191 B |
BIN
assets/npc_front_face_redMask.png
Normal file
|
After Width: | Height: | Size: 227 B |
BIN
assets/npc_front_face_whiteMask.png
Normal file
|
After Width: | Height: | Size: 186 B |
BIN
assets/npc_front_hair_blonde.png
Normal file
|
After Width: | Height: | Size: 393 B |
BIN
assets/npc_front_hair_brown.png
Normal file
|
After Width: | Height: | Size: 334 B |
BIN
assets/npc_front_hair_grey.png
Normal file
|
After Width: | Height: | Size: 191 B |
BIN
assets/npc_front_hat_brown.png
Normal file
|
After Width: | Height: | Size: 326 B |
BIN
assets/npc_front_hat_white.png
Normal file
|
After Width: | Height: | Size: 300 B |
BIN
assets/npc_front_shirt_brown.png
Normal file
|
After Width: | Height: | Size: 528 B |
BIN
assets/npc_front_shirt_white.png
Normal file
|
After Width: | Height: | Size: 479 B |
BIN
assets/npc_front_shoes_brown.png
Normal file
|
After Width: | Height: | Size: 273 B |
BIN
assets/npc_front_trousers_blue.png
Normal file
|
After Width: | Height: | Size: 361 B |
BIN
assets/npc_front_trousers_white.png
Normal file
|
After Width: | Height: | Size: 354 B |
BIN
assets/oath_corner_inner.png
Normal file
|
After Width: | Height: | Size: 409 B |
BIN
assets/outside_ambience.wav
Normal file
BIN
assets/path_corner.png
Normal file
|
After Width: | Height: | Size: 520 B |
BIN
assets/path_middle.png
Normal file
|
After Width: | Height: | Size: 483 B |
BIN
assets/path_middle_edge.png
Normal file
|
After Width: | Height: | Size: 453 B |
BIN
assets/pool_table.png
Normal file
|
After Width: | Height: | Size: 846 B |
BIN
assets/rug0.png
Normal file
|
After Width: | Height: | Size: 634 B |
BIN
assets/rug1.png
Normal file
|
After Width: | Height: | Size: 1.1 KiB |
BIN
assets/saloon_ext.png
Normal file
|
After Width: | Height: | Size: 4.0 KiB |
BIN
assets/saloon_int.png
Normal file
|
After Width: | Height: | Size: 8.2 KiB |
BIN
assets/skull.png
Normal file
|
After Width: | Height: | Size: 555 B |
BIN
assets/table.png
Normal file
|
After Width: | Height: | Size: 1012 B |
BIN
assets/tile_detail_0.png
Normal file
|
After Width: | Height: | Size: 208 B |
BIN
assets/tile_detail_1.png
Normal file
|
After Width: | Height: | Size: 329 B |
BIN
assets/tile_detail_2.png
Normal file
|
After Width: | Height: | Size: 358 B |
BIN
assets/tile_detail_3.png
Normal file
|
After Width: | Height: | Size: 351 B |
BIN
assets/tile_detail_4.png
Normal file
|
After Width: | Height: | Size: 223 B |
BIN
assets/tile_detail_5.png
Normal file
|
After Width: | Height: | Size: 368 B |
BIN
assets/tile_detail_6.png
Normal file
|
After Width: | Height: | Size: 317 B |
BIN
assets/tile_dirt_0.png
Normal file
|
After Width: | Height: | Size: 462 B |
BIN
assets/tile_dirt_1.png
Normal file
|
After Width: | Height: | Size: 494 B |
@@ -1,7 +1,7 @@
|
||||
#if !defined(LD_CORE_ARENA_H_)
|
||||
#define LD_CORE_ARENA_H_
|
||||
|
||||
#define AlignUp(x, a) (((x) + ~((a) - 1)) & ~((a) - 1))
|
||||
#define AlignUp(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
|
||||
#define AlignDown(x, a) (((x)) & ~((a) - 1))
|
||||
|
||||
#define KB(x) ((U64) (x) << 10)
|
||||
@@ -64,8 +64,8 @@ function void *_M_ArenaPush(M_Arena *arena, U64 esize, M_ArenaPushOpts *opts)
|
||||
function void *_M_ArenaPushCopy(M_Arena *arena, void *src, U64 esize, M_ArenaPushOpts *opts);
|
||||
|
||||
#define M_ArenaAlloc(limit, ...) _M_ArenaAlloc(limit, &(M_ArenaOpts) { .initial = MB(1), .increment = KB(64), ##__VA_ARGS__ })
|
||||
#define M_ArenaPush(arena, T, ...) (T *) _M_ArenaPush(arena, sizeof(T), &(M_ArenaOpts) { .count = 1, .align = Alignof(T), ##__VA_ARGS__ })
|
||||
#define M_ArenaPushCopy(arena, T, src, ...) (T *) _M_ArenaPush(arena, src, sizeof(T), &(M_ArenaOpts) { .count = 1, .align = Alignof(T), ##__VA_ARGS__ })
|
||||
#define M_ArenaPush(arena, T, ...) (T *) _M_ArenaPush(arena, sizeof(T), &(M_ArenaPushOpts) { .count = 1, .align = Alignof(T), ##__VA_ARGS__ })
|
||||
#define M_ArenaPushCopy(arena, T, src, ...) (T *) _M_ArenaPush(arena, src, sizeof(T), &(M_ArenaPushOpts) { .count = 1, .align = Alignof(T), ##__VA_ARGS__ })
|
||||
|
||||
function void M_ArenaReset(M_Arena *arena);
|
||||
function void M_ArenaRelease(M_Arena *arena);
|
||||
|
||||
@@ -1 +1,3 @@
|
||||
#include "impl/arena.c"
|
||||
#include "impl/string.c"
|
||||
#include "impl/math.c"
|
||||
|
||||
@@ -5,5 +5,7 @@
|
||||
#include "platform.h"
|
||||
#include "macros.h"
|
||||
#include "arena.h"
|
||||
#include "string.h"
|
||||
#include "math.h"
|
||||
|
||||
#endif // LD_CORE_CORE_H_
|
||||
|
||||
233
code/core/impl/math.c
Normal file
@@ -0,0 +1,233 @@
|
||||
#include <math.h>
|
||||
V2f V2F(F32 x, F32 y) {
|
||||
V2f result = { x, y };
|
||||
return result;
|
||||
}
|
||||
|
||||
function V2f V2f_Scale(V2f x, F32 scale) {
|
||||
return V2F(x.x * scale, x.y * scale);
|
||||
}
|
||||
|
||||
V2f NormaliseV2F(V2f x) {
|
||||
F32 magnitude = sqrtf((x.x * x.x) + (x.y * x.y));
|
||||
if(magnitude > 0.0){
|
||||
F32 inverse = 1.0f/magnitude;
|
||||
return V2F(x.x*inverse, x.y*inverse);
|
||||
}
|
||||
return x;
|
||||
}
|
||||
|
||||
V3f V3F(F32 x, F32 y, F32 z) {
|
||||
V3f result = { x, y, z };
|
||||
return result;
|
||||
}
|
||||
|
||||
V4f V4F(F32 x, F32 y, F32 z, F32 w) {
|
||||
V4f result = { x, y, z, w };
|
||||
return result;
|
||||
}
|
||||
|
||||
R2f R2F(V2f min, V2f max) {
|
||||
R2f result = { min, max };
|
||||
return result;
|
||||
}
|
||||
|
||||
V3f V3f_Neg(V3f x) {
|
||||
V3f result = { -x.x, -x.y, -x.z };
|
||||
return result;
|
||||
}
|
||||
|
||||
V3f V3f_Scale(V3f x, F32 s) {
|
||||
V3f result = { s * x.x, s * x.y, s * x.z };
|
||||
return result;
|
||||
}
|
||||
|
||||
V3f V3f_Sub(V3f a, V3f b) {
|
||||
V3f result = { a.x - b.x, a.y - b.y, a.z - b.z };
|
||||
return result;
|
||||
}
|
||||
|
||||
F32 V3f_Dot(V3f a, V3f b) {
|
||||
F32 result = (a.x * b.x) + (a.y * b.y) + (a.z * b.z);
|
||||
return result;
|
||||
}
|
||||
|
||||
F32 V4f_Dot(V4f a, V4f b) {
|
||||
F32 result = (a.x * b.x) + (a.y * b.y) + (a.z * b.z) + (a.w * b.w);
|
||||
return result;
|
||||
}
|
||||
|
||||
Mat4x4F M4x4F_Rows(V3f x, V3f y, V3f z) {
|
||||
Mat4x4F result = {
|
||||
x.x, x.y, x.z, 0,
|
||||
y.x, y.y, y.z, 0,
|
||||
z.x, z.y, z.z, 0,
|
||||
0, 0, 0, 1
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Mat4x4F M4x4F_Columns(V3f x, V3f y, V3f z) {
|
||||
Mat4x4F result = {
|
||||
x.x, y.x, z.x, 0,
|
||||
x.y, y.y, z.y, 0,
|
||||
x.z, y.z, z.z, 0,
|
||||
0, 0, 0, 1
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Mat4x4F M4x4F_Mul(Mat4x4F a, Mat4x4F b) {
|
||||
Mat4x4F result;
|
||||
|
||||
for (U32 r = 0; r < 4; ++r) {
|
||||
for (U32 c = 0; c < 4; ++c) {
|
||||
result.m[r][c] =
|
||||
(a.m[r][0] * b.m[0][c]) + (a.m[r][1] * b.m[1][c]) +
|
||||
(a.m[r][2] * b.m[2][c]) + (a.m[r][3] * b.m[3][c]);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
V4f M4x4F_VMul4(Mat4x4F m, V4f v) {
|
||||
V4f result;
|
||||
result.x = V4f_Dot(m.r[0], v);
|
||||
result.y = V4f_Dot(m.r[1], v);
|
||||
result.z = V4f_Dot(m.r[2], v);
|
||||
result.w = V4f_Dot(m.r[3], v);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
V3f M4x4F_VMul3(Mat4x4F m, V3f v) {
|
||||
V4f tx;
|
||||
tx.xyz = v;
|
||||
tx.w = 1.0f;
|
||||
|
||||
V3f result = M4x4F_VMul4(m, tx).xyz;
|
||||
return result;
|
||||
}
|
||||
|
||||
Mat4x4FInv M4x4F_Perspective(F32 fov, F32 aspect, F32 nearp, F32 farp) {
|
||||
F32 focal_length = 1.0f / tanf(0.5f * (PI_F32 * (fov / 360.0f)));
|
||||
|
||||
F32 a = focal_length;
|
||||
F32 b = focal_length * aspect;
|
||||
|
||||
F32 c = -(nearp + farp) / (farp - nearp);
|
||||
F32 d = -(2.0f * nearp * farp) / (farp - nearp);
|
||||
|
||||
Mat4x4FInv result = {
|
||||
// fwd
|
||||
{
|
||||
a, 0, 0, 0,
|
||||
0, b, 0, 0,
|
||||
0, 0, c, d,
|
||||
0, 0, -1, 0
|
||||
},
|
||||
// inv
|
||||
{
|
||||
(1 / a), 0, 0, 0,
|
||||
0, (1 / b), 0, 0,
|
||||
0, 0, 0, -1,
|
||||
0, 0, (1/ d), (c / d)
|
||||
}
|
||||
};
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Mat4x4FInv M4x4F_CameraView(V3f x, V3f y, V3f z, V3f p) {
|
||||
Mat4x4FInv result;
|
||||
|
||||
// Construct orthonomal basis from axes
|
||||
//
|
||||
result.fwd = M4x4F_Rows(x, y, z);
|
||||
V3f txp = V3f_Neg(M4x4F_VMul3(result.fwd, p));
|
||||
|
||||
// Translate by txp
|
||||
//
|
||||
result.fwd.r[0].w += txp.x;
|
||||
result.fwd.r[1].w += txp.y;
|
||||
result.fwd.r[2].w += txp.z;
|
||||
|
||||
// Calculate inverse axes
|
||||
//
|
||||
V3f ix = V3f_Scale(x, 1.0f / V3f_Dot(x, x));
|
||||
V3f iy = V3f_Scale(y, 1.0f / V3f_Dot(y, y));
|
||||
V3f iz = V3f_Scale(z, 1.0f / V3f_Dot(z, z));
|
||||
|
||||
// Calculate inverse position
|
||||
//
|
||||
V3f ip;
|
||||
ip.x = (txp.x * ix.x) + (txp.y * iy.x) + (txp.z * iz.x);
|
||||
ip.y = (txp.x * ix.y) + (txp.y * iy.y) + (txp.z * iz.y);
|
||||
ip.z = (txp.x * ix.z) + (txp.y * iy.z) + (txp.z * iz.z);
|
||||
|
||||
result.inv = M4x4F_Columns(ix, iy, iz);
|
||||
|
||||
// Translate by ip
|
||||
//
|
||||
result.inv.r[0].w -= ip.x;
|
||||
result.inv.r[1].w -= ip.y;
|
||||
result.inv.r[2].w -= ip.z;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Random
|
||||
|
||||
Random Random_Seed(U64 seed) {
|
||||
Random result = { seed };
|
||||
return result;
|
||||
}
|
||||
|
||||
U64 Random_Next(Random *rnd) {
|
||||
U64 result = rnd->state;
|
||||
|
||||
result ^= (result << 13);
|
||||
result ^= (result >> 7);
|
||||
result ^= (result << 17);
|
||||
|
||||
rnd->state = result;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
F32 Random_F32(Random *rnd, F32 min, F32 max) {
|
||||
F32 result = min + (Random_Unilateral(rnd) * (max - min));
|
||||
return result;
|
||||
}
|
||||
|
||||
F64 Random_F64(Random *rnd, F64 min, F64 max) {
|
||||
F64 result = min + (Random_Unilateral(rnd) * (max - min));
|
||||
return result;
|
||||
}
|
||||
|
||||
U32 Random_U32(Random *rnd, U32 min, U32 max) {
|
||||
U32 result = min + (U32) (Random_Unilateral(rnd) * (max - min));
|
||||
return result;
|
||||
}
|
||||
|
||||
F32 Random_Unilateral(Random *rnd) {
|
||||
F32 result = Random_Next(rnd) / (F32) U64_MAX;
|
||||
return result;
|
||||
}
|
||||
|
||||
F32 Random_Bilateral(Random *rnd) {
|
||||
F32 result = -1.0f + (2.0f * Random_Unilateral(rnd));
|
||||
return result;
|
||||
}
|
||||
|
||||
V2f V2f_Clip(V2f screen_xy, V2f screen_size) {
|
||||
V2f result;
|
||||
result.x = ((screen_xy.x / screen_size.w) * 2.0f) - 1.0f;
|
||||
result.y = ((screen_xy.y / screen_size.h) * 2.0f) - 1.0f;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
164
code/core/impl/string.c
Normal file
@@ -0,0 +1,164 @@
|
||||
Str8 Str8_Wrap(S64 count, U8 *data) {
|
||||
Str8 result;
|
||||
result.data = data;
|
||||
result.count = count;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Str8 Str8_WrapRange(U8 *start, U8 *end) {
|
||||
Str8 result;
|
||||
result.data = start;
|
||||
result.count = cast(S64) (end - start);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
internal S64 Str8_CountZ(U8 *data) {
|
||||
S64 result = 0;
|
||||
while (data[result] != 0) { result += 1; }
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Str8 Str8_WrapZ(U8 *data) {
|
||||
Str8 result;
|
||||
result.data = data;
|
||||
result.count = Str8_CountZ(data);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#define FNV_OFFSET_BIAS ((U64) 0xCBF29CE484222325)
|
||||
#define FNV_PRIME ((U64) 0x100000001B3)
|
||||
|
||||
U64 Str8_Hash(Str8 v) {
|
||||
U64 result = FNV_OFFSET_BIAS;
|
||||
|
||||
for (S64 it = 0; it < v.count; ++it) {
|
||||
result ^= v.data[it];
|
||||
result *= FNV_PRIME;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Str8 Str8_Copy(M_Arena *arena, Str8 s) {
|
||||
Str8 result;
|
||||
result.count = s.count;
|
||||
result.data = M_ArenaPush(arena, U8, .count = s.count + 1);
|
||||
|
||||
M_CopySize(result.data, s.data, s.count);
|
||||
return result;
|
||||
}
|
||||
|
||||
Str8 Str8_Format(M_Arena *arena, const char *format, ...) {
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
|
||||
Str8 result = Str8_FormatArgs(arena, format, args);
|
||||
va_end(args);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
internal S64 Str8_ProcessFormat(Str8 out, const char *format, va_list args) {
|
||||
S64 result = vsnprintf((char *) out.data, (int) out.count, format, args);
|
||||
return result;
|
||||
}
|
||||
|
||||
Str8 Str8_FormatArgs(M_Arena *arena, const char *format, va_list args) {
|
||||
Str8 result;
|
||||
|
||||
va_list copy;
|
||||
va_copy(copy, args);
|
||||
|
||||
U64 offset = M_ArenaOffset(arena);
|
||||
|
||||
result.count = KB(1);
|
||||
result.data = M_ArenaPush(arena, U8, .count = result.count);
|
||||
|
||||
S64 needed = Str8_ProcessFormat(result, format, args);
|
||||
if (needed >= result.count) {
|
||||
M_ArenaPop(arena, offset);
|
||||
|
||||
result.count = needed;
|
||||
result.data = M_ArenaPush(arena, U8, .count = result.count + 1);
|
||||
|
||||
Str8_ProcessFormat(result, format, copy);
|
||||
}
|
||||
else {
|
||||
U64 size = result.count - needed - 1;
|
||||
M_ArenaPopSize(arena, size);
|
||||
|
||||
result.count = needed;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
B32 Str8_Equal(Str8 a, Str8 b, Str8_EqualFlags flags) {
|
||||
B32 result = (a.count == b.count);
|
||||
|
||||
if (result) {
|
||||
B32 ignore_case = (flags & STR8_EQUAL_IGNORE_CASE) != 0;
|
||||
|
||||
for (S64 it = 0; it < a.count; ++it) {
|
||||
U8 ac = ignore_case ? Chr_ToLowercase(a.data[it]) : a.data[it];
|
||||
U8 bc = ignore_case ? Chr_ToLowercase(b.data[it]) : b.data[it];
|
||||
|
||||
if (ac != bc) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Str8 Str8_Prefix(Str8 s, S64 count) {
|
||||
Str8 result;
|
||||
result.data = s.data;
|
||||
result.count = Min(s.count, count);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Str8 Str8_Suffix(Str8 s, S64 count) {
|
||||
Str8 result;
|
||||
result.count = Min(s.count, count);
|
||||
result.data = s.data + (s.count - result.count);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Str8 Str8_RemoveAfterLast(Str8 s, U8 c) {
|
||||
Str8 result;
|
||||
result.data = s.data;
|
||||
result.count = s.count;
|
||||
|
||||
for (S64 it = s.count - 1; it >= 0; --it) {
|
||||
if (result.data[it] == c) {
|
||||
result.count = it;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
B32 Str8_EndsWith(Str8 s, Str8 suffix) {
|
||||
B32 result = Str8_Equal(Str8_Suffix(s, suffix.count), suffix, 0);
|
||||
return result;
|
||||
}
|
||||
|
||||
U8 Chr_ToUppercase(U8 c) {
|
||||
U8 result = (c >= 'a' && c <= 'z') ? (c - ('a' - 'A')) : c;
|
||||
return result;
|
||||
}
|
||||
|
||||
U8 Chr_ToLowercase(U8 c) {
|
||||
U8 result = (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c;
|
||||
return result;
|
||||
}
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <assert.h>
|
||||
|
||||
#define Assert(exp) assert(exp)
|
||||
#define StaticAssert(exp) static_assert(exp, #exp)
|
||||
|
||||
#define ArraySize(x) (sizeof(x) / sizeof((x)[0]))
|
||||
#define Min(a, b) ((a) < (b) ? (a) : (b))
|
||||
@@ -34,6 +35,13 @@
|
||||
#define SLL_PushN(h, n, next) ((n)->next = (h), (h) = (n))
|
||||
#define SLL_PopN(h, next) (((h) != 0) ? (h) = (h)->next : 0)
|
||||
|
||||
#define SLL_Enqueue(h, t, n) SLL_EnqueueN(h, t, n, next)
|
||||
#define SLL_EnqueueFront(h, t, n) SLL_EnqueueFrontN(h, t, n, next)
|
||||
#define SLL_Dequeue(h, t) SLL_DequeueN(h, t, next)
|
||||
|
||||
#define SLL_Push(h, n) SLL_PushN(h, n, next)
|
||||
#define SLL_Pop(h) (((h) != 0) ? (h) = (h)->next : 0)
|
||||
|
||||
#define function static
|
||||
#define internal static
|
||||
#define global_var static
|
||||
@@ -45,14 +53,4 @@
|
||||
#define thread_var __thread
|
||||
#endif
|
||||
|
||||
#define S(x) Str8_Wrap(sizeof(x) - sizeof(*(x)), (U8 *) (x))
|
||||
Str8 Str8_Wrap(S64 count, U8 *data) {
|
||||
Str8 result;
|
||||
result.data = data;
|
||||
result.count = count;
|
||||
|
||||
return result;
|
||||
}
|
||||
#define Sv(x) (int) (x).count, (x).data
|
||||
|
||||
#endif // LD_CORE_MACROS_H_
|
||||
|
||||
152
code/core/math.h
Normal file
@@ -0,0 +1,152 @@
|
||||
#if !defined(LD_CORE_MATH_H_)
|
||||
#define LD_CORE_MATH_H_
|
||||
|
||||
#define PI_F32 (3.14159265358979323846264338f)
|
||||
#define TAU_F32 (2.0f * PI_F32)
|
||||
|
||||
#define Abs(x) (((x) < 0 ? -(x) : (x)))
|
||||
|
||||
typedef struct Random Random;
|
||||
struct Random {
|
||||
U64 state;
|
||||
};
|
||||
|
||||
typedef union V2f V2f;
|
||||
union V2f {
|
||||
struct {
|
||||
F32 x, y;
|
||||
};
|
||||
|
||||
struct {
|
||||
F32 u, v;
|
||||
};
|
||||
|
||||
struct {
|
||||
F32 w, h;
|
||||
};
|
||||
|
||||
F32 e[2];
|
||||
};
|
||||
|
||||
typedef union V2i V2i;
|
||||
union V2i {
|
||||
struct {
|
||||
U32 x, y;
|
||||
};
|
||||
|
||||
struct {
|
||||
U32 w, h;
|
||||
};
|
||||
|
||||
U32 e[2];
|
||||
};
|
||||
|
||||
typedef union V3f V3f;
|
||||
union V3f {
|
||||
struct {
|
||||
F32 x, y, z;
|
||||
};
|
||||
|
||||
struct {
|
||||
F32 r, g, b;
|
||||
};
|
||||
|
||||
struct {
|
||||
F32 w, h, d;
|
||||
};
|
||||
|
||||
struct {
|
||||
V2f xy;
|
||||
F32 _z;
|
||||
};
|
||||
|
||||
F32 e[3];
|
||||
};
|
||||
|
||||
typedef union V4f V4f;
|
||||
union V4f {
|
||||
struct {
|
||||
F32 x, y, z, w;
|
||||
};
|
||||
|
||||
struct {
|
||||
F32 r, g, b, a;
|
||||
};
|
||||
|
||||
struct {
|
||||
V3f xyz;
|
||||
F32 _w;
|
||||
};
|
||||
|
||||
F32 e[4];
|
||||
};
|
||||
|
||||
typedef union Mat4x4F Mat4x4F;
|
||||
union Mat4x4F {
|
||||
F32 m[4][4];
|
||||
F32 e[16];
|
||||
V4f r[4];
|
||||
};
|
||||
|
||||
typedef struct Mat4x4FInv Mat4x4FInv;
|
||||
struct Mat4x4FInv {
|
||||
Mat4x4F fwd;
|
||||
Mat4x4F inv;
|
||||
};
|
||||
|
||||
typedef struct R2f R2f;
|
||||
struct R2f {
|
||||
V2f min;
|
||||
V2f max;
|
||||
};
|
||||
|
||||
typedef struct R3f R3f;
|
||||
struct R3f {
|
||||
V3f min;
|
||||
V3f max;
|
||||
};
|
||||
|
||||
function V2f V2F(F32 x, F32 y);
|
||||
function V3f V3F(F32 x, F32 y, F32 z);
|
||||
function V4f V4F(F32 x, F32 y, F32 z, F32 w);
|
||||
|
||||
function R2f R2F(V2f min, V2f max);
|
||||
|
||||
function V2f V2f_Scale(V2f x, F32 s);
|
||||
|
||||
function V3f V3f_Neg(V3f x);
|
||||
function V3f V3f_Scale(V3f x, F32 s);
|
||||
function V3f V3f_Sub(V3f a, V3f b);
|
||||
|
||||
function F32 V3f_Dot(V3f a, V3f b);
|
||||
function F32 V4f_Dot(V4f a, V4f b);
|
||||
|
||||
function Mat4x4F M4x4F_Rows(V3f x, V3f y, V3f z);
|
||||
function Mat4x4F M4x4F_Columns(V3f x, V3f y, V3f z);
|
||||
|
||||
function V4f M4x4F_VMul4(Mat4x4F m, V4f v);
|
||||
function V3f M4x4F_VMul3(Mat4x4F m, V3f v);
|
||||
|
||||
function Mat4x4FInv M4x4F_Perspective(F32 fov, F32 aspect, F32 nearp, F32 farp);
|
||||
function Mat4x4FInv M4x4F_CameraView(V3f x, V3f y, V3f z, V3f p);
|
||||
|
||||
function V2f NormaliseV2F(V2f x);
|
||||
function V2f V2f_Scale(V2f x, F32 scale);
|
||||
|
||||
// Random
|
||||
|
||||
function Random Random_Seed(U64 seed);
|
||||
|
||||
function U64 Random_Next(Random *rnd);
|
||||
|
||||
function F32 Random_F32(Random *rnd, F32 min, F32 max);
|
||||
function F64 Random_F64(Random *rnd, F64 min, F64 max);
|
||||
|
||||
function U32 Random_U32(Random *rnd, U32 min, U32 max);
|
||||
|
||||
function F32 Random_Unilateral(Random *rnd);
|
||||
function F32 Random_Bilateral(Random *rnd);
|
||||
|
||||
V2f V2f_Clip(V2f screen_xy, V2f screen_size);
|
||||
|
||||
#endif // LD_CORE_MATH_H_
|
||||
@@ -68,11 +68,17 @@
|
||||
#if OS_WINDOWS
|
||||
#define WIN32_LEAN_AND_MEAN 1
|
||||
#include <windows.h>
|
||||
#include <shlobj_core.h>
|
||||
#pragma warning(disable : 4201)
|
||||
#elif OS_LINUX
|
||||
#include <sys/types.h>
|
||||
#include <dlfcn.h>
|
||||
#include <sys/mman.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <errno.h>
|
||||
#include <dirent.h>
|
||||
#endif
|
||||
|
||||
#endif // LD_CORE_PLATFORM_H_
|
||||
|
||||
38
code/core/string.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#if !defined(LD_CORE_STRING_H_)
|
||||
#define LD_CORE_STRING_H_
|
||||
|
||||
#define S(x) Str8_Wrap(sizeof(x) - sizeof(*(x)), (U8 *) (x))
|
||||
#define Sz(x) Str8_WrapZ((U8 *) (x))
|
||||
|
||||
#define Sv(x) (int) (x).count, (x).data
|
||||
#define Sf(arena, fmt, ...) Str8_Format(arena, fmt, ##__VA_ARGS__)
|
||||
|
||||
function Str8 Str8_Wrap(S64 count, U8 *data);
|
||||
function Str8 Str8_WrapRange(U8 *start, U8 *end);
|
||||
function Str8 Str8_WrapZ(U8 *data);
|
||||
|
||||
function U64 Str8_Hash(Str8 v);
|
||||
|
||||
function Str8 Str8_Copy(M_Arena *arena, Str8 s);
|
||||
|
||||
function Str8 Str8_Format(M_Arena *arena, const char *format, ...);
|
||||
function Str8 Str8_FormatArgs(M_Arena *arena, const char *format, va_list args);
|
||||
|
||||
typedef U32 Str8_EqualFlags;
|
||||
enum {
|
||||
STR8_EQUAL_IGNORE_CASE = (1 << 0)
|
||||
};
|
||||
|
||||
function B32 Str8_Equal(Str8 a, Str8 b, Str8_EqualFlags flags);
|
||||
|
||||
function Str8 Str8_Prefix(Str8 s, S64 count);
|
||||
function Str8 Str8_Suffix(Str8 s, S64 count);
|
||||
|
||||
function Str8 Str8_RemoveAfterLast(Str8 s, U8 c);
|
||||
|
||||
function B32 Str8_EndsWith(Str8 s, Str8 suffix);
|
||||
|
||||
function U8 Chr_ToUppercase(U8 c);
|
||||
function U8 Chr_ToLowercase(U8 c);
|
||||
|
||||
#endif // LD_CORE_STRING_H_
|
||||
@@ -29,16 +29,12 @@ struct Str8 {
|
||||
U8 *data;
|
||||
};
|
||||
|
||||
typedef struct V2f V2f;
|
||||
struct V2f {
|
||||
F32 x;
|
||||
F32 y;
|
||||
};
|
||||
#define U8_MAX ((U8) -1)
|
||||
#define U16_MAX ((U16) -1)
|
||||
#define U32_MAX ((U32) -1)
|
||||
#define U64_MAX ((U64) -1)
|
||||
|
||||
typedef struct V2i V2i;
|
||||
struct V2i {
|
||||
U32 x;
|
||||
U32 y;
|
||||
};
|
||||
#define F32_MAX ((F32) 3.40282346638528859811704183484516925e+038F)
|
||||
#define F64_MAX ((F64) 1.79769313486231570814527423731704357e+308L)
|
||||
|
||||
#endif // LD_CORE_TYPES_H_
|
||||
|
||||
376
code/draw/core.c
Normal file
@@ -0,0 +1,376 @@
|
||||
|
||||
U32 D_ImageHandle(D_Context *draw, Str8 name) {
|
||||
U32 result = 0;
|
||||
|
||||
U64 hash = Str8_Hash(name);
|
||||
U64 index = hash & (D_ASSET_HASH_COUNT - 1);
|
||||
|
||||
D_AssetHash *lookup = draw->lookup[index];
|
||||
while (lookup != 0) {
|
||||
if (lookup->hash == hash && Str8_Equal(lookup->value, name, 0)) {
|
||||
result = lookup->id;
|
||||
break;
|
||||
}
|
||||
|
||||
lookup = lookup->next;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
void D_AnimationInit(D_Animation *a, U32 id, U32 rows, U32 cols, F32 time) {
|
||||
a->id = id;
|
||||
|
||||
a->rows = rows;
|
||||
a->cols = cols;
|
||||
|
||||
a->time = 0;
|
||||
a->frame_time = time;
|
||||
|
||||
a->frame = V2F(1.0f / (F32) cols, 1.0f / (F32) rows);
|
||||
|
||||
a->index = 0;
|
||||
}
|
||||
|
||||
R2f D_AnimationFrame(D_Animation *a) {
|
||||
R2f result = { 0 };
|
||||
|
||||
U32 row = a->index / a->cols;
|
||||
U32 col = a->index % a->cols;
|
||||
|
||||
result.min = V2F(a->frame.w * col, a->frame.h * row);
|
||||
result.max = V2F(result.min.x + a->frame.w, result.min.y + a->frame.h);
|
||||
return result;
|
||||
}
|
||||
|
||||
void D_AnimationUpdate(D_Animation *a, F32 dt) {
|
||||
a->time += dt;
|
||||
if (a->time >= a->frame_time) {
|
||||
a->time = 0;
|
||||
a->index += 1;
|
||||
|
||||
if (a->index >= (a->rows * a->cols)) { a->index = 0; }
|
||||
}
|
||||
}
|
||||
|
||||
void D_Begin(D_Context *draw, Vk_Frame *frame, U32 max_rects) {
|
||||
Vk_Buffer *rbo = &frame->rbo;
|
||||
|
||||
draw->rbo = rbo;
|
||||
draw->n_rects = 0;
|
||||
draw->max_rects = max_rects;
|
||||
draw->rects = rbo->data;
|
||||
}
|
||||
|
||||
void D_End(D_Context *draw, Vk_Frame *frame) {
|
||||
VkCommandBuffer cmd = frame->cmd;
|
||||
Vk_Pipeline *basic = &draw->pipelines[0];
|
||||
|
||||
// We can probably stop doing this at some point
|
||||
VkDescriptorSet set;
|
||||
VkDescriptorSetAllocateInfo alloc_info = { 0 };
|
||||
alloc_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
|
||||
alloc_info.descriptorPool = frame->descriptors;
|
||||
alloc_info.descriptorSetCount = 1;
|
||||
alloc_info.pSetLayouts = &basic->layout.set;
|
||||
|
||||
vk.AllocateDescriptorSets(vk.device, &alloc_info, &set);
|
||||
|
||||
// 'update' the descriptor sets for binding
|
||||
M_TempScope(0, 0) {
|
||||
VkWriteDescriptorSet writes[2] = { 0 };
|
||||
|
||||
VkDescriptorBufferInfo rbo_info = { 0 };
|
||||
rbo_info.buffer = draw->rbo->handle;
|
||||
rbo_info.offset = 0;
|
||||
rbo_info.range = VK_WHOLE_SIZE;
|
||||
|
||||
U32 total = draw->n_images + draw->n_fonts;
|
||||
VkDescriptorImageInfo *image_info = M_ArenaPush(temp.arena, VkDescriptorImageInfo, .count = total);
|
||||
|
||||
for (U32 it = 0; it < draw->n_images; ++it) {
|
||||
image_info[it].imageView = draw->images[it].image.view;
|
||||
image_info[it].sampler = vk.sampler;
|
||||
image_info[it].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
||||
}
|
||||
|
||||
U32 idx = draw->n_images;
|
||||
for (D_Font *it = draw->fonts; it != 0; it = it->next) {
|
||||
image_info[idx].imageView = it->image.view;
|
||||
image_info[idx].sampler = vk.sampler; // @Todo: probably want linear filtering on fonts
|
||||
image_info[idx].imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
||||
|
||||
idx += 1;
|
||||
}
|
||||
|
||||
writes[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
||||
writes[0].dstSet = set;
|
||||
writes[0].dstBinding = 0;
|
||||
writes[0].descriptorCount = 1;
|
||||
writes[0].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
|
||||
writes[0].pBufferInfo = &rbo_info;
|
||||
|
||||
writes[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
||||
writes[1].dstSet = set;
|
||||
writes[1].dstBinding = 1;
|
||||
writes[1].descriptorCount = total;
|
||||
writes[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
||||
writes[1].pImageInfo = image_info;
|
||||
|
||||
vk.UpdateDescriptorSets(vk.device, ArraySize(writes), writes, 0, 0);
|
||||
}
|
||||
|
||||
vk.CmdBindPipeline(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, basic->handle);
|
||||
vk.CmdBindDescriptorSets(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, basic->layout.pipeline, 0, 1, &set, 0, 0);
|
||||
vk.CmdPushConstants(cmd, basic->layout.pipeline, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(Mat4x4F), &draw->camera->proj.fwd);
|
||||
|
||||
VkViewport viewport = { 0, 0, (F32) draw->window_width, (F32) draw->window_height, 0.0f, 1.0f };
|
||||
VkRect2D scissor = { 0, 0, draw->window_width, draw->window_height };
|
||||
|
||||
vk.CmdSetViewport(cmd, 0, 1, &viewport);
|
||||
vk.CmdSetScissor(cmd, 0, 1, &scissor);
|
||||
|
||||
vk.CmdDraw(cmd, 6, draw->n_rects, 0, 0);
|
||||
}
|
||||
|
||||
internal U32 V4f_UnormColour(V4f c) {
|
||||
// @Todo: SRGB handling
|
||||
U32 result =
|
||||
((U8) (255.0f * c.a)) << 24 |
|
||||
((U8) (255.0f * c.r)) << 16 |
|
||||
((U8) (255.0f * c.g)) << 8 |
|
||||
((U8) (255.0f * c.b)) << 0;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void _D_Rect(D_Context *draw, D_RectOpts *opts) {
|
||||
if (draw->n_rects < draw->max_rects) {
|
||||
D_Rect *rect = &draw->rects[draw->n_rects];
|
||||
|
||||
rect->texture = opts->texture;
|
||||
|
||||
rect->x = opts->p.x;
|
||||
rect->y = opts->p.y;
|
||||
|
||||
rect->uv[0] = opts->uv.min.x;
|
||||
rect->uv[1] = opts->uv.min.y;
|
||||
rect->uv[2] = opts->uv.max.x;
|
||||
rect->uv[3] = opts->uv.max.y;
|
||||
|
||||
rect->angle = opts->angle;
|
||||
|
||||
if (opts->flags & D_RECT_PER_VERTEX_COLOUR) {
|
||||
rect->c[0] = V4f_UnormColour(opts->vtxc[0]);
|
||||
rect->c[1] = V4f_UnormColour(opts->vtxc[1]);
|
||||
rect->c[2] = V4f_UnormColour(opts->vtxc[2]);
|
||||
rect->c[3] = V4f_UnormColour(opts->vtxc[3]);
|
||||
}
|
||||
else {
|
||||
U32 unorm = V4f_UnormColour(opts->c);
|
||||
|
||||
rect->c[0] = unorm;
|
||||
rect->c[1] = unorm;
|
||||
rect->c[2] = unorm;
|
||||
rect->c[3] = unorm;
|
||||
}
|
||||
|
||||
if (opts->flags & D_RECT_IGNORE_ASPECT) {
|
||||
rect->w = opts->dim.w;
|
||||
rect->h = opts->dim.h;
|
||||
}
|
||||
else {
|
||||
Vk_Image *image = &draw->images[opts->texture].image;
|
||||
|
||||
F32 width = cast(F32) image->width;
|
||||
F32 height = cast(F32) image->height;
|
||||
|
||||
if (opts->flags & D_RECT_UV_ASPECT) {
|
||||
width *= (opts->uv.max.x - opts->uv.min.x);
|
||||
height *= (opts->uv.max.y - opts->uv.min.y);
|
||||
}
|
||||
|
||||
F32 aspect_w = (width > height) ? (width / height) : 1.0f;
|
||||
F32 aspect_h = (width > height) ? 1.0f : (height / width);
|
||||
|
||||
rect->w = opts->scale * aspect_w;
|
||||
rect->h = opts->scale * aspect_h;
|
||||
}
|
||||
|
||||
draw->n_rects += 1;
|
||||
}
|
||||
}
|
||||
|
||||
void D_Text(D_Context *draw, D_Font *font, Str8 text, F32 x, F32 y) {
|
||||
F32 xoff = x;
|
||||
local_persist B32 done = 0;
|
||||
|
||||
for (S64 it = 0; it < text.count; ++it) {
|
||||
if (text.data[it] >= ' ' && text.data[it] <= '~') {
|
||||
D_Glyph *glyph = &font->glyphs[text.data[it] - ' '];
|
||||
|
||||
V2f size;
|
||||
size.w = (glyph->box.max.x - glyph->box.min.x);
|
||||
size.h = (glyph->box.max.y - glyph->box.min.y);
|
||||
|
||||
V2f dim;
|
||||
|
||||
F32 scale = 150.0f / (font->ascent - font->descent);
|
||||
|
||||
if (size.w > size.h) {
|
||||
dim.w = scale * (size.w / size.h);
|
||||
dim.h = scale;
|
||||
}
|
||||
else {
|
||||
dim.w = scale;
|
||||
dim.h = scale * (size.h / size.w);
|
||||
}
|
||||
|
||||
// @Hardcode: need font index in font struct
|
||||
D_Rect(draw, xoff, y, .texture = 0, .c = V4F(0, 1, 1, 1), .dim = dim, .flags = D_RECT_IGNORE_ASPECT);
|
||||
D_Rect(draw, xoff, y, .texture = draw->n_images, .uv = glyph->box, .dim = dim, .flags = D_RECT_IGNORE_ASPECT);
|
||||
xoff += (1.05f * dim.w); // glyph->advance; //+ glyph->offset.x;
|
||||
|
||||
if (!done) { printf(" %f (%f, %f)", xoff, dim.w, dim.h); }
|
||||
}
|
||||
}
|
||||
|
||||
if (!done) { printf("\n"); }
|
||||
done = true;
|
||||
}
|
||||
|
||||
void D_FontLoad(D_Context *draw, Str8 name, F32 size) {
|
||||
M_TempScope(0, 0) {
|
||||
(void) draw;
|
||||
|
||||
Str8 exe_path = FS_SystemPath(temp.arena, FS_SYSTEM_PATH_EXE);
|
||||
Str8 path = Sf(temp.arena, "%.*s/assets/%.*s.ttf", Sv(exe_path), Sv(name));
|
||||
|
||||
Str8 font_data = FS_ReadEntireFile(temp.arena, path);
|
||||
|
||||
stbtt_fontinfo info = { 0 };
|
||||
stbtt_InitFont(&info, font_data.data, 0);
|
||||
|
||||
F32 scale = stbtt_ScaleForPixelHeight(&info, 20);
|
||||
|
||||
U32 w = 512;
|
||||
U32 h = 512;
|
||||
|
||||
U8 *pixels = M_ArenaPush(temp.arena, U8, .count = (w * h));
|
||||
|
||||
stbtt_pack_context pack = { 0 };
|
||||
stbtt_PackBegin(&pack, pixels, w, h, 0, 1, 0);
|
||||
|
||||
U32 count = '~' - ' ';
|
||||
stbtt_packedchar *packed = M_ArenaPush(temp.arena, stbtt_packedchar, .count = count);
|
||||
stbtt_PackFontRange(&pack, font_data.data, 0, size, ' ', count, packed);
|
||||
|
||||
D_Font *font = M_ArenaPush(draw->arena, D_Font);
|
||||
|
||||
font->px = size;
|
||||
|
||||
S32 asc, desc, line;
|
||||
stbtt_GetFontVMetrics(&info, &asc, &desc, &line);
|
||||
|
||||
font->line_advance = cast(F32) line;
|
||||
font->ascent = cast(F32) asc;
|
||||
font->descent = cast(F32) desc;
|
||||
|
||||
font->glyphs = M_ArenaPush(draw->arena, D_Glyph, .count = count);
|
||||
|
||||
for (U32 it = 0; it < count; ++it) {
|
||||
D_Glyph *glyph = &font->glyphs[it];
|
||||
stbtt_packedchar *chr = &packed[it];
|
||||
|
||||
S32 left, width;
|
||||
stbtt_GetCodepointHMetrics(&info, ' ' + it, &width, &left);
|
||||
|
||||
glyph->box = R2F(V2F(chr->x0 / (F32) w, chr->y0 / (F32) h), V2F(chr->x1 / (F32) w, chr->y1 / (F32) h));
|
||||
|
||||
glyph->advance = glyph->box.max.x - glyph->box.min.x; //chr->xadvance * scale;
|
||||
glyph->offset = V2F(chr->xoff * scale, chr->yoff * scale);
|
||||
}
|
||||
|
||||
Vk_Buffer *staging = &draw->staging;
|
||||
U32 *px = (U32 *) staging->data;
|
||||
for (U32 y = 0; y < h; ++y) {
|
||||
for (U32 x = 0; x < w; ++x) {
|
||||
*px++ = 0x0 | ((U32) pixels[(y * w) + x] << 24);
|
||||
}
|
||||
}
|
||||
|
||||
Vk_CommandBuffer *cmds = Vk_CommandBufferPush();
|
||||
|
||||
font->image.width = w;
|
||||
font->image.height = h;
|
||||
|
||||
font->image.format = VK_FORMAT_R8G8B8A8_SRGB;
|
||||
font->image.usage = VK_IMAGE_USAGE_SAMPLED_BIT;
|
||||
|
||||
Vk_ImageCreate(&font->image);
|
||||
|
||||
VkBufferImageCopy copy = { 0 };
|
||||
|
||||
copy.bufferOffset = 0;
|
||||
copy.bufferRowLength = 0;
|
||||
copy.bufferImageHeight = 0;
|
||||
|
||||
copy.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||
copy.imageSubresource.mipLevel = 0;
|
||||
copy.imageSubresource.baseArrayLayer = 0;
|
||||
copy.imageSubresource.layerCount = 1;
|
||||
|
||||
copy.imageExtent.width = w;
|
||||
copy.imageExtent.height = h;
|
||||
copy.imageExtent.depth = 1;
|
||||
|
||||
VkImageMemoryBarrier2 transfer = { 0 };
|
||||
VkImageMemoryBarrier2 shader_read = { 0 };
|
||||
|
||||
transfer.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2;
|
||||
transfer.srcStageMask = VK_PIPELINE_STAGE_2_NONE;
|
||||
transfer.srcAccessMask = VK_ACCESS_2_NONE;
|
||||
transfer.dstStageMask = VK_PIPELINE_STAGE_2_TRANSFER_BIT;
|
||||
transfer.dstAccessMask = VK_ACCESS_2_TRANSFER_WRITE_BIT;
|
||||
transfer.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||||
transfer.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
|
||||
transfer.image = font->image.handle;
|
||||
|
||||
transfer.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||
transfer.subresourceRange.layerCount = 1;
|
||||
transfer.subresourceRange.levelCount = 1;
|
||||
|
||||
shader_read.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2;
|
||||
shader_read.srcStageMask = VK_PIPELINE_STAGE_2_TRANSFER_BIT;
|
||||
shader_read.srcAccessMask = VK_ACCESS_2_TRANSFER_WRITE_BIT;
|
||||
shader_read.dstStageMask = VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT;
|
||||
shader_read.dstAccessMask = VK_ACCESS_2_SHADER_SAMPLED_READ_BIT;
|
||||
shader_read.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
|
||||
shader_read.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
||||
shader_read.image = font->image.handle;
|
||||
|
||||
shader_read.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||
shader_read.subresourceRange.layerCount = 1;
|
||||
shader_read.subresourceRange.levelCount = 1;
|
||||
|
||||
VkDependencyInfo dep = { 0 };
|
||||
dep.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO;
|
||||
|
||||
dep.imageMemoryBarrierCount = 1;
|
||||
dep.pImageMemoryBarriers = &transfer;
|
||||
|
||||
vk.CmdPipelineBarrier2(cmds->handle, &dep);
|
||||
|
||||
vk.CmdCopyBufferToImage(cmds->handle, staging->handle, font->image.handle, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ©);
|
||||
|
||||
dep.pImageMemoryBarriers = &shader_read;
|
||||
|
||||
vk.CmdPipelineBarrier2(cmds->handle, &dep);
|
||||
|
||||
Vk_CommandBufferSubmit(cmds, true);
|
||||
|
||||
SLL_PushN(draw->fonts, font, next);
|
||||
draw->n_fonts += 1;
|
||||
}
|
||||
}
|
||||
158
code/draw/core.h
Normal file
@@ -0,0 +1,158 @@
|
||||
#if !defined(LD_DRAW_CORE_H_)
|
||||
#define LD_DRAW_CORE_H_
|
||||
|
||||
#define D_MAX_RECTS (262144)
|
||||
|
||||
typedef struct D_Glyph D_Glyph;
|
||||
struct D_Glyph {
|
||||
F32 advance;
|
||||
V2f offset;
|
||||
R2f box;
|
||||
};
|
||||
|
||||
typedef struct D_Font D_Font;
|
||||
struct D_Font {
|
||||
D_Font *next;
|
||||
|
||||
F32 px;
|
||||
|
||||
F32 line_advance;
|
||||
F32 ascent;
|
||||
F32 descent;
|
||||
|
||||
F32 *kerning;
|
||||
D_Glyph *glyphs;
|
||||
|
||||
Vk_Image image;
|
||||
};
|
||||
|
||||
typedef struct D_Image D_Image;
|
||||
struct D_Image {
|
||||
Str8 name;
|
||||
Vk_Image image;
|
||||
};
|
||||
|
||||
typedef struct D_Rect D_Rect;
|
||||
struct D_Rect {
|
||||
U32 texture;
|
||||
|
||||
U32 c[4]; // per-vertex colours
|
||||
F32 uv[4];
|
||||
|
||||
F32 angle;
|
||||
F32 x, y;
|
||||
F32 w, h;
|
||||
|
||||
U32 p0, p1;
|
||||
};
|
||||
|
||||
StaticAssert(sizeof(D_Rect) == 64);
|
||||
|
||||
typedef struct D_Animation D_Animation;
|
||||
struct D_Animation {
|
||||
U32 id;
|
||||
|
||||
U32 rows;
|
||||
U32 cols;
|
||||
F32 frame_time;
|
||||
F32 time;
|
||||
|
||||
V2f frame; // size of one frame
|
||||
|
||||
U32 index;
|
||||
};
|
||||
|
||||
struct G_Camera;
|
||||
|
||||
#define D_ASSET_HASH_COUNT 128
|
||||
|
||||
typedef struct D_AssetHash D_AssetHash;
|
||||
struct D_AssetHash {
|
||||
D_AssetHash *next;
|
||||
|
||||
Str8 value;
|
||||
U64 hash;
|
||||
U32 id; // texture id
|
||||
};
|
||||
|
||||
typedef struct D_Context D_Context;
|
||||
struct D_Context {
|
||||
Vk_Buffer *rbo;
|
||||
Vk_Buffer staging;
|
||||
|
||||
M_Arena *arena;
|
||||
|
||||
U32 n_pipelines;
|
||||
Vk_Pipeline *pipelines;
|
||||
|
||||
U32 n_images;
|
||||
D_Image *images;
|
||||
|
||||
U32 n_fonts;
|
||||
D_Font *fonts;
|
||||
|
||||
U32 max_rects;
|
||||
U32 n_rects;
|
||||
D_Rect *rects;
|
||||
|
||||
U32 window_width;
|
||||
U32 window_height;
|
||||
|
||||
D_AssetHash *lookup[D_ASSET_HASH_COUNT];
|
||||
|
||||
struct G_Camera *camera;
|
||||
};
|
||||
|
||||
typedef U32 D_RectFlags;
|
||||
enum D_RectFlags {
|
||||
D_RECT_IGNORE_ASPECT = (1 << 0), // by default only width is used as a "dimension"
|
||||
D_RECT_PER_VERTEX_COLOUR = (1 << 1), // split colours per vertex
|
||||
D_RECT_UV_ASPECT = (1 << 2), // get the aspect from the uv rect rather than the full image
|
||||
};
|
||||
|
||||
typedef struct D_RectOpts D_RectOpts;
|
||||
struct D_RectOpts {
|
||||
D_RectFlags flags;
|
||||
|
||||
U32 texture;
|
||||
|
||||
R2f uv;
|
||||
V2f p;
|
||||
|
||||
F32 angle;
|
||||
|
||||
union {
|
||||
V2f dim;
|
||||
|
||||
struct {
|
||||
F32 w, h;
|
||||
};
|
||||
|
||||
struct {
|
||||
F32 scale, _h;
|
||||
};
|
||||
};
|
||||
|
||||
union {
|
||||
V4f c;
|
||||
V4f vtxc[4];
|
||||
};
|
||||
};
|
||||
|
||||
function U32 D_ImageHandle(D_Context *draw, Str8 name);
|
||||
|
||||
function void D_AnimationInit(D_Animation *a, U32 id, U32 rows, U32 cols, F32 time);
|
||||
function R2f D_AnimationFrame(D_Animation *a);
|
||||
function void D_AnimationUpdate(D_Animation *a, F32 dt);
|
||||
|
||||
function void D_Begin(D_Context *draw, Vk_Frame *frame, U32 max_rects);
|
||||
function void D_End(D_Context *draw, Vk_Frame *frame);
|
||||
|
||||
function void D_Text(D_Context *draw, D_Font *font, Str8 text, F32 x, F32 y);
|
||||
|
||||
function void _D_Rect(D_Context *draw, D_RectOpts *opts);
|
||||
#define D_Rect(draw, x, y, ...) _D_Rect(draw, &(D_RectOpts) { .p = V2F(x, y), .uv = R2F(V2F(0, 0), V2F(1, 1)), .scale = 1, .c = V4F(1, 1, 1, 1), ##__VA_ARGS__ })
|
||||
|
||||
function void D_FontLoad(D_Context *draw, Str8 path, F32 size);
|
||||
|
||||
#endif // LD_DRAW_CORE_H_
|
||||
414
code/first.c
@@ -1,21 +1,36 @@
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3/SDL_keycode.h>
|
||||
|
||||
#define STB_IMAGE_IMPLEMENTATION 1
|
||||
#include <stb_image.h>
|
||||
|
||||
#define STB_RECT_PACK_IMPLEMENTATION 1
|
||||
#include <stb_rect_pack.h>
|
||||
|
||||
#define STB_TRUETYPE_IMPLEMENTATION 1
|
||||
#include <stb_truetype.h>
|
||||
|
||||
#include "core/core.h"
|
||||
#include "core/types.h"
|
||||
#include "os/core.h"
|
||||
|
||||
#include "vulkan/core.h"
|
||||
#include "draw/core.h"
|
||||
|
||||
#include "game/impl/player.c"
|
||||
#include "game/core.h"
|
||||
|
||||
#include "game/impl/world.c"
|
||||
#include "game/impl/npc.c"
|
||||
#include "game/impl/bandit.c"
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
(void) argc;
|
||||
(void) argv;
|
||||
|
||||
if (!SDL_Init(SDL_INIT_VIDEO))
|
||||
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO))
|
||||
{
|
||||
printf("[Error] :: Failed to initialise SDL3 (%s)\n", SDL_GetError());
|
||||
return 1;
|
||||
@@ -28,52 +43,326 @@ int main(int argc, char **argv)
|
||||
return 1;
|
||||
}
|
||||
|
||||
SDL_AudioStream *austream;
|
||||
Str8 audio_data;
|
||||
M_TempScope(0, 0) {
|
||||
SDL_AudioSpec spec;
|
||||
spec.format = SDL_AUDIO_S16LE;
|
||||
spec.channels = 2;
|
||||
spec.freq = 44100;
|
||||
|
||||
austream = SDL_OpenAudioDeviceStream(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &spec, 0, 0);
|
||||
|
||||
if (!austream) {
|
||||
printf("Failed to open audio stream (%s)\n", SDL_GetError());
|
||||
}
|
||||
|
||||
Str8 exec = FS_SystemPath(temp.arena, FS_SYSTEM_PATH_EXE);
|
||||
Str8 path = Sf(temp.arena, "%.*s/assets/outside_ambience.wav", Sv(exec));
|
||||
|
||||
SDL_AudioSpec wav_spec;
|
||||
U32 count;
|
||||
|
||||
SDL_LoadWAV((const char *) path.data, &wav_spec, &audio_data.data, &count);
|
||||
audio_data.count = count;
|
||||
}
|
||||
|
||||
Vk_Setup(window);
|
||||
|
||||
G_State *game = 0;
|
||||
{
|
||||
M_Arena *arena = M_ArenaAlloc(GB(64), .initial = MB(4));
|
||||
game = M_ArenaPush(arena, G_State);
|
||||
|
||||
game->arena = arena;
|
||||
game->draw.arena = arena;
|
||||
|
||||
G_ImagesLoad(game);
|
||||
G_PipelinesLoad(game);
|
||||
G_AudioLoad(game);
|
||||
|
||||
G_Camera *camera = &game->camera;
|
||||
|
||||
camera->x = V3F(1, 0, 0);
|
||||
camera->y = V3F(0, 1, 0);
|
||||
camera->z = V3F(0, 0, 1);
|
||||
camera->p = V3F(0, 0, 48);
|
||||
|
||||
camera->fov = 60.0f;
|
||||
|
||||
camera->nearp = 0.01f;
|
||||
camera->farp = 1000.0f;
|
||||
|
||||
game->draw.camera = camera;
|
||||
World *world = M_ArenaPush(arena, World);
|
||||
//LoadWorld(arena, world);
|
||||
game->world = world;
|
||||
world->arena = arena;
|
||||
//world->navMesh = &TestNavMesh;
|
||||
world->random = Random_Seed(29237489723847);
|
||||
world->npcCount = 127;
|
||||
for(U32 i = 0; i < world->npcCount; i++) {
|
||||
NPC *npc1 = &world->npcs[i];
|
||||
npc1->collision.pos.x = 0;
|
||||
npc1->collision.pos.y = 0;
|
||||
npc1->collision.size.x = 1;
|
||||
npc1->collision.size.y = 2;
|
||||
npc1->name = S("Matt");
|
||||
npc1->mode = NPC_ACTION_WAITING;
|
||||
npc1->currentArea = WORLD_AREA_OUTSIDE;
|
||||
npc1->waitTime = 0;
|
||||
npc1->maxWaitTime = 5;
|
||||
npc1->currentNavNode = 0;
|
||||
}
|
||||
|
||||
Bandit *badman = &world->bandit;
|
||||
badman->collision.pos.x = 15;
|
||||
badman->collision.pos.y = 15;
|
||||
badman->collision.size.x = 1;
|
||||
badman->collision.size.y = 2;
|
||||
badman->name = S("Leroy Brown");
|
||||
badman->mode = BANDIT_WAITING;
|
||||
badman->waitTime = 0;
|
||||
badman->maxWaitTime = 2;
|
||||
badman->poiCount = 2;
|
||||
badman->pointsOfInterest[0] = 937;
|
||||
badman->pointsOfInterest[1] = 12;
|
||||
|
||||
world->npcPOI[0] = 100;
|
||||
world->player.world = world;
|
||||
world->player.pos.x = 0;
|
||||
world->player.pos.y = 0;
|
||||
world->player.bulletsLoaded = PLAYER_BULLET_COUNT;
|
||||
world->player.reloadTimer = 0;
|
||||
world->player.currentArea = WORLD_AREA_OUTSIDE;
|
||||
world->map = map;
|
||||
|
||||
world->tileTypes = M_ArenaPush(arena, World_Tile, .count=WORLD_TILE_TYPE_MAX);
|
||||
world->tileTypes[0].rotation=0;
|
||||
world->tileTypes[0].tag=S("tile_dirt_0");
|
||||
world->tileTypes[1].rotation=0,
|
||||
world->tileTypes[1].tag=S("path_middle");
|
||||
world->tileTypes[2].rotation=0;
|
||||
world->tileTypes[2].tag=S("path_middle_edge");
|
||||
world->tileTypes[3].rotation=PI_F32/2;
|
||||
world->tileTypes[3].tag=S("path_middle_edge");
|
||||
world->tileTypes[4].rotation=PI_F32;
|
||||
world->tileTypes[4].tag=S("path_middle_edge");
|
||||
world->tileTypes[5].rotation=-PI_F32/2;
|
||||
world->tileTypes[5].tag=S("path_middle_edge");
|
||||
world->tileTypes[6].rotation=0;
|
||||
world->tileTypes[6].tag=S("path_middle");
|
||||
world->tileTypes[7].rotation=PI_F32/2;
|
||||
world->tileTypes[7].tag=S("path_middle");
|
||||
world->tileTypes[8].rotation=-PI_F32;
|
||||
world->tileTypes[8].tag=S("path_middle");
|
||||
world->tileTypes[9].rotation=-PI_F32/2;
|
||||
world->tileTypes[9].tag=S("path_middle");
|
||||
world->tileTypes[10].rotation=0;
|
||||
world->tileTypes[10].tag=S("path_corner");
|
||||
world->tileTypes[11].rotation=PI_F32/2;
|
||||
world->tileTypes[11].tag=S("path_corner");
|
||||
world->tileTypes[12].rotation=-PI_F32/2;
|
||||
world->tileTypes[12].tag=S("path_corner");
|
||||
world->tileTypes[13].rotation=PI_F32;
|
||||
world->tileTypes[13].tag=S("path_corner");
|
||||
world->tileTypes[14].rotation=0;
|
||||
world->tileTypes[14].tag=S("tile_dirt_1");
|
||||
|
||||
world->propTypes = M_ArenaPush(arena, World_PropType, .count=WORLD_PROP_TYPE_MAX);
|
||||
world->propTypes[0].tag=S("rug0");
|
||||
world->propTypes[0].scale=1;
|
||||
world->propTypes[1].tag=S("rug1");
|
||||
world->propTypes[1].scale=1;
|
||||
world->propTypes[2].tag=S("skull");
|
||||
world->propTypes[2].scale=1;
|
||||
world->propTypes[3].tag = S("table");
|
||||
world->propTypes[3].scale=2;
|
||||
world->propTypes[4].tag = S("barrel");
|
||||
world->propTypes[4].scale=1;
|
||||
world->propTypes[5].tag = S("can");
|
||||
world->propTypes[5].scale=1;
|
||||
world->propTypes[6].tag = S("candle");
|
||||
world->propTypes[6].scale=1;
|
||||
world->propTypes[7].tag = S("clock");
|
||||
world->propTypes[7].scale=1;
|
||||
world->propTypes[8].tag = S("log_pile");
|
||||
world->propTypes[8].scale=1;
|
||||
world->propTypes[9].tag = S("nightstand");
|
||||
world->propTypes[9].scale=1;
|
||||
world->propTypes[10].tag = S("pool_table");
|
||||
world->propTypes[10].scale=2;
|
||||
world->propTypes[11].tag = S("saloon_ext");
|
||||
world->propTypes[11].scale=6.875f;
|
||||
world->propTypes[12].tag = S("saloon_int");
|
||||
world->propTypes[12].scale=2*6.875f;
|
||||
world->propTypes[13].tag = S("house");
|
||||
world->propTypes[13].scale=6.875f;
|
||||
world->propTypes[14].tag = S("house_int");
|
||||
world->propTypes[14].scale=6.875f;
|
||||
world->propTypes[15].tag=S("tile_detail_0");
|
||||
world->propTypes[15].scale=1;
|
||||
world->propTypes[16].tag = S("tile_detail_1");
|
||||
world->propTypes[16].scale=1;
|
||||
world->propTypes[17].tag = S("tile_detail_2");
|
||||
world->propTypes[17].scale=1;
|
||||
world->propTypes[18].tag= S("tile_detail_3");
|
||||
world->propTypes[18].scale=1;
|
||||
world->propTypes[19].tag= S("tile_detail_4");
|
||||
world->propTypes[19].scale=1;
|
||||
world->propTypes[20].tag=S("tile_detail_5");
|
||||
world->propTypes[20].scale=1;
|
||||
world->propTypes[21].tag=S("tile_detail_6");
|
||||
world->propTypes[21].scale=1;
|
||||
world->propCount = 0;
|
||||
world->props = M_ArenaPush(arena, World_Prop, .count=WORLD_PROP_MAX);
|
||||
world->hitboxes = M_ArenaPush(arena, AABB, .count=WORLD_HITBOX_MAX);
|
||||
GenerateNavMesh(arena, world);
|
||||
}
|
||||
game->editor.enabled = false;
|
||||
game->editor.mode = G_EDITOR_MODE_TILE;
|
||||
game->editor.currentLevel = WORLD_AREA_OUTSIDE;
|
||||
|
||||
|
||||
D_Animation animation;
|
||||
{
|
||||
U32 id = D_ImageHandle(&game->draw, S("npc_front_base_white"));
|
||||
D_AnimationInit(&animation, id, 1, 4, 1.0f / 20.0f);
|
||||
}
|
||||
|
||||
bool running = true;
|
||||
Player player;
|
||||
player.pos.x = 0;
|
||||
player.pos.y = 0;
|
||||
|
||||
const int width = 1280;
|
||||
const int height = 720;
|
||||
|
||||
while (running)
|
||||
{
|
||||
SDL_Event e;
|
||||
while (SDL_PollEvent(&e))
|
||||
{
|
||||
PlayerUpdate(&e, &player);
|
||||
if (e.type == SDL_EVENT_QUIT)
|
||||
{
|
||||
running = false;
|
||||
}
|
||||
V3f projection = G_CameraUnproject(&game->camera, V2f_Clip(
|
||||
V2F(e.button.x, e.button.y),
|
||||
V2F((F32) width, (F32) height)
|
||||
));
|
||||
game->world->mouseProjected = V2F(projection.x, projection.y);
|
||||
if(e.type==SDL_EVENT_MOUSE_MOTION) {
|
||||
game->editor.cursor = V2F(projection.x, projection.y);
|
||||
}
|
||||
if (!game->editor.enabled) {
|
||||
ProcessEvents(&e, game->world);
|
||||
} else {
|
||||
if(e.type==SDL_EVENT_MOUSE_BUTTON_DOWN && e.button.button == SDL_BUTTON_LEFT) {
|
||||
switch(game->editor.mode){
|
||||
case G_EDITOR_MODE_TILE: {
|
||||
F32 tilex = (S32)(game->editor.cursor.x+TILE_SIZE/2);
|
||||
F32 tiley = (S32)(game->editor.cursor.y+TILE_SIZE/2);
|
||||
game->world->map[(S32)tilex + (S32)tiley * 96] = game->editor.currentAsset;
|
||||
break;
|
||||
}
|
||||
case G_EDITOR_MODE_PROP: {
|
||||
game->world->props[game->world->propCount].propType = game->editor.currentAsset;
|
||||
game->world->props[game->world->propCount].area = game->editor.currentLevel;
|
||||
game->world->props[game->world->propCount].pos = game->editor.cursor;
|
||||
game->world->propCount++;
|
||||
break;
|
||||
}
|
||||
case G_EDITOR_MODE_HITBOX: {
|
||||
game->editor.dragStart = game->editor.cursor;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else if(e.type==SDL_EVENT_MOUSE_BUTTON_UP && e.button.button == SDL_BUTTON_LEFT && game->editor.mode == G_EDITOR_MODE_HITBOX) {
|
||||
// Add hitbox
|
||||
V2f topLeft = V2F(Min(game->editor.cursor.x, game->editor.dragStart.x), Min(game->editor.cursor.y, game->editor.dragStart.y));
|
||||
game->world->hitboxes[game->world->hitboxCount].pos = topLeft;
|
||||
game->world->hitboxes[game->world->hitboxCount].size = V2F(
|
||||
Abs(game->editor.cursor.x-game->editor.dragStart.x),
|
||||
Abs(game->editor.cursor.y-game->editor.dragStart.y)
|
||||
);
|
||||
game->world->hitboxCount++;
|
||||
}
|
||||
}
|
||||
|
||||
if (e.type == SDL_EVENT_KEY_DOWN) {
|
||||
switch(e.key.key) {
|
||||
case SDLK_F10: {
|
||||
game->editor.enabled = !game->editor.enabled;
|
||||
break;
|
||||
}
|
||||
case SDLK_RIGHT: {
|
||||
game->editor.currentAsset = Min(game->editor.currentAsset+1, 64);
|
||||
break;
|
||||
}
|
||||
case SDLK_LEFT: {
|
||||
game->editor.currentAsset = Max(game->editor.currentAsset-1, 0);
|
||||
break;
|
||||
}
|
||||
case SDLK_A: {
|
||||
game->camera.p.x -= 5;
|
||||
break;
|
||||
}
|
||||
case SDLK_D: {
|
||||
game->camera.p.x += 5;
|
||||
break;
|
||||
}
|
||||
case SDLK_W: {
|
||||
game->camera.p.y -= 5;
|
||||
break;
|
||||
}
|
||||
case SDLK_S: {
|
||||
game->camera.p.y += 5;
|
||||
break;
|
||||
}
|
||||
case SDLK_UP: {
|
||||
game->editor.currentLevel++;
|
||||
game->world->player.currentArea++;
|
||||
break;
|
||||
}
|
||||
case SDLK_DOWN: {
|
||||
game->editor.currentLevel--;
|
||||
game->world->player.currentArea--;
|
||||
break;
|
||||
}
|
||||
case SDLK_SPACE: {
|
||||
game->editor.mode = (game->editor.mode + 1) % 4;
|
||||
printf("EDITOR MODE %d\n", game->editor.mode);
|
||||
break;
|
||||
}
|
||||
case SDLK_U: {
|
||||
switch(game->editor.mode) {
|
||||
case G_EDITOR_MODE_PROP: {game->world->propCount--;}
|
||||
case G_EDITOR_MODE_HITBOX: {game->world->hitboxCount--;}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(!game->editor.enabled) {
|
||||
UpdateWorld(1.0f / 60.0f, game->world);
|
||||
game->camera.p.x = game->world->player.pos.x;
|
||||
game->camera.p.y = game->world->player.pos.y;
|
||||
}
|
||||
D_AnimationUpdate(&animation, 1.0f / 250.0f);
|
||||
|
||||
int w, h;
|
||||
SDL_GetWindowSizeInPixels(window, &w, &h);
|
||||
|
||||
game->draw.window_width = w;
|
||||
game->draw.window_height = h;
|
||||
|
||||
G_CalculateCamera(&game->camera, (F32)w / (F32)h);
|
||||
|
||||
Vk_Frame *frame = Vk_FrameBegin(window);
|
||||
VkCommandBuffer cmd = frame->cmd;
|
||||
VkImageMemoryBarrier2 colour_optimal = {0};
|
||||
colour_optimal.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2;
|
||||
colour_optimal.srcStageMask = VK_PIPELINE_STAGE_2_NONE;
|
||||
colour_optimal.srcAccessMask = VK_ACCESS_2_NONE;
|
||||
colour_optimal.dstStageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT;
|
||||
colour_optimal.dstAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT;
|
||||
colour_optimal.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||||
colour_optimal.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
||||
colour_optimal.image = vk.swapchain.images[frame->image];
|
||||
|
||||
colour_optimal.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||
colour_optimal.subresourceRange.layerCount = 1;
|
||||
colour_optimal.subresourceRange.levelCount = 1;
|
||||
|
||||
VkDependencyInfo colour_barrier = {0};
|
||||
colour_barrier.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO;
|
||||
colour_barrier.imageMemoryBarrierCount = 1;
|
||||
colour_barrier.pImageMemoryBarriers = &colour_optimal;
|
||||
|
||||
vk.CmdPipelineBarrier2(cmd, &colour_barrier);
|
||||
|
||||
VkClearValue clear_colour;
|
||||
clear_colour.color.float32[0] = 1.0f;
|
||||
clear_colour.color.float32[0] = 0.0f;
|
||||
clear_colour.color.float32[1] = 0.0f;
|
||||
clear_colour.color.float32[2] = 0.0f;
|
||||
clear_colour.color.float32[3] = 1.0f;
|
||||
@@ -95,29 +384,52 @@ int main(int argc, char **argv)
|
||||
|
||||
vk.CmdBeginRendering(cmd, &rendering_info);
|
||||
|
||||
D_Begin(&game->draw, frame, D_MAX_RECTS);
|
||||
|
||||
RenderWorld(game->world, &game->draw);
|
||||
|
||||
R2f aframe = D_AnimationFrame(&animation);
|
||||
D_Rect(&game->draw, 0, 0, .texture = animation.id, .uv = aframe, .flags = D_RECT_UV_ASPECT);
|
||||
|
||||
if(game->editor.enabled) {
|
||||
G_Editor editor = game->editor;
|
||||
F32 tilex = floor(editor.cursor.x+TILE_SIZE/2);
|
||||
F32 tiley = floor(editor.cursor.y+TILE_SIZE/2);
|
||||
switch(game->editor.mode) {
|
||||
case G_EDITOR_MODE_TILE: {
|
||||
World_Tile asset = game->world->tileTypes[editor.currentAsset];
|
||||
D_Rect(&game->draw, tilex, tiley, .texture=D_ImageHandle(&game->draw, asset.tag), .angle=asset.rotation);
|
||||
break;
|
||||
}
|
||||
case G_EDITOR_MODE_PROP: {
|
||||
World_PropType prop = game->world->propTypes[editor.currentAsset];
|
||||
D_Rect(&game->draw, editor.cursor.x, editor.cursor.y, .texture=D_ImageHandle(&game->draw, prop.tag), .scale=prop.scale);
|
||||
break;
|
||||
}
|
||||
case G_EDITOR_MODE_HITBOX: {
|
||||
for(int i = 0; i < game->world->hitboxCount; i++) {
|
||||
V2f centre = AABB_Centre(game->world->hitboxes[i]);
|
||||
D_Rect(
|
||||
&game->draw,
|
||||
centre.x,
|
||||
centre.y,
|
||||
.texture=0,
|
||||
.dim=game->world->hitboxes[i].size,
|
||||
.flags=D_RECT_IGNORE_ASPECT,
|
||||
.c=V4F(100,0,0,0.7),
|
||||
);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case G_EDITOR_MODE_POI: {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
D_End(&game->draw, frame);
|
||||
|
||||
vk.CmdEndRendering(cmd);
|
||||
|
||||
VkImageMemoryBarrier2 present_src = {0};
|
||||
present_src.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2;
|
||||
present_src.srcStageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT;
|
||||
present_src.srcAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT;
|
||||
present_src.dstStageMask = VK_PIPELINE_STAGE_2_NONE;
|
||||
present_src.dstAccessMask = VK_ACCESS_2_NONE;
|
||||
present_src.oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
||||
present_src.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
|
||||
present_src.image = vk.swapchain.images[frame->image];
|
||||
|
||||
present_src.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||
present_src.subresourceRange.layerCount = 1;
|
||||
present_src.subresourceRange.levelCount = 1;
|
||||
|
||||
VkDependencyInfo to_present = {0};
|
||||
to_present.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO;
|
||||
to_present.imageMemoryBarrierCount = 1;
|
||||
to_present.pImageMemoryBarriers = &present_src;
|
||||
|
||||
vk.CmdPipelineBarrier2(cmd, &to_present);
|
||||
|
||||
Vk_FrameEnd();
|
||||
}
|
||||
|
||||
@@ -130,3 +442,5 @@ int main(int argc, char **argv)
|
||||
#include "core/core.c"
|
||||
#include "os/core.c"
|
||||
#include "vulkan/core.c"
|
||||
#include "draw/core.c"
|
||||
#include "game/core.c"
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
#if !defined(LD_GAME_AABB_H_)
|
||||
#define LD_GAME_AABB_H_
|
||||
#include "types.h"
|
||||
#include "../core/types.h"
|
||||
#include "../core/macros.h"
|
||||
|
||||
|
||||
typedef struct AABB AABB;
|
||||
struct AABB {
|
||||
struct AABB
|
||||
{
|
||||
V2f pos;
|
||||
V2f size;
|
||||
};
|
||||
|
||||
function bool AABB_Collide(AABB a, AABB b);
|
||||
function bool AABB_Point(AABB a, V2f v);
|
||||
function bool AABB_Slab(V2f origin, V2f point, AABB a);
|
||||
function V2f AABB_Centre(AABB a);
|
||||
|
||||
function bool AABB_Collide(AABB a, AABB b);
|
||||
function bool AABB_Point(AABB a, V2f v);
|
||||
|
||||
#endif // LD_GAME_AABB_H_
|
||||
#endif // LD_GAME_AABB_H_
|
||||
|
||||
61
code/game/bandit.h
Normal file
@@ -0,0 +1,61 @@
|
||||
#if !defined(LD_GAME_BANDIT_H_)
|
||||
#define LD_GAME_BANDIT_H_
|
||||
|
||||
typedef enum BANDIT_ACTION BANDIT_ACTION;
|
||||
enum BANDIT_ACTION {
|
||||
BANDIT_WAITING,
|
||||
BANDIT_WALKING,
|
||||
BANDIT_RUNNING,
|
||||
BANDIT_SHOOTING,
|
||||
};
|
||||
|
||||
typedef struct Bandit Bandit;
|
||||
struct Bandit {
|
||||
//// Personal
|
||||
AABB collision;
|
||||
Str8 name;
|
||||
World_Area currentArea;
|
||||
|
||||
//// Actions
|
||||
BANDIT_ACTION mode;
|
||||
// How long they've been waiting.
|
||||
F32 waitTime;
|
||||
// How long they will wait in this location.
|
||||
F32 maxWaitTime;
|
||||
|
||||
U32 poiCount;
|
||||
// Places the bandit walks to / from
|
||||
// E.g. hide outs, home, saloon
|
||||
U32 pointsOfInterest[12];
|
||||
//// Navigation
|
||||
// The bandits's current path
|
||||
NavPath path;
|
||||
// Which node the bandit is on in the path
|
||||
U32 currentNavNode;
|
||||
// The current index of the bandit's current node
|
||||
U32 pathIndex;
|
||||
// Target navNode index the bandit is walking to
|
||||
U32 targetNavNode;
|
||||
// How long the bandit has been walking to the next index
|
||||
F32 walkTimer;
|
||||
|
||||
// How many shots they can take.
|
||||
U32 health;
|
||||
// How paranoid they are about being hunted,
|
||||
// this will make them more trigger happy.
|
||||
F32 paranoidLevel;
|
||||
// Max 6?
|
||||
U8 bullets;
|
||||
// How long it its between shots.
|
||||
F32 shootDelay;
|
||||
// After each shot this is set to shootDelay;
|
||||
F32 shootCooldownTimer;
|
||||
// How long it takes them to reload.
|
||||
F32 reloadTime;
|
||||
// Accuracy, their shots can vary between this angle either side (rads)
|
||||
F32 accuracyRange;
|
||||
// A the circle around the bandit where they will trigger the quicktime reaction scene
|
||||
F32 agroRadius;
|
||||
};
|
||||
|
||||
#endif // LD_GAME_BANDIT_H_
|
||||
362
code/game/core.c
Normal file
@@ -0,0 +1,362 @@
|
||||
// @Todo: These should move to draw/core.c
|
||||
//
|
||||
internal void G_AssetNameHash(D_Context *draw, Str8 name, U32 id) {
|
||||
D_AssetHash *result = M_ArenaPush(draw->arena, D_AssetHash);
|
||||
|
||||
U64 hash = Str8_Hash(name);
|
||||
U64 index = hash & (D_ASSET_HASH_COUNT - 1);
|
||||
|
||||
result->value = name;
|
||||
result->hash = hash;
|
||||
result->id = id;
|
||||
|
||||
SLL_Push(draw->lookup[index], result);
|
||||
}
|
||||
|
||||
void G_ImagesLoad(G_State *game) {
|
||||
M_TempScope(0, 0) {
|
||||
D_Context *draw = &game->draw;
|
||||
|
||||
Str8 exe_path = FS_SystemPath(temp.arena, FS_SYSTEM_PATH_EXE);
|
||||
Str8 path = Sf(temp.arena, "%.*s/assets", Sv(exe_path));
|
||||
|
||||
FS_List assets = FS_PathList(temp.arena, path);
|
||||
|
||||
Vk_Buffer *staging = &draw->staging;
|
||||
staging->size = MB(256);
|
||||
staging->usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
||||
staging->host_visible = true;
|
||||
|
||||
Vk_BufferCreate(staging);
|
||||
|
||||
U8 *base = staging->data;
|
||||
U64 offset = 0;
|
||||
|
||||
Vk_CommandBuffer *cmds = Vk_CommandBufferPush();
|
||||
|
||||
// We reserve the first texture for the "white" texture
|
||||
draw->n_images = 1;
|
||||
|
||||
for (FS_Entry *it = assets.first; it != 0; it = it->next) {
|
||||
if (Str8_EndsWith(it->basename, S("png"))) {
|
||||
draw->n_images += 1;
|
||||
}
|
||||
}
|
||||
|
||||
VkBufferImageCopy copy = { 0 };
|
||||
draw->images = M_ArenaPush(game->arena, D_Image, .count = draw->n_images);
|
||||
draw->n_images = 1;
|
||||
|
||||
// Upload the white texture
|
||||
{
|
||||
D_Image *white = &draw->images[0];
|
||||
U32 white_data[] = { 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF };
|
||||
|
||||
M_CopySize(base, white_data, sizeof(white_data));
|
||||
|
||||
copy.bufferOffset = offset;
|
||||
copy.bufferRowLength = 0;
|
||||
copy.bufferImageHeight = 0;
|
||||
|
||||
copy.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||
copy.imageSubresource.mipLevel = 0;
|
||||
copy.imageSubresource.baseArrayLayer = 0;
|
||||
copy.imageSubresource.layerCount = 1;
|
||||
|
||||
copy.imageExtent.width = 2;
|
||||
copy.imageExtent.height = 2;
|
||||
copy.imageExtent.depth = 1;
|
||||
|
||||
base += sizeof(white_data);
|
||||
offset += sizeof(white_data);
|
||||
|
||||
white->name = S("_WHITE");
|
||||
|
||||
// This doesn't _really_ need one because its just zero and always will be zero
|
||||
G_AssetNameHash(draw, white->name, 0);
|
||||
|
||||
white->image.width = 2;
|
||||
white->image.height = 2;
|
||||
white->image.format = VK_FORMAT_R8G8B8A8_SRGB;
|
||||
white->image.usage = VK_IMAGE_USAGE_SAMPLED_BIT;
|
||||
|
||||
Vk_ImageCreate(&white->image);
|
||||
|
||||
// We could combine all of these 'pre-transfer' and 'post-transfer' layers into one
|
||||
// batch, it would simply mean doing three loops over the images and setting them all up
|
||||
// and submitting them in one go. It doesn't really matter for now
|
||||
//
|
||||
|
||||
VkImageMemoryBarrier2 transfer = { 0 };
|
||||
VkImageMemoryBarrier2 shader_read = { 0 };
|
||||
|
||||
transfer.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2;
|
||||
transfer.srcStageMask = VK_PIPELINE_STAGE_2_NONE;
|
||||
transfer.srcAccessMask = VK_ACCESS_2_NONE;
|
||||
transfer.dstStageMask = VK_PIPELINE_STAGE_2_TRANSFER_BIT;
|
||||
transfer.dstAccessMask = VK_ACCESS_2_TRANSFER_WRITE_BIT;
|
||||
transfer.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||||
transfer.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
|
||||
transfer.image = white->image.handle;
|
||||
|
||||
transfer.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||
transfer.subresourceRange.layerCount = 1;
|
||||
transfer.subresourceRange.levelCount = 1;
|
||||
|
||||
shader_read.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2;
|
||||
shader_read.srcStageMask = VK_PIPELINE_STAGE_2_TRANSFER_BIT;
|
||||
shader_read.srcAccessMask = VK_ACCESS_2_TRANSFER_WRITE_BIT;
|
||||
shader_read.dstStageMask = VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT;
|
||||
shader_read.dstAccessMask = VK_ACCESS_2_SHADER_SAMPLED_READ_BIT;
|
||||
shader_read.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
|
||||
shader_read.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
||||
shader_read.image = white->image.handle;
|
||||
|
||||
shader_read.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||
shader_read.subresourceRange.layerCount = 1;
|
||||
shader_read.subresourceRange.levelCount = 1;
|
||||
|
||||
VkDependencyInfo dep = { 0 };
|
||||
dep.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO;
|
||||
|
||||
dep.imageMemoryBarrierCount = 1;
|
||||
dep.pImageMemoryBarriers = &transfer;
|
||||
|
||||
vk.CmdPipelineBarrier2(cmds->handle, &dep);
|
||||
|
||||
vk.CmdCopyBufferToImage(cmds->handle, staging->handle, white->image.handle, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ©);
|
||||
|
||||
dep.pImageMemoryBarriers = &shader_read;
|
||||
|
||||
vk.CmdPipelineBarrier2(cmds->handle, &dep);
|
||||
}
|
||||
|
||||
// Image upload is sbi_load -> copy to staging -> upload to gpu texture
|
||||
|
||||
for (FS_Entry *it = assets.first; it != 0; it = it->next) {
|
||||
if (Str8_EndsWith(it->basename, S("png"))) {
|
||||
S32 w, h, c;
|
||||
stbi_uc *data = stbi_load((const char *) it->path.data, &w, &h, &c, 4);
|
||||
|
||||
if (data) {
|
||||
D_Image *image = &draw->images[draw->n_images];
|
||||
|
||||
U64 image_sz = 4 * w * h;
|
||||
|
||||
M_CopySize(base, data, image_sz);
|
||||
|
||||
copy.bufferOffset = offset;
|
||||
copy.bufferRowLength = 0;
|
||||
copy.bufferImageHeight = 0;
|
||||
|
||||
copy.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||
copy.imageSubresource.mipLevel = 0;
|
||||
copy.imageSubresource.baseArrayLayer = 0;
|
||||
copy.imageSubresource.layerCount = 1;
|
||||
|
||||
copy.imageExtent.width = w;
|
||||
copy.imageExtent.height = h;
|
||||
copy.imageExtent.depth = 1;
|
||||
|
||||
base += image_sz;
|
||||
offset += image_sz;
|
||||
|
||||
Assert(offset <= staging->size);
|
||||
|
||||
image->name = Str8_Copy(game->arena, Str8_RemoveAfterLast(it->basename, '.'));
|
||||
|
||||
G_AssetNameHash(draw, image->name, draw->n_images);
|
||||
|
||||
draw->n_images += 1;
|
||||
printf("[Info] :: Loaded %.*s from %.*s\n", Sv(image->name), Sv(it->basename));
|
||||
|
||||
image->image.width = w;
|
||||
image->image.height = h;
|
||||
|
||||
image->image.format = VK_FORMAT_R8G8B8A8_SRGB;
|
||||
image->image.usage = VK_IMAGE_USAGE_SAMPLED_BIT;
|
||||
|
||||
Vk_ImageCreate(&image->image);
|
||||
|
||||
VkImageMemoryBarrier2 transfer = { 0 };
|
||||
VkImageMemoryBarrier2 shader_read = { 0 };
|
||||
|
||||
transfer.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2;
|
||||
transfer.srcStageMask = VK_PIPELINE_STAGE_2_NONE;
|
||||
transfer.srcAccessMask = VK_ACCESS_2_NONE;
|
||||
transfer.dstStageMask = VK_PIPELINE_STAGE_2_TRANSFER_BIT;
|
||||
transfer.dstAccessMask = VK_ACCESS_2_TRANSFER_WRITE_BIT;
|
||||
transfer.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||||
transfer.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
|
||||
transfer.image = image->image.handle;
|
||||
|
||||
transfer.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||
transfer.subresourceRange.layerCount = 1;
|
||||
transfer.subresourceRange.levelCount = 1;
|
||||
|
||||
shader_read.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2;
|
||||
shader_read.srcStageMask = VK_PIPELINE_STAGE_2_TRANSFER_BIT;
|
||||
shader_read.srcAccessMask = VK_ACCESS_2_TRANSFER_WRITE_BIT;
|
||||
shader_read.dstStageMask = VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT;
|
||||
shader_read.dstAccessMask = VK_ACCESS_2_SHADER_SAMPLED_READ_BIT;
|
||||
shader_read.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
|
||||
shader_read.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
||||
shader_read.image = image->image.handle;
|
||||
|
||||
shader_read.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||
shader_read.subresourceRange.layerCount = 1;
|
||||
shader_read.subresourceRange.levelCount = 1;
|
||||
|
||||
VkDependencyInfo dep = { 0 };
|
||||
dep.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO;
|
||||
|
||||
dep.imageMemoryBarrierCount = 1;
|
||||
dep.pImageMemoryBarriers = &transfer;
|
||||
|
||||
vk.CmdPipelineBarrier2(cmds->handle, &dep);
|
||||
|
||||
vk.CmdCopyBufferToImage(cmds->handle, staging->handle, image->image.handle, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ©);
|
||||
|
||||
dep.pImageMemoryBarriers = &shader_read;
|
||||
|
||||
vk.CmdPipelineBarrier2(cmds->handle, &dep);
|
||||
|
||||
stbi_image_free(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Vk_CommandBufferSubmit(cmds, true /* wait */);
|
||||
}
|
||||
}
|
||||
|
||||
void G_PipelinesLoad(G_State *game) {
|
||||
D_Context *draw = &game->draw;
|
||||
|
||||
draw->pipelines = M_ArenaPush(game->arena, Vk_Pipeline, .count = 1);
|
||||
|
||||
Vk_Pipeline *basic = &draw->pipelines[0];
|
||||
|
||||
VkShaderModule vshader = 0, fshader = 0;
|
||||
M_TempScope(0, 0) {
|
||||
Str8 exe_path = FS_SystemPath(temp.arena, FS_SYSTEM_PATH_EXE);
|
||||
Str8 vshader_code = FS_ReadEntireFile(temp.arena, Sf(temp.arena, "%.*s/assets/shaders/basic.vert.spv", Sv(exe_path)));
|
||||
Str8 fshader_code = FS_ReadEntireFile(temp.arena, Sf(temp.arena, "%.*s/assets/shaders/basic.frag.spv", Sv(exe_path)));
|
||||
|
||||
VkShaderModuleCreateInfo create_info = { 0 };
|
||||
create_info.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
|
||||
create_info.pCode = (U32 *) vshader_code.data;
|
||||
create_info.codeSize = vshader_code.count;
|
||||
|
||||
vk.CreateShaderModule(vk.device, &create_info, 0, &vshader);
|
||||
|
||||
create_info.pCode = (U32 *) fshader_code.data;
|
||||
create_info.codeSize = fshader_code.count;
|
||||
|
||||
vk.CreateShaderModule(vk.device, &create_info, 0, &fshader);
|
||||
}
|
||||
|
||||
// Create pipeline layout, its insane what you have to do for this because the ALREADY KNOW
|
||||
// all of this information
|
||||
{
|
||||
VkDescriptorSetLayoutBinding bindings[2] = { 0 };
|
||||
|
||||
bindings[0].binding = 0;
|
||||
bindings[0].descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
|
||||
bindings[0].descriptorCount = 1;
|
||||
bindings[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
|
||||
|
||||
bindings[1].binding = 1;
|
||||
bindings[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
||||
bindings[1].descriptorCount = game->draw.n_images + game->draw.n_fonts;
|
||||
bindings[1].stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
|
||||
|
||||
VkDescriptorSetLayoutCreateInfo set_info = { 0 };
|
||||
set_info.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
|
||||
set_info.bindingCount = ArraySize(bindings);
|
||||
set_info.pBindings = bindings;
|
||||
|
||||
vk.CreateDescriptorSetLayout(vk.device, &set_info, 0, &basic->layout.set);
|
||||
|
||||
VkPushConstantRange push_range = { 0 };
|
||||
push_range.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
|
||||
push_range.offset = 0;
|
||||
push_range.size = 128;
|
||||
|
||||
VkPipelineLayoutCreateInfo layout_create = { 0 };
|
||||
layout_create.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
|
||||
layout_create.setLayoutCount = 1;
|
||||
layout_create.pSetLayouts = &basic->layout.set;
|
||||
layout_create.pushConstantRangeCount = 1;
|
||||
layout_create.pPushConstantRanges = &push_range;
|
||||
|
||||
vk.CreatePipelineLayout(vk.device, &layout_create, 0, &basic->layout.pipeline);
|
||||
}
|
||||
|
||||
basic->targets.items[0] = vk.swapchain.format.format;
|
||||
basic->targets.count = 1;
|
||||
|
||||
basic->shaders.count = 2;
|
||||
|
||||
basic->shaders.items[0].stage = VK_SHADER_STAGE_VERTEX_BIT;
|
||||
basic->shaders.items[0].handle = vshader;
|
||||
|
||||
basic->shaders.items[1].stage = VK_SHADER_STAGE_FRAGMENT_BIT;
|
||||
basic->shaders.items[1].handle = fshader;
|
||||
|
||||
Vk_PipelineCreate(basic);
|
||||
}
|
||||
|
||||
void G_AudioLoad(G_State *game) {
|
||||
(void) game;
|
||||
|
||||
M_TempScope(0, 0) {
|
||||
Str8 exe_path = FS_SystemPath(temp.arena, FS_SYSTEM_PATH_EXE);
|
||||
Str8 path = Sf(temp.arena, "%.*s/assets", Sv(exe_path));
|
||||
|
||||
FS_List assets = FS_PathList(temp.arena, path);
|
||||
|
||||
// @Incomplete: finish this
|
||||
|
||||
for (FS_Entry *it = assets.first; it != 0; it = it->next) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void G_CalculateCamera(G_Camera *camera, F32 aspect) {
|
||||
Mat4x4FInv proj = M4x4F_Perspective(camera->fov, aspect, camera->nearp, camera->farp);
|
||||
Mat4x4FInv view = M4x4F_CameraView(camera->x, camera->y, camera->z, camera->p);
|
||||
|
||||
camera->proj.fwd = M4x4F_Mul(proj.fwd, view.fwd);
|
||||
camera->proj.inv = M4x4F_Mul(view.inv, proj.inv);
|
||||
}
|
||||
|
||||
V3f G_CameraUnprojectAt(G_Camera *camera, V2f clip, F32 z) {
|
||||
V3f dir = V3f_Sub(camera->p, V3f_Scale(camera->z, z));
|
||||
V4f z_dist = V4F(dir.x, dir.y, dir.z, 1.0f);
|
||||
V4f persp = M4x4F_VMul4(camera->proj.fwd, z_dist);
|
||||
|
||||
clip = V2f_Scale(clip, persp.w);
|
||||
|
||||
V4f world = M4x4F_VMul4(camera->proj.inv, V4F(clip.x, clip.y, persp.z, persp.w));
|
||||
|
||||
V3f result = world.xyz;
|
||||
return result;
|
||||
}
|
||||
|
||||
V3f G_CameraUnproject(G_Camera *camera, V2f clip) {
|
||||
V3f result = G_CameraUnprojectAt(camera, clip, camera->p.z);
|
||||
return result;
|
||||
}
|
||||
|
||||
R3f G_CameraBounds(G_Camera *camera) {
|
||||
R3f result;
|
||||
result.min = G_CameraUnproject(camera, V2F(-1, -1));
|
||||
result.max = G_CameraUnproject(camera, V2F( 1, 1));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#include "impl/aabb.c"
|
||||
#include "impl/nav.c"
|
||||
#include "impl/player.c"
|
||||
64
code/game/core.h
Normal file
@@ -0,0 +1,64 @@
|
||||
#if !defined(LD_GAME_CORE_H_)
|
||||
#define LD_GAME_CORE_H_
|
||||
#include "world.h"
|
||||
|
||||
typedef struct G_Camera G_Camera;
|
||||
struct G_Camera {
|
||||
V3f x, y, z;
|
||||
V3f p;
|
||||
|
||||
F32 fov;
|
||||
F32 nearp, farp;
|
||||
|
||||
Mat4x4FInv proj;
|
||||
};
|
||||
|
||||
#define TILE_SIZE 1.0
|
||||
|
||||
typedef enum G_EDITOR_MODE G_EDITOR_MODE;
|
||||
enum G_EDITOR_MODE {
|
||||
G_EDITOR_MODE_TILE,
|
||||
G_EDITOR_MODE_PROP,
|
||||
G_EDITOR_MODE_HITBOX,
|
||||
G_EDITOR_MODE_POI,
|
||||
};
|
||||
|
||||
typedef struct G_Editor G_Editor;
|
||||
struct G_Editor {
|
||||
bool enabled;
|
||||
U32 currentLevel;
|
||||
S32 currentAsset;
|
||||
G_EDITOR_MODE mode;
|
||||
V2f cursor;
|
||||
V2f dragStart;
|
||||
};
|
||||
|
||||
typedef struct G_State G_State;
|
||||
struct G_State {
|
||||
M_Arena *arena;
|
||||
|
||||
D_Context draw;
|
||||
G_Camera camera;
|
||||
|
||||
G_Editor editor;
|
||||
World *world;
|
||||
};
|
||||
|
||||
function void G_ImagesLoad(G_State *game);
|
||||
function void G_PipelinesLoad(G_State *game);
|
||||
function void G_AudioLoad(G_State *game);
|
||||
|
||||
function void G_CalculateCamera(G_Camera *camera, F32 aspect);
|
||||
|
||||
// Assumes 'calculate' has been called
|
||||
function V3f G_CameraUnprojectAt(G_Camera *camera, V2f clip, F32 z);
|
||||
function V3f G_CameraUnproject(G_Camera *camera, V2f clip);
|
||||
|
||||
function R3f G_CameraBounds(G_Camera *camera);
|
||||
|
||||
#include "aabb.h"
|
||||
#include "player.h"
|
||||
#include "nav.h"
|
||||
#include "npc.h"
|
||||
|
||||
#endif // LD_GAME_CORE_H_
|
||||
@@ -1,15 +1,40 @@
|
||||
#include "../aabb.h"
|
||||
#include "../types.h"
|
||||
#include "game/aabb.h"
|
||||
#include <math.h>
|
||||
|
||||
bool AABB_Collide(AABB a, AABB b) {
|
||||
bool AABB_Collide(AABB a, AABB b)
|
||||
{
|
||||
bool collision_x = a.pos.x + a.size.x >= b.pos.x && b.pos.x + b.size.x >= a.pos.x;
|
||||
bool collision_y = a.pos.y + a.size.x >= b.pos.y && b.pos.y + b.size.y >= a.pos.y;
|
||||
return collision_x && collision_y;
|
||||
}
|
||||
|
||||
bool AABB_Point(AABB a, V2f v) {
|
||||
bool AABB_Point(AABB a, V2f v)
|
||||
{
|
||||
bool collision_x = a.pos.x + a.size.x >= v.x && a.pos.x <= v.x;
|
||||
bool collision_y = a.pos.x + a.size.y >= v.y && a.pos.y <= v.y;
|
||||
return collision_x && collision_y;
|
||||
}
|
||||
|
||||
bool AABB_Slab(V2f origin, V2f point, AABB a)
|
||||
{
|
||||
V2f start = a.pos;
|
||||
V2f finish = {a.pos.x + a.size.x, a.pos.y + a.size.y};
|
||||
V2f invdirection = {1 / (origin.x - point.x), 1 / (origin.y - point.y)};
|
||||
// x
|
||||
F32 tLow = (start.x - origin.x) * invdirection.x;
|
||||
F32 tHigh = (finish.x - origin.x) * invdirection.x;
|
||||
F32 tMin = Min(tLow, tHigh);
|
||||
F32 tMax = Max(tLow, tHigh);
|
||||
// y
|
||||
tLow = (start.y - origin.y) * invdirection.y;
|
||||
tHigh = (finish.y - origin.y) * invdirection.y;
|
||||
|
||||
tMin = Max(tMin, Min(tLow, tHigh));
|
||||
tMax = Min(tMax, Max(tLow, tHigh));
|
||||
|
||||
return tMax >= tMin;
|
||||
}
|
||||
|
||||
V2f AABB_Centre(AABB a) {
|
||||
return V2F(a.pos.x + a.size.x/2, a.pos.y + a.size.y/2);
|
||||
}
|
||||
|
||||
47
code/game/impl/bandit.c
Normal file
@@ -0,0 +1,47 @@
|
||||
#include "game/world.h"
|
||||
#include "game/bandit.h"
|
||||
|
||||
void UpdateBandit(F32 delta, Bandit *bandit, World *world) {
|
||||
if (
|
||||
world->player.controls.shot && AABB_Slab(world->player.pos, world->player.shotPos, bandit->collision) && bandit->currentArea == world->player.currentArea)
|
||||
{
|
||||
printf("You shot the bandit %*.s\n", Sv(bandit->name));
|
||||
bandit->health--;
|
||||
}
|
||||
switch (bandit->mode) {
|
||||
case BANDIT_WAITING:
|
||||
bandit->waitTime+=delta;
|
||||
if(bandit->waitTime > bandit->maxWaitTime) {
|
||||
bandit->mode = BANDIT_WALKING;
|
||||
do {
|
||||
U32 randomChoice = Random_U32(&world->random, 0, bandit->poiCount);
|
||||
bandit->targetNavNode = bandit->pointsOfInterest[randomChoice];
|
||||
} while(bandit->targetNavNode == bandit->currentNavNode);
|
||||
bandit->path = Nav_Path(world->navMesh, bandit->currentNavNode, bandit->targetNavNode);
|
||||
bandit->walkTimer = 0;
|
||||
}
|
||||
break;
|
||||
case BANDIT_WALKING:
|
||||
bandit->walkTimer+=delta;
|
||||
if(bandit->walkTimer >= NPC_SPEED){
|
||||
bandit->walkTimer = 0;
|
||||
if(bandit->path.nodeCount == bandit->pathIndex+1){
|
||||
bandit->mode = BANDIT_WAITING;
|
||||
bandit->maxWaitTime = Random_F32(&world->random, 20, 140);
|
||||
bandit->waitTime = 0;
|
||||
bandit->currentNavNode = bandit->targetNavNode;
|
||||
bandit->pathIndex = 0;
|
||||
return;
|
||||
}
|
||||
bandit->currentNavNode = bandit->path.indexes[bandit->pathIndex];
|
||||
bandit->pathIndex+=1;
|
||||
}
|
||||
NavNode cNav = world->navMesh->nodes[bandit->currentNavNode];
|
||||
NavNode tNav = world->navMesh->nodes[bandit->path.indexes[bandit->pathIndex]];
|
||||
bandit->collision.pos.x = cNav.pos.x * (1 - bandit->walkTimer/NPC_SPEED) + tNav.pos.x * bandit->walkTimer/NPC_SPEED;
|
||||
bandit->collision.pos.y = cNav.pos.y * (1 - bandit->walkTimer/NPC_SPEED) + tNav.pos.y * bandit->walkTimer/NPC_SPEED;
|
||||
break;
|
||||
// TODO Shooting
|
||||
// TODO Running away
|
||||
}
|
||||
}
|
||||
@@ -1,50 +1,59 @@
|
||||
#include "../nav.h"
|
||||
#include "../../core/types.h"
|
||||
#include "game/nav.h"
|
||||
#include "core/types.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define MAX_UNFINISHED 128
|
||||
|
||||
typedef struct navSearchNodeState navSearchNodeState;
|
||||
struct navSearchNodeState{
|
||||
struct navSearchNodeState
|
||||
{
|
||||
bool visited;
|
||||
U64 distance;
|
||||
F64 distance;
|
||||
U32 shortest;
|
||||
bool addedToUnvisited;
|
||||
};
|
||||
|
||||
typedef struct navSearchState navSearchState;
|
||||
struct navSearchState{
|
||||
navSearchNodeState nodeStates[NAV_MAX_NODES];
|
||||
struct navSearchState
|
||||
{
|
||||
navSearchNodeState nodeStates[NAV_MAX_NODES];
|
||||
};
|
||||
|
||||
navSearchState initState(U32 start, U32 meshSize) {
|
||||
navSearchState state = {};
|
||||
for(int i = 0; i < meshSize; i++) {
|
||||
navSearchState initState(U32 start, U32 meshSize)
|
||||
{
|
||||
navSearchState state;
|
||||
for (U32 i = 0; i < meshSize; i++)
|
||||
{
|
||||
state.nodeStates[i].visited = false;
|
||||
state.nodeStates[i].addedToUnvisited = false;
|
||||
// underflow to the max :)
|
||||
state.nodeStates[i].distance = -1;
|
||||
state.nodeStates[i].distance = F64_MAX;
|
||||
state.nodeStates[i].shortest = 0;
|
||||
}
|
||||
state.nodeStates[start].distance = 0;
|
||||
return state;
|
||||
}
|
||||
|
||||
U32 getLowestState(U32 unfinishedIndexes[128], U32 unfinishedCount, navSearchState state, U32 *offset) {
|
||||
U32 lowest = -1;
|
||||
U32 lowestI = -1;
|
||||
U32 getLowestState(U32 unfinishedIndexes[128], U32 unfinishedCount, navSearchState state, U32 *offset)
|
||||
{
|
||||
U32 lowest = U32_MAX;
|
||||
U32 lowestI = U32_MAX;
|
||||
bool startFound = false;
|
||||
for(int i = *offset; i < unfinishedCount; i++) {
|
||||
for (U32 i = *offset; i < unfinishedCount; i++)
|
||||
{
|
||||
navSearchNodeState checkNode = state.nodeStates[unfinishedIndexes[i]];
|
||||
if(checkNode.visited) {
|
||||
if(!startFound) {
|
||||
if (checkNode.visited)
|
||||
{
|
||||
if (!startFound)
|
||||
{
|
||||
*offset = i;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
startFound = true;
|
||||
if (lowest > checkNode.distance) {
|
||||
lowest = checkNode.distance;
|
||||
if (lowest > checkNode.distance)
|
||||
{
|
||||
lowest = cast(U32) checkNode.distance;
|
||||
lowestI = unfinishedIndexes[i];
|
||||
}
|
||||
}
|
||||
@@ -52,44 +61,53 @@ U32 getLowestState(U32 unfinishedIndexes[128], U32 unfinishedCount, navSearchSta
|
||||
}
|
||||
|
||||
// Generate a path to follow between the start and end node.
|
||||
NavPath Nav_Path(NavMesh mesh, U32 start, U32 end) {
|
||||
navSearchState state = initState(start, mesh.nodeCount);
|
||||
NavPath Nav_Path(NavMesh *mesh, U32 start, U32 end) {
|
||||
// This is a stupid fix, since it's easier to work backwards
|
||||
// to generate a path, I'm swapping the start / end, to generate
|
||||
// it backwards
|
||||
U32 tmp = end;
|
||||
end = start;
|
||||
start = tmp;
|
||||
navSearchState state = initState(start, mesh->nodeCount);
|
||||
U32 unfinishedCount = 1;
|
||||
U32 unfinishedIndexes[NAV_MAX_NODES] = {start};
|
||||
// I don't want to spend time removing items from
|
||||
// the unfinished nodes, so when checking for a lowest
|
||||
// if I find the first N items have been checked, I'll mark
|
||||
// an offset to skip the first N items.
|
||||
U32 unfinishedOffset = 0;
|
||||
U32 unfinishedOffset = 0;
|
||||
U32 lowestNodeIndex = start;
|
||||
bool found = false;
|
||||
while(!found) {
|
||||
for(int connectionI = 0 ; connectionI < mesh.nodes[lowestNodeIndex].connectionCount; connectionI++) {
|
||||
NavConnection connection = mesh.nodes[lowestNodeIndex].connections[connectionI];
|
||||
navSearchNodeState *testNode = &state.nodeStates[connection.NodeIndex];
|
||||
for(int connectionI = 0 ; connectionI < mesh->nodes[lowestNodeIndex].connectionCount; connectionI++) {
|
||||
NavConnection *connection = &mesh->nodes[lowestNodeIndex].connections[connectionI];
|
||||
navSearchNodeState *testNode = &state.nodeStates[connection->NodeIndex];
|
||||
if(testNode->visited) {continue;}
|
||||
U32 distance = state.nodeStates[lowestNodeIndex].distance + connection.Cost;
|
||||
distance += mesh.nodes[end].pos.x - mesh.nodes[connection.NodeIndex].pos.x;
|
||||
distance += mesh.nodes[end].pos.y - mesh.nodes[connection.NodeIndex].pos.y;
|
||||
F64 distance = cast(F64) (state.nodeStates[lowestNodeIndex].distance + connection->Cost);
|
||||
distance += cast(F64) Abs((mesh->nodes[end].pos.x - mesh->nodes[connection->NodeIndex].pos.x));
|
||||
distance += cast(F64) Abs((mesh->nodes[end].pos.y - mesh->nodes[connection->NodeIndex].pos.y));
|
||||
if(testNode->distance > distance) {
|
||||
testNode->distance = distance;
|
||||
testNode->distance = distance;
|
||||
testNode->shortest = lowestNodeIndex;
|
||||
}
|
||||
if(!testNode->addedToUnvisited) {
|
||||
unfinishedIndexes[unfinishedCount] = connection.NodeIndex;
|
||||
if (!testNode->addedToUnvisited)
|
||||
{
|
||||
unfinishedIndexes[unfinishedCount] = connection->NodeIndex;
|
||||
unfinishedCount++;
|
||||
testNode->addedToUnvisited = true;
|
||||
}
|
||||
}
|
||||
state.nodeStates[lowestNodeIndex].visited = true;
|
||||
lowestNodeIndex = getLowestState(unfinishedIndexes, unfinishedCount, state, &unfinishedOffset);
|
||||
if(lowestNodeIndex == end) {
|
||||
if (lowestNodeIndex == end)
|
||||
{
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
NavPath res_path = {0};
|
||||
U32 index = end;
|
||||
while(index!=start) {
|
||||
while (index != start)
|
||||
{
|
||||
res_path.indexes[res_path.nodeCount] = index;
|
||||
res_path.nodeCount++;
|
||||
index = state.nodeStates[index].shortest;
|
||||
|
||||
43
code/game/impl/npc.c
Normal file
@@ -0,0 +1,43 @@
|
||||
#include "game/npc.h"
|
||||
#include "game/world.h"
|
||||
#include "core/types.h"
|
||||
#include "core/math.h"
|
||||
|
||||
void UpdateNPC(F32 delta, NPC *npc, World *world) {
|
||||
switch (npc->mode) {
|
||||
case NPC_ACTION_WAITING:
|
||||
npc->waitTime+=delta;
|
||||
if(npc->waitTime > npc->maxWaitTime) {
|
||||
npc->mode = NPC_ACTION_WALKING;
|
||||
// TODO change targets to poi's rather than just random nodes
|
||||
// TODO choose either global POI's or use NPC custom poi if
|
||||
// customPOI is true
|
||||
do {
|
||||
npc->targetNavNode = Random_U32(&world->random, 0, world->navMesh->nodeCount);
|
||||
} while(npc->targetNavNode == npc->currentNavNode);
|
||||
npc->path = Nav_Path(world->navMesh, npc->currentNavNode, npc->targetNavNode);
|
||||
npc->walkTimer = 0;
|
||||
}
|
||||
break;
|
||||
case NPC_ACTION_WALKING:
|
||||
npc->walkTimer+=delta;
|
||||
if(npc->walkTimer >= NPC_SPEED){
|
||||
npc->walkTimer = 0;
|
||||
if(npc->path.nodeCount == npc->pathIndex+1){
|
||||
npc->mode = NPC_ACTION_WAITING;
|
||||
npc->maxWaitTime = Random_F32(&world->random, 20, 140);
|
||||
npc->waitTime = 0;
|
||||
npc->currentNavNode = npc->targetNavNode;
|
||||
npc->pathIndex = 0;
|
||||
return;
|
||||
}
|
||||
npc->currentNavNode = npc->path.indexes[npc->pathIndex];
|
||||
npc->pathIndex+=1;
|
||||
}
|
||||
NavNode cNav = world->navMesh->nodes[npc->currentNavNode];
|
||||
NavNode tNav = world->navMesh->nodes[npc->path.indexes[npc->pathIndex]];
|
||||
npc->collision.pos.x = cNav.pos.x * (1 - npc->walkTimer/NPC_SPEED) + tNav.pos.x * npc->walkTimer/NPC_SPEED;
|
||||
npc->collision.pos.y = cNav.pos.y * (1 - npc->walkTimer/NPC_SPEED) + tNav.pos.y * npc->walkTimer/NPC_SPEED;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,78 @@
|
||||
#include "../player.h"
|
||||
#include <SDL3/SDL_events.h>
|
||||
#include <SDL3/SDL_keycode.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void PlayerUpdate(SDL_Event *event, Player *player)
|
||||
void PlayerInput(SDL_Event *event, Player *player)
|
||||
{
|
||||
SDL_KeyboardEvent key = event->key;
|
||||
if (key.key == SDLK_W)
|
||||
SDL_KeyboardEvent key = event->key;
|
||||
SDL_MouseButtonEvent mouseBtn = event->button;
|
||||
if(event->type == SDL_EVENT_KEY_DOWN || event->type == SDL_EVENT_KEY_UP) {
|
||||
bool val = event->type == SDL_EVENT_KEY_DOWN;
|
||||
switch (key.key)
|
||||
{
|
||||
player->pos.y += 10;
|
||||
}
|
||||
if (key.key == SDLK_A)
|
||||
{
|
||||
player->pos.x -= 10;
|
||||
}
|
||||
if (key.key == SDLK_D)
|
||||
{
|
||||
player->pos.x += 10;
|
||||
}
|
||||
if (key.key == SDLK_S)
|
||||
{
|
||||
player->pos.y -= 10;
|
||||
case SDLK_W:
|
||||
{
|
||||
player->controls.upDown = val;
|
||||
break;
|
||||
}
|
||||
case SDLK_A:
|
||||
{
|
||||
player->controls.leftDown = val;
|
||||
break;
|
||||
}
|
||||
case SDLK_D:
|
||||
{
|
||||
player->controls.rightDown = val;
|
||||
break;
|
||||
}
|
||||
case SDLK_S:
|
||||
{
|
||||
player->controls.downDown = val;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (
|
||||
event->type == SDL_EVENT_MOUSE_BUTTON_DOWN
|
||||
&& mouseBtn.button == SDL_BUTTON_LEFT
|
||||
) {
|
||||
if(player->bulletsLoaded > 0) {
|
||||
// shooting
|
||||
player->bulletsLoaded -= 1;
|
||||
player->controls.shot = true;
|
||||
player->shotPos = player->world->mouseProjected;
|
||||
printf("shot %f %f\n", player->shotPos.x, player->shotPos.y);
|
||||
} else if(player->reloadTimer == 0) {
|
||||
player->reloadTimer = PLAYER_RELOAD_TIME;
|
||||
printf("reloading\n");
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerUpdate(F32 delta, Player *player) {
|
||||
player->controls.shot = false;
|
||||
V2f dir = V2F(0, 0);
|
||||
if(player->controls.upDown) {
|
||||
dir.y -= 1;
|
||||
}
|
||||
if(player->controls.downDown) {
|
||||
dir.y += 1;
|
||||
}
|
||||
if(player->controls.leftDown) {
|
||||
dir.x -= 1;
|
||||
}
|
||||
if(player->controls.rightDown) {
|
||||
dir.x += 1;
|
||||
}
|
||||
if(player->reloadTimer > 0) {
|
||||
player->reloadTimer-=delta;
|
||||
if(player->reloadTimer <= 0) {
|
||||
player->bulletsLoaded = PLAYER_BULLET_COUNT;
|
||||
player->reloadTimer = 0;
|
||||
}
|
||||
}
|
||||
dir = V2f_Scale(NormaliseV2F(dir), PLAYER_SPEED*delta);
|
||||
player->pos.x += dir.x;
|
||||
player->pos.y += dir.y;
|
||||
}
|
||||
|
||||
210
code/game/impl/world.c
Normal file
@@ -0,0 +1,210 @@
|
||||
#include "../world.h"
|
||||
#include "../npc.h"
|
||||
#include "../player.h"
|
||||
#include "../aabb.h"
|
||||
#include <SDL3/SDL_events.h>
|
||||
#include <SDL3/SDL_keycode.h>
|
||||
#include <SDL3/SDL_oldnames.h>
|
||||
#include "../map.h"
|
||||
|
||||
void UpdateWorld(F32 delta, World *world)
|
||||
{
|
||||
UpdateBandit(delta, &world->bandit, world);
|
||||
UpdateNPCs(delta, world);
|
||||
PlayerUpdate(delta, &world->player);
|
||||
}
|
||||
|
||||
void UpdateNPCs(F32 delta, World *world)
|
||||
{
|
||||
for (U32 i = 0; i < world->npcCount; i++)
|
||||
{
|
||||
NPC *npc = &world->npcs[i];
|
||||
UpdateNPC(delta, npc, world);
|
||||
if (
|
||||
world->player.controls.shot && AABB_Slab(world->player.pos, world->player.shotPos, npc->collision) && npc->currentArea == world->player.currentArea)
|
||||
{
|
||||
printf("You shot %*.s\n", Sv(world->npcs[i].name));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ProcessEvents(SDL_Event *event, World *world)
|
||||
{
|
||||
PlayerInput(event, &world->player);
|
||||
if(event->type == SDL_EVENT_KEY_DOWN && event->key.key == SDLK_F5){
|
||||
SaveWorld(world->arena, world);
|
||||
}
|
||||
if(event->type == SDL_EVENT_KEY_DOWN && event->key.key == SDLK_F6){
|
||||
LoadWorld(world->arena, world);
|
||||
}
|
||||
}
|
||||
|
||||
void RenderWorld(World *world, D_Context *draw) {
|
||||
if(world->player.currentArea == WORLD_AREA_OUTSIDE) {
|
||||
for (int i = 0; i < 4800; i++) {
|
||||
D_Rect(
|
||||
draw,
|
||||
(F32) (i % 96), (F32) (i / 96),
|
||||
.texture = D_ImageHandle(draw,world->tileTypes[world->map[i]].tag),
|
||||
.angle = (F32) world->tileTypes[world->map[i]].rotation,
|
||||
);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < world->propCount; i++) {
|
||||
if(world->props[i].area == world->player.currentArea) {
|
||||
D_Rect(
|
||||
draw,
|
||||
world->props[i].pos.x,
|
||||
world->props[i].pos.y,
|
||||
.texture = D_ImageHandle(draw,world->propTypes[world->props[i].propType].tag),
|
||||
.scale = world->propTypes[world->props[i].propType].scale,
|
||||
);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < world->navMesh->nodeCount; i++) {
|
||||
NavNode n = world->navMesh->nodes[i];
|
||||
D_Rect(
|
||||
draw,
|
||||
n.pos.x,
|
||||
n.pos.y,
|
||||
.texture = 0,
|
||||
.scale = 0.2f,
|
||||
);
|
||||
}
|
||||
for(U32 i = 0; i < world->npcCount; i++) {
|
||||
NPC npc = world->npcs[i];
|
||||
if(npc.currentArea == world->player.currentArea) {
|
||||
V2f drawPos = AABB_Centre(npc.collision);
|
||||
D_Rect(draw, drawPos.x, drawPos.y, .texture = 1);
|
||||
}
|
||||
}
|
||||
if(world->bandit.currentArea == world->player.currentArea) {
|
||||
V2f drawPos = AABB_Centre(world->bandit.collision);
|
||||
D_Rect(draw, drawPos.x, drawPos.y, .texture = 9);
|
||||
}
|
||||
D_Rect(draw, world->player.pos.x, world->player.pos.y, .texture = 1);
|
||||
}
|
||||
|
||||
void SaveWorld(M_Arena *arena, World *world) {
|
||||
printf("Saving world\n");
|
||||
OS_Handle file = FS_FileOpen(S("world.sgdat"), FS_ACCESS_WRITE);
|
||||
U32 offset = 0;
|
||||
for(int i = 0; i < WORLD_TILE_TYPE_MAX; i++) {
|
||||
FS_FileWrite(file, &world->tileTypes[i].tag.count, sizeof(S64), offset);
|
||||
offset += sizeof(S64);
|
||||
FS_FileWrite(file, world->tileTypes[i].tag.data, world->tileTypes[i].tag.count, offset);
|
||||
offset += sizeof(U8) * world->tileTypes[i].tag.count;
|
||||
FS_FileWrite(file, &world->tileTypes[i].rotation, sizeof(F32), offset);
|
||||
offset += sizeof(F32);
|
||||
}
|
||||
for(int i = 0; i < WORLD_PROP_TYPE_MAX; i++) {
|
||||
FS_FileWrite(file, &world->propTypes[i].tag.count, sizeof(S64), offset);
|
||||
offset += sizeof(S64);
|
||||
FS_FileWrite(file, world->propTypes[i].tag.data, world->propTypes[i].tag.count, offset);
|
||||
offset += sizeof(U8) * world->propTypes[i].tag.count;
|
||||
FS_FileWrite(file, &world->propTypes[i].scale, sizeof(F32), offset);
|
||||
offset += sizeof(F32);
|
||||
}
|
||||
|
||||
FS_FileWrite(file, world->propTypes, sizeof(World_PropType)*WORLD_PROP_TYPE_MAX, offset);
|
||||
offset += sizeof(World_PropType)*WORLD_PROP_TYPE_MAX;
|
||||
|
||||
FS_FileWrite(file, &world->propCount, sizeof(U32), offset);
|
||||
offset += sizeof(U32);
|
||||
|
||||
FS_FileWrite(file, world->props, sizeof(World_Prop)*WORLD_PROP_MAX, offset);
|
||||
offset += sizeof(World_Prop)*WORLD_PROP_MAX;
|
||||
|
||||
FS_FileWrite(file, &world->hitboxCount, sizeof(U32), offset);
|
||||
offset += sizeof(U32);
|
||||
|
||||
FS_FileWrite(file, world->hitboxes, sizeof(AABB)*WORLD_HITBOX_MAX, offset);
|
||||
offset += sizeof(AABB)*WORLD_HITBOX_MAX;
|
||||
|
||||
FS_FileWrite(file, world->map, sizeof(U32)*WORLD_MAP_MAX, offset);
|
||||
offset += sizeof(U32)*WORLD_MAP_MAX;
|
||||
|
||||
FS_FileClose(file);
|
||||
printf("Saved world :)\n");
|
||||
}
|
||||
|
||||
void LoadWorld(M_Arena *arena, World *world) {
|
||||
printf("loading world\n");
|
||||
OS_Handle file = FS_FileOpen(S("world.sgdat"), FS_ACCESS_READ);
|
||||
U32 offset = 0;
|
||||
|
||||
world->tileTypes = M_ArenaPush(arena, World_Tile, .count=WORLD_TILE_TYPE_MAX);
|
||||
for(int i = 0; i < WORLD_TILE_TYPE_MAX; i++) {
|
||||
FS_FileRead(file, &world->tileTypes[i].tag.count, sizeof(S64), offset);
|
||||
offset += sizeof(S64);
|
||||
world->tileTypes[i].tag.data = M_ArenaPush(arena, U8, .count=world->tileTypes[i].tag.count);
|
||||
FS_FileRead(file, world->tileTypes[i].tag.data, world->tileTypes[i].tag.count, offset);
|
||||
offset += sizeof(U8) * world->tileTypes[i].tag.count;
|
||||
FS_FileRead(file, &world->tileTypes[i].rotation, sizeof(F32), offset);
|
||||
offset += sizeof(F32);
|
||||
}
|
||||
world->propTypes = M_ArenaPush(arena, World_PropType, .count=WORLD_PROP_TYPE_MAX);
|
||||
for(int i = 0; i < WORLD_PROP_TYPE_MAX; i++) {
|
||||
FS_FileRead(file, &world->propTypes[i].tag.count, sizeof(S64), offset);
|
||||
offset += sizeof(S64);
|
||||
world->propTypes[i].tag.data = M_ArenaPush(arena, U8, .count=world->propTypes[i].tag.count);
|
||||
FS_FileRead(file, world->propTypes[i].tag.data, world->propTypes[i].tag.count, offset);
|
||||
offset += sizeof(U8) * world->propTypes[i].tag.count;
|
||||
FS_FileRead(file, &world->propTypes[i].scale, sizeof(F32), offset);
|
||||
offset += sizeof(F32);
|
||||
}
|
||||
|
||||
FS_FileRead(file, &world->propCount, sizeof(U32), offset);
|
||||
offset += sizeof(U32);
|
||||
|
||||
world->props = M_ArenaPush(arena, World_Prop, .count=WORLD_PROP_MAX);
|
||||
FS_FileRead(file, world->props, sizeof(World_Prop)*WORLD_PROP_MAX, offset);
|
||||
offset += sizeof(World_Prop)*world->propCount;
|
||||
|
||||
FS_FileRead(file, &world->hitboxCount, sizeof(U32), offset);
|
||||
offset += sizeof(U32);
|
||||
|
||||
world->hitboxes = M_ArenaPush(arena, AABB, .count=WORLD_HITBOX_MAX);
|
||||
FS_FileRead(file, world->hitboxes, sizeof(AABB)*WORLD_HITBOX_MAX, offset);
|
||||
offset += sizeof(AABB)*world->hitboxCount;
|
||||
|
||||
world->map = M_ArenaPush(arena, U32, .count=WORLD_MAP_MAX);
|
||||
FS_FileRead(file, world->map, sizeof(U32)*WORLD_MAP_MAX, offset);
|
||||
offset += sizeof(U32)*WORLD_MAP_MAX;
|
||||
|
||||
FS_FileClose(file);
|
||||
printf("loaded world\n");
|
||||
}
|
||||
|
||||
void GenerateNavMesh(M_Arena *arena, World *world) {
|
||||
world->navMesh = M_ArenaPush(arena, NavMesh);
|
||||
world->navMesh->nodeCount = 0;
|
||||
for(int i = 0; i < WORLD_MAP_MAX; i++) {
|
||||
U32 x = (i % 96);
|
||||
U32 y = (i / 96);
|
||||
world->navMesh->nodes[world->navMesh->nodeCount].pos.x = x;
|
||||
world->navMesh->nodes[world->navMesh->nodeCount].pos.y = y;
|
||||
U32 cost = 20;
|
||||
if(Str8_Equal(world->tileTypes[world->map[i]].tag, S("path_middle"), STR8_EQUAL_IGNORE_CASE)) {
|
||||
cost = 10;
|
||||
}
|
||||
world->navMesh->nodes[world->navMesh->nodeCount].connectionCount = 0;
|
||||
for(int nx = -1; nx < 2; nx++){
|
||||
for(int ny = -1; ny < 2; ny++){
|
||||
if((nx==ny) && nx==0) {continue;}
|
||||
if(x+nx < 0 || x+nx > 95) {
|
||||
continue;
|
||||
}
|
||||
if(y+ny < 0 || y+ny > 49) {
|
||||
continue;
|
||||
}
|
||||
U32 index = x+nx + (y+ny)*96;
|
||||
U32 nCount = world->navMesh->nodeCount;
|
||||
world->navMesh->nodes[nCount].connections[world->navMesh->nodes[nCount].connectionCount].NodeIndex = index;
|
||||
world->navMesh->nodes[nCount].connections[world->navMesh->nodes[nCount].connectionCount].Cost = cost;
|
||||
world->navMesh->nodes[nCount].connectionCount++;
|
||||
}
|
||||
}
|
||||
world->navMesh->nodeCount++;
|
||||
}
|
||||
}
|
||||
54
code/game/map.h
Normal file
@@ -0,0 +1,54 @@
|
||||
#include "world.h"
|
||||
|
||||
U32 map[] = {
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,6,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,4,4,4,4,4,4,4,4,4,4,4,4,9,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,8,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,9,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,5,3,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,7,2,2,2,2,2,6,7,2,2,2,2,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,8,4,4,4,4,4,4,4,4,4,4,4,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,5,3,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,7,2,2,2,2,2,6,7,2,2,2,2,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,8,4,4,4,4,4,4,4,4,4,4,4,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,5,3,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,7,2,2,2,2,2,6,7,2,2,2,2,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,8,4,4,4,4,4,4,4,4,4,4,4,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,1,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
|
||||
};
|
||||
@@ -1,12 +1,12 @@
|
||||
#if !defined(LD_GAME_NAV_H_)
|
||||
#define LD_GAME_NAV_H_
|
||||
|
||||
#include "../core/types.h"
|
||||
#include "../core/macros.h"
|
||||
#include "core/types.h"
|
||||
#include "core/macros.h"
|
||||
|
||||
#define NAV_MAX_PATH 1024
|
||||
#define NAV_MAX_CONNECTIONS 8
|
||||
#define NAV_MAX_NODES 4096
|
||||
#define NAV_MAX_NODES 4800
|
||||
|
||||
typedef struct NavNode NavNode;
|
||||
|
||||
@@ -34,7 +34,6 @@ struct NavMesh{
|
||||
NavNode nodes[NAV_MAX_NODES];
|
||||
};
|
||||
|
||||
|
||||
function NavPath Nav_Path(NavMesh mesh, U32 start, U32 end);
|
||||
function NavPath Nav_Path(NavMesh *mesh, U32 start, U32 end);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,10 +1,55 @@
|
||||
#if !defined(LD_GAME_NPC_H_)
|
||||
#define LD_GAME_NPC_H_
|
||||
#include "aabb.h"
|
||||
#include "nav.h"
|
||||
#include "npc_look.h"
|
||||
#include "../core/types.h"
|
||||
|
||||
#define NPC_SPEED 1.0f
|
||||
|
||||
typedef enum NPC_ACTION NPC_ACTION;
|
||||
enum NPC_ACTION {
|
||||
// Waiting can be at any point of interest
|
||||
NPC_ACTION_WAITING,
|
||||
// Walking is when they are actively moving somewhere
|
||||
NPC_ACTION_WALKING,
|
||||
};
|
||||
|
||||
|
||||
typedef struct NPC NPC;
|
||||
struct NPC {
|
||||
//// Personal
|
||||
AABB collision;
|
||||
Str8 name;
|
||||
NPC_LOOK look;
|
||||
World_Area currentArea;
|
||||
|
||||
bool customPOI;
|
||||
U32 customPOICount;
|
||||
U32 npcPOI[16];
|
||||
|
||||
//// Actions
|
||||
NPC_ACTION mode;
|
||||
// How long they've been waiting
|
||||
F32 waitTime;
|
||||
// How long they will wait until changing action
|
||||
F32 maxWaitTime;
|
||||
|
||||
//// Navigation
|
||||
// The NPC's current path
|
||||
NavPath path;
|
||||
// Which node the NPC is on in the path
|
||||
U32 currentNavNode;
|
||||
// The current index of the NPC's current node
|
||||
U32 pathIndex;
|
||||
// Target navNode index the npc is walking to
|
||||
U32 targetNavNode;
|
||||
// How long the npc has been walking to the next index
|
||||
F32 walkTimer;
|
||||
|
||||
//// Knowledge
|
||||
// What the NPC knows about the bandit.
|
||||
NPC_LOOK banditKnowledge;
|
||||
};
|
||||
|
||||
#endif // LD_GAME_NPC_H_
|
||||
|
||||
82
code/game/npc_look.h
Normal file
@@ -0,0 +1,82 @@
|
||||
#if !defined(LD_GAME_NPC_LOOK_H_)
|
||||
#define LD_GAME_NPC_LOOK_H_
|
||||
|
||||
typedef struct NPC_LOOK NPC_LOOK;
|
||||
typedef enum NPC_HAIR_COLOUR NPC_HAIR_COLOUR;
|
||||
typedef enum NPC_JACKET_COLOUR NPC_JACKET_COLOUR;
|
||||
typedef enum NPC_JEANS_COLOUR NPC_JEANS_COLOUR;
|
||||
typedef enum NPC_EYE_COLOUR NPC_EYE_COLOUR;
|
||||
typedef enum NPC_SKIN_TONE NPC_SKIN_TONE;
|
||||
typedef enum NPC_HAT NPC_HAT;
|
||||
typedef enum NPC_FACIAL_FEATURE NPC_FACIAL_FEATURE;
|
||||
|
||||
enum NPC_HAIR_COLOUR {
|
||||
NPC_HAIR_COLOUR_UNKNOWN,
|
||||
NPC_HAIR_COLOUR_BLONDE,
|
||||
NPC_HAIR_COLOUR_BLACK,
|
||||
NPC_HAIR_COLOUR_BRUNETTE,
|
||||
NPC_HAIR_COLOUR_GIGNER,
|
||||
};
|
||||
|
||||
enum NPC_JACKET_COLOUR {
|
||||
NPC_JACKET_COLOUR_UNKNOWN,
|
||||
NPC_JACKET_COLOUR_TAN,
|
||||
NPC_JACKET_COLOUR_BLUE,
|
||||
NPC_JACKET_COLOUR_WHITE,
|
||||
};
|
||||
|
||||
enum NPC_JEANS_COLOUR {
|
||||
NPC_JEANS_COLOUR_UNKNOWN,
|
||||
NPC_JEANS_COLOUR_TAN,
|
||||
NPC_JEANS_COLOUR_BLACK,
|
||||
NPC_JEANS_COLOUR_BLUE,
|
||||
NPC_JEANS_COLOUR_WHITE,
|
||||
};
|
||||
|
||||
enum NPC_EYE_COLOUR {
|
||||
NPC_EYE_COLOUR_UNKNOWN,
|
||||
NPC_EYE_COLOUR_GREEN,
|
||||
NPC_EYE_COLOUR_BLUE,
|
||||
NPC_EYE_COLOUR_BROWN,
|
||||
};
|
||||
|
||||
enum NPC_SHOE_COLOUR {
|
||||
NPC_SHOE_COLOUR_UNKNOWN,
|
||||
NPC_SHOE_COLOUR_BLACK,
|
||||
NPC_SHOE_COLOUR_BROWN,
|
||||
NPC_SHOE_COLOUR_WHITE,
|
||||
NPC_SHOE_COLOUR_NAVY,
|
||||
};
|
||||
|
||||
enum NPC_SKIN_TONE {
|
||||
NPC_SKIN_TONE_UNKNOWN,
|
||||
NPC_SKIN_TONE_WHITE,
|
||||
NPC_SKIN_TONE_BLACK,
|
||||
};
|
||||
|
||||
enum NPC_FACIAL_FEATURE {
|
||||
NPC_FACIAL_FEATURE_UNKNOWN,
|
||||
NPC_FACIAL_FEATURE_BEARD,
|
||||
NPC_FACIAL_FEATURE_MUSTACHE,
|
||||
NPC_FACIAL_FEATURE_GLASSES,
|
||||
};
|
||||
|
||||
enum NPC_HAT {
|
||||
NPC_HAT_UNKNOWN,
|
||||
NPC_HAT_TEN_GALLON,
|
||||
NPC_TOP_HAT,
|
||||
NPC_FLAT_CAP,
|
||||
NPC_NONE,
|
||||
};
|
||||
|
||||
struct NPC_LOOK {
|
||||
NPC_HAIR_COLOUR hair;
|
||||
NPC_JACKET_COLOUR jacket;
|
||||
NPC_JEANS_COLOUR jeans;
|
||||
NPC_EYE_COLOUR eyes;
|
||||
NPC_SKIN_TONE skinTone;
|
||||
NPC_HAT hat;
|
||||
NPC_FACIAL_FEATURE facialFeature;
|
||||
};
|
||||
|
||||
#endif // LD_GAME_NPC_LOOK_H_
|
||||
@@ -1,11 +1,38 @@
|
||||
#include "core/types.h"
|
||||
#include <SDL3/SDL_events.h>
|
||||
#if !defined(LD_GAME_PLAYER_H_)
|
||||
#define LD_GAME_PLAYER_H_
|
||||
|
||||
#include "../core/types.h"
|
||||
#include "../core/macros.h"
|
||||
|
||||
#include <SDL3/SDL_events.h>
|
||||
|
||||
#define PLAYER_SPEED 10.0f
|
||||
#define PLAYER_RELOAD_TIME 1.5f
|
||||
#define PLAYER_BULLET_COUNT 6
|
||||
|
||||
typedef struct ControlState ControlState;
|
||||
struct ControlState {
|
||||
bool rightDown;
|
||||
bool leftDown;
|
||||
bool upDown;
|
||||
bool downDown;
|
||||
bool shot;
|
||||
};
|
||||
|
||||
typedef struct Player Player;
|
||||
struct Player
|
||||
{
|
||||
World *world;
|
||||
V2f pos;
|
||||
World_Area currentArea;
|
||||
U32 bulletsLoaded;
|
||||
ControlState controls;
|
||||
V2f shotPos;
|
||||
|
||||
F32 reloadTimer;
|
||||
};
|
||||
|
||||
function void PlayerUpdate(SDL_Event *event, Player *player);
|
||||
function void PlayerInput(SDL_Event *event, Player *player);
|
||||
function void PlayerUpdate(F32 delta, Player *player);
|
||||
|
||||
#endif // LD_GAME_PLAYER_H_
|
||||
|
||||
@@ -1,10 +1,88 @@
|
||||
#if !defined(LD_GAME_WORLD_H_)
|
||||
#define LD_GAME_WORLD_H_
|
||||
|
||||
typedef struct World World;
|
||||
struct World {
|
||||
NPC[128] npcs;
|
||||
u32 npcCount;
|
||||
#include "../core/math.h"
|
||||
|
||||
#define WORLD_HITBOX_MAX 4096
|
||||
#define WORLD_PROP_MAX 2048
|
||||
#define WORLD_PROP_TYPE_MAX 64
|
||||
#define WORLD_TILE_TYPE_MAX 64
|
||||
#define WORLD_MAP_MAX 4800
|
||||
|
||||
// Areas are which
|
||||
typedef U32 World_Area;
|
||||
enum World_Area
|
||||
{
|
||||
WORLD_AREA_OUTSIDE = (1 << 0),
|
||||
WORLD_AREA_SALOON = (1 << 1),
|
||||
};
|
||||
|
||||
typedef struct World_Tile World_Tile;
|
||||
struct World_Tile {
|
||||
Str8 tag;
|
||||
F32 rotation;
|
||||
};
|
||||
|
||||
typedef struct World_PropType World_PropType;
|
||||
struct World_PropType {
|
||||
Str8 tag;
|
||||
F32 scale;
|
||||
};
|
||||
|
||||
typedef struct World_Prop World_Prop;
|
||||
struct World_Prop
|
||||
{
|
||||
U32 propType;
|
||||
World_Area area;
|
||||
V2f pos;
|
||||
};
|
||||
|
||||
typedef struct World World;
|
||||
#include "player.h"
|
||||
#include "npc.h"
|
||||
#include "bandit.h"
|
||||
|
||||
struct World {
|
||||
//// Utils
|
||||
M_Arena *arena;
|
||||
//// Static stuff
|
||||
NavMesh *navMesh;
|
||||
Random random;
|
||||
V2f mouseProjected;
|
||||
//// Loaded from world file
|
||||
World_Tile *tileTypes;
|
||||
World_PropType *propTypes;
|
||||
World_Prop *props;
|
||||
AABB *hitboxes;
|
||||
U32 *map;
|
||||
|
||||
U32 propCount;
|
||||
U32 hitboxCount;
|
||||
|
||||
//// Player
|
||||
Player player;
|
||||
|
||||
//// NPCs
|
||||
U32 npcCount;
|
||||
NPC npcs[128];
|
||||
|
||||
////Bandit
|
||||
// The bandit the player is after.
|
||||
Bandit bandit;
|
||||
|
||||
// NPC points of interest, places to walk to.
|
||||
U32 npcPOI[256];
|
||||
};
|
||||
|
||||
function void UpdateWorld(F32 delta, World *world);
|
||||
function void RenderWorld(World *world, D_Context *drawContext);
|
||||
function void ProcessEvents(SDL_Event *event, World *world);
|
||||
function void UpdateNPCs(F32 delta, World *world);
|
||||
function void UpdateNPC(F32 delta, NPC *npc, World *world);
|
||||
function void UpdateBandit(F32 delta, Bandit *bandit, World *world);
|
||||
|
||||
function void LoadWorld(M_Arena *arena, World *world);
|
||||
function void SaveWorld(M_Arena *arena, World *world);
|
||||
function void GenerateNavMesh(M_Arena *arena, World *world);
|
||||
|
||||
#endif // LD_GAME_WORLD_H_
|
||||
|
||||
@@ -3,3 +3,19 @@
|
||||
#elif OS_LINUX
|
||||
#include "impl/linux/core.c"
|
||||
#endif
|
||||
|
||||
|
||||
Str8 FS_ReadEntireFile(M_Arena *arena, Str8 path) {
|
||||
Str8 result = { 0 };
|
||||
|
||||
OS_Handle file = FS_FileOpen(path, FS_ACCESS_READ);
|
||||
if (file.v[0]) {
|
||||
result.count = FS_FileSize(file);
|
||||
result.data = M_ArenaPush(arena, U8, .count = result.count);
|
||||
|
||||
FS_FileRead(file, result.data, result.count, 0);
|
||||
FS_FileClose(file);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
#if !defined(LD_OS_CORE_H_)
|
||||
#define LD_OS_CORE_H_
|
||||
|
||||
typedef struct OS_Handle OS_Handle;
|
||||
struct OS_Handle {
|
||||
U64 v[1];
|
||||
};
|
||||
|
||||
// Virtual memory
|
||||
|
||||
function U64 VM_PageSize();
|
||||
@@ -11,4 +16,6 @@ function B32 VM_Commit(void *base, U64 size);
|
||||
function void VM_Decommit(void *base, U64 size);
|
||||
function void VM_Release(void *base, U64 size);
|
||||
|
||||
#include "filesystem.h"
|
||||
|
||||
#endif // LD_OS_CORE_H_
|
||||
|
||||
59
code/os/filesystem.h
Normal file
@@ -0,0 +1,59 @@
|
||||
#if !defined(LD_OS_FILESYSTEM_H_)
|
||||
#define LD_OS_FILESYSTEM_H_
|
||||
|
||||
typedef U32 FS_AccessFlags;
|
||||
enum {
|
||||
FS_ACCESS_READ = (1 << 0),
|
||||
FS_ACCESS_WRITE = (1 << 1)
|
||||
};
|
||||
|
||||
function OS_Handle FS_FileOpen(Str8 path, FS_AccessFlags access);
|
||||
function void FS_FileClose(OS_Handle file);
|
||||
|
||||
function void FS_FileRead(OS_Handle file, void *ptr, U64 size, U64 offset);
|
||||
function void FS_FileWrite(OS_Handle file, void *ptr, U64 size, U64 offset);
|
||||
|
||||
function U64 FS_FileSize(OS_Handle file);
|
||||
|
||||
typedef U32 FS_EntryType;
|
||||
enum {
|
||||
FS_ENTRY_TYPE_FILE = 0,
|
||||
FS_ENTRY_TYPE_DIR
|
||||
};
|
||||
|
||||
typedef struct FS_Entry FS_Entry;
|
||||
struct FS_Entry {
|
||||
FS_Entry *next;
|
||||
|
||||
FS_EntryType type;
|
||||
|
||||
Str8 path;
|
||||
Str8 basename;
|
||||
|
||||
U64 time;
|
||||
U64 size;
|
||||
};
|
||||
|
||||
typedef struct FS_List FS_List;
|
||||
struct FS_List {
|
||||
FS_Entry *first;
|
||||
FS_Entry *last;
|
||||
|
||||
U32 count;
|
||||
};
|
||||
|
||||
function FS_List FS_PathList(M_Arena *arena, Str8 path);
|
||||
|
||||
function Str8 FS_ReadEntireFile(M_Arena *arena, Str8 path);
|
||||
|
||||
typedef U32 FS_SystemPathType;
|
||||
enum {
|
||||
FS_SYSTEM_PATH_EXE = 0,
|
||||
FS_SYSTEM_PATH_WORKING,
|
||||
FS_SYSTEM_PATH_USER,
|
||||
FS_SYSTEM_PATH_TEMP
|
||||
};
|
||||
|
||||
function Str8 FS_SystemPath(M_Arena *arena, FS_SystemPathType path);
|
||||
|
||||
#endif // LD_OS_FILESYSTEM_H_
|
||||
@@ -28,4 +28,4 @@ void VM_Release(void *base, U64 size) {
|
||||
munmap(base, size);
|
||||
}
|
||||
|
||||
|
||||
#include "filesystem.c"
|
||||
|
||||
215
code/os/impl/linux/filesystem.c
Normal file
@@ -0,0 +1,215 @@
|
||||
OS_Handle FS_FileOpen(Str8 path, FS_AccessFlags access) {
|
||||
OS_Handle result = { 0 };
|
||||
|
||||
M_TempScope(0, 0) {
|
||||
Str8 zpath = Str8_Copy(temp.arena, path);
|
||||
|
||||
int mode = 0644;
|
||||
int flags = 0;
|
||||
if (access & FS_ACCESS_READ) {
|
||||
if (access & FS_ACCESS_WRITE) {
|
||||
flags = O_RDWR | O_CREAT;
|
||||
}
|
||||
else {
|
||||
flags = O_RDONLY;
|
||||
}
|
||||
}
|
||||
else if (access & FS_ACCESS_WRITE) {
|
||||
flags = O_WRONLY | O_CREAT;
|
||||
}
|
||||
|
||||
|
||||
int fd = open((const char *) zpath.data, flags, mode);
|
||||
result.v[0] = (fd > 0) ? (U64) fd : 0;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
void FS_FileClose(OS_Handle file) {
|
||||
int fd = cast(int) file.v[0];
|
||||
if (fd > 0) {
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
|
||||
void FS_FileRead(OS_Handle file, void *ptr, U64 size, U64 offset) {
|
||||
int fd = cast(int) file.v[0];
|
||||
if (fd > 0) {
|
||||
U8 *buffer = ptr;
|
||||
U64 current = offset;
|
||||
U64 remainder = size;
|
||||
|
||||
while (remainder != 0) {
|
||||
S64 nread = pread(fd, buffer, remainder, current);
|
||||
if (nread <= 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
buffer += nread;
|
||||
current += nread;
|
||||
remainder -= nread;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void FS_FileWrite(OS_Handle file, void *ptr, U64 size, U64 offset) {
|
||||
int fd = cast(int) file.v[0];
|
||||
if (fd > 0) {
|
||||
U8 *buffer = ptr;
|
||||
U64 current = offset;
|
||||
U64 remainder = size;
|
||||
|
||||
while (remainder != 0) {
|
||||
S64 nwritten = pwrite(fd, buffer, remainder, current);
|
||||
if (nwritten <= 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
buffer += nwritten;
|
||||
current += nwritten;
|
||||
remainder -= nwritten;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
U64 FS_FileSize(OS_Handle file) {
|
||||
U64 result = 0;
|
||||
|
||||
int fd = cast(int) file.v[0];
|
||||
if (fd > 0) {
|
||||
struct stat sb;
|
||||
fstat(fd, &sb);
|
||||
|
||||
result = sb.st_size;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
FS_List FS_PathList(M_Arena *arena, Str8 path) {
|
||||
FS_List result = { 0 };
|
||||
|
||||
M_TempScope(1, &arena) {
|
||||
Str8 zpath = Str8_Copy(temp.arena, path);
|
||||
|
||||
DIR *dir = opendir((const char *) zpath.data);
|
||||
|
||||
struct dirent *ent = readdir(dir);
|
||||
while (ent != 0) {
|
||||
if (ent->d_name[0] != '.') {
|
||||
FS_Entry *entry = M_ArenaPush(arena, FS_Entry);
|
||||
|
||||
entry->basename = Str8_Copy(arena, Sz(ent->d_name));
|
||||
entry->path = Sf(arena, "%.*s/%.*s", Sv(path), Sv(entry->basename));
|
||||
|
||||
struct stat sb;
|
||||
stat((const char *) entry->path.data, &sb);
|
||||
|
||||
entry->type = ((sb.st_mode & S_IFMT) == S_IFDIR) ? FS_ENTRY_TYPE_DIR : FS_ENTRY_TYPE_FILE;
|
||||
entry->time = (sb.st_mtim.tv_sec * 1e9) + sb.st_mtim.tv_nsec;
|
||||
entry->size = sb.st_size;
|
||||
|
||||
SLL_Enqueue(result.first, result.last, entry);
|
||||
result.count += 1;
|
||||
}
|
||||
|
||||
ent = readdir(dir);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Str8 FS_SystemPath(M_Arena *arena, FS_SystemPathType path) {
|
||||
Str8 result = { 0 };
|
||||
|
||||
switch (path) {
|
||||
case FS_SYSTEM_PATH_EXE: {
|
||||
U64 offset = M_ArenaOffset(arena);
|
||||
|
||||
Str8 buffer;
|
||||
buffer.count = KB(1);
|
||||
buffer.data = M_ArenaPush(arena, U8, .count = buffer.count);
|
||||
|
||||
ssize_t nwritten = 0;
|
||||
for (;;) {
|
||||
nwritten = readlink("/proc/self/exe", cast(char *) buffer.data, buffer.count);
|
||||
if (nwritten < buffer.count) {
|
||||
break;
|
||||
}
|
||||
|
||||
M_ArenaPop(arena, offset);
|
||||
|
||||
buffer.count *= 2;
|
||||
buffer.data = M_ArenaPush(arena, U8, .count = buffer.count);
|
||||
}
|
||||
|
||||
if (nwritten > 0) {
|
||||
// Walk back until the prevous slash, then null-terminate there
|
||||
while (buffer.data[nwritten] != '/') { nwritten -= 1; }
|
||||
buffer.data[nwritten] = 0;
|
||||
|
||||
M_ArenaPopSize(arena, buffer.count - nwritten - 1);
|
||||
|
||||
result.count = nwritten;
|
||||
result.data = buffer.data;
|
||||
}
|
||||
else {
|
||||
M_ArenaPop(arena, offset);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case FS_SYSTEM_PATH_WORKING: {
|
||||
U64 offset = M_ArenaOffset(arena);
|
||||
|
||||
Str8 buffer;
|
||||
buffer.count = KB(1);
|
||||
buffer.data = M_ArenaPush(arena, U8, .count = buffer.count);
|
||||
|
||||
char *wd;
|
||||
|
||||
for (;;) {
|
||||
wd = getcwd((char *) buffer.data, buffer.count);
|
||||
if (wd != 0) {
|
||||
break;
|
||||
}
|
||||
else if (errno != ERANGE) {
|
||||
break;
|
||||
}
|
||||
|
||||
M_ArenaPop(arena, offset);
|
||||
|
||||
buffer.count *= 2;
|
||||
buffer.data = M_ArenaPush(arena, U8, .count = buffer.count);
|
||||
}
|
||||
|
||||
if (wd) {
|
||||
result = Sz(wd);
|
||||
M_ArenaPopSize(arena, buffer.count - result.count - 1);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case FS_SYSTEM_PATH_USER: {
|
||||
char *home = getenv("HOME");
|
||||
if (home) {
|
||||
result = Sf(arena, "%s/.local/share", home);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case FS_SYSTEM_PATH_TEMP: {
|
||||
char *temp = getenv("TEMP");
|
||||
if (temp) {
|
||||
result = Str8_Copy(arena, Sz(temp));
|
||||
}
|
||||
else {
|
||||
result = Str8_Copy(arena, S("/tmp"));
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -1,3 +1,32 @@
|
||||
// Windows utilities
|
||||
|
||||
internal Str8 Win32_WideStr(M_Arena *arena, Str8 v) {
|
||||
Str8 result = { 0 };
|
||||
|
||||
S32 nchars = MultiByteToWideChar(CP_UTF8, 0, (LPCCH) v.data, (int) v.count, 0, 0) + 1;
|
||||
U64 nbytes = nchars << 1;
|
||||
|
||||
result.count = nbytes - 2; // -2 because we don't include the null-terminator in the count
|
||||
result.data = M_ArenaPush(arena, U8, .count = nbytes);
|
||||
|
||||
MultiByteToWideChar(CP_UTF8, 0, (LPCCH) v.data, (int) v.count, (LPWSTR) result.data, nchars);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
internal Str8 Win32_MultiByteStr(M_Arena *arena, LPWSTR wstr) {
|
||||
Str8 result = { 0 };
|
||||
|
||||
S32 nbytes = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, 0, 0, 0, 0);
|
||||
|
||||
result.count = nbytes - 1;
|
||||
result.data = M_ArenaPush(arena, U8, .count = nbytes);
|
||||
|
||||
WideCharToMultiByte(CP_UTF8, 0, wstr, -1, (LPSTR) result.data, nbytes, 0, 0);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Virtual memory
|
||||
|
||||
U64 VM_PageSize() {
|
||||
@@ -36,3 +65,4 @@ void VM_Release(void *base, U64 size) {
|
||||
VirtualFree(base, 0, MEM_RELEASE);
|
||||
}
|
||||
|
||||
#include "filesystem.c"
|
||||
|
||||
209
code/os/impl/windows/filesystem.c
Normal file
@@ -0,0 +1,209 @@
|
||||
OS_Handle FS_FileOpen(Str8 path, FS_AccessFlags access) {
|
||||
OS_Handle result = { 0 };
|
||||
|
||||
M_TempScope(0, 0) {
|
||||
Str8 wpath = Win32_WideStr(temp.arena, path);
|
||||
|
||||
DWORD dwAccess = 0;
|
||||
DWORD dwShare = FILE_SHARE_READ;
|
||||
DWORD dwCreate = OPEN_EXISTING;
|
||||
|
||||
if (access & FS_ACCESS_WRITE) {
|
||||
dwCreate = CREATE_ALWAYS;
|
||||
dwAccess = GENERIC_WRITE;
|
||||
}
|
||||
|
||||
if (access & FS_ACCESS_READ) {
|
||||
dwAccess |= GENERIC_READ;
|
||||
}
|
||||
|
||||
HANDLE hFile = CreateFileW((LPCWSTR) wpath.data, dwAccess, dwShare, 0, dwCreate, 0, 0);
|
||||
|
||||
result.v[0] = (hFile == INVALID_HANDLE_VALUE) ? 0 : ((U64) hFile);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
void FS_FileClose(OS_Handle file) {
|
||||
HANDLE hFile = cast(HANDLE) file.v[0];
|
||||
if (hFile) {
|
||||
CloseHandle(hFile);
|
||||
}
|
||||
}
|
||||
|
||||
void FS_FileRead(OS_Handle file, void *ptr, U64 size, U64 offset) {
|
||||
HANDLE hFile = cast(HANDLE) file.v[0];
|
||||
if (hFile) {
|
||||
U8 *buffer = ptr;
|
||||
U64 current = offset;
|
||||
U64 remainder = size;
|
||||
|
||||
while (remainder != 0) {
|
||||
OVERLAPPED overlapped = { 0 };
|
||||
overlapped.Offset = cast(DWORD) (current & 0xFFFFFFFF);
|
||||
overlapped.OffsetHigh = cast(DWORD) ((current >> 32) & 0xFFFFFFFF);
|
||||
|
||||
DWORD toread = (remainder > U32_MAX) ? U32_MAX : (DWORD) remainder;
|
||||
DWORD nread = 0;
|
||||
|
||||
if (!ReadFile(hFile, buffer, toread, &nread, &overlapped)) {
|
||||
break;
|
||||
}
|
||||
|
||||
buffer += nread;
|
||||
current += nread;
|
||||
remainder -= nread;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void FS_FileWrite(OS_Handle file, void *ptr, U64 size, U64 offset) {
|
||||
HANDLE hFile = cast(HANDLE) file.v[0];
|
||||
if (hFile) {
|
||||
U8 *buffer = ptr;
|
||||
U64 current = offset;
|
||||
U64 remainder = size;
|
||||
|
||||
while (remainder != 0) {
|
||||
OVERLAPPED overlapped = { 0 };
|
||||
overlapped.Offset = cast(DWORD) (current & 0xFFFFFFFF);
|
||||
overlapped.OffsetHigh = cast(DWORD) ((current >> 32) & 0xFFFFFFFF);
|
||||
|
||||
DWORD towrite = (remainder > U32_MAX) ? U32_MAX : (DWORD) remainder;
|
||||
DWORD nwritten = 0;
|
||||
|
||||
if (!WriteFile(hFile, buffer, towrite, &nwritten, &overlapped)) {
|
||||
break;
|
||||
}
|
||||
|
||||
buffer += nwritten;
|
||||
current += nwritten;
|
||||
remainder -= nwritten;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
U64 FS_FileSize(OS_Handle file) {
|
||||
U64 result = 0;
|
||||
HANDLE hFile = cast(HANDLE) file.v[0];
|
||||
if (hFile) {
|
||||
DWORD dwLow, dwHigh;
|
||||
dwLow = GetFileSize(hFile, &dwHigh);
|
||||
|
||||
result = ((U64) dwHigh << 32) | ((U64) dwLow);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
FS_List FS_PathList(M_Arena *arena, Str8 path) {
|
||||
FS_List result = { 0 };
|
||||
|
||||
M_TempScope(1, &arena) {
|
||||
Str8 wpath = Win32_WideStr(temp.arena, Sf(temp.arena, "%.*s\\*", Sv(path)));
|
||||
|
||||
WIN32_FIND_DATAW find = { 0 };
|
||||
HANDLE hFind = FindFirstFileW((LPCWSTR) wpath.data, &find);
|
||||
|
||||
while (hFind != INVALID_HANDLE_VALUE) {
|
||||
if (find.cFileName[0] != L'.') {
|
||||
FS_Entry *entry = M_ArenaPush(arena, FS_Entry);
|
||||
|
||||
entry->type = (find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? FS_ENTRY_TYPE_DIR : FS_ENTRY_TYPE_FILE;
|
||||
|
||||
entry->basename = Win32_MultiByteStr(arena, find.cFileName);
|
||||
entry->path = Sf(arena, "%.*s/%.*s", Sv(path), Sv(entry->basename));
|
||||
|
||||
FILETIME *wt = &find.ftLastWriteTime;
|
||||
|
||||
entry->time = ((U64) wt->dwHighDateTime << 32) | ((U64) wt->dwLowDateTime);
|
||||
entry->size = ((U64) find.nFileSizeHigh << 32) | ((U64) find.nFileSizeLow);
|
||||
|
||||
SLL_Enqueue(result.first, result.last, entry);
|
||||
|
||||
result.count += 1;
|
||||
}
|
||||
|
||||
if (!FindNextFileW(hFind, &find)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Str8 FS_SystemPath(M_Arena *arena, FS_SystemPathType path) {
|
||||
Str8 result = { 0 };
|
||||
|
||||
M_TempScope(1, &arena) {
|
||||
switch (path) {
|
||||
case FS_SYSTEM_PATH_EXE: {
|
||||
U64 offset = M_ArenaOffset(temp.arena);
|
||||
|
||||
DWORD err, nchars;
|
||||
|
||||
DWORD nSize = 1024;
|
||||
LPWSTR lpFilename = M_ArenaPush(temp.arena, WCHAR, .count = nSize, .flags = M_ARENA_NO_ZERO);
|
||||
|
||||
for (;;) {
|
||||
nchars = GetModuleFileNameW(0, lpFilename, nSize);
|
||||
|
||||
err = GetLastError();
|
||||
if (err != ERROR_INSUFFICIENT_BUFFER) {
|
||||
break;
|
||||
}
|
||||
|
||||
M_ArenaPop(temp.arena, offset);
|
||||
|
||||
nSize *= 2;
|
||||
lpFilename = M_ArenaPush(temp.arena, WCHAR, .count = nSize, .flags = M_ARENA_NO_ZERO);
|
||||
}
|
||||
|
||||
if (err == ERROR_SUCCESS) {
|
||||
while (lpFilename[nchars] != '\\') { nchars -= 1; }
|
||||
lpFilename[nchars] = 0;
|
||||
|
||||
result = Win32_MultiByteStr(arena, lpFilename);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case FS_SYSTEM_PATH_WORKING: {
|
||||
DWORD nSize = GetCurrentDirectoryW(0, 0);
|
||||
|
||||
if (nSize != 0) {
|
||||
LPWSTR lpBuffer = M_ArenaPush(temp.arena, WCHAR, .count = nSize, .flags = M_ARENA_NO_ZERO);
|
||||
GetCurrentDirectoryW(nSize, lpBuffer);
|
||||
|
||||
result = Win32_MultiByteStr(arena, lpBuffer);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case FS_SYSTEM_PATH_USER: {
|
||||
LPWSTR wpath;
|
||||
HRESULT hResult = SHGetKnownFolderPath(&FOLDERID_RoamingAppData, 0, 0, &wpath);
|
||||
if (hResult == S_OK) {
|
||||
result = Win32_MultiByteStr(arena, wpath);
|
||||
CoTaskMemFree(wpath);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case FS_SYSTEM_PATH_TEMP: {
|
||||
DWORD nSize = MAX_PATH + 1;
|
||||
LPWSTR lpBuffer = M_ArenaPush(temp.arena, WCHAR, .count = nSize);
|
||||
|
||||
DWORD nchars = GetTempPathW(nSize, lpBuffer);
|
||||
if (nchars != 0) {
|
||||
lpBuffer[nchars - 1] = 0; // Strip trailing backslash
|
||||
result = Win32_MultiByteStr(arena, lpBuffer);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -180,6 +180,7 @@ bool Vk_Setup(SDL_Window *window) {
|
||||
features12.storageBuffer8BitAccess = VK_TRUE;
|
||||
features12.uniformAndStorageBuffer8BitAccess = VK_TRUE;
|
||||
features12.descriptorIndexing = VK_TRUE;
|
||||
features12.runtimeDescriptorArray = VK_TRUE;
|
||||
|
||||
// @Todo: we will probably need to enable some of the 'nonuniform' indexing features
|
||||
|
||||
@@ -330,6 +331,7 @@ bool Vk_Setup(SDL_Window *window) {
|
||||
|
||||
VkResult err = vk.CreateCommandPool(vk.device, &pool, 0, &frame->pool);
|
||||
|
||||
// Base command buffer
|
||||
VkCommandBufferAllocateInfo alloc = { 0 };
|
||||
alloc.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
|
||||
alloc.commandBufferCount = 1;
|
||||
@@ -338,21 +340,68 @@ bool Vk_Setup(SDL_Window *window) {
|
||||
|
||||
vk.AllocateCommandBuffers(vk.device, &alloc, &frame->cmd);
|
||||
|
||||
VkSemaphoreCreateInfo semaphore = { 0 };
|
||||
semaphore.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
|
||||
// Scratch command buffers
|
||||
VkCommandBuffer scratch[VK_NUM_SCRATCH];
|
||||
alloc.commandBufferCount = VK_NUM_SCRATCH;
|
||||
|
||||
err = Min(vk.CreateSemaphore(vk.device, &semaphore, 0, &frame->acquire), err);
|
||||
err = Min(vk.CreateSemaphore(vk.device, &semaphore, 0, &frame->complete), err);
|
||||
vk.AllocateCommandBuffers(vk.device, &alloc, scratch);
|
||||
|
||||
VkFenceCreateInfo fence = { 0 };
|
||||
fence.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
|
||||
fence.flags = VK_FENCE_CREATE_SIGNALED_BIT;
|
||||
|
||||
for (U32 s = 0; s < VK_NUM_SCRATCH; ++s) {
|
||||
frame->scratch[s].handle = scratch[s];
|
||||
err = Min(vk.CreateFence(vk.device, &fence, 0, &frame->scratch[s].fence), err);
|
||||
}
|
||||
|
||||
// Descriptor pool
|
||||
VkDescriptorPoolSize pool_sizes[2] = { 0 };
|
||||
|
||||
pool_sizes[0].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
||||
pool_sizes[0].descriptorCount = 2048;
|
||||
|
||||
pool_sizes[1].type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
|
||||
pool_sizes[1].descriptorCount = 2048;
|
||||
|
||||
VkDescriptorPoolCreateInfo descriptor_pool = { 0 };
|
||||
descriptor_pool.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
|
||||
descriptor_pool.maxSets = 1024;
|
||||
descriptor_pool.poolSizeCount = 2;
|
||||
descriptor_pool.pPoolSizes = pool_sizes;
|
||||
|
||||
vk.CreateDescriptorPool(vk.device, &descriptor_pool, 0, &frame->descriptors);
|
||||
|
||||
// rect buffer
|
||||
//
|
||||
|
||||
Vk_Buffer *rbo = &frame->rbo;
|
||||
rbo->usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
|
||||
rbo->size = D_MAX_RECTS * sizeof(D_Rect);
|
||||
rbo->host_visible = true;
|
||||
|
||||
Vk_BufferCreate(rbo);
|
||||
|
||||
VkSemaphoreCreateInfo semaphore = { 0 };
|
||||
semaphore.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;
|
||||
|
||||
err = Min(vk.CreateSemaphore(vk.device, &semaphore, 0, &frame->acquire), err);
|
||||
err = Min(vk.CreateSemaphore(vk.device, &semaphore, 0, &frame->complete), err);
|
||||
err = Min(vk.CreateFence(vk.device, &fence, 0, &frame->fence), err);
|
||||
|
||||
vk.err = err;
|
||||
}
|
||||
|
||||
{
|
||||
VkSamplerCreateInfo create_info = { 0 };
|
||||
create_info.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
|
||||
create_info.magFilter = VK_FILTER_NEAREST;
|
||||
create_info.minFilter = VK_FILTER_NEAREST;
|
||||
create_info.mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST;
|
||||
|
||||
vk.CreateSampler(vk.device, &create_info, 0, &vk.sampler);
|
||||
}
|
||||
|
||||
result = (vk.err == VK_SUCCESS);
|
||||
return result;
|
||||
}
|
||||
@@ -365,6 +414,7 @@ Vk_Frame *Vk_FrameBegin(SDL_Window *window) {
|
||||
|
||||
vk.ResetFences(vk.device, 1, &frame->fence);
|
||||
vk.ResetCommandPool(vk.device, frame->pool, 0);
|
||||
vk.ResetDescriptorPool(vk.device, frame->descriptors, 0);
|
||||
|
||||
VkResult res = vk.AcquireNextImageKHR(vk.device, vk.swapchain.handle, UINT64_MAX, frame->acquire, 0, &frame->image);
|
||||
(void) res; // @Todo: check res to see if swapchain is out of date and rebuild
|
||||
@@ -374,12 +424,54 @@ Vk_Frame *Vk_FrameBegin(SDL_Window *window) {
|
||||
|
||||
vk.BeginCommandBuffer(frame->cmd, &begin_info);
|
||||
|
||||
VkImageMemoryBarrier2 colour_optimal = { 0 };
|
||||
colour_optimal.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2;
|
||||
colour_optimal.srcStageMask = VK_PIPELINE_STAGE_2_NONE;
|
||||
colour_optimal.srcAccessMask = VK_ACCESS_2_NONE;
|
||||
colour_optimal.dstStageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT;
|
||||
colour_optimal.dstAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT;
|
||||
colour_optimal.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
||||
colour_optimal.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
||||
colour_optimal.image = vk.swapchain.images[frame->image];
|
||||
|
||||
colour_optimal.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||
colour_optimal.subresourceRange.layerCount = 1;
|
||||
colour_optimal.subresourceRange.levelCount = 1;
|
||||
|
||||
VkDependencyInfo colour_barrier = { 0 };
|
||||
colour_barrier.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO;
|
||||
colour_barrier.imageMemoryBarrierCount = 1;
|
||||
colour_barrier.pImageMemoryBarriers = &colour_optimal;
|
||||
|
||||
vk.CmdPipelineBarrier2(frame->cmd, &colour_barrier);
|
||||
|
||||
return frame;
|
||||
}
|
||||
|
||||
void Vk_FrameEnd() {
|
||||
Vk_Frame *frame = &vk.frames[vk.n_frames % vk.in_flight];
|
||||
|
||||
VkImageMemoryBarrier2 present_src = { 0 };
|
||||
present_src.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2;
|
||||
present_src.srcStageMask = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT;
|
||||
present_src.srcAccessMask = VK_ACCESS_2_COLOR_ATTACHMENT_WRITE_BIT;
|
||||
present_src.dstStageMask = VK_PIPELINE_STAGE_2_NONE;
|
||||
present_src.dstAccessMask = VK_ACCESS_2_NONE;
|
||||
present_src.oldLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
||||
present_src.newLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
|
||||
present_src.image = vk.swapchain.images[frame->image];
|
||||
|
||||
present_src.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||
present_src.subresourceRange.layerCount = 1;
|
||||
present_src.subresourceRange.levelCount = 1;
|
||||
|
||||
VkDependencyInfo to_present = { 0 };
|
||||
to_present.sType = VK_STRUCTURE_TYPE_DEPENDENCY_INFO;
|
||||
to_present.imageMemoryBarrierCount = 1;
|
||||
to_present.pImageMemoryBarriers = &present_src;
|
||||
|
||||
vk.CmdPipelineBarrier2(frame->cmd, &to_present);
|
||||
|
||||
vk.EndCommandBuffer(frame->cmd);
|
||||
|
||||
VkPipelineStageFlags stage = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
||||
@@ -408,3 +500,228 @@ void Vk_FrameEnd() {
|
||||
|
||||
vk.n_frames += 1;
|
||||
}
|
||||
|
||||
Vk_CommandBuffer *Vk_CommandBufferPush() {
|
||||
Vk_Frame *frame = &vk.frames[vk.n_frames % vk.in_flight];
|
||||
|
||||
// If this scratch buffer is still in use wait for it to finish, this is _bad_ but we
|
||||
// shouldn't hit this
|
||||
U32 idx = frame->next_scratch & (VK_NUM_SCRATCH - 1);
|
||||
|
||||
Vk_CommandBuffer *result = &frame->scratch[idx];
|
||||
|
||||
vk.WaitForFences(vk.device, 1, &result->fence, VK_TRUE, U64_MAX);
|
||||
vk.ResetFences(vk.device, 1, &result->fence);
|
||||
|
||||
VkCommandBufferBeginInfo begin_info = { 0 };
|
||||
begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
|
||||
|
||||
vk.BeginCommandBuffer(result->handle, &begin_info);
|
||||
|
||||
frame->next_scratch += 1;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void Vk_CommandBufferSubmit(Vk_CommandBuffer *cmds, B32 wait) {
|
||||
vk.EndCommandBuffer(cmds->handle);
|
||||
|
||||
VkSubmitInfo submit_info = { 0 };
|
||||
submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
|
||||
submit_info.commandBufferCount = 1;
|
||||
submit_info.pCommandBuffers = &cmds->handle;
|
||||
|
||||
vk.QueueSubmit(vk.queue.handle, 1, &submit_info, cmds->fence);
|
||||
|
||||
if (wait) { vk.DeviceWaitIdle(vk.device); }
|
||||
}
|
||||
|
||||
#define VK_HOST_VISIBLE_FLAGS (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)
|
||||
|
||||
internal VkDeviceMemory Vk_Allocate(VkMemoryRequirements *mreq, VkMemoryPropertyFlags usage) {
|
||||
VkDeviceMemory result = VK_NULL_HANDLE;
|
||||
|
||||
VkPhysicalDeviceMemoryProperties2 _props = { 0 };
|
||||
_props.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_PROPERTIES_2;
|
||||
|
||||
vk.GetPhysicalDeviceMemoryProperties2(vk.gpu, &_props);
|
||||
|
||||
// ?????
|
||||
VkPhysicalDeviceMemoryProperties *props = &_props.memoryProperties;
|
||||
|
||||
U32 type_index = U32_MAX;
|
||||
|
||||
for (U32 it = 0; it < props->memoryTypeCount; ++it) {
|
||||
VkMemoryType *type = &props->memoryTypes[it];
|
||||
if ((type->propertyFlags & usage) == usage) {
|
||||
type_index = it;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (type_index != U32_MAX) {
|
||||
VkMemoryAllocateInfo alloc_info = { 0 };
|
||||
alloc_info.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
||||
alloc_info.allocationSize = mreq->size;
|
||||
alloc_info.memoryTypeIndex = type_index;
|
||||
|
||||
vk.AllocateMemory(vk.device, &alloc_info, 0, &result);
|
||||
}
|
||||
|
||||
Assert(result != VK_NULL_HANDLE);
|
||||
return result;
|
||||
}
|
||||
|
||||
void Vk_BufferCreate(Vk_Buffer *buffer) {
|
||||
VkBufferCreateInfo create_info = { 0 };
|
||||
create_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
||||
create_info.usage = buffer->usage;
|
||||
create_info.size = buffer->size;
|
||||
|
||||
vk.CreateBuffer(vk.device, &create_info, 0, &buffer->handle);
|
||||
Assert(buffer->handle != VK_NULL_HANDLE);
|
||||
|
||||
VkMemoryRequirements req;
|
||||
vk.GetBufferMemoryRequirements(vk.device, buffer->handle, &req);
|
||||
|
||||
VkMemoryPropertyFlags usage = buffer->host_visible ? VK_HOST_VISIBLE_FLAGS : VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
|
||||
|
||||
buffer->size = req.size;
|
||||
buffer->memory = Vk_Allocate(&req, usage);
|
||||
|
||||
vk.BindBufferMemory(vk.device, buffer->handle, buffer->memory, 0);
|
||||
|
||||
if (buffer->host_visible) {
|
||||
vk.MapMemory(vk.device, buffer->memory, 0, buffer->size, 0, &buffer->data);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void Vk_ImageCreate(Vk_Image *image) {
|
||||
VkImageCreateInfo create_info = { 0 };
|
||||
create_info.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
|
||||
create_info.imageType = VK_IMAGE_TYPE_2D;
|
||||
create_info.format = image->format;
|
||||
create_info.extent.width = image->width;
|
||||
create_info.extent.height = image->height;
|
||||
create_info.extent.depth = 1;
|
||||
create_info.mipLevels = 1;
|
||||
create_info.arrayLayers = 1;
|
||||
create_info.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | image->usage;
|
||||
create_info.samples = VK_SAMPLE_COUNT_1_BIT;
|
||||
create_info.tiling = VK_IMAGE_TILING_OPTIMAL;
|
||||
|
||||
vk.CreateImage(vk.device, &create_info, 0, &image->handle);
|
||||
|
||||
VkMemoryRequirements req;
|
||||
vk.GetImageMemoryRequirements(vk.device, image->handle, &req);
|
||||
|
||||
image->size = req.size;
|
||||
image->memory = Vk_Allocate(&req, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
||||
|
||||
vk.BindImageMemory(vk.device, image->handle, image->memory, 0);
|
||||
|
||||
VkImageViewCreateInfo view_info = { 0 };
|
||||
view_info.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
||||
view_info.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
||||
view_info.format = image->format;
|
||||
view_info.image = image->handle;
|
||||
|
||||
view_info.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
||||
view_info.subresourceRange.levelCount = 1;
|
||||
view_info.subresourceRange.layerCount = 1;
|
||||
|
||||
vk.CreateImageView(vk.device, &view_info, 0, &image->view);
|
||||
}
|
||||
|
||||
void Vk_PipelineCreate(Vk_Pipeline *pipeline) {
|
||||
VkPipelineInputAssemblyStateCreateInfo ia = { 0 };
|
||||
ia.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
|
||||
ia.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
|
||||
|
||||
VkPipelineShaderStageCreateInfo shaders[8] = { 0 };
|
||||
for (U32 it = 0; it < pipeline->shaders.count; ++it) {
|
||||
shaders[it].sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
||||
shaders[it].stage = pipeline->shaders.items[it].stage;
|
||||
shaders[it].module = pipeline->shaders.items[it].handle;
|
||||
shaders[it].pName = "main";
|
||||
}
|
||||
|
||||
VkPipelineVertexInputStateCreateInfo vi = { 0 };
|
||||
vi.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
|
||||
|
||||
// This will be updated later using dynamic commands but we still need to supply one here for
|
||||
// some reason
|
||||
VkViewport viewport = { 0, 0, 10, 10, 0, 1 };
|
||||
VkRect2D scissor = { 0, 0, 10, 10 };
|
||||
|
||||
VkPipelineViewportStateCreateInfo vp = { 0 };
|
||||
vp.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
|
||||
vp.viewportCount = 1;
|
||||
vp.pViewports = &viewport;
|
||||
vp.scissorCount = 1;
|
||||
vp.pScissors = &scissor;
|
||||
|
||||
VkPipelineRasterizationStateCreateInfo rs = { 0 };
|
||||
rs.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
|
||||
rs.lineWidth = 1.0f;
|
||||
|
||||
VkPipelineMultisampleStateCreateInfo ms = { 0 };
|
||||
ms.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
|
||||
ms.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
|
||||
|
||||
VkPipelineDepthStencilStateCreateInfo ds = { 0 };
|
||||
ds.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
|
||||
|
||||
VkPipelineColorBlendAttachmentState blend = { 0 };
|
||||
blend.blendEnable = VK_TRUE;
|
||||
blend.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
|
||||
blend.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA;
|
||||
blend.colorBlendOp = VK_BLEND_OP_ADD;
|
||||
blend.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE;
|
||||
blend.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO;
|
||||
blend.alphaBlendOp = VK_BLEND_OP_ADD;
|
||||
blend.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
|
||||
|
||||
VkPipelineColorBlendStateCreateInfo om = { 0 };
|
||||
om.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
|
||||
om.attachmentCount = 1;
|
||||
om.pAttachments = &blend;
|
||||
|
||||
VkDynamicState dynamic_state[] = {
|
||||
VK_DYNAMIC_STATE_VIEWPORT,
|
||||
VK_DYNAMIC_STATE_SCISSOR
|
||||
};
|
||||
|
||||
VkPipelineDynamicStateCreateInfo dyn = { 0 };
|
||||
dyn.sType = VK_STRUCTURE_TYPE_PIPELINE_DYNAMIC_STATE_CREATE_INFO;
|
||||
dyn.dynamicStateCount = ArraySize(dynamic_state);
|
||||
dyn.pDynamicStates = dynamic_state;
|
||||
|
||||
VkFormat formats[8];
|
||||
for (U32 it = 0; it < pipeline->targets.count; ++it) {
|
||||
formats[it] = pipeline->targets.items[it];
|
||||
}
|
||||
|
||||
VkPipelineRenderingCreateInfo rendering_info = { 0 };
|
||||
rendering_info.sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO;
|
||||
rendering_info.colorAttachmentCount = pipeline->targets.count;
|
||||
rendering_info.pColorAttachmentFormats = formats;
|
||||
|
||||
VkGraphicsPipelineCreateInfo create_info = { 0 };
|
||||
create_info.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
|
||||
create_info.pNext = &rendering_info;
|
||||
create_info.stageCount = pipeline->shaders.count;
|
||||
create_info.pStages = shaders;
|
||||
create_info.pVertexInputState = &vi;
|
||||
create_info.pInputAssemblyState = &ia;
|
||||
create_info.pViewportState = &vp;
|
||||
create_info.pRasterizationState = &rs;
|
||||
create_info.pMultisampleState = &ms;
|
||||
create_info.pDepthStencilState = &ds;
|
||||
create_info.pColorBlendState = &om;
|
||||
create_info.pDynamicState = &dyn;
|
||||
create_info.layout = pipeline->layout.pipeline;
|
||||
|
||||
vk.CreateGraphicsPipelines(vk.device, 0, 1, &create_info, 0, &pipeline->handle);
|
||||
}
|
||||
|
||||
@@ -23,15 +23,81 @@
|
||||
#define function static
|
||||
|
||||
#define VK_MAX_FRAMES_IN_FLIGHT 8
|
||||
#define VK_NUM_SCRATCH 8
|
||||
|
||||
typedef struct Vk_Image Vk_Image;
|
||||
struct Vk_Image {
|
||||
VkDeviceMemory memory;
|
||||
VkDeviceSize size;
|
||||
|
||||
VkImage handle;
|
||||
VkImageView view;
|
||||
|
||||
VkFormat format;
|
||||
VkImageUsageFlags usage;
|
||||
U32 width, height;
|
||||
};
|
||||
|
||||
typedef struct Vk_Buffer Vk_Buffer;
|
||||
struct Vk_Buffer {
|
||||
VkBuffer handle;
|
||||
|
||||
VkDeviceMemory memory;
|
||||
VkDeviceSize size;
|
||||
|
||||
VkBufferUsageFlags usage;
|
||||
|
||||
B32 host_visible;
|
||||
void *data; // if mapped host visible memory
|
||||
};
|
||||
|
||||
typedef struct Vk_Shader Vk_Shader;
|
||||
struct Vk_Shader {
|
||||
VkShaderModule handle;
|
||||
VkShaderStageFlags stage;
|
||||
};
|
||||
|
||||
typedef struct Vk_Pipeline Vk_Pipeline;
|
||||
struct Vk_Pipeline {
|
||||
VkPipeline handle;
|
||||
|
||||
struct {
|
||||
VkPipelineLayout pipeline;
|
||||
VkDescriptorSetLayout set;
|
||||
} layout;
|
||||
|
||||
struct {
|
||||
VkFormat items[8];
|
||||
U32 count;
|
||||
} targets;
|
||||
|
||||
struct {
|
||||
Vk_Shader items[8];
|
||||
U32 count;
|
||||
} shaders;
|
||||
};
|
||||
|
||||
typedef struct Vk_CommandBuffer Vk_CommandBuffer;
|
||||
struct Vk_CommandBuffer {
|
||||
VkCommandBuffer handle;
|
||||
VkFence fence;
|
||||
};
|
||||
|
||||
typedef struct Vk_Frame Vk_Frame;
|
||||
struct Vk_Frame {
|
||||
VkCommandPool pool;
|
||||
VkCommandBuffer cmd;
|
||||
|
||||
VkDescriptorPool descriptors;
|
||||
|
||||
VkSemaphore acquire, complete;
|
||||
VkFence fence;
|
||||
|
||||
U32 next_scratch;
|
||||
Vk_CommandBuffer scratch[VK_NUM_SCRATCH];
|
||||
|
||||
Vk_Buffer rbo;
|
||||
|
||||
U32 image; // swapchain image index
|
||||
};
|
||||
|
||||
@@ -77,6 +143,8 @@ struct Vk_Context {
|
||||
U32 width;
|
||||
U32 height;
|
||||
} swapchain;
|
||||
|
||||
VkSampler sampler;
|
||||
};
|
||||
|
||||
extern Vk_Context vk;
|
||||
@@ -86,4 +154,10 @@ function bool Vk_Setup(SDL_Window *window);
|
||||
function Vk_Frame *Vk_FrameBegin(SDL_Window *window);
|
||||
function void Vk_FrameEnd();
|
||||
|
||||
function Vk_CommandBuffer *Vk_CommandBufferPush();
|
||||
function void Vk_CommandBufferSubmit(Vk_CommandBuffer *cmds, B32 wait);
|
||||
|
||||
function void Vk_BufferCreate(Vk_Buffer *buffer);
|
||||
function void Vk_ImageCreate(Vk_Image *image);
|
||||
|
||||
#endif // LD_VULKAN_CORE_H_
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
VK_FUNC(GetDeviceProcAddr);
|
||||
VK_FUNC(GetPhysicalDeviceSurfaceCapabilitiesKHR);
|
||||
VK_FUNC(GetPhysicalDeviceSurfaceFormatsKHR);
|
||||
VK_FUNC(GetPhysicalDeviceMemoryProperties2);
|
||||
|
||||
#if defined(VK_USE_PLATFORM_WIN32_KHR)
|
||||
VK_FUNC(CreateWin32SurfaceKHR);
|
||||
@@ -39,7 +40,33 @@
|
||||
VK_FUNC(BeginCommandBuffer);
|
||||
VK_FUNC(EndCommandBuffer);
|
||||
VK_FUNC(DeviceWaitIdle);
|
||||
VK_FUNC(AllocateMemory);
|
||||
VK_FUNC(CreateBuffer);
|
||||
VK_FUNC(GetBufferMemoryRequirements);
|
||||
VK_FUNC(BindBufferMemory);
|
||||
VK_FUNC(CreateImage);
|
||||
VK_FUNC(GetImageMemoryRequirements);
|
||||
VK_FUNC(BindImageMemory);
|
||||
VK_FUNC(MapMemory);
|
||||
VK_FUNC(CreateGraphicsPipelines);
|
||||
VK_FUNC(CreateShaderModule);
|
||||
VK_FUNC(CreateDescriptorSetLayout);
|
||||
VK_FUNC(CreatePipelineLayout);
|
||||
VK_FUNC(CmdBindPipeline);
|
||||
VK_FUNC(CreateDescriptorPool);
|
||||
VK_FUNC(ResetDescriptorPool);
|
||||
VK_FUNC(AllocateDescriptorSets);
|
||||
VK_FUNC(CmdBindDescriptorSets);
|
||||
VK_FUNC(UpdateDescriptorSets);
|
||||
VK_FUNC(CreateSampler);
|
||||
VK_FUNC(CmdDraw);
|
||||
VK_FUNC(CmdSetViewport);
|
||||
VK_FUNC(CmdSetScissor);
|
||||
VK_FUNC(CmdDrawIndexed);
|
||||
VK_FUNC(CmdBindIndexBuffer);
|
||||
|
||||
VK_FUNC(CmdPushConstants);
|
||||
VK_FUNC(CmdCopyBufferToImage);
|
||||
VK_FUNC(CmdPipelineBarrier2);
|
||||
VK_FUNC(CmdBeginRendering);
|
||||
VK_FUNC(CmdEndRendering);
|
||||
|
||||
15
code/vulkan/shaders/basic.frag
Normal file
@@ -0,0 +1,15 @@
|
||||
#version 460 core
|
||||
|
||||
#extension GL_EXT_nonuniform_qualifier : enable
|
||||
|
||||
layout(location = 0) in vec2 frag_uv;
|
||||
layout(location = 1) in vec4 frag_c;
|
||||
layout(location = 2) in flat uint texid;
|
||||
|
||||
layout(location = 0) out vec4 framebuffer;
|
||||
|
||||
layout(binding = 1) uniform sampler2D u_images[];
|
||||
|
||||
void main() {
|
||||
framebuffer = frag_c * texture(u_images[texid], frag_uv);
|
||||
}
|
||||
72
code/vulkan/shaders/basic.vert
Normal file
@@ -0,0 +1,72 @@
|
||||
#version 460 core
|
||||
|
||||
#extension GL_EXT_scalar_block_layout : enable
|
||||
|
||||
uint indices[6] = { 0, 1, 3, 0, 3, 2 };
|
||||
|
||||
vec2 verticies[4] = vec2[](
|
||||
vec2(-0.5, -0.5),
|
||||
vec2( 0.5, -0.5),
|
||||
vec2(-0.5, 0.5),
|
||||
vec2( 0.5, 0.5)
|
||||
);
|
||||
|
||||
struct G_Rect {
|
||||
uint id;
|
||||
|
||||
uint c[4];
|
||||
float uv[4];
|
||||
|
||||
float angle;
|
||||
float x, y;
|
||||
float w, h;
|
||||
|
||||
float _pad0, _pad1;
|
||||
};
|
||||
|
||||
struct Vertex {
|
||||
vec4 p;
|
||||
vec2 uv;
|
||||
uint c;
|
||||
uint pad;
|
||||
};
|
||||
|
||||
layout(push_constant, row_major)
|
||||
uniform Global {
|
||||
mat4 proj;
|
||||
};
|
||||
|
||||
layout(binding = 0, scalar)
|
||||
readonly buffer Rect {
|
||||
G_Rect rects[];
|
||||
};
|
||||
|
||||
layout(location = 0) out vec2 frag_uv;
|
||||
layout(location = 1) out vec4 frag_c;
|
||||
layout(location = 2) out flat uint texid;
|
||||
|
||||
vec4 unorm_colour(uint c) {
|
||||
vec4 result = vec4((c >> 0) & 0xFF, (c >> 8) & 0xFF, (c >> 16) & 0xFF, (c >> 24) & 0xFF) / 255.0f;
|
||||
return result;
|
||||
}
|
||||
|
||||
vec2 rotate2f(vec2 v, float angle) {
|
||||
float s = sin(angle);
|
||||
float c = cos(angle);
|
||||
|
||||
vec2 result = vec2(c * v.x - s * v.y, s * v.x + c * v.y);
|
||||
return result;
|
||||
}
|
||||
|
||||
void main() {
|
||||
G_Rect rect = rects[gl_InstanceIndex];
|
||||
uint idx = indices[gl_VertexIndex];
|
||||
|
||||
vec2 p = (rotate2f(verticies[idx], rect.angle) * vec2(rect.w, rect.h)) + vec2(rect.x, rect.y);
|
||||
|
||||
gl_Position = proj * vec4(p, 1.0f, 1.0f);
|
||||
|
||||
frag_uv = vec2(rect.uv[(idx & 1) << 1], rect.uv[1 + (uint(idx / 2) << 1)]);
|
||||
frag_c = unorm_colour(rect.c[idx]);
|
||||
texid = rect.id;
|
||||
}
|
||||
BIN
code/world.sgdat
Normal file
24
linux
@@ -12,6 +12,7 @@ pushd "build" > /dev/null
|
||||
|
||||
release=0
|
||||
deps=0
|
||||
assets=0
|
||||
|
||||
for a in $*
|
||||
do
|
||||
@@ -23,6 +24,11 @@ then
|
||||
deps=1
|
||||
fi
|
||||
|
||||
if [[ ! -d "assets" ]]
|
||||
then
|
||||
assets=1
|
||||
fi
|
||||
|
||||
if [[ $deps == 1 ]]
|
||||
then
|
||||
# We don't build SDL3 like on windows, this assumes you have it installed instead. We can't
|
||||
@@ -35,10 +41,24 @@ then
|
||||
cp ../thirdparty/stb/*.h deps/stb
|
||||
fi
|
||||
|
||||
if [[ $assets == 1 ]]
|
||||
then
|
||||
echo "[Copying assets]"
|
||||
|
||||
cp -r "../assets" .
|
||||
fi
|
||||
|
||||
echo "[Building shaders]"
|
||||
|
||||
[[ ! -d "assets/shaders" ]] && mkdir "assets/shaders"
|
||||
|
||||
glslangValidator -o "assets/shaders/basic.vert.spv" --target-env "vulkan1.3" "../code/vulkan/shaders/basic.vert"
|
||||
glslangValidator -o "assets/shaders/basic.frag.spv" --target-env "vulkan1.3" "../code/vulkan/shaders/basic.frag"
|
||||
|
||||
echo "[Building source]"
|
||||
|
||||
COMPILER_OPTS="-Wall -Wno-missing-braces -Wno-unused-function -I'deps/stb'"
|
||||
LINKER_OPTS="-lSDL3"
|
||||
COMPILER_OPTS="-Wall -Wno-override-init-side-effects -Wno-missing-braces -Wno-unused-function -Ideps/stb -I../code"
|
||||
LINKER_OPTS="-lSDL3 -lm"
|
||||
|
||||
if [[ $release == 1 ]]
|
||||
then
|
||||
|
||||
18
windows.bat
@@ -9,6 +9,7 @@ PUSHD "build"
|
||||
|
||||
SET deps=0
|
||||
SET release=0
|
||||
SET assets=0
|
||||
|
||||
FOR %%A in (%*) DO (
|
||||
SET %%A=1
|
||||
@@ -51,10 +52,23 @@ IF %deps% equ 1 (
|
||||
COPY "..\thirdparty\stb\*.h" "deps\stb" > NUL
|
||||
)
|
||||
|
||||
IF %assets% equ 1 (
|
||||
ECHO [Copying assets]
|
||||
|
||||
xcopy /Y /Q "..\assets" "assets"
|
||||
)
|
||||
|
||||
ECHO [Building shaders]
|
||||
|
||||
IF NOT EXIST "assets\shaders" ( MKDIR "assets\shaders" )
|
||||
|
||||
glslangValidator -o "assets\shaders\basic.vert.spv" --target-env vulkan1.3 "..\code\vulkan\shaders\basic.vert"
|
||||
glslangValidator -o "assets\shaders\basic.frag.spv" --target-env vulkan1.3 "..\code\vulkan\shaders\basic.frag"
|
||||
|
||||
ECHO [Building source]
|
||||
|
||||
SET COMPILER_OPTS=-nologo -W4 -I"deps\SDL3\include" -I"deps\stb" -I"%VULKAN_SDK%\Include"
|
||||
SET LINKER_OPTS=-LIBPATH:"deps\SDL3\lib" SDL3.lib
|
||||
SET COMPILER_OPTS=-nologo -W4 -I"deps\SDL3\include" -I"deps\stb" -I"%VULKAN_SDK%\Include" -I"..\code"
|
||||
SET LINKER_OPTS=-LIBPATH:"deps\SDL3\lib" SDL3.lib Ole32.lib Shell32.lib
|
||||
|
||||
IF %release% equ 1 (
|
||||
ECHO [Release build]
|
||||
|
||||