News:

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

Proper Real-Time Event Loop

Started by grofaz, September 10, 2008, 06:55:33 AM

Previous topic - Next topic

grofaz

Would some kind soul please show me an example of a properly functioning real-time main event loop using:

invoke PeekMessage, ADDR msg, 0, 0, PM_REMOVE

instead of the standard GetMessage variety.

Thanks!

greg



MichaelW

This example does a scatter plot of the values returned by nrandom.

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
      hInstance dd    0
      hDlg      dd    0
      hdc       dd    0
      msg       MSG   <>
      rc        RECT  <>
    .code
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

DialogProc proc hwndDlg:DWORD, uMsg:DWORD, wParam:DWORD, lParam:DWORD

    SWITCH uMsg

      CASE WM_CTLCOLORDLG

        invoke GetStockObject, WHITE_BRUSH
        ret

      CASE WM_SIZE

        invoke InvalidateRgn, hwndDlg, NULL, TRUE

      CASE WM_CLOSE

        invoke DestroyWindow, hwndDlg

      CASE WM_DESTROY

        invoke PostQuitMessage, NULL

    ENDSW

    xor eax, eax
    ret

DialogProc endp

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    invoke GetModuleHandle, NULL
    mov hInstance, eax

    Dialog "Test", \
           "FixedSys", 11, \
            WS_VISIBLE or WS_OVERLAPPEDWINDOW or \
            DS_CENTER, \
            0, \
            0,0,100,75, \
            1024

    CallModelessDialog hInstance, 0, DialogProc, NULL
    mov hDlg, eax

    invoke GetDC, hDlg
    mov   hdc, eax

    invoke Sleep, 1000

  msgLoop:

    invoke PeekMessage, ADDR msg, NULL, 0, 0, PM_REMOVE
    .IF msg.message != WM_QUIT
      .IF eax != 0
        invoke TranslateMessage, ADDR msg
        invoke DispatchMessage, ADDR msg
      .ELSE
        invoke GetClientRect, hDlg, ADDR rc
        invoke nrandom, rc.right
        mov ebx, eax
        invoke nrandom, rc.bottom
        mov esi, eax
        invoke nrandom, 1 SHL 24
        mov edi, eax       
        invoke SetPixel, hdc, ebx, esi, edi
      .ENDIF
      jmp msgLoop
    .ENDIF

    invoke ExitProcess, eax

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start

eschew obfuscation

grofaz

Hey that's great, thanks for this example. Help's out a lot!

greg

dsouza123

MichaelW

   I couldn't get your example to assemble until the CallModelessDialog macro
was replaced with the body of the macro and the appropriate parameter
substitution with the actual variables.
  Is the program supposed to show an empty white area which is replaced by
what looks like constanly changing colored snow on a TV without a signal ?


    ; CallModelessDialog hInstance, 0, DialogProc, NULL

    invoke CreateDialogIndirectParam, hInstance,esi,0,ADDR DialogProc,NULL
    push eax
    invoke GlobalFree,esi
    pop eax
    pop edi
    pop esi

MichaelW

It assembles fine for me, using MASM32 version 10. It is supposed to display a dialog with no controls and a white client area, and after a one-second delay a constantly changing pattern of randomly positioned and randomly colored pixels, which now that you mention it does look like colored snow on a TV without a signal.
eschew obfuscation

dsouza123

Ah, MASM32 version 10 explains it.
Also thanks for the quick reply and for describing what the output should be.