let surface_to_texture ?texture surface =
Init.init_sdl_once () ;
let surface_fmt_to_pix surface =
let bits = Sdlvideo.surface_bpp surface in
if bits = 24 then `rgb else
if bits = 32 then `rgba else
raise (Invalid_surface_format ((Printf.sprintf "I don't know how to handle an image depth of %d bits. Please convert to 24bit RGB or 32bit RGBA format." bits),bits))
in
let surface_info = Sdlvideo.surface_info surface in
let pix_fmt = surface_fmt_to_pix surface in
let texture_id = match texture with
None -> print_endline "gentex" ; GlTex.gen_texture ()
| Some texture_id -> print_endline "usetex" ; texture_id
in
let pix = GlPix.of_raw (surface_to_raw surface)
~format:(surface_fmt_to_pix surface)
~width:surface_info.Sdlvideo.w
~height:surface_info.Sdlvideo.h
in
GlTex.bind_texture `texture_2d texture_id ;
GlTex.image2d pix ;
GlTex.parameter `texture_2d (`mag_filter `linear) ;
GlTex.parameter `texture_2d (`min_filter `linear) ;
texture_id