Click here to Skip to main content
15,879,535 members
Articles / Desktop Programming / Win32

PureBasic - The Perfect Cross-Platform & Native Development Language

Rate me:
Please Sign up or sign in to vote.
4.67/5 (27 votes)
10 Jan 2015CPOL5 min read 100.4K   19   46
Native, simply, fast, cross-platform, and quite powerful PL? Is it nothing? No, it isn't. It is PureBasic!

Why BASIC? Why this article?

No. I am not a BASIC adherent. I writing in more than 10 PLs including C#, Java, C++ and other main-stream PLs.

I don't think that PureBasic better than all of these PLs.

PureBasic isn't a best PL. C# is more convenient and powerful than PB for writing Windows applications. Python and Java is better for cross-platform development, not least because not compiling to platform-dependent executable format on many OSes.

But, if you want write native applications, not requiring a third-party dlls and platforms, or native and cross-platform applications, you can't use C#, Java, Python. You can use Delphi, a few C++ implementations, and a few more PLs. And, PureBasic is best for most native purposes. Really.

Also I am not a spammer advertising a commercial product on CodeProject. I do not work for the authors of PB. PB already enough popularly PL. I just help you orient that language is best for your purposes.

Why native & no 3rd-party? Why not .NET, JVM?

Where native and third-party independent development is necessary?

  1. Installers development. Installer shouldn't require a third-party platform. Also very undesirable to require a few third-party DLLs weighing a few MBs at all.
  2. SFX-archives development.
  3. CD-shells development.
  4. Drivers development. Driver just will not work if will require a .NET, JVM. high-level third-party libs.
  5. Adware and "legal adware" (with neccessary license agreement). Adware should eat a little CPU time, RAM and disk space and shouldn't require a third-party platform.
  6. Game development. There high performance needed. MS.NET or JVM are bad choice.
  7. And other soft requiring a performance and light weight.
  8. And "temporary" soft writing for experiments with undocument libx, hacks, etc. Tool for this should be quick, native and simply.

Why not Pure C/C++, Delphi, C++ Builder?

Pure C/C++ is hard to learning and using.
For example, you can't write just:
C++
MessageBox(Str(2 + Int("2")));
or
C++
intvar = Int("2");

intvar2 = 2 + intvar;

strvar = Str(intvar);

MessageBox(strvar);
You should write:
C++
// WARNING: compile in Unicode
#include <Windows.h>
#include <tchar.h>
#include <sstream>
#include <string>
// #pragma comment(lib, "user32.lib") --- for MessageBox, required if lib not added in the compiler options

using namespace std;

int main()
{
    int intvar;
    wistringstream s(_T("2"));
    s >> intvar;

    wostringstream ss;
    ss << 2 + intvar;
    wstring strvar = ss.str();

    MessageBox(0, strvar.c_str(), _T(""), MB_OK); 

    return 0;
}

You should learn and write a much more to obtain the same result.

Delphi and C++ Builder aren't so hard. But they are paid (and very expensive) and creating quite large EXEs. Also, Delphi has long been used as malware creating tool - now few antiviruses suspects of Delphi EXEs in malware.

PureBasic: Compiler

PB designed for Windows, Linux, Mac OS and Amiga OS development. Have x86 and x64 versions.

PB compiling to platform-dependent binary format (EXE in Win) by platform-specified compiler version.

PB compiler at first translates a PB code into FASM code and next compiles this code by FASM compiler which provides a great performance and inline FASM using possibility.

PB compiler creating a light-weight (2 KB and more), fully native EXEs requiring only msvcrt.dll and WinAPI (DependencyWalker screen):

msvcrt.dll pre-installed in Win 2000 and newer.

PureBasic: Standard Library

PB's standard library including a many of functions with different "cross-platformity" level (see documentation).

For example, it contains functions for use:

  • BMP, JPEG, JPEG2000, PNG, TGA, TIFF formats
  • Scintilla (code editing control)
  • SQLite, PostgreSQL, ODBC databases
  • Ogre 3D engine (!) It allow you to work with 2D and 3D graphics. Ogre lib integrared into PureBasic.

Also you can use a many of native OS API functions and interfaces, such as WinAPI on Windows and GDK/GTK on Linux.

PureBasic: Paradigm and Syntax

PB supports imperative and procedural paradigm. 

PB officially not fully supports object-oriented paradigm today. In PB 5.20+ (23 July 2014) you can use Modules, like a Modules in VB 6.0 / VBA or static classes in C# / VB.NET. Also you can use "dirty tricks" such as http://forum.purebasic.com/english/viewtopic.php?f=12&t=61126 to imitate OOP in your applications.

In PB you shouldn't implement a main class or an entry point a main() in C++. You can just write a code in editor from its first to last line, such as in many of script PLs. It's very useful for "temporary" and "experimental" apps (see Why native & no 3rd-party? Why not .NET, JVM?)

Comments

ASM
Code() ; Comment as in the asm

Variables

Explicit integer variable definition:

ASM
x.i

Explicit integer variable definition and initialization:

ASM
x.i = 5

Explicit integer variable definition and using:

ASM
x.i
x = 5   

y.i
y = x * 2 ; 10

Explicit string variable definition (via 2 ways!) and using:

ASM
str1.s = "Lorem"           ; 1 way
str2$ = "ipsum"            ; 2 way

str3$ = str1 + " " + str2$ ; Lorem ipsum

Fixed string:

ASM
str1.s{5}
str1 = "Lorem ipsum" ; Lorem

str2${5}
str2$ = "Lorem ipsum" ; Lorem

<span style="color: rgb(17, 17, 17); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px;">; Other variable types see in documentation.</span>

Implicit variable definition:

ASM
x = 5
y = 2 * x ; 10

Variables convertation

Integer to string and back:

ASM
x = 5
str$ = Str(x)
x = 5 + Val(str$) ; 10

Double to string and back:

ASM
x.d = 3.2
str$ = StrD(x)
x = 2.5 + ValD(str$) ; 5.7

Double to string without 0 at end:

ASM
x.d = 3.2
str$ = StrD(x)            ; 3.2000000000
str$ = Trim(str$, "0") ; 3.2

Constants

ASM
#x = 1
#y = "2"
#z = 3.14

Enumerations

ASM
Enumeration
  #x ; 0
  #y ; 1
  #z ; 2
EndEnumeration

Ext. syntax:

VB.NET
Enumeration [<constant> [Step <constant>]] 
  #Constant1
  #Constant2 [= <constant>]
  #Constant3
  ...
EndEnumeration

Console Interface

ASM
OpenConsole()
Print("Hello world!")
Input()                      ; wait for key or input a value

Debug-time console interface

ASM
Debug "It will be printed in the debug output window"

Procedures

VB.NET
Procedure MsgBox(text$)
  MessageRequester("", text$)
EndProcedure

MsgBox("Hello World")

With value return:

VB.NET
Procedure$ InputBox(text$)
  ProcedureReturn InputRequester("", text$, "")
EndProcedure

name$ = InputBox("Print your name")
MessageRequester("", name$)

Data in EXE

This feature resembles resources in many other programming languages. But it isn't standard Windows resources and can't be readed by hackers using ResHacker or another resource viewer.

Numerical data:

ASM
Restore NumData
Read.l A
Read.l B

MessageRequester("", Str(A))
MessageRequester("", Str(B))

DataSection
  NumData:
  Data.l 1, 2
EndDataSection 

String data:

ASM
Restore NumData
Read$ A$
Read$ B$

MessageRequester("", A$ + B$)

DataSection
  NumData:
  Data$ "Hello ", "world"
EndDataSection

Text file content:

ASM
Restore File1
Read.s File1Data$

MessageRequester("", File1Data$)

DataSection
  File1:
  IncludeBinary "file.txt"
EndDataSection

To be continued...

PureBasic: IDE

PureBasic IDE is very simply:

...but quite powerful:

Code regions (folding):

PureBasic: GUI

In PureBasic you can create GUI by 5 ways:

  1. Using PB's standard cross-platform GUI library only. Simplest way.
  2. Using native OS API only. For example, WinAPI allows you to create extemelly small executables, sometimes gives access to special features OS. Hardest and not always justified way.
  3. Combining PB's standard cross-platform GUI library and OS API if it needed. Prefered way in most cases.
  4. Using PB's standard cross-platform 3D GUI library. It's using 3D engine to draw game-like GUI and a bit like WPF in Microsoft .NET :) Prefered way for game and 3D graphics development.
  5. Using third-party GUI libraries based on 1, 2, 4 way or combining few ways. Backhanded way, depends on the quality of libraries.

Way 1 - Standard cross-platform GUI library

A simple GUI program in PB:
ASM
Enumeration
  #btn1
EndEnumeration

OpenWindow(#PB_Any, 10, 50, 400, 200, "Lorem ipsum", #PB_Window_SystemMenu)

ButtonGadget(#btn1, 10, 10, 100, 25, "Button 1", 0)

Repeat
  Event = WaitWindowEvent()
  
  If Event = #PB_Event_Gadget
    GadgetID = EventGadget()
    
    If GadgetID = #btn1
      MessageRequester("", "Button 1 clicked!")
    EndIf
  EndIf
  
Until Event = #PB_Event_CloseWindow
 
There is very simply to write GUI in the PureBasic by hand. But even quicker and possibly more convenient to use a visual (WYSIWYG) designer.
 
You can use a simply Visual Deigner included in PB:
 
 
... or more powerful PurePORM editor
 
 
... or create your own GUI builder :)

Way 2 - Native OS API

A simple (or not so? :)) WinAPI GUI program in PB:

ASM
; If you want to use in GUI not only latin characters, compile it in Unicode!
; Warning: exe size - it's very, very small!

Declare.l WndProc(hWnd, Msg, wParam, lParam) ; declare Window events callback

; Global vars
WindowClass.s = "WndClass1"

; Initialize Window Class
wc.WNDCLASSEX
wc\cbSize = SizeOf(WNDCLASSEX)
wc\hbrBackground = #COLOR_WINDOW
wc\hCursor = LoadCursor_(0, #IDC_ARROW)
wc\lpfnWndProc = @WndProc()
wc\lpszClassName = @WindowClass

; register Window Class
If RegisterClassEx_(@wc) = 0
  MessageBox_(hWnd, "Can't register main window class.", "", #MB_ICONERROR)
  End
EndIf

; create window
hWnd = CreateWindowEx_(0, WindowClass, "Lorem ipsum", #WS_SYSMENU, 10, 50, 400, 200, 0, 0, 0, 0)
If hWnd = 0
  MessageBox_(hWnd, "Can't create main window.", "", #MB_ICONERROR)
  End
EndIf

; create button and set it's font
hButton1 = CreateWindowEx_(0, "Button", "Button 1", #WS_CHILD | #WS_VISIBLE, 10, 10, 100, 25, hWnd, 0, 0, 0) 
SendMessage_(hButton1, #WM_SETFONT, GetStockObject_(#DEFAULT_GUI_FONT), 1)

; show window
ShowWindow_(hWnd, #SW_SHOWDEFAULT) 
UpdateWindow_(hWndMain)

; messages handling loop
While GetMessage_(msg.MSG, #Null, 0, 0 ) 
  TranslateMessage_(msg) 
  DispatchMessage_(msg) 
Wend

; window events callback
Procedure.l WndProc(hWnd, Msg, wParam, lParam)
  Shared hButton1
  
  Select Msg
    Case #WM_COMMAND
      If hButton1 = lParam
        MessageBox_(hWnd, "Button 1 clicked!", "", #MB_OK)
      EndIf
    Case #WM_CLOSE 
      DestroyWindow_(hWnd) 
    Case #WM_DESTROY 
      PostQuitMessage_(0) : Result  = 0 
    Default 
      Result  = DefWindowProc_(hWnd, Msg, wParam, lParam) 
  EndSelect 
  
  ProcedureReturn Result 
EndProcedure
To be continued...

PureBasic: Third-party Libraries

 
PureBasic open-source libraries repositary collection: http://pbosl.purearea.net/
 
Professional GUI library: http://www.progui.co.uk/
 
Also you can use any other DLL by importing it functions.
 
To be continued... I'll add more, much more PB libs... I bit later.

PureBasic: Community and Documentation

Forums

http://www.purebasic.fr/english/ (English)

http://forums.purebasic.com/german/ (German)

http://www.purebasic.fr/french/ (French)

http://purebasic.info/phpBB3ex/index.php (Russian)

http://www.cyberforum.ru/pure-basic/ (Russian)

http://purebasic.mybb.ru/ (Russian)

http://purebasic.ucoz.ru/forum (Russian)

http://www.purebasic.cn/forum.php (Chinese)

Documentation, tutorials, code samples

http://www.purebasic.com/documentation/

http://purearea.net/pb/CodeArchiv/CodeArchiv.html

http://pure-basic.narod.ru/ (Russian)

http://purebasic.info/Chapters/index.html (Russian)

http://purebasic.ucoz.ru/ (Russian)

http://mirashic.narod.ru/ (Russian)

http://purebasic.ucoz.com/ (Russian)

Alternative PB overview article

https://freeshell.de/~luis/purebasic/about/index.php

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
GeneralRe: My vote of 5 Pin
User-175147220-Nov-17 2:24
User-175147220-Nov-17 2:24 
QuestionMy vote of 5! Pin
jediYL22-Dec-14 19:53
professionaljediYL22-Dec-14 19:53 
AnswerRe: My vote of 5! Pin
User-175147220-Nov-17 2:25
User-175147220-Nov-17 2:25 
GeneralRe: My vote of 5! Pin
jediYL23-Jan-18 15:16
professionaljediYL23-Jan-18 15:16 
GeneralRe: My vote of 5! Pin
User-175147227-Apr-18 1:05
User-175147227-Apr-18 1:05 
QuestionWhy not use D language ? Pin
Alexandre Bencz21-Dec-14 23:25
Alexandre Bencz21-Dec-14 23:25 
AnswerRe: Why not use D language ? Pin
Emiliarge22-Dec-14 0:44
professionalEmiliarge22-Dec-14 0:44 
GeneralRe: Why not use D language ? Pin
Alexandre Bencz22-Dec-14 0:56
Alexandre Bencz22-Dec-14 0:56 
Hi Smile | :)

D language is developed by Walter Bright, he createad a new implementation of C++, it's a very, very good language to code...

Facebook use D language.

All you can do in C, C++ you can do in D

D for win32: http://wiki.dlang.org/D_for_W[^]
D for GTK+: http://www.gtk.org/language-bindings.php[^]
D API: http://dlang.org/library/[^]
D doc: http://dlang.org/ddoc.html[^]

D in facebook: http://forum.dlang.org/thread/l37h5s$2gd8$1@digitalmars.com[^]
http://www.wired.com/2014/07/d-programming-language/[^]

A good D language book: http://ddili.org/ders/d.en/[^]

Some projects in D: http://www.dsource.org/projects/[^]

>Yep. Therefore we need articles like it. We must determine our task and select PL that is more suitable for our purposes. Articles helping us with it.

Yes, u right!
My friend developed a new language, a SmallTalk based language, the name of this language is Elena, I love this language!

At now, it's just for win32, but in the next version of the language, the compiler will already be prepared to generate executables for Linux
https://sourceforge.net/projects/elenalang/[^]
GeneralRe: Why not use D language ? Pin
Emiliarge22-Dec-14 1:46
professionalEmiliarge22-Dec-14 1:46 
GeneralRe: Why not use D language ? Pin
Alexandre Bencz22-Dec-14 2:02
Alexandre Bencz22-Dec-14 2:02 
GeneralRe: Why not use D language ? Pin
Emiliarge22-Dec-14 2:22
professionalEmiliarge22-Dec-14 2:22 
GeneralRe: Why not use D language ? Pin
Alexandre Bencz22-Dec-14 2:37
Alexandre Bencz22-Dec-14 2:37 
AnswerRe: Why not use D language ? Pin
User-175147220-Nov-17 2:14
User-175147220-Nov-17 2:14 
GeneralRe: Why not use D language ? Pin
Alexandre Bencz20-Nov-17 7:57
Alexandre Bencz20-Nov-17 7:57 
GeneralRe: Why not use D language ? Pin
User-17514728-Dec-17 1:02
User-17514728-Dec-17 1:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.