FHC - FreeBasic Helper Classes

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
VonGodric
Posts: 997
Joined: May 27, 2005 9:06
Location: London
Contact:

FHC - FreeBasic Helper Classes

Postby VonGodric » Feb 07, 2007 14:04

I started this a while ago - a project aimed to provide some basic data structures and tools for aiding development, but as at the moment I am busy with other things and this project is on hold and someone here posted about overlapping stuff, I thought I release the subset of FHC in hope that people will find it useful.

I provide 3 simple classes: CPos - representing and manipulating the position, CSize - representing and manipulating sized and finally CRect that represents a rectangle.

I suggest you skim through the source as all 3 classes provide variety of different functionality and manipulation techniques. (like multiplying the size by a factor, adding several rectangles together to form one big rectangle that contains the others, check overlapping and even what kind of overlapping, inlfating and centering one rectangle against another and more, ...)

if you need debug info at any moment: print yourInstanceObject - as all classes have CASTs to a string.

I hope I'll later post a small tutorial to demonstrate this all a bit better. This is meant to be used in my GUI library.

FHC.bi

Code: Select all

''
'' (F)reeBasic (H)elper (C)lasses
namespace FHC
   
    ''
    '' Bool is a type that is used by FHC to return
    '' true / false values.
    type BOOL as integer
    enum
        FALSE = 0
        TRUE = NOT FALSE
    end enum

    #define MIN(a, b) iif((a) >= (b), (b), (a))
    #define MAX(a, b) iif((a) >= (b), (a), (b))
    #define NULL cast(any ptr, 0)
   
    enum
        FHC_VERTICAL    = 1
        FHC_HORIZONTAL  = 2
        FHC_LEFT        = 4
        FHC_RIGHT       = 8
        FHC_TOP         = 16
        FHC_BOTTOM      = 32
        FHC_ALL         = FHC_LEFT or FHC_RIGHT or FHC_TOP or FHC_BOTTOM
        FHC_BOTH        = FHC_VERTICAL or FHC_HORIZONTAL
    end enum
    const DEFAULT_COORD = -1
   
    ' Forward declarations
    type _CPos_ as CPos
    type _CSize as CSize
   
   
   
    '===========================================================================
    ' CPos is a a class that represents a position
    ' on the screen. Contains X and Y value and
    ' useful methods to manipulate position
    '---------------------------------------------------------------------------
    Type CPos
        as integer x, y
       
        ' Constructors.
        declare constructor ()
        declare constructor (xx as integer, yy as integer)
        declare constructor (byref p as CPos)
       
        ' returns a string with x and y (useful for debugging)
        declare operator CAST () as string
       
        ' arithmetic
        #macro DEF_OP(op)
            declare operator op= (byref rhs as CPos)
            declare operator op= (byref rhs as _CSize)
            declare operator op= (i as integer)
        #endmacro
        DEF_OP(+)
        DEF_OP(-)
        DEF_OP(*)
        DEF_OP(/)
        DEF_OP(\)
        #undef DEF_OP
        declare operator *= (i as double)
        declare operator /= (i as double)
    end type
   
    ' Operators for testing equality
    declare operator = (byref lhs as CPos, byref rhs as CPos) as BOOL
    declare operator <> (byref lhs as CPos, byref rhs as CPos) as BOOL
   
    ' Operator for returning positions as negative numbers
    declare operator - (byref rhs as CPos) as CPos
   
    ' operations with the position
    #macro DEF_OP(op)
        declare operator op (byref lhs as CPos, byref rhs as CPos) as CPos
        declare operator op (byref lhs as CPos, byref rhs as _CSize) as CPos
        declare operator op (byref lhs as CPos, i as integer) as CPos   
    #endmacro
    DEF_OP(+)
    DEF_OP(-)
    DEF_OP(*)
    DEF_OP(/)
    DEF_OP(\)
    #undef DEF_OP
    declare operator * (byref lhs as CPos, f as double) as CPos
    declare operator / (byref lhs as CPos, f as double) as CPos
   
   
   
    '===========================================================================
    ' CSize is a class that represents a 2 dimensional size with width and
    ' height. Contains useful methods and operators to manipulate it.
    '---------------------------------------------------------------------------
    type CSize
        as integer w, h
       
        ' Constructors
        declare constructor ()
        declare constructor (ww as integer, hh as integer)
        declare constructor (byref s as CSize)
        declare constructor (byref p1 as CPos, byref p2 as CPos)
       
        ' returns a string with x and y (useful for debugging)
        declare operator CAST () as string
       
        ' arithmetic operations with size
        ' arithmetic
        #macro DEF_OP(op)
            declare operator op= (byref rhs as CSize)
            declare operator op= (i as integer)
        #endmacro
        DEF_OP(+)
        DEF_OP(-)
        DEF_OP(*)
        DEF_OP(/)
        DEF_OP(\)
        #undef DEF_OP
        declare operator *= (i as double)
        declare operator /= (i as double)
       
        ' Incerement to size (only if smaller)
        declare sub IncTo (byref s as CSize)
        declare sub IncTo (ww as integer, hh as integer)
       
        ' Decrement to size (only if is larger)
        declare sub DecTo (byref s as CSize)
        declare sub DecTo (ww as integer, hh as integer)
       
        ' Scale the size by given factor(s)
        declare sub Scale (f as single)
        declare sub Scale (fw as single, fh as single)
       
        ' If w and h values are -1 then it is considered as
        ' not fully specified
        declare function IsFullySpecified() as BOOL
       
        ' Set new values if size or height not specified
        declare sub SetDefaults (byref s as CSize)
        declare sub SetDefaults (ww as integer, hh as integer)
       
        ' Just a function to set calues
        declare sub Set (ww as integer, hh as integer)
    end type
   
    ' test equality
    declare operator = (byref lhs as CSize, byref rhs as CSize) as BOOL
    declare operator <> (byref lhs as CSize, byref rhs as CSize) as BOOL
   
    ' Arithmetic functions
    ' operations with the position
    #macro DEF_OP(op)
        declare operator op (byref lhs as CSize, byref rhs as CSize) as CSize
        declare operator op (byref lhs as CSize, i as integer) as CSize   
    #endmacro
    DEF_OP(+)
    DEF_OP(-)
    DEF_OP(*)
    DEF_OP(/)
    DEF_OP(\)
    #undef DEF_OP
    declare operator * (byref lhs as CSize, f as double) as CSize
    declare operator / (byref lhs as CSize, f as double) as CSize
   
   
   
    '===========================================================================
    ' CRect class provides handling of rectangular area on the screen
    ' and contains useful methods.
    '---------------------------------------------------------------------------
    type CRect
        as integer x, y, w, h
       
        ' Constructors
        declare constructor ()
        declare constructor (byref r as CRect)
        declare constructor (xx as integer, yy as integer, ww as integer, hh as integer)
        declare constructor (xx as integer, yy as integer, byref s as CSize)
        declare constructor (byref p as CPos, ww as integer, hh as integer)
        declare constructor (byref p as CPos, byref s as CSize)
        declare constructor (byref p1 as CPos, byref p2 as CPos)
       
        ' returns a string with x and y (useful for debugging)
        declare operator CAST () as string
        declare operator cast () as CSize
        declare operator cast () as CPos
       
        ' properties for positions
        declare property Pos () as CPos
        declare property Pos (byref p as CPos)
       
        declare property LeftTop () as CPos
        declare property LeftTop (byref p as CPos)
       
        declare property LeftBottom () as CPos
        declare property LeftBottom (byref p as CPos)
       
        declare property RightTop () as CPos
        declare property RightTop (byref p as CPos)
       
        declare property RightBottom () as CPos
        declare property RightBottom (byref p as CPos)
       
        declare property x2 () as integer
        declare property x2 (xx as integer)
       
        declare property y2 () as integer
        declare property y2 (yy as integer)
       
        ' properties for size
        declare property size () as CSize
        declare property size (byref s as CSize)
       
        ' returns TRUE if rect width or height <= 0
        declare function IsEmpty () as BOOL
        ' Return true if there is a common area
        declare function Intersects (byref r as CRect) as BOOL
        declare function GetOverFlow (byref r as CRect) as integer
        ' return TRUE if these are on the original rectangle
        declare function Contains (byref p as CPos) as BOOL
        declare function Contains (xx as integer, yx as integer) as BOOL
        declare function Contains (byref r as CRect) as BOOL       
       
        ' Operations with the rectangle.
        ' Returns common area as a rectangle
        declare function Intersect (byref r as CRect) as CRect
        ' Deflating
        declare function Deflate (dx as integer, dy as integer) as CRect
        declare function Deflate (d as integer) as CRect
        declare function Deflate (byref s as CSize) as CRect
        ' Inflate
        declare function Inflate (dx as integer, dy as integer) as CRect
        declare function Inflate (d as integer) as CRect
        declare function Inflate (byref s as CSize) as CRect
        ' add two rect areas
        declare function Join (byref r as CRect) as CRect
        ' Center one reactangle against another
        declare function CenterIn (byref r as CRect, d as integer = FHC_BOTH) as CRect
       
        ' Move the rectangle
        declare sub Move (dx as integer, dy as integer)
        declare sub Move (byref s as CSize)
       
        ' Cut away a peace of the rectangke
        declare sub CutLeft (d as integer)
        declare sub CutRight (d as integer)
        declare sub CutTop (d as integer)
        declare sub CutBottom (d as integer)
       
        ' Add two rectangle areas together
        declare operator += (byref rhs as CRect)
    end type
   
    ' Check for equality
    declare operator = (byref lhs as CRect, byref rhs as CRect) as integer
    declare operator <> (byref lhs as CRect, byref rhs as CRect) as integer
   
    ' Add rectangles
    declare operator + (byref lhs as CRect, byref rhs as CRect) as CRect
   
end namespace


fhc.bas

Code: Select all

#include once "fhc.bi"

namespace FHC
   
    '===========================================================================
    ' CPos
    '---------------------------------------------------------------------------
    constructor CPos ()
        x = 0 : y = 0
    end constructor
   
    constructor CPos (xx as integer, yy as integer)
        x = xx : y = yy
    end constructor
   
    constructor CPos (byref p as CPos)
        x = p.x : y = p.y
    end constructor
   
    operator CPos.CAST () as string
        return "x = " & x & ", y = " & y
    end operator
   
    #macro IMPL_OP(OP)
        operator CPos.op=(byref rhs as CPos)
            x op= rhs.x
            y op= rhs.y
        end operator
        operator CPos.op=(byref rhs as CSize)
            x op= rhs.w
            y op= rhs.h
        end operator
        operator CPos.op=(i as integer)
            x op= i
            y op= i
        end operator
    #endmacro
    IMPL_OP(+)
    IMPL_OP(-)
    IMPL_OP(*)
    IMPL_OP(/)
    IMPL_OP(\)
    #undef IMPL_OP
   
    operator CPos.*= (f as double)
        x = cint(x * f) : y = cint(y * f)
    end operator
   
    operator CPos./= (f as double)
        x = cint(x / f) : y = cint(y / f)
    end operator
   
    operator = (byref lhs as CPos, byref rhs as CPos) as BOOL
        return lhs.x = rhs.x AND lhs.y = rhs.y
    end operator
   
    operator <> (byref lhs as CPos, byref rhs as CPos) as BOOL
        return lhs.x <> rhs.x OR lhs.y <> rhs.y
    end operator
   
    operator - (byref rhs as CPos) as CPos
        return CPos(-rhs.x, -rhs.y)
    end operator
   
    #macro IMPL_OP(OP)
        operator OP (byref lhs as CPos, byref rhs as CPos) as CPos
            return CPos(lhs.x OP rhs.x, lhs.y OP rhs.y)
        end operator
        operator OP (byref lhs as CPos, byref rhs as CSize) as CPos
            return CPos(lhs.x OP rhs.w, lhs.y OP rhs.h)
        end operator
        operator OP (byref lhs as CPos, i as integer) as CPos
            return CPos(lhs.x OP i, lhs.y OP i)
        end operator
    #endmacro
    IMPL_OP(+)
    IMPL_OP(-)
    IMPL_OP(*)
    IMPL_OP(/)
    IMPL_OP(\)
    #undef IMPL_OP
   
    operator * (byref lhs as CPos, f as double) as CPos
        return CPos(cint(lhs.x * f), cint(lhs.y * f))
    end operator
   
    '===========================================================================
    ' CSize
    '---------------------------------------------------------------------------
    constructor CSize ()
        w = 0 : h = 0
    end constructor
   
    constructor CSize (ww as integer, hh as integer)
        w = ww : h = hh
    end constructor
   
    constructor CSize (byref s as CSize)
        w = s.w : h = s.h
    end constructor
   
    constructor CSize (byref p1 as CPos, byref p2 as CPos)
        w = iif(p1.x > p2.x, p1.x - p2.x, p2.x - p1.x)
        h = iif(p1.y > p2.y, p1.y - p2.y, p2.y - p1.y)
    end constructor
   
    operator CSize.CAST () as string
        return "w = " & w & ", h = " & h
    end operator
   
    #macro IMPL_OP(OP)
        operator CSize.op=(byref rhs as CSize)
            w op= rhs.w
            h op= rhs.h
        end operator
        operator CSize.op=(i as integer)
            w op= i
            h op= i
        end operator
    #endmacro
    IMPL_OP(+)
    IMPL_OP(-)
    IMPL_OP(*)
    IMPL_OP(/)
    IMPL_OP(\)
    #undef IMPL_OP
   
    operator CSize.*= (f as double)
        w = cint(w * f) : h = cint(h * f)
    end operator
   
    operator CSize./= (f as double)
        w = cint(w / f) : h = cint(h / f)
    end operator
   
   
    sub CSize.IncTo (byref s as CSize)
        if (w < s.w) then w = s.w
        if (h < s.h) then h = s.h
    end sub
   
    sub CSize.IncTo (ww as integer, hh as integer)
        if (w < ww) then w = ww
        if (h < hh) then h = hh       
    end sub
   
    sub CSize.DecTo (byref s as CSize)
        if (w > s.w) then w = s.w
        if (h > s.h) then h = s.h
    end sub
   
    sub CSize.DecTo (ww as integer, hh as integer)
        if (w > ww) then w = ww
        if (h > hh) then h = hh 
    end sub
   
    sub CSize.Scale (f as single)
        w = cint(w * f)
        h = cint(h * f)
    end sub
   
    sub CSize.Scale (fw as single, fh as single)
        w = cint(w * fw)
        h = cint(h * fh)
    end sub
   
    function CSize.IsFullySpecified() as BOOL
        return w <> DEFAULT_COORD AND h <> DEFAULT_COORD
    end function
   
    sub CSize.SetDefaults (byref s as CSize)
        if (w = DEFAULT_COORD) then w = s.w
        if (h = DEFAULT_COORD) then h = s.h
    end sub
   
    sub CSize.SetDefaults (ww as integer, hh as integer)
        if (w = DEFAULT_COORD) then w = ww
        if (h = DEFAULT_COORD) then h = hh
    end sub
   
    sub CSize.Set (ww as integer, hh as integer)
        w = ww : h = hh
    end sub
   
    operator = (byref lhs as CSize, byref rhs as CSize) as BOOL
        return lhs.w = rhs.w AND lhs.h = rhs.h
    end operator
   
    operator <> (byref lhs as CSize, byref rhs as CSize) as BOOL
        return lhs.w <> rhs.w OR lhs.h <> rhs.h
    end operator
   
    #macro IMPL_OP(OP)
        operator OP (byref lhs as CSize, byref rhs as CSize) as CSize
            return CSize(lhs.w OP rhs.w, lhs.h OP rhs.h)
        end operator
        operator OP (byref lhs as CSize, i as integer) as CSize
            return CSize(lhs.w OP i, lhs.h OP i)
        end operator
    #endmacro
    IMPL_OP(+)
    IMPL_OP(-)
    IMPL_OP(*)
    IMPL_OP(/)
    IMPL_OP(\)
    #undef IMPL_OP
   
    operator * (byref lhs as CSize, f as double) as CSize
        return CSize(cint(lhs.w * f), cint(lhs.h * f))
    end operator
   
    operator / (byref lhs as CSize, f as double) as CSize
        return CSize(cint(lhs.w / f), cint(lhs.h / f))
    end operator
   
   
   
    '===========================================================================
    ' CRect
    '---------------------------------------------------------------------------
    constructor CRect ()
        x = 0 : y = 0
        w = 0 : h = 0
    end constructor
    constructor CRect (byref r as CRect)
        x = r.x : y = r.y
        w = r.w : h = r.h
    end constructor
    constructor CRect (xx as integer, yy as integer, ww as integer, hh as integer)
        x = xx : y = yy
        w = ww : h = hh
    end constructor
    constructor CRect (xx as integer, yy as integer, byref s as CSize)
        x = xx : y = yy
        w = s.w : h = s.h
    end constructor
    constructor CRect (byref p as CPos, ww as integer, hh as integer)
        x = p.x : y = p.y
        w = ww : h = hh
    end constructor
    constructor CRect (byref p as CPos, byref s as CSize)
        x = p.x : y = p.y
        w = s.w : h = s.h
    end constructor
    constructor CRect (byref p1 as CPos, byref p2 as CPos)
        x = iif(p1.x > p2.x, p2.x, p1.x)
        y = iif(p1.y > p2.y, p2.y, p1.y)
        w = iif(p1.x > p2.x, p1.x - p2.x, p2.x - p1.x) + 1
        h = iif(p1.y > p2.y, p1.y - p2.y, p2.y - p1.y) + 1
    end constructor
   
    operator CRect.CAST () as string
        return  "x = " & x & ", y = " & y & ", w = " & w & ", h = " & h
    end operator
   
    operator CRect.cast () as CSize
        return CSize(w, h)
    end operator
   
    operator CRect.cast () as CPos
        return CPos(x, y)
    end operator
   
    property CRect.Pos () as CPos
        return CPos (x, y)
    end property
   
    property CRect.Pos (byref p as CPos)
        x = p.x : y = p.y
    end property
   
    property CRect.LeftTop () as CPos
        return CPos (x, y)
    end property
   
    property CRect.LeftTop (byref p as CPos)
        x = p.x : y = p.y
    end property
   
    property CRect.LeftBottom () as CPos
        return CPos(x, y + h - 1)
    end property
   
    property CRect.LeftBottom (byref p as CPos)
        x = p.x : h = p.y - y + 1
    end property
   
    property CRect.RightTop () as CPos
        return CPos(x + w - 1, y)
    end property
   
    property CRect.RightTop (byref p as CPos)
        w = p.x - x + 1 : y = p.y
    end property
   
    property CRect.RightBottom () as CPos
        return CPos(x + w - 1, y + h - 1)
    end property
   
    property CRect.RightBottom (byref p as CPos)
        h = p.y - y + 1
        w = p.x - x + 1
    end property
   
    property CRect.x2 () as integer
        return x + w - 1
    end property
   
    property CRect.x2 (xx as integer)
        w = xx - x + 1
    end property
   
    property CRect.y2 () as integer
        return y + h - 1
    end property
   
    property CRect.y2 (yy as integer)
        h = yy - y + 1
    end property
   
    property CRect.Size () as CSize
        return CSize(w, h)
    end property
   
    property CRect.Size (byref s as CSize)
        w = s.w : h = s.h
    end property
   
    sub CRect.Move (dx as integer, dy as integer)
        x += dx : y += dy
    end sub
   
    sub CRect.CutLeft (d as integer)
        x += d
        w -= d
    end sub

    sub CRect.CutRight (d as integer)
        w -= d
    end sub
   
    sub CRect.CutTop (d as integer)
        y += d
        h -= d
    end sub
   
    sub CRect.CutBottom (d as integer)
        h -= d
    end sub
   
    sub CRect.Move (byref s as CSize)
        x += s.w : y += s.h
    end sub
   
    function CRect.IsEmpty () as BOOL
        return w <= 0 or h <= 0
    end function
   
    function CRect.Contains (byref p as CPos) as BOOL
        if (p.x >= x) then
            if (p.y >= y) then
                if (p.x <= x + w) then
                    if (p.y <= y + h) then
                        return TRUE
                    end if
                end if
            end if
        end if
        return FALSE
    end function
   
    function CRect.Contains (xx as integer, yy as integer) as BOOL
        if (xx >= x) then
            if (yy >= y) then
                if (xx <= x + w) then
                    if (yy <= y + h) then
                        return TRUE
                    end if
                end if
            end if
        end if
        return FALSE
    end function
   
    function CRect.Contains (byref r as CRect) as BOOL
        if (r.x >= x) then
            if (r.y >= y) then
                if (r.x + r.w <= x + w) then
                    if (r.y + r.h <= y + h) then
                        return TRUE
                    end if
                end if
            end if
        end if
        return FALSE
    end function
   
    '' Return true if there is a common area
    function CRect.Intersects (byref r as CRect) as BOOL
        if (r.x >= (x + w) OR r.y >= (y + h) OR _
            (r.x + r.w) <= x or (r.y + r.h) <= y) then return FALSE
        return TRUE
    end function
   
    '' Return a bitmask with sides where one surface crosses other
    function CRect.GetOverFlow (byref r as CRect) as integer
        dim ret as integer = 0
        if (NOT Intersects(r)) then return 0
       
        if (x > r.x) then ret or= FHC_LEFT
        if (y > r.y) then ret or= FHC_TOP
        if (x + w < r.x + r.w) then ret or= FHC_RIGHT
        if (y + h < r.y + r.h) then ret or= FHC_BOTTOM
        return ret
    end function
   
    function CRect.Intersect (byref r as CRect) as CRect
        if (NOT Intersects(r)) then return CRect()
        dim as integer xx1 = any, yy1 = any, xx2 = any, yy2 = any
        xx1 = iif (x > r.x, x, r.x)
        yy1 = iif (y > r.y, y, r.y)
        xx2 = iif (x + w < r.x + r.w, x + w, r.x + r.w)
        yy2 = iif (y + h < r.y + r.h, y + h, r.y + r.h)
        return CRect (xx1, yy1, xx2 - xx1, yy2 - yy1)
    end function
   
    function CRect.Inflate (dx as integer, dy as integer) as CRect
        return CRect (x - dx, y - dy, w + 2 * dx, h + 2 * dy)
    end function
   
    function CRect.Inflate (d as integer) as CRect
        return CRect (x - d, y - d, w + 2 * d, h + 2 * d)
    end function
   
    function CRect.Inflate (byref s as CSize) as CRect
        return CRect (x - s.w, y - s.h, w + 2 * s.w, h + 2 * s.h)
    end function
   
    function CRect.Deflate (dx as integer, dy as integer) as CRect
        return CRect (x + dx, y + dy, w - 2 * dx, h - 2 * dy)
    end function
   
    function CRect.Deflate (d as integer) as CRect
        return CRect (x + d, y + d, w - 2 * d, h - 2 * d)
    end function
   
    function CRect.Deflate (byref s as CSize) as CRect
        return CRect (x + s.w, y + s.h, w - 2 * s.w, h - 2 * s.h)
    end function
   
    function CRect.Join (byref r as CRect) as CRect
         dim as integer xx, yy, ww, hh
         xx = MIN (x, r.x)
         yy = MIN (y, r.y)
         ww = MAX (x + w, r.x + r.w) - xx
         hh = MAX (y + h, r.y + r.h) - yy
         return CRect(xx, yy, ww, hh)
    end function
   
    function CRect.CenterIn (byref r as CRect, d as integer) as CRect
        dim as integer rx = any, ry = any
        if (d and FHC_HORIZONTAL) then
            rx = r.x + (r.w - w) / 2
        else
            rx = x
        end if
        if (d and FHC_VERTICAL) then
            ry = r.y + (r.h - h) / 2
        else
            ry = y
        end if
        return CRect (rx, ry, w, h)
    end function
   
    operator CRect.+= (byref rhs as CRect)
         w = MAX (x + w, rhs.x + rhs.w)
         h = MAX (y + h, rhs.y + rhs.h)
         x = MIN (x, rhs.x)
         y = MIN (y, rhs.y)
         w -= x : h -= y
    end operator
   
    operator = (byref lhs as CRect, byref rhs as CRect) as integer
        return lhs.x = rhs.x AND lhs.y = rhs.y AND lhs.w = rhs.w AND lhs.h = rhs.h
    end operator
   
    operator <> (byref lhs as CRect, byref rhs as CRect) as integer
        return lhs.x <> rhs.x or lhs.y <> rhs.y or lhs.w <> rhs.w or lhs.h <> rhs.h
    end operator
   
    operator + (byref lhs as CRect, byref rhs as CRect) as CRect
        dim ret as CRect = CRect( _
                    MIN (lhs.x, rhs.x), MIN (lhs.y, rhs.y), _
                    MAX (lhs.x + lhs.w, rhs.x + rhs.w), MAX (lhs.y + lhs.h, rhs.y + rhs.h) _
                )
        ret.w -= ret.x : ret.h -= ret.y
        return ret
    end operator

end namespace



small demo

Code: Select all

#include once "fhc.bi"
using FHC
    type CBoard
        declare constructor (byref p as CPos, s as CSize, ns as integer, sn as integer)
        declare sub DrawBoard ()
        declare sub DrawRect (byref r as CRect, c as uinteger = RGB(0, 0, 255))
       
        private:
            m_ns as integer
            m_pos as CPos
            m_size as CSize
            m_sn as integer
    end type
   
   
    screenres 649, 480, 32, 2
    screenset 0,1
    dim as integer mx, my
    dim board1 as CBoard = CBoard(CPos(0, 0), CSize(31, 23), 20, 1)

    dim r1 as CRect = CRect (8, 8, 8, 5)
    dim r2 as CRect = CRect (8, 8, 10, 7)
    dim r3 as CRect

    do
        CLS
        getmouse mx, my
        mx -= 23 : my -= 18
        mx \=  20 : my \= 20
        r1.pos = CPos(mx, my)
       
        board1.DrawRect(r2, RGB(255, 0, 0))
        board1.DrawRect(r1)
       
        dim ret as integer
        ret = r1.GetOverFlow(r2)
       
        if (NOT r3.IsEmpty()) then board1.DrawRect(r3, RGB(0, 255, 0))
        board1.DrawBoard()
       
        if (ret AND FHC_LEFT) then print "FHC_LEFT"
        if (ret AND FHC_RIGHT) then print "FHC_RIGHT"
        if (ret AND FHC_TOP) then print "FHC_TOP"
        if (ret AND FHC_BOTTOM) then print "FHC_BOTTOM"
       
       
        sleep 20
        screencopy
    loop until len(inkey)

   
   
    '===========================================================================
   
   
   
    constructor CBoard (byref p as CPos, s as CSize, ns as integer, sn as integer)
        m_ns = ns
        m_pos = p
        m_size = s
        m_sn = sn
    end constructor
   
    sub CBoard.DrawBoard ()
        dim ls as CSize = m_size * m_ns
        dim lp as CPos = m_pos
        lp.x += 23
        lp.y += 14
       
        for x as integer = 0 to m_size.w
            if (x < m_size.w AND m_sn <> 0) then
                draw string (lp.x + 5, lp.y - 10), str(x)
            end if
            line (lp.x, lp.y) - (lp.x, lp.y + ls.h), RGB(128, 128, 128)
            lp.x += m_ns
        next
       
        lp = m_pos
        lp.x += 23
        lp.y += 14
        for y as integer = 0 to m_size.h
            if (y < m_size.h and m_sn <> 0) then
                draw string (lp.x - 18, lp.y + 6), str(y)
            end if
            line (lp.x, lp.y) - (lp.x + ls.w, lp.y), RGB(128, 128, 128)
            lp.y += m_ns
        next
    end sub
   
    sub CBoard.DrawRect (byref r as CRect, c as uinteger = RGB(0, 0, 255))
        dim s as CSize = r.size * m_ns
        dim p as CPos = m_pos + (r.pos * m_ns)
       
        p.x += 23
        p.y += 14

        line (p.x, p.y) - (p.x + s.w, p.y + m_ns), c, BF
        line (p.x, p.y) - (p.x + m_ns, p.y + s.h), c, BF
       
        line (p.x, p.y + s.h -m_ns) - (p.x + s.w, p.y + s.h), c, BF
        line (p.x + s.w - m_ns, p.y) - (p.x + s.w, p.y + s.h), c, BF
    end sub
Imortis
Moderator
Posts: 1679
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Postby Imortis » Feb 07, 2007 18:26

That's pretty neat, Von.

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 1 guest