DX9 Not Displaying Bitmaps

This is a discussion on DX9 Not Displaying Bitmaps within the Game Programming forums, part of the General Programming Boards category; I am reading a book called "Begining Game Programming", and I got to the part where you start displaying bitmaps ...

  1. #1
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719

    DX9 Not Displaying Bitmaps

    I am reading a book called "Begining Game Programming", and I got to the part where you start displaying bitmaps through the use of a game loop. It comes with a CD so I took the source code from their and put it into my compiler MSVC 7.1. It says to save it with a .c extension not a .cpp (I dont know if this matters). So I save it with a .c . I put the bitmap in my resource folder in the solution explorer, and then put the code in (pasted) When I run it, it comes up with the window, but no bitmaps. Anyone know why this is?
    Code:
    // Beginning Game Programming
    // Chapter 4
    // GameLoop project
    
    #include <windows.h>
    #include <winuser.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define APPTITLE "Game Loop"
    
    //function prototypes
    LRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM);
    ATOM MyRegisterClass(HINSTANCE);
    BOOL InitInstance(HINSTANCE,int);
    void DrawBitmap(HDC,char*,int,int);
    void Game_Init();
    void Game_Run();
    void Game_End();
    
    
    //local variables
    HWND global_hwnd;
    HDC global_hdc;
    
    
    //the window event callback function
    LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        global_hwnd = hWnd;
        global_hdc = GetDC(hWnd);
    
    	switch (message) 
    	{
    		case WM_DESTROY:
                Game_End();
    			PostQuitMessage(0);
    			break;
        }
    	return DefWindowProc(hWnd, message, wParam, lParam);
    }
    
    //helper function to set up the window properties
    ATOM MyRegisterClass(HINSTANCE hInstance)
    {
        //create the window class structure
        WNDCLASSEX wc;
        wc.cbSize = sizeof(WNDCLASSEX); 
    
        //fill the struct with info
        wc.style         = CS_HREDRAW | CS_VREDRAW;
        wc.lpfnWndProc   = (WNDPROC)WinProc;
        wc.cbClsExtra	 = 0;
        wc.cbWndExtra	 = 0;
        wc.hInstance     = hInstance;
        wc.hIcon         = NULL;
        wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
        wc.lpszMenuName  = NULL;
        wc.lpszClassName = APPTITLE;
        wc.hIconSm       = NULL;
    
        //set up the window with the class info
        return RegisterClassEx(&wc);
    
    }
    
    //helper function to create the window and refresh it
    BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
    {
        HWND hWnd;
    
        //create a new window
        hWnd = CreateWindow(
           APPTITLE,              //window class
           APPTITLE,              //title bar
           WS_OVERLAPPEDWINDOW,   //window style
           CW_USEDEFAULT,         //x position of window
           CW_USEDEFAULT,         //y position of window
           500,                   //width of the window
           400,                   //height of the window
    	   NULL,                  //parent window
           NULL,                  //menu
           hInstance,             //application instance
           NULL);                 //window parameters
    
        //was there an error creating the window?
        if (!hWnd)
          return FALSE;
    
        //display the window
        ShowWindow(hWnd, nCmdShow);
        UpdateWindow(hWnd);
    
        return TRUE;
    }
    
    
    //entry point for a Windows program
    int WINAPI WinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPSTR     lpCmdLine,
                       int       nCmdShow)
    {
        int done = 0;
    	MSG msg;
    
    	// register the class
    	MyRegisterClass(hInstance);
    
    
        // initialize application
    	if (!InitInstance (hInstance, nCmdShow)) 
    		return FALSE;
    
        //initialize the game
        Game_Init();
    
        // main message loop
    	while (!done)
    	{
            if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
            {
                //look for quit message
                if (msg.message == WM_QUIT)
                    done = 1;
    
                //decode and pass messages on to WndProc
                TranslateMessage(&msg);
    		    DispatchMessage(&msg);
            }
            
            //process game loop
            Game_Run();
    	}
    
    	return msg.wParam;
    }
    
    void DrawBitmap(HDC hdcDest, char *filename, int x, int y)
    {
        HBITMAP image;
        BITMAP bm;
    	HDC hdcMem;
    
        //load the bitmap image
        image = LoadImage(0,"c.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
    
        //read the bitmap's properties
        GetObject(image, sizeof(BITMAP), &bm);
    
        //create a device context for the bitmap
        hdcMem = CreateCompatibleDC(global_hdc);
    	SelectObject(hdcMem, image);
    
        //draw the bitmap to the window (bit block transfer)
        BitBlt( 
            global_hdc,              //destination device context
            x, y,                    //x,y location on destination
            bm.bmWidth, bm.bmHeight, //width,height of source bitmap
    	    hdcMem,                  //source bitmap device context
            0, 0,                    //start x,y on source bitmap
            SRCCOPY);                //blit method
    
        //delete the device context and bitmap
        DeleteDC(hdcMem);
        DeleteObject((HBITMAP)image);
    }
    
    
    void Game_Init()
    {
        //initialize the game...
        //load bitmaps, meshes, textures, sounds, etc.
    
        srand(time(NULL));
    }
    
    void Game_Run()
    {
        //this is called once every frame
        //do not include your own loop here!
        
        int x = 0, y = 0;
        RECT rect;
        GetClientRect(global_hwnd, &rect);
    
        if (rect.right > 0)
        {
            x = rand() % (rect.right - rect.left);
            y = rand() % (rect.bottom - rect.top);
            DrawBitmap(global_hdc, "c.bmp", x, y);
        }
    }
    
    void Game_End()
    {
    
    }
    EDIT- LOL! I got it now, it was an incorrect path to the .bmp, i created one from inside the IDE and now it works. Peace.
    Last edited by Sentral; 01-28-2006 at 11:41 AM.

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    You probably know this, but your DrawBitmap function is very, very slow

  3. #3
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    For one I see no DirectX in your code in any way shape or form in your code. Secondly, you should not use the windows bitmap class or any function they have created for bitmaps, they are heli slow and i think they are harder to use than DirectX, no joke. Youe first need to initialize DirectX then create a surface, via CreateOffscreenPlainSurface(), then load a surface from a file, via D3DXLoadSurfaceFromFile(), then use StrechRect() to take a portion of that bitmap and render it to a portion of your back buffer. Now keep in mind that this stuff is not truly Direct3D. this is just some simple stuff thrown in there to make it easy to render 2d images if nessary. if you want more interactive objects in your world, ie you wont even be able to rotate with the other stuff, you will need to learn a little about real Direct3D objects. hope that helps.

    ps the bitmap functions i was refering to are located in the D3DX Library

  4. #4
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Yeah, I didn't understand anything you just talked about. LOL! That's why I am reading the book, duh? The code was from the book's cd so tell your comments to him not me. I thought this was direct x but this was a earlier chapter. So, just give me a break , k?

  5. #5
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    Dont worry i was not trying to bash you for your post, i was just wondering what was going on. This is not DirectX and you may just want to play around with the windows code for a while, to understand it a little, then i would skip to DirectX, this is a sloppy and horrible example.

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    46
    I take it you're reading the book by Jonathan Harbour? If that's the case, be forwarned, not much of the code will compile at all... This is the first DX book I bought, and it led me to buy subesquent ones to compensate... I may just not be tenacious enough, but the inability to compile the code in this book frustrated me to no end... :P

    I'd reccommend checking out "2D in Direct3D" by Ernest Pazera instead... It's in DX8, so you'll end up having to change a few things around to make it DX9 compatible, but I think it's a better read.

    -maxthecat

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    There is no DX code in that sample. That's all Windows GDI.

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    46
    No kidding. I'm just pointing out how much of a pain in the ass that book is.

  9. #9
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    The title of this thread is stating DX9 is not displaying bitmaps. There is no DX9 code here so it's the GDI that's not displaying bitmaps, not DX9.

  10. #10
    ---
    Join Date
    May 2004
    Posts
    1,379
    I find it funny that the image is loaded and destroyed every iteration. Why are there so many books that teach ........?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing bitmaps efficiently
    By scwizzo in forum Windows Programming
    Replies: 28
    Last Post: 06-30-2009, 08:25 PM
  2. Problem displaying bitmaps
    By batman123 in forum Game Programming
    Replies: 2
    Last Post: 01-09-2005, 01:01 AM
  3. Bitmaps Not Displaying
    By DCII764II00 in forum Windows Programming
    Replies: 3
    Last Post: 05-25-2004, 03:14 PM
  4. Displaying Two BitMaps
    By kryptkat in forum Windows Programming
    Replies: 2
    Last Post: 12-24-2002, 07:15 PM
  5. Displaying bitmaps on a dialog
    By minesweeper in forum Windows Programming
    Replies: 2
    Last Post: 05-15-2002, 03:11 PM

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