49 lines
1.3 KiB
Odin
49 lines
1.3 KiB
Odin
package images
|
|
|
|
import "core:fmt"
|
|
import "core:strings"
|
|
import stbi "vendor:stb/image"
|
|
|
|
Error :: Maybe(string)
|
|
|
|
@(require_results)
|
|
load_string :: proc(text: string) -> (image: Image, error: Error = nil) {
|
|
pixels_mp := stbi.load_from_memory(
|
|
raw_data(text),
|
|
i32(len(text)),
|
|
&image.width,
|
|
&image.height,
|
|
&image.channels,
|
|
4,
|
|
)
|
|
if pixels_mp == nil {
|
|
error = fmt.tprintf("Failed to load, falling back to default texture")
|
|
image = default_image
|
|
return
|
|
}
|
|
|
|
image.pixels = pixels_mp[:image.width * image.height * image.channels]
|
|
return
|
|
}
|
|
|
|
@(require_results)
|
|
load :: proc(filename: string) -> (image: Image, error: Error = nil) {
|
|
filename_cstring := strings.clone_to_cstring(filename, context.temp_allocator)
|
|
|
|
pixels_mp := stbi.load(filename_cstring, &image.width, &image.height, &image.channels, 4)
|
|
if pixels_mp == nil {
|
|
error = fmt.tprintf("Failed to load {}, falling back to default texture", filename)
|
|
image = default_image
|
|
return
|
|
}
|
|
|
|
image.pixels = pixels_mp[:image.width * image.height * image.channels]
|
|
return
|
|
}
|
|
|
|
default_image := Image {
|
|
width = 2,
|
|
height = 2,
|
|
channels = 4,
|
|
pixels = []u8{255, 0, 0, 255, 0, 0, 0, 255, 0, 0, 0, 255, 255, 0, 0, 255},
|
|
}
|