GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: Add Line Numbers


The Whole Damned Thing by jnighthawk
#include "cBuffer.h"
#include <string>
using namespace std;

//////////////////////////////////////////////////////////////////////////
/////
///// FUNCTION:    CreateDoubleBuffering()
///// DESCRIPTION: This sets up the double buffering by filling in the 
/////               buffer structure that is passed in.
///// INPUT:       hwnd    - The window handle
///// OUTPUT:      pBuffer - The back buffer structure to fill in
///// RETURN:      nothing
/////
//////////////////////////////////////////////////////////////////////////

void cBuffer::CreateDoubleBuffering(HWND hwnd)
{
    // Assign the window handle to our structure
    hWnd = hwnd;                                    
    
    // Store the client rectangle in our structure
    GetClientRect(hwnd, &scrnRect);

    // Get a handle to the device context and store it as the front buffer
    hdcFront = GetDC(hWnd);

    // Create a compatible device context for the backbuffer
    hdcBack = CreateCompatibleDC(hdcFront);

    // Create another hdc to store the bitmap in before the backbuffer
    hdcBitmap = CreateCompatibleDC(hdcFront);

    // Create a dummy bitmap handle to store in the front buffer
    hCompBitmap = CreateCompatibleBitmap(hdcFront, scrnRect.right, scrnRect.bottom);

    // Select the dummy bitmap handle into the back buffer to make it like the front hdc
    hOldBitmap = (HBITMAP)SelectObject(hdcBack, hCompBitmap);    
}

//////////////////////////////////////////////////////////////////////////
/////
///// FUNCTION:    LoadBitmap()
///// DESCRIPTION: This loads a bitmap and returns a handle to that bitmap.
///// INPUT:       szFileName - The name of the file to load (Ie. "Bitmap.bmp")
///// OUTPUT:      nothing
///// RETURN:      HBITMAP - The handle that references the bitmap loaded
/////
//////////////////////////////////////////////////////////////////////////

HBITMAP cBuffer::LoadBitmap(string szFileName)
{
    // Load the bitmap and return the handle to the bitmap we just loaded
    char * szTemp = new char[szFileName.length() + 1];
    strcpy(szTemp, szFileName.c_str());
    //char * szTemp = "images\\RedTile.bmp\0";
    MessageBox(hWnd, szTemp, "Test", MB_OK);
    HBITMAP hbmapTemp = (HBITMAP)LoadImage(GetModuleHandle(NULL), szTemp, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    //HBITMAP hbmapTemp = (HBITMAP)LoadImage(NULL, "images\\RedTile.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    delete [] szTemp;
    //HBITMAP hbmapTemp = (HBITMAP)LoadImage(NULL, szFileName.c_str(), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    if(hbmapTemp) {
        DJBitmap DJBTemp;
        DJBTemp.hBitmap = hbmapTemp;
        DJBTemp.szName = szFileName.substr(7, szFileName.length() - 11);
        Bitmaps.push_back(DJBTemp);
    }
    return hbmapTemp;
}

//////////////////////////////////////////////////////////////////////////
/////
///// FUNCTION:    DisplayBitmap()
///// DESCRIPTION: This puts the bitmap in the backbuffer
///// INPUT:       hBitmap - The handle to the bitmap to display
/////               x, y    - The X and Y coordinates to display the bitmap
///// OUTPUT:      pBuffer - The back buffer that holds the buffer information
///// RETURN:      nothing
/////
//////////////////////////////////////////////////////////////////////////

void cBuffer::DisplayBitmap(int iBitmapID, int iXLoc, int iYLoc, bool bSwap)
{
    // Select the bitmap handle into the extra hdc that holds the bitmap
    hOldBitmap2 = (HBITMAP)SelectObject(hdcBitmap, Bitmaps[iBitmapID].hBitmap);

    // Blit the bitmap hdc into the backbuffer
    BitBlt(hdcBack, iXLoc, iYLoc, scrnRect.right, scrnRect.bottom,  hdcBitmap, 0, 0, SRCCOPY);

    // Select the old handle to the bitmap back where it was to clean up
    SelectObject(hdcBitmap, hOldBitmap2);
    if(bSwap)
        SwapBackBuffer(false);
}

void cBuffer::DisplayBitmap(string szBitmapName, int iXLoc, int iYLoc)
{
    //This hasn't been implemented yet.
}

//////////////////////////////////////////////////////////////////////////
/////
///// FUNCTION:    ClearScreen()
///// DESCRIPTION: This fills the rectangle passed in with a specified color
///// INPUT:       hdc      - The handle to the device context to fill
/////              scrnRect - The screens rectangle to fill in
/////               color    - The color to fill the rectangle in (Ie, WHITE_BRUSH)
///// OUTPUT:      nothing
///// RETURN:      nothing
/////
//////////////////////////////////////////////////////////////////////////

void ClearScreen(HDC hdc, RECT scrnRect, int color)
{
    // Fill the backbuffer with white to erase the previous position of the bitmap
    FillRect(hdc, &scrnRect, (HBRUSH)GetStockObject(color));
}

//////////////////////////////////////////////////////////////////////////
/////
///// FUNCTION:    SwapBackBuffer()
///// DESCRIPTION: This flips the backbuffer to the front of the screen,
/////               which displays the bitmaps to the screen.
///// INPUT:       bClearBackBuffer - A flag to clear the backbuffer or not
///// OUTPUT:      pBuffer - The back buffer info to swap
///// RETURN:      nothing
/////
//////////////////////////////////////////////////////////////////////////

void cBuffer::SwapBackBuffer(bool bClearBackBuffer)
{
    // Blit the whole backbuffer to the screen (The front buffer)
    BitBlt(hdcFront, scrnRect.left, scrnRect.top, scrnRect.right, scrnRect.bottom, hdcBack, 0, 0, SRCCOPY);

    // If we want to clear the screen afterwards, set the background to white
    if(bClearBackBuffer)
        ClearScreen(hdcBack, scrnRect, WHITE_BRUSH);
}

//////////////////////////////////////////////////////////////////////////
/////
///// FUNCTION:    ResizeBuffers()
///// DESCRIPTION: Changes the size of the front and back buffers according 
/////               to the new client size of the window.
///// INPUT:       nothing
///// OUTPUT:      pBuffer - The back buffer info to resize
///// RETURN:      nothing
/////
//////////////////////////////////////////////////////////////////////////

void cBuffer::ResizeBuffers()
{
    // Get the new client rectangle of the window
    GetClientRect(hWnd, &scrnRect);
            
    // Select the old bitmap back into the back buffer
    SelectObject(hdcBack, hOldBitmap);

    // Delete the old compatible bitmap
    DeleteObject(hCompBitmap);

    // Create a new compatible bitmap with the new size of the window
    hCompBitmap = CreateCompatibleBitmap(hdcFront, scrnRect.right, scrnRect.bottom);

    // Select the new compatible bitmap into the backbuffer and store the old one again
    hOldBitmap = (HBITMAP)SelectObject(hdcBack, hCompBitmap);    
}    

void cBuffer::PrintColoredText(string szText, COLORREF Color, int iXLoc, int iYLoc)
{
    if(!szText.c_str())
        return;
    SetTextColor(hdcBack, Color);
    TextOut(hdcBack, iXLoc, iYLoc, szText.c_str(), szText.length());
    SwapBackBuffer(FALSE);
}

cBuffer::~cBuffer()
{
    //for(int iTemp = 0 ; iTemp < Bitmaps.size() ; iTemp++) {
    //    DeleteObject(Bitmaps[iTemp].hBitmap);
    //}
}