Some beginner questions

New to FreeBASIC? Post your questions here.
Post Reply
misterc
Posts: 15
Joined: Sep 04, 2018 18:41

Some beginner questions

Post by misterc »

I'm a beginner to FreeBASIC and am enjoying it so far (Linux x86_64 w/ Geany). I have some beginner-level questions:

1. Where do I find out all the functions available to me through various libraries, like vbcompat, and any others? I see references to them, but am not sure where I can find the contents themselves.

2. How do I make my own include file for my own functions that I use all the time? Any best practices?

3. If I want to modify data that is provided to a Sub, rather than working on a copy of that data, do I always need to explicitly indicate ByRef?

4. The code below raises two errors with the compiler. I know I'm probably doing this the "PHP" way, but in addition to figuring out why the length won't print out, I'm looking for a way to get the type returned by a function, in order to troubleshoot its use. I thought I'd try TypeOf for that but I'm not sure how it's used (below).

Code: Select all

'Show the length of input text
Dim as String MyName, FileName

Dim as Integer NameLength, MyVar

Print "Hello world!"

Input "What's your name? ", MyName

MyVar = LEN(MyName)

'ERROR 3:  Line below raises error: Unexpected EOL. Why?
Print TypeOf(MyVar)

'ERROR 20: Line below raises error: Type mismatch, found '+' ...why?
Print "Hello, " + MyName + ". " + "Your name is " + NameLength + " letters long." 
5. Testing SDL using some code from the German FreeBASIC site, I get an error: Line 21, Error 41: Variable not declared, IMG_Load. But I think it is declared on line 20?

Code: Select all

' The following code loads an avatar.png and then displays it, waits for a keystroke, and then quits:
#INCLUDE "SDL\SDL.bi"                   'SDL inkludieren.
#DEFINE xmax 640                        'Fensterweite
#DEFINE ymax 480                        'Fensterhöhe

DIM SHARED video AS SDL_Surface PTR     'Pointer auf den späteren Fensterpuffer.
DIM SHARED event AS SDL_Event           'Event zum Abfangen von Tastenanschlägen.

'Einmal ein Fenster der größe 640x480 mit 32bit Farbtiefe
'SDL_HWSURFACE = Im Grafikspeicher (Grafikkarte / Reservierter RAM in manchen Laptops)
'SDL_DOUBLEBUF = Mit 2 Pages, damit das Zeichnen nicht so hässlich aussieht
'SDL HWACCELL =  Und Hardwarebeschleunigt
video = SDL_SetVideoMode(xmax,ymax,32, SDL_HWSURFACE OR SDL_DOUBLEBUF OR SDL_HWACCEL)
'Unter Freebasic entspräche das ungefähr screenres 640,480,32,2
'SDL_HWACCEL und SDL_HWSURFACE haben keine wirkliche Entsprechung.

''Ein Bild wie einen Avatar könnte man nun einfach mit
'IMG_Load laden.
''Code dafür wäre:
DIM avatar AS SDL_SURFACE PTR
avatar = IMG_Load("avatar.png")
''Gezeichnet kann das Ganze dann mit SDL_BlitSurface(start,startkoordinaten,Ziel, Zielkoordinaten) werden.
SDL_BlitSurface(avatar,NULL,video,NULL)
''Und angezeigt wird das Ganze letzendlich mit
SDL_Flip(video)
''und wenn man das Bild nicht mehr braucht, einfach mit
SDL_FreeSurface(avatar)
''den Speicher freigeben.

WHILE 1
    SDL_PollEvent(@event)       'Falls ein Event geschehen ist (Taste gedrückt?) bitte in event speichern.
    SELECT CASE event.TYPE      'Event ist ein zusammengesetzter Datentyp und hat von SDL aus schon einen Typen (Maus, Tastatur usw)
    CASE SDL_KEYDOWN:           'Falls eine Taste gedrückt wurde, Programm beenden.
            SDL_QUIT()          'SDL beenden und Fenster schließen.
            END
    END SELECT
WEND
Thanks for any help as I get up to speed.
nimdays
Posts: 236
Joined: May 29, 2014 22:01
Location: West Java, Indonesia

Re: Some beginner questions

Post by nimdays »

Something like this?

Code: Select all

'Show the length of input text
Dim as String MyName, FileName

Dim as Integer NameLength, MyVar

Print "Hello world!"

Input "What's your name? ", MyName

MyVar = LEN(MyName)

'ERROR 3:  Line below raises error: Unexpected EOL. Why?
#Print TypeOf(MyVar)

'ERROR 20: Line below raises error: Type mismatch, found '+' ...why?
Print "Hello, " + MyName + ". " + "Your name is " & MyVar & " letters long."

sleep
grindstone
Posts: 862
Joined: May 05, 2015 5:35
Location: Germany

Re: Some beginner questions

Post by grindstone »

misterc wrote:1. Where do I find out all the functions available to me through various libraries, like vbcompat, and any others? I see references to them, but am not sure where I can find the contents themselves.
An "include" file contains "normal" FB - source code. Simply load it into your IDE. The vbcompat.bi for example can be found in the /inc - folder of your FB installation.
misterc wrote:2. How do I make my own include file for my own functions that I use all the time? Any best practices?
You can include any FB - source file. It is commom practice to save those files with the extension ".bi", but you can include ".bas" files as well.
misterc wrote:3. If I want to modify data that is provided to a Sub, rather than working on a copy of that data, do I always need to explicitly indicate ByRef?
Strings and arrays are submitted ByRef by default, all other variables are default ByVal.
misterc wrote:4. The code below raises two errors with the compiler. I know I'm probably doing this the "PHP" way, but in addition to figuring out why the length won't print out, I'm looking for a way to get the type returned by a function, in order to troubleshoot its use. I thought I'd try TypeOf for that but I'm not sure how it's used (below).
TypeOf is only available at compile time and can't be printed out. In

Code: Select all

Print "Hello, " + MyName + ". " + "Your name is " + NameLength + " letters long." 
NameLength is an integer and can't be merged with strings. You either have to convert it

Code: Select all

Print "Hello, " + MyName + ". " + "Your name is " + Str(NameLength) + " letters long." 
or use the ampersand to merge the string

Code: Select all

Print "Hello, " & MyName & ". " & "Your name is " & NameLength % " letters long." 
misterc wrote:5. Testing SDL using some code from the German FreeBASIC site, I get an error: Line 21, Error 41: Variable not declared, IMG_Load. But I think it is declared on line 20?
The error message refers to IMG_Load, not to avatar. Try to change

Code: Select all

#INCLUDE "SDL\SDL.bi"                   'SDL inkludieren.
to

Code: Select all

#INCLUDE "SDL\SDL_image.bi"                   'SDL inkludieren.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Some beginner questions

Post by MrSwiss »

2. How do I make my own include file for my own functions that I use all the time? Any best practices?
There is a library-tutorial (for static lib's): Library Tutorial - Step by Step - DateTimeHelper
which in the process, also deals with the creation of an .bi file.
Additionally, there are some informations on, how to structure the coding process.
Mcmillian
Posts: 1
Joined: Sep 27, 2018 10:08

Re: Some beginner questions

Post by Mcmillian »

MrSwiss wrote: Sep 06, 2018 12:43
2. How do I find more about good sleep on Sleep Pursuits and make my own include file for my own functions that I use all the time? Any best practices?
There is a library-tutorial (for static lib's): Library Tutorial - Step by Step - DateTimeHelper
which in the process, also deals with the creation of an .bi file.
Additionally, there are some informations on, how to structure the coding process.
Are there any other examples of this I could use for practice btw? I'd appreciate it a lot. Cheers.
Last edited by Mcmillian on Jun 20, 2022 8:07, edited 2 times in total.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Some beginner questions

Post by MrSwiss »

Since I can't decipher, to what your question is referring to ... (this ???)
Have a look at the FreeBASIC-Manual.

It is far better to 'spell it out' in detail, if you are expecting a decent answer,
that is relevant to the question. (See the opening post, of this thread!)
Post Reply