Useful defines

General FreeBASIC programming questions.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Useful defines

Post by deltarho[1859] »

Just to get the ball rolling - with a little bit of editing so as to use the same parameters.

By MrSwiss

Code: Select all

#Define IsFalse(e) ( Not CBool(e) )
#Define IsTrue(e) ( CBool(e) )
I use those two a lot.

Code: Select all

' generate a random single from: within a given range
#Define SRange( f, l ) CSng( Rnd*( (l) - (f) ) + (f) )
By dodicat

Code: Select all

#Define IRange( f, l ) Int( Rnd*( (l+1) - (f) ) + (f) )
One of mine

Code: Select all

#Define CrLf Chr(13,10)
It will only take a few minutes to add some of your gems, so, add some of your gems. <smile>
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Useful defines

Post by Tourist Trap »

Hello, nice idea.

I use a lot of defines, butmost frequently min and max

Code: Select all

#define MIN(a, b)  iif((a)<(b), (a), (b))
Parenthesis are mandatory if the macro wants to handle any expression.

CEIL and FLOOR are also invaluable. But there is a debate on which fastest version one should use.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Useful defines

Post by deltarho[1859] »

Like it. Thanks, Tourist Trap.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Useful defines

Post by MrSwiss »

Another collection: little code = big difference (in convenience, for the programmer)
I'm too lazy, to re-write them ... <smile>
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Useful defines

Post by deltarho[1859] »

MrSwiss wrote:I'm too lazy, to re-write them ... <smile>
I wouldn't re-write them either.

Great link, MrSwiss, thank you.
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Useful defines

Post by deltarho[1859] »

By badidea

Code: Select all

#DEFINE ESC chr(27)
Ok, a simple one but it aids readability.
rpkelly
Posts: 52
Joined: Sep 03, 2016 22:36

Re: Useful defines

Post by rpkelly »

Although not defines, I'll contribute some of my favorite flavors of MOD for real numbers with Ceiling/Floor support.

Code: Select all

' ========================================================================================
' Force modulus x into the range a..b for Real Numbers
' ========================================================================================
Function cmMod3(ByVal x as Double, _
                           ByVal a as Double, _
                           ByVal b as Double) as Double

    If a = b Then
       Function = x
    Else
       Function = a + (cmMod(x - a,b - a))
    EndIf

End Function
' ========================================================================================
' x MOD y for Real Numbers, y<>0
' ========================================================================================
Function cmMod(ByVal x as Double, _
                         ByVal y as Double) as Double

    Function = x - y * cmFloor(x / y)

End Function
' ========================================================================================
' Return smallest integer greater than or equal to x
' ========================================================================================
Function cmCeiling(ByVal x as Double) as Long

    Function = -cmFloor(-x)

End Function
' ========================================================================================
' Return largest integer less than or equal to x
' ========================================================================================
Function cmFloor(ByVal x as Double) as Long

    Function = Int(x)

End Function
Last edited by rpkelly on Jun 27, 2018 2:07, edited 1 time in total.
PaulSquires
Posts: 999
Joined: Jul 14, 2005 23:41

Re: Useful defines

Post by PaulSquires »

I'll contribute a couple of thousand indispensable functions for every Windows FB programmer. If you are not using this library then you are certainly missing out on a lot.

From the master himself, José Roca: https://github.com/JoseRoca/WinFBX
deltarho[1859]
Posts: 4292
Joined: Jan 02, 2017 0:34
Location: UK
Contact:

Re: Useful defines

Post by deltarho[1859] »

Hi Rick - haven't seen you for a while.

@PaulSquires

A magistrate's warrant has been issued ordering you to remove one item of clothing for citing José Roca's superb WinFBX Framework in a thread entitled 'Useful defines'. Failure to comply may result in a custodial sentence of no more than 90 days. <smile>

Image

Who posted that image?
rpkelly
Posts: 52
Joined: Sep 03, 2016 22:36

Re: Useful defines

Post by rpkelly »

deltarho[1859] wrote:Hi Rick - haven't seen you for a while.
Been traveling, tying fishing flies, and of course, fishing. I'll be up in Alaska in Aug chasing those pretty arctic grayling with my son.

Now, I'm a bit behind on my FB efforts. We'll get cCalendar done with the addition of an IANA based time zone class and then my SQLite class that will have both a client and TCP based server class. That server class has a challenge to figure out how to support a connection pool without prior knowledge of the databases the client may want with attachments in addition to designing some kind of stored procedure that functions like that on MS SQL Server.

During all this, Paul looks likely to have his new IDE ready for some work and I'll get into that as well.

Rick
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Useful defines

Post by coderJeff »

I typically use GETMOUSE the same way every time. I find these #defines helpful to check mouse button combinations, like I am looking for left-mouse-button-down and right-mouse-button-released, or whatever. I included a little demo; ESCAPE to quit.

Code: Select all

'' useful defines for mouse buttons

#define MB_LEFT 1
#define MB_RIGHT 2
#define MB_MIDDLE 4

#define MB_IsUp(b_) ((mb and (b_))<>0)
#define MB_IsDown(b_) ((mb and (b_))=0)
#define MB_Pressed(b_) (((not omb and mb) and (b_))<>0)
#define MB_Released(b_) (((omb and not mb) and (b_))<>0)
#define MB_Changed(b_) (((omb xor mb) and (b_))<>0)


'' demo

dim as integer mx, my, mz, mb, omb
dim s as string 

do
	omb = mb
	GetMouse mx, my, mz, mb

	s = "LEFT:"
	if MB_Changed(MB_LEFT) then s += " C" else s += "  " 
	if MB_IsUp(MB_LEFT) then s += " U" else s += "  "
	if MB_IsDown(MB_LEFT) then s += " D" else s += "  "
	if MB_Pressed(MB_LEFT) then s += " P" else s += "  "
	if MB_Released(MB_LEFT) then s += " R" else s += "  "
	print s;

	s = "  --  RIGHT:"
	if MB_Changed(MB_RIGHT) then s += " C" else s += "  " 
	if MB_IsUp(MB_RIGHT) then s += " U" else s += "  "
	if MB_IsDown(MB_RIGHT) then s += " D" else s += "  "
	if MB_Pressed(MB_RIGHT) then s += " P" else s += "  "
	if MB_Released(MB_RIGHT) then s += " R" else s += "  "
	print s
		
	'' slow down printing
	sleep 100,1

loop until multikey(1)
frisian
Posts: 249
Joined: Oct 08, 2009 17:25

Re: Useful defines

Post by frisian »

rpkelly wrote:Although not defines, I'll contribute some of my favorite flavors of MOD for real numbers with Ceiling/Floor support.

Code: Select all

' ========================================================================================
' Force modulus x into the range a..b for Real Numbers
' ========================================================================================
Function cmMod3(ByVal x as Double, _
                           ByVal a as Double, _
                           ByVal b as Double) as Double

    If a = b Then
       Function = x
    Else
       Function = a + (cmMod(x - a,b - a))
    EndIf

End Function
' ========================================================================================
' x MOD y for Real Numbers, y<>0
' ========================================================================================
Function cmMod(ByVal x as Double, _
                         ByVal y as Double) as Double

    Function = x - y * cmFloor(x / y)

End Function
' ========================================================================================
' Return smallest integer greater than or equal to x
' ========================================================================================
Function cmCeiling(ByVal x as Double) as Long

    Function = -cmFloor(-x)

End Function
' ========================================================================================
' Return largest integer less than or equal to x
' ========================================================================================
Function cmFloor(ByVal x as Double) as Long

    Function = Int(x)

End Function
Ok functions rewitten as defines

Code: Select all

#Define def_floor(x) CLngInt(Int(x))
#Define def_ceiling(x) -def_floor(-x)
#Define def_fpmod(x, y) x - (y * def_floor(x / y))
#Define def_fpmod3(x, a, b) IIf(a = b, x, a + def_fpmod((x - a), (b - a)))
I also include a few define that I find usefull

Code: Select all

Const pi = Atn(1) * 4
#Define rad2deg(x) (x) * 180 / pi
#Define deg2rad(x) (x) * pi / 180
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Useful defines

Post by MrSwiss »

frisian wrote:I also include a few define that I find usefull

Code: Select all

Const pi = Atn(1) * 4
#Define rad2deg(x) (x) * 180 / pi
#Define deg2rad(x) (x) * pi / 180
I use them too, but in order to *eliminate* the division, as follows:

Code: Select all

Const As Single pi = Atn(1f) * 4f, d2r = pi / 180f, r2d = 180f / pi
#Define RAD(d)  ( (d) * d2r )
#Define DEG(r)  ( (r) * r2d )
Const As Single, to prevent "behind the scenes" cast's (from Double to Single).
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Useful defines

Post by dodicat »

I have a different fmod.
also, I use the map macro often.
mapping from one range into another.

Code: Select all

#define map(a,b,x,c,d) ((d)-(c))*((x)-(a))/((b)-(a))+(c)
#define dmod(x,y) (y)*frac((x)/(y))
screen 19

for x as long=350 to 450 step 5
    var newx=map(350,450,x,0,800)
    line(x,100)-(newx,500)
next

locate 5
print dmod(20*atn(1)-.05,4*atn(1))
#include "crt.bi"
print fmod(20*atn(1)-.05,4*atn(1))
sleep   
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Useful defines

Post by MrSwiss »

Another one, dealing with (32 bits) random-color generation:

Code: Select all

#Define RndARGB         ( CULng(Rnd() * &hFFFFFFFFul) )
Instead of *monstrosities* like the following:

Code: Select all

RGBA(Rnd() * 255, Rnd() * 255, Rnd() * 255, Rnd() * 255)
RGBA() is already a Macro (#Define) and, 4 calls to Rnd() ...

Many more, collected and written, dealing with fbGFX related stuff:
GFX_MATH.bi
Post Reply