The MASM Forum

General => The Campus => Topic started by: dc on June 09, 2017, 06:26:39 AM

Title: EndPaint placement
Post by: dc on June 09, 2017, 06:26:39 AM
if i put the EndPaint at the end of the paint msg handler, i cant paint anymore in later paint msg handling???
if i put it in destroy handler, painting gets slower and slower till it crashes...

msg==Create
LoadBmp's     ;     hBmp[n]...

msg==Paint
BeginPaint   ;    hdc
CreateCompatableDC    ;    hdc1
CreateCompatableDC    ;    hdc2
CreateCompatableBmp    ;   hbmp1
SelectObj hdc1, hbmp1
SelectObj hdc2, hbmp[n]
BitBlt hdc1, hdc2      ;      n times...
bitBlt hdc, hdc1
SelectObj DC's, old
DeleteObj hbmp1
DeleteDC    hdc1
DeleteDC    hdc2
EndPaint

msg==Destroy
DeleteObj's   hBmp[n]

is this the wrong order?

thanx.... been a while.... :)
Title: Re: EndPaint placement
Post by: dc on June 09, 2017, 06:33:28 AM
also the new windows update killed my help system, is there a fix for that?
Title: Re: EndPaint placement
Post by: jj2007 on June 09, 2017, 06:56:16 AM
Try the forum help (http://masm32.com/board/index.php?action=search;advanced;search=) - it's quite powerful. Same for the old forum. (http://www.masmforum.com/board/index.php?action=search;advanced)
Title: Re: EndPaint placement
Post by: jimg on June 09, 2017, 10:29:49 AM
http://masm32.com/board/index.php?topic=5201.msg55897#msg55897 (http://masm32.com/board/index.php?topic=5201.msg55897#msg55897) for old help system.
Title: Re: EndPaint placement
Post by: dc on June 10, 2017, 01:32:00 AM
http://masmforum.com/~masm32/board/Smileys/mforum/greenclp.gif
thanx, my world is nomalizing now
except - my main drawing routine still doesn't work
is beginpaint and endpaint not for main bitmap flipping?
thanx again
dc
Title: Re: EndPaint placement
Post by: jimg on June 10, 2017, 01:56:15 AM
Your code doesn't look like any kind of assembly language I am familiar with, so it's hard to say.  BeginPaint and EndPaint have parameters.  Your problem could be with any one of your statements.
Maybe give us some actual code to look at.
Title: Re: EndPaint placement
Post by: dc on June 10, 2017, 02:27:08 AM
ok, here's a little more detail:
Code: [Select]
WndProc Proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
LOCAL ps:PAINTSTRUCT
;====================================CREATE========================
    .If uMsg==WM_CREATE
        invoke LoadBmps
;=====================================PAINT==========================
    .ElseIf uMsg==WM_PAINT 
        invoke BeginPaint,hWnd,addr ps
        mov hdc, eax
        invoke CreateCompatibleDC, hdc
        mov hdcCH, eax
        invoke CreateCompatibleDC, hdc
        mov hdcP, eax
        invoke CreateCompatibleBitmap, hdc, wX, wY
        mov hBmpP, eax
        invoke SelectObject, hdcP, hBmpP
        mov oldP, eax
        invoke SelectObject, hdcCH, hBmps[0]
        mov oldCH, eax
;----------------------------start up screen fill--------
        .if !sFlag ; stretchBlt's to memDC and
            invoke NewBoard ; bitblt to windowDC
;------------------------------------flop--------------------------
        .elseif flop
            invoke Flop, hWnd ; stretchBlt's to windowDC
        .endif
;---------------------------------clean up-----------------------
        invoke EndPaint,hWnd,addr ps
        invoke SelectObject, hdcCH, oldCH
        invoke DeleteDC, hdcCH
        invoke SelectObject, hdcP, oldP
        invoke DeleteDC, hdcP
        invoke DeleteObject, hBmpP
;==========================================MOUSE CLICK======================================
    .ElseIf uMsg==WM_LBUTTONUP
        mov eax, lParam
        mov loWd, ax    ;   x
        shr eax, 16     ;   hiWd    y
        div sXY
        mov nsY, eax    ;   current sqare Y
        mul twentyfour
        mov temp, eax
        mov ax, loWd
        div sXY
        mov nsX, eax    ;   current square X
        add eax, temp   ;   new current index (XY)
        .if nsX !=0 && nsX !=23 && nsY !=0 && nsY !=17   ;   on edge?
            mov edx, eax
            shl edx, 2
            mov eax, brd[edx]
            and eax, 1
            jne @F
            mov brd[edx], 1
            m2m flip.hBMP, hBmps[00100b]
            jmp dun
    @@:     mov brd[edx], 0
            m2m flip.hBMP, hBmps[00b]
    dun:    mov eax, nsY
            mul sXY
            mov flip.Y, eax
            mov eax, nsX
            mul sXY
            mov flip.X, eax
            mov eax, temp
            mov eax, brd[eax]
            mov flop, 1     ;   flag tells PAINT to change a square
            invoke SendMessage, hWnd, WM_PAINT, 0, 0    ;   trigger PAINT
        .endif
;==============================================DESTROY===================================
    .ElseIf uMsg==WM_DESTROY
        invoke DeleteObject,hBmpe00
        invoke DeleteObject,hBmpe01
        invoke DeleteObject,hBmpe10
        invoke DeleteObject,hBmpe11
        invoke DeleteObject,hBmpc1
        invoke DeleteObject,hBmpc2
        invoke DeleteObject,hBmpc3
        invoke DeleteObject,hBmpc4
        mov ebx, 0
        .while ebx < 400h
        .if hBmps[ebx]
            invoke DeleteObject,hBmps[ebx]
        .endif
            add ebx, 4
        .endw
       
        invoke PostQuitMessage,NULL
Title: Re: EndPaint placement
Post by: jimg on June 10, 2017, 11:16:35 AM
I don't see anything obvious, but then I haven't done any of this type of stuff for many years.  Perhaps someone else will have an idea.
Title: Re: EndPaint placement
Post by: aw27 on June 10, 2017, 02:43:57 PM
ok, here's a little more detail:
It is the best approach, just on a need to know base, otherwise someone can steal our hard work  :lol:
Title: Re: EndPaint placement
Post by: dc on June 10, 2017, 02:53:41 PM
once the endPaint happens, i cant paint anymore????
if I put it in the destroy it works till it crashes, slower and slower tho
Title: Re: EndPaint placement
Post by: hutch-- on June 10, 2017, 06:28:20 PM
Time to learn your basic architecture, it has worked the same way since Win3.?

    .elseif uMsg == WM_PAINT
        invoke BeginPaint,hWin,ADDR Ps
          mov hDC, eax
        ; ----------------------------------------
          ; do what you need to do here.

          ;;;; invoke Paint_Proc,hWin,hDC


        ; ----------------------------------------
        invoke EndPaint,hWin,ADDR Ps
        return 0
Title: Re: EndPaint placement
Post by: jj2007 on June 10, 2017, 06:38:45 PM
Strange, Hutch, what you posted looks almost exactly like that paragraph in the Petzold book ::)
Title: Re: EndPaint placement
Post by: caballero on June 11, 2017, 01:40:15 AM
It is not strictelly necessary to paint inside WM_PAINT, but it is the standard way, and you can meet some inconveniences if you don't do in this way. In OSs before W7 the Client Area would be automatically cleaned (invalidated) when you minimize or overlap your window with other, for example.

The example attached runs ok in my W7 64 bits, but who knows in another one ??

Here is (http://www.abreojosensamblador.net/Productos/AOW32_/html/Pags/Cap03.html#Desktop) another example that paints over the desktop with no callback WndProc at all in the program, to exit from it you would open the task manager and kill the process if you run it, BE WARNED named something like "CalW?01".
Title: Re: EndPaint placement
Post by: hutch-- on June 11, 2017, 02:38:24 AM
 :biggrin:

> Strange, Hutch, what you posted looks almost exactly like that paragraph in the Petzold book

He probably got it from the same place that I did, the original SDK help file for WM_PAINT and BeginPaint and EndPaint. Many things in software are creative but OS functions are not, either do it the right way or it won't work properly. Still, you can't tell 'em so let it explode in their face so they learn the hard way.  :badgrin:
Title: Re: EndPaint placement
Post by: dc on June 12, 2017, 05:57:56 AM
in this code, it goes through the code but after the first draw, it wont anymore
thanx again,
dc
Title: Re: EndPaint placement
Post by: qWord on June 12, 2017, 06:59:58 AM
The rectangle passed to InvalidRect is not initialized. Also, never send WM_PAINT yourself - it's an message intended to be send by windows components to your code and not vice versa. You might also remove the UpdateWindow call and let window decided when to redraw.

BTW: you can pass a Null pointer to InvalidateRect to invalidate the whole client area
BTW2: forcing the background to be erased makes no sense for you application, because you draw the entire client area yourself
Title: Re: EndPaint placement
Post by: dc on June 12, 2017, 07:24:08 AM
so this doesn't initialize the rectangle?

    .if uMsg==WM_CREATE
        mov tangle.top, 0
        mov tangle.left, 0
        mov tangle.right, 500
        mov tangle.bottom, 500
Title: Re: EndPaint placement
Post by: hutch-- on June 12, 2017, 07:26:30 AM
OK,

I have put the line,

        invoke EndPaint, hWnd, addr ps

at the end of the WM_PAINT where it should be, manually set the main window so it was not clipping the edges of your display and it shows a 10 x 10 set of zero images in the client area.
I commented out the line,

            ; dxd sFlag

as I could not see what it was useful for.

You have the mouse cursor set to a "+" symbol but there is no code written for acting on mouse movements or mouse click events so I don't know what you are trying to do.
Title: Re: EndPaint placement
Post by: qWord on June 12, 2017, 07:36:39 AM
so this doesn't initialize the rectangle?
tangle is a local variable and thus created on the stack each time the WndProc is called. Put the definition in the .data?-section to make it static.
Title: Re: EndPaint placement
Post by: hutch-- on June 12, 2017, 07:57:36 AM
This may be useful in the WM_LBUTTONUP processing.


  ; at the top
    LOCAL hiwd  :DWORD
    LOCAL lowd  :DWORD


    .elseif uMsg==WM_LBUTTONUP
 ;         invoke InvalidateRect, hWnd, addr tangle, TRUE
 ;         invoke UpdateWindow, hWnd
 ;         invoke SendMessage, hWnd, WM_PAINT, 0, 0

        mov eax, lParam
        movzx ecx, ax
        mov lowd, ecx
        rol eax, 16
        movzx ecx, ax
        mov hiwd, ecx

        invoke SetWindowText,hWnd,str$(hiwd)
Title: Re: EndPaint placement
Post by: dc on June 12, 2017, 11:06:45 AM
no fixes yet, im trying to flip bitmaps in the main window
ive been able to with the endpaint function in the wrong place but it makes a bad memory leak
Title: Re: EndPaint placement
Post by: qWord on June 12, 2017, 11:19:51 AM
Here a working version of your game.asm with some simple extensions:
Code: [Select]
.386
.MODEL FLAT,STDCALL
OPTION CASEMAP:NONE
Include Game.inc
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 hInst
    POP wc.hInstance
    mov wc.hbrBackground, COLOR_MENU
    mov wc.lpszMenuName, NULL
    mov wc.lpszClassName, Offset ClassName
    invoke LoadIcon, hInstance, offset IDI_ICON
    mov wc.hIcon,EAX
    mov wc.hIconSm,0
    Invoke LoadCursor, NULL, IDC_CROSS
    mov wc.hCursor,EAX
    Invoke RegisterClassEx, addr wc
    Invoke CreateWindowEx, WS_EX_OVERLAPPEDWINDOW, addr ClassName, addr szAppName,\
                        WS_SYSMENU, 400, 200, 500, 500, NULL, NULL, hInst, NULL
    mov hwnd, eax
    Invoke ShowWindow, hwnd, SW_SHOWNORMAL
    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
WndProc Proc uses esi hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
LOCAL ps:PAINTSTRUCT
LOCAL rect:RECT,wndrect:RECT

    .data?
       
        squares BYTE 10*10 dup (?)
       
    .code

    .if uMsg==WM_CREATE
       
        ;
        ; resize window to get 500x500 client area
        ;
        invoke GetWindowRect,hWnd,addr wndrect
        invoke GetClientRect,hWnd,addr rect
        mov ecx,500
        mov edx,ecx
        add ecx,rect.left
        add edx,rect.top
        sub ecx,rect.right
        sub edx,rect.bottom
        sub ecx,wndrect.left
        sub edx,wndrect.top
        add ecx,wndrect.right
        add edx,wndrect.bottom
        invoke SetWindowPos,hWnd, NULL,0,0,ecx,edx,SWP_NOMOVE or SWP_NOZORDER
       
        invoke LoadBitmap,hInstance,IDB_1
        mov hBmp1,eax
        invoke LoadBitmap,hInstance,IDB_0
        mov hBmp0,eax
       
    .elseIf uMsg==WM_PAINT
        invoke BeginPaint, hWnd, addr ps
        mov hdc, eax
        invoke CreateCompatibleDC, hdc
        mov hdcCH, eax
        invoke CreateCompatibleDC, hdc
        mov hdcP, eax
        invoke CreateCompatibleBitmap, hdc, 500, 500
        mov hBmpP, eax
        invoke SelectObject, hdcP, hBmpP
        mov oldP, eax

        ; esi = index in array 'squares'
        xor esi,esi
        mov oldCH, esi
        mov sX, esi
        mov sY, esi
       
        ; for each row
        .while sY<500
           
            ; for each column in current row
            .while sX<500
               
                ; select bitmap for current square
                .if squares[esi]
                    invoke SelectObject, hdcCH, hBmp1
                .else
                    invoke SelectObject, hdcCH, hBmp0
                .endif
               
                ; save handle of default bitmap for first selection only
                .if !oldCH
                    mov oldCH,eax
                .endif
                               
                invoke BitBlt, hdcP, sX, sY, 50, 50, hdcCH, 0, 0, SRCCOPY
               
                ; next column
                add sX, 50
               
                ; update index
                add esi,1
            .endw
           
            ; next row
            add sY, 50
           
            ; reset to column 0
            mov sX, 0
        .endw
       
        invoke BitBlt, hdc, 0, 0, 500, 500, hdcP, 0, 0, SRCCOPY
       
        invoke SelectObject, hdcCH, oldCH
        invoke DeleteDC, hdcCH
        invoke SelectObject, hdcP, oldP
        invoke DeleteDC, hdcP
        invoke DeleteObject, hBmpP
       
        invoke EndPaint, hWnd, addr ps
       
    .elseif uMsg==WM_LBUTTONUP
       
        ; get x-coord.
        xor edx,edx
        movsx eax,WORD ptr lParam
        mov ecx,50
        idiv ecx
        push eax
       
        ; get y-coord.
        xor edx,edx
        movsx eax,WORD ptr lParam[2]
        idiv ecx
       
        ; index = x + y * 10
        pop edx
        imul eax,eax,10
        add eax,edx
       
        ; if index valid
        .if eax < LENGTHOF squares
           
            ; toggle state
            xor squares[eax],1
           
            ; force redraw of client area
            invoke InvalidateRect, hWnd, 0,FALSE
        .endif
       
    .elseif uMsg==WM_DESTROY
        invoke DeleteObject,hBmp0
        invoke DeleteObject,hBmp1
       
        invoke PostQuitMessage,NULL
    .else
        Invoke DefWindowProc, hWnd, uMsg, wParam, lParam
        ret
    .endif
    xor eax, eax
    ret
WndProc EndP

End Start
Title: Re: EndPaint placement
Post by: dc on June 12, 2017, 11:52:01 AM
right on, i still dont see what is making the difference but ill find it, thanx, sure seems like it would be more strait forward.
i probly just got a wrong variable somewhere or something :)
Title: Re: EndPaint placement
Post by: qWord on June 12, 2017, 11:58:33 AM
right on, i still dont see what is making the difference
The important difference is the InvalidatRect-call and the removed SendMessage(WM_PAINT). Applying this changes to your previously uploaded code make it work.
Title: Re: EndPaint placement
Post by: dc on June 12, 2017, 12:22:43 PM
sweet success, thanx everyone, ill post the finished project when its done, dont hold your breath, as i am a working man with grand kids :)
Title: Re: EndPaint placement
Post by: dc on September 28, 2017, 02:07:11 AM
so far what i have... wondering how much figuring out should be the game and splainin i should do up front
Title: Re: EndPaint placement
Post by: dc on October 06, 2017, 04:17:34 AM
this version is a bit closer to what i had in mind
Title: Re: EndPaint placement
Post by: jj2007 on October 06, 2017, 06:07:39 AM
You ran out of mojo??  ::)

It looks very impressing, but how does it work?
Title: Re: EndPaint placement
Post by: dc on October 06, 2017, 08:21:20 AM
i am seeing it needs quite a bit of explaining, also im noticing that people dont really care much for it, thinkin of moving on, make crystal clusters by changing the maze into little diamond patterns, direct the spark to pass by one to make it green and you get mojo, turn all 4 green to explode the cluster which changes the maze slightly, then secondary explosions from new clusters created by the explosions explode, 3 or more secondary ones result in the spark going quantum leaving behind residual sparks which are worth more mojo and points, if you cross the threshold amount of mojo, the speed increases and all the clusters explode and change the maze.... when the spark is trapped in a small loop, you lose mojo quickly - the smaller the loop the faster you lose mojo....
Title: Re: EndPaint placement
Post by: jj2007 on October 06, 2017, 09:08:44 AM
i am seeing it needs quite a bit of explaining
yes

Quote
make crystal clusters by changing the maze into little diamond patterns, direct the spark
how? special key?
Title: Re: EndPaint placement
Post by: HSE on October 06, 2017, 11:05:45 AM
I dc!

In this system there is a crash!
Code: [Select]
CPU Disasm
Address   Hex dump          Command                                  Comments
00402E83  |.  33C0          XOR EAX,EAX
00402E85  |.  66:A1 AE58400 MOV AX,WORD PTR DS:[4058AE]
00402E8B  |.  2B05 205E4000 SUB EAX,DWORD PTR DS:[405E20]
00402E91  |.  F735 D0584000 DIV DWORD PTR DS:[4058D0]  <--------------- CRASH
00402E97  |.  A3 BC584000   MOV DWORD PTR DS:[4058BC],EAX
00402E9C  |.  F725 FC5E4000 MUL DWORD PTR DS:[405EFC]
00402EA2  |.  8945 BC       MOV DWORD PTR SS:[LOCAL.17],EAX
After "xor eax, eax", you can write "xor edx, edx"
Title: Re: EndPaint placement
Post by: dc on October 06, 2017, 12:25:06 PM
bet you a dollar thats sposed to be edx's :icon_redface:
Title: Re: EndPaint placement
Post by: dc on October 06, 2017, 12:27:59 PM
click the maze section and it changes the maze
Title: Re: EndPaint placement
Post by: dc on October 06, 2017, 01:11:23 PM
i fixed one xor eax blah blah blah...
first one i found ....
Title: Re: EndPaint placement
Post by: HSE on October 06, 2017, 11:57:06 PM
bet you a dollar thats sposed to be edx's :icon_redface:

 "div" work with "edx:eax" pair.

Very nice.  :t
Title: Re: EndPaint placement
Post by: jj2007 on October 07, 2017, 12:25:51 AM
"div" work with "edx:eax" pair.

Right. And to make it work, there is a dedicated instruction: cdq

After "xor eax, eax", you can write "xor edx, edx"
Title: Re: EndPaint placement
Post by: HSE on October 07, 2017, 12:38:27 AM
Right. And to make it work, there is a dedicated instruction: cdq
:t
Title: Re: EndPaint placement
Post by: dc on October 07, 2017, 04:03:28 AM
i like that
Title: Re: EndPaint placement
Post by: K_F on October 14, 2017, 11:14:43 PM
Maybe here ??

Code: [Select]
.if !sFlag