News:

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

MASM WIN32 GDI Graphics Skeleton for Beginner's

Started by OceanJeff32, February 14, 2005, 02:14:58 AM

Previous topic - Next topic

OceanJeff32

This code is the framework for simple bitmap page flipping, where you modify the bitmap then flip it onto the windows device.  Let me know what you think, alternatives, anything really.  All input is welcome, and it's not optimized, etc.  Just basic and quickly put together.

Jeff, I tweaked the display by using the code /code tages in square brackets so your code displayed properly.  hutch--

:wink


;-----------------------------------------------------------------------------------------------
; Graphics Framework with Bitmap (Page) Drawing
; by Jeff Cummings
; cool website: http://www.ronybc.8k.com (very inspiring graphics)
; Uses SetDIBitsToDevice() to display a modified bitmap on-screen.
; Frame Counter using two threads, kind of cool.
; Draws a color to the screen that fills the entire bitmap.
; any recommendations, options, extras, add-ons are very much welcome.
; Please send to my email: jeffwebgamer@hotmail.com
;

.686p
.MMX
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\gdi32.inc
include \masm32\include\user32.inc

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\gdi32.lib
includelib \masm32\lib\user32.lib

.data
ClassName db "apocalypse",0
AppName   db "Jeff's SetDIBitsToDevice Frame Counter using two threads :-)",0,0,0,0,0,0
wwidth    dd 680               ; 1:1.618, The ratio of beauty ;)
wheight   dd 420               ; smaller the window faster the fires
maxx      dd 800               ; 123: values set on execution
maxy      dd 600               ; this thing is best for comparing
fcount    dd 0
stop    dd  0
seed    dd  0
bminf     BITMAPINFO <<40,0,0,1,32,0,0,0,0,0,0>>

.data?
hInstance HINSTANCE ?
hwnd      LPVOID ?
hmnu      HWND ?
wnddc     HDC ?
hFThread  HANDLE ?
hHeap     HANDLE ?
idThread1 DWORD ?
idThread2 DWORD ?
bitmap1   LPVOID ?
bitmap2   LPVOID ?
;hFShells  LPVOID ?
msg       MSG <>
wc        WNDCLASSEX <>

.code

random PROC base:DWORD         ; Park Miller random number algorithm
    mov eax, seed              ; from M32lib/nrand.asm
    xor edx, edx
    mov ecx, 127773
    div ecx
    mov ecx, eax
    mov eax, 16807
    mul edx
    mov edx, ecx
    mov ecx, eax
    mov eax, 2836
    mul edx
    sub ecx, eax
    xor edx, edx
    mov eax, ecx
    mov seed, ecx
    div base
    mov eax, edx
    ret ; returns random number in EAX from 0 to BASE (parameter)
random ENDP
; -------------------------------------------------------------------------

GraphicsThread:
    invoke SetThreadPriority,idThread1,THREAD_PRIORITY_HIGHEST
    invoke GetDC,hwnd
    mov wnddc,eax
    invoke GetProcessHeap
    mov hHeap,eax
    invoke HeapAlloc,hHeap,HEAP_ZERO_MEMORY,4194304
    mov bitmap1,eax

here:
    mov esi, bitmap1

    mov ecx, 1890000
    mov eax, 000FF00FFh ; it goes 00 alpha FF RED 00 GREEN FF BLUE (h for hexadecimal)
lbegin:
    mov [esi+ecx], eax ; fill bitmap with this color.
    sub ecx, 3
    loop lbegin

    invoke SetDIBitsToDevice,wnddc,0,0,maxx,maxy,\
           0,0,0,maxy,bitmap1,ADDR bminf,DIB_RGB_COLORS

    inc fcount                 ; count the frames
    jmp here


    invoke ReleaseDC,hwnd,wnddc
    invoke HeapFree,hHeap,0,bitmap1
    mov idThread1,-1
    invoke ExitThread,2003
    hlt                        ; ...! i8085 memories
; -------------------------------------------------------------------------
.data
fps  db 64 dup (0)
fmat db "fps = %u   (jeffwebgamer@hotmail.com)",0
.code

FrameThread:
    invoke Sleep,1000
    invoke wsprintf,ADDR fps,ADDR fmat,fcount
    invoke SetWindowText,hwnd,ADDR fps
    xor eax,eax
    mov fcount,eax
    mov eax,stop
    test eax,eax
    jz FrameThread
    mov idThread2,-1
    invoke ExitThread,2003
; -------------------------------------------------------------------------
WndProc PROC hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
    .IF uMsg==WM_CLOSE
        mov stop,1                  ; stop running threads
        invoke Sleep,100            ; avoid FireThread drawing without window
        invoke DestroyWindow,hwnd
        invoke PostQuitMessage,0
    .ELSEIF uMsg==WM_SIZE
xor edx, edx
mov eax, lParam
mov dx, ax
shr eax, 16
shr edx, 2
shl edx, 2
mov maxx, edx
mov maxy, eax
mov bminf.bmiHeader.biWidth, edx
neg eax
mov bminf.bmiHeader.biHeight, eax
    .ELSE
        invoke DefWindowProc,hWnd,uMsg,wParam,lParam       
        ret
    .ENDIF
    xor eax,eax
    ret
WndProc ENDP
; -------------------------------------------------------------------------

start:
    invoke GetModuleHandle,NULL
    mov hInstance,eax
    mov wc.hInstance,eax
    mov wc.cbSize,SIZEOF WNDCLASSEX
    mov wc.style,CS_HREDRAW or CS_VREDRAW or CS_BYTEALIGNCLIENT
    mov wc.lpfnWndProc,OFFSET WndProc
    mov wc.cbClsExtra,NULL
    mov wc.cbWndExtra,NULL
    mov wc.hbrBackground,COLOR_MENUTEXT
    mov wc.lpszMenuName,NULL
    mov wc.lpszClassName,OFFSET ClassName
    invoke LoadCursor,NULL,IDC_ARROW
    mov wc.hCursor,eax
    invoke LoadIcon,hInstance,500
    mov wc.hIcon,eax
    mov wc.hIconSm,eax
    invoke RegisterClassEx,ADDR wc
    invoke CreateWindowEx,WS_EX_OVERLAPPEDWINDOW,ADDR ClassName,ADDR AppName,\
                          WS_OVERLAPPEDWINDOW,0,0,800,600,NULL,NULL,\
                          hInstance,NULL
    mov hwnd,eax
    invoke ShowWindow,hwnd,SW_SHOWNORMAL
    invoke UpdateWindow,hwnd
    invoke CreateThread,0,4096,ADDR FrameThread,0,0,ADDR idThread1
    invoke CreateThread,0,4096,ADDR GraphicsThread,0,0,ADDR idThread2
    mov hFThread,eax
    MsgLoop:
        invoke GetMessage,ADDR msg,0,0,0
        test eax,eax
        jz EndLoop
        invoke TranslateMessage,ADDR msg
        invoke DispatchMessage,ADDR msg
        jmp MsgLoop
    EndLoop:
    @@: mov eax,idThread1
        or  eax,idThread2
        not eax
        and eax,eax
        jnz @B
    invoke ExitProcess,eax

end start


P.S. The original code is posted here on the GDI+ forum/topic...I barebones it down to the essentials, just thought I would post it to get some graphics talk going.

There's more to come soon!!

Later all,

Jeff C

:U
Any good programmer knows, every large and/or small job, is equally large, to the programmer!