2019-06-23 04:21:30 +02: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 20:20:28 +02:00
|
|
|
module stbi
|
|
|
|
|
|
|
|
import gl
|
|
|
|
|
2019-06-25 14:24:12 +02:00
|
|
|
#flag linux -I @VROOT/thirdparty/stb_image
|
|
|
|
#flag darwin -I @VROOT/thirdparty/stb_image
|
2019-06-23 14:08:17 +02:00
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
#define STB_IMAGE_IMPLEMENTATION
|
2019-06-23 14:08:17 +02:00
|
|
|
#include "stb_image.h"
|
2019-06-22 20:20:28 +02:00
|
|
|
struct Image {
|
|
|
|
mut:
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
nr_channels int
|
|
|
|
ok bool
|
|
|
|
data voidptr
|
|
|
|
ext string
|
|
|
|
}
|
|
|
|
|
|
|
|
import const (
|
|
|
|
GL_RGBA
|
|
|
|
GL_RGB
|
|
|
|
GL_UNSIGNED_BYTE
|
|
|
|
GL_TEXTURE_2D
|
|
|
|
STBI_rgb_alpha
|
|
|
|
)
|
|
|
|
|
|
|
|
fn load(path string) Image {
|
|
|
|
ext := path.all_after('.')
|
|
|
|
mut res := Image {
|
|
|
|
ok: true
|
|
|
|
ext: ext
|
|
|
|
data: 0
|
|
|
|
}
|
|
|
|
if ext == 'png' {
|
|
|
|
res.data = C.stbi_load(path.str, &res.width, &res.height, &res.nr_channels, STBI_rgb_alpha)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res.data = C.stbi_load(path.str, &res.width, &res.height, &res.nr_channels, 0)
|
|
|
|
}
|
|
|
|
if isnil(res.data) {
|
2019-06-23 10:41:42 +02:00
|
|
|
println('stbi cant load')
|
|
|
|
exit(1)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (img Image) free() {
|
|
|
|
C.stbi_image_free(img.data)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (img Image) tex_image_2d() {
|
|
|
|
mut rgb_flag := GL_RGB
|
|
|
|
if img.ext == 'png' {
|
|
|
|
rgb_flag = GL_RGBA
|
|
|
|
}
|
|
|
|
C.glTexImage2D(GL_TEXTURE_2D, 0, rgb_flag, img.width, img.height, 0, rgb_flag, GL_UNSIGNED_BYTE,
|
|
|
|
img.data)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn set_flip_vertically_on_load(val bool) {
|
|
|
|
C.stbi_set_flip_vertically_on_load(val)
|
|
|
|
}
|
|
|
|
|