Z Order

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
nimdays
Posts: 236
Joined: May 29, 2014 22:01
Location: West Java, Indonesia

Z Order

Post by nimdays »

Code: Select all

#define MAXR 2   'max rect
#define MAXW 800 'max width
#define MAXH 600 'max height
#define MINWH 80 'min width/height
#define TBH 25   'titlebar height

type rect
    as integer x,y,w,h
    as uinteger c
    as string title
    declare constructor()
    declare constructor(as integer,as integer,as integer,as integer,as string,as integer)
    declare sub redraw()
end type

constructor rect()
end constructor

constructor rect(x as integer,y as integer,w as integer,h as integer,t as string,c as integer)
  this.x = x
  this.y = y
  this.w = w
  this.h = h
  this.title = t
  this.c = c
  redraw()
end constructor

sub rect.redraw()
    line(x,y)-(x+w,y+h),&hffffff,BF    'frame
    line(x+w-10,y+h-10)-(x+w,y+h),0,BF 'angle
    line(x+4,y+TBH)-(x+w-4,y+h-4),c,BF 'box
    draw string(x+4,y+8),title
end sub

dim shared as rect r(MAXR)

sub refresh()
    screenlock
    cls
    for i as integer = 0 to MAXR
       r(i).redraw()
    next i
    screenunlock
    sleep 1
end sub

screenres MAXW,MAXH,32
color 0,&h3A6EA5
cls

r(0) = rect(10,10,200,140,"Frame 1",&hff0000)
r(1) = rect(40,40,200,140,"Frame 2",&hffff00)
r(2) = rect(80,80,200,140,"Frame 3",&h0000ff)

dim as integer mx,my,mb,x1,y1

do
    getmouse mx,my,,mb
    x1 = mx
    y1 = my
    if mb = 1 then
        for i as integer = MAXR to 0 step -1
            if (mx > r(i).x and mx < r(i).x+r(i).w) and (my > r(i).y and my < r(i).y+r(i).h) then
                if i = 1 then
                    swap r(1),r(2)
                elseif i = 0 then
                    swap r(0),r(1)
                    swap r(1),r(2)
                end if
                if i < 2 then refresh()
                
                if mx > (r(2).x + r(2).w - 10) and my > (r(2).y + r(2).h - 10) then
                    do
                        getmouse mx,my,,mb
                        r(2).w += mx - x1
                        r(2).h += my - y1
                        x1 = mx
                        y1 = my
                        if r(2).w < MINWH then  r(2).w = MINWH
                        if r(2).h < MINWH then  r(2).h = MINWH
                        if r(2).w >= MAXW then  r(2).w = MAXW
                        if r(2).h >= MAXH then  r(2).h = MAXH
                        refresh()
                    loop until mb = 0
                elseif my < (r(2).y + TBH) then
                    do
                        getmouse mx,my,,mb
                        r(2).x += mx - x1
                        r(2).y += my - y1
                        x1 = mx
                        y1 = my
                        refresh()
                    loop until mb = 0
                end if
                exit for
            end if
        next i
    end if
    sleep 15,1
loop until multikey(1)

''sleep
D.J.Peters
Posts: 8586
Joined: May 28, 2005 3:28
Contact:

Re: Z Order

Post by D.J.Peters »

cute :-)

Joshy
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Re: Z Order

Post by counting_pine »

That's pretty effective for not much code. I was impressed enough with the movable/sizable windows!
nimdays
Posts: 236
Joined: May 29, 2014 22:01
Location: West Java, Indonesia

Re: Z Order

Post by nimdays »

D.J.Peters wrote:cute :-)

Joshy
Thanks, And please come back.
counting_pine wrote:That's pretty effective for not much code. I was impressed enough with the movable/sizable windows!
Thanks, I'm thinking to add more windows.
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Z Order

Post by Munair »

Great example. Thanks for sharing.
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Z Order

Post by jj2007 »

Definitely impressing. It strikes my eye, though, how far certain parts are from traditional BASIC:

Code: Select all

type rect
    as integer x,y,w,h
    as uinteger c
    as string title
    declare constructor()
    declare constructor(as integer,as integer,as integer,as integer,as string,as integer)
    declare sub redraw()
end type

constructor rect()
end constructor

constructor rect(x as integer,y as integer,w as integer,h as integer,t as string,c as integer)
  this.x = x
  ...
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Z Order

Post by Munair »

jj2007 wrote:Definitely impressing. It strikes my eye, though, how far certain parts are from traditional BASIC:

Code: Select all

type rect
    as integer x,y,w,h
    as uinteger c
    as string title
    declare constructor()
    declare constructor(as integer,as integer,as integer,as integer,as string,as integer)
    declare sub redraw()
end type

constructor rect()
end constructor

constructor rect(x as integer,y as integer,w as integer,h as integer,t as string,c as integer)
  this.x = x
  ...
I wouldn't call it "far from traditional BASIC". It's just that programming techniques have developed over time up to the level of objects. ;)
jj2007
Posts: 2326
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Z Order

Post by jj2007 »

Maybe. But empty obfuscated stuff like this is definitely closer to C/C++ than to BASIC.

Code: Select all

constructor rect()
end constructor
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

Re: Z Order

Post by sancho3 »

For the benefit of those who might be wondering.
A default constructor is necessary because an array of rect's is created and there is no way to dimension an array of objects and initialize them with the other non-default constructor at the same time.
If it wasn't for the array you could do something like this and avoid the empty constructor:

Code: Select all

dim shared as rect m = rect(1,2,3,4,"title", 0)
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Z Order

Post by fxm »

A constructor is mandatory to fill the variable's fields of the rect instance (if at least one field value must be different from its default value) only because of the string field which request a construction itself. In that case, the syntax "instance = Type(.....)" does not work (bit-wise assignments are impossible).
But define an explicit default constructor is not mandatory if the parameters of the constructor are all optional:

Code: Select all

#define MAXR 2   'max rect
#define MAXW 800 'max width
#define MAXH 600 'max height
#define MINWH 80 'min width/height
#define TBH 25   'titlebar height

type rect
    as integer x,y,w,h
    as uinteger c
    as string title
'    declare constructor()
    declare constructor(as integer=0,as integer=0,as integer=0,as integer=0,as string="",as integer=0)
    declare sub redraw()
end type

'constructor rect()
'end constructor

constructor rect(x as integer,y as integer,w as integer,h as integer,t as string,c as integer)
  this.x = x
  this.y = y
  this.w = w
  this.h = h
  this.title = t
  this.c = c
  redraw()
end constructor

sub rect.redraw()
    line(x,y)-(x+w,y+h),&hffffff,BF    'frame
    line(x+w-10,y+h-10)-(x+w,y+h),0,BF 'angle
    line(x+4,y+TBH)-(x+w-4,y+h-4),c,BF 'box
    draw string(x+4,y+8),title
end sub

dim shared as rect r(MAXR)

sub refresh()
    screenlock
    cls
    for i as integer = 0 to MAXR
       r(i).redraw()
    next i
    screenunlock
    sleep 1
end sub

screenres MAXW,MAXH,32
color 0,&h3A6EA5
cls

r(0) = rect(10,10,200,140,"Frame 1",&hff0000)
r(1) = rect(40,40,200,140,"Frame 2",&hffff00)
r(2) = rect(80,80,200,140,"Frame 3",&h0000ff)

dim as integer mx,my,mb,x1,y1

do
    getmouse mx,my,,mb
    x1 = mx
    y1 = my
    if mb = 1 then
        for i as integer = MAXR to 0 step -1
            if (mx > r(i).x and mx < r(i).x+r(i).w) and (my > r(i).y and my < r(i).y+r(i).h) then
                if i = 1 then
                    swap r(1),r(2)
                elseif i = 0 then
                    swap r(0),r(1)
                    swap r(1),r(2)
                end if
                if i < 2 then refresh()
               
                if mx > (r(2).x + r(2).w - 10) and my > (r(2).y + r(2).h - 10) then
                    do
                        getmouse mx,my,,mb
                        r(2).w += mx - x1
                        r(2).h += my - y1
                        x1 = mx
                        y1 = my
                        if r(2).w < MINWH then  r(2).w = MINWH
                        if r(2).h < MINWH then  r(2).h = MINWH
                        if r(2).w >= MAXW then  r(2).w = MAXW
                        if r(2).h >= MAXH then  r(2).h = MAXH
                        refresh()
                    loop until mb = 0
                elseif my < (r(2).y + TBH) then
                    do
                        getmouse mx,my,,mb
                        r(2).x += mx - x1
                        r(2).y += my - y1
                        x1 = mx
                        y1 = my
                        refresh()
                    loop until mb = 0
                end if
                exit for
            end if
        next i
    end if
    sleep 15,1
loop until multikey(1)

''sleep
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Z Order

Post by fxm »

Why work with a global (shared) rect array (r())?
In addition, that forbids the copy-construction (because initialization is done before program beginning when using global variable).
It's better to pass the rect array (r()) as a parameter to refresh():

Code: Select all

#define MAXR 2   'max rect
#define MAXW 800 'max width
#define MAXH 600 'max height
#define MINWH 80 'min width/height
#define TBH 25   'titlebar height

type rect
    as integer x,y,w,h
    as uinteger c
    as string title
    declare constructor(as integer=0,as integer=0,as integer=0,as integer=0,as string="",as integer=0)
    declare sub redraw()
end type

constructor rect(x as integer,y as integer,w as integer,h as integer,t as string,c as integer)
  this.x = x
  this.y = y
  this.w = w
  this.h = h
  this.title = t
  this.c = c
  redraw()
end constructor

sub rect.redraw()
    line(x,y)-(x+w,y+h),&hffffff,BF    'frame
    line(x+w-10,y+h-10)-(x+w,y+h),0,BF 'angle
    line(x+4,y+TBH)-(x+w-4,y+h-4),c,BF 'box
    draw string(x+4,y+8),title
end sub



sub refresh(r() as rect)
    screenlock
    cls
    for i as integer = 0 to MAXR
       r(i).redraw()
    next i
    screenunlock
    sleep 1
end sub

screenres MAXW,MAXH,32
color 0,&h3A6EA5
cls

dim as rect r(MAXR) = {rect(10,10,200,140,"Frame 1",&hff0000), _
                       rect(40,40,200,140,"Frame 2",&hffff00), _
                       rect(80,80,200,140,"Frame 3",&h0000ff)}

dim as integer mx,my,mb,x1,y1

do
    getmouse mx,my,,mb
    x1 = mx
    y1 = my
    if mb = 1 then
        for i as integer = MAXR to 0 step -1
            if (mx > r(i).x and mx < r(i).x+r(i).w) and (my > r(i).y and my < r(i).y+r(i).h) then
                if i = 1 then
                    swap r(1),r(2)
                elseif i = 0 then
                    swap r(0),r(1)
                    swap r(1),r(2)
                end if
                if i < 2 then refresh(r())
               
                if mx > (r(2).x + r(2).w - 10) and my > (r(2).y + r(2).h - 10) then
                    do
                        getmouse mx,my,,mb
                        r(2).w += mx - x1
                        r(2).h += my - y1
                        x1 = mx
                        y1 = my
                        if r(2).w < MINWH then  r(2).w = MINWH
                        if r(2).h < MINWH then  r(2).h = MINWH
                        if r(2).w >= MAXW then  r(2).w = MAXW
                        if r(2).h >= MAXH then  r(2).h = MAXH
                        refresh(r())
                    loop until mb = 0
                elseif my < (r(2).y + TBH) then
                    do
                        getmouse mx,my,,mb
                        r(2).x += mx - x1
                        r(2).y += my - y1
                        x1 = mx
                        y1 = my
                        refresh(r())
                    loop until mb = 0
                end if
                exit for
            end if
        next i
    end if
    sleep 15,1
loop until multikey(1)

''sleep
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: Z Order

Post by Munair »

fxm wrote:Why work with a global (shared) rect array (r())?
In addition, that forbids the copy-construction (because initialization is done before program beginning when using global variable).
It's better to pass the rect array (r()) as a parameter to refresh():
I agree with fxm. It's been common practice (since the days of QuickBASIC) to simply allow variables global access instead of passing them specifically as parameter where needed. Perhaps out of laziness, I'm not sure.
Post Reply