big improvements to asset system
This commit is contained in:
parent
e412400c11
commit
b8b1aaaac5
@ -1,7 +1,10 @@
|
||||
#scope_file
|
||||
|
||||
#import "String";
|
||||
hash :: #import "Hash";
|
||||
|
||||
#load "loaders.jai";
|
||||
|
||||
MAX_FILE_SIZE :: 2_000_000_000;
|
||||
buf : [MAX_FILE_SIZE]u8;
|
||||
|
||||
@ -12,15 +15,6 @@ Pack_Request :: struct {
|
||||
shouldBlockEngine : bool; // Means that the engine loop should do nothing while this is loading...
|
||||
}
|
||||
|
||||
Loaded_Pack :: struct {
|
||||
name : string;
|
||||
nameHash : u32 = 0;
|
||||
content : Load_Package;
|
||||
textures : [..]sg_image;
|
||||
sounds : [..]Audio_Data;
|
||||
//fonts : [..]Font??;
|
||||
|
||||
}
|
||||
|
||||
Asset_Manager :: struct {
|
||||
packQueue : [..]Pack_Request;
|
||||
@ -31,34 +25,124 @@ g_asset_manager : Asset_Manager;
|
||||
|
||||
packcb :: (res: *sfetch_response_t) #c_call {
|
||||
push_context,defer_pop default_context;
|
||||
mem : []u8;
|
||||
mem.count = res.data.size.(s64);
|
||||
mem.data = res.data.ptr;
|
||||
mem := NewArray(res.data.size.(s64), u8, false);
|
||||
memcpy(mem.data, res.data.ptr, res.data.size.(s64));
|
||||
|
||||
pack: Loaded_Pack;
|
||||
pack.nameHash = hash.get_hash(g_asset_manager.packQueue[0].name);
|
||||
pack.name = g_asset_manager.packQueue[0].name;
|
||||
success := init_from_memory(*pack.content, mem, sprint("%", g_asset_manager.packQueue[0].name));
|
||||
if !success then print("Failed to load pack!!\n");
|
||||
add_resources_from_pack(*pack);
|
||||
array_add(*g_asset_manager.loadedPacks, pack);
|
||||
array_unordered_remove_by_index(*g_asset_manager.packQueue, 0);
|
||||
}
|
||||
|
||||
add_resources_from_pack(*pack);
|
||||
Loaded_Pack :: struct {
|
||||
name : string;
|
||||
nameHash : u32 = 0;
|
||||
content : Load_Package;
|
||||
textures : Table(string, sg_image);
|
||||
animations : Table(string, Animation);
|
||||
audio : Table(string, Audio_Data);
|
||||
//fonts : [..]Font??;
|
||||
}
|
||||
|
||||
add_resources_from_pack :: (pack: *Loaded_Pack) {
|
||||
add_new_spritesheets_from_pack(*pack.content, pack.name);
|
||||
load_color_lut_images(*pack.content, pack.name);
|
||||
// We need to go trough this at the end.
|
||||
Queued_Sheet_File :: struct {
|
||||
name : string;
|
||||
image : sg_image;
|
||||
sheet_w : s32;
|
||||
sheet_h : s32;
|
||||
sheet : Aseprite_Sheet;
|
||||
}
|
||||
sheets_to_init : Table(string, Queued_Sheet_File);
|
||||
sheets_to_init.allocator = temp;
|
||||
|
||||
for v : pack.content.lookup {
|
||||
_, name, extension := split_from_left(v.name, ".");
|
||||
if extension == {
|
||||
case "png";
|
||||
img, w, h := create_texture_from_memory(v.data);
|
||||
table_set(*pack.textures, sprint("%", v.name), img);
|
||||
case "sheet.png";
|
||||
img, w, h := create_texture_from_memory(v.data);
|
||||
queuedSheet := table_find_pointer(*sheets_to_init, name);
|
||||
if !queuedSheet {
|
||||
table_set(*sheets_to_init, name, .{ name = name, image = img, sheet_w = w, sheet_h = h });
|
||||
} else {
|
||||
queuedSheet.image = img;
|
||||
queuedSheet.sheet_w = w;
|
||||
queuedSheet.sheet_h = h;
|
||||
}
|
||||
case "sheet.json";
|
||||
s := create_string_from_memory(v.data);
|
||||
success, sheet := Jaison.json_parse_string(s, Aseprite_Sheet,, temp);
|
||||
if !success {
|
||||
print("Failed to parse animation sheet JSON for sheet(%)\n", name);
|
||||
continue;
|
||||
}
|
||||
queuedSheet := table_find_pointer(*sheets_to_init, name);
|
||||
if !queuedSheet {
|
||||
table_set(*sheets_to_init, name, .{ name = name, sheet = sheet });
|
||||
} else {
|
||||
queuedSheet.sheet = sheet;
|
||||
}
|
||||
case "colorgrade.png";
|
||||
img, x, y := create_texture_from_memory(v.data);
|
||||
add_image_to_lut_list(img, v.name);
|
||||
case "wav";
|
||||
audio := load_wav_from_memory(v.data);
|
||||
table_set(*pack.audio, name, audio);
|
||||
case "ttf";
|
||||
// Load into a font. Add to free list.
|
||||
case;
|
||||
print("There was file(%) in pack(%), that did not match any known fileformat...\n", v.name, pack);
|
||||
}
|
||||
print("% -> %\n", v.name, extension);
|
||||
}
|
||||
|
||||
// Properly initialize animations from the pack now that we have the images
|
||||
// and the JSON files both combined.
|
||||
for qsheet : sheets_to_init {
|
||||
for qsheet.sheet.meta.frameTags {
|
||||
anim : Animation;
|
||||
anim.name = sprint("%", it.name);
|
||||
anim.sheet = qsheet.image;
|
||||
anim.sheet_w = qsheet.sheet_w;
|
||||
anim.sheet_h = qsheet.sheet_h;
|
||||
for idx : it.from..it.to {
|
||||
frameData := qsheet.sheet.frames[idx];
|
||||
array_add(*anim.frames, Frame.{
|
||||
frameData.frame.x,
|
||||
frameData.frame.y,
|
||||
frameData.frame.w,
|
||||
frameData.frame.h,
|
||||
frameData.duration
|
||||
});
|
||||
}
|
||||
table_add(*pack.animations, anim.name, anim);
|
||||
print("Added anim(%)\n", anim.name);
|
||||
}
|
||||
}
|
||||
|
||||
// add_new_spritesheets_from_pack(*pack.content, pack.name);
|
||||
// load_color_lut_images(*pack.content, pack.name);
|
||||
}
|
||||
|
||||
free_resources_from_pack :: (pack: *Loaded_Pack) {
|
||||
|
||||
}
|
||||
|
||||
find_pack_by_name :: (name: string) -> (bool, Loaded_Pack) {
|
||||
nameHash := get_hash(name);
|
||||
asset_pack : Loaded_Pack;
|
||||
for g_asset_manager.loadedPacks {
|
||||
if it.nameHash == nameHash {
|
||||
return true, it;
|
||||
}
|
||||
}
|
||||
print("Was unable to find pack: %\n", name);
|
||||
print("[WARNING] Was unable to find pack: %\n", name);
|
||||
return false, .{};
|
||||
}
|
||||
|
||||
@ -108,53 +192,42 @@ load_pack :: (name: string, shouldBlock: bool = true, shouldBlockEngine: bool =
|
||||
#scope_file
|
||||
#scope_export
|
||||
|
||||
create_texture_from_pack :: (pack: string, path: string) -> (sg_image, s32, s32) {
|
||||
pack_ok, pack := find_pack_by_name(pack);
|
||||
ok, entry := table_find(*pack.content.lookup, path);
|
||||
get_texture_from_pack :: (pack: string, path: string) -> (sg_image) {
|
||||
found, pack := find_pack_by_name(pack);
|
||||
invalid_img : sg_image;
|
||||
|
||||
if !found {
|
||||
// find_pack_by_name already logs this
|
||||
return invalid_img;
|
||||
}
|
||||
|
||||
ok, img := table_find(*pack.textures, path);
|
||||
|
||||
if !ok {
|
||||
print("Failed to find texture % from pack...\n", path);
|
||||
img : sg_image;
|
||||
return img, 0, 0;
|
||||
print("[WARNING] Failed to find texture(%) from pack(%)\n", path, pack.name);
|
||||
return invalid_img;
|
||||
}
|
||||
|
||||
x : s32;
|
||||
y : s32;
|
||||
channels : s32;
|
||||
data := stbi.stbi_load_from_memory(entry.data.data, xx entry.data.count, *x, *y, *channels, 4);
|
||||
img := sg_alloc_image();
|
||||
|
||||
subimg : [6][16]sg_range;
|
||||
subimg[0][0] = .{
|
||||
ptr = data,
|
||||
size = xx (x * y * 4)
|
||||
};
|
||||
|
||||
sg_init_image(img, *(sg_image_desc.{
|
||||
width = x,
|
||||
height = y,
|
||||
pixel_format = sg_pixel_format.RGBA8,
|
||||
data = .{
|
||||
subimage = subimg
|
||||
}
|
||||
}));
|
||||
|
||||
stbi.stbi_image_free(data);
|
||||
|
||||
return img, x, y;
|
||||
return img;
|
||||
}
|
||||
|
||||
get_animation_from_pack :: (pack: string, path: string) -> *Animation {
|
||||
found, pack := find_pack_by_name(pack);
|
||||
if !found then return null;
|
||||
return table_find_pointer(*pack.animations, path);
|
||||
}
|
||||
|
||||
// get_texture_pointer :: (pack: string, path: string) -> (ready: bool, texture: *sg_image) {
|
||||
|
||||
|
||||
// }
|
||||
get_audio_from_pack :: (pack: string, path: string) -> *Audio_Data {
|
||||
found, pack := find_pack_by_name(pack);
|
||||
if !found then return null;
|
||||
audio := table_find_pointer(*pack.audio, path);
|
||||
if !audio then print("Failed to find audio(%)\n", path);
|
||||
return audio;
|
||||
}
|
||||
|
||||
add_font_from_pack :: (pack: string, path: string) {
|
||||
pack_ok, pack := find_pack_by_name(pack);
|
||||
if !pack_ok then return;
|
||||
for _, key : pack.content.lookup {
|
||||
print("Key: %\n", key);
|
||||
}
|
||||
ok, entry := table_find(*pack.content.lookup, path);
|
||||
if !ok {
|
||||
print("Failed to find font % from pack...\n", path);
|
||||
|
||||
50
src/assets/loaders.jai
Normal file
50
src/assets/loaders.jai
Normal file
@ -0,0 +1,50 @@
|
||||
create_texture_from_memory :: (data: []u8) -> (sg_image, s32, s32) {
|
||||
x, y, channels : s32;
|
||||
image := stbi.stbi_load_from_memory(data.data, xx data.count, *x, *y, *channels, 4);
|
||||
|
||||
subimg : [6][16]sg_range;
|
||||
subimg[0][0] = .{
|
||||
ptr = image,
|
||||
size = xx (x * y * 4)
|
||||
};
|
||||
|
||||
img := sg_alloc_image();
|
||||
|
||||
sg_init_image(img, *(sg_image_desc.{
|
||||
width = x,
|
||||
height = y,
|
||||
pixel_format = sg_pixel_format.RGBA8,
|
||||
data = .{
|
||||
subimage = subimg
|
||||
}
|
||||
}));
|
||||
|
||||
stbi.stbi_image_free(image);
|
||||
return img, x, y;
|
||||
}
|
||||
|
||||
create_string_from_memory :: (data: []u8) -> string {
|
||||
s: string;
|
||||
s.data = data.data;
|
||||
s.count = data.count;
|
||||
return s;
|
||||
}
|
||||
|
||||
load_wav_from_memory :: (data: []u8) -> Audio_Data {
|
||||
Wav :: #import "Wav_File";
|
||||
audio : Audio_Data;
|
||||
dataString : string;
|
||||
dataString.data = data.data;
|
||||
dataString.count = data.count;
|
||||
format, samples, success := Wav.get_wav_header(dataString);
|
||||
if !success print("Failed to load wav file!\n");
|
||||
audio_samples : []s16;
|
||||
audio_samples.data = cast(*s16)samples.data;
|
||||
audio_samples.count = samples.count / 2;
|
||||
for sample, i: audio_samples {
|
||||
if i % 2 == 0 {
|
||||
array_add(*audio.data, cast(float)sample / 32768.0);
|
||||
}
|
||||
}
|
||||
return audio;
|
||||
}
|
||||
@ -3,7 +3,7 @@
|
||||
#load "mixer.jai";
|
||||
|
||||
audio_init :: () {
|
||||
load_wav_file();
|
||||
// load_wav_file();
|
||||
}
|
||||
|
||||
audio_cleanup :: () {
|
||||
|
||||
@ -1,12 +1,17 @@
|
||||
sokol_audio_callback :: (buffer: *float, num_frames: s32, num_channels: s32) #c_call {
|
||||
push_context,defer_pop default_context;
|
||||
if mono_track.count < 1 then return;
|
||||
audio := get_audio_from_pack("game_core", "sound/music/monoco");
|
||||
if !audio {
|
||||
print("Audio was null!!\n");
|
||||
return;
|
||||
}
|
||||
if audio.data.count < 1 then return;
|
||||
|
||||
for i : 0..num_frames-1 {
|
||||
if cur_sample >= mono_track.count {
|
||||
if cur_sample >= audio.data.count {
|
||||
cur_sample = 0;
|
||||
}
|
||||
buffer[i] = mono_track[cur_sample] * 0.5;
|
||||
buffer[i] = audio.data[cur_sample] * 0.5;
|
||||
cur_sample += 1;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,24 +1,4 @@
|
||||
Audio_Data :: struct {
|
||||
channels: u16;
|
||||
data: []float;
|
||||
}
|
||||
|
||||
Wav :: #import "Wav_File";
|
||||
|
||||
audio_info : Wav.Waveformatex;
|
||||
|
||||
load_wav_file :: () {
|
||||
audio := load_string_from_pack("game_core", "sound/music/monoco.wav");
|
||||
format, samples, success := Wav.get_wav_header(audio);
|
||||
if !success print("Failed to load wav file!!!!!\n");
|
||||
audio_info = format;
|
||||
audio_samples.data = cast(*s16)samples.data;
|
||||
audio_samples.count = samples.count / 2;
|
||||
print("Format: %\n", format.wBitsPerSample);
|
||||
for sample, i: audio_samples {
|
||||
if i % 2 == 0 {
|
||||
array_add(*mono_track, cast(float)sample / 32768.0);
|
||||
}
|
||||
}
|
||||
print("Converted % samples", mono_track.count);
|
||||
data: [..]float;
|
||||
}
|
||||
|
||||
@ -53,7 +53,7 @@ body {
|
||||
</style>
|
||||
</head>
|
||||
<body style="background:black">
|
||||
<canvas class="game" id="canvas" oncontextmenu="event.preventDefault()"></canvas>
|
||||
<canvas class="game" id="canvas" oncontextmenu="event.preventDefault()" onclick="window.focus()"></canvas>
|
||||
<script type="text/javascript">
|
||||
window.focus()
|
||||
var Module = {
|
||||
|
||||
@ -1,5 +1,16 @@
|
||||
g_animations: Table(string, Animation);
|
||||
|
||||
#scope_file
|
||||
get_animation_from_string :: (animation: string) -> *Animation {
|
||||
ok, pack, anim := split_from_left(animation, ".");
|
||||
if !ok {
|
||||
print("Malformed animation query: %\n", animation);
|
||||
return null;
|
||||
}
|
||||
return get_animation_from_pack(pack, anim);
|
||||
}
|
||||
#scope_export
|
||||
|
||||
Animation_Player :: struct {
|
||||
current_animation : *Animation;
|
||||
queued_animation : *Animation = null;
|
||||
@ -32,11 +43,11 @@ animation_draw :: (player: *Animation_Player, position: Vector3, flipX: bool = f
|
||||
|
||||
animation_set :: (player: *Animation_Player, animation: string) {
|
||||
player.current_frame = 0;
|
||||
player.current_animation = table_find_pointer(*g_animations, animation);
|
||||
player.current_animation = get_animation_from_string(animation);
|
||||
}
|
||||
|
||||
animation_set_queued :: (player: *Animation_Player, animation: string) {
|
||||
player.queued_animation = table_find_pointer(*g_animations, animation);
|
||||
player.queued_animation = get_animation_from_string(animation);
|
||||
|
||||
}
|
||||
animation_set_if_not :: (player: *Animation_Player, animation: string) {
|
||||
@ -46,7 +57,8 @@ animation_set_if_not :: (player: *Animation_Player, animation: string) {
|
||||
}
|
||||
|
||||
animation_is :: (player: *Animation_Player, animation: string) -> bool {
|
||||
return player.current_animation.name == animation;
|
||||
pack, anim := split_from_left(animation, ".");
|
||||
return player.current_animation.name == anim;
|
||||
}
|
||||
|
||||
Frame :: struct {
|
||||
@ -91,41 +103,3 @@ Aseprite_Sheet :: struct {
|
||||
frames : [..]Aseprite_Frame;
|
||||
meta : Aseprite_Sheet_Info;
|
||||
}
|
||||
|
||||
add_new_spritesheets_from_pack :: (pack: *Load_Package, packName: string) {
|
||||
String :: #import "String";
|
||||
|
||||
for v : pack.lookup {
|
||||
isSpritesheet, remainder := String.contains(v.name, "sprites/");
|
||||
isData := String.contains(remainder, ".json");
|
||||
if isSpritesheet && isData {
|
||||
print("Adding sheet: %\n", remainder);
|
||||
is_ok, name_without_fileformat := split_from_right(v.name, #char ".");
|
||||
sheet_image_name := sprint("%.png", name_without_fileformat);
|
||||
sheet_image, sheet_w, sheet_h := create_texture_from_pack(packName, sheet_image_name);
|
||||
|
||||
|
||||
s := load_string_from_pack(packName, v.name);
|
||||
success, sheet := Jaison.json_parse_string(s, Aseprite_Sheet,, temp);
|
||||
|
||||
for sheet.meta.frameTags {
|
||||
anim : Animation;
|
||||
anim.name = sprint("%", it.name);
|
||||
anim.sheet = sheet_image;
|
||||
anim.sheet_w = sheet_w;
|
||||
anim.sheet_h = sheet_h;
|
||||
for idx : it.from..it.to {
|
||||
frameData := sheet.frames[idx];
|
||||
array_add(*anim.frames, Frame.{
|
||||
frameData.frame.x,
|
||||
frameData.frame.y,
|
||||
frameData.frame.w,
|
||||
frameData.frame.h,
|
||||
frameData.duration
|
||||
});
|
||||
}
|
||||
table_add(*g_animations, anim.name, anim);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1063,7 +1063,7 @@ create_ssao_pipeline :: () {
|
||||
}
|
||||
|
||||
init_plane_textures :: () {
|
||||
gPipelines.plane.bind.images[2] = create_texture_from_pack("core", "utiltex/water_small.png");
|
||||
gPipelines.plane.bind.images[2] = get_texture_from_pack("core", "utiltex/water_small.png");
|
||||
}
|
||||
|
||||
g_plane_gbuffer_vertex_buffer : sg_buffer;
|
||||
|
||||
@ -53,18 +53,16 @@ LUT_list : [..]Color_LUT;
|
||||
LUT_name_list : [..]string;
|
||||
g_current_lut_texture_index : s32 = 0;
|
||||
|
||||
load_color_lut_images :: (pack: *Load_Package, packName: string) {
|
||||
for v : pack.lookup {
|
||||
isSpritesheet := String.contains(v.name, ".colorgrade.png");
|
||||
if isSpritesheet {
|
||||
print("Adding LUT: %\n", v.name);
|
||||
img, w, h := create_texture_from_pack(packName, v.name);
|
||||
|
||||
add_image_to_lut_list :: (img: sg_image, name: string) {
|
||||
for LUT_list {
|
||||
if it.name == name then return;
|
||||
}
|
||||
|
||||
newSheet := Color_LUT.{
|
||||
name = v.name,
|
||||
name = sprint("%", name),
|
||||
image = img,
|
||||
};
|
||||
array_add(*LUT_list, newSheet);
|
||||
array_add(*LUT_name_list, newSheet.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user