Jump to content

Lightning or Shader in GDI+?


Recommended Posts

Is it anyhow possible, to do some lightning or shader effects in gdi+? Like overlaying some alpha blended figures above shapes?

Won't this lead to slow FPS? Which leads to the next question: Is it possible to tweak GDI+ to the maximum? Pure Autoit?

Link to comment
Share on other sites

Something like this here?

#include <GuiSlider.au3>
#include <WindowsConstants.au3>
#include <ScreenCapture.au3>

Opt("GUIOnEventMode", 1)

_GDIPlus_Startup()
Global Const $hBgImage = _GDIPlus_ImageLoadFromFile(StringReplace(@AutoItExe, "autoit3.exe", "ExamplesGUImsoobe.jpg"))
Global Const $iW = _GDIPlus_ImageGetWidth($hBgImage)
Global Const $iH = _GDIPlus_ImageGetHeight($hBgImage)

Global Const $hHBmp = _ScreenCapture_Capture("", 0, 0, 500, 500)
Global Const $hFgBitmap = _GDIPlus_BitmapCreateFromHBITMAP($hHBmp)
_WinAPI_DeleteObject($hHBmp)

Global Const $hGUI = GUICreate("GDI+ Test by UEZ 2012", 800, 600)
GUISetState(@SW_SHOW, $hGUI)

Global Const $hGraphic = _GDIPlus_GraphicsCreateFromHWND($hGUI)
Global Const $hBitmap = _GDIPlus_BitmapCreateFromGraphics($iW, $iH, $hGraphic)
Global Const $hContext = _GDIPlus_ImageGetGraphicsContext($hBitmap)
Global Const $hBrush = _GDIPlus_BrushCreateSolid(0xFFFFFFFF)

Global Const $tagGDIPCOLORMATRIX = "float m[25];"
Global $hAttribute_Alpha = _GDIPlus_ImageAttributesCreate()
Global $tColorMatrix = _GDIPlus_ColorMatrixCreateTranslate(0, 0, 0, 0)
Global $pColorMatrix = DllStructGetPtr($tColorMatrix)
_GDIPlus_ImageAttributesSetColorMatrix($hAttribute_Alpha, 0, True, $pColorMatrix)

Global Const $aWinPos = WinGetPos($hGUI)
Global Const $hGUI_Child = GUICreate("Transparency Settings", 512, 85, $aWinPos[0], $aWinPos[1] + $aWinPos[3] + 10, Default, Default, $hGUI)
Global Const $idGroup = GUICtrlCreateGroup("Transparency", 8, 8, 497, 65)
Global Const $idSlider = GUICtrlCreateSlider(16, 24, 478, 45, BitOR($GUI_SS_DEFAULT_SLIDER, $TBS_BOTH, $TBS_ENABLESELRANGE, $WS_TABSTOP, $TBS_TOOLTIPS))
Global Const $hSlider  = GUICtrlGetHandle($idSlider)
_GUICtrlSlider_SetRangeMin($hSlider, 0)
_GUICtrlSlider_SetRangeMax($hSlider, 100)
_GUICtrlSlider_SetPos($hSlider, 30)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW, $hGUI_Child)

GUISetOnEvent(-3, "_Exit")
Set_Transparency()

GUIRegisterMsg($WM_HSCROLL, "Set_Transparency")
GUIRegisterMsg($WM_PAINT, "ReDrawScreen")


While Sleep(2^16)
WEnd

Func Set_Transparency()
    Local $fTransparency =  (GUICtrlRead($idSlider) / 100) * -1
    $tColorMatrix = _GDIPlus_ColorMatrixCreateTranslate(0, 0, 0, $fTransparency)
    $pColorMatrix = DllStructGetPtr($tColorMatrix)
    _GDIPlus_ImageAttributesSetColorMatrix($hAttribute_Alpha, 0, True, $pColorMatrix)
    Draw()
    Return "GUI_RUNDEFMSG"
EndFunc

Func ReDrawScreen()
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 0, 0, $iW, $iH)
    Return "GUI_RUNDEFMSG"
EndFunc

Func Draw()
    _GDIPlus_GraphicsDrawImage($hContext, $hBgImage, 0, 0)
    _GDIPlus_GraphicsDrawString($hContext, "Coded by UEZ", 300, 280, "Arial", 20)
    _GDIPlus_GraphicsDrawImageRectRectIA($hContext, $hFgBitmap, 0, 0, $iW, $iH, 150, 50, $iW, $iH, $hAttribute_Alpha)
    _GDIPlus_GraphicsDrawImageRect($hGraphic, $hBitmap, 0, 0, $iW, $iH)
EndFunc

Func _Exit()
    GUIRegisterMsg($WM_HSCROLL, "")
    GUIRegisterMsg($WM_PAINT, "")
    _GDIPlus_ImageAttributesDispose($hAttribute_Alpha)
    _GDIPlus_ImageDispose($hBgImage)
    _GDIPlus_BitmapDispose($hFgBitmap)
    _GDIPlus_GraphicsDispose($hContext)
    _GDIPlus_BitmapDispose ($hBitmap)
    _GDIPlus_GraphicsDispose($hGraphic)
    _GDIPlus_Shutdown()
    GUIDelete($hGUI_Child)
    GUIDelete($hGUI)
    Exit
EndFunc

Func _GDIPlus_ColorMatrixCreate()
    Return _GDIPlus_ColorMatrixCreateScale(1, 1, 1, 1)
EndFunc   ;==>_GDIPlus_ColorMatrixCreate

Func _GDIPlus_ColorMatrixCreateScale($nRed, $nGreen, $nBlue, $nAlpha = 1)
    Local $tCM
    $tCM = DllStructCreate($tagGDIPCOLORMATRIX)
    DllStructSetData($tCM, "m", $nRed, 1)
    DllStructSetData($tCM, "m", $nGreen, 7)
    DllStructSetData($tCM, "m", $nBlue, 13)
    DllStructSetData($tCM, "m", $nAlpha, 19)
    DllStructSetData($tCM, "m", 1, 25)
    Return $tCM
EndFunc   ;==>_GDIPlus_ColorMatrixCreateScale

Func _GDIPlus_ColorMatrixCreateTranslate($nRed, $nGreen, $nBlue, $nAlpha = 0)
    Local $iI, $tCM, $aFactors[4] = [$nRed, $nGreen, $nBlue, $nAlpha]
    $tCM = _GDIPlus_ColorMatrixCreate()
    For $iI = 0 To 3
        DllStructSetData($tCM, "m", $aFactors[$iI], 21 + $iI)
    Next
    Return $tCM
EndFunc   ;==>_GDIPlus_ColorMatrixCreateTranslate

Func _GDIPlus_GraphicsDrawImageRectRectIA($hGraphics, $hImage, $nSrcX, $nSrcY, $nSrcWidth, $nSrcHeight, $nDstX, $nDstY, $nDstWidth, $nDstHeight, $hImageAttributes = 0, $iUnit = 2)
    Local $aResult = DllCall($ghGDIPDll, "int", "GdipDrawImageRectRect", "handle", $hGraphics, "handle", $hImage, "float", $nDstX, "float", $nDstY, "float", $nDstWidth, "float", $nDstHeight, "float", $nSrcX, "float", $nSrcY, "float", $nSrcWidth, "float", $nSrcHeight, "int", $iUnit, "handle", $hImageAttributes, "int", 0, "int", 0)
    If @error Then Return SetError(@error, @extended, False)
    Return $aResult[0] = 0
EndFunc   ;==>_GDIPlus_GraphicsDrawImageRectRectIA

Func _GDIPlus_ImageAttributesCreate()
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipCreateImageAttributes", "int*", 0)
    If @error Then Return SetError(@error, @extended, 0)
    Return $aResult[1]
EndFunc   ;==>_GDIPlus_ImageAttributesCreate

Func _GDIPlus_ImageAttributesDispose($hImageAttributes)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipDisposeImageAttributes", "handle", $hImageAttributes)
    If @error Then Return SetError(@error, @extended, False)
    Return $aResult[0] = 0
EndFunc   ;==>_GDIPlus_ImageAttributesDispose

Func _GDIPlus_ImageAttributesSetColorMatrix($hImageAttributes, $iColorAdjustType = 0, $fEnable = False, $pClrMatrix = 0, $pGrayMatrix = 0, $iColorMatrixFlags = 0)
    Local $aResult = DllCall($ghGDIPDll, "uint", "GdipSetImageAttributesColorMatrix", "handle", $hImageAttributes, "int", $iColorAdjustType, "int", $fEnable, "ptr", $pClrMatrix, "ptr", $pGrayMatrix, "int", $iColorMatrixFlags)
    If @error Then Return SetError(@error, @extended, False)
    Return $aResult[0] = 0
EndFunc   ;==>_GDIPlus_ImageAttributesSetColorMatrix

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Thats not the case for me, it shows a Windows Vista background and on it is a screen-capture (whose transparency changes according to slider). @jWalker, which OS are you using?

----------------------------------------

:bye: Hey there, was I helpful?

----------------------------------------

My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1

Link to comment
Share on other sites

Probably the issue is with the path to the background image.

Just replace the line with a path to an image.

Global Const $hBgImage = _GDIPlus_ImageLoadFromFile(<path to an image>)

Tested on Win7 x64 Aero on and WinXP x86 (vm).

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

I did as you said the first time i didnt see anything. The picture is in the named folder and all i see is nothing.

I use Win7 x64 too!

Well, i tried to do some basic 2D dynamic lightning effects. Like just some circles on top of each other. They become more transparent the bigger they are.

This is good for like 1 torch. But i want to make a game where there are lets say 10 torches and background and a player and enemies... This would brake the performance as hell i think.

Can one simply rise performance?

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...