Recent

Author Topic: lazarus beginer quetions  (Read 10369 times)

bluatigro

  • New Member
  • *
  • Posts: 26
  • everybody is diferent therefore everybody is equal
lazarus beginer quetions
« on: December 14, 2017, 10:14:31 am »
i m new to lazzarus NOT to programming

i have a view questions :
how do i create a array ?
how do i save/load a bmp ?
how do i do OOP , subs and functions ?
what about random ?
whitch string functions are there ?
how do i do opengl ?

i have no book and i can't pay for it

i have experiance whit :
raytacing , fuzzy logic , genetic [ algotims | programing ]
animation in opengl and a sphere-system
i m interested in :
A.I. , graphics and games

i m not good at designing new games

my speciality is building libraries

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: lazarus beginer quetions
« Reply #1 on: December 14, 2017, 10:48:47 am »
Hello bluatigro,
Welcome back to the forum.

You don't need to spend money to buy books, lots of information are free on the web.

How to create and use array in Pascal:
http://wiki.freepascal.org/Array

Example to load bmp files in Lazarus:
http://forum.lazarus.freepascal.org/index.php/topic,39014.msg266654.html#msg266654

OOP in Pascal:
http://wiki.freepascal.org/Object_Oriented_Programming_with_Free_Pascal_and_Lazarus
http://wiki.freepascal.org/Programming_Using_Objects
https://www.tutorialspoint.com/pascal/pascal_object_oriented.htm

Random in Pascal:
https://www.freepascal.org/docs-html/rtl/system/random.html

About string in Pascal:
https://www.freepascal.org/docs-html/rtl/sysutils/stringfunctions.html
http://wiki.freepascal.org/String

OpenGL in Lazarus:
http://wiki.freepascal.org/OpenGL_Tutorial
http://wiki.freepascal.org/Character_and_string_types

----------

Some other pages I recommend:

Lazarus tutorial for beginners:
http://wiki.lazarus.freepascal.org/Lazarus_Tutorial

List of tutorials with wide range of topics (graphics, database, printer, web, etc):
http://wiki.freepascal.org/Lazarus_Documentation#Lazarus.2FFPC_.28Free_Pascal.29
http://wiki.freepascal.org/Category:Tutorials

(Free) Pascal reference guide:
http://freepascal.org/docs-html/current/ref/ref.html

If you're looking for step-by-step Pascal tutorial, try these: http://www.pascal-programming.info/index.php
http://www.taoyue.com/tutorials/pascal/index.html

Some cool Pascal tricks:
http://lazplanet.blogspot.co.id/

Pascal video tutorials:
https://devstructor.com/index.php?page=tutorials

If you want to be a game developer/programmer, read my post:
http://forum.lazarus.freepascal.org/index.php/topic,30799.msg196282.html#msg196282


bluatigro

  • New Member
  • *
  • Posts: 26
  • everybody is diferent therefore everybody is equal
Re: lazarus beginer quetions
« Reply #3 on: December 14, 2017, 12:02:36 pm »
@ al :
thanks for al help

it wil take some time to read that all
i just looked at a tutoial on YouTube

leaned a lot already

can lazarus be instaled on usb ?
whit OOP i m missing operator overloading !

Handoko

  • Hero Member
  • *****
  • Posts: 5122
  • My goal: build my own game engine using Lazarus
Re: lazarus beginer quetions
« Reply #4 on: December 14, 2017, 12:19:47 pm »

turrican

  • Full Member
  • ***
  • Posts: 133
  • Pascal is my life.
    • Homepage
Re: lazarus beginer quetions
« Reply #5 on: December 14, 2017, 05:32:40 pm »
how do i create a array ?

-  You haven't to create an array itself. You can define an array like this :

Code: Pascal  [Select][+][-]
  1. // On the stack
  2. var
  3.   //Dynamic array
  4.   strdarray : array of string;
  5.   // Fixed array
  6.   strfarray : array[0..1024] of string;
  7.   //iterator
  8.   x : Integer;
  9. begin
  10.   //Dynamic array
  11.   //Allocate dynamic array Length
  12.   SetLength(strdarray ,1024); //Caution 0 doesn't count
  13.   //Iterate and fill array
  14.   for x := 0 to 1023 do strdarray[x] := 'Iteration' + IntToStr(x); //Caution 0 counts
  15.   //Static array
  16.   //You don't need to allocate the array
  17.   for x := 0 to 1023 do strfarray [x] := 'Iteration' + IntToStr(x); //Caution 0 counts
  18. end;
  19.  

how do i save/load a bmp ?
You can use streaming class to do it like another binary file (load bytes to memory) You didn't mention paint bmp on the canvas :)
Code: Pascal  [Select][+][-]
  1. var
  2.   bmpStream : TFileStream;
  3.   byteChar : Char;
  4. begin
  5.   bmpStream := TFileStream.Create('file.bmp', fmOpenRead);
  6.   try
  7.     while (bmpStream.Read( byteChar, 1 ) <> 0) do writeln(byteChar);
  8.     bmpStream
  9.   finally
  10.     bmpStream.Free;
  11.   end;
  12. end;
  13.  

how do i do OOP , subs and functions ?
Like other OOP Languages! Object Pascal is strong OOP Language.
Code: Pascal  [Select][+][-]
  1. //Interface declaration like .h/h++ files
  2. interface
  3.  
  4. type
  5. //Class definition  
  6. TClassObject = class(TObject)
  7. //Method and property definition
  8. //Visibility
  9. private
  10.   fname : string;
  11. public
  12.   property Name : string read fname write fname; //Auto get set property
  13.   property CustomName : string read fGetCustomName write fSetCustomNames; //With custom getter setter
  14.   constructor Create(const Name : string); //Constructor
  15.   destructor Destroy; //Destructor
  16. end;
  17.  
  18. // Class implementation like .c/c++ files
  19. implementation
  20.  
  21. constructor TClassObject.Create(const Name : string);
  22. begin
  23.   fname := 'Test';
  24. end;
  25.  
  26. destructor TClassObject.Destroy;
  27. begin
  28.   inherited;
  29. end;
  30.  

what about random ?
I like random but this is pseudorandom at all :)
Code: Pascal  [Select][+][-]
  1. begin
  2.   Randomize; //Prepair Random seed
  3.   if Random > 1 then WriteLn('Integer is larger than 1') //Generate a number
  4.   else WriteLn('0 or less');
  5.   Writeln(IntToStr(RandomRange(1, 100))); //Generate a random number
  6. end;
  7.  

whitch string functions are there ?
All, like other languages!
how do i do opengl ?
It's very "easy" but better on another chapter :)

bluatigro

  • New Member
  • *
  • Posts: 26
  • everybody is diferent therefore everybody is equal
Re: lazarus beginer quetions
« Reply #6 on: December 15, 2017, 11:18:40 am »
first try at OOP

i need for al graphics a real3d class

this i think is not compleetly corect
this wil be changed | extended

Code: Pascal  [Select][+][-]
  1. type TReal3d = object
  2. public :
  3.   x , y , z : real ;
  4.   constructor init( a , b , c : real )
  5. end ;
  6. operator + ( a , b : TReal3d ) : TReal3d
  7.   return init( a.x + b.x , a.y + b.y , a.z + b.z ) ;
  8. end ;
  9. function cross( a , b : TReal3d ) : TReal3d
  10.   cross := init( a.y * b.z - a.z * b.y
  11.                , a.z * b.x - a.x * b.z
  12.                , a.x * b.y - a.y * b.x ) ;
  13. end ;
  14.  

what is the extencion of lazarus code ?
« Last Edit: December 15, 2017, 11:31:48 am by bluatigro »

Thaddy

  • Hero Member
  • *****
  • Posts: 14158
  • Probably until I exterminate Putin.
Re: lazarus beginer quetions
« Reply #7 on: December 15, 2017, 12:09:51 pm »
There are many things wrong with your code, but try this:
Code: Pascal  [Select][+][-]
  1. program untitled;
  2. {$ifdef fpc}{$mode objfpc}{$modeswitch advancedrecords}{$H+}{$endif}
  3. type
  4.   TReal3d = record
  5.     x , y , z : Double;
  6.     procedure Initialize( const a , b , c : double );
  7.   end;
  8.  
  9. procedure TReal3d.Initialize(const a,b,c :double);
  10. begin
  11.    x:=a;
  12.    y:=b;
  13.    z:=c;
  14. end;
  15.  
  16. operator + (const  a , b : TReal3d ): TReal3d;
  17. begin
  18.   Result.x := a.x+b.x;
  19.   Result.y := a.y+b.y;
  20.   Result.z := a.z+b.z;
  21. end ;
  22.  
  23. function cross(const a ,b :TReal3d):TReal3d;
  24. begin
  25.   Result.x := a.y * b.z - a.z * b.y;
  26.   Result.y := a.z * b.x - a.x * b.z;
  27.   Result.z := a.x * b.y - a.y * b.x;
  28. end;
  29.  
  30. var aa,bb,cc,crossme:TReal3d;
  31. begin
  32.   aa.initialize(1.0,2.0,3.0);
  33.   bb.initialize(3.0,2.0,1.0);
  34.   cc := aa + bb;
  35.   writeln(cc.x);
  36.   crossme := cross(aa,bb);
  37.   writeln(crossme.x);
  38. end.

Note object is deprecated, you should use records or classes, not object
Note real is deprecated, it is now an alias for double. (although there is a unit that supports old school real, as in real48)
Note that your type of code, with all the errors corrected, is allowed and will still work. But you should learn from a more modern source, at least beyond 1995.
« Last Edit: December 15, 2017, 02:01:24 pm by Thaddy »
Specialize a type, not a var.

bluatigro

  • New Member
  • *
  • Posts: 26
  • everybody is diferent therefore everybody is equal
Re: lazarus beginer quetions
« Reply #8 on: December 15, 2017, 02:58:20 pm »
i think i got it now almost correct


i m not sure about the toColor() function
[ this idea looks strange but it wil be clear later ]

how do form's eat color's ? int32 , ulong or else ?
Code: Pascal  [Select][+][-]
  1. type dbl3d = record
  2.   x , y , z : double ;
  3.   constructor init( a , b , c : double )
  4. end ;
  5. procedure dbl3d.init( a , b , c : double )
  6. begin
  7.   x := a ;
  8.   y := b ;
  9.   z := c ;
  10. end ;
  11. operator + ( a , b : dbl3d ) : dbl3d
  12. begin
  13.   Result.x := a.x + b.x ;
  14.   Result.y := a.y + b.y ;
  15.   Result.z := a.z + b.z ;
  16. end ;
  17. operator - ( a , b : dbl3d ) : dbl3d
  18. begin
  19.   Result.x := a.x - b.x ;
  20.   Result.y := a.y - b.y ;
  21.   Result.z := a.z - b.z ;
  22. end ;
  23. operator / ( a : dbl3d , b : double ) : dbl3d
  24. begin
  25.   Result.x := a.x / b ;
  26.   Result.y := a.y / b ;
  27.   Result.z := a.z / b ;
  28. end ;
  29. operator * ( a : dbl3d , b : double ) : dbl3d
  30. begin
  31.   Result.x := a.x * b ;
  32.   Result.y := a.y * b ;
  33.   Result.z := a.z * b ;
  34. end ;
  35. function cross( a , b : dbl3d ) : dbl3d
  36. begin
  37.   Result.x := a.y * b.z - a.z * b.y ;
  38.   Result.y := a.z * b.x - a.x * b.z ;
  39.   Result.z := a.x * b.y - a.y * b.x ;
  40. end ;
  41. function dot( a , b : dbl3d ) : double
  42. begin
  43.   Result := a.x * b.x + a.y * b.y + a.z * b.z ;
  44. end ;
  45. function len( a : dbl3d ) : double
  46. begin
  47.   Result := sqr( dot( a , a ) ) ;
  48. end ;
  49. function normalize( a : dbl3d ) : dbl3d
  50. begin
  51.   Result := a / len( a ) ;
  52. end ;
  53. function getangle( a , b : dbl3d ) : double
  54. var
  55.   la , lb , d : double
  56. begin
  57.   la := len( a ) ;
  58.   lb := len( b ) ;
  59.   d := dot( a , b ) :
  60.   Result := acos( d / ( la * lb ) ) ;
  61. end ;
  62. function toColor( a : dbl3d ) : int32
  63. begin
  64.   Result := rgb( a.x*255 , a.y*255 , a.z*255 ) ;
  65. end ;
  66.  

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: lazarus beginer quetions
« Reply #9 on: December 15, 2017, 03:27:37 pm »
In the LCL the Color property of forms and other controls is a TColor.
This is a 4-byte signed integer type, and many values have Delphi-compatible names such as clWhite, clNone, clYellow, clBtnFace etc.
The Graphics unit provides a conversion function RGBToColor() which takes three Byte parameters for the R, G, B values and returns a TColor value you can assign to any Color property where you want to use an unnamed color with specific R, G and B components.

Thaddy

  • Hero Member
  • *****
  • Posts: 14158
  • Probably until I exterminate Putin.
Re: lazarus beginer quetions
« Reply #10 on: December 15, 2017, 05:13:55 pm »
i think i got it now almost correct


i m not sure about the toColor() function
[ this idea looks strange but it wil be clear later ]

how do form's eat color's ? int32 , ulong or else ?
Code: Pascal  [Select][+][-]
  1. type dbl3d = record
  2.   x , y , z : double ;
  3.   constructor init( a , b , c : double ); // this won't work
  4. end ;
Unfortunately an advanced record can not use a constructor with parameters.
That's why my example has a simple procedure Initialize.
After you correct that, your code will work, but note Howardpc's explanation about TColor.
Specialize a type, not a var.

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: lazarus beginer quetions
« Reply #11 on: December 15, 2017, 06:47:27 pm »
first try at OOP
i need for al graphics a real3d class
this i think is not compleetly corect
this wil be changed | extended
In FPC, as far as I know, object is not deprecated, so you can safely use it. If you want to use inheritance, virtual functions, then this is better than just a record. If you do not need virtual functions, you can use procedure instead of constructor.
Structured types as parameters are always better to pass as const (as seen in the Thaddy code). It's probably easier to do always for parameters of any type.
Well, here's your original version would look like this.
Code: Pascal  [Select][+][-]
  1. {$APPTYPE CONSOLE}
  2. {$mode objfpc}
  3. program Project1;
  4.  
  5. type
  6.   TReal3d = object
  7.   public
  8.     x, y, z: Double;
  9.     procedure Init(const ax, ay, az: Double);
  10.   end;
  11.  
  12. operator + (const a, b: TReal3d): TReal3d;
  13. begin
  14.   Result.Init(a.x + b.x, a.y + b.y, a.z + b.z);
  15. end;
  16.  
  17. function Cross(const a, b: TReal3d ): TReal3d;
  18. begin
  19.   Cross.Init(
  20.     a.y * b.z - a.z * b.y,
  21.     a.z * b.x - a.x * b.z,
  22.     a.x * b.y - a.y * b.x);
  23. end;
  24.  
  25. procedure TReal3d.Init(const ax, ay, az: Double);
  26. begin
  27.   x := ax;
  28.   y := ay;
  29.   z := az;
  30. end;
  31.  
  32. var
  33.   Test1, Test2: TReal3d;
  34. begin
  35.   Test1.Init(1, 2, 3);
  36.   Test2.Init(4.5, 5.5, 6.5);
  37.   Test1 := Test1 + Test2;
  38.   Writeln(Test1.y:0:2);
  39.   Test2 := Cross(Test1, Test2);
  40.   Writeln(Test2.z:0:2);
  41.   Readln;
  42. end.

Thaddy

  • Hero Member
  • *****
  • Posts: 14158
  • Probably until I exterminate Putin.
Re: lazarus beginer quetions
« Reply #12 on: December 15, 2017, 09:32:26 pm »
first try at OOP
i need for al graphics a real3d class
this i think is not compleetly corect
this wil be changed | extended
In FPC, as far as I know, object is not deprecated, so you can safely use it. If you want to use inheritance, virtual functions, then this is better than just a record. I
In mode objfpc and mode delphi - and to my deep regret, because I like it, object must be assumed deprecated in favor of classes and advanced records.
Any advise that differs from that is bad advise.
In mode TP (which is a legacy mode by itself) of course object is the way to go.
Don't give newbee's advise about such things if you are not completely sure what you are writing. Newbee's should use classes, not objects. Objects are more difficult to use correctly as well.
Although they are a good tool in the hands of an advanced Pascal specialist.

Fresh programmers in Object Pascal should for the time being NEVER EVER use objects.
If they are seasoned programmers and KNOW all the low level ins and outs they may want to use objects, because it is powerful in the right hands. (as long as it lasts, that is)

And You Know That. <very grumpy, Friday again  >:D >:D >:D >:D > Stop putting noobs on the wrong track.

New users should start with the current language, not with legacy support.
You can see from his efforts he is already mixed up with modern features (operators) and legacy (objects)
From an educational point of view hardly defendable to encourage him on that path.....

I repeat:
And You Know That. <very grumpy, Friday again  >:D >:D >:D >:D >
« Last Edit: December 15, 2017, 09:39:59 pm by Thaddy »
Specialize a type, not a var.

ASerge

  • Hero Member
  • *****
  • Posts: 2212
Re: lazarus beginer quetions
« Reply #13 on: December 15, 2017, 09:39:17 pm »
Newbee's should use classes, not objects. Objects are more difficult to use correctly as well.
Yes. But comparing object and record, I prefer object. Because its possibilities are wider.

Thaddy

  • Hero Member
  • *****
  • Posts: 14158
  • Probably until I exterminate Putin.
Re: lazarus beginer quetions
« Reply #14 on: December 15, 2017, 09:42:44 pm »
Compared to what? classes? advanced records? It only complicates things.
Maybe a handful of people here, you, me, Marco, Howardpc, molly, getmem, dev team, etc know how to handle the old object paradigm.
I like object, you know that (KOL) but do not give the secret away to new users.... 8-) O:-) O:-) O:-) :D

IMHO it must be considered a language feature for advanced users. And use at your own risk.
« Last Edit: December 15, 2017, 09:46:22 pm by Thaddy »
Specialize a type, not a var.

 

TinyPortal © 2005-2018