Last active
June 13, 2021 06:41
-
-
Save pazworld/e821a334bcd562a8058d59c79b4eb6fe to your computer and use it in GitHub Desktop.
wxErlang graphic test for chip8er
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*.beam |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(collision). | |
-export([start/0]). | |
-include_lib("wx/include/wx.hrl"). | |
start() -> | |
wx:new(), | |
F = wxFrame:new(wx:null(), -1, "collision test"), | |
wxFrame:show(F), | |
DC = wxClientDC:new(F), | |
wxDC:setBackground(DC, wxBrush:new(?wxBLACK)), | |
wxDC:clear(DC), | |
wxDC:setUserScale(DC, 16, 16), | |
draw_block(DC, 8, 4), | |
draw_block(DC, 16, 4), | |
X1 = lists:seq(0, 24), | |
X2 = lists:reverse(X1), | |
lists:map(fun(X) -> | |
collision_light(DC, is_collision(DC, X, 2)), | |
draw_runner(DC, X, 2), | |
timer:sleep(100), | |
draw_runner(DC, X, 2) | |
end, X1 ++ X2 ++ X1 ++ X2), | |
ok. | |
draw_block(DC, X, Y) -> | |
wxDC:blit(DC, {X, Y}, {4, 4}, block_dc(), {0, 0}). | |
draw_runner(DC, X, Y) -> | |
wxDC:blit(DC, {X, Y}, {8, 9}, runner_dc(), {0, 0}, [{rop, ?wxXOR}]). | |
block_dc() -> | |
ImgBin = [ | |
2#11110000, | |
2#11110000, | |
2#11110000, | |
2#11110000 | |
], | |
make_image_dc(ImgBin, 4). | |
runner_dc() -> | |
ImgBin = [ | |
2#00011000, | |
2#00011000, | |
2#01110000, | |
2#10011000, | |
2#00100100, | |
2#00100000, | |
2#11010000, | |
2#00010000, | |
2#00010000 | |
], | |
make_image_dc(ImgBin, 9). | |
make_image_dc(ImgBin, Height) -> | |
ImgBinStr = | |
lists:flatten([tl(integer_to_list(X + 16#100, 2)) || X <- ImgBin]), | |
ImgRGB = list_to_binary(lists:map( | |
fun(X) -> X2 = (X - 48) * 255, [X2, X2, X2] end, ImgBinStr)), | |
Img = wxImage:new(8, Height), | |
wxImage:setData(Img, ImgRGB), | |
Bmp = wxBitmap:new(Img), | |
DC = wxMemoryDC:new(), | |
wxMemoryDC:selectObject(DC, Bmp), | |
DC. | |
is_collision(DC, X, Y) -> | |
{Bmp, BDC} = store_bitmap(DC, X, Y), | |
and_bitmap(BDC), | |
have_white_pixel(Bmp). | |
store_bitmap(DC, X, Y) -> | |
Bmp = wxBitmap:new(8, 9), | |
BDC = wxMemoryDC:new(), | |
wxMemoryDC:selectObject(BDC, Bmp), | |
wxDC:blit(BDC, {0, 0}, {8, 9}, DC, {X, Y}), | |
{Bmp, BDC}. | |
and_bitmap(BDC) -> | |
wxDC:blit(BDC, {0, 0}, {8, 9}, runner_dc(), {0, 0}, [{rop, ?wxAND}]). | |
have_white_pixel(Bmp) -> | |
Img = wxBitmap:convertToImage(Bmp), | |
RGB = binary:bin_to_list(wxImage:getData(Img)), | |
lists:any(fun(X) -> X =/= 0 end, RGB). | |
collision_light(DC, On) -> | |
Color = case On of | |
true -> ?wxRED; | |
_ -> ?wxBLACK | |
end, | |
wxDC:setPen(DC, wxPen:new(?wxBLACK, [{style, ?wxTRANSPARENT}])), | |
wxDC:setBrush(DC, wxBrush:new(Color)), | |
wxDC:drawRectangle(DC, {0, 0, 1, 1}), | |
ok. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(drawtest). | |
-compile(export_all). | |
setup() -> | |
wx:new(), | |
F = wxFrame:new(wx:null(), -1, "drawtest", [{size, {600, 600}}]), | |
wxFrame:show(F), | |
DC = wxClientDC:new(F), | |
Pen = wxPen:new({0, 0, 0}), | |
wxDC:setPen(DC, Pen), | |
Fun = fun(X, Y) -> | |
wxDC:drawRectangle(DC, {X * 8, Y * 16, 8, 16}) | |
end, | |
[Fun(X, Y) || Y <- lists:seq(0, 31), X <- lists:seq(0, 63)], | |
DC. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(listboxtest). | |
-export([start/0]). | |
-include_lib("wx/include/wx.hrl"). | |
start() -> | |
wx:new(), | |
F = wxFrame:new(wx:null(), -1, "ListBox test"), | |
B = wxListBox:new(F, -1, [{size, {10, 10}}]), | |
wxFrame:show(F), | |
wxListBox:set(B, items()), | |
wxListBox:setSelection(B, 999), | |
wxListBox:setFirstItem(B, 999 - 5), | |
ok. | |
items() -> | |
[integer_to_list(X) || X <- lists:seq(0, 1000)]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% This program is written with | |
% https://python-minutes.blogspot.com/2017/06/pythongui-listctrl.html | |
% as a reference. | |
-module(listctrltest). | |
-export([start/0]). | |
-include_lib("wx/include/wx.hrl"). | |
start() -> | |
setup_frame(), | |
ok. | |
setup_frame() -> | |
wx:new(), | |
Frame = wxFrame:new(wx:null(), -1, "ListCtrl test"), | |
wxFrame:createStatusBar(Frame), | |
setup_listctrl(Frame), | |
wxFrame:show(Frame). | |
setup_listctrl(Frame) -> | |
ListCtrl = wxListCtrl:new(Frame, [{style, ?wxLC_REPORT}]), | |
add_listctrl_column(ListCtrl), | |
add_listctrl_data(ListCtrl), | |
add_listctrl_event(Frame, ListCtrl), | |
ok. | |
add_listctrl_column(ListCtrl) -> | |
wxListCtrl:insertColumn(ListCtrl, 0, "Name", [{width, 150}]), | |
wxListCtrl:insertColumn(ListCtrl, 1, "Age", [{width, 100}]). | |
add_listctrl_data(ListCtrl) -> | |
lists:foreach(fun(X) -> | |
wxListCtrl:insertItem(ListCtrl, X, "Yamada " ++ [$0 + X + 1] ++ " rou"), | |
wxListCtrl:setItem(ListCtrl, X, 1, integer_to_list((X + 1) * 5)) | |
end, lists:seq(0, 2)). | |
add_listctrl_event(Frame, ListCtrl) -> | |
wxListCtrl:connect(ListCtrl, command_list_item_selected, | |
[{callback, fun(_, Event) -> on_listitem_selected(Frame, ListCtrl, Event) end}]). | |
on_listitem_selected(Frame, ListCtrl, Event) -> | |
Index = wxListEvent:getIndex(Event), | |
Str = wxListCtrl:getItemText(ListCtrl, Index) ++ " is selected.", | |
wxFrame:setStatusText(Frame, Str). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% this code is from https://github.com/arifishaq/wxerlang/blob/master/speeding_up/drawing/paint_event_capture.erl | |
-module(paint_event_capture). | |
-behaviour(wx_object). | |
-include_lib("wx/include/wx.hrl"). | |
-export([start_link/0]). | |
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, | |
handle_event/2, terminate/2, code_change/3]). | |
start_link() -> | |
wx_object:start_link(?MODULE, [], []). | |
init([]) -> | |
wx:new(), | |
Frame = wxFrame:new(wx:null(), ?wxID_ANY, "paint event capture"), | |
wxFrame:connect(Frame, paint), %% subscribing to the paint event | |
wxFrame:show(Frame), | |
{Frame, #{frame => Frame}}. | |
handle_event(#wx{} = Event, State) -> | |
io:format("got ~p~n", [Event]), | |
{noreply, State}. | |
handle_call(_Request, _From, State) -> | |
Reply = ok, | |
{reply, Reply, State}. | |
handle_cast(_Msg, State) -> | |
{noreply, State}. | |
handle_info(_Info, State) -> | |
{noreply, State}. | |
terminate(_Reason, _State) -> | |
wx:destroy(). | |
code_change(_OldVsn, State, _Extra) -> | |
{ok, State}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(painttest). | |
-export([start/0]). | |
-include_lib("wx/include/wx.hrl"). | |
start() -> | |
wx:new(), | |
F = wxFrame:new(wx:null(), ?wxID_ANY, "paint test"), | |
DC = wxClientDC:new(F), | |
wxFrame:connect(F, paint, [{callback, fun(_, _) -> on_paint(DC) end}]), | |
wxFrame:show(F), | |
ok. | |
on_paint(DC) -> | |
io:format("paint ~n"), | |
wxDC:drawRectangle(DC, {100, 100, 100, 100}), | |
ok. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(scale). | |
-export([start/0]). | |
-include_lib("wx/include/wx.hrl"). | |
start() -> | |
wx:new(), | |
F = wxFrame:new(wx:null(), -1, "scale test", [{size, {600, 600}}]), | |
wxFrame:show(F), | |
DC = wxClientDC:new(F), | |
wxDC:setPen(DC, wxPen:new(?wxBLACK, [{style, ?wxTRANSPARENT}])), | |
wxDC:setBrush(DC, wxBrush:new(?wxWHITE)), | |
wxDC:setUserScale(DC, 16, 8), | |
MDC = wxMemoryDC:new(), | |
Bmp = make_bitmap(), | |
wxMemoryDC:selectObject(MDC, Bmp), | |
wxDC:blit(DC, {2, 2}, {8, 16}, MDC, {0, 0}), | |
wxDC:blit(DC, {12, 2}, {8, 16}, MDC, {0, 0}), | |
wxDC:blit(DC, {22, 2}, {8, 16}, MDC, {0, 0}), | |
wxDC:drawRectangle(DC, {2, 20, 28, 8}), | |
DC. | |
make_bitmap() -> | |
ImgBin = [ | |
2#00000001, % #######_ | |
2#11111110, % _______# | |
2#11000110, % __###__# | |
2#10111010, % _#___#_# | |
2#10111010, % _#___#_# | |
2#10000010, % _#####_# | |
2#10111110, % _#_____# | |
2#10111110, % _#_____# | |
2#10111110, % _#_____# | |
2#10111110, % _#_____# | |
2#11011010, % __#__#_# | |
2#11100110, % ___##__# | |
2#11111110, % _______# | |
2#11111110, % _______# | |
2#00000001, % #######_ | |
2#11111111 % ________ | |
], | |
ImgBinStr = | |
lists:flatten([tl(integer_to_list(X + 16#100, 2)) || X <- ImgBin]), | |
ImgRGB = list_to_binary(lists:map( | |
fun(X) -> X2 = (X - 48) * 255, [X2, X2, X2] end, ImgBinStr)), | |
Img = wxImage:new(8, 16), | |
wxImage:setData(Img, ImgRGB), | |
wxBitmap:new(Img). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% this code is from https://arifishaq.files.wordpress.com/2018/04/wxerlang-speeding-up.pdf | |
-module(speedup). | |
-compile(export_all). | |
test1() -> | |
wx:new(), | |
Frame = wxFrame:new(wx:null(), -1, "test1"), | |
wxFrame:show(Frame), | |
DC = wxWindowDC:new(Frame), | |
wxDC:drawText(DC, "wxErlang is cool!", {50, 50}), | |
wxDC:drawLine(DC, {50, 70}, {150, 70}), | |
Pen = wxPen:new({255, 0, 0}, [{width, 3}, {style, 104}]), | |
wxDC:setPen(DC, Pen), | |
wxDC:drawLine(DC, {50,80}, {150,80}), | |
wxDC:drawRectangle(DC, {50,90,100,20}), | |
Brush = wxBrush:new({0,0,255}, [{style, 112}]), | |
wxDC:setBrush(DC, Brush), | |
wxDC:drawRectangle(DC, {200,50,100,20}), | |
wxDC:setClippingRegion(DC, {70, 30, 30, 100}), | |
wxDC:setLogicalFunction(DC, 2), %% 2 = wxINVERT | |
YellowBrush = wxBrush:new({255,255,0}, [{style,100}]), %% 100 = wxSOLID | |
wxDC:setBrush(DC, YellowBrush), | |
wxDC:drawRectangle(DC, {0,0,200,200}), | |
DC. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(sprite). | |
-compile(export_all). | |
-include_lib("wx/include/wx.hrl"). | |
start() -> | |
wx:new(), | |
ImgBin = [ | |
2#11111110, % #######_ | |
2#00000001, % _______# | |
2#00111001, % __###__# | |
2#01000101, % _#___#_# | |
2#01000101, % _#___#_# | |
2#01111101, % _#####_# | |
2#01000001, % _#_____# | |
2#01000001, % _#_____# | |
2#01000001, % _#_____# | |
2#01000001, % _#_____# | |
2#00100101, % __#__#_# | |
2#00011001, % ___##__# | |
2#00000001, % _______# | |
2#11111110, % #######_ | |
2#00000000 | |
], | |
ImgBinStr = | |
lists:flatten([tl(integer_to_list(X + 16#100, 2)) || X <- ImgBin]), | |
ImgRGB = list_to_binary(lists:map( | |
fun(X) -> X2 = (X - 48) * 255, [X2, X2, X2] end, ImgBinStr)), | |
Img = wxImage:new(8, 16), | |
wxImage:setData(Img, ImgRGB), | |
Bmp = wxBitmap:new(Img), | |
MDC = wxMemoryDC:new(), | |
wxMemoryDC:selectObject(MDC, Bmp), | |
F = wxFrame:new(wx:null(), -1, "sprite test", [{size, {600, 600}}]), | |
wxFrame:show(F), | |
DC = wxClientDC:new(F), | |
[wxDC:blit(DC, {X * 8, Y * 16}, {8, 16}, MDC, {0, 0}) | |
|| Y <- lists:seq(0, 31), X <- lists:seq(0, 63)]. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(xortest). | |
-export([start/0]). | |
-include_lib("wx/include/wx.hrl"). | |
start() -> | |
wx:new(), | |
F = wxFrame:new(wx:null(), -1, "xor test"), | |
wxFrame:show(F), | |
DC = wxClientDC:new(F), | |
wxDC:setBackground(DC, wxBrush:new(?wxBLACK)), | |
wxDC:clear(DC), | |
wxDC:setUserScale(DC, 16, 16), | |
DrawFunc = fun(X, Y) -> | |
wxDC:blit(DC, {X, Y}, {4, 4}, make_bmpdc(), {0, 0}, [{rop, ?wxXOR}]) | |
end, | |
X1 = lists:seq(0, 24), | |
X2 = lists:reverse(X1), | |
DrawFunc(8, 4), | |
DrawFunc(16, 4), | |
lists:map(fun(X) -> | |
DrawFunc(X, 2), | |
timer:sleep(100), | |
DrawFunc(X, 2) | |
end, X1 ++ X2). | |
make_bmpdc() -> | |
ImgBin = list_to_binary([255 || _ <- lists:seq(1, 16 * 3 * 4)]), | |
Img = wxImage:new(4, 4), | |
wxImage:setData(Img, ImgBin), | |
Bmp = wxBitmap:new(Img), | |
BmpDC = wxMemoryDC:new(), | |
wxMemoryDC:selectObject(BmpDC, Bmp), | |
BmpDC. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment