@coderJeff & dodicat,
it took about 10 Min. incl. testing, to remove all ptr's and, correct test/demo code.
Copy-Constructor in .bi, replaced with: Sub init() ... doing exactly the same.
The Types size, as shown with SizeOf(rec(lba)) is still 4 Bytes, with FBC 1.05.0 x64.
The new Rect_t2.bi:
Code: Select all
' Rect_t2.bi -- 2018-08-14, MrSwiss
'
#Pragma Once ' only loaded once (include guard)
' ----- start type -----
Type Rect_t ' a very basic rectangle type (only: width/height, held internally)
Private: ' no direct user access (to variables)
As UShort w, h ' variables (unsigned 16 bit integer)
Public: ' interface (gives some rights, to user)
Declare Sub init(ByVal w1 As UShort, ByVal h1 As UShort) ' instead of copy-constructor
' actor: put rectangle to screen at: x/y, with color: clr and fill: fll (default=FALSE=NOT filled)
Declare Sub show(ByVal x As Short, ByVal y As Short, ByVal clr As ULong=&hFF7F007F, ByVal fll As Boolean=FALSE)
Declare Sub setn(ByVal nw As UShort=0, ByVal nh As UShort=0) ' setter: change current values
Declare Sub getv(ByRef wt As UShort=0, ByRef ht As UShort=0) ' getter: returns stored values
' no need for a Destructor (default by compiler, does 'the trick')
End Type
'
Sub Rect_t.init( _
ByVal w1 As UShort, _
ByVal h1 As UShort _
)
This.w = w1 - 1 : This.h = h1 - 1
End Sub
'
Sub Rect_t.show( _ ' actor: put rectangle to screen
ByVal x As Short, _ ' x position, mandatory (signed 16 bit integer)
ByVal y As Short, _ ' y position, mandatory (signed 16 bit integer)
ByVal clr As ULong=&hFF7F007F, _ ' 32 bit color (default = purple), optional
ByVal fll As Boolean=FALSE _ ' default, not filled Box, optional
)
If fll Then ' if fll=TRUE then, show Box filled
Line (x, y)-Step(This.w, This.h), clr, BF ' Step = width/height are relative to x/y
'Line (x, y)-(x + This.w, y + This.h), clr, BF ' absolute positioning
Else ' otherwise, show Box NOT filled (default)
Line (x, y)-Step(This.w, This.h), clr, B
End If
End Sub
Sub Rect_t.setn( _ ' set new value(s), default: do nothing
ByVal nw As UShort=0, _ ' optional new width
ByVal nh As UShort=0 _ ' optional new height
)
If nw > 0 Then THis.w = nw - 1 ' correction for: relative sizing of RECT
If nh > 0 Then THis.h = nh - 1
End Sub
Sub Rect_t.getv( _ ' get currently set value(s)
ByRef wt As UShort=0, _ ' optional width (if var. provided)
ByRef ht As UShort=0 _ ' optional height (if var. provided)
)
wt = This.w + 1 : ht = This.h + 1 ' correction for: real size
End Sub
' ----- end type -----
And, the new test/demo code:
Code: Select all
' simplest_RECTANGLE_type1.bas -- 2018-08-14, MrSwiss
'
' compile: -s gui
'
#Include "Rect_t2.bi" ' load the rectangle type
' macros ...
#Define RndARGB ( CULng(Rnd * &hFFFFFFFFul) ) ' all color32 random, incl. alpha
#Define RngShrt(l, h) ( CShort(Rnd * ((h) - (l)) + (l)) ) ' random number from a defined range
Const As UShort scr_w = 1280, scr_h = 768, _ ' this two can be adjusted, by user
cd = 32, pg = 2, fg = 64 ' this stuff must remain unchanged!
' ===== start main =====
ScreenRes(scr_w, scr_h, cd, pg, fg) ' window sizes | 32 bit color | 2 pages | using alpha
Var sp1 = 1, sp2 = 0 ' screen pages (for buffer swapping)
ScreenSet(sp1, sp2) ' pre-set first buffer (to be drawn/shown)
Dim As Rect_t rec(1 To 30) ' array of rect_t (number of rectangles)
Dim As Short lba = LBound(rec), uba = UBound(rec) ' store current array definitions
Dim As short tl_offs = -(scr_h Shr 4) ' top/left offset (based on assumed smaller size)
Dim As Boolean flg1 = TRUE ' process control (fill, no fill)
For i As UInteger = lba To uba ' initialize the array (copy constructor call)
rec(i).init(RngShrt(scr_W * .1, scr_w * .4), _ ' min. = 10% | max. 40% (of width)
RngShrt(scr_h * .05, scr_h * .3)) ' min. = 5% | max. 30% (of height)
Next ' use macros for sizing (fixed min/max aspect ratio's _
' but, based on current screen sizes used)
Do
If Len(InKey()) > 0 Then Exit Do ' on user action --> EXIT LOOP (quit prog.)
Cls
For i As UInteger = lba To uba ' rectangles to screen (using macros for pos. & color)
rec(i).show(RngShrt(tl_offs, scr_w - tl_offs), _ ' positioning wit offset, based _
RngShrt(tl_offs, scr_h - tl_offs), _ ' on the current width/height used
RndARGB, flg1) ' random color | fill/no fill (switches each run!)
Next
Draw String (30, 21), "type size: " + Str(SizeOf(rec(lba))) ' show types size (4 bytes)
Swap sp1, sp2 : ScreenSet(sp1, sp2) ' swap visible/working page (show latest drawn buffer)
flg1 = Not flg1 : Sleep(500, 1) ' invert flag | free some CPU (let user see it 0.5 sec.)
Loop
' clean up ...
Erase(rec)
' ===== end main ===== ' ----- EOF -----
1) If you read carefully, I've stated 40 bytes, alligned in x64.