News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Random colored rects

Started by felipe, April 20, 2018, 01:40:09 PM

Previous topic - Next topic

felipe

Here is just another little demo of directx9. Fullscreen, escape with the esc key.
Special note: i'm the master of flickering... :P But i will gradually (i suppose  :redface:) understand and improve with this library. The .exe is attached.
Here is the source code. Not too much commented. As i said i have to dedicate some more time to do best things. Maybe i will take a little bit of time to upload another but better program, or maybe i will load soon another simple demo as this one (don't really know):


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

include     \masm32\include\DX9Includes\d3d9.inc

 
SCREEN_WIDTH    equ 1024                       ; Kind of old standard
SCREEN_HEIGHT   equ 768                             ;   resolution.

.data
align 1
class_name      byte        "game class",0
wnd_name        byte        "RANDOM COLORED RECTS",0
d3d9_lib        byte        "d3d9.dll",0
dx9func0        byte        "Direct3DCreate9",0

align 4
float4          dword       1.0

.data?
align 4
hinstance       dword       ?
hwindow         dword       ?
bye         dword     ?
hdll            dword       ?
proc_add        dword       ?
inter_add       dword       ?
d3dpp           D3DPRESENT_PARAMETERS <?>
d3ddev          dword       ?
backbuffer      dword       ?
surface         dword       ?
rect            dword       4 dup(?)
red             dword       ?
green           dword       ?
blue            dword       ?





.code
align 4
start:
            push        ebp
            mov         ebp,esp

            call        GetTickCount                                        ; Obtaining 
           
            push        eax                                                 ;  the seed
            call        nseed                                               ;     for the randomness.

            push        NULL                                                ; Get a handle for the program instance.
            call        GetModuleHandle
           
            mov         hinstance,eax
           
            sub         esp,76                                              ; WNDCLASSEX and MSG.
                                           
            mov         dword ptr[ebp-48],48                                ; The size of the structure.
            mov         dword ptr[ebp-44],CS_HREDRAW or CS_VREDRAW          ; The style of the class.
            mov         [ebp-40],wndproc
            mov         dword ptr[ebp-36],0                                 ; No extra
            mov         dword ptr[ebp-32],0                                 ;   bytes.
            mov         eax,hinstance
            mov         [ebp-28],eax

            mov         dword ptr[ebp-24],NULL                              ; No icon.

            push        IDC_ARROW                                           ; The system cursor.
            push        NULL                                             
            call        LoadCursor

            mov         [ebp-20],eax                                        ; hCursor.

            push        WHITE_BRUSH                                         ; Typical background color for a window.
            call        GetStockObject                                      ; Get the handle of the brush.

            mov         [ebp-16],eax                                        ; hBrush.
            mov         dword ptr[ebp-12],NULL                              ; No menu.
            mov         [ebp-8],offset class_name
            mov         dword ptr[ebp-4],NULL                               ; Use the same app icon for the small one.

            lea         eax,[ebp-48]                                        ; WNDCLASSEX.

            push        eax
            call        RegisterClassEx

            push        NULL                                                ; No value passed to the window.
            push        hinstance
            push        NULL                                                ; No menu.
            push        NULL                                                ; No parent of the window.
            push        SCREEN_HEIGHT                                       ; Old standar
            push        SCREEN_WIDTH                                        ;   resolution.
            push        CW_USEDEFAULT                                       ; Default position
            push        CW_USEDEFAULT                                       ;  of the window.
            push        WS_EX_TOPMOST or WS_VISIBLE or WS_POPUP             ; Game window style.
            push        offset wnd_name                                     ; Name of the game window.   
            push        offset class_name
            push        WS_EX_TOPMOST     ; Some extension of the style.
            call CreateWindowEx

            mov         hwindow,eax

            push        SW_SHOWNORMAL                                       ; Displaying the window for the first time.
            push        eax
            call        ShowWindow                                         

            push        hwindow
            call        UpdateWindow                                        ; Update the client area.

            push        offset d3d9_lib
            call        LoadLibrary                                         ; Load d3d9.dll to the address space of this program.

            mov         hdll,eax                                            ; Handle to the module loaded.

            push        offset dx9func0                                     ; Direct3DCreate9.
            push        eax                                                 ; hdll.
            call        GetProcAddress                                      ; Get the address of the dx9 function.

            mov         proc_add,eax                                        ; Save the address of the funtion Direct3DCreate9.

            push        D3D_SDK_VERSION
            call        eax                                                 ; Call the function Direct3DCreate9.
           
            mov         inter_add,eax                                       ; Safe the com interface address.                                 

            mov         esi,offset d3dpp
            mov         ecx,14                                              ; There are 14 dword members in this structure.

align 4
fill_struc:
            mov        dword ptr[esi],0
            add        esi,4                                                ; Next field.
            loop       fill_struc                                           ; Fill the structure with 0.

            mov        esi,offset d3dpp
   
    mov        dword ptr[esi+0],SCREEN_WIDTH                        ; Backbuffer width.
    mov        dword ptr[esi+4],SCREEN_HEIGHT                       ; Backbuffer height.
            mov        dword ptr[esi+8],D3DFMT_X8R8G8B8                     ; Backbuffer format.
            mov        dword ptr[esi+12],1                                  ; Backbuffer count.
            mov        dword ptr[esi+24],D3DSWAPEFFECT_DISCARD              ; Swap effect.
            mov        eax,hwindow
            mov        dword ptr[esi+28],eax                                ; device window.
            mov        dword ptr[esi+32],FALSE                              ; Not windowed, we want fullscreen.
     
            push        offset d3ddev
            push        offset d3dpp
            push        D3DCREATE_SOFTWARE_VERTEXPROCESSING
            push        eax                                                 ; hwindow
            push D3DDEVTYPE_HAL
            push        D3DADAPTER_DEFAULT

            mov         eax,inter_add                                       ; Com interface address.
            push        eax
           
            mov         eax,[eax]                                           ; Get the base address of the methods from this interface.                       

         
            call        dword ptr[eax+IDirect3D9_CreateDevice]              ; Call method CreateDevice (base+address of the method CreateDevice).       

            push        0
            push        float4
            push        00000000h                                           ; Black color.
            push        D3DCLEAR_TARGET
            push        NULL
            push        0

            mov         eax,d3ddev                                          ; Com interface address.
            push        eax
           
            mov         eax,[eax]                                           ; Get the base address of the methods from this interface.
            call        dword ptr[eax+IDirect3DDevice9_Clear]               ; Call method Clear (base+address of the method Clear).

            push        offset backbuffer
            push        D3DBACKBUFFER_TYPE_MONO
            push        0
            push        0

            mov         eax,d3ddev
            push        eax

            mov         eax,[eax]
            call        dword ptr[eax+IDirect3DDevice9_GetBackBuffer]

            push        NULL
            push        offset surface
            push        D3DPOOL_DEFAULT
            push        D3DFMT_X8R8G8B8
            push        100
            push        100

            mov         eax,d3ddev
            push        eax

            mov         eax,[eax]
            call        dword ptr[eax+IDirect3DDevice9_CreateOffscreenPlainSurface]

    lea         esi,[ebp-76]      ; MSG.
    mov bye,0      ; Flag to stop the game loop.

                   
align 4
msgloop:           
    push        PM_REMOVE                                           ; Pull out messages.
            push        0                                                   ; Retrieve all
            push        0                                                   ;  available messages.
            push        NULL                                                ; Messages of any window of this thread.
            push        esi                     ; MSG.
            call        PeekMessage       

    cmp eax,0
    je no_msg     ; No messages yet. Give the game the resources.

    push        esi
            call        TranslateMessage                                    ; Translate keyboard to messages.

            push        esi
            call        DispatchMessage                                     ; Dispatch messages to wndproc.

            cmp         bye,1
    je end_program                             ; Check if the game is over.

align 4
no_msg:
            mov         eax,d3ddev
            push        eax
            mov         eax,[eax]
            call        dword ptr[eax+IDirect3DDevice9_BeginScene]

; Random colors:
            push        255
            call        nrandom
            mov         red,eax

            push        255
            call        nrandom
            mov         green,eax
           
            push        255
            call        nrandom
            mov         blue,eax

            mov         eax,0ffh
            shl         eax,24
            and         red,0ffh
            shl         red,16
            and         green,0ffh
            shl         green,8
            and         blue,0ffh
            or          eax,red
            or          eax,green
            or          eax,blue

            push        eax
            push        NULL
            push        surface

            mov         eax,d3ddev
            push        eax

            mov         eax,[eax]
            call        dword ptr[eax+IDirect3DDevice9_ColorFill]

            ; RECT: left top right bottom.
            mov         esi,offset rect

            mov         eax,SCREEN_WIDTH
            shr         eax,1
            push        eax
            call        nrandom
            mov         [esi+0],eax

            add         eax,[esi+0]
            mov         [esi+8],eax         

            push        SCREEN_HEIGHT
            call        nrandom
            mov         [esi+4],eax

            mov         eax,SCREEN_HEIGHT
            shr         eax,1
            push        eax
            call        nrandom
            add         eax,[esi+4]
            mov         [esi+12],eax

            push        D3DTEXF_NONE
            push        offset rect
            push        backbuffer
            push        NULL
            push        surface

            mov         eax,d3ddev
            push        eax

            mov         eax,[eax]
            call        dword ptr[eax+IDirect3DDevice9_StretchRect]
           
            mov         eax,d3ddev
            push        eax
            mov         eax,[eax]
            call        dword ptr[eax+IDirect3DDevice9_EndScene]

            push        NULL               
            push        NULL
            push        NULL
            push        NULL
            mov         eax,d3ddev
            push        eax               
            mov         eax,[eax]         
            call        dword ptr[eax+IDirect3DDevice9_Present]     

    push VK_ESCAPE
    call GetAsyncKeyState                             ; The esc key turns off the game.

    test eax,80000000h
    jz keep_rolling                  ; If is not pressed keep playing.

    push 0
    push 0
    push WM_DESTROY
    push hwindow
    call PostMessage             ; If pressed let start to shutdown the game.

align 4
keep_rolling:
    jmp msgloop                                     ; All again.


align 4
end_program:
            mov         eax,[ebp-68]                                        ; wParam (exit code).

            mov         esp,ebp
            pop         ebp
 
            push        eax                                 
            call        ExitProcess                                         ; End the program.


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

align 4
wndproc:                                                                    ; The window procedure for the main window class.
            push        ebp
            mov         ebp,esp

            cmp         dword ptr[ebp+12],WM_DESTROY             ; Game over.
            jne do_the_default


            cmp         d3ddev,NULL
            je          skip_cleaning

            mov         eax,d3ddev
            push        eax
            mov         eax,[eax]
            call        dword ptr[eax+IUnknown_Release]
           
            mov         eax,d3ddev
            push        eax               
            mov         eax,[eax]         
            call        dword ptr[eax+IUnknown_Release]

            cmp         inter_add,NULL
            je          skip_cleaning

            mov         eax,inter_add
            push        eax               
            mov         eax,[eax]         
            call        dword ptr[eax+IUnknown_Release]


align 4
skip_cleaning:
            push        0                                                   ; Exit code (wParam in WM_QUIT).
            call        PostQuitMessage                                     ; Notify to windows that is the end of the application.   

            mov bye,1     ; To stop the game loop.


            xor         eax,eax
           
            mov         esp,ebp
            pop         ebp
           
            ret         16



align 4
do_the_default:
    push        dword ptr[ebp+20]                                   ; lParam.
            push        dword ptr[ebp+16]                                   ; wParam.
            push        dword ptr[ebp+12]                                   ; The message.
            push        dword ptr[ebp+8]                                    ; Handle of the window.
            call        DefWindowProc                                       ; Default processing for all the other messages.

            mov         esp,ebp
            pop         ebp

            ret         16
           
            end         start

daydreamer

nice to see dx code without macros :t, enough knowledge for make a tetris or a breakout game


my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding

Siekmanski

Can't wait to see your first game in DX9.  :t
Creative coders use backward thinking techniques as a strategy.

LordAdef

Hi Felipe, sorry to hijack the thread just for one question to Marinus:


Marinus, how difficult would it be to port from GDI to Dx9?

felipe

 :biggrin: Hi Felipe, sorry to hijack the thread just for one comment to DayDreamer:


I like your new personal text. :P

QuoteI hate C++ right now

:icon14:

Siekmanski

Quote from: LordAdef on April 21, 2018, 05:53:32 AM
Hi Felipe, sorry to hijack the thread just for one question to Marinus:


Marinus, how difficult would it be to port from GDI to Dx9?

Not too difficult, you can set up DX9 to 2D mode and do the same kind of blitting as you do in GDI.
One thing only.... it will be a lot faster.  :bgrin:
And you can use all the functions from the video card and create very nice effects.
DX9 is only difficult if you make it difficult for yourself.
Creative coders use backward thinking techniques as a strategy.

LordAdef

Quote from: Siekmanski on April 21, 2018, 10:19:46 AM
Quote from: LordAdef on April 21, 2018, 05:53:32 AM
Hi Felipe, sorry to hijack the thread just for one question to Marinus:


Marinus, how difficult would it be to port from GDI to Dx9?

Not too difficult, you can set up DX9 to 2D mode and do the same kind of blitting as you do in GDI.
One thing only.... it will be a lot faster.  :bgrin:
And you can use all the functions from the video card and create very nice effects.
DX9 is only difficult if you make it difficult for yourself.

Thanks Marinus. And with respect with Felipe's thread, I bring this question back to op's subject:

I find rather interesting that Felipe (and the other guys) and I are following different development routes. Felipe is cracking on DX9 straight away, while my approach was to stick with GDI and develop the game architecture first. Then, port the rendering to something (which I originally thought of openGl).

But you guys are exchanging such interesting stuff I'm almost porting it to DX9 now

daydreamer

Quote from: felipe on April 21, 2018, 07:31:52 AM
:biggrin: Hi Felipe, sorry to hijack the thread just for one comment to DayDreamer:


I like your new personal text. :P

QuoteI hate C++ right now

:icon14:
thanks
:icon14:
my none asm creations
https://masm32.com/board/index.php?topic=6937.msg74303#msg74303
I am an Invoker
"An Invoker is a mage who specializes in the manipulation of raw and elemental energies."
Like SIMD coding