1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/stbi/stbi.v

57 lines
1.2 KiB
V
Raw Normal View History

2019-06-23 05:21:30 +03:00
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
2019-06-22 21:20:28 +03:00
module stbi
2019-09-23 13:42:20 +03:00
// note we might need special case for this
// import gl
2019-06-22 21:20:28 +03:00
#flag -I @VROOT/thirdparty/stb_image
2019-06-23 15:08:17 +03:00
2019-06-22 21:20:28 +03:00
#define STB_IMAGE_IMPLEMENTATION
2019-06-23 15:08:17 +03:00
#include "stb_image.h"
2019-06-22 21:20:28 +03:00
struct Image {
mut:
width int
height int
nr_channels int
ok bool
data voidptr
ext string
}
2019-06-26 18:49:50 +03:00
pub fn load(path string) Image {
2019-06-22 21:20:28 +03:00
ext := path.all_after('.')
mut res := Image {
ok: true
ext: ext
data: 0
}
2019-08-23 00:00:31 +03: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 21:20:28 +03:00
if isnil(res.data) {
2019-08-23 00:00:31 +03:00
println('stbi image failed to load')
2019-06-23 11:41:42 +03:00
exit(1)
2019-06-22 21:20:28 +03:00
}
return res
}
2019-06-26 18:49:50 +03:00
pub fn (img Image) free() {
2019-06-22 21:20:28 +03:00
C.stbi_image_free(img.data)
}
2019-06-26 18:49:50 +03:00
pub fn (img Image) tex_image_2d() {
2019-08-23 00:00:31 +03:00
mut rgb_flag := C.GL_RGB
2019-06-22 21:20:28 +03:00
if img.ext == 'png' {
2019-08-23 00:00:31 +03:00
rgb_flag = C.GL_RGBA
2019-06-22 21:20:28 +03:00
}
2019-08-23 00:00:31 +03: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 21:20:28 +03:00
}
2019-06-26 18:49:50 +03:00
pub fn set_flip_vertically_on_load(val bool) {
2019-06-22 21:20:28 +03:00
C.stbi_set_flip_vertically_on_load(val)
}