Murga-Projects Forums
drawing graphics in murgaLua - Printable Version

+- Murga-Projects Forums (http://www.murga-projects.com/forum)
+-- Forum: Project Forums (/forumdisplay.php?fid=1)
+--- Forum: MurgaLua - General (/forumdisplay.php?fid=2)
+--- Thread: drawing graphics in murgaLua (/showthread.php?tid=38)


drawing graphics in murgaLua - widged - 06-05-2007 11:01 PM

After reading information on drawing functions, I tried this

Code:
ww=180 --window width
wh=200 --window height

w=fltk:Fl_Window(ww,wh,"murgaLua Drawing Demo")
w:color(55)

-- fltk void fl_color(Fl_Color)
--- Set the color for all subsequent drawing operations.
fltk.fl_color(96)

-- fltk  void fl_rectf(int x, int y, int w, int h)
--- Color a rectangle that exactly fills the given bounding box.
fltk.fl_rect(20,20,50,50)

w:show()
Fl:run()


This doesn't do anything. Not implemented yet? I had come across a list of stuff not implemented yet but cannot find it again.


RE: drawing graphics in murgaLua - mikshaw - 06-06-2007 02:00 AM

I tried the same thing several months ago, and it didn't do anything =o)
You're probably right about it not being implemented.

There is a list in the murgaLua distribution archive, doc/other
fl_draw_pixmap
fl_draw_image_mono
fl_draw_image
fl_draw
are listed as unimplemented.
fl_rectf is not listed, but i think it relies on fl_draw?


RE: drawing graphics in murgaLua - JohnMurga - 06-22-2007 11:39 PM

Hi,

Those functions are now supported (I have to update the list), however if you look at the FLTK reference they have to be within a widget (I have to find a workaround, or maybe we can use callbacks) ... Either way here I have a test that produces some amusing results under Win32 :

Code:
w=fltk:Fl_Window(0,0, 200, 200, "draw demo")

w:color(55)

fltk.fl_color(96)

b=fltk:Fl_Box(30,30, 100, 100, "Test box")

w:make_current()
w:show()

fltk.fl_rectf(20,20,50,50)
fltk.fl_draw("this is a test",20,18)

fltk.fl_rectf(20,300,50,50)
fltk.fl_draw("this is a test",20,300)

Fl:run()




RE: drawing graphics in murgaLua - JohnMurga - 06-23-2007 12:53 AM

OK, it kinda works now ...

I decided to have a quick go before I got my things ready for this weekend.

Try running this and pressing the ESC key :

Code:
function draw_callback(eventValue)

  fltk.fl_color(96)

  w:make_current()

  fltk.fl_rectf(20,20,50,50)
  fltk.fl_draw("this is a test",20,18)

end

w=fltk:Fl_Window(0,0, 200, 200, "draw demo")

w:callback(draw_callback)

w:color(55)

w:show()

while 1 do

    Fl:check()

end


Now the thing is that we should be hooking this into the idle callback or a timeout for animations, but you'll have to wait for version 0.5 for this kinda stuff :-) ...

Cheers
JohnM


RE: drawing graphics in murgaLua - mikshaw - 06-23-2007 06:00 AM

That's pretty cool!
I'm not sure what the while loop does, though. I had to force the app to close, and received this message (not an error, but a message):

Quote:
/usr/bin/murgaLua: ./draw.lua:26: interrupted!
stack traceback:
[C]: in function 'check'
./draw.lua:26: in main chunk
[C]: ?

After replacing the loop with Fl:run() it still required a force close (due to the w:callback I assume), but no message.
I'm not sure if the force close is a problem. My window manager doesn't supply the typical titlebar buttons, but uses a key combo instead to close apps. I assume the key combo passes the same signal as a button, though, but it didn't close the app.

Anyway, I was messing around with it for a few minutes trying to see if I could animate it at all. I was able to repeat the draw_callback function with a repeat button just for testing purposes, and set the rectf position a little to the right each time. The drawback is that I didn't spend enough time with it to explore whether or not the previous rectf could be removed. The result was something that resembled a progress bar.

This is going to be a very fun thing to experiment with.


RE: drawing graphics in murgaLua - JohnMurga - 06-23-2007 07:00 AM

mikshaw Wrote:
That's pretty cool!

OK, what I have now is cooler :-) :-)

mikshaw Wrote:
/usr/bin/murgaLua: ./draw.lua:26: interrupted!
stack traceback:
[C]: in function 'check'
./draw.lua:26: in main chunk
[C]: ?

That is normal as you where interrupting a busy loop ... What I did before was a REAL quick hack.

Here is another hack, but this is pretty cool as drawing now works propperly !!!

Code:
function draw_callback(eventValue)

  if (Fl:event() == 0)
  then
      -- 0 means no even I think, so we know we are requesting a draw.
      fltk.fl_color(96)

      w:make_current()

      fltk.fl_rectf(20,20,50,50)
      fltk.fl_draw("this is a test",20,18)
  else
      os.exit(0)
  end

end

w=fltk:Fl_Window(0,0, 200, 200, "draw demo")

w:callback(draw_callback)

w:color(55)

w:show()

Fl:wait(1)
w:do_callback()

Fl:run()




RE: drawing graphics in murgaLua - JohnMurga - 06-23-2007 07:54 AM

And now for an animated demo ... Looks a lot nicer on murgaLua 0.5 but you'll have to wait ;-)

Code:
function draw_callback(eventValue)

  -- Need to look into what event 11 is ...

  if (Fl:event() == 0 or Fl:event() == 11)
  then
      -- 0 means no even I think, so we know we are requesting a draw.
      fltk.fl_color(96+counter)

      w:make_current()

      fltk.fl_rectf(20+counter,20,50,50)
      fltk.fl_draw("this is a test",20,80+counter)
      fltk.fl_loop( 20,20,80+counter,80+counter, 200,0)
      counter=counter+4
      if counter > 120 then counter=2 end
  else
      print(Fl:event())
      os.exit(0)
  end

end

counter = 0

w=fltk:Fl_Window(0,0, 210, 220, "draw demo")

w:callback(draw_callback)

w:color(55)

w:show()

while 1 do
    Fl:check()
    w:do_callback()
    -- murgaLua.sleep(25) -- Milliseconds, looks really cool (murgaLua 0.5).

    -- This sleeps reliably for one second
    socket.sleep(1)
end

Fl:run()




RE: drawing graphics in murgaLua - mikshaw - 06-23-2007 12:38 PM

Hmmm.....

The first one exits immediately. If I remove the "else os.exit(0)" then it shows a blank window which doesn't appear to accept any events.

The second one results in a segmentation fault.

murgaLua 0.4.1 on Slackware Linux


RE: drawing graphics in murgaLua - mikshaw - 06-23-2007 01:02 PM

The segfault was related to event 0, apparently.
I changed it to fltk.FL_MOUSEWHEEL and it seems to work.
That's really impressive.


RE: drawing graphics in murgaLua - iGame3D - 06-25-2007 03:29 PM


Code:
--- concentric circles at mouse coordinates demo
--- mouse delta distance modifies circle height and width
windowHeight = 280
windowWidth = 320
centerx,centery=windowHeight/2,windowWidth/2
oldpointx,oldpointy=centerx,centery
counter = 96
lineStep=.1 -- increase for geometry
radiusStep=-.4 -- changes concentric density

function circles(cWidth,cHeight)
pi=3.14159
for ring=30,2,radiusStep do
radius=ring
C=2*pi

for cPoint=0,C,lineStep do
x=radius*math.cos(cPoint)*(cWidth/30)
y=radius*math.sin(cPoint)*(cHeight/30)
pointx=math.floor(Fl:event_x()+x)
pointy=math.floor(Fl:event_y()+y)
fltk.fl_loop(pointx,pointy,oldpointx,oldpointy,pointx,pointy)
oldpointx,oldpointy=pointx,pointy
end
end
end

function draw_callback(eventValue)
      fltk.fl_color(96+counter)
      w:make_current() -- whats this?
      flmousex=math.abs(oldpointx-Fl:event_x())
  flmousey=math.abs(oldpointy-Fl:event_y())
      circles(flmousex,flmousey)
         counter=counter+2
     if counter > 128 then counter=2 end
end

w=fltk:Fl_Window(0,0, windowWidth, windowHeight, "circles demo")
w:callback(draw_callback)
w:color(55)
w:labelsize(9)
w:show()

while (Fl:event_key() ~= fltk.FL_Escape)  do
    Fl:check()
     w:do_callback()
murgaLua.sleep(35)
end




RE: drawing graphics in murgaLua - iGame3D - 06-25-2007 08:11 PM



Code:
-- draws color cycling polygon squares at mouse
windowHeight, windowWidth = 480,480
counter = 0
quads={}
qx,qy=0,0
gridSize,quadSize=14,14
qflip = false

function mouseDown_quads()
  flmousex=Fl:event_x()
  flmousey=Fl:event_y()
-- FL_DRAG =flevent 5 FL_PUSH =flevent 1,
if  Fl:event()==5 or Fl:event()==1 then
doQuad=true
quadX=(math.floor((flmousex+(quadSize/2))/gridSize)*gridSize)-1
quadY=(math.floor((flmousey+(quadSize/2))/gridSize)*gridSize)-1

--don't draw in the same place twice, doQuad=qflip for fun
for i=0,table.getn(quads),1 do
if  (quadX..","..quadY) == quads[i]  then doQuad=qflip end
end
end

if doQuad==true then drawQuad(quadX,quadY,quadSize) end

end   ---mouseDown_quads*

function drawQuad(qx,qy,qs,deltax,deltay)
quadHalf=math.floor(qs/2)
l,t,r,b=qx-quadHalf,qy-quadHalf,qx+quadHalf,qy+quadHalf
color_cycle(1,255,5)
-- --draw diagonal vector dependant on mouse movements      
   if qflip==false then
     fltk.fl_loop(l,t,r,t,r,b)
      color_cycle(1,255,5)
      fltk.fl_loop(l,b,l,t,r,b)
   else
      fltk.fl_loop(l,t,l,b,r,t)
      color_cycle(1,255,5)
      fltk.fl_loop(l,b,r,b,r,t)
   end
   qflip=not qflip
   quadCount=table.getn(quads)+1
   quads[quadCount]=(quadX..","..quadY)
   doQuad=false    
end ---drawQuad

function color_cycle(baseColor,counterLimit,counterStep)
      counter=counter+counterStep
     if counter > counterLimit then counter=counterStep end
      fltk.fl_color(baseColor+counter)  
end ---color_cycle
      
---callback
function quad_draw_window_callback(eventValue)  
      w:make_current() -- whats this?
      mouseDown_quads()  
end ---color_cycle

----FLTK WINDOW
w=fltk:Fl_Window(0,0, windowWidth, windowHeight, "Quad draw demo")
w:callback(quad_draw_window_callback)
w:color(55)
w:labelsize(9)
w:show()

--LOOP
while (Fl:event_key() ~= fltk.FL_Escape)  do
    Fl:check()
     w:do_callback()
murgaLua.sleep(35)
end




RE: drawing graphics in murgaLua - iGame3D - 06-25-2007 08:25 PM


Basically the same thing as the quad demo, only using filled rects, and adding a touch of art.

Code:
-- draws cycle colored filled rectangles squares with mouse with random scale
windowHeight,windowWidth  = 240,340
counter = 0
quads={}
qx,qy=0,0
gridSize=24
quadSize=24 -- a little overlap
qflip = false

function mouseDown_quads()
  flmousex=Fl:event_x()
  flmousey=Fl:event_y()
-- FL_DRAG =flevent 5 FL_PUSH =flevent 1,
if  Fl:event()==5 or Fl:event()==1 then
doQuad=true
quadX=(math.floor((flmousex+(quadSize/2))/gridSize)*gridSize)-1
quadY=(math.floor((flmousey+(quadSize/2))/gridSize)*gridSize)-1

--don't draw in the same place twice, doQuad=qflip for fun
for i=0,table.getn(quads),1 do
if  (quadX..","..quadY) == quads[i]  then doQuad=qflip end
end
end
qr=math.random(quadSize-15,quadSize)
if doQuad==true then drawrectF(quadX,quadY,qr) end

end   ---mouseDown_quads*

function drawrectF(qx,qy,qs)
color_cycle(1,255,5)
quadHalf=math.floor(qs/2)
l,t =qx-quadHalf,qy-quadHalf
fltk.fl_rectf(l,t,qs,qs)
end

function drawQuad(qx,qy,qs,deltax,deltay)
quadHalf=math.floor(qs/2)
l,t,r,b=qx-quadHalf,qy-quadHalf,qx+quadHalf,qy+quadHalf
color_cycle(1,255,5)
-- --draw diagonal vector dependant on mouse movements      
   if qflip==false then
     fltk.fl_loop(l,t,r,t,r,b)
      color_cycle(1,255,5)
      fltk.fl_loop(l,b,l,t,r,b)
   else
      fltk.fl_loop(l,t,l,b,r,t)
      color_cycle(1,255,5)
      fltk.fl_loop(l,b,r,b,r,t)
   end
   qflip=not qflip
   quadCount=table.getn(quads)+1
   quads[quadCount]=(quadX..","..quadY)
   doQuad=false    
end ---drawQuad

function color_cycle(baseColor,counterLimit,counterStep)
      counter=counter+counterStep
     if counter > counterLimit then counter=counterStep end
      fltk.fl_color(baseColor+counter)  
end ---color_cycle
      
---callback
function quad_draw_window_callback(eventValue)  
      w:make_current() -- whats this?
      mouseDown_quads()  
end ---color_cycle

----FLTK WINDOW
w=fltk:Fl_Window(0,0, windowWidth, windowHeight, "Quad draw demo")
w:callback(quad_draw_window_callback)
w:color(55)
w:labelsize(9)
w:show()

--LOOP
while (Fl:event_key() ~= fltk.FL_Escape)  do
    Fl:check()
     w:do_callback()
murgaLua.sleep(35)
end




RE: drawing graphics in murgaLua - mikshaw - 06-27-2007 12:21 AM

That last one is awesome!
Next step would be to give the squares a random size with overlap =o)

The circles demo segfaults on my system.


RE: drawing graphics in murgaLua - JohnMurga - 06-27-2007 02:04 AM

mikshaw Wrote:
That last one is awesome!
Next step would be to give the squares a random size with overlap =o)

The circles demo segfaults on my system.


This segfaulting is really worrying me ... I'll have some time tonight so I'll and track the cause of these segfaults.

Cheers
JohnM


RE: drawing graphics in murgaLua - JohnMurga - 06-27-2007 09:35 AM

OK, there are two issues here ...

The circles demo segfaults on Linux because of the fl_color being invoked before make_current() has been done (to set the display window)... This is completely an FLTK issue.

Just like the reason my demo was failing under Linux (due to the font not being set) ... Apparently Linux FLTK doesn't set up as many defaults or cope as well with bad use :-)

More bad news ...

The construct

Code:
while (Fl:event_key() ~= fltk.FL_Escape)  do
    Fl:check()
     w:do_callback()
end

Creates a VERY NASTY busy loop under Linux (again I think this FLTK) ... Putting a wait(1) helps a little but it is pretty nuts ... Under murgaLua 0.5 putting a murgaLua.sleep(50) makes the problem go away (50 milliseconds).

The busy loop is pretty nasty on Windows too, and the murgaLua.sleep fixes it there too ... You can put a socket.sleep(1), but I would advise using a timer callback until 0.5 comes out.

There you go ... Be careful when using the drawing functions under Linux.

Cheers
JohnM


RE: drawing graphics in murgaLua - observer - 08-03-2007 03:46 PM

An Fl:wait() in the event loop works for me in murgaLua.5.5 in my linux, Cygwin/Win32 build and Cygwin/X11 builds.


while (Fl:event_key() ~= fltk.FL_Escape) do
Fl:check()
wBig Grino_callback()
Fl:wait()
end


RE: drawing graphics in murgaLua - JohnMurga - 08-06-2007 10:12 PM

observer Wrote:
An Fl:wait() in the event loop works for me in murgaLua.5.5 in my linux, Cygwin/Win32 build and Cygwin/X11 builds.


while (Fl:event_key() ~= fltk.FL_Escape) do
Fl:check()
wBig Grino_callback()
Fl:wait()
end


Fl_Wait works a lot of the time, but can be VERY unreliable. The best thing is to use a timer, but you really want to use that kind of loop, and for it to work on several different systems you should replace the "Fl:wait()" with a murgaLua.sleep() for a few milliseconds.

Trust me ;-)

Cheers
JohnM