v/vlib/stbi/stbi.v

82 lines
1.7 KiB
V
Raw Normal View History

2020-01-23 21:04:46 +01:00
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
2019-06-23 04:21:30 +02:00
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
2019-06-22 20:20:28 +02:00
module stbi
#flag -I @VROOT/thirdparty/stb_image
2019-06-23 14:08:17 +02:00
#include "stb_image.h"
#flag @VROOT/thirdparty/stb_image/stbi.o
2019-10-27 12:05:50 +01:00
pub struct Image {
2020-05-15 13:15:04 +02:00
pub mut:
2019-06-22 20:20:28 +02:00
width int
height int
nr_channels int
ok bool
data voidptr
ext string
}
2019-11-24 04:27:02 +01:00
fn C.stbi_load() voidptr
fn C.stbi_load_from_memory() voidptr
2019-11-24 04:27:02 +01:00
fn C.stbi_image_free()
fn C.stbi_set_flip_vertically_on_load()
2020-08-04 01:26:56 +02:00
fn init() {
set_flip_vertically_on_load(false)
}
2019-06-26 17:49:50 +02:00
pub fn load(path string) Image {
ext := path.all_after_last('.')
2019-06-22 20:20:28 +02:00
mut res := Image {
ok: true
ext: ext
data: 0
}
2019-08-22 23:00:31 +02:00
flag := if ext == 'png' { C.STBI_rgb_alpha } else { 0 }
res.data = C.stbi_load(path.str, &res.width, &res.height, &res.nr_channels, flag)
2019-06-22 20:20:28 +02:00
if isnil(res.data) {
2019-08-22 23:00:31 +02:00
println('stbi image failed to load')
2019-06-23 10:41:42 +02:00
exit(1)
2019-06-22 20:20:28 +02:00
}
return res
}
//pub fn load_from_memory(buf []byte) Image {
pub fn load_from_memory(buf byteptr) Image {
mut res := Image {
ok: true
data: 0
}
flag := C.STBI_rgb_alpha
res.data = C.stbi_load_from_memory(buf, 3812, &res.width, &res.height, &res.nr_channels, flag)
if isnil(res.data) {
println('stbi image failed to load from memory')
exit(1)
}
return res
}
2019-06-26 17:49:50 +02:00
pub fn (img Image) free() {
2019-06-22 20:20:28 +02:00
C.stbi_image_free(img.data)
}
2020-07-05 19:28:28 +02:00
/*
2019-06-26 17:49:50 +02:00
pub fn (img Image) tex_image_2d() {
2019-08-22 23:00:31 +02:00
mut rgb_flag := C.GL_RGB
2019-06-22 20:20:28 +02:00
if img.ext == 'png' {
2019-08-22 23:00:31 +02:00
rgb_flag = C.GL_RGBA
2019-06-22 20:20:28 +02:00
}
2019-08-22 23:00:31 +02:00
C.glTexImage2D(C.GL_TEXTURE_2D, 0, rgb_flag, img.width, img.height, 0,
rgb_flag, C.GL_UNSIGNED_BYTE, img.data)
2019-06-22 20:20:28 +02:00
}
2020-07-05 19:28:28 +02:00
*/
2019-06-22 20:20:28 +02:00
2019-06-26 17:49:50 +02:00
pub fn set_flip_vertically_on_load(val bool) {
2019-06-22 20:20:28 +02:00
C.stbi_set_flip_vertically_on_load(val)
}