-
Notifications
You must be signed in to change notification settings - Fork 0
/
gl_texture.ml
46 lines (37 loc) · 1.5 KB
/
gl_texture.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
open Tsdl
open Gl_utils
open Tgl4
type t = {tid: int; surface: Sdl.surface; width: int; height: int}
let width t = t.width
let height t = t.height
let init () =
let open Tsdl_image in
let img_flags = Image.Init.(jpg + png) in
if Image.init img_flags = img_flags then Result.ok ()
else Result.error (`Msg "Failed to load JPG/PNG support")
let terminate () = Tsdl_image.Image.quit ()
let create_from_surface raw_surface =
let kind = Bigarray.Int8_unsigned in
let dest_format = Sdl.Pixel.format_argb8888 in
let* surface = Sdl.convert_surface_format raw_surface dest_format in
Sdl.free_surface raw_surface ;
let* _ = Sdl.lock_surface surface in
let width, height = Sdl.get_surface_size surface in
let pixels = Sdl.get_surface_pixels surface kind in
let tid = get_int (Gl.gen_textures 1) in
Gl.bind_texture Gl.texture_2d tid ;
Gl.tex_parameteri Gl.texture_2d Gl.texture_min_filter Gl.linear ;
Gl.tex_parameteri Gl.texture_2d Gl.texture_wrap_s Gl.repeat ;
Gl.tex_parameteri Gl.texture_2d Gl.texture_wrap_t Gl.repeat ;
Gl.tex_parameteri Gl.texture_2d Gl.texture_mag_filter Gl.linear ;
Gl.tex_image2d Gl.texture_2d 0 Gl.rgba width height 0 Gl.bgra Gl.unsigned_byte
(`Data pixels) ;
Gl.bind_texture Gl.texture_2d 0 ;
Ok {tid; surface; width; height}
let create_from_bmp path =
let* surface = Tsdl_image.Image.load path in
create_from_surface surface
let delete t =
set_int (Gl.delete_textures 1) t.tid ;
Sdl.free_surface t.surface
let bind t = Gl.bind_texture Gl.texture_2d t.tid