Jump to content

Game


Drchubby
 Share

Recommended Posts

Hey, im trying to script a simple game engine, and it works so far. But you see constant flikkering of the images.

Is there any way to do this using gdi which would prevent this?

heres my code... srry its a mess, im only actively doing autoit for a month or so. Ive also attached a rar in which are all necessary files.

;gdi testing environment
#include <GuiConstantsEx.au3>
#include <GDIPlus.au3>
#Include <Misc.au3>


; Create GUI
$hGUI = GUICreate("Test", 800, 800)
$hWnd = WinGetHandle("Test")
GUISetState()
   
_GDIPlus_Startup ()

$onGui = _GDIPlus_GraphicsCreateFromHWND($hWnd)

dim const $mapFolder = "maps/"
dim const $spriteFolder = "sprites/"


dim const $maxDrawnPlayers = 5

dim $player[$maxDrawnPlayers][7]
dim const $map          = 0
dim const $sprite       = 1
dim const $handle       = 2
dim const $posX         = 3
dim const $posY         = 4
dim const $mapX         = 5
dim const $mapY         = 6

dim $playerSpriteFamily[$maxDrawnPlayers][6]
dim const $family         = 0
dim const $up             = 1
dim const $down         = 2
dim const $left       = 3
dim const $right          = 4


dim const $maxObjects = 100
dim $objects[$maxObjects][4]
dim const $objectPath = 0
dim const $objectX = 1
dim const $objectY = 2
dim const $objectHandle = 3


global $objects
global $player
global $playerSpriteFamily


dim $localPlayer    = 1



func checkKeyPressed()
    if _IsPressed("1b") then 
        exit
    elseif _IsPressed(26) then
        movePlayer(0, -1, 10, "up")
    elseif _IsPressed(27) then 
        movePlayer(1, 0, 10, "right")
    elseif _IsPressed(28) then 
        movePlayer(0, 1, 10, "down")
    elseif _IsPressed(25) then 
        movePlayer(-1, 0, 10, "left")
    EndIf
EndFunc


;starting variables
$currentMap                     = "map1"
$player[$localPlayer][$map]     = _GDIPlus_ImageLoadFromFile($mapFolder & $currentMap & ".jpg")
; not needed anymore >>> $player[$localPlayer][$sprite]     = _GDIPlus_ImageLoadFromFile("bird.png")
$player[$localPlayer][$posX]    = 395; fixed for local players
$player[$localPlayer][$posY]    = 395; fixed for local players
$player[$localPlayer][$mapX]    = 395
$player[$localPlayer][$mapY]    = 395

$playerSpriteFamily[$localPlayer][$family] = "bird"


$playerSpriteFamily[$localPlayer][$up] = _GDIPlus_ImageLoadFromFile($spriteFolder & $playerSpriteFamily[$localPlayer][$family] & "/up.png")
$playerSpriteFamily[$localPlayer][$down] = _GDIPlus_ImageLoadFromFile($spriteFolder & $playerSpriteFamily[$localPlayer][$family] & "/down.png")
$playerSpriteFamily[$localPlayer][$left] = _GDIPlus_ImageLoadFromFile($spriteFolder & $playerSpriteFamily[$localPlayer][$family] & "/left.png")
$playerSpriteFamily[$localPlayer][$right] = _GDIPlus_ImageLoadFromFile($spriteFolder & $playerSpriteFamily[$localPlayer][$family] & "/right.png")

;create background
dim $backgroundHandle   = $player[$localPlayer][$map]
updateBackground()

parseObjects()
updateObjects()

;draw player first time
drawPlayer("down")


while 1
    checkKeyPressed()
    sleep(25)
WEnd

func parseObjects($map = $currentMap)
    for $obj = 1 to 100
        $temp = Iniread($mapFolder & $currentMap & ".obj", "objects", $obj, "nothing") 
        if $temp = "nothing" then 
            $objects[0][0] = $obj
            exitloop
        EndIf
        $temp = stringsplit($temp, ":")
        $objects[$obj][$objectPath] = $temp[1]
        $objects[$obj][$objectX]    = $temp[2]
        $objects[$obj][$objectY]    = $temp[3]
        $objects[$obj][$objectHandle] = _GDIPlus_ImageLoadFromFile($mapFolder & $temp[1])
    Next
EndFunc

#cs not needed anymore
func updatePlayerLocationOnScreen($playerId, $newPosX, $newPosY)
    $player[$playerId][$posX]       = $newPosX
    $player[$playerId][$posY]       = $newPosY
    $player[$playerId][$handle]     = _GDIPlus_GraphicsDrawImage($onGui, $player[$playerId][$sprite], $player[$playerId][$posX], $player[$playerId][$posY])
EndFunc
#ce

func drawPlayer($direction, $playerId = $localPlayer, $newPosX = 395, $newPosY = 395)
    switch $direction
        case "up"
            $player[$playerId][$handle]     = _GDIPlus_GraphicsDrawImage($onGui, $playerSpriteFamily[$playerId][$up], $player[$playerId][$posX], $player[$playerId][$posY])
        case "down"
            $player[$playerId][$handle]     = _GDIPlus_GraphicsDrawImage($onGui, $playerSpriteFamily[$playerId][$down], $player[$playerId][$posX], $player[$playerId][$posY])
        case "left"
            $player[$playerId][$handle]     = _GDIPlus_GraphicsDrawImage($onGui, $playerSpriteFamily[$playerId][$left], $player[$playerId][$posX], $player[$playerId][$posY])
        case "right"
            $player[$playerId][$handle]     = _GDIPlus_GraphicsDrawImage($onGui, $playerSpriteFamily[$playerId][$right], $player[$playerId][$posX], $player[$playerId][$posY])
    endswitch
EndFunc

func updateBackground()
    $background = _gdiplus_graphicsdrawimage($onGui, $backgroundHandle,  (0 - $player[$localPlayer][$mapX]), (0 - $player[$localPlayer][$mapY]))
EndFunc


func updateObjects()
    for $obj = 1 to $objects[0][0]
        if ($player[$localPlayer][$mapX] - $objects[$obj][$objectX]) < 400 And ($player[$localPlayer][$mapX] - $objects[$obj][$objectX]) > -400 then
            if ($player[$localPlayer][$mapY] - $objects[$obj][$objectY]) < 400 And ($player[$localPlayer][$mapY] - $objects[$obj][$objectY]) > -400 then
                $differenceX = $player[$localPlayer][$mapX] - $objects[$obj][$objectX]
                $differenceY = $player[$localPlayer][$mapY] - $objects[$obj][$objectY]
                _GDIPlus_GraphicsDrawImage($onGui, $objects[$obj][$objectHandle], $player[$localPlayer][$posX] - $differenceX, $player[$localPlayer][$posY] - $differenceY)
            EndIf
        EndIf
    next
EndFunc

func movePlayer($shiftX, $shiftY, $speed = 1, $direction = "up")
    $player[$localPlayer][$mapX] = $player[$localPlayer][$mapX] + ($shiftX * $speed)
    $player[$localPlayer][$mapY] = $player[$localPlayer][$mapY] + ($shiftY * $speed)
    
    updateBackground()
    drawPlayer($direction)
    updateObjects()
EndFunc

game.rar

Edited by Drchubby
Link to comment
Share on other sites

the flickering is to be expected with GDI im adraid, no way to avoid it =(

Not true in any way. Did you try one of my GDI+ Scripts? Did they flicker? :)

The secret is double buffering. The technique is based around the idea that you don't draw to the screen until everything is completed. Here's a template which use double buffering.

#include <GUIConstants.au3>
#include <GDIplus.au3>

Global Const $width=600
Global Const $height=400
GLobal $title="GDI+"

; Build your GUI here
Opt("GUIOnEventMode",1)
$hwnd=GUICreate($title,$width,$height)
GUISetOnEvent($GUI_EVENT_CLOSE,"close")
GUISetState()

; Load your GDI+ resources here:
_GDIPlus_Startup()
$graphics=_GDIPlus_GraphicsCreateFromHWND($hwnd)
$bitmap=_GDIPlus_BitmapCreateFromGraphics($width,$height,$graphics)
$backbuffer=_GDIPlus_ImageGetGraphicsContext($bitmap)


Do
    _GDIPlus_GraphicsClear($backbuffer)
    
   ; Draw your GDI+ scene onto $backbuffer here
    
    _GDIPlus_GraphicsDrawImageRect($graphics,$bitmap,0,0,$width,$height)
    Sleep(10)
Until False







Func close()
    _GDIPlus_GraphicsDispose($backbuffer)
    _GDIPlus_BitmapDispose($bitmap)
    _GDIPlus_GraphicsDispose($graphics)
    _GDIPlus_Shutdown()
    Exit
EndFunc

:)

Broken link? PM me and I'll send you the file!

Link to comment
Share on other sites

Not true in any way. Did you try one of my GDI+ Scripts? Did they flicker? :)

The secret is double buffering. The technique is based around the idea that you don't draw to the screen until everything is completed. Here's a template which use double buffering.

thnx this helped allot, I was thinking about drawing everything at the last step as well.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...