I'm attempting to use a Graphics Context with wxLua. My understanding is that I need to:
1. Draw to the Graphics Context, using a member function, like gc:DrawRectangle(50, 50, 500, 500)
2. At the end of drawing all the things I would like to draw on the Graphics Context, I then need to draw this into a bitmap, like gc:DrawBitmap(bitmap, 0, 0, 1920, 1080).
3. In the Paint Handler event for my frame, I use a regular PaintDC to draw that bitmap onto the frame.
When I run the attached code, I only get a black bitmap. Could someone please explain to me what I'm doing wrong?
Thanks
Code: Select all
require "wx"
red = wx.wxColour(255, 0, 0, 255)
green = wx.wxColour(0, 255, 0, 255)
local screenSizeX, screenSizeY = 1920, 1080
local windowSizeX, windowSizeY = 1920, 1080
local windowPositionX, windowPositionY = 0, 50
local pen = wx.wxPen(wx.wxBLACK_PEN)
local brush = wx.wxBrush(wx.wxBLACK_BRUSH)
local bitmap = wx.wxBitmap(1920, 1080)
local dc = wx.wxMemoryDC()
local frame = wx.wxFrame(
wx.NULL, -- Parent Window
wx.wxID_ANY, -- Window ID
"Graphics Window", -- Name
wx.wxPoint(0 + windowPositionX, 0 + windowPositionY),
wx.wxSize(windowSizeX, windowSizeY),
wx.wxDEFAULT_FRAME_STYLE + wx.wxSTAY_ON_TOP- wx.wxRESIZE_BORDER - wx.wxMAXIMIZE_BOX
)
local gc = wx.wxGraphicsContext.Create(dc)
local function onPaint()
local dc = wx.wxPaintDC(frame)
dc:DrawBitmap(bitmap, 0, 0, true)
dc:delete()
end
frame:Connect(wx.wxEVT_PAINT, onPaint)
frame:Connect(wx.wxEVT_ERASE_BACKGROUND, function () end)
frame:Connect(wx.wxEVT_CLOSE_WINDOW, function(event) os.exit() end)
frame:Show(true)
while true do
pen:SetColour(red)
brush:SetColour(green)
gc:SetPen(pen)
gc:SetBrush(brush)
gc:DrawRectangle(50, 50, 500, 500)
gc:DrawBitmap(bitmap, 0, 0, 1920, 1080)
frame:Refresh()
frame:Update()
wx.wxGetApp():MainLoop()
end