News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Invoke with "byref" parameters

Started by italogomes, April 02, 2011, 09:12:34 PM

Previous topic - Next topic

italogomes

Hello everyone! It's been a looong time since I last developed in assembly, so I'm a little lost in the new masm32 language features, although they are fascinating for a old-timer like myself.

My first attempt was to make a windows application and paint some background. While doing it in the "old way", it worked. But when I tried creating a procedure with parameters, I did not have success. It would be really great to use this feature as it makes the code a lot cleaner and easier to maintain.

Here is the relevant code:


(...)
BMP_FUNDO equ 800
LARGURA_JANELA equ 640
ALTURA_JANELA equ 480

.DATA?
hInstance HINSTANCE ?
hFundoDC HDC ?
hFundo HBITMAP ?
(...)

.CODE
(...)
InicializarBitmap proc uses eax edi, lpBitmapId: DWORD, lpDC: ptr HDC, lpHBitmap: ptr HBITMAP
invoke CreateCompatibleDC, NULL
mov edi, lpDC
mov [edi], eax

invoke LoadBitmap, hInstance, lpBitmapId
mov edi, lpHBitmap
mov [edi], eax
   
ret
InicializarBitmap endp

PintarFundo proc uses eax, hWnd: HWND
local paintStruct: PAINTSTRUCT
local hDC: HDC

invoke BeginPaint, hWnd, addr paintStruct
mov hDC, eax
invoke BitBlt, hDC, 0, 0, LARGURA_JANELA, ALTURA_JANELA, hFundoDC, 0, 0, SRCCOPY
invoke EndPaint, hWnd, addr paintStruct
       
ret
PintarFundo endp
(...)

invoke InicializarBitmap, BMP_FUNDO, addr hFundoDC, addr hFundo ; <-- called in WM_CREATE message handler
invoke PintarFundo, hWnd ; <-- called in WM_PAINT message handler


What could I possibly doing wrong?

Thank you all for any help. And sorry for my english mistakes; it's not my natural language.

jj2007

Hi Italo,

Welcome to the Forum :thumbu

Since you did not post a complete example, I cannot test it, of course. It looks alright at first sight. What is the problem? Error messages? No bitmap drawn??

You pass as lpBitmapId the value 800 - certainly not a pointer. Is it one of the OBM_ constants?

italogomes

Quote from: jj2007 on April 02, 2011, 09:23:22 PM
Hi Italo,

Welcome to the Forum :thumbu

Since you did not post a complete example, I cannot test it, of course. It looks alright at first sight. What is the problem? Error messages? No bitmap drawn??

You pass as lpBitmapId the value 800 - certainly not a pointer. Is it one of the OBM_ constants?

Hello, jj. Thank you for the welcome. I noticed this is a very friendly community and I'm glad I could join it.

Sorry for not posting everything. I'm sending the complete source now.

So, the problem is that no bitmap is painted. And the value 800 is the bitmap resource ID.


.486
.model flat, stdcall
;
; Liga a opção de case-sensitive.
option caseMap: none
;
; Includes usados pelo jogo.
include \masm32\include\gdi32.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
include \masm32\include\windows.inc
;
; Libs usadas pelo jogo.
includelib \masm32\lib\gdi32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
;
; Constantes.
LARGURA_JANELA equ 640
ALTURA_JANELA equ 480
;
; Identificadores dentro do arquivo de recursos.
BMP_FUNDO equ 800
BMP_NAVE equ 801
BMP_MASCARA_NAVE equ 802



.DATA
    ;
    ; Nome da classe da janela principal da aplicação.
    className db "StarTrekoWinClass", 0
    ;
    ; Nome da aplicação.
    appName db "Star Treko!", 0



.DATA?
;
; Handle da instância da aplicação.
hInstance HINSTANCE ?
;
; Handle do device-context e do bitmap para a imagem de fundo.
hFundoDC HDC ?
hFundo HBITMAP ?


   
.CODE
    WinMain proc, hInst: HINSTANCE, hPrevInst: HINSTANCE, CmdLine: LPSTR, CmdShow: DWORD
        local wc: WNDCLASSEX
        local msg: MSG
        local hWnd: HWND

        mov wc.cbSize, SIZEOF WNDCLASSEX
        mov wc.style, CS_HREDRAW or CS_VREDRAW
        mov wc.lpfnWndProc, offset WndProc
        mov wc.cbClsExtra, NULL
        mov wc.cbWndExtra, NULL
        push hInstance
        pop wc.hInstance
        mov wc.hbrBackground, COLOR_WINDOW + 1
        mov wc.lpszMenuName, NULL
        mov wc.lpszClassName, offset className
        invoke LoadIcon, NULL, IDI_APPLICATION
        mov wc.hIcon, eax
        mov wc.hIconSm, eax
        invoke LoadCursor, NULL, IDC_ARROW
        mov wc.hCursor, eax
        invoke RegisterClassEx, addr wc
        invoke CreateWindowEx, NULL, addr className, addr appName, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, LARGURA_JANELA, ALTURA_JANELA, NULL, NULL, hInst, NULL
        mov hWnd, eax
        invoke ShowWindow, hWnd, CmdShow
        invoke UpdateWindow, hWnd

        .WHILE TRUE
            invoke GetMessage, addr msg, NULL, 0, 0
            .BREAK .IF (!eax)
            invoke TranslateMessage, addr msg
            invoke DispatchMessage, addr msg
        .ENDW
       
        mov eax, msg.wParam
        ret
    WinMain endp



    PintarFundo proc uses eax, hWnd: HWND
        local paintStruct: PAINTSTRUCT
        local hDC: HDC

        invoke BeginPaint, hWnd, addr paintStruct
        mov hDC, eax
        invoke BitBlt, hDC, 0, 0, LARGURA_JANELA, ALTURA_JANELA, hFundoDC, 0, 0, SRCCOPY
        invoke EndPaint, hWnd, addr paintStruct
       
        ret
    PintarFundo endp
   
   
   
    InicializarBitmap proc uses eax edi, lpBitmapId: DWORD, lpDC: ptr HDC, lpHBitmap: ptr HBITMAP
invoke CreateCompatibleDC, NULL
mov edi, lpDC
mov [edi], eax

    invoke LoadBitmap, hInstance, lpBitmapId
mov edi, lpHBitmap
    mov [edi], eax
   
ret
    InicializarBitmap endp

   
   
    InicializarRecursos proc, hWnd: HWND
    invoke InicializarBitmap, BMP_FUNDO, addr hFundoDC, addr hFundo
ret
    InicializarRecursos endp



    FinalizarRecursos proc
invoke DeleteDC, hFundoDC
ret
    FinalizarRecursos endp
   
   

    WndProc proc, hWnd: HWND, uMsg: UINT, wParam: WPARAM, lParam: LPARAM
        .IF uMsg == WM_CREATE
    invoke InicializarRecursos, hWnd
        .ELSEIF uMsg == WM_DESTROY
            invoke FinalizarRecursos
            invoke PostQuitMessage, NULL
        .ELSEIF uMsg == WM_PAINT
            invoke PintarFundo, hWnd
        .ELSE
            invoke DefWindowProc, hWnd, uMsg, wParam, lParam
            ret
        .ENDIF
       
        xor eax, eax
        ret
    WndProc endp



    Main proc
        invoke GetModuleHandle, NULL
        mov hInstance, eax
        invoke WinMain, hInstance, NULL, NULL, SW_SHOWDEFAULT
        invoke ExitProcess, eax
        Main endp

end Main

italogomes

This is the resource file content:


#define BMP_FUNDO 800
#define BMP_NAVE 801
#define BMP_MASCARA_NAVE 802

BMP_FUNDO BITMAP DISCARDABLE "fundo.bmp"
BMP_NAVE BITMAP DISCARDABLE "nave.bmp"
BMP_MASCARA_NAVE BITMAP DISCARDABLE "mascaraNave.bmp"


As you can see, I will have many other bitmaps. That's why I tried to create the procedure to modularize the code.

Thank you again for your help.

drizz

Looks like you are missing a SelectObject call.
The truth cannot be learned ... it can only be recognized.

italogomes

Quote from: drizz on April 02, 2011, 10:30:37 PM
Looks like you are missing a SelectObject call.

Oh my god... how could I miss that?!  :eek That explains why it was working before I tried the parameters' version...

Thank you, drizz. I'm back to development again now!

:bg

dedndave

this looked like fun, so i did a little cleanup and added prototypes
also, i use AdjustWindowRect to set the window size according to the desired client size

http://www.4shared.com/file/Ncg6BuQ-/StarTreko.html

jj2007

Dave, my friend, AVIRA reports that you want to smuggle the TR/ATRAPS.Gen2 trojan on my system :naughty:

dedndave


italogomes

Quote from: dedndave on April 03, 2011, 02:06:39 AM
this looked like fun, so i did a little cleanup and added prototypes
also, i use AdjustWindowRect to set the window size according to the desired client size

Thank you, Dave. Now that I have realized my so-silly-mistake about SelectObject, it's time to modularize a lot of things, like the code for sprite-sheet drawing.

And yes, it's being a LOT of fun to develop this project. It will be a very simple game for a child. So, what could be better for a vetern like myself than join assembly programming with the fun of a game?

You may have noticed that the name of the project is a parody of the Star Trek series.   :wink

Once again, thank everyone for stopping by and helping me.  :bg

dedndave

if you look at my file, i cleaned up some syntax on the PROC lines
you had some extraneous commas, and unnecessary USES EAX stuff, etc