
 format PE GUI 4.0
 entry start


 include 'WIN32AX.inc'
 include 'DxIncld/d3d11.inc'
 include 'win32errNmbrRet.inc'

 SCREEN_WIDTH        equ  800;1920
 SCREEN_HEIGHT       equ  600;


 section '.code' code readable executable
 ;=======================================================================================================



  start:
      invoke            GetModuleHandle,0
      mov               [hInst],eax
      invoke            GetCommandLine
      mov               [cmdLine],eax
      stdcall           InitializeWindow,[hInst],[cmdLine]

      stdcall           InitializeDirect3d11App
      stdcall           InitScene
      stdcall           messegeLoop
      stdcall           CleanUp

      invoke          ExitProcess,eax


;------------------------------------------------------------------
    proc       InitializeWindow       hInstance:DWORD,commandLine:DWORD
                push            ebx esi edi
    ;----declare window struct----------------------------
                push            [hInstance]
                pop             [wc.hInstance]
                mov             [wc.cbSize], sizeof.WNDCLASSEX
                mov             [wc.style], CS_HREDRAW or CS_VREDRAW
                mov             [wc.lpfnWndProc],WindowProc
                xor             eax,eax
                mov             [wc.cbClsExtra],eax
                mov             [wc.cbWndExtra],eax
                mov             [wc.hbrBackground],COLOR_WINDOW + 2
                mov             [wc.lpszMenuName], eax
                mov             [wc.lpszClassName],ClassName
                invoke          LoadIcon, NULL, IDI_APPLICATION
                mov             [wc.hIcon], eax
                mov             [wc.hIconSm], eax
                invoke          LoadCursor, NULL, IDC_ARROW
                mov             [wc.hCursor], eax

    ;----Registering window class----------
                invoke          RegisterClassEx, wc
                or              eax,eax
                jz              err1


                invoke          CreateWindowEx,0,ClassName,AppName,WS_VISIBLE+WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,SCREEN_WIDTH,SCREEN_HEIGHT,0,0,[wc.hInstance],0
                or              eax,eax
                jz              err2
                mov             [hwnd],eax

                ;-----Update window---------------------
                invoke          UpdateWindow, [hwnd]
                jmp             end_WinMain


                ;----errors------------------------------------------------------------------------
        err1:
                invoke          MessageBox, 0,ErrTxt1,ErrMsgBoxCaption, MB_OK
                jmp             end_WinMain
        err2:
                invoke          MessageBox, 0,ErrTxt2,ErrMsgBoxCaption, MB_OK
                jmp             end_WinMain

    ;----------------------------------------------------------------------------------

        end_WinMain:
                ret
                pop             edi esi ebx
    endp

;----------------------------------------------------------------------------------------------------------
;------------------------------------InitializeDirect3d11App procedure------------------------------------------------------
    proc       InitializeDirect3d11App
                push            ebx esi edi

                mov             ecx,sizeof.DXGI_SWAP_CHAIN_DESC
                mov             eax,swapChainDesc
                stdcall         ClearStructr,eax,ecx

                mov             [swapChainDesc.SampleDesc.Quality], 0
                mov             [swapChainDesc.SampleDesc.Count],1
                mov             [swapChainDesc.BufferUsage],DXGI_USAGE_RENDER_TARGET_OUTPUT    ;    how swap chain is to be used
                mov             [swapChainDesc.BufferCount],1                                  ;    one back buffer
                mov             eax,[hwnd]
                mov             [swapChainDesc.OutputWindow],eax                               ;  the window to be used
                mov             [swapChainDesc.Windowed],TRUE                                 ;  windowed/full-screen mode
                mov             [swapChainDesc.SwapEffect],DXGI_SWAP_EFFECT_DISCARD

                mov             [swapChainDesc.BufferDesc.Format],DXGI_FORMAT_R8G8B8A8_UNORM   ;    use 32-bit color
                mov             [swapChainDesc.BufferDesc.Width], SCREEN_WIDTH
                mov             [swapChainDesc.BufferDesc.Height], SCREEN_HEIGHT
                mov             [swapChainDesc.BufferDesc.RefreshRate.Numerator],60
                mov             [swapChainDesc.BufferDesc.RefreshRate.Denominator],1
                mov             [swapChainDesc.BufferDesc.ScanlineOrdering],DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
                mov             [swapChainDesc.BufferDesc.Scaling],DXGI_MODE_SCALING_UNSPECIFIED;

                mov             [swapChainDesc.Flags],DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH


                invoke          D3D11CreateDeviceAndSwapChain,NULL,D3D_DRIVER_TYPE_HARDWARE,NULL,NULL,NULL,NULL,D3D11_SDK_VERSION,addr swapChainDesc,addr SwapChain,addr d3d11Device,NULL,addr d3d11DevCon
                or              eax,eax
                jz              InitD3D_errorChk2

                mov             eax,DxErrStr1
                jmp             InitD3D_error

        InitD3D_errorChk2:
                cmp             [SwapChain],NULL
                jne             InitD3D_errorChk3

                mov             eax,DxErrStr2
                jmp             InitD3D_error

        InitD3D_errorChk3:
                cmp             [d3d11Device],NULL
                jne             InitD3D_errorChk4
                mov             eax,DxErrStr3
                jmp             InitD3D_error

        InitD3D_errorChk4:
                cmp             [d3d11DevCon],NULL
                jne             ctInitD3D
                mov             eax,DxErrStr4
                jmp             InitD3D_error


        ctInitD3D:
                cominvk         SwapChain,GetBuffer,0, IID_ID3D11Texture2D,BackBuffer
                or              eax,eax
                jnz             BackBuffObjErr

                cominvk         d3d11Device,CreateRenderTargetView,[BackBuffer], NULL,renderTargetView
                or              eax,eax
                jnz             rndrtrgtVwObjErr

                ;pusha
                ;call        HxDcValtostr
                ;popa

                cominvk         d3d11DevCon, OMSetRenderTargets,1,renderTargetView, NULL

                cominvk         BackBuffer,Release


                jmp             end_InitD3D

    ;----errors------------------------------------------------------------------------
        BackBuffObjErr:
                mov             eax,DxErrStr5
                jmp             InitD3D_error

        rndrtrgtVwObjErr:
                mov             eax,DxErrStr6
                jmp             InitD3D_error

        InitD3D_error:
                invoke          MessageBox, 0,eax,ErrMsgBoxCaption, MB_OK
                invoke          PostQuitMessage, 0
    ;----------------------------------------------------------------------------------
        end_InitD3D:
                pop             edi esi ebx

                ret
    endp
;----------------------------------------------------------------------------------------------------------
;----------------------------CleanUp--------------close and release all existing COM objects--------------

    proc     CleanUp
                push            ebx esi edi                          ;
                ;cominvk         swapchain,SetFullscreenState,FALSE, NULL

                cominvk         SwapChain,Release
                ;mov             [swapchain], 0
                cominvk         d3d11Device, Release
                ;mov             [dev], 0
                cominvk         d3d11DevCon,Release

                cominvk         renderTargetView,Release
                ;mov             [devCntxt], 0
                cominvk         triangleVertBuffer,Release
                cominvk         VS,Release
                cominvk         PS,Release
                cominvk         VS_Buffer,Release
                cominvk         PS_Buffer,Release
                cominvk         vertLayout,Release

        end_CleanD3D:
                pop             edi esi ebx

                ret
    endp
;----------------------------------------------------------------------------------------------------------

;------------------------------------InitScene procedure------------------------------------------------------
    proc       InitScene
                push            ebx esi edi

    ;----------------------Load and compile the two shaders from the .shader file and save compiled shaders in blob objects
                invoke          D3DX11CompileFromFile,Effects, 0, 0, VSdr, vs_4_0, 0, 0, 0, VS_Buffer, 0, 0;
                invoke          D3DX11CompileFromFile,Effects, 0, 0, PSdr, ps_4_0, 0, 0, 0, PS_Buffer, 0, 0;
                ;pusha
                ;call        HxDcValtostr
                ;popa
     ;---------------encapsulate compiled shaders to com objects-----------------------------------------------------------
                cominvk         VS_Buffer,GetBufferPointer
                mov             ecx,eax
                push            ecx
                cominvk         VS_Buffer,GetBufferSize
                pop             ecx
                cominvk         d3d11Device,CreateVertexShader,ecx,eax,NULL, VS   ;/**


                cominvk         PS_Buffer,GetBufferPointer
                mov             ecx,eax
                push            ecx
                cominvk         PS_Buffer,GetBufferSize
                pop             ecx
                cominvk         d3d11Device,CreatePixelShader,ecx,eax,NULL, PS   ;/**

    ;----------------------Set both shaders to be the active-------------------------------------
                cominvk         d3d11DevCon,VSSetShader, [VS],0,0

                cominvk         d3d11DevCon,PSSetShader, [PS],0,0


                cominvk         VS_Buffer,GetBufferPointer
                mov             ecx,eax
                push            ecx
                cominvk         VS_Buffer,GetBufferSize
                pop             ecx
                cominvk         d3d11Device,CreateInputLayout, layout,1, ecx,eax, vertLayout;
                ;pusha
                ;call        HxDcValtostr
                ;popa

                cominvk         d3d11DevCon,IASetInputLayout,[vertLayout];


    ;--------------------------------------------------------------------------------------------
                mov             ecx,sizeof.D3D11_BUFFER_DESC
                mov             eax,vertexBufferDesc
                stdcall         ClearStructr,eax,ecx

                mov             [vertexBufferDesc.Usage],D3D11_USAGE_DEFAULT;
                mov             [vertexBufferDesc.ByteWidth],sizeof.Vertex * 3;
                mov             [vertexBufferDesc.BindFlags],D3D11_BIND_VERTEX_BUFFER;
                mov             [vertexBufferDesc.CPUAccessFlags],0;
                mov             [vertexBufferDesc.MiscFlags],0;


                mov             ecx,sizeof.D3D11_SUBRESOURCE_DATA
                mov             eax,vertexBufferData
                stdcall         ClearStructr,eax,ecx
                ;mov             edi,v
                mov             [vertexBufferData.pSysMem],v;


                cominvk         d3d11Device,CreateBuffer, vertexBufferDesc, vertexBufferData, triangleVertBuffer;

                mov             [stride],sizeof.Vertex;
                mov             [offset],0;
                cominvk         d3d11DevCon,IASetVertexBuffers, 0, 1, triangleVertBuffer,addr stride,addr offset;
      ;--------------------------------------------------------------------------------------------

                cominvk         d3d11DevCon,IASetPrimitiveTopology, D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST;
      ;--------------------------------------------------------------------------------------------

                mov             ecx,sizeof.D3D11_VIEWPORT
                mov             eax,viewport
                stdcall         ClearStructr,eax,ecx

                mov             [viewport.TopLeftX],0;
                mov             [viewport.TopLeftY],0;
                mov             [viewport.Width],800.0;
                mov             [viewport.Height],600.0;
                ;mov             [viewport.MaxDepth],1.0;
                cominvk         d3d11DevCon,RSSetViewports,1, viewport;

                xor             eax,eax
                pop             edi esi ebx

                ret
    endp
;----------------------------------------------------------------------------------------------------------

;///////////////**************new**************////////////////////

 proc      UpdateScene
;{
;
    ret
 endp

;///////////////**************new**************////////////////////

;------------------------------------DrawScene procedure------------------------------------------------------
    proc       DrawScene
                push            ebx esi edi

                cominvk         d3d11DevCon,ClearRenderTargetView,[renderTargetView], addr bgColor;
                cominvk         d3d11DevCon,Draw,3, 0;
                cominvk         SwapChain,Present,0, 0;

                pop             edi esi ebx

                ret
    endp
;----------------------------------------------------------------------------------------------------------

;----------------------------------------------------------------------------------------------------------
    proc       messegeLoop
               push            ebx esi edi



        msg_loop:
                invoke          PeekMessage,msg,NULL,0, 0, PM_REMOVE
                or              eax,eax
                jz              dxProgrmng      ;no masseges

                cmp             [msg.message],WM_QUIT            ;if messages present
                je              end_loop

                invoke          TranslateMessage,msg
                invoke          DispatchMessage,msg



                jmp             msg_loop


        dxProgrmng:

    ;--game code---------------------------------------
                ;stdcall         UpdateScene
                stdcall         DrawScene;
                jmp             msg_loop


    ;---------------------------------------------------
        end_loop:
                mov             eax,[msg.wParam]
                pop             edi esi ebx
                ret

    endp

;----------------------------------------------------------------------------------------------------------

;------------------------------------window procedure------------------------------------------------------
    proc       WindowProc hwnd,umsg:DWORD,wParam:DWORD,lparam:DWORD

                push            ebx esi edi
                cmp             [umsg],WM_DESTROY
                je              wmdestroy
                cmp             [umsg],WM_CLOSE
                je              close
                cmp             [umsg],WM_KEYDOWN
                je              checkKeys

        defwndproc:
                invoke          DefWindowProc,[hwnd],[umsg],[wParam],[lparam]
                jmp             end_WindowProc


        checkKeys:
                cmp             [wParam],VK_ESCAPE
                je              close

                xor             eax,eax
                jmp             end_WindowProc

        close:
                invoke          DestroyWindow,[hwnd]

        wmdestroy:
                invoke          PostQuitMessage,0
                xor             eax,eax

        end_WindowProc:
                pop             edi esi ebx
                ret
    endp
;----------------------------------------------------------------------------------------------------------

;------------------------------------ClearStructr-----------------------------------------------------------
    proc       ClearStructr strucName,strucSize

                pusha
                mov             edi,[strucName]
                mov             ecx,[strucSize]
                mov             al,0
                cld
                rep             stosb

                popa
                ret
    endp
;----------------------------------------------------------------------------------------------------------
;=======================================================================================================

 section '.data' data readable writeable
;========================================================================================================

;//Global Declarations - Others//
 ClassName           db 'MNSwndClass',0
 AppName             db 'Direct3d11 sample',0
 position            db "POSITION",0
 ErrTxt1             db 'Registering window class Failed',0
 ErrTxt2             db 'Fail to create a window using MNSwndClass',0
 ErrMsgBoxCaption    db 'Program Error ',0

 DxErrStr1           db 'Error in the D3D11CreateDeviceAndSwapChain',0
 DxErrStr2           db 'Error in creating swapchain object',0
 DxErrStr3           db 'Error in creating device object',0
 DxErrStr4           db 'Error in creating device context object',0
 DxErrStr5           db 'Error in creating swapchain backbuffer object',0
 DxErrStr6           db 'Error in creating render-target view object',0
 Effects             db 'Effects.fx',0
 VSdr                db 'VS',0
 PSdr                db 'PS',0
 vs_4_0              db 'vs_4_0',0
 ps_4_0              db 'ps_4_0',0
;========================================================================================================


 section '.bss' readable writeable
 ;=======================================================================================================
             ;//Global Declarations - Interfaces//

 layout    D3D11_INPUT_ELEMENT_DESC     position, 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0


 struct    Vertex
                 dd       ?,?,?

       ;color     D3DXCOLOR
ends


 hwnd                dd ?
 hr                  dd ?
 hInst               dd ?
 cmdLine             dd ?

 msg                 MSG
 wc                  WNDCLASSEX
 vertexBufferDesc    D3D11_BUFFER_DESC ;
 swapChainDesc       DXGI_SWAP_CHAIN_DESC
 vertexBufferData    D3D11_SUBRESOURCE_DATA ;
 viewport            D3D11_VIEWPORT ;



 SwapChain             IDXGISwapChain
 d3d11Device           ID3D11Device
 d3d11DevCon           ID3D11DeviceContext
 renderTargetView      ID3D11RenderTargetView

 triangleVertBuffer     ID3D11Buffer
 VS                     ID3D11VertexShader
 PS                     ID3D11PixelShader
 VS_Buffer              ID3D10Blob
 PS_Buffer              ID3D10Blob
 vertLayout             ID3D11InputLayout
 BackBuffer             ID3D11Texture2D


  stride              dd ?
 offset              dd ?

 bgColor             dd 0.0,0.0, 0.0, 0.0


v      Vertex  < 0.0, 0.5, 0.5 >
        Vertex < 0.5, -0.5, 0.5 >
        Vertex < -0.5, -0.5, 0.5 >


;=======================================================================================================



;========================================================================================================





 section '.idata' import data readable
;=======================================================================================================
         library         kernel32,'KERNEL32.DLL',\
                         user32,'USER32.DLL',\
                         d3d11,'D3D11.dll',\
                         d3dx11,'d3dx11_43.dll';,\
                         ;d3dx10,'D3DX10_43.DLL'
                         ;msvcrt, 'msvcrt.dll'

         ;include 'api/msvcrt.inc'
         ;include 'api/COMDLG32.inc'
         include 'api/kernel32.inc'
         include 'api/USER32.inc'
         include 'DxIncld/Api/d3d11Api.inc'
         include 'DxIncld/Api/D3DX11Api.inc'
         ;include 'DxIncld/Api/D3DX10Api.inc'
;=======================================================================================================

