flat assembler
Message board for the users of flat assembler.

Index > Windows > Displaying a pixel buffer

Author
Thread Post new topic Reply to topic
Mota



Joined: 29 Dec 2004
Posts: 22
Mota 29 Dec 2004, 19:36
I'm Currently working on a raytracer which I call GOD, Graphical Object Display, and I've been having problems with displaying my pixel buffer. My code is below, please help me!

Code:
format PE GUI 4.0
entry start

include '..\..\..\include\win32a.inc'
include '..\..\..\include\own\main.inc'

SCRWIDTH equ 680
SCRHEIGHT equ 480

section '.data' data readable writeable
     wHMain         dd   ?
     wHInstance     dd   ?
     hDC            dd   ?

     wTitle         db   'GOD',0 ;name of our window
     wClsName       db   'GODCLASS',0      ;name of our window class

     wMsg           MSG
     wCls           WNDCLASS
     rect           RECT

     bmi            BITMAPINFOHEADER
                    dd 0FF0000h      ; RED
                    dd 000FF00h      ; GREEN
                    dd 00000FFh      ; BLUE
                    dd 0000000h      ; BLACK

     buffer:        array SCRWIDTH array SCRHEIGHT dd 0

section '.code' code readable executable
     start:
          ; +------------------------------+
          ; | registering the window class |
          ; +------------------------------+
          invoke    GetModuleHandle,NULL
                    mov  [wHInstance],eax
                    mov  [wCls.hInstance],0;eax
                    mov  [wCls.style],CS_HREDRAW or CS_VREDRAW or CS_OWNDC
                    mov  [wCls.lpfnWndProc],window_procedure
                    mov  [wCls.lpszClassName],wClsName
                    mov  [wCls.hbrBackground],COLOR_WINDOW+1
          invoke    LoadIcon,NULL,IDI_APPLICATION
                    mov  [wCls.hIcon],eax
          invoke    LoadCursor,NULL,IDC_ARROW
                    mov  [wCls.hCursor],eax

                    mov  [bmi.biSize],40
                    mov  [bmi.biPlanes],1
                    mov  [bmi.biBitCount],32
                    mov  [bmi.biCompression],BI_BITFIELDS
                    mov  [bmi.biWidth],SCRWIDTH
                    mov  [bmi.biHeight],-SCRHEIGHT

                    ; ignore these
                    mov  [bmi.biSizeImage],0
                    mov  [bmi.biXPelsPerMeter],0
                    mov  [bmi.biYPelsPerMeter],0
                    mov  [bmi.biClrUsed],0
                    mov  [bmi.biClrImportant],0

                    mov  [rect.left],0
                    mov  [rect.top],0
                    mov  [rect.right],SCRWIDTH
                    mov  [rect.bottom],SCRHEIGHT
                    invoke AdjustWindowRect,rect,WS_POPUP or WS_SYSMENU or WS_CAPTION,0



          invoke    RegisterClass,wCls

          ; +--------------------------+
          ; | creating the main window |
          ; +--------------------------+
          invoke    CreateWindowEx,\
                         0,\
                         wClsName,\
                         wTitle,\
                         WS_OVERLAPPEDWINDOW xor WS_THICKFRAME xor WS_MAXIMIZEBOX,\
                         CW_USEDEFAULT,\
                         CW_USEDEFAULT,\
                         [rect.right],\
                         [rect.bottom],\
                         NULL,\
                         NULL,\
                         [wHInstance],\
                         NULL
                    mov  [wHMain],eax
          invoke    ShowWindow,[wHMain],SW_SHOW
          invoke    GetDC,[wHMain]
          mov       [hDC],eax

          ; +---------------------------+
          ; | entering the message loop |
          ; +---------------------------+
          window_message_loop_start:
               invoke    GetMessage,wMsg,NULL,0,0
                         or   eax,eax
                         je   window_message_loop_end
               invoke    TranslateMessage,wMsg
               invoke    DispatchMessage,wMsg
                         jmp  window_message_loop_start

          window_message_loop_end:
               invoke    ReleaseDC,[hDC],[wHMain]
               invoke    ExitProcess,0

          ; +----------------------+
          ; | the window procedure |
          ; +----------------------+
          proc window_procedure,hWnd,uMsg,wParam,lParam
               push ebx esi edi    ;eventhough the API would preserved, but play safe :p
               cmp  [uMsg],WM_DESTROY
               je   wmDESTROY

               cmp  [uMsg],WM_PAINT
               je   wmPAINT
               cmp  [uMsg],WM_KEYDOWN
               je   wmKEY

               wmDEFAULT:
                    invoke    DefWindowProc,[hWnd],[uMsg],[wParam],[lParam]
                              jmp  wmBYE
               wmPAINT:
                    invoke    StretchDIBits,[hDC], 0, 0, SCRWIDTH, SCRHEIGHT, 0, 0, SCRWIDTH, SCRHEIGHT, buffer, bmi, DIB_RGB_COLORS, SRCCOPY ; LOOK AT ME, I CAUSE HEADACHES
                              jmp  wmBYE
               wmKEY:
                              and [wParam],0FFh
                              cmp [wParam],27
                              jne wmBYE
               wmDESTROY:
                    invoke    PostQuitMessage,0

               wmBYE:
                    pop  edi esi ebx
                    return
          endp

section '.idata' import data readable writeable
     library   KERNEL32, 'KERNEL32.DLL',\
               USER32,   'USER32.DLL'

     import    KERNEL32,\
               GetModuleHandle,    'GetModuleHandleA',\
               ExitProcess,        'ExitProcess'

     import    USER32,\
               RegisterClass,      'RegisterClassA',\
               CreateWindowEx,     'CreateWindowExA',\
               DefWindowProc,      'DefWindowProcA',\
               ShowWindow,         'ShowWindow',\
               LoadCursor,         'LoadCursorA',\
               LoadIcon,           'LoadIconA',\
               GetMessage,         'GetMessageA',\
               TranslateMessage,   'TranslateMessage',\
               DispatchMessage,    'DispatchMessageA',\
               GetDC,              'GetDC',\
               ReleaseDC,          'ReleaseDC',\
               AdjustWindowRect,   'AdjustWindowRect',\
               PostQuitMessage,    'PostQuitMessage'

      include  "..\..\..\include\apia\gdi32.inc"
    


Thanks in advance


EDIT:

Array is defined as:
array equ times

DIB_RGB_COLORS and DIB_PAL_COlORS aren't defined in the includes that come with fasm, so I added thm there myself from C, DIB_RGB_COLORS is 0 and DIB_PAL_COLORS is 1
Post 29 Dec 2004, 19:36
View user's profile Send private message Reply with quote
Mota



Joined: 29 Dec 2004
Posts: 22
Mota 31 Dec 2004, 13:58
Please People, I really need your help. There must e a way to make it work!

If I uncomment 'invoke StretchDIBits, ...' it doesnt crash. Whats the problem? Please help.
Post 31 Dec 2004, 13:58
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 31 Dec 2004, 14:12
Mota wrote:
Please People, I really need your help. There must e a way to make it work!

If I uncomment 'invoke StretchDIBits, ...' it doesnt crash. Whats the problem? Please help.


Hm, I simply can't understand your problem... doesn't work is not an explanation and definately noone will try to guess what you have to explain...
Also, your code is uncompilable, because it depends on some other files and you are using "../../../some.asm" - beleave me, my directory structure is not as yours...
Simply prepare your homework and you will get all help you can carry. Very Happy

Regards and happy new year.
Post 31 Dec 2004, 14:12
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Mota



Joined: 29 Dec 2004
Posts: 22
Mota 31 Dec 2004, 17:36
'..\..\..\include\' is my include path. I dunno how to use environment variables, so maybe you people could help me there too Razz

'..\..\..\include\own\main.asm' should really be called main.inc. It contains a single definition:

array equ times


im trying to display a pixel buffer, called buffer, onto my window using StretchDIBits. Whenever the program reaches StretchDIBits, it crashes. There could be many reasons to this, but ive tried all I can think of and it still crashes.
Post 31 Dec 2004, 17:36
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 31 Dec 2004, 18:17
Mota wrote:
I dunno how to use environment variables, so maybe you people could help me there too Razz


You could use the search function of this forum. Razz
Try this: http://board.flatassembler.net/topic.php?t=805
OK, I will look at your sources...

Happy new year.
Post 31 Dec 2004, 18:17
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Mota



Joined: 29 Dec 2004
Posts: 22
Mota 01 Jan 2005, 17:24
Thanks, happy new year everyone
Post 01 Jan 2005, 17:24
View user's profile Send private message Reply with quote
JohnFound



Joined: 16 Jun 2003
Posts: 3499
Location: Bulgaria
JohnFound 03 Jan 2005, 00:30
Hi, Mota.
I fixed your sources. There was several problems. Also, I rewrote some fragments in more standard and safe way (also to use %fasminc% environment variable). Check sources - there are short comments for you.
And hey, don't use so big horizontal indents. Make your sources readable with vertical indents instead (only IMHO. Wink )
Code:
; Generally speaking this way of allocating bitmaps is not very good,
; better use HeapAlloc with reallocation on WM_SIZE
; Also, using only global labels for bigger programs may lead to
; problems with name space exhoust - you simply can't invent so many
; different names. Use local labels (with "." as first char) that will
; save you a lot of troubles when the project grows.
;

format PE GUI 4.0
entry start

include '%fasminc%\win32a.inc'

SCRWIDTH equ 680
SCRHEIGHT equ 480

section '.code' code readable executable

start:

; +------------------------------+
; | registering the window class |
; +------------------------------+
invoke    GetModuleHandle,NULL
          mov  [wHInstance],eax
          mov  [wCls.hInstance],0;eax
          mov  [wCls.style],CS_HREDRAW or CS_VREDRAW or CS_OWNDC
          mov  [wCls.lpfnWndProc],window_procedure
          mov  [wCls.lpszClassName],wClsName
          mov  [wCls.hbrBackground],COLOR_WINDOW+1
          invoke    LoadIcon,NULL,IDI_APPLICATION
          mov  [wCls.hIcon],eax
          invoke    LoadCursor,NULL,IDC_ARROW
          mov  [wCls.hCursor],eax


          mov  [bmi.biSize], sizeof.BITMAPINFOHEADER
          mov  [bmi.biWidth],SCRWIDTH
          mov  [bmi.biHeight],-SCRHEIGHT
          mov  [bmi.biPlanes],1
          mov  [bmi.biBitCount], 32
          mov  [bmi.biCompression], BI_RGB

          ; ignore these
          mov  [bmi.biSizeImage], 0
          mov  [bmi.biXPelsPerMeter],0
          mov  [bmi.biYPelsPerMeter],0
          mov  [bmi.biClrUsed],0
          mov  [bmi.biClrImportant],0

          mov  [rect.left],0
          mov  [rect.top],0
          mov  [rect.right],SCRWIDTH
          mov  [rect.bottom],SCRHEIGHT
          invoke AdjustWindowRect, rect, WS_POPUP or WS_SYSMENU or WS_CAPTION, 0

          invoke    RegisterClass,wCls

; +--------------------------+
; | creating the main window |
; +--------------------------+
        mov     ecx, [rect.bottom]
        mov     eax, [rect.right]
        sub     ecx, [rect.top]
        sub     eax, [rect.left]
        invoke  CreateWindowEx,\
                       0,\
                       wClsName,\
                       wTitle,\
                       WS_OVERLAPPEDWINDOW xor WS_THICKFRAME xor WS_MAXIMIZEBOX,\
                       0,\
                       0,\
                       eax,\
                       ecx,\
                       NULL,\
                       NULL,\
                       [wHInstance],\
                       NULL
                  mov  [wHMain],eax

        invoke    ShowWindow,[wHMain],SW_SHOW

; +---------------------------+
; | entering the message loop |
; +---------------------------+
window_message_loop_start:

        invoke  GetMessage,wMsg,NULL,0,0
        or      eax,eax
        je      window_message_loop_end

        invoke  TranslateMessage, wMsg
        invoke  DispatchMessage, wMsg

        jmp     window_message_loop_start

window_message_loop_end:
        invoke    ExitProcess, 0


; +----------------------+
; | the window procedure |
; +----------------------+
proc window_procedure, hWnd, uMsg, wParam, lParam

        push ebx esi edi    ;eventhough the API would preserved, but play safe :p

        cmp  [uMsg],WM_DESTROY
        je   wmDESTROY

        cmp  [uMsg],WM_PAINT
        je   wmPAINT

        cmp     [uMsg], WM_ERASEBKGND
        je      wmERASE

        cmp  [uMsg],WM_KEYDOWN
        je   wmKEY

  wmDEFAULT:
        invoke    DefWindowProc,[hWnd],[uMsg],[wParam],[lParam]
        jmp       wmFINISH

  wmPAINT:
; Never get DC for long. Use BeginPaint|EndPaint in WM_PAINT handlers.
        invoke  BeginPaint, [hWnd], paintstruct
        invoke  StretchDIBits, [paintstruct.hdc], 0, 0, SCRWIDTH, SCRHEIGHT, 0, 0, SCRWIDTH, SCRHEIGHT, buffer, bmi, 0, SRCCOPY
        invoke  EndPaint, [hWnd], paintstruct
        jmp     wmBYE

; Handle WM_ERASE and return TRUE - this will prevent flickering of the window on repaint.
  wmERASE:
        xor     eax, eax
        inc     eax
        jmp     wmFINISH

  wmKEY:
        and     [wParam],0FFh
        cmp     [wParam],27
        jne     wmBYE

  wmDESTROY:
        invoke  PostQuitMessage,0

wmBYE:
        xor  eax, eax

wmFINISH:
        pop  edi esi ebx
        return
endp


section '.data' data readable writeable
; Place your data after the code - especially for so big arrays.
; Place uninitialized data at the end of the section.

     wTitle         db   'GOD',0 ;name of our window
     wClsName       db   'GODCLASS',0      ;name of our window class

     wHMain         dd   ?
     wHInstance     dd   ?

     wMsg           MSG
     wCls           WNDCLASS
     rect           RECT

     paintstruct PAINTSTRUCT

     bmi            BITMAPINFOHEADER
     buffer         rd  SCRWIDTH * SCRHEIGHT    ; black DIB


section '.idata' import data readable writeable

; Import everything - FASM will select only API's you use in the program.
     library   kernel32, 'KERNEL32.DLL',\
               user32,   'USER32.DLL',  \
               gdi32, 'gdi32.dll'

      include  "%fasminc%\apia\kernel32.inc"
      include  "%fasminc%\apia\user32.inc"
      include  "%fasminc%\apia\gdi32.inc"
    


Regards.
Post 03 Jan 2005, 00:30
View user's profile Send private message Visit poster's website ICQ Number Reply with quote
Mota



Joined: 29 Dec 2004
Posts: 22
Mota 03 Jan 2005, 01:38
thanks!
I couldnt have done anything better!

the beginpaint and endpaint part is what bugs me, i need to spend the leas amount of time to paint a frame, and I want to be able to paint outside of WM_PAINT messages.

Anyway, thanks a bunch!
Post 03 Jan 2005, 01:38
View user's profile Send private message Reply with quote
vbVeryBeginner



Joined: 15 Aug 2004
Posts: 884
Location: \\world\asia\malaysia
vbVeryBeginner 03 Jan 2005, 04:53
Quote:

push ebx esi edi ;eventhough the API would preserved, but play safe :p


this sounds familiar :-p lol

Code:
mov  [wCls.hInstance],0;eax 

should be
mov [wCls.hInstance],eax
    
Post 03 Jan 2005, 04:53
View user's profile Send private message Visit poster's website Reply with quote
Mota



Joined: 29 Dec 2004
Posts: 22
Mota 04 Jan 2005, 17:50
Yes, it must sound familiar to you Razz
Those tutorials are really useful, you know Razz

the hInstance thing is because I saw it on a c++ implementation and was testing it.

Thanks JohnFound, vbVeryBegginer, for the help. I will forever be in your debt (sounds familiar too Smile)
Post 04 Jan 2005, 17:50
View user's profile Send private message 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.