flat assembler
Message board for the users of flat assembler.

Index > Tutorials and Examples > Simplest way for graphics prototyping with FreshLib

Author
Thread Post new topic Reply to topic
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 09 Dec 2016, 10:44
It was easy in the DOS days. The video memory was accessible from the user programs. Graphics programming in assembly was easy and fun.

That is why even now, the most examples for the beginners are written for DOS.

The modern OSes provide too complex API for graphics that is pretty hard for the beginners to learn.

FreshLib provides very simple way to make graphics, very close to the old DOS approach. One big array of pixels and your assembly program that to fill them up.

Here is a very simple example that illustrates how simple is to make graphics routines in FreshLib. The same code compiles for Linux and Windows:

Code:
include "%lib%/freshlib.inc"

@BinaryType GUI, compact

include "%lib%/freshlib.asm"


frmMainForm:
        ObjTemplate  tfEnd, TForm, frmMain, \
                     x = 100,        \
                     y = 50,         \
                     width = 320,    \
                     height = 240,   \
                     Visible = TRUE, \
                     OnPaint = OnFormPaint,     \
                     Caption = 'Easy drawing with FreshLib'


start:
        InitializeAll

        create  [pApplication], TApplication
        stdcall GUI.Init
        stdcall CreateFromTemplate, frmMainForm, 0
        set     [pApplication], TApplication:MainWindow, frmMain

        stdcall Run

        FinalizeAll
        stdcall TerminateAll, 0



Colors dd $ff584435, $ffBD9D86

proc OnFormPaint, .self, .canvas
begin

        mov     esi, [.canvas]
        mov     edi, [esi+TImage.pPixels]

        xor     edx, edx  ; Y coordinate

.loop_y:

        xor     ecx, ecx  ; X coordinate

.loop_x:

        mov     eax, ecx
        xor     eax, edx
        shr     eax, 5    ; 2^5 = 32px one field size.
        and     eax, 1    ; dark or light?

        mov     eax, [Colors+4*eax]

        stosd

        inc     ecx
        cmp     ecx, [esi+TImage.width]
        jl      .loop_x

        inc     edx
        cmp     edx, [esi+TImage.height]
        jl      .loop_y

        return
endp    

_________________
Tox ID: 48C0321ADDB2FE5F644BB5E3D58B0D58C35E5BCBC81D7CD333633FEDF1047914A534256478D9


Last edited by JohnFound on 09 Dec 2016, 12:18; edited 1 time in total
Post 09 Dec 2016, 10:44
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
avcaballero



Joined: 02 Feb 2004
Posts: 203
Location: Madrid - Spain
avcaballero 09 Dec 2016, 11:09
Interesting. What is .canvas and TImage? I guess that you paint on the HDC of the client area.
Post 09 Dec 2016, 11:09
View user's profile Send private message Visit poster's website Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 09 Dec 2016, 11:33
.canvas is an argument of the OnPaint event handler, that contains pointer to the canvas TImage of the window that have to be drawn.

TImage is in memory bitmap, defined following way:
Code:
struct TImage
  .width   dd ?  ; width in pixels.
  .height  dd ?  ; height in pixels.
  .pPixels dd ?  ; pointer to the pixel memory.

; follows os dependent data
ends    


For the user only the first 3 fields are useful, because the following are OS dependent fields. For example for Linux TImage is defined this way: Source code

The .pPixels member of TImage contains pointer to the pixels array. Every pixel is always 32bit ARBG value, using premultiplied alpha.
Post 09 Dec 2016, 11:33
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 09 Dec 2016, 12:18
I have added the missed attachment from the first post.
Post 09 Dec 2016, 12:18
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 20 Dec 2016, 07:54
This example, with some additional comments has been added to the Fresh IDE examples/ directory in v2.5.1
Post 20 Dec 2016, 07:54
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.