Jump to content

GDI+ help! I'm completely lost!


 Share

Recommended Posts

This is my script :

#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_compression=4
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <string.au3>
#include <GDIPlus.au3>
#include <array.au3>

Const $GUI_RUNDEFMSG = 'GUI_RUNDEFMSG'
Const $GUI_EVENT_CLOSE = -3
Const $WM_LBUTTONDOWN = 0x201
Const $WM_PAINT = 0x000F
Const $WS_POPUP = 0x80000000
Const $C_BLACK = 0xFF111111
Const $C_GREY = 0xFFAAAAAA
Const $C_TEXT = 0xFF009FAF

Const $borderSize = 3
Const $separatorSize= 2 
Const $guiWidth = 1224+2*$borderSize+1*$separatorSize, $guiHeight = 720+2*$borderSize, $rPanelWidth = 200+2*$borderSize
Const $geoxOffset = 19
Const $geoyOffset = 18
Const $geoUnit = 32768

Const $font = "Arial"
Const $delimiter = "#"
Const $edgeSize = 1

Dim $xx, $yy, $field_id, $id, $species_population, $total = 1, $countspecies = 0, $count = 0, $i = 0, $namelist, $aggrolist
Dim $geox = 19, $geoy = 22
Dim $i = 0
Dim $max, $previd = 1, $c = 0



Dim $IMG_handle1, $hwnd, $hpen, $hbitmap, $IMG_handle2
Dim $color, $prevcolor

GUIRegisterMsg($WM_LBUTTONDOWN, "_WinMove")
GUIRegisterMsg($WM_PAINT, "_WinPaint")

$oconn = _MySQLConnect("root", "root", "localhost", "omfg", 3306, "{MySQL ODBC 5.1 Driver}")
$result = _MySQLExec($oconn, "SELECT s.npc_templateid, s.locx, s.locy FROM omfg.spawnlist s WHERE locx >= -32768 AND locy >= 131072  AND locx < 0 AND locy < 163840  ORDER BY npc_templateid")
If Not @error Then

    With $result
        While Not .EOF; is equal to "Do this until we run out of records"

            ;Write the value for every column
            For $X In .Fields
                Switch $field_id
                    Case 0
                        $count += 1
                        If Not ($X.value = $previd) Then
                            $prevcolor = "FF" & Hex(Random(55, 255), 2) & Hex(Random(55, 200), 2) & Hex(Random(55, 200), 2)
                            $color &= $prevcolor
                            $previd = $X.value
                            If Not ($total = 1) Then
                                $species_population &= $count & @CRLF
                                $count = Reset()
                                $id &= $previd & $delimiter
                            Else
                                $id &= $previd & $delimiter
                            EndIf
                            $countspecies += 1
                        Else
                            $color &= $prevcolor
                        EndIf
                    Case 1
                        $xx &= $geoUnit - (Abs($X.value) - ($geoUnit * Abs($geox - $geoxOffset)))
                    Case 2
                        $yy &= (Abs($X.value) - ($geoUnit * Abs($geoy - $geoyOffset)))
                EndSwitch

                $field_id += 1

            Next
            .MoveNext;Move to the next row

            $total += 1
            $xx &= $delimiter
            $yy &= $delimiter
            $color &= $delimiter

            $field_id = Reset()
        WEnd
        $count += 1
        $species_population &= $count & @CRLF
        $xx = _StringSplit($xx, $delimiter)
        $yy = _StringSplit($yy, $delimiter)
        $color = _StringSplit($color, $delimiter)
        $id = _StringSplit($id, $delimiter)
    EndWith
Else
    MsgBox(0, "Error", "Something Went Wrong!")
EndIf

Dim $result2[$countspecies], $result3[$countspecies]

While Not ($i = $countspecies)
    $result2[$i] = _MySQLExec($oconn, "SELECT name FROM omfg.npc WHERE id =" & $id[$i] & ";")
    With $result2[$i]
        For $Y In .Fields
            $namelist &= $Y.value & @CRLF
        Next
    EndWith
    $result3[$i] = _MySQLExec($oconn, "SELECT aggro FROM omfg.npc WHERE id =" & $id[$i] & ";")
    With $result3[$i]
        For $Z In .Fields
            If $Z.value = 0 Then
                $aggrolist &= @CRLF
            Else
                $aggrolist &= "*" & @CRLF
            EndIf
        Next
    EndWith
    $i += 1
WEnd

_MySQLEnd($oconn)

_GDIPlus_Startup()
$hGui = GUICreate("Spawn Map", $guiWidth, $guiHeight, -1, -1, $WS_POPUP, 0)
GUISetBkColor($C_BLACK, $hGui)

$IMG_handle1 = _GDIPlus_GraphicsCreateFromHWND($hGui)
$hbitmap = _GDIPlus_BitmapCreateFromGraphics($guiWidth, $guiHeight, $IMG_handle1)
$IMG_handle2 = _GDIPlus_ImageGetGraphicsContext($hbitmap)
$hpen = _GDIPlus_PenCreate(0x00000000, 4)

GUISetState()

DrawCross($IMG_handle2 ,$xx, $yy, $total, $color, $hPen)

DrawLine($IMG_handle2,  $guiWidth - $rPanelWidth        ,   0                   ,   $guiWidth - $rPanelWidth        ,   $guiHeight,         2,      $C_GREY,    $hpen)
DrawLine($IMG_handle2,  $guiWidth - ($rPanelWidth - 132),   $guiHeight - 19     ,   $guiWidth - $rPanelWidth + 132  ,   $guiHeight - 3,     2,      $C_GREY,    $hpen)
DrawLine($IMG_handle2,  $guiWidth - ($rPanelWidth - 132),   $guiHeight - 18     ,   $guiWidth - 3                   ,   $guiHeight - 18,    2,      $C_GREY,    $hpen)

DrawText($IMG_handle2, "Name"               , $C_TEXT   , $font , 8, 4, $guiWidth - ($rPanelWidth - 22) ,   8                   ,   150,    16)
DrawText($IMG_handle2, $namelist            , $C_TEXT   , $font , 8, 0, $guiWidth - ($rPanelWidth - 8)  ,   21                  ,   150,    600)
DrawText($IMG_handle2, "Count"              , $C_TEXT   , $font , 8, 4, $guiWidth - ($rPanelWidth - 160),   8                   ,   44,     600)
DrawText($IMG_handle2, $species_population  , $C_TEXT   , $font , 8, 0, $guiWidth - ($rPanelWidth - 169),   22                  ,   44,     600)
DrawText($IMG_handle2, $aggrolist           , $C_TEXT   , $font , 8, 0, $guiWidth - ($rPanelWidth - 3)  ,   21                  ,   44,     600)
DrawText($IMG_handle2, "Total = " & $total  , 0xFF009899, $font , 8, 0, $guiWidth - ($rPanelWidth - 134),   ($guiHeight - 17)   ,   150,    16)

DrawBorder($IMG_handle2, 1, 1, $guiWidth-($separatorSize+1), $guiHeight-($separatorSize+1), $borderSize, $C_GREY, $hpen)

_GDIPlus_GraphicsDrawImage($IMG_handle1, $hbitmap, 0, 0)
HotKeySet("^s", "SaveImage")

Do

    
Until GUIGetMsg() = $GUI_EVENT_CLOSE

_WinAPI_DeleteObject($hbitmap)
_GDIPlus_PenDispose($hpen)
_GDIPlus_GraphicsDispose($IMG_handle1)
_GDIPlus_GraphicsDispose($IMG_handle2)
_GDIPlus_Shutdown()

Func DrawBorder($Graphics, $XOffset, $YOffset, $Width, $Height, $BorderSize, $Color, $Pen)
    _GDIPlus_PenSetWidth($Pen, $BorderSize)
    _GDIPlus_PenSetColor($Pen, $Color)
    _GDIPlus_GraphicsDrawRect($Graphics, 1, 1, $Width, $Height, $Pen)
EndFunc

Func DrawLine($Graphics, $X1, $Y1, $X2, $Y2, $Width, $color, $Pen)
    _GDIPlus_PenSetWidth($hpen, $Width)
    _GDIPlus_PenSetColor($hpen, $color)
    _GDIPlus_GraphicsDrawLine($Graphics, $X1, $Y1, $X2, $Y2, $Pen)
EndFunc   ;==>DrawLine

Func DrawCross($Graphics ,$VarX, $varY, $count, $Color, $Pen)
    local $drawx[$count], $drawy[$count], $i=0
        While ($i<$count)
            $drawx[$i] = Floor(BitShift($xx[$i], 5)) + $borderSize
            $drawy[$i] = Floor(($varY[$i]) / 45.5) + $borderSize
            _GDIPlus_PenSetColor($Pen, "0x" & $Color[$i])
            _GDIPlus_GraphicsDrawLine($Graphics,    $drawx[$i]-$edgeSize,   $drawy[$i]          ,   $drawx[$i]+$edgeSize,   $drawy[$i]          ,   $Pen)
            _GDIPlus_GraphicsDrawLine($Graphics,    $drawx[$i]          ,   $drawy[$i]-$edgeSize,   $drawx[$i]          ,   $drawy[$i]+$edgeSize,   $Pen)
        $i+=1
    Wend
EndFunc   ;==>DrawCross

Func DrawText($Graphics, $String, $color, $Font, $Size, $Style, $TxtX, $TxtY, $TxtZoneWidth, $TxtZoneHeight)
    $Brush = _GDIPlus_BrushCreateSolid($color)
    $Format = _GDIPlus_StringFormatCreate()
    $Family = _GDIPlus_FontFamilyCreate($Font)
    $FontObj = _GDIPlus_FontCreate($Family, $Size, $Style)
    $TxtZone = _GDIPlus_RectFCreate($TxtX, $TxtY, $TxtZoneWidth, $TxtZoneHeight)
    $Ainfo = _GDIPlus_GraphicsMeasureString($Graphics, $String, $FontObj, $TxtZone, $Format)
    
    _GDIPlus_GraphicsDrawStringEx($Graphics, $String, $FontObj, $Ainfo[0], $Format, $Brush)
    
    _GDIPlus_BrushDispose($Brush)
    _GDIPlus_StringFormatDispose($Format)
    _GDIPlus_FontFamilyDispose($Family)
    _GDIPlus_FontDispose($FontObj)
EndFunc   ;==>DrawText

Func AddSeparator($String)
    $String &= "#"
    Return ($String)
EndFunc   ;==>AddSeparator

Func _WinPaint($hwnd, $Command, $wParam, $lParam)
    _GDIPlus_GraphicsDrawImage($IMG_handle1, $hbitmap, 0, 0)
EndFunc   ;==>_WinPaint

Func _WinMove($hwnd, $Command, $wParam, $lParam)
    If BitAND(WinGetState($hwnd), 32) Then Return $GUI_RUNDEFMSG
    DllCall("user32.dll", "int", "SendMessage", "hWnd", $hwnd, "int", 0x0112, "int", 0xF009, "int", 0)
EndFunc   ;==>_WinMove

Func _MySQLConnect($sUsername, $sPassword, $sServer, $sDatabase, $iPort = 3306, $sDriver = "{MySQL ODBC 5.1 Driver}")
    Local $v = StringMid($sDriver, 2, StringLen($sDriver) - 2)
    Local $key = "HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers", $val = RegRead($key, $v)
    If @error Or $val = "" Then
        SetError(2)
        Return 0
    EndIf
    $ObjConn = ObjCreate("ADODB.Connection")
    $ObjConn.open("DRIVER=" & $sDriver & ";SERVER=" & $sServer & ";DATABASE=" & $sDatabase & ";UID=" & $sUsername & ";PWD=" & $sPassword & ";PORT=" & $iPort)
    If @error Then
        SetError(1)
        Return 0
    Else
        Return $ObjConn
    EndIf
EndFunc   ;==>_MySQLConnect

Func _MySQLExec($oConnectionObj, $sQuery)
    If Not IsObj($oConnectionObj) Then
        SetError(1)
        Return 0
    Else
        Return $oConnectionObj.Execute($sQuery)
    EndIf
    If @error Then
        SetError(2)
        Return 0
    EndIf
    
EndFunc   ;==>_MySQLExec

Func _MySQLEnd($oConnectionObj)
    If IsObj($oConnectionObj) Then
        $oConnectionObj.close
        Return 1
    Else
        SetError(1)
        Return 0
    EndIf
EndFunc   ;==>_MySQLEnd

Func SaveImage()
    $sCLSID = _GDIPlus_EncodersGetCLSID("BMP")
    _GDIPlus_ImageSaveToFileEx($hbitmap, ".\" & $geox & "_" & $geoy & ".bmp", $sCLSID)
EndFunc   ;==>SaveImage

Func Reset()
    Return 0
EndFunc   ;==>Reset

I'd like to know what I can improve.

Thanks in advance

EDIT : This is the output that I get atm Posted Image

Edited by eleria
Link to comment
Share on other sites

1) post more code to get more help

2) use GUIRegisterMsg & WM_PAINT

3) use _GetDC() to simplify it

3)

_GDIPlus_Startup()
$hGui = GUICreate("Spawn Map", $guiWidth, $guiHeight, -1, -1, 0x80000000, 0)
GUISetBkColor(0xFF000000, $hGui)

$hDC = _WinAPI_GetDC($hGui)

;~ $gdig = _GDIPlus_GraphicsCreateFromHWND($hGui)
;~ $hbitmap = _GDIPlus_BitmapCreateFromGraphics(1240, 720, $gdig)
;~ $hDC = _GDIPlus_ImageGetGraphicsContext($hbitmap)

GUISetState()

$hpen = _GDIPlus_PenCreate(0x000000, 4)
$drawx = Floor(($xx[$cnt]) / 32)
$drawy = Floor(($yy[$cnt]) / 45.5)
_GDIPlus_GraphicsDrawLine($hDC, $drawx - 1, $drawy, $drawx + 1, $drawy, $hpen)
_GDIPlus_GraphicsDrawLine($hDC, $drawx, $drawy - 1, $drawx, $drawy + 1, $hpen)

_GDIPLUS_GRAPHICSDRAWIMAGE($gdig, $hbitmap, 0, 0)
Link to comment
Share on other sites

I can't use_GetDC() to simplify it, or it crashes : /

And anyway I need 2 handles one to write on the other, or else I can't re-calculated stuff during the program execution and add it to the image, can I ?

Edit: modified source to match actual state of dev and a pic for those wondering what It looks like when it's running...

Edited by eleria
Link to comment
Share on other sites

Hi,

Ways to improve your script:

Use formating to make the script easier to follow.

Indenting lines can make things more easier to read and follow.

If it's easier to read and follow, yourself and others may actually spot problems or potential problems easier.

ATM it's a blur of loops and functions and it's just not easy to see where what starts and ends.

I think it's only fair if your going to ask how to improve your code make it at least easy readable by the average person.

Try using includes instead of evil magic numbers when possible while writing your script.

After you have your script where you want it then thin it down and speed it up using evil magic numbers if you need to.

Atm the code you've supplied does not work for me without trying to follow what your code is doing and debugging where it fails.

As for repainting the screen then store the drawn points and data in a variable/array and then loop the stored points/data with appropriate brushes/pens/etc in the WM_PAINT function.

Cheers

Link to comment
Share on other sites

I\'ve post my code in autoit beacons, now it looks much better!

$memDC = _WinAPI_CreatecompatibleDC($hGui)

$memBmp = _WinAPI_CreateCompatibleBitmap($memDC, $guiWidth, $guiHeight)

Gives me an error o_O, and

$hDC = _WinAPI_GetDC($hGui)

$memDC = _WinAPI_CreatecompatibleDC($hDC)

$memBmp = _WinAPI_CreateCompatibleBitmap($memDC, $guiWidth, $guiHeight)

Completely crashes the application

Edited by eleria
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...