GLBasic forum

Codesnippets => Code Snippets => Topic started by: ampos on 2010-Oct-08

Title: ZONES
Post by: ampos on 2010-Oct-08
I have translated a BB3D routine I had laying on my hard-disk to GLB.

You can create "zones" on the screen and return the zone under mouse coords or anything. Also, a test mode to use while creating zones is provided.

Code: GLBasic [Select]
// --------------------------------- //
// Project: ZONES 1.4
// Start: Friday, October 08, 2010
// IDE Version: 8.125
//
//
//       CreateZone     Define a zone. You can use any number >0  Notice that zone[n] is not ZONE "n". Zone[1] is the first one created and could have ZoneID 21. Size is relative to a screensize 1024x640
//       CreateZoneAbs  Define a zone with ABSolute coordinates
//       DeleteZone             DELETE zone (id)
//       FreeZones              DELETE all zones AND free up reserved memory.
//       MouseZone()    Returns the number of the zone the mouse pointer is in.
//       ZoneProperty() Returns zone properties. 1=x, 2=y, 3=w, 4=h, 5=ID 6=Position of item in matrix
//       Zone()                 RETURN the number of the zone at given X AND Y co-ordinates.
//   ShowZones          Call this function each loop before SHOWSCREEN to draw zone rectangles
//
//   1.0        Inicial version
//       1.1    Fixed bug where zones with existing numbers where created as new, creating a ton of new zones
//       1.2    Fixed a bug in ZoneProperty
//       1.3    Fixed for rotating screens
//       1.4    CreateZone now creates zones relatives to screen size (1024x768 is origin screen size)
//                      CreateZoneAbs create ABSolute coordinates zones (the old createzone() function really)

TYPE zon
        x       // x coord
        y       // y coord
        w       // weight
        h       // heigh
        n       // zone number (ID)
ENDTYPE

GLOBAL zones[] AS zon

FUNCTION CreateZone: ZoneNum, Xcoord, Ycoord, Width, Height
                LOCAL m,r,x1,x2,y1,y2,x,y,xz,yz,z,xd,yd

                x1=MIN(Xcoord,Xcoord+Width)
                x2=MAX(Xcoord,Xcoord+Width)
                y1=MIN(Ycoord,Ycoord+Height)
                y2=MAX(Ycoord,Ycoord+Height)


                r=ZoneProperty(ZoneNum,6)
                IF r=-1
                        m=BOUNDS(zones[],0)
                        REDIM zones[m+1]
                ELSE
                        m=r
                ENDIF

                xz=xres/1024;yz=yres/640
                z=MIN(xz,yz)

                xd=(xres-(1024*z))/2
                yd=(yres-(640*z))/2

                zones[m].x=(x1*z)+xd
                zones[m].y=(y1*z)+yd
                zones[m].w=(x2-x1)*z
                zones[m].h=(y2-y1)*z
                zones[m].n=ZoneNum
ENDFUNCTION

FUNCTION CreateZoneAbs: ZoneNum, Xcoord, Ycoord, Width, Height
                LOCAL m,r,x1,x2,y1,y2
                x1=MIN(Xcoord,Xcoord+Width)
                x2=MAX(Xcoord,Xcoord+Width)
                y1=MIN(Ycoord,Ycoord+Height)
                y2=MAX(Ycoord,Ycoord+Height)


                r=ZoneProperty(ZoneNum,6)
                IF r=-1
                        m=BOUNDS(zones[],0)
                        REDIM zones[m+1]
                ELSE
                        m=r
                ENDIF

                zones[m].x=x1
                zones[m].y=y1
                zones[m].w=x2-x1
                zones[m].h=y2-y1
                zones[m].n=ZoneNum
ENDFUNCTION

FUNCTION ZoneProperty: number, item
        LOCAL z,r,n
        n=-1;r=-1
        FOREACH z IN zones[]
                INC n
                IF z.n=number
                        IF item=1 THEN r=z.x
                        IF item=2 THEN r=z.y
                        IF item=3 THEN r=z.w
                        IF item=4 THEN r=z.h
                        IF item=5 THEN r=z.n
                        IF item=6 THEN r=n
                        BREAK
                ENDIF
        NEXT
        RETURN r

ENDFUNCTION

FUNCTION DeleteZone: n
        FOREACH z IN zones[]
                IF z.n=n
                        DELETE z
                ENDIF
        NEXT
ENDFUNCTION

FUNCTION FreeZones:
        DIM zones[0]
ENDFUNCTION

FUNCTION MouseZone:
        LOCAL z,mx,my,b1,b2
        MOUSESTATE mx,my,b1,b2
        z=Zone(mx,my)
        RETURN z
ENDFUNCTION


FUNCTION Zone: xcoord, ycoord
        LOCAL zo
        FOREACH z IN zones[]
                IF xcoord>=z.x AND xcoord<=(z.x+z.w) AND ycoord>=z.y AND ycoord<=(z.y+z.h)
                        zo=z.n
                        BREAK
                ENDIF
        NEXT
        RETURN zo
ENDFUNCTION

FUNCTION ShowZones:
        FOREACH z IN zones[]
//              DRAWRECT z.x,z.y,5,5,RGB(0,255,0)
                DRAWLINE z.x,z.y,z.x+z.w,z.y,RGB(255,50,0)
                DRAWLINE z.x,z.y,z.x,z.y+z.h,RGB(255,50,0)
                DRAWLINE z.x+z.w,z.y+z.h,z.x+z.w,z.y,RGB(255,50,0)
                DRAWLINE z.x+z.w,z.y+z.h,z.x,z.y+z.h,RGB(255,50,0)
                PRINT z.n,z.x,z.y,1
        NEXT
ENDFUNCTION


 

Check this demo on the other thread:

http://www.glbasic.com/forum/index.php?topic=6905.0
Title: Re: ZONES
Post by: doimus on 2010-Oct-09
Cool, this is something GLB really needs.

There is a routine that Gernot developed, which enables AC3D 2D polys to act as sprites/zones.

It would be nice if we had a tool similar to polyvector that could define these zones/areas/whatever as polygonal shapes.
Title: Re: ZONES
Post by: erico on 2010-Oct-09
Agree with doimus, polivector zones for box collision would be great.  Although I guess the same functionality could be achieved some other way.

ps:.spelling
Title: Re: ZONES
Post by: ampos on 2010-Oct-13
Had a bug, line

Code: GLBasic [Select]
        IF xcoord>=z.x AND xcoord<=(z.x+z.h) AND ycoord>=z.y AND ycoord<=(z.y+z.w)
 
should be

Code: GLBasic [Select]
        IF xcoord>=z.x AND xcoord<=(z.x+z.w) AND ycoord>=z.y AND ycoord<=(z.y+z.h)
Title: Re: ZONES
Post by: ampos on 2011-Mar-07
Fixed a bug that prevented existing zones to be overwritten, so infinite zones with same zone-id will be created.

Now, if you create a zone with an existing number, previous zone is delete/overwritten with new datas.
Title: Re: ZONES
Post by: ampos on 2011-Mar-19
Fixed another bug (in ZoneCreate/ZoneProperties)
Title: Re: ZONES
Post by: ampos on 2011-Mar-23
Fixed for support of rotating screens
Title: Re: ZONES
Post by: ampos on 2011-Sep-18
There is now 2 functions to create zones.

One creates ABSolute coordinates zones (as always was done) and the other creates them resizing from 1024x640 to current screen size.

Rename as you wish to set one as the default for you.
Title: Re: ZONES
Post by: ampos on 2011-Sep-19
You can check the demo

http://www.glbasic.com/forum/index.php?action=dlattach;topic=6905.0;attach=3860

with a few buttons working with ZONES. Just press right mouse button to see them.
Title: Re: ZONES
Post by: gigios on 2011-Sep-22
I tried to download the demo but clicking on the link but I get the following message:

Code: GLBasic [Select]
An Error Has Occurred!

You are not allowed to access this section
 

Thanks.
Title: Re: ZONES
Post by: ampos on 2011-Sep-22
I tried to download the demo but clicking on the link but I get the following message:

Code: GLBasic [Select]
An Error Has Occurred!

You are not allowed to access this section
 

Thanks.

Ops, I linket the attach of the IMPUT$ thread (it is the same demo for both):

http://www.glbasic.com/forum/index.php?topic=6905.0
Title: Re: ZONES
Post by: gigios on 2011-Sep-22
Thank you  :good: