Sign in to follow this  
Followers 0
Diablo

Flicker problem

3 posts in this topic

Hey guys, this is a card game of mine..well, part of it, it aint done yet~

The screen flickers whenever you choose a card from the bottom row and is really irritating, i wonder if anyone have a solution to this problem

Partially done card game ->

CODE
#include <GUIConstants.au3>

#include <Array.au3>

Opt("MouseCoordMode", 2)

$Form1 = GUICreate("Big Two", 560, 500, 189, 179, -1, -1)

GUISetBkColor(0x008000)

GUISetState(@SW_SHOW)

;===================DLL STUFF==================

Global $hDLL = DllOpen("cards.dll")

Global $hdc = DllCall("user32.dll", "int", "GetDC", "hwnd", $Form1)

DllCall($hDLL, "int", "cdtInit", "int_ptr", 0, "int_ptr", 0)

$hdc = $hdc[0]

; create memory DC with size = size of client area of main window

Global $client_rect = WinGetClientSize($Form1)

Global $memory_dc = _CreateCompatibleDC($hdc)

Global $memory_bm = _CreateCompatibleBitmap($hdc, $client_rect[0], $client_rect[1])

_SelectObject($memory_dc, $memory_bm)

Global $WM_PAINT = 0x000F

;When window is not in sight call 'MY_WM_PAINT'

GUIRegisterMsg($WM_PAINT, 'MY_WM_PAINT')

;==============================================

Global $x = 0

Global $y = 0

Global $z = 2

Global $arrange = 0

Global $bgPaint = 0

Global $OneNoOfCards = 13

Global $TwoNoOfCards = 13

Global $ThreeNoOfCards = 13

Global $FourNoOfCards = 13

Global $OneXcoord = 150

Global $TwoYcoord = 285

Global $ThreeYcoord = 285

Global $FourXcoord = 150

Global $OneCard[13]

Global $TwoCard[13]

Global $ThreeCard[13]

Global $FourCard[13]

Global $FourHand[13][2]

Global $UsedCards[1]

Global $CardStatus[14]

;Draw cards

;Four cards to be optional

Do

$value = Random(1, 13, 1)

$shape = Random(0, 3, 1)

$drawCard = $value * 4 - 1 - $shape

$Arraysearch = _ArraySearch($UsedCards, $drawCard)

If $Arraysearch = -1 Then

$FourHand[$x][0] = $value

$FourHand[$x][1] = $shape

_ArrayInsert($UsedCards, UBound($UsedCards), $drawCard)

Else

$x = $x - 1

EndIf

$x = $x + 1

Until ($x = 13)

$x = 0

;=============Arrange Hand===========================================

_ArraySort($FourHand, 0, 0, 0, 2)

_SortHand($FourHand)

;====================================================================

While 1

$msg = GUIGetMsg()

Select

Case $msg = $GUI_EVENT_CLOSE

ExitLoop

Case $msg = $GUI_EVENT_PRIMARYDOWN

$pos = MouseGetPos()

If $pos[1] > 380 Then

If $pos[1] < 476 Then

If $pos[0] > 150 Then

Do

If $pos[0] < 150 + 15 * 1 Then

If $CardStatus[1] = 1 Then

$CardStatus[1] = 0

Else

$CardStatus[1] = 1

EndIf

$y = $FourNoOfCards - 2

ElseIf $pos[0] < 150 + 15 * $z Then

If $CardStatus[$z] = 1 Then

$CardStatus[$z] = 0

Else

$CardStatus[$z] = 1

EndIf

$y = $FourNoOfCards - 2

EndIf

If $z = $FourNoOfCards Then

If $pos[0] < 150 + 15 * $FourNoOfCards + 58 Then

If $CardStatus[$FourNoOfCards] = 1 Then

$CardStatus[$FourNoOfCards] = 0

Else

$CardStatus[$FourNoOfCards] = 1

EndIf

$y = $FourNoOfCards - 2

EndIf

EndIf

$y = $y + 1

$z = $z + 1

Until ($y = $FourNoOfCards - 1)

$y = 0

$z = 2

DllCall("user32.dll", "int", "InvalidateRect", "hwnd", $Form1, "int", 0, "int", 1)

_RePaint()

EndIf

EndIf

EndIf

EndSelect

;length of card = 70

;length of all cards = 70 + 15 * 12 ; range = from 150

;height of card = 95 ; range = 380 - 476

WEnd

Func _RePaint()

; copy content of Window DC to memory DC

_BitBlt($memory_dc, 0, 0, $client_rect[0], $client_rect[1], $hdc, 0, 0)

;possible background 54,55,58,59,61,62,63

;Top Player

Do

DllCall($hDLL, "int", "cdtDraw", "int", $memory_dc, "int", $OneXcoord, "int", 20, "int", 58, "int", 1, "int", 0xffffff)

$bgPaint = $bgPaint + 1

$OneXcoord = $OneXcoord + 15

Until ($bgPaint = $OneNoOfCards)

$OneXcoord = 150

$bgPaint = 0

;Right Player

Do

DllCall($hDLL, "int", "cdtDraw", "int", $memory_dc, "int", 465, "int", $TwoYcoord, "int", 58, "int", 1, "int", 0xffffff)

$bgPaint = $bgPaint + 1

$TwoYcoord = $TwoYcoord - 15

Until ($bgPaint = $TwoNoOfCards)

$bgPaint = 0

$TwoYcoord = 285

;Left Player

Do

DllCall($hDLL, "int", "cdtDraw", "int", $memory_dc, "int", 20, "int", $ThreeYcoord, "int", 58, "int", 1, "int", 0xffffff)

$bgPaint = $bgPaint + 1

$ThreeYcoord = $ThreeYcoord - 15

Until ($bgPaint = $ThreeNoOfCards)

$bgPaint = 0

$ThreeYcoord = 285

;Bottom Player

$x = 0

Do

$drawCard = $FourHand[$x][0] * 4 - 1 - $FourHand[$x][1]

If $CardStatus[$x + 1] = 1 Then

DllCall($hDLL, "int", "cdtDraw", "int", $memory_dc, "int", $FourXcoord, "int", 365, "int", $drawCard, "int", 0, "int", 0xffffff)

Else

DllCall($hDLL, "int", "cdtDraw", "int", $memory_dc, "int", $FourXcoord, "int", 380, "int", $drawCard, "int", 0, "int", 0xffffff)

EndIf

$bgPaint = $bgPaint + 1

$FourXcoord = $FourXcoord + 15

$x = $x + 1

Until ($bgPaint = $FourNoOfCards)

$bgPaint = 0

$FourXcoord = 150

$x = 0

; copy changed content of memory DC back to Window DC

_BitBlt($hdc, 0, 0, $client_rect[0], $client_rect[1], $memory_dc, 0, 0)

EndFunc ;==>_RePaint

Func MY_WM_PAINT($hwnd, $msg, $wparam, $lparam)

_RePaint()

Return $GUI_RUNDEFMSG

EndFunc ;==>MY_WM_PAINT

Func _CreateCompatibleDC($hdc, $dll_h = 'gdi32.dll')

;~ http://msdn.microsoft.com/library/en-us/gdi/devcons_499f.asp

Local $ret = DllCall($dll_h, 'ptr', 'CreateCompatibleDC', 'ptr', $hdc)

Return $ret[0]

EndFunc ;==>_CreateCompatibleDC

Func _CreateCompatibleBitmap($hdc, $w, $h, $dll_h = 'Gdi32.dll')

;~ http://msdn.microsoft.com/library/en-us/gdi/bitmaps_1cxc.asp

Local $ret = DllCall($dll_h, 'ptr', 'CreateCompatibleBitmap', _

'ptr', $hdc, _

'int', $w, _

'int', $h)

Return $ret[0]

EndFunc ;==>_CreateCompatibleBitmap

Func _SelectObject($hdc, $hgdiobj, $dll_h = 'Gdi32.dll')

;~ http://msdn.microsoft.com/library/en-us/gdi/devcons_9v3o.asp

Local $ret = DllCall($dll_h, 'hwnd', 'SelectObject', 'ptr', $hdc, 'hwnd', $hgdiobj)

Return $ret[0]

EndFunc ;==>_SelectObject

Func _BitBlt($destination_hdc, $destination_x, $destination_y, $destination_width, $destination_height, _

$source_hdc, $source_x, $source_y, $code = 0xCC0020, $dll_h = 'Gdi32.dll')

;~ Const $SRCCOPY = 0xCC0020

;~ http://msdn.microsoft.com/library/en-us/gdi/bitmaps_0fzo.asp

Local $ret = DllCall($dll_h, 'int', 'BitBlt', _

'ptr', $destination_hdc, _

'int', $destination_x, _

'int', $destination_y, _

'int', $destination_width, _

'int', $destination_height, _

'ptr', $source_hdc, _

'int', $source_x, _

'int', $source_y, _

'int', $code)

Return $ret[0]

EndFunc ;==>_BitBlt

Func _DeleteObject($hObject, $dll_h = 'Gdi32.dll')

;~ http://msdn.microsoft.com/library/en-us/gdi/devcons_1vsk.asp

Local $ret = DllCall($dll_h, 'int', 'DeleteObject', 'hwnd', $hObject)

Return $ret[0]

EndFunc ;==>_DeleteObject

Func _DeleteDC($hdc, $dll_h = 'Gdi32.dll')

;~ http://msdn.microsoft.com/library/en-us/gdi/devcons_2p2b.asp

Local $ret = DllCall($dll_h, 'int', 'DeleteDC', 'ptr', $hdc)

Return $ret[0]

EndFunc ;==>_DeleteDC

Func OnAutoItExit()

DllCall($hDLL, "none", "cdtTerm")

DllClose($hDLL)

DllCall("user32.dll", "int", "ReleaseDC", "hwnd", $Form1, "int", $hdc)

_DeleteObject($memory_bm)

_DeleteDC($memory_dc)

EndFunc ;==>OnAutoItExit

Func _SortHand(ByRef $Hand)

;$FourHand[$x][0] = $value

;$FourHand[$x][1] = $shape

Do

If $Hand[$y][0] = $Hand[$y + 1][0] Then

If $Hand[$y][0] = $Hand[$y + 2][0] Then

If $Hand[$y][0] = $Hand[$y + 3][0] Then

;Check four cards

$Hand[$y][1] = 2

$Hand[$y + 1][1] = 3

$Hand[$y + 2][1] = 1

$Hand[$y + 3][1] = 0

Else

;Check three cards

$check = $Hand[$y][1] + $Hand[$y + 1][1] + $Hand[$y + 2][1]

;$check = 6 - $check

Select

Case $check = 6

$Hand[$y][1] = 2

$Hand[$y + 1][1] = 3

$Hand[$y + 2][1] = 1

Case $check = 5

$Hand[$y][1] = 2

$Hand[$y + 1][1] = 3

$Hand[$y + 2][1] = 0

Case $check = 4

$Hand[$y][1] = 3

$Hand[$y + 1][1] = 1

$Hand[$y + 2][1] = 0

Case $check = 3

$Hand[$y][1] = 2

$Hand[$y + 1][1] = 1

$Hand[$y + 2][1] = 0

EndSelect

EndIf

Else

;Check two cards

If $Hand[$y][1] + $Hand[$y + 1][1] = 5 Then

$Hand[$y][1] = 2

$Hand[$y + 1][1] = 3

ElseIf $Hand[$y][1] < $Hand[$y + 1][1] Then

_ArraySwap($Hand[$y][1], $Hand[$y + 1][1])

EndIf

EndIf

EndIf

$y = $y + 1

Until ($y = 10)

$y = 0

If $Hand[12][0] <> $Hand[9][0] Then

If $Hand[12][0] = $Hand[10][0] Then

$check = $Hand[12][1] + $Hand[11][1] + $Hand[10][1]

Select

Case $check = 6

$Hand[10][1] = 2

$Hand[11][1] = 3

$Hand[12][1] = 1

Case $check = 5

$Hand[10][1] = 2

$Hand[11][1] = 3

$Hand[12][1] = 0

Case $check = 4

$Hand[10][1] = 3

$Hand[11][1] = 1

$Hand[12][1] = 0

Case $check = 3

$Hand[10][1] = 2

$Hand[11][1] = 1

$Hand[12][1] = 0

EndSelect

EndIf

EndIf

DllCall("user32.dll", "int", "InvalidateRect", "hwnd", $Form1, "int", 0, "int", 1)

_RePaint()

Return

EndFunc ;==>_SortHand

Thanks in advance :whistle:

Share this post


Link to post
Share on other sites



like in allegro c++, you must create a buffer first. In other words, a blank bitmap of full screen size. After that, you must create the desired picture bitmap onto the buffer. In a while loop, you clear the buffer and the screen does not flicker. In C++ it would kind of look like :

BITMAP *buffer, *pic;

buffer = create_bitmap(SCREEN_H, SCREEN_W);

pic = load_bitmap("thing.bmp",NULL);

blit(buffer, pic, 0,0,0,0,pic->w,pic->h);

blit(screen, buffer,0,0,0,0,SCREEN_W,SCREEN_H);

hope this sort of helps...If you've used allegro for c++, then you'll know what i'm talking about.


F@m!ly Guy Fr33k! - Avatar speaks for itself__________________________________________________________________________________________ite quotes... - Is your refrigerator running? If it is, It probably runs like you...very homosexually - Christians don't believe in gravity - Geeze Brian where do you think you are, Payless?- Show me potato Salad!__________________________________________________________________________________________Programs available - Shutdown timer[indent][/indent]

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

As eynstyne said.

follow these changes:

- remove InvalidateRect calls (this cause flickering)

- in _RePaint() first clear all memorydc buffer area

- in _RePaint() you must paint ALL objects to memorydc buffer (also buttons and other standard windows objects created with GUICtrCreate..)

or make some smaller areas for BitBlt copy between window DC and memory DC covering only your changing cards so

you will have to paint only your cards and not other standard window controls

Edited by Zedna

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  
Followers 0