Skip to content

Commit

Permalink
Fixed it on linux, changed alpha handling to be better.
Browse files Browse the repository at this point in the history
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
dgud committed Aug 31, 2009
1 parent ce78e47 commit 48e3367
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 34 deletions.
2 changes: 1 addition & 1 deletion gui.erl
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ set_view(State) ->


gl:depthFunc(?GL_LEQUAL), gl:depthFunc(?GL_LEQUAL),
gl:enable(?GL_DEPTH_TEST), 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:clearDepth(State#state.zmax),


gl:matrixMode(?GL_PROJECTION), gl:matrixMode(?GL_PROJECTION),
Expand Down
17 changes: 12 additions & 5 deletions hello.erl
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
-define(ZERO, 0.0). -define(ZERO, 0.0).
-define(ONE, 1.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() -> start() ->
Expand All @@ -36,11 +36,16 @@ loop0(Env, GL) ->


Font = wxFont:new(20, ?wxFONTFAMILY_SWISS, ?wxFONTSTYLE_NORMAL, Font = wxFont:new(20, ?wxFONTFAMILY_SWISS, ?wxFONTSTYLE_NORMAL,
?wxFONTENCODING_ISO8859_15), ?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!"), List = wx_glfont:make_list(GLFont, "Hello world!"),
{ok, File0} = file:read_file(?MODULE_STRING ++ ".erl"), {ok, File0} = file:read_file(?MODULE_STRING ++ ".erl"),
File = re:split(File0, "\r?\n", [{return, list}]), 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).


loop(State) -> loop(State) ->
Expand Down Expand Up @@ -115,7 +120,7 @@ test2(State) ->
scale({Width, Height}) -> scale({Width, Height}) ->
gl:scalef(2.0/Width, 1.0/Height, 1.0). 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(255, 255, 255),
gl:color3ub(0, 0, 0), gl:color3ub(0, 0, 0),
{W,H} = wxWindow:getSize(GL), {W,H} = wxWindow:getSize(GL),
Expand All @@ -128,7 +133,9 @@ test3(#state{gl=GL, file=File, font20=Font}) ->
gl:loadIdentity(), gl:loadIdentity(),
TextH = float(wx_glfont:height(Font)), TextH = float(wx_glfont:height(Font)),
gl:translatef(30.0, H-30.0-TextH, 0.0), 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) -> lists:foreach(fun(Row) ->
gl:translatef(0.0, -TextH, 0.0), gl:translatef(0.0, -TextH, 0.0),
wx_glfont:render(Font, Row) wx_glfont:render(Font, Row)
Expand Down
59 changes: 31 additions & 28 deletions wx_glfont.erl
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -89,33 +89,29 @@ gen_glfont(Font, AW, H, Options) ->


make_glyphs(Font,From,To,TW,TH) -> make_glyphs(Font,From,To,TW,TH) ->
MDC = memory_dc(Font), MDC = memory_dc(Font),
Bitmap = wxBitmap:new(TW, TH), Bitmap = wxBitmap:new(TW, TH, [{depth,32}]),
ok = wxMemoryDC:selectObject(MDC, Bitmap), ok = wxMemoryDC:selectObject(MDC, Bitmap),


BG = {0, 0, 0, 0}, BG = {0, 0, 0, 0},
Brush = wxBrush:new(BG, [{style, ?wxTRANSPARENT}]), Brush = wxBrush:new(BG, [{style, ?wxTRANSPARENT}]),
%% wxMemoryDC:clear(MDC),
wxMemoryDC:setBackground(MDC, Brush), wxMemoryDC:setBackground(MDC, Brush),
wxMemoryDC:clear(MDC),


FG = {255, 255, 255, 255}, FG = {255, 255, 255, 255},
wxMemoryDC:setTextForeground(MDC, FG), wxMemoryDC:setTextForeground(MDC, FG),
wxMemoryDC:setTextBackground(MDC, BG),
Glyphs = make_glyphs(MDC, From, To, 0, 0, TW, TH, array:new()), Glyphs = make_glyphs(MDC, From, To, 0, 0, TW, TH, array:new()),
Image = wxBitmap:convertToImage(Bitmap), Image = wxBitmap:convertToImage(Bitmap),
%%debug(Image), %% Remove the destroy lines below if debug

BinData = wxImage:getData(Image), BinData = wxImage:getData(Image),
Alpha = case wxImage:hasAlpha(Image) of Alpha = case wxImage:hasAlpha(Image) of
true -> wxImage:getAlpha(Image); true ->
%%io:format("A = ~p ~n", [wxImage:hasAlpha(Image)]),
wxImage:getAlpha(Image);
false -> false ->
case wxImage:hasMask(Image) of false
true ->
wxImage:initAlpha(Image),
wxImage:getAlpha(Image);
false ->
wxImage:setMaskColour(Image, 0,0,0),
wxImage:initAlpha(Image),
wxImage:getAlpha(Image)
end
end, end,
%% debug(Image), %% Remove the lines below if debug


wxBrush:destroy(Brush), wxBrush:destroy(Brush),
wxImage:destroy(Image), wxImage:destroy(Image),
Expand All @@ -124,9 +120,9 @@ make_glyphs(Font,From,To,TW,TH) ->
greyscale(BinData, Alpha, Glyphs). greyscale(BinData, Alpha, Glyphs).


%% Minimize texture space, use greyscale images %% Minimize texture space, use greyscale images
%% greyscale(BinData, false, Glyphs) -> greyscale(BinData, false, Glyphs) -> %% Alpha use gray scale value
%% Bin = << <<R:8>> || <<R:8,_:8,_:8>> <= BinData>>, Bin = << <<255:8, A:8>> || <<A:8,_:8,_:8>> <= BinData>>,
%% {Bin, false, Glyphs}; {Bin, true, Glyphs};
greyscale(BinData, Alpha, Glyphs) -> greyscale(BinData, Alpha, Glyphs) ->
{greyscale2(BinData, Alpha, <<>>), true, Glyphs}. {greyscale2(BinData, Alpha, <<>>), true, Glyphs}.


Expand Down Expand Up @@ -157,7 +153,7 @@ make_glyph(DC, Char, X0, Y0, TW, TH, Acc0) ->
end, end,
wxMemoryDC:drawText(DC, [Char], {X1, Y1}), wxMemoryDC:drawText(DC, [Char], {X1, Y1}),
G = #glyph{w=Width, u=X1/TW, v=(Y1)/TH}, 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) -> gen_texture(TW,TH,Bin,HaveAlpha,Options) ->
[TexId] = gl:genTextures(1), [TexId] = gl:genTextures(1),
Expand Down Expand Up @@ -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:texCoordPointer(2, ?GL_FLOAT, 16, TxBin),
gl:enableClientState(?GL_VERTEX_ARRAY), gl:enableClientState(?GL_VERTEX_ARRAY),
gl:enableClientState(?GL_TEXTURE_COORD_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_VERTEX_ARRAY),
gl:disableClientState(?GL_TEXTURE_COORD_ARRAY) gl:disableClientState(?GL_TEXTURE_COORD_ARRAY),
clean(Bin,TxBin)
end, end,
Size. Size.


render_text3([], _Gs, _IH, _IW, H, {W, Bin}) -> 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) -> render_text3([Char|String], Gs, IH, IW, H, Data0) ->
case array:get(Char, Gs) of 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 undefined when Char =:= 9 -> %% TAB
Space = array:get(32, Gs), Space = array:get(32, Gs),
Data = lists:foldl(fun(_, Data) -> Data = lists:foldl(fun(_, Data) ->
render_glyph(Space,H,IW,IH,Data) render_glyph(Space,H,IW,IH,Data)
end, Data0, " "), end, Data0, " "),
render_text3(String, Gs, IH, IW, H, Data); render_text3(String, Gs, IH, IW, H, Data);
undefined -> %% Should we render something strange here undefined -> %% Should we render something strange here
render_text3(String, Gs, IH, IW, H, Data0); 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)
end. end.


render_glyph(#glyph{u=U,v=V,w=W},H,IW,IH, {X0,Bin}) -> render_glyph(#glyph{u=U,v=V,w=W},H,IW,IH, {X0,Bin}) ->
X1 = X0 + W, X1 = X0 + W,
UD = U + (W-1)*IW, UD = U + W*IW,
VD = V + IH, VD = V + IH,
{X1, {X1,
<<Bin/binary, %% wxImage: 0,0 is upper left turn each <<Bin/binary, %% wxImage: 0,0 is upper left turn each
Expand Down Expand Up @@ -283,10 +282,10 @@ calc_tex_size(X, Y, No, CW, CH, Prev = {BestArea,Dec}, BestCoord)
Square = abs(Xp - Yp), Square = abs(Xp - Yp),
NextX = ((No-1) div (Y+1)) + 1, NextX = ((No-1) div (Y+1)) + 1,
if Area < BestArea -> 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}); calc_tex_size(NextX, Y+1, No, CW, CH, {Area,Square}, {Xp,Yp});
Area == BestArea, Square < Dec -> 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}); calc_tex_size(NextX, Y+1, No, CW, CH, {Area,Square}, {Xp,Yp});
true -> true ->
calc_tex_size(NextX, Y+1, No, CW, CH, Prev, BestCoord) calc_tex_size(NextX, Y+1, No, CW, CH, Prev, BestCoord)
Expand Down Expand Up @@ -319,6 +318,10 @@ check_pow2(NoX, W, Pow2) when NoX * W > Pow2 ->
check_pow2(NoX, W, Pow2) -> check_pow2(NoX, W, Pow2) ->
{trunc((Pow2 - NoX*W)/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) -> debug(Image) ->
Frame = wxFrame:new(wx:null(), ?wxID_ANY, "DEBUG", Frame = wxFrame:new(wx:null(), ?wxID_ANY, "DEBUG",
Expand Down

0 comments on commit 48e3367

Please sign in to comment.