News : The level of daily SPAM has reached insane proportions, all registrations are now manual. I ask you to send me an e-mail (john (at) murga (dot) org), to confirm that you want me to create an account for you.


Post Reply  Post Thread 
Pages (2): « First < Previous 1 [2] Last »
drawing graphics in murgaLua
Author Message
iGame3D
Moderator
***


Posts: 231
Group: Moderators
Joined: Apr 2007
Status: Offline
Reputation: 0
Post: #11
RE: drawing graphics in murgaLua



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

This post was last modified: 01-06-2014 11:37 PM by iGame3D.

06-25-2007 08:11 PM
Visit this user's website Find all posts by this user Quote this message in a reply
iGame3D
Moderator
***


Posts: 231
Group: Moderators
Joined: Apr 2007
Status: Offline
Reputation: 0
Post: #12
RE: drawing graphics in murgaLua


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

This post was last modified: 01-06-2014 11:39 PM by iGame3D.

06-25-2007 08:25 PM
Visit this user's website Find all posts by this user Quote this message in a reply
mikshaw
Senior Member
****


Posts: 522
Group: Registered
Joined: Apr 2007
Status: Offline
Reputation: 5
Post: #13
RE: drawing graphics in murgaLua

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.

06-27-2007 12:21 AM
Find all posts by this user Quote this message in a reply
JohnMurga
Administrator
*******


Posts: 381
Group: Administrators
Joined: Apr 2007
Status: Offline
Reputation: 2
Post: #14
RE: drawing graphics in murgaLua

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

06-27-2007 02:04 AM
Visit this user's website Find all posts by this user Quote this message in a reply
JohnMurga
Administrator
*******


Posts: 381
Group: Administrators
Joined: Apr 2007
Status: Offline
Reputation: 2
Post: #15
RE: drawing graphics in murgaLua

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

This post was last modified: 06-27-2007 09:51 AM by JohnMurga.

06-27-2007 09:35 AM
Visit this user's website Find all posts by this user Quote this message in a reply
observer
Junior Member
**


Posts: 6
Group: Registered
Joined: Aug 2007
Status: Offline
Reputation: 0
Post: #16
RE: drawing graphics in murgaLua

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

08-03-2007 03:46 PM
Find all posts by this user Quote this message in a reply
JohnMurga
Administrator
*******


Posts: 381
Group: Administrators
Joined: Apr 2007
Status: Offline
Reputation: 2
Post: #17
RE: drawing graphics in murgaLua

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

08-06-2007 10:12 PM
Visit this user's website Find all posts by this user Quote this message in a reply
Pages (2): « First < Previous 1 [2] Last »
Post Reply  Post Thread 

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites

Forum Jump: