Need help with Bitmap Display

This is a discussion on Need help with Bitmap Display within the Windows Programming forums, part of the Platform Specific Boards category; This code is expanding on the forger's windows programming tutorial; bmp_one (simply displays a static bitmap of a ball on ...

  1. #1
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Need help with Bitmap Display

    This code is expanding on the forger's windows programming tutorial; bmp_one (simply displays a static bitmap of a ball on a screen).

    I am attempting to load an array of HBITMAPS (deck[53]) with a bunch of bitmaps of playing cards I scanned earlier today.. and display 5 random cards from the deck onto the main window.

    I can get the code to work if I omit trying to load the deck[53] and just btblt( ) one specific card.

    Can anyone spot the problemo...??

    using codeblocks v8.02

    resource.h
    Code:
    #define ID_2OFSPADES       100
    #define ID_2OFCLUBS        101
    #define ID_2OFHEARTS       102
    #define ID_2OFDIAMONDS     103
    
    #define ID_3OFSPADES       104
    #define ID_3OFCLUBS        105
    #define ID_3OFHEARTS       106
    #define ID_3OFDIAMONDS     107
    
    #define ID_4OFSPADES       108
    #define ID_4OFCLUBS        109
    #define ID_4OFHEARTS       110
    #define ID_4OFDIAMONDS     111
    
    #define ID_5OFSPADES       112
    #define ID_5OFCLUBS        113
    #define ID_5OFHEARTS       114
    #define ID_5OFDIAMONDS     115
    
    #define ID_6OFSPADES       116
    #define ID_6OFCLUBS        117
    #define ID_6OFHEARTS       118
    #define ID_6OFDIAMONDS     119
    
    #define ID_7OFSPADES       120
    #define ID_7OFCLUBS        121
    #define ID_7OFHEARTS       122
    #define ID_7OFDIAMONDS     123
    
    #define ID_8OFSPADES       124
    #define ID_8OFCLUBS        125
    #define ID_8OFHEARTS       126
    #define ID_8OFDIAMONDS     127
    
    #define ID_9OFSPADES       128
    #define ID_9OFCLUBS        129
    #define ID_9OFHEARTS       130
    #define ID_9OFDIAMONDS     131
    
    #define ID_10OFSPADES      132
    #define ID_10OFCLUBS       133
    #define ID_10OFHEARTS      134
    #define ID_10OFDIAMONDS    135
    
    #define ID_JOFSPADES       136
    #define ID_JOFCLUBS        137
    #define ID_JOFHEARTS       138
    #define ID_JOFDIAMONDS     139
    
    #define ID_QOFSPADES       140
    #define ID_QOFCLUBS        141
    #define ID_QOFHEARTS       142
    #define ID_QOFDIAMONDS     143
    
    #define ID_KOFSPADES       144
    #define ID_KOFCLUBS        145
    #define ID_KOFHEARTS       146
    #define ID_KOFDIAMONDS     147
    
    #define ID_AOFSPADES       148
    #define ID_AOFCLUBS        149
    #define ID_AOFHEARTS       150
    #define ID_AOFDIAMONDS     151
    
    #define ID_BACKOFCARD      152

    main.cpp
    Code:
    #include <windows.h>
    #include "resource.h"
    #include <ctime>
    
    const char g_szClassName[] = "myWindowClass";
    
    
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	static HBITMAP deck[53];
            static HBITMAP hbmOld;
    
    	switch(msg)
    	{
    		case WM_CREATE:
                   
                    srand(time(NULL));
    
    		for(int i=0, j=ID_2OFSPADES; i<53; i++, j++)
    		{
                       deck[i] = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(j));
    
                        if(deck[i] == NULL)
                        {
                            MessageBox(hwnd, "Could not load cards!", "Error", MB_OK | MB_ICONEXCLAMATION);
                        }
    	 	}
    
    		break;
    		case WM_CLOSE:
    			DestroyWindow(hwnd);
    
    		break;
    		case WM_PAINT:
    		{
    			// Just a note, never use a MessageBox from inside WM_PAINT
    			// The box will cause more WM_PAINT messages and you'll probably end up
    			// stuck in a loop
    
    			BITMAP bm;
    			PAINTSTRUCT ps;
    
    			HDC hdc = BeginPaint(hwnd, &ps);
    
    			HDC hdcMem = CreateCompatibleDC(hdc);
    
    			for (int i=0, j=100; i<5; i++, j+=300)
    			{
                               hbmOld = (HBITMAP)SelectObject(hdcMem, deck[rand()%53]);
    
                                GetObject(deck[i], sizeof(bm), &bm);
    
                                BitBlt(hdc, j, 100, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
    			}
    
    			SelectObject(hdcMem,hbmOld);
    			DeleteDC(hdcMem);
    
    			EndPaint(hwnd, &ps);
    		}
    		break;
    		case WM_DESTROY:
    
                               for(int i=0; i<53; i++)
                               {
                                   DeleteObject(deck[i]);
                               }
    
    			PostQuitMessage(0);
    		break;
    		default:
    			return DefWindowProc(hwnd, msg, wParam, lParam);
    	}
    	return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    	LPSTR lpCmdLine, int nCmdShow)
    {
    	WNDCLASSEX wc;
    	HWND hwnd;
    	MSG Msg;
    
    	wc.cbSize		 = sizeof(WNDCLASSEX);
    	wc.style		 = 0;
    	wc.lpfnWndProc	 = WndProc;
    	wc.cbClsExtra	 = 0;
    	wc.cbWndExtra	 = 0;
    	wc.hInstance	 = hInstance;
    	wc.hIcon		 = LoadIcon(NULL, IDI_APPLICATION);
    	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
    	wc.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(0,255,0));
    	wc.lpszMenuName  = NULL;
    	wc.lpszClassName = g_szClassName;
    	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION);
    
    	if(!RegisterClassEx(&wc))
    	{
    		MessageBox(NULL, "Window Registration Failed!", "Error!",
    			MB_ICONEXCLAMATION | MB_OK);
    		return 0;
    	}
    
    	hwnd = CreateWindowEx(
    		WS_EX_CLIENTEDGE,
    		g_szClassName,
    		"Blackjack v1.0",
    		WS_OVERLAPPEDWINDOW,
    		CW_USEDEFAULT, CW_USEDEFAULT, 900,500,
    		NULL, NULL, hInstance, NULL);
    
    	if(hwnd == NULL)
    	{
    		MessageBox(NULL, "Window Creation Failed!", "Error!",
    			MB_ICONEXCLAMATION | MB_OK);
    		return 0;
    	}
    
    	ShowWindow(hwnd, nCmdShow);
    	UpdateWindow(hwnd);
    
    	while(GetMessage(&Msg, NULL, 0, 0) > 0)
    	{
    		TranslateMessage(&Msg);
    		DispatchMessage(&Msg);
    	}
    	return Msg.wParam;
    }
    resource.rc
    Code:
    #include "resource.h"
    
    ID_2OFSPADES   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\2 of spades.bmp"
    ID_2OFCLUBS    BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\2 of clubs.bmp"
    ID_2OFHEARTS   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\2 of hearts.bmp"
    ID_2OFDIAMONDS BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\2 of diamonds.bmp"
    
    ID_3OFSPADES   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\3 of spades.bmp"
    ID_3OFCLUBS    BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\3 of clubs.bmp"
    ID_3OFHEARTS   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\3 of hearts.bmp"
    ID_3OFDIAMONDS BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\3 of diamonds.bmp"
    
    ID_4OFSPADES   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\4 of spades.bmp"
    ID_4OFCLUBS    BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\4 of clubs.bmp"
    ID_4OFHEARTS   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\4 of hearts.bmp"
    ID_4OFDIAMONDS BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\4 of diamonds.bmp"
    
    ID_5OFSPADES   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\5 of spades.bmp"
    ID_5OFCLUBS    BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\5 of clubs.bmp"
    ID_5OFHEARTS   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\5 of hearts.bmp"
    ID_5OFDIAMONDS BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\5 of diamonds.bmp"
    
    ID_6OFSPADES   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\6 of spades.bmp"
    ID_6OFCLUBS    BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\6 of clubs.bmp"
    ID_6OFHEARTS   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\6 of hearts.bmp"
    ID_6OFDIAMONDS BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\6 of diamonds.bmp"
    
    ID_7OFSPADES   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\7 of spades.bmp"
    ID_7OFCLUBS    BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\7 of clubs.bmp"
    ID_7OFHEARTS   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\7 of hearts.bmp"
    ID_7OFDIAMONDS BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\7 of diamonds.bmp"
    
    ID_8OFSPADES   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\8 of spades.bmp"
    ID_8OFCLUBS    BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\8 of clubs.bmp"
    ID_8OFHEARTS   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\8 of hearts.bmp"
    ID_8OFDIAMONDS BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\8 of diamonds.bmp"
    
    ID_9OFSPADES   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\9 of spades.bmp"
    ID_9OFCLUBS    BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\9 of clubs.bmp"
    ID_9OFHEARTS   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\9 of hearts.bmp"
    ID_9OFDIAMONDS BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\9 of diamonds.bmp"
    
    ID_10OFSPADES  BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\10 of spades.bmp"
    ID_10OFCLUBS   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\10 of clubs.bmp"
    ID_10OFHEARTS  BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\10 of hearts.bmp"
    ID_10OFDIAMONDS    BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\10 of diamonds.bmp"
    
    ID_JOFSPADES   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\jack of spades.bmp"
    ID_JOFCLUBS    BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\jack of clubs.bmp"
    ID_JOFHEARTS   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\jack of hearts.bmp"
    ID_JOFDIAMONDS BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\jack of diamonds.bmp"
    
    ID_QOFSPADES   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\queen of spades.bmp"
    ID_QOFCLUBS    BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\queen of clubs.bmp"
    ID_QOFHEARTS   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\queen of hearts.bmp"
    ID_QOFDIAMONDS     BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\queen of diamonds.bmp"
    
    ID_KOFSPADES   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\king of spades.bmp"
    ID_KOFCLUBS    BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\king of clubs.bmp"
    ID_KOFHEARTS   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\king of hearts.bmp"
    ID_KOFDIAMONDS BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\king of diamonds.bmp"
    
    ID_AOFSPADES   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\ace of spades.bmp"
    ID_AOFCLUBS    BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\ace of clubs.bmp"
    ID_AOFHEARTS   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\ace of hearts.bmp"
    
    ID_BACKOFCARD  BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\back of card.bmp"
    Last edited by The Brain; 03-21-2009 at 07:25 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  2. #2
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    I get the message box to finally stop displaying.. after I change the loop value from 53 to 51.. but it doesn't make sense... there are 52 cards in the deck + 1 bitmap of the back of a card...

    Code:
    for(int i=0, j=ID_2OFSPADES; i<51; i++, j++)
    		{
                       deck[i] = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(j));
    
                        if(deck[i] == NULL)
                        {
                            MessageBox(hwnd, "Could not load cards!", "Error", MB_OK | MB_ICONEXCLAMATION);
                        }
    	 	}
    Last edited by The Brain; 03-21-2009 at 07:46 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  3. #3
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    611
    What does GetLastError() have to say when LoadBitmap returns NULL?

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,854
    Do you have anyother resources with the IDs of 151 or 152 etc?

    EDIT: You seem to be missing location of the image for the ace of diamonds

    I prefer...

    Code:
    for(int i=0;  i<53; i++)
    {
              deck[i] = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE( ID_2OFSPADES + i ));
    
    
    
    int iCard=0;
    int iGap=5;//put a gap of 5 pixcels between the cards
    for (int i=0; i<5; i++)
    {
                iCard=rand()%53;//check the edge cases here
                //error check for valid image in array
    
                hbmOld = (HBITMAP)SelectObject(hdcMem, deck[iCard]);
    
                GetObject(deck[iCard], sizeof(bm), &bm);
    
                 BitBlt(hdc, 100 + (i*(bm.bmWidth+iGap)), 100, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY)
    }
    Last edited by novacain; 03-21-2009 at 09:02 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Question Bitmap display after button press help

    That missing bitmap location in the resource script solved a lot of me' problems.. thanks novacain.

    My next task I would like to accomplish is to have a card bitmap display to the window whenever the, "HIT ME!" button is pressed.. everything looks alright to me... but whenever I press the button, nothing happens. Any suggestions..??

    main.cpp
    Code:
    
    #include<windows.h>
    #include<algorithm>
    #include<ctime>
    #include "resource.h"
    
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
    
    ////////////////////////////////////////////////////////////////
    /* window creation and registration stuff here */
    //////////////////////////////////////////////////////////////
    
    
    class Blackjack
    {
        public:
    
        //Constructor
        Blackjack(HWND);
        //Destructor
        //~Blackjack();
        //WinProc Functions
        void create(HWND);
        void command(HWND,WPARAM, LPARAM);
        void paint();
        void close(HWND);
        int  destroy(HWND);
        //Other Functions
        void get_card_player();
        void get_card_dealer();
        void shuffle();
    
    
        private:
    
        BITMAP bm;
        PAINTSTRUCT ps;
        HBITMAP deck[53];
        HBITMAP hbmOld;
        HWND hHit;
        HWND hStay;
        HDC hdc;
        HDC hdcMem;
        int card[52];
        int current_hand_value;
        int card_counter;
        int player_card_coord_x,
            player_card_coord_y,
            dealer_card_coord_x,
            dealer_card_coord_y;
    };
    
    enum{ID_hit, ID_stay};
    
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        static Blackjack myBlackjack(hwnd);
    
    	switch(msg)
    	{
    		case WM_CREATE:     myBlackjack.create(hwnd);                   break;
    		case WM_COMMAND:    myBlackjack.command(hwnd, wParam, lParam);  break;
                    //case WM_PAINT:      myBlackjack.command(hwnd, wParam, lParam);  break;
    		case WM_CLOSE:      myBlackjack.close(hwnd);                    break;
    		case WM_DESTROY:    myBlackjack.destroy(hwnd);                  break;
    
    		default:
    			return DefWindowProc(hwnd, msg, wParam, lParam);
    	}
    	return 0;
    }
    
    Blackjack::Blackjack(HWND hwnd)
    {
    
        for(int i=0; i<52; i++)
        {
            card[52] = i;
        }
    
        //random_shuffle(&card[0], &card[52]);
    
        hdc = BeginPaint(hwnd, &ps);
        hdcMem = CreateCompatibleDC(hdc);
    
        player_card_coord_x = 100;
        player_card_coord_y = 300;
        dealer_card_coord_x = 100;
        dealer_card_coord_y = 50;
    
    }
    /*
    Blackjack::~Blackjack(HWND hwnd)
    {
    
    }
    */
    void Blackjack::create(HWND hwnd)
    {
        srand(time(NULL));
    
        hHit = CreateWindow("BUTTON", "HIT ME!",
                                WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
                                100, 500, 200, 50, hwnd, (HMENU)ID_hit,
                                (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
    
        hStay = CreateWindow("BUTTON", "STAY",
                                WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
                                500, 500, 200, 50, hwnd, (HMENU)ID_stay,
                                (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
    
    
        for(int i=0, j=ID_2OFSPADES; i<53; i++, j++)
        {
            deck[i] = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(j));
    
            if(deck[i] == NULL)
            {
                MessageBox(hwnd, "Could Not Load Card!", "Error", MB_OK | MB_ICONEXCLAMATION);
            }
    
        }
    }
    
    void Blackjack::command(HWND hwnd, WPARAM wParam, LPARAM lParam)
    {
        WORD msg = LOWORD(wParam);
    
        if(HIWORD(wParam) == BN_CLICKED)
        {
            if(msg == ID_hit)
            {
                get_card_player();
            }
            else if(msg == ID_stay)
            {
                get_card_dealer();
            }
        }
    }
    
    void Blackjack::paint()
    {
        return;
    }
    
    void Blackjack::close(HWND hwnd)
    {
        DestroyWindow(hwnd);
    }
    
    int Blackjack::destroy(HWND hwnd)
    {
    
        for(int i=0; i<53; i++)
        {
            DeleteObject(deck[i]);
        }
    
        SelectObject(hdcMem,hbmOld);
        DeleteDC(hdcMem);
        EndPaint(hwnd, &ps);
    
        PostQuitMessage(0);
        return 0;
    }
    
    void Blackjack::get_card_player()
    {
    
        hbmOld = (HBITMAP)SelectObject(hdcMem,deck[card[card_counter]]);
    
        GetObject(deck[card[card_counter]], sizeof(bm), &bm);
    
        BitBlt(hdc, player_card_coord_x+=200, 300, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
    
        card_counter+=1;
    }
    
    void Blackjack::get_card_dealer()
    {
    
    }
    
    void Blackjack::shuffle()
    {
        //random_shuffle(&card[0], &card[52]);
    }
    Last edited by The Brain; 03-22-2009 at 02:30 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  6. #6
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Exclamation Found the Answer

    I just remembered that creation/usage of Device Context can only occur in response to a WM_PAINT message.. I was trying to display bitmaps from WM_COMMAND...
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  7. #7
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Wink Ready for some BJ?

    Just want to thank everyone who helped me out with some basic windows programming... just completed my first program that uses bitmaps. I would be interested in ya'lls opinion; maybe there is a better way to do some of these things.

    After the code was complete, I took the idea of seperate compilation to the extreme. According to my CS101 professor, the main( ) function of a program should be as streamlined as possible to hide as much data as possible. I split the project up into 5 different files: resource.h, resource.rc, blackjack.h, blackjack.cpp, and main.cpp. Notice how little code there is in main.cpp.

    Lessons Learned: Thou shalt not attempt to device context outside of WM_PAINT.
    The importance of "compiling/testing as you go"

    One area of concern though: although my program appears to run fine, I am not sure if I am deleting all my DC's properly... I store the returning value from SelectObject( ) into a class global "hbmOld" and then at the end of the wm_paint, I delete whatever functions used the DC.. (I hope).


    resource.h
    Code:
    #define ID_2OFSPADES       100
    #define ID_2OFCLUBS        101
    #define ID_2OFHEARTS       102
    #define ID_2OFDIAMONDS     103
    
    #define ID_3OFSPADES       104
    #define ID_3OFCLUBS        105
    #define ID_3OFHEARTS       106
    #define ID_3OFDIAMONDS     107
    
    #define ID_4OFSPADES       108
    #define ID_4OFCLUBS        109
    #define ID_4OFHEARTS       110
    #define ID_4OFDIAMONDS     111
    
    #define ID_5OFSPADES       112
    #define ID_5OFCLUBS        113
    #define ID_5OFHEARTS       114
    #define ID_5OFDIAMONDS     115
    
    #define ID_6OFSPADES       116
    #define ID_6OFCLUBS        117
    #define ID_6OFHEARTS       118
    #define ID_6OFDIAMONDS     119
    
    #define ID_7OFSPADES       120
    #define ID_7OFCLUBS        121
    #define ID_7OFHEARTS       122
    #define ID_7OFDIAMONDS     123
    
    #define ID_8OFSPADES       124
    #define ID_8OFCLUBS        125
    #define ID_8OFHEARTS       126
    #define ID_8OFDIAMONDS     127
    
    #define ID_9OFSPADES       128
    #define ID_9OFCLUBS        129
    #define ID_9OFHEARTS       130
    #define ID_9OFDIAMONDS     131
    
    #define ID_10OFSPADES      132
    #define ID_10OFCLUBS       133
    #define ID_10OFHEARTS      134
    #define ID_10OFDIAMONDS    135
    
    #define ID_JOFSPADES       136
    #define ID_JOFCLUBS        137
    #define ID_JOFHEARTS       138
    #define ID_JOFDIAMONDS     139
    
    #define ID_QOFSPADES       140
    #define ID_QOFCLUBS        141
    #define ID_QOFHEARTS       142
    #define ID_QOFDIAMONDS     143
    
    #define ID_KOFSPADES       144
    #define ID_KOFCLUBS        145
    #define ID_KOFHEARTS       146
    #define ID_KOFDIAMONDS     147
    
    #define ID_AOFSPADES       148
    #define ID_AOFCLUBS        149
    #define ID_AOFHEARTS       150
    #define ID_AOFDIAMONDS     151
    
    #define ID_BACKOFCARD      152



    resource.rc
    Code:
    #include "resource.h"
    
    ID_2OFSPADES   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\2 of spades.bmp"
    ID_2OFCLUBS    BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\2 of clubs.bmp"
    ID_2OFHEARTS   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\2 of hearts.bmp"
    ID_2OFDIAMONDS BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\2 of diamonds.bmp"
    
    ID_3OFSPADES   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\3 of spades.bmp"
    ID_3OFCLUBS    BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\3 of clubs.bmp"
    ID_3OFHEARTS   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\3 of hearts.bmp"
    ID_3OFDIAMONDS BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\3 of diamonds.bmp"
    
    ID_4OFSPADES   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\4 of spades.bmp"
    ID_4OFCLUBS    BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\4 of clubs.bmp"
    ID_4OFHEARTS   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\4 of hearts.bmp"
    ID_4OFDIAMONDS BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\4 of diamonds.bmp"
    
    ID_5OFSPADES   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\5 of spades.bmp"
    ID_5OFCLUBS    BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\5 of clubs.bmp"
    ID_5OFHEARTS   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\5 of hearts.bmp"
    ID_5OFDIAMONDS BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\5 of diamonds.bmp"
    
    ID_6OFSPADES   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\6 of spades.bmp"
    ID_6OFCLUBS    BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\6 of clubs.bmp"
    ID_6OFHEARTS   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\6 of hearts.bmp"
    ID_6OFDIAMONDS BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\6 of diamonds.bmp"
    
    ID_7OFSPADES   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\7 of spades.bmp"
    ID_7OFCLUBS    BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\7 of clubs.bmp"
    ID_7OFHEARTS   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\7 of hearts.bmp"
    ID_7OFDIAMONDS BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\7 of diamonds.bmp"
    
    ID_8OFSPADES   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\8 of spades.bmp"
    ID_8OFCLUBS    BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\8 of clubs.bmp"
    ID_8OFHEARTS   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\8 of hearts.bmp"
    ID_8OFDIAMONDS BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\8 of diamonds.bmp"
    
    ID_9OFSPADES   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\9 of spades.bmp"
    ID_9OFCLUBS    BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\9 of clubs.bmp"
    ID_9OFHEARTS   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\9 of hearts.bmp"
    ID_9OFDIAMONDS BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\9 of diamonds.bmp"
    
    ID_10OFSPADES  BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\10 of spades.bmp"
    ID_10OFCLUBS   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\10 of clubs.bmp"
    ID_10OFHEARTS  BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\10 of hearts.bmp"
    ID_10OFDIAMONDS    BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\10 of diamonds.bmp"
    
    ID_JOFSPADES   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\jack of spades.bmp"
    ID_JOFCLUBS    BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\jack of clubs.bmp"
    ID_JOFHEARTS   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\jack of hearts.bmp"
    ID_JOFDIAMONDS BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\jack of diamonds.bmp"
    
    ID_QOFSPADES   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\queen of spades.bmp"
    ID_QOFCLUBS    BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\queen of clubs.bmp"
    ID_QOFHEARTS   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\queen of hearts.bmp"
    ID_QOFDIAMONDS     BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\queen of diamonds.bmp"
    
    ID_KOFSPADES   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\king of spades.bmp"
    ID_KOFCLUBS    BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\king of clubs.bmp"
    ID_KOFHEARTS   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\king of hearts.bmp"
    ID_KOFDIAMONDS BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\king of diamonds.bmp"
    
    ID_AOFSPADES   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\ace of spades.bmp"
    ID_AOFCLUBS    BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\ace of clubs.bmp"
    ID_AOFHEARTS   BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\ace of hearts.bmp"
    ID_AOFDIAMONDS BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\ace of diamonds.bmp"
    
    ID_BACKOFCARD  BITMAP DISCARDABLE "C:\\Users\\Dsve\\Desktop\\Card Bitmaps\\back of card.bmp"



    blackjack.h
    Code:
    #include <windows.h>
    #include <algorithm>
    #include <string>
    #include <vector>
    #include <ctime>
    using namespace std;
    
    class Blackjack
    {
        public:
    
        //Constructor
        Blackjack(HWND);
        //WinProc Functions
        void create(HWND);
        void command(HWND,WPARAM, LPARAM);
        void paint(HWND);
        void close(HWND);
        int  destroy(HWND);
        //Game Functions
        void get_card_player(HWND, HDC, HDC);
        void  display_deck(HWND, HDC, HDC);
        void get_card_dealer(HWND, HDC, HDC);
        void get_winner(HWND);
        void set_hand_value(int);
        void set_dealer_hand_value(int);
        bool is_bust(HWND);
        bool is_blackjack(HWND);
        void reset(HWND);
    
    
        private:
    
        HBITMAP deck[53];    
        HBITMAP hbmOld;
        HWND hHit;
        HWND hStay;
        HDC hdc;
        HDC hdcMem;
        vector<int> index;
        bool stay;
        int player_hand_value;
        int dealer_hand_value;
        int card_counter;
        int player_card_coord_x,
            dealer_card_coord_x;
    };
    
    enum{ID_hit, ID_stay};
    
    const char g_szClassName[] = "myWindowClass";
    
    /*  Declare Windows procedure  */
    LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);



    blackjack.cpp
    Code:
    #include "blackjack.h"
    #include "resource.h"
    
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    	LPSTR lpCmdLine, int nCmdShow)
    {
    	WNDCLASSEX wc;
    	HWND hwnd;
    	MSG Msg;
    
    	wc.cbSize		 = sizeof(WNDCLASSEX);
    	wc.style		 = 0;
    	wc.lpfnWndProc	 = WndProc;
    	wc.cbClsExtra	 = 0;
    	wc.cbWndExtra	 = 0;
    	wc.hInstance	 = hInstance;
    	wc.hIcon		 = LoadIcon(NULL, IDI_APPLICATION);
    	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
    	wc.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(0,100,0));
    	wc.lpszMenuName  = NULL;
    	wc.lpszClassName = g_szClassName;
    	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION);
    
    	if(!RegisterClassEx(&wc))
    	{
    		MessageBox(NULL, "Window Registration Failed!", "Error!",
    			MB_ICONEXCLAMATION | MB_OK);
    		return 0;
    	}
    
    	hwnd = CreateWindowEx(
    		WS_EX_CLIENTEDGE,
    		g_szClassName,
    		"Blackjack v1.0",
    		WS_OVERLAPPEDWINDOW,
    		CW_USEDEFAULT, CW_USEDEFAULT, 1200,625,
    		NULL, NULL, hInstance, NULL);
    
    	if(hwnd == NULL)
    	{
    		MessageBox(NULL, "Window Creation Failed!", "Error!",
    			MB_ICONEXCLAMATION | MB_OK);
    		return 0;
    	}
    
    	ShowWindow(hwnd, nCmdShow);
    	UpdateWindow(hwnd);
    
    	while(GetMessage(&Msg, NULL, 0, 0) > 0)
    	{
    		TranslateMessage(&Msg);
    		DispatchMessage(&Msg);
    	}
    	return Msg.wParam;
    }
    
    
    
    Blackjack::Blackjack(HWND hwnd)
    {
    
        srand(time(NULL));
    
        for(int i=0; i<52; i++)
        {
            index.push_back(i);
        }
    
        random_shuffle(index.begin(), index.end());
    
        player_card_coord_x = 100;
        dealer_card_coord_x = 250;
    
        card_counter = 0;
        player_hand_value = 0;
        dealer_hand_value = 0;
    
        stay = FALSE;
    
    }
    
    void Blackjack::create(HWND hwnd){
    
    
        hHit = CreateWindow("BUTTON", "HIT ME!",
                                WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
                                100, 500, 200, 50, hwnd, (HMENU)ID_hit,
                                (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
    
        hStay = CreateWindow("BUTTON", "STAY",
                                WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
                                500, 500, 200, 50, hwnd, (HMENU)ID_stay,
                                (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
    
    
        for(int i=0, j=ID_2OFSPADES; i<53; i++, j++)
        {
            deck[i] = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(j));
    
            if(deck[i] == NULL)
            {
                MessageBox(hwnd, "Could Not Load Card!", "Error", MB_OK | MB_ICONEXCLAMATION);
            }
    
        }
    }
    
    void Blackjack::command(HWND hwnd, WPARAM wParam, LPARAM lParam)
    {
        WORD msg = LOWORD(wParam);
    
        if(HIWORD(wParam) == BN_CLICKED)
        {
            if(msg == ID_hit)
            {
                InvalidateRect(hwnd, NULL, FALSE);
            }
            else if(msg == ID_stay)
            {
                stay = TRUE;
                InvalidateRect(hwnd, NULL, FALSE);
            }
        }
    }
    
    void Blackjack::paint(HWND hwnd)
    {
        PAINTSTRUCT ps;
    
        HDC hdc = BeginPaint(hwnd, &ps);
        HDC hdcMem = CreateCompatibleDC(hdc);
    
        if(!card_counter)
        {
            display_deck(hwnd, hdc, hdcMem);
            get_card_player(hwnd, hdc, hdcMem);
        }
    
        if(stay == FALSE)
        {
            get_card_player(hwnd, hdc, hdcMem);
        }
    
        if(stay == TRUE)
        {
    
            get_card_dealer(hwnd, hdc, hdcMem);
        }
    
        SelectObject(hdcMem, hbmOld);
        DeleteDC(hdcMem);
        EndPaint(hwnd, &ps);
    }
    
    
    void Blackjack::close(HWND hwnd)
    {
        DestroyWindow(hwnd);
    }
    
    int Blackjack::destroy(HWND hwnd)
    {
    
        for(int i=0; i<53; i++)
        {
            DeleteObject(deck[i]);
        }
    
        PostQuitMessage(0);
        return 0;
    }
    
    void Blackjack::display_deck(HWND hwnd, HDC hdc, HDC hdcMem)
    {
        BITMAP bm;
    
        hbmOld = (HBITMAP)SelectObject(hdcMem, deck[52]);
    
        GetObject(deck[52], sizeof(bm), &bm);
    
        BitBlt(hdc, 100, 50, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
    
        //SelectObject(hdcMem, hbmOld);
        //DeleteDC(hdcMem);
    }
    
    void Blackjack::get_card_player(HWND hwnd, HDC hdc, HDC hdcMem)
    {
        BITMAP bm;
    
        int card = index[card_counter];
    
        hbmOld = (HBITMAP)SelectObject(hdcMem,deck[card]);
    
        GetObject(deck[card], sizeof(bm), &bm);
    
        BitBlt(hdc, player_card_coord_x, 300, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
    
        set_hand_value(index[card_counter]);
    
        player_card_coord_x+=150;
        card_counter++;
    
        is_blackjack(hwnd);
        is_bust(hwnd);
    }
    
    void Blackjack::get_card_dealer(HWND hwnd, HDC hdc, HDC hdcMem)
    {
        BITMAP bm;
        HBITMAP hbmOld;
        int card;
    
        do
        {
            card = index[card_counter];
    
            hbmOld = (HBITMAP)SelectObject(hdcMem,deck[card]);
    
            GetObject(deck[card], sizeof(bm), &bm);
            BitBlt(hdc, dealer_card_coord_x, 50, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
    
            set_dealer_hand_value(card);
            dealer_card_coord_x += 150;
            card_counter++;
            card++;
    
            Sleep(400);
    
        }while(dealer_hand_value <= player_hand_value);
    
        get_winner(hwnd);
        reset(hwnd);
    
    }
    
    void Blackjack::get_winner(HWND hwnd)
    {
       if(dealer_hand_value <= 21 && dealer_hand_value > player_hand_value)
       {
           MessageBox(hwnd, "Dealer Wins.", "Loser." , MB_OK | MB_ICONSTOP);
    
       }
    
       else
       {
           MessageBox(hwnd, "Winner..!!!", "You win." , MB_OK | MB_ICONINFORMATION);
    
       }
    }
    
    
    void Blackjack::set_hand_value(int card)
    {
        if(card < 4)// [0] to [3]
        {
             player_hand_value += 2;
        }
        else if(card > 3 && card < 8)// [4] to [7]
        {
             player_hand_value += 3;
        }
        else if(card > 7 && card < 12)//[8] to [11]
        {
             player_hand_value += 4;
        }
        else if(card > 11 && card < 16)//[12] to [15]
        {
             player_hand_value += 5;
        }
        else if(card > 15 && card < 20)//[16] to [19]
        {
             player_hand_value += 6;
        }
        else if(card > 19 && card < 24)//[20] to [23]
        {
             player_hand_value += 7;
        }
        else if(card > 23 && card < 28)//[24] to [27]
        {
             player_hand_value += 8;
        }
        else if(card > 27 && card < 32)//[28] to [31]
        {
             player_hand_value += 9;
        }
        else if(card > 31 && card < 48 )//[32] to [47]
        {
             player_hand_value += 10;
        }
    
        //Handle the Ace
        else if(card > 47 && card < 52 && player_hand_value < 11)
        {
            player_hand_value += 11;
        }
        else if(card < 47 && card < 52 && player_hand_value > 10)
        {
            player_hand_value += 1;
        }
    }
    
    void Blackjack::set_dealer_hand_value(int card)
    {
        if(card < 4)// [0] to [3]
        {
             dealer_hand_value += 2;
        }
        else if(card > 3 && card < 8)// [4] to [7]
        {
             dealer_hand_value += 3;
        }
        else if(card > 7 && card < 12)//[8] to [11]
        {
             dealer_hand_value += 4;
        }
        else if(card > 11 && card < 16)//[12] to [15]
        {
             dealer_hand_value += 5;
        }
        else if(card > 15 && card < 20)//[16] to [19]
        {
             dealer_hand_value += 6;
        }
        else if(card > 19 && card < 24)//[20] to [23]
        {
             dealer_hand_value += 7;
        }
        else if(card > 23 && card < 28)//[24] to [27]
        {
             dealer_hand_value += 8;
        }
        else if(card > 27 && card < 32)//[28] to [31]
        {
             dealer_hand_value += 9;
        }
        else if(card > 31 && card < 48 )//[32] to [47]
        {
             dealer_hand_value += 10;
        }
    
        //Handle the Ace
        else if(card > 47 && card < 52 && player_hand_value < 11)
        {
            dealer_hand_value += 11;
        }
        else if(card < 47 && card < 52 && player_hand_value > 10)
        {
            dealer_hand_value += 1;
        }
    }
    
    
    bool Blackjack::is_bust(HWND hwnd)
    {
        if(player_hand_value > 21)
        {
            MessageBox(hwnd, "Busted...!!!", "Busted.", MB_OK | MB_ICONSTOP);
            reset(hwnd);
            return TRUE;
        }
    
        else
        {
            return FALSE;
        }
    }
    
    
    bool Blackjack::is_blackjack(HWND hwnd)
    {
        if(player_hand_value == 21)
        {
            MessageBox(hwnd, "BlAcKjAcK..!!", "You Win.", MB_OK | MB_ICONEXCLAMATION);
            reset(hwnd);
            return TRUE;
        }
    
        else
        {
            return FALSE;
        }
    }
    
    
    void Blackjack::reset(HWND hwnd)
    {
    
        random_shuffle(index.begin(), index.end());
    
        player_card_coord_x = 100;
        dealer_card_coord_x = 250;
    
        card_counter = 0;
        player_hand_value = 0;
        dealer_hand_value = 0;
    
        stay = FALSE;
    
        InvalidateRect(hwnd, NULL, TRUE);
    }

    And finally, the driver.. main.cpp
    Code:
    #include "blackjack.h"
    
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        static Blackjack myBlackjack(hwnd);
    
    	switch(msg)
    	{
    		case WM_CREATE:     myBlackjack.create(hwnd);                   break;
    		case WM_COMMAND:    myBlackjack.command(hwnd, wParam, lParam);  break;
                    case WM_PAINT:      myBlackjack.paint(hwnd);                    break;
    		case WM_CLOSE:      myBlackjack.close(hwnd);                    break;
    		case WM_DESTROY:    myBlackjack.destroy(hwnd);                  break;
    
    		default:
    			return DefWindowProc(hwnd, msg, wParam, lParam);
    	}
    	return 0;
    }
    Attached Images Attached Images  
    Last edited by The Brain; 03-23-2009 at 05:12 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  8. #8
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Need More Help..!@#!@#

    Just sent this program to my cousin who is running the same operating system as I; window's vista.. and the program offers some very weird behavor as shown in the attachment.. The program runs fine on my machine.. any explaination as to how this could happen...?!??!
    Attached Images Attached Images  
    Last edited by The Brain; 03-24-2009 at 12:55 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL -- Bitmaps
    By HQSneaker in forum Game Programming
    Replies: 14
    Last Post: 09-06-2004, 04:04 PM
  2. Simple Ddraw question. Display bitmap on a screen.
    By tegwin in forum Game Programming
    Replies: 0
    Last Post: 05-22-2004, 05:50 PM
  3. How to create and display a bitmap programatically?
    By solar3147 in forum Windows Programming
    Replies: 4
    Last Post: 06-24-2003, 02:55 AM
  4. bitmap not going onto screen
    By stallion in forum Windows Programming
    Replies: 4
    Last Post: 02-22-2003, 09:07 AM
  5. Using BitBlt() to display only a portion of a big bitmap
    By Leeman_s in forum Windows Programming
    Replies: 4
    Last Post: 01-16-2003, 09:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21