Older Version Newer Version

Alyce Alyce Jul 7, 2011

=Another Cards DLL Demo=
[[user:StPendl]]
----
[[cardsDLL|Cards DLL Tutorial]]
----
[[code format="lb"]]
    'Windows Cards.DLL demo by Stefan Pendl
    'based on code by UncleBen, Rod Bird, Alyce Watson and others.

    nomainwin
    'Indices of cards in deck, numbered 0-51:
    clubs = 0     'ace of clubs=0,deuce=4,three=8...king=48
    diamonds = 1  'ace of diamonds=1,deuce=5,three=9...king=49
    hearts = 2    'ace of hearts=2,deuce=6,three=10...king=50
    spades = 3    'ace of spades=3,deuce=7,three=11...king=51
    ace=0         'values increment by 4, which is the number of suits
    deuce=4
    three=8
    four=12
    five=16
    six=20
    seven=24
    eight=28
    nine=32
    ten=36
    jack=40
    queen=44
    king=48

    'formula for card index: cardValue+cardSuit
    'queen of hearts is
    queenHearts = queen+hearts

    hDLL=LoadLibrary("cards32.dll")
    if hDLL<>0 then
        dll$="cards32.dll"
    else
        hDLL=LoadLibrary("cards.dll")
        if hDLL<>0 then
            dll$="cards.dll"
        else
            notice "No DLL available."
            end
        end if
    end if

    r=FreeLibrary(hDLL)

    WindowWidth = 800
    WindowHeight = 500
    UpperLeftX=1
    UpperLeftY=1

    open "Cards.DLL" for graphics_nsb as #main
    #main "trapclose [quit]"
    #main "down ; fill 0 255 0"

    'Open the card DLL
    open dll$ for dll as #card

    'create structs that allow function to return default card sizes
    struct cardX, cardwidth as long
    struct cardY, cardheight as long

    calldll #card,"cdtInit",_ 'initialize DLL
        cardX as struct,_     'will contain card width
        cardY as struct,_     'will contain card height
        result as long

    cardWide = cardX.cardwidth.struct
    cardHigh = cardY.cardheight.struct

    hgDC=GetDC(hwnd(#main))  'device context for graphicbox
    col=MakeRGB(0,127,0)     'color matches fill color of graphicbox

    for Suit=0 to 3                                 'card indices 0-51
        for Value = 0 to 48 step 4
            cardX=int(10 + 20 * Value / 4)          'draw overlapped cards
            cardY=int(10 + (10 + cardHigh) * Suit)  'each suit on their own row
            nCard = Suit + Value                    'calculate card index
            nDraw=0                                 'draw card front
            r=DrawCard(hgDC,cardX,cardY,nCard,nDraw,col)
        next
    next

    oldCardX = cardX + cardWide + 10
    row = 0
    column = 0

    for nCard = 53 to 65                                 'card indices 52-65
        cardX=int(oldCardX + (10 + cardWide) * column)   'draw full cards
        cardY=int(10 + (10 + cardHigh) * row)            'on three rows
        nDraw=1                                          'draw card
        r=DrawCard(hgDC,cardX,cardY,nCard,nDraw,col)

        column = column + 1
        if column > 4 then
            column = 0
            row = row + 1
        end if
    next

    column = 3

    for nCard = 67 to 68                                 'card indices 67-68
        cardX=int(oldCardX + (10 + cardWide) * column)   'draw full cards
        cardY=int(10 + (10 + cardHigh) * 3)              'on last row
        nDraw=1                                          'draw card
        r=DrawCard(hgDC,cardX,cardY,nCard,nDraw,col)

        column = column + 1
    next
    wait

[quit]
    'terminate DLL and call FreeLibrary
    calldll #card,"cdtTerm",result as void

    close #main
    close #card
    end

Function DrawCard(hDC,x,y,iCard,iDraw,clr)
    calldll #card,"cdtDraw",_
        hDC as ulong,_      'graphics device context
        x as long,_         'desired x location
        y as long,_         'desired y location
        iCard as long,_     '0-51=deck, 52-68=specials
        iDraw as long,_     '0=front, 1=back, 2=invert for active
        clr as long,_       'background color
        result as long
end function

Function GetDC(hWnd)
    CallDLL #user32, "GetDC",_
        hWnd  As Ulong,_ 'window or control handle
        GetDC As Ulong   'returns device context
End Function

Function LoadLibrary(file$)
    Calldll #kernel32, "LoadLibraryA", file$ As Ptr, LoadLibrary As Ulong
End Function

Function FreeLibrary(hDLL)
    Calldll #kernel32, "FreeLibrary", hDLL As Ulong, FreeLibrary As Long
End Function

function MakeRGB(red,green,blue)
    if red<0 then red=0
    if red>255 then red=255
    if green<0 then green=0
    if green>255 then green=255
    if blue<0 then blue=0
    if blue>255 then blue=255
    MakeRGB=(blue*256*256)+(green*256)+red
end function

[[code]]
----
[[toc|flat]]