Lazarus

Programming => LCL => Topic started by: Flaze07 on July 03, 2017, 04:37:42 pm

Title: needs a little help..
Post by: Flaze07 on July 03, 2017, 04:37:42 pm
hi so in order to make a gui app
which option should I choose ??

when I try to
Code: Pascal  [Select][+][-]
  1. uses Graphics;
  2.  

in simple programs;
it errors
Title: Re: needs a little help..
Post by: Thaddy on July 03, 2017, 04:47:51 pm
You should simply start up lazarus and choose File|New|Application.

You can also write GUI apps in FPC itself, but that is definitely not for beginners. You have to do everything by hand and it becomes very complex very quickly.

With Lazarus it is as easy as the above.
Title: Re: needs a little help..
Post by: Handoko on July 03, 2017, 04:57:15 pm
These simple tutorials will be provide some good starts:
http://wiki.lazarus.freepascal.org/Lazarus_Tutorial
https://devstructor.com/index.php?page=tutorials

For beginners who just started to learn Pascal:
http://www.pascal-programming.info/index.php
http://www.taoyue.com/tutorials/pascal/index.html

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

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

Must bookmark:
https://freepascal.org/docs-html/current/ref/ref.html
Title: Re: needs a little help..
Post by: RAW on July 03, 2017, 04:59:23 pm
Found this some time ago... (Windows only):

Runs fine and fast...  :D
Code: Pascal  [Select][+][-]
  1. {
  2.   Copyright (c) 1996 by Charlie Calvert
  3.   Modifications by Florian Klaempfl
  4.  
  5.   Standard Windows API application written in Object Pascal.
  6.   No VCL code included. This is all done on the Windows API
  7.   level.
  8. }
  9.  
  10. {$APPTYPE GUI}
  11. {$MODE DELPHI}
  12. program WinHello;
  13.  
  14. uses
  15.   Windows;
  16.  
  17. const
  18.   AppName = 'WinHello';
  19.  
  20. function WindowProc(Window: HWnd; AMessage: UINT; WParam : WPARAM;
  21.                     LParam: LPARAM): LRESULT; stdcall; export;
  22.  
  23.   var
  24.      dc : hdc;
  25.      ps : paintstruct;
  26.      r : rect;
  27.  
  28. begin
  29.   WindowProc := 0;
  30.  
  31.   case AMessage of
  32.     wm_paint:
  33.       begin
  34.          dc:=BeginPaint(Window,@ps);
  35.          GetClientRect(Window,@r);
  36.          DrawText(dc,'Hello world by Free Pascal',-1,@r,
  37.            DT_SINGLELINE or DT_CENTER or DT_VCENTER);
  38.          EndPaint(Window,ps);
  39.          Exit;
  40.       end;
  41.     wm_Destroy:
  42.       begin
  43.          PostQuitMessage(0);
  44.          Exit;
  45.       end;
  46.   end;
  47.  
  48.   WindowProc := DefWindowProc(Window, AMessage, WParam, LParam);
  49. end;
  50.  
  51.  { Register the Window Class }
  52. function WinRegister: Boolean;
  53. var
  54.   WindowClass: WndClass;
  55. begin
  56.   WindowClass.Style := cs_hRedraw or cs_vRedraw;
  57.   WindowClass.lpfnWndProc := WndProc(@WindowProc);
  58.   WindowClass.cbClsExtra := 0;
  59.   WindowClass.cbWndExtra := 0;
  60.   WindowClass.hInstance := system.MainInstance;
  61.   WindowClass.hIcon := LoadIcon(0, idi_Application);
  62.   WindowClass.hCursor := LoadCursor(0, idc_Arrow);
  63.   WindowClass.hbrBackground := GetStockObject(WHITE_BRUSH);
  64.   WindowClass.lpszMenuName := nil;
  65.   WindowClass.lpszClassName := AppName;
  66.  
  67.   Result := RegisterClass(WindowClass) <> 0;
  68. end;
  69.  
  70.  { Create the Window Class }
  71. function WinCreate: HWnd;
  72. var
  73.   hWindow: HWnd;
  74. begin
  75.   hWindow := CreateWindow(AppName, 'Hello world program',
  76.               ws_OverlappedWindow, cw_UseDefault, cw_UseDefault,
  77.               cw_UseDefault, cw_UseDefault, 0, 0, system.MainInstance, nil);
  78.  
  79.   if hWindow <> 0 then begin
  80.     ShowWindow(hWindow, CmdShow);
  81.     ShowWindow(hWindow, SW_SHOW);
  82.     UpdateWindow(hWindow);
  83.   end;
  84.  
  85.   Result := hWindow;
  86. end;
  87.  
  88. var
  89.   AMessage: Msg;
  90.   hWindow: HWnd;
  91.  
  92. begin
  93.   if not WinRegister then begin
  94.     MessageBox(0, 'Register failed', nil, mb_Ok);
  95.     Exit;
  96.   end;
  97.   hWindow := WinCreate;
  98.   if longint(hWindow) = 0 then begin
  99.     MessageBox(0, 'WinCreate failed', nil, mb_Ok);
  100.     Exit;
  101.   end;
  102.  
  103.   while GetMessage(@AMessage, 0, 0, 0) do begin
  104.     TranslateMessage(AMessage);
  105.     DispatchMessage(AMessage);
  106.   end;
  107.   Halt(AMessage.wParam);
  108. end.
Title: Re: needs a little help..
Post by: Thaddy on July 03, 2017, 05:36:50 pm
Yeah, we started off with that in TP7.... Charlie also wrote it in Pascal...
Anyway if you positively absolutely have to you can "simply" do this with the fpc editor (DON'T DO THIS!)
Code: Pascal  [Select][+][-]
  1. program sanslazaruside;
  2. {$mode objfpc}{$H+}
  3. uses
  4.   Interfaces, Forms;  // no res file needed...
  5. type
  6.   TForm1 = class(TForm)
  7.   end;
  8.  
  9. var
  10.   Form1: TForm1;
  11.  
  12. begin
  13.   Application.Initialize;
  14.   Application.CreateForm(TForm1, Form1);
  15.   Form1.Caption := 'GUI Application created from code';
  16.   Form1.SetBounds(100,100,700,500);
  17.   Application.Run;
  18. end.

You have to set a lot of the correct paths, but it works.

Plz just use lazarus, File|New|Application. You will be a lot happier.... 8-) O:-) And you need Lazarus units installed anyway.
Title: Re: needs a little help..
Post by: jc99 on July 03, 2017, 06:42:58 pm
Plz just use lazarus, File|New|Application. You will be a lot happier.... 8-) O:-) And you need Lazarus units installed anyway.
Definitely +1 (more +10)
a smaller start (assuming we're on windows) is
Code: Pascal  [Select][+][-]
  1. program PrjHelloWorld;
  2. {$apptype gui}
  3. uses windows;
  4. begin
  5.   MessageBox(0,'Hello World','Greetings',0);
  6. end.
But that is just a Messagebox, no Application.
As Thaddy said: Please use/start with Lazarus ...
Title: Re: needs a little help..
Post by: Thaddy on July 03, 2017, 07:09:34 pm
It is not even cross platform, this  is:
Code: Pascal  [Select][+][-]
  1. program sanslazarus;{$mode objfpc}{$H+}
  2. uses Interfaces,Dialogs;begin ShowMessage('Hello, there');end.
O:-)
Title: Re: needs a little help..
Post by: Flaze07 on July 03, 2017, 07:11:23 pm
Thank you for the advice  :D
So pascal is the first programming language I understand (tried to learn C++ on the web, understood nothing)
Anyways
so
Class is like Java's class (pointer )and
Object is like C++'s class (no pointer), am I right ?
And because I kinda dislike memory management (before I knew object I still do it , just afraid there might me memory leak ) should I use object ?

P.S win api is awesome I made several console aplication game using it
Title: Re: needs a little help..
Post by: jc99 on July 03, 2017, 07:23:05 pm
It is not even cross platform, this  is:
Code: Pascal  [Select][+][-]
  1. program sanslazarus;{$mode objfpc}{$H+}
  2. uses Interfaces,Dialogs;begin ShowMessage('Hello, there');end.
O:-)
No, it has to look like :
Code: Pascal  [Select][+][-]
  1. program PrjHelloWorld3;{$mode objfpc}{$H+}
  2. uses Interfaces,Dialogs,forms;begin Application.Initialize; ShowMessage('Hello, there');end.
And you have to import LCL, but you're right, that's cross platform ...

otherwise you get :
Code: Text  [Select][+][-]
  1. Failed to create win32 control, error: 1407 : Fensterklasse wurde nicht gefunden.
  2. [FORMS.PP] ExceptionOccurred
  3.   Sender=Exception
  4.   Exception=Failed to create win32 control, error: 1407 : Fensterklasse wurde nicht gefunden.
  5.   Stack trace:
  6.   $00000001001476F3
  7.   $000000010015D63C
  8.   $0000000100103FEE
  9.   $00000001000E269F
  10.   $00000001000E89A3
  11.   $00000001000E99CA
  12.   $00000001001036A1
  13.   $0000000100104C08
  14.   $00000001000F51C0
  15.   $000000010009AAB7
  16.   $000000010009A8CD
  17.   $000000010001801C
  18.   $0000000100017E86
  19.   $00000001000188F5
  20.   $0000000100024EF7
  21.   $00000001000DFD88
  22.   $00000001000A45BA
  23. TApplication.HandleException Failed to create win32 control, error: 1407 : Fensterklasse wurde nicht gefunden.
  24.   Stack trace:
  25.   $00000001001476F3
  26.   $000000010015D63C
  27.   $0000000100103FEE
  28.   $00000001000E269F
  29.   $00000001000E89A3
  30.   $00000001000E99CA
  31.   $00000001001036A1
  32.   $0000000100104C08
  33.   $00000001000F51C0
  34.   $000000010009AAB7
  35.   $000000010009A8CD
  36.   $000000010001801C
  37.   $0000000100017E86
  38.   $00000001000188F5
  39.   $0000000100024EF7
  40.   $00000001000DFD88
  41.   $00000001000A45BA
  42. Exception at 00000001001476F3: Exception:
  43. Failed to create win32 control, error: 1407 : Fensterklasse wurde nicht gefunden.
  44.  
Title: Re: needs a little help..
Post by: Thaddy on July 03, 2017, 09:31:38 pm
Probably only on Windows. Works like a charm on Linux.
Title: Re: needs a little help..
Post by: jc99 on July 03, 2017, 09:37:39 pm
Probably only on Windows. Works like a charm on Linux.
So much on crossplatform ...

What's the size of the bin on linux ? On windows its about 2.2M.
BWT the win only has about 44K.
Title: Re: needs a little help..
Post by: RAW on July 04, 2017, 01:44:18 am
Quote
BWT the win only has about 44K.
Yes, really nice ... 43,5 k..
(x64: 1,5 MB RAM)

BTW: compiles fine with {$MODE OBJFPC}{$H+}{$J-}...

I'm curious who will win the RAM-Game: WIN-API, KOL, fpGUI, MSE-IDE, LLCL...
Never really did that test.. nowadays nobody seems to care about that (of course), but it's interesting..  :D   
Title: Re: needs a little help..
Post by: Flaze07 on July 04, 2017, 07:21:47 am
I think I got it wrong
so the lcl is a gui library...
does that means it is unlike C++'s sfml ??
Title: Re: needs a little help..
Post by: Flaze07 on July 04, 2017, 09:27:08 am
so I need TCanvas to make something like C++'s sfml can make ?
Title: Re: needs a little help..
Post by: Ñuño_Martínez on July 04, 2017, 09:49:39 am
Thank you for the advice  :D
So pascal is the first programming language I understand (tried to learn C++ on the web, understood nothing)
That's the point.  C++ is one of the worst languages to start with.
Anyways
so
Class is like Java's class (pointer )
It is a simplification, but yes, it is.
and
Object is like C++'s class (no pointer), am I right ?
Again, it is a simplification, but yes, you are.
And because I kinda dislike memory management (before I knew object I still do it , just afraid there might me memory leak ) should I use object ?
No, you shouldn't.

OBJECT is only for backwards compatibility with (the very) old Turbo Pascal compilers.  You should use CLASS based objects instead.  I learned that recently.

As I've said, CLASS objects are similar to pointers but it is a simplification.  Actually there are some compiler magic that makes memory management a bit more easy than in Java and C++ (not garbage manager).  It isn't too hard.

You can read more about this in the CLASS vs. OBJECT and memory management (http://www.pascalgamedevelopment.com/showthread.php?32520-CLASS-vs-OBJECT-and-memory-management) discussion I had at Pascal Game Development forums.

If you need automatic objects like in C++ you can use advanced records (http://wiki.freepascal.org/Record#Advanced_record), but you must remember they're not objects (no inheritance, can't be used to implement TNotifyEvent responses, etc.).

Anyway you should follow some of the tutorials (http://wiki.freepascal.org/Lazarus_Documentation#Lazarus_and_Pascal_Tutorials) to learn more.
so I need TCanvas to make something like C++'s sfml can make ?
Nope, you don't need TCanvas.  Please follow the tutorials to learn more.
Title: Re: needs a little help..
Post by: Thaddy on July 04, 2017, 09:57:48 am
And, btw, it is possible to use sfml in Object Pascal by translating the C binding headers using h2paspp and h2pas and a little manual massaging.
There may be even already a Pascal header out there...[edit] Yes there is: https://github.com/DJMaster/csfml-fpc
Title: Re: needs a little help..
Post by: Handoko on July 04, 2017, 09:58:35 am
so I need TCanvas to make something like C++'s sfml can make ?

It depends on what the 'something' you meant is.

Quoted from Wiki:
Quote
Simple and Fast Multimedia Library (SFML) is a cross-platform software development library designed to provide a simple application programming interface (API) to various multimedia components in computers.

From the description above, we can know SFML is optimized for performance.

TCanvas is slow. If your application does not perform a lot of screen drawing, TCanvas is okay. But if you want to do graphics, games or multimedia, you should choose other libraries:

http://wiki.freepascal.org/Developing_with_Graphics
http://wiki.lazarus.freepascal.org/Graphics_libraries

I ever tried BGRABitmap, Allegro.pas, Castle Game Engine, all of them have good performance, should be enough to used for most cases. But if you want 'better' performance, you may try to use OpenGL to control the hardware directly:

http://wiki.freepascal.org/OpenGL
http://wiki.freepascal.org/OpenGL_Tutorial
Title: Re: needs a little help..
Post by: Thaddy on July 04, 2017, 09:59:52 am
See here Handoko, looks nice ;) https://github.com/DJMaster/csfml-fpc My edit crossed your last post again.
This may be an optimal solution for OP since he seems familiar with it.
Title: Re: needs a little help..
Post by: Handoko on July 04, 2017, 10:05:44 am
Great to know now SFML can be used on Free Pascal. It is very popular, I heard SFML a lot.

Someone please add it in to the wiki page.
Title: Re: needs a little help..
Post by: Flaze07 on July 04, 2017, 11:14:41 am
I see...thank you :) :D

(there's actually 2 sfml pascal bindings)

https://github.com/CWBudde/PasSFML

Edit : I tested these 2 pascal bindings several months ago... the codes were ugly, furthermore those are made for C++ and not pascal so ...I am just gonna try ZenGL (which is not updated anymore but it is the most popular ones I have heard)
Title: Re: needs a little help..
Post by: Thaddy on July 04, 2017, 12:49:54 pm
I see...thank you :) :D

(there's actually 2 sfml pascal bindings)

https://github.com/CWBudde/PasSFML
Christian Budde's version is based on the other one by Laurent Gomila. Christian usually writes extremely good code.
Quote
Edit : I tested these 2 pascal bindings several months ago... the codes were ugly, furthermore those are made for C++ and not pascal so ...I am just gonna try ZenGL (which is not updated anymore but it is the most popular ones I have heard)
The Pascal bindings are for Pascal use, not C++. Just the library is programmed and compiled in C++.
ZenGL is a mess (and not popular for that reason, not only because it isn'tmaintained) but there seems to be one (1) person that advocates its use.
That's something different from popular...

Try the BGRABitmap stuff and the TOpenGlControl. Those are popular! maintained! and very, very good!
Title: Re: needs a little help..
Post by: Flaze07 on July 04, 2017, 01:09:34 pm
ahh I see....thank you  :D
Title: Re: needs a little help..
Post by: DJMaster on September 10, 2017, 10:01:09 am
More info about csfml-fpc can be found within this thread:

https://forum.lazarus.freepascal.org/index.php/topic,33877.0.html

Take a look at the attached examples.
Title: Re: needs a little help..
Post by: DJMaster on September 10, 2017, 10:25:08 am
Great to know now SFML can be used on Free Pascal. It is very popular, I heard SFML a lot.

Someone please add it in to the wiki page.

Done! http://wiki.freepascal.org/Multimedia_Programming
Title: Re: needs a little help..
Post by: Handoko on September 10, 2017, 10:41:03 am
You should also add some info here:
http://wiki.freepascal.org/Graphics_libraries

Linux is not supported yet. I'm a Linux user. :'(
Title: Re: needs a little help..
Post by: DJMaster on September 11, 2017, 07:43:19 pm
...
Linux is not supported yet. I'm a Linux user. :'(

I'm a Windows user but Linux support is not a big deal: https://github.com/DJMaster/csfml-fpc/commit/2e9832451965927ff4defa6780be61abab896850 (https://github.com/DJMaster/csfml-fpc/commit/2e9832451965927ff4defa6780be61abab896850)

I've just tested the attached Colorful.pas example and it worked like a charm under bot x86_64-linux and i386-linux  8-)

BTW: can you explain why under Linux I have to call SetExceptionMask to avoid the exception "EInvalidOp: Invalid floating point operation"?
Title: Re: needs a little help..
Post by: Handoko on September 12, 2017, 04:59:05 am
Thanks DJMaster for making it Linux compilable.

I managed to compile Colorful.pas on my Linux64 GeForce 430, but I only got a blank window. I have libcfsml-dev 2.3 installed properly. I got no error but only 2 warnings when compiling.
Title: Re: needs a little help..
Post by: DJMaster on September 12, 2017, 05:46:25 am
SFML 2.4.2 and CSFML 2.4 are required. Make them from source and install in the right order. FPC 3.0.2 is recommended.
Title: Re: needs a little help..
Post by: DJMaster on September 12, 2017, 07:07:48 am
P.S.: shader loading has been modified in CSFML version 2.4 without backward compatibility: https://github.com/SFML/CSFML/issues/100

You can try the attached little Circle example, it should work out of the box.
Title: Re: needs a little help..
Post by: Handoko on September 12, 2017, 08:00:34 pm
Hooray, it worked!  :D
Although just some circles, it gives me reasons to explore more.

Can you tell me how to deploy programs that use SFML, especially on Windows? What dll files are required to copy to the users?
Title: Re: needs a little help..
Post by: DJMaster on September 12, 2017, 08:23:12 pm
On Windows you can deploy original CSFML dlls and OpenAL dll for i386-win32 (or x86_64-win64).
CSFML dlls are available here: https://www.sfml-dev.org/download/csfml/
OpenAL dll is avaliable here (as installer): https://www.openal.org/downloads/

Alternatively you can build dlls yourself from sources under MSYS2 for both 32 and 64 bit Windows OSs. In this case you have to additionally deploy libgcc_s_dw2-1.dll (for win32) or libgcc_s_seh-1.dll (for win64) and libwinpthread-1.dll from the corresponding mingw32/mingw64 bin directories. I prefer this way when I use other dlls at the same time to be sure to use libraries built under the same environment.
Title: Re: needs a little help..
Post by: Handoko on September 12, 2017, 08:39:01 pm
Thank you for the information. I am currently a bit busy, I will try it when I have more time.
Title: Re: needs a little help..
Post by: DJMaster on September 12, 2017, 08:43:28 pm
You are welcome!  :D

p.s.: what about the Colorful.pas example? Did you test it with CSFML 2.4.0?
Title: Re: needs a little help..
Post by: Handoko on September 12, 2017, 09:02:56 pm
I have not. I know I can build CSFM 2.4.0 or set Linux PPA manually, but prefer the easiest. The repository of my Ubuntu Mate 16.10 only shows CFSML 2.3 and SFML 2.4. I am not willing to change it, at least for now.

Ubuntu (Mate) 17.10 is going to be released, maybe this month or next month. Then I will upgrade my Linux and see what CFSML version will be in the repository by default.

Installing libraries in Linux is very easy, just some clicks. Below is the screenshot of Synaptic Package Manager, which I usually use for installing/removing applications and libraries.
TinyPortal © 2005-2018