update the generator tool & added unmarshalling a whole directory

This commit is contained in:
Stefan Stefanov 2024-04-21 19:17:27 +03:00
parent 81d9632396
commit 973af01380
7 changed files with 49 additions and 39 deletions

View file

@ -1,7 +1,7 @@
{ {
"workbench.colorCustomizations": { "workbench.colorCustomizations": {
"activityBar.background": "#322C2D", "activityBar.background": "#322C2D",
"titleBar.activeBackground": "#463E3F", "titleBar.activeBackground": "#463E3F",
"titleBar.activeForeground": "#FAFAFA" "titleBar.activeForeground": "#FAFAFA"
} }
} }

14
.vscode/tasks.json vendored
View file

@ -31,6 +31,20 @@
}, },
"group": "build" "group": "build"
}, },
{
"label": "Clean build folder(s)",
"type": "shell",
"windows": {
"command": "cd ${workspaceFolder}\\build && rm game*; cd ${workspaceFolder} && rm aseprite_odin_generator*",
},
// "linux": {
// "command": "${workspaceFolder}/scripts/build_release.sh",
// },
// "osx": {
// "command": "${workspaceFolder}/scripts/build_release.sh",
// },
"group": "build"
},
{ {
"label": "Build Hot Reload", "label": "Build Hot Reload",
"type": "shell", "type": "shell",

View file

@ -1,2 +1,2 @@
@echo off @echo off
odin build src/main_release -define:RAYLIB_SHARED=false -out:build/game_debug.exe -no-bounds-check -subsystem:windows -debug odin build src/aseprite_odin_generator -define:RAYLIB_SHARED=true -out:build_generator/aseprite_odin_generator.exe -debug

View file

@ -23,22 +23,15 @@ main :: proc() {
fmt.panicf("Couldn't load file!") fmt.panicf("Couldn't load file!")
} }
doc: ase.Document cwd := os.get_current_directory()
read, um_err := ase.unmarshal_from_slice(ase_file, &doc) target_dir := strings.concatenate({cwd, "\\src\\aseprite_odin_generator\\"})
if um_err != nil {
fmt.panicf("Couldn't unmarshall file!")
} else {
fmt.printfln("Read {0} bytes from file", read)
}
fmt.println("Header:\n\t", doc.header)
// fmt.println("Frames:\n\t", doc.frames)
atlas: rl.Image = rl.GenImageColor(ATLAS_SIZE, ATLAS_SIZE, rl.BLANK) atlas: rl.Image = rl.GenImageColor(ATLAS_SIZE, ATLAS_SIZE, rl.BLANK)
atlas_entries: [dynamic]gen.AtlasEntry
gen.unmarshall_aseprite_dir(target_dir, &atlas_entries)
atlas_entry := gen.atlas_entry_from_compressed_cells(doc)
// Packs the cells & blits them to the atlas gen.pack_atlas_entries(atlas_entries[:], &atlas, 10, 10)
gen.pack_atlas_entries({atlas_entry}, &atlas)
rl.ExportImage(atlas, EXPORT_PATH) rl.ExportImage(atlas, EXPORT_PATH)
} }

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

Before After
Before After

Binary file not shown.

Before

Width:  |  Height:  |  Size: 515 B

View file

@ -13,11 +13,13 @@ AtlasEntry :: struct {
} }
unmarshall_aseprite_dir :: proc(path: string, atlas_entries: ^[dynamic]AtlasEntry) { unmarshall_aseprite_dir :: proc(path: string, atlas_entries: ^[dynamic]AtlasEntry) {
fp.dir(path) if dir_fd, err := os.open(path, os.O_RDONLY); err == os.ERROR_NONE {
if fd, ok := os.open(path); ok == 0 { fis: []os.File_Info
if fi_files, fi_ok := os.read_dir(fd, -1); fi_ok == 0 { if fis, err = os.read_dir(dir_fd, -1); err == os.ERROR_NONE {
unmarshall_aseprite_files_file_info(fi_files, atlas_entries) unmarshall_aseprite_files_file_info(fis, atlas_entries)
} }
} else {
fmt.println("Couldn't open folder: ", path)
} }
} }
@ -33,10 +35,15 @@ unmarshall_aseprite_files_file_info :: proc(
} }
unmarshall_aseprite_files :: proc(file_paths: []string, atlas_entries: ^[dynamic]AtlasEntry) { unmarshall_aseprite_files :: proc(file_paths: []string, atlas_entries: ^[dynamic]AtlasEntry) {
current_document: ase.Document aseprite_document: ase.Document
for fp in file_paths { for file in file_paths {
atlas_entry := atlas_entry_from_compressed_cells(current_document) extension := fp.ext(file)
ase.unmarshal_from_filename(fp, &current_document) if extension != ".aseprite" {continue}
fmt.println("Unmarshalling file: ", file)
ase.unmarshal_from_filename(file, &aseprite_document)
atlas_entry := atlas_entry_from_compressed_cells(aseprite_document)
append(atlas_entries, atlas_entry) append(atlas_entries, atlas_entry)
} }
} }
@ -71,14 +78,9 @@ atlas_entry_from_compressed_cells :: proc(document: ase.Document) -> (atlas_entr
} }
/* /*
Takes in a slice of entries, an output texture, the width & height Takes in a slice of entries, an output texture and offsets (offset_x/y)
*/ */
pack_atlas_entries :: proc( pack_atlas_entries :: proc(entries: []AtlasEntry, atlas: ^rl.Image, offset_x: i32, offset_y: i32) {
entries: []AtlasEntry,
atlas: ^rl.Image,
offset_x: int = 0,
offset_y: int = 0,
) {
all_entries: [dynamic]rl.Image // it's fine to store it like this, rl.Image just stores a pointer to the data all_entries: [dynamic]rl.Image // it's fine to store it like this, rl.Image just stores a pointer to the data
{ {
for entry in entries { for entry in entries {
@ -100,7 +102,7 @@ pack_atlas_entries :: proc(
cellIdx: int cellIdx: int
for &entry, entryIdx in entries { for &entry, entryIdx in entries {
for &cell in entry.cells { for &cell in entry.cells {
// I can probably infer this information with just the id of the rect but I'm being lazy right now // I can probably infer this information with just the id of the rect but I'm being lazy right now
map_insert(&rect_idx_to_entry_and_cell, cellIdx, EntryAndCell{&entry, &cell}) map_insert(&rect_idx_to_entry_and_cell, cellIdx, EntryAndCell{&entry, &cell})
rects[cellIdx].id = auto_cast entryIdx rects[cellIdx].id = auto_cast entryIdx
cellIdx += 1 cellIdx += 1
@ -109,13 +111,13 @@ pack_atlas_entries :: proc(
for entry, entryIdx in all_entries { for entry, entryIdx in all_entries {
entry_stb_rect := &rects[entryIdx] entry_stb_rect := &rects[entryIdx]
entry_stb_rect.w = auto_cast entry.width entry_stb_rect.w = stbrp.Coord(entry.width + offset_x * 2)
entry_stb_rect.h = auto_cast entry.height entry_stb_rect.h = stbrp.Coord(entry.height + offset_y * 2)
} }
ctx: stbrp.Context ctx: stbrp.Context
stbrp.init_target(&ctx, atlas.width, atlas.height, &nodes[0], auto_cast num_entries) stbrp.init_target(&ctx, atlas.width, atlas.height, &nodes[0], i32(num_entries))
res := stbrp.pack_rects(&ctx, &rects[0], auto_cast num_entries) res := stbrp.pack_rects(&ctx, &rects[0], i32(num_entries))
if res == 1 { if res == 1 {
fmt.println("Packed everything successfully!") fmt.println("Packed everything successfully!")
fmt.printfln("Rects: {0}", rects[:]) fmt.printfln("Rects: {0}", rects[:])
@ -135,11 +137,12 @@ pack_atlas_entries :: proc(
} }
// Placing it in the atlas in the calculated offsets (in the packing step) // Placing it in the atlas in the calculated offsets (in the packing step)
dst_rect := rl.Rectangle { dst_rect := rl.Rectangle {
auto_cast rect.x, auto_cast rect.x + auto_cast offset_x,
auto_cast rect.y, auto_cast rect.y + auto_cast offset_y,
auto_cast cell.width, auto_cast cell.width,
auto_cast cell.height, auto_cast cell.height,
} }
fmt.printfln("Src rect: {0}\nDst rect:{1}", src_rect, dst_rect)
rl.ImageDraw(atlas, cell^, src_rect, dst_rect, rl.WHITE) rl.ImageDraw(atlas, cell^, src_rect, dst_rect, rl.WHITE)
} }