let generate_texture font =
Sdlttf.init () ;
let char_to_string chr = Printf.sprintf "%c" chr in
let font_height = Sdlttf.font_height font in
let texture_size = 256 and
end_ascii = ascii_last_printable + 1 in
let to_tex pixel = (float pixel) /. (float texture_size) in
(* glyph map format: a vector... a char's index in the vector is
(char_to_int chr) - ascii_first_printable.
format is (texture, x texture coord,
y texture coord, width texture coord. *)
let dummy_texture = GlTex.gen_texture () in
let glyphs = Array.make (ascii_last_printable - ascii_first_printable + 1)
(dummy_texture, 0.0, 0.0, 0.0) in
let make_surface () =
Sdlvideo.create_RGB_surface []
~w:texture_size ~h:texture_size ~bpp:24
~rmask:(Int32.of_int 0x000000ff)
~gmask:(Int32.of_int 0x0000ff00)
~bmask:(Int32.of_int 0x00ff0000)
~amask:(Int32.of_int 0x00000000)
in
(* Arg, all my functional attempts at this turn ugly fast. What is
the clean way to do it? *)
let ascii = ref ascii_first_printable and
(*line = ref " and*)
surface = ref (make_surface ()) and
texture = ref (GlTex.gen_texture ()) and
x = ref 0 and
y = ref 0
in
let all_textures = ref [!texture] in
add_tex (!texture) ;
Sdlvideo.fill_rect (!surface) (Int32.of_int 0x0000) ;
(*
let blit_line () =
let render = Sdlttf.render_text_solid font (!line) (255,0,0) in
let (line_w, _, _) = Sdlvideo.surface_dims render in
Sdlvideo.blit_surface ~src:render ~dst:(!surface)
~dst_rect:(Sdlvideo.rect ~x:0 ~y:(!y) ~w:line_w ~h:font_height) () ;
()
in
*)
while !ascii <= ascii_last_printable do
let chr = char_of_int (!ascii) in
let str = char_to_string chr in
let (glyph_width, _) = Sdlttf.size_text font str in
assert (glyph_width < texture_size) ;
if (!x) + glyph_width > texture_size then begin
(*Printf.printf " (!line) ; print_newline () ;*)
(*blit_line () ;*)
x := 0 ;
y := (!y) + font_height ;
(*line := " ;*)
if !y + font_height > texture_size then begin
(*Printf.printf " ; print_newline () ;*)
y := 0 ;
ignore (surface_to_texture ~texture:(!texture) (!surface)) ;
Sdlvideo.fill_rect (!surface) (Int32.of_int 0x0000) ;
(*(Int32.zero) ;*)
(*let old_text = !texture in*)
texture := GlTex.gen_texture () ;
add_tex (!texture) ;
all_textures := (!texture) :: (!all_textures) ;
()
end else ()
end else () ;
let glyph_index = (!ascii) - ascii_first_printable in
glyphs.(glyph_index) <-
(!texture,
(to_tex (!x)),
(to_tex (!y)),
(to_tex glyph_width) ) ;
let render = Sdlttf.render_text_solid font str (255,0,0) in
let (line_w, _, _) = Sdlvideo.surface_dims render in
Sdlvideo.blit_surface ~src:render ~dst:(!surface)
~dst_rect:(Sdlvideo.rect
~x:(!x) ~y:(!y)
~w:line_w ~h:font_height) () ;
(*line := (!line) ^ str ;*)
x := (!x) + glyph_width ;
incr ascii ;
()
done ;
(*if !line <> " then blit_line () else () ;*)
ignore (surface_to_texture ~texture:(!texture) (!surface)) ;
(List.rev (!all_textures), glyphs)