Permalink
Browse files

Fixed it on linux, changed alpha handling to be better.

It seems to work better now, I have some glitches if you stare at the
screen for a while I don't know what is causing them.
  • Loading branch information...
1 parent ce78e47 commit 48e33677bb09570142812856f9550c9495382f76 @dgud committed Aug 31, 2009
Showing with 44 additions and 34 deletions.
  1. +1 −1 gui.erl
  2. +12 −5 hello.erl
  3. +31 −28 wx_glfont.erl
View
2 gui.erl
@@ -144,7 +144,7 @@ set_view(State) ->
gl:depthFunc(?GL_LEQUAL),
gl:enable(?GL_DEPTH_TEST),
- gl:clearColor(1.0,0.0,0.0,1.0),
+ gl:clearColor(1.0,1.0,1.0,1.0),
gl:clearDepth(State#state.zmax),
gl:matrixMode(?GL_PROJECTION),
View
17 hello.erl
@@ -10,7 +10,7 @@
-define(ZERO, 0.0).
-define(ONE, 1.0).
--record(state, {enabled=true, gl, rot=0, list, file, font20}).
+-record(state, {enabled=true, gl, rot=0, list, file, font20, font10}).
start() ->
@@ -36,11 +36,16 @@ loop0(Env, GL) ->
Font = wxFont:new(20, ?wxFONTFAMILY_SWISS, ?wxFONTSTYLE_NORMAL,
?wxFONTENCODING_ISO8859_15),
- {ok, GLFont} = wx_glfont:load_font(Font, [{name, teletype_20}]),
+ {ok, GLFont} = wx_glfont:load_font(Font, []),
+ Fixed = wxFont:new(10, ?wxFONTFAMILY_MODERN, ?wxFONTSTYLE_NORMAL,
+ ?wxFONTENCODING_ISO8859_15),
+ {ok, GLFixed} = wx_glfont:load_font(Fixed, []),
+
List = wx_glfont:make_list(GLFont, "Hello world!"),
{ok, File0} = file:read_file(?MODULE_STRING ++ ".erl"),
File = re:split(File0, "\r?\n", [{return, list}]),
- State = #state{gl=GL, list=List, file=File, font20=GLFont},
+ State = #state{gl=GL, list=List, file=File,
+ font20=GLFont, font10=GLFixed},
loop(State).
loop(State) ->
@@ -115,7 +120,7 @@ test2(State) ->
scale({Width, Height}) ->
gl:scalef(2.0/Width, 1.0/Height, 1.0).
-test3(#state{gl=GL, file=File, font20=Font}) ->
+test3(#state{gl=GL, file=File, font10=Font}) ->
%%gl:color3ub(255, 255, 255),
gl:color3ub(0, 0, 0),
{W,H} = wxWindow:getSize(GL),
@@ -128,7 +133,9 @@ test3(#state{gl=GL, file=File, font20=Font}) ->
gl:loadIdentity(),
TextH = float(wx_glfont:height(Font)),
gl:translatef(30.0, H-30.0-TextH, 0.0),
- wx_glfont:render(Font, "This is teletype_20"),
+ wx_glfont:render(Font, "This is a fixed font in size 10"),
+ gl:translatef(0.0, -TextH, 0.0),
+ wx_glfont:render(Font, "iiiiiiiiiiiiiiiiiiiiiiiiiiiiiii"),
lists:foreach(fun(Row) ->
gl:translatef(0.0, -TextH, 0.0),
wx_glfont:render(Font, Row)
View
59 wx_glfont.erl
@@ -89,33 +89,29 @@ gen_glfont(Font, AW, H, Options) ->
make_glyphs(Font,From,To,TW,TH) ->
MDC = memory_dc(Font),
- Bitmap = wxBitmap:new(TW, TH),
+ Bitmap = wxBitmap:new(TW, TH, [{depth,32}]),
ok = wxMemoryDC:selectObject(MDC, Bitmap),
BG = {0, 0, 0, 0},
Brush = wxBrush:new(BG, [{style, ?wxTRANSPARENT}]),
- %% wxMemoryDC:clear(MDC),
wxMemoryDC:setBackground(MDC, Brush),
+ wxMemoryDC:clear(MDC),
FG = {255, 255, 255, 255},
wxMemoryDC:setTextForeground(MDC, FG),
+ wxMemoryDC:setTextBackground(MDC, BG),
Glyphs = make_glyphs(MDC, From, To, 0, 0, TW, TH, array:new()),
Image = wxBitmap:convertToImage(Bitmap),
+ %%debug(Image), %% Remove the destroy lines below if debug
+
BinData = wxImage:getData(Image),
Alpha = case wxImage:hasAlpha(Image) of
- true -> wxImage:getAlpha(Image);
+ true ->
+ %%io:format("A = ~p ~n", [wxImage:hasAlpha(Image)]),
+ wxImage:getAlpha(Image);
false ->
- case wxImage:hasMask(Image) of
- true ->
- wxImage:initAlpha(Image),
- wxImage:getAlpha(Image);
- false ->
- wxImage:setMaskColour(Image, 0,0,0),
- wxImage:initAlpha(Image),
- wxImage:getAlpha(Image)
- end
+ false
end,
- %% debug(Image), %% Remove the lines below if debug
wxBrush:destroy(Brush),
wxImage:destroy(Image),
@@ -124,9 +120,9 @@ make_glyphs(Font,From,To,TW,TH) ->
greyscale(BinData, Alpha, Glyphs).
%% Minimize texture space, use greyscale images
-%% greyscale(BinData, false, Glyphs) ->
-%% Bin = << <<R:8>> || <<R:8,_:8,_:8>> <= BinData>>,
-%% {Bin, false, Glyphs};
+greyscale(BinData, false, Glyphs) -> %% Alpha use gray scale value
+ Bin = << <<255:8, A:8>> || <<A:8,_:8,_:8>> <= BinData>>,
+ {Bin, true, Glyphs};
greyscale(BinData, Alpha, Glyphs) ->
{greyscale2(BinData, Alpha, <<>>), true, Glyphs}.
@@ -157,7 +153,7 @@ make_glyph(DC, Char, X0, Y0, TW, TH, Acc0) ->
end,
wxMemoryDC:drawText(DC, [Char], {X1, Y1}),
G = #glyph{w=Width, u=X1/TW, v=(Y1)/TH},
- {array:set(Char, G, Acc0), X+1, Y}.
+ {array:set(Char, G, Acc0), X+2, Y}.
gen_texture(TW,TH,Bin,HaveAlpha,Options) ->
[TexId] = gl:genTextures(1),
@@ -224,33 +220,36 @@ render_text2(#font{tex=TexId, glyphs=Gs, height=H, ih=IH, iw=IW}, String) ->
gl:texCoordPointer(2, ?GL_FLOAT, 16, TxBin),
gl:enableClientState(?GL_VERTEX_ARRAY),
gl:enableClientState(?GL_TEXTURE_COORD_ARRAY),
- gl:drawArrays(?GL_QUADS, 0, byte_size(Bin) div 16),
+ gl:drawArrays(?GL_QUADS, 0, (byte_size(Bin)-65) div 16),
gl:disableClientState(?GL_VERTEX_ARRAY),
- gl:disableClientState(?GL_TEXTURE_COORD_ARRAY)
+ gl:disableClientState(?GL_TEXTURE_COORD_ARRAY),
+ clean(Bin,TxBin)
end,
Size.
render_text3([], _Gs, _IH, _IW, H, {W, Bin}) ->
- {{W,H},Bin};
+ %% 65 bytes so it's off heap and the binary doesn't move
+ %% if garbage collect.
+ {{W,H},<<Bin/bytes, 0:520>>};
render_text3([Char|String], Gs, IH, IW, H, Data0) ->
case array:get(Char, Gs) of
+ #glyph{}=Glyph ->
+ %%io:format("~s ~p~n",[[Char], Glyph]),
+ Data = render_glyph(Glyph,H,IW,IH,Data0),
+ render_text3(String, Gs, IH, IW, H, Data);
undefined when Char =:= 9 -> %% TAB
Space = array:get(32, Gs),
Data = lists:foldl(fun(_, Data) ->
render_glyph(Space,H,IW,IH,Data)
end, Data0, " "),
render_text3(String, Gs, IH, IW, H, Data);
undefined -> %% Should we render something strange here
- render_text3(String, Gs, IH, IW, H, Data0);
- Glyph ->
- %%io:format("~s ~p~n",[[Char], Glyph]),
- Data = render_glyph(Glyph,H,IW,IH,Data0),
- render_text3(String, Gs, IH, IW, H, Data)
+ render_text3(String, Gs, IH, IW, H, Data0)
end.
render_glyph(#glyph{u=U,v=V,w=W},H,IW,IH, {X0,Bin}) ->
X1 = X0 + W,
- UD = U + (W-1)*IW,
+ UD = U + W*IW,
VD = V + IH,
{X1,
<<Bin/binary, %% wxImage: 0,0 is upper left turn each
@@ -283,10 +282,10 @@ calc_tex_size(X, Y, No, CW, CH, Prev = {BestArea,Dec}, BestCoord)
Square = abs(Xp - Yp),
NextX = ((No-1) div (Y+1)) + 1,
if Area < BestArea ->
- %%io:format("Best is ~p ~p ~p ~n", [Area, Xp,Yp]),
+ %io:format("Best is ~p ~p ~p ~n", [Area, Xp,Yp]),
calc_tex_size(NextX, Y+1, No, CW, CH, {Area,Square}, {Xp,Yp});
Area == BestArea, Square < Dec ->
- %%io:format("Best is ~p ~p ~p ~n", [Area, Xp,Yp]),
+ %io:format("Best is ~p ~p ~p ~n", [Area, Xp,Yp]),
calc_tex_size(NextX, Y+1, No, CW, CH, {Area,Square}, {Xp,Yp});
true ->
calc_tex_size(NextX, Y+1, No, CW, CH, Prev, BestCoord)
@@ -319,6 +318,10 @@ check_pow2(NoX, W, Pow2) when NoX * W > Pow2 ->
check_pow2(NoX, W, Pow2) ->
{trunc((Pow2 - NoX*W)/W), Pow2}.
+%% Make a call with the binaries so that they don't
+%% get garbage collected until they are not needed.
+clean(Bin,_) ->
+ size(Bin).
debug(Image) ->
Frame = wxFrame:new(wx:null(), ?wxID_ANY, "DEBUG",

0 comments on commit 48e3367

Please sign in to comment.