Jump to content
Sign in to follow this  

Ding Ding Ding Ding Ding

Recommended Posts


I'm attempting to change terrain example (#11) to have the 3D view take up less space on the gui, so I can have buttons and the like, but whatever I try changing doesnt do anything, the view still takes up all gui space.

So I have added it to a child window instead, and got it working, except when the gui is deactivated and then activated again, then my keyboard goes ding ding ding when using the wasd keys for movement.

The code.. (TerrainApp.au3 in the D3D example #11)

#include "Camera.au3"
#include "Terrain.au3"
#include <GUIConstantsEx.au3>
#include <Misc.au3>
#include <WindowsConstants.au3>
#include <WinAPI.au3>
;Opt("MustDeclareVars", 1)
Opt("GUIOnEventMode", 1)

;Local Const $sHMapfile   = ".\Heightmaps\dont_use_spaces\dont_use_spaces_8b.pgm"
;Local Const $sTerrainTex = ".\Heightmaps\dont_use_spaces\dont_use_spaces.bmp"
Local Const $sHMapfile   = ".\..\..\Media\map128.raw"
Local Const $sTerrainTex = ".\..\..\Media\Background.jpg"

; Weight modifier used by the weighted averaging of the mouse movement history array
; As the modifier value increases, the camera gets rotated smoothly but takes longer to rotate to the target.
Local Const $nWeightModifier = 0.3

Local Const $iWidth = 800, $iHeight = 600
Local Const $iCenterX = $iWidth/2, $iCenterY = $iHeight/2
; Camera mouse movement sensitivity. Decrease for faster scroll, increase for slower, yet more accurate scroll..
Local Const $nSensitivity = 100

Local $gui = GUICreate("Viewer", 1024, 600, 300, 200)
;GUICtrlCreateLabel("", 0, 0, 0, 0) ; <----------- stuff i've been trying

$filemenu = GUICtrlCreateMenu("File")
$openg2c = GUICtrlCreateMenuItem("Open project", $filemenu)
GUICtrlCreateMenuItem("", $filemenu)
$exit = GUICtrlCreateMenuItem("Exit", $filemenu)

$viewmenu = GUICtrlCreateMenu("View")
$viewtextured = GUICtrlCreateMenuItem("Textured", $viewmenu)
$viewwireframe = GUICtrlCreateMenuItem("Wireframe", $viewmenu)
$viewgradient = GuiCtrlCreateMenuItem("Gradient", $viewmenu)

$helpmenu = GUICtrlCreateMenu("Help")
$help = GUICtrlCreateMenuItem("Help", $helpmenu)
$about = GUICtrlCreateMenuItem("About", $helpmenu)

Local $hGUI = GUICreate("Viewer", 800, 600, 0, 0, $WS_CHILD, Default, $gui)
;GUISwitch($gui); <----------- stuff i've been trying
;GUICtrlCreateLabel("", 0, 0, 0, 0); <----------- stuff i've been trying
;_WinAPI_SetParent($hGui, $gui); <----------- stuff i've been trying
GUICtrlCreateLabel("", 0, 0, 0, 0)
Local $pDevice, $oDevice

; Offset to the window's center from the screen left-top
Local $iWndCenterX, $iWndCenterY
; History array used to hold last X mouse movement deltas,
; so we can use smooth mouse filtering. Smooth mouse filtering reduces
; the jerky motion of the camera caused by the native jerky mouse movement
Local $aHistory[10][2]

; Camera and terrain objects
Local $tCamera, $tTerrain
; Direction to light holds the opposite vector of the light dir
; so if we're lighting our terrain with a source of light [0, -1, 0] or downward,
; the direction to light should be [0, 1, 0] or upward.
Local $tDirToLight
; Terrain texturing switch
Local $fSwitch

; Check if failed to load necessary dlls
If @error Then
    If @error = 1 Then
        Exit MsgBox(0x10, "Error", 'Could not load "d3d9.dll"')
        Exit MsgBox(0x10, "Error", 'Could not load "d3dx9_36.dll"')

$pDevice = _D3D_Init($hGUI, $iWidth, $iHeight)
$oDevice = _AutoItObject_WrapperCreate($pDevice, $tagIDirect3DDevice9)

GUISetOnEvent($GUI_EVENT_CLOSE, "_Quit")
; Change terrain texturing using spacebar

While 1

Func _Render()
    ; Allow user to update the camera view
    Local $iRunFactor, $aDeltas, $tCameraPos, $nHeight, $tView, $tWorld
If WinActive("Viewer") Then
If _IsPressed("02")Then
    ; Left shift to run
    If _IsPressed("A0") Then $iRunFactor = 0.5

    If _IsPressed("57") Then     ; W - move forward
        _CameraWalk($tCamera,  0.7 + $iRunFactor)
    ElseIf _IsPressed("53") Then ; S - move backward
        _CameraWalk($tCamera, -0.7 - $iRunFactor)

    If _IsPressed("41") Then     ; A - move leftward
        _CameraStrafe($tCamera, -0.7 - $iRunFactor)
    ElseIf _IsPressed("44") Then ; S - move rightward
        _CameraStrafe($tCamera,  0.7 + $iRunFactor)

    $aDeltas = _MouseSmoothFilter($aHistory)

    _CameraYaw  ($tCamera, $aDeltas[0] / $nSensitivity)
    _CameraPitch($tCamera, $aDeltas[1] / $nSensitivity)

    If IsDllStruct($tTerrain) Then
        _CameraGetPos($tCamera, $tCameraPos)
        $nHeight = _TerrainGetHeight($tTerrain, DllStructGetData($tCameraPos, "x"), DllStructGetData($tCameraPos, "z"))
        DllStructSetData($tCameraPos, "y", 50)
        _CameraSetPos($tCamera, $tCameraPos)

    ; Get the camera view matrix
    $oDevice.SetTransform("long", "int", $D3DTS_VIEW, "ptr", _CameraGetView($tCamera, $tView))

    ; Draw the scene, clear the depth and back buffers
    $oDevice.Clear("long", "uint", 0, "ptr", 0, "uint", BitOR($D3DCLEAR_TARGET, $D3DCLEAR_ZBUFFER), "uint", 0xFF5D6FA2, "float", 1, "uint", 0)

    If IsDllStruct($tTerrain) Then
        _TerrainDraw($tTerrain, $tWorld)

    $oDevice.Present("long", "ptr", 0, "ptr", 0, "hwnd", 0, "ptr", 0)

Func _MouseSmoothFilter(ByRef $aMovement)
    Local $aPos, $iDX, $iDY

    ; Get cursor position
    $aPos = _GetCursorPos()
    ; Reset mouse position back to the center of the window
    _SetCursorPos($iWndCenterX, $iWndCenterY)
    $iDX = $aPos[0] - $iWndCenterX
    $iDY = $aPos[1] - $iWndCenterY

    _MousePerformFiltering($aMovement, $iDX, $iDY)
    Return _MousePerformSmoothing($aMovement)

Func _MousePerformFiltering(ByRef $aMovement, $nDX, $nDY)
    Local $aDeltas[2], $nAvgX, $nAvgY, $nAvgSum, $nCurrWeight = 1

    ; Update the arraw by pushing all elements of the array, thus the array contains the last X deltas
    ; where X is the size of the array
    For $i = UBound($aMovement)-1 To 1 Step -1
        $aMovement[$i][0] = $aMovement[$i - 1][0]
        $aMovement[$i][1] = $aMovement[$i - 1][1]

    $aMovement[0][0] = $nDX
    $aMovement[0][1] = $nDY

    ; Weighted average of deltas
    For $i = 0 To UBound($aMovement)-1
        $nAvgX += $aMovement[$i][0] * $nCurrWeight
        $nAvgY += $aMovement[$i][1] * $nCurrWeight
        $nAvgSum += $nCurrWeight
        $nCurrWeight *= $nWeightModifier

    $aDeltas[0] = $nAvgX / $nAvgSum
    $aDeltas[1] = $nAvgY / $nAvgSum
    ; Update the current mouse movement delta with the weighted delta
    $aMovement[0][0] = $aDeltas[0]
    $aMovement[0][1] = $aDeltas[1]

    Return $aDeltas

Func _MousePerformSmoothing(ByRef $aMovement)
    Local $aDeltas[2]

    $aDeltas[0] = ($aMovement[0][0] + $aMovement[1][0]) * 0.5
    $aDeltas[1] = ($aMovement[0][1] + $aMovement[1][1]) * 0.5

    Return $aDeltas

Func _Setup()
    Local $tProj, $aPos, $aRet, $iSize

    ; Use the square root of the file size for dimensions of the terrain
    $iSize = Int(Sqrt(FileGetSize($sHMapfile)))

    ; Create the first person camera object. Land object camera type.
    $tCamera  = _CameraCreate($CAMERATYPE_LANDOBJECT)
    ; Create the direction to light vector for colors shading. Light rays travel at -45 degrees down relative to the x-axis
    ; so the direction to light is upword at 135 degrees relative to the x-axis
    ; Note that the direction to light vector is a unit vector.
    _D3D_CreateVector3($tDirToLight, -0.707, 0.707, 0)
    ; Higher cell spaced terrains are said to be lower resolution terrains.
    $tTerrain = _TerrainCreate($pDevice, $sHMapfile, $iSize, $iSize, 2, 0.5)
    If Not @error Then
        ;_TerrainGenerateTexture($tTerrain, $tDirToLight)

    ; Set texture filters
    $oDevice.SetSamplerState("long", "uint", 0, "int", $D3DSAMP_MAGFILTER, "uint", $D3DTEXF_LINEAR)
    $oDevice.SetSamplerState("long", "uint", 0, "int", $D3DSAMP_MINFILTER, "uint", $D3DTEXF_LINEAR)
    $oDevice.SetSamplerState("long", "uint", 0, "int", $D3DSAMP_MIPFILTER, "uint", $D3DTEXF_LINEAR)

    ; Set projection matrix (45 degrees)
    $oDevice.SetTransform("long", "int", $D3DTS_PROJECTION, "ptr", _D3DX_MatrixPerspectiveFovLH($tProj, ASin(1)/2, $iWidth/$iHeight, 1, 5000))

    ; Calculate the screen position of our window's center
    $aPos = WinGetPos($hGUI)
    $iWndCenterX = $aPos[0] + $iCenterX
    $iWndCenterY = $aPos[1] + $iCenterY

    ; Reset mouse to the center of the window and hide the mouse cursor
    ;_SetCursorPos($iWndCenterX, $iWndCenterY)

Func _WM_KEYDOWN($hWnd, $iMsg, $iwParam, $ilParam)
    If $iwParam = 0x20 Then
        If IsDllStruct($tTerrain) Then
            $fSwitch = Not $fSwitch


Func _Quit()

Func _Cleanup()
    $oDevice = 0

; #FUNCTION#;===============================================================================
; Name...........: _HighPrecisionSleep()
; Description ...: Sleeps down to 0.1 microseconds
; Syntax.........: _HighPrecisionSleep( $iMicroSeconds, $hDll=False)
; Parameters ....:  $iMicroSeconds      - Amount of microseconds to sleep
;                  $hDll  - Can be supplied so the UDF doesn't have to re-open the dll all the time.
; Return values .: None
; Author ........: Andreas Karlsson (monoceres)
; Modified.......:
; Remarks .......: Even though this has high precision you need to take into consideration that it will take some time for autoit to call the function.
; Related .......:
; Link ..........;
; Example .......; No
Func _HighPrecisionSleep($iMicroSeconds,$hDll=False)
    Local $hStruct, $bLoaded
    If Not $hDll Then
    $hStruct=DllStructCreate("int64 time;")
    If $bLoaded Then DllClose($hDll)

Func _GetCursorPos()
    Local $aRet[2], $tPoint = DllStructCreate("int;int")
    DllCall("user32.dll", "bool", "GetCursorPos", "ptr", DllStructGetPtr($tPoint))

    $aRet[0] = DllStructGetData($tPoint, 1) ; X
    $aRet[1] = DllStructGetData($tPoint, 2) ; Y

    Return $aRet

Func _SetCursorPos($iX, $iY)
    DllCall("user32.dll", "bool", "SetCursorPos", "int", $iX, "int", $iY)

Hold down right mouse button and use wasd for movement, then try deactivating the gui by doing something else like notepad or focus on this browser window, and then go back to the gui, then it goes ding ding when using the wasd keys.

I've been reading around ofcourse and tried some stuff like the "empty label trick" I've heard mentioned, and guiswitch() and setparent(), but cant get it working,

Ultimately, get rid of the child window and just have the D3D view not take up all the gui space. :graduated:


Edited by Werty

Share this post

Link to post
Share on other sites

24 hour bump :graduated:

Share this post

Link to post
Share on other sites

I still need this, really :graduated:

Share this post

Link to post
Share on other sites

Your attached code is not executable without the dependencies! This is the reason you got no answer!

Try GUISetAccelerators() for all keys that are used. Use a dummy function for them.

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post

Link to post
Share on other sites

Your attached code is not executable without the dependencies! This is the reason you got no answer!

No, I dont get any answers because ppl are lazy :(

Try GUISetAccelerators() for all keys that are used. Use a dummy function for them.

I'll look into that, thanks.

If I'm unsuccessfull I'll look into making another example (new thread!) that doesnt require any extras. :graduated:

Edited by Werty

Share this post

Link to post
Share on other sites


And ofc my old topic on identical problem shud help i hope :graduated:

Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)

There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this