• Advertisement

Archived

This topic is now archived and is closed to further replies.

Here's the source code everyone asked for!!!

This topic is 5271 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here''s the source code for my buffer class and app. The buffer class:
//  "Buffer.h"


// Includes the standard windows header file

// Making sure to avoid redefinition

#ifndef _WINDOWS_H_
#define _WINDOWS_H_
    #include <windows.h>
#endif

// The Buffer class

class Buffer
{
public:
    // Constructors / Destructor

    Buffer ();
    Buffer( HWND hwnd );
    ~Buffer();

    // Buffer controls functions

    void CreateBuffer ( HWND hwnd );
    void ResizeBuffer( HWND hwnd );
    void DrawBufferToWindow();
    void ClearBuffer();
    void DrawBitmap( HBITMAP hbmBitmap, HBITMAP hbmMask, int x, int y );
    void DrawText( char* szText, char* szFont, int Size, COLORREF crColor, bool bBold, bool bItalic, bool bUnderline, int x, int y);

private:
    // Variables needed

    HWND        m_hwnd;
    HDC         m_hdcBuffer;
    HBITMAP     m_hbmBuffer;
    HBITMAP     m_hbmOldBuffer;
    RECT        m_rcDimentions;
    HDC         m_hdc;

    // Private Buffer control functions

    int GetStringLength( char* String );
};

// Constructor, sets most variables to null.

// This Constructor is used when making a global

// double buffer, it requires the use of the

// function void Buffer::CreateBuffer ( HWND hwnd )

// following the creation of the window used in the

// appliation. "hwnd" points to this window.

Buffer::Buffer ()
{
    m_hwnd = NULL;
    m_hdcBuffer = NULL;
    m_hbmBuffer = NULL;
    m_hbmOldBuffer = NULL;
    m_hdc = NULL;
}

// Constructor, creates the double buffer for "hwnd".

// This Constructor is used for making a local double

// buffer in the message handler of the application.

Buffer::Buffer( HWND hwnd )
{
    m_hwnd = hwnd;
    GetClientRect(m_hwnd, &m_rcDimentions);
    m_hdc = GetDC(m_hwnd);
    m_hdcBuffer = CreateCompatibleDC(m_hdc);
    m_hbmBuffer = CreateCompatibleBitmap(m_hdc, m_rcDimentions.right, m_rcDimentions.bottom);
    m_hbmOldBuffer = (HBITMAP)SelectObject(m_hdcBuffer, m_hbmBuffer);
    FillRect(m_hdcBuffer, &m_rcDimentions, (HBRUSH)GetStockObject(LTGRAY_BRUSH));
}

// Destructor, deletes the double buffer.

Buffer::~Buffer()
{
    SelectObject(m_hdcBuffer, m_hbmOldBuffer);
    DeleteDC(m_hdcBuffer);
    DeleteObject(m_hbmBuffer);
    ReleaseDC(m_hwnd, m_hdc);
}

// This function is used when the Buffer()

// Constructor is used.  This function creates

// the double buffer for "hwnd".

void Buffer::CreateBuffer ( HWND hwnd )
{
    m_hwnd = hwnd;
    GetClientRect(m_hwnd, &m_rcDimentions);
    m_hdc = GetDC(m_hwnd);
    m_hdcBuffer = CreateCompatibleDC(m_hdc);
    m_hbmBuffer = CreateCompatibleBitmap(m_hdc, m_rcDimentions.right, m_rcDimentions.bottom);
    m_hbmOldBuffer = (HBITMAP)SelectObject(m_hdcBuffer, m_hbmBuffer);
    FillRect(m_hdcBuffer, &m_rcDimentions, (HBRUSH)GetStockObject(LTGRAY_BRUSH));
}

// This function resizes the double buffer

// to the new size of the window in the

// application. "hwnd" points to the window.

void Buffer::ResizeBuffer( HWND hwnd )
{
    SelectObject(m_hdcBuffer, m_hbmOldBuffer);
    DeleteDC(m_hdcBuffer);
    DeleteObject(m_hbmBuffer);
    ReleaseDC(m_hwnd, m_hdc);

    m_hwnd = hwnd;
    GetClientRect(m_hwnd, &m_rcDimentions);
    m_hdc = GetDC(m_hwnd);
    m_hdcBuffer = CreateCompatibleDC(m_hdc);
    m_hbmBuffer = CreateCompatibleBitmap(m_hdc, m_rcDimentions.right, m_rcDimentions.bottom);
    m_hbmOldBuffer = (HBITMAP)SelectObject(m_hdcBuffer, m_hbmBuffer);
    FillRect(m_hdcBuffer, &m_rcDimentions, (HBRUSH)GetStockObject(LTGRAY_BRUSH));
}

// This function draws a bitmap to the double buffer,

// with "hbmBitmap" pointing to the bitmap, "hbmMask"

// is the mask for bitmap transparency for "hbmBitmap,

// and "x" and "y" are the position relative to the

// window of where the bitmap will be drawn.

void Buffer::DrawBitmap( HBITMAP hbmBitmap, HBITMAP hbmMask, int x, int y )
{
    BITMAP bitmap;

    HDC hdcMem = CreateCompatibleDC(m_hdcBuffer);
    HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, hbmMask);

    GetObject(hbmBitmap, sizeof(bitmap), &bitmap);

    BitBlt(m_hdcBuffer, x, y, bitmap.bmWidth, bitmap.bmHeight, hdcMem, 0, 0, SRCAND);

    SelectObject(hdcMem, hbmBitmap);
    BitBlt(m_hdcBuffer, x, y, bitmap.bmWidth, bitmap.bmHeight, hdcMem, 0, 0, SRCPAINT);

    SelectObject(hdcMem, hbmOld);
    DeleteDC(hdcMem);
}

// This function draws text to the double buffer at the

// postion specified by "x" and "y". "szText" is the text

// to be drawn and "szFont" is the font face that it uses,

// "Size" is the point size the text is drawn, "crColor"

// is the color of the text, "bBold" is true if the

// text is bold, "bItalic" is true if the text is italic,

// and "bUnderline" is true if the text is underlined.

void Buffer::DrawText( char* szText, char* szFont, int Size, COLORREF crColor, bool bBold, bool bItalic, bool bUnderline, int x, int y)
{

    HFONT hfFont;
    long lfHeight;
    
    HDC hdc2 = GetDC(NULL);
    lfHeight = -MulDiv(Size, GetDeviceCaps(hdc2, LOGPIXELSY), 72);
    ReleaseDC(NULL, hdc2);

    hfFont = CreateFont(lfHeight, 0, 0, 0, 700 * bBold, bItalic, bUnderline, 0, 0, 0, 0, 0, 0, szFont);

    HFONT hfFontOld = (HFONT)SelectObject(m_hdcBuffer, hfFont);

    SetBkColor(m_hdcBuffer, RGB(255,255,255));
    SetTextColor(m_hdcBuffer, crColor);

    SetBkMode(m_hdcBuffer, TRANSPARENT);
    TextOut(m_hdcBuffer, x, y, szText, GetStringLength(szText));
    SetTextColor(m_hdcBuffer, RGB(0,0,0));
    SelectObject(m_hdcBuffer, hfFontOld);
    DeleteObject(hfFont);

}

// This function draws the double buffer to the

// current window in "m_hwnd", the HWND variable

// for the class.

void Buffer::DrawBufferToWindow()
{
    BitBlt(m_hdc, 0, 0, m_rcDimentions.right, m_rcDimentions.bottom, m_hdcBuffer, 0, 0, SRCCOPY);
}

// This function clears the double buffer to

// a light gray color.

void Buffer::ClearBuffer()
{
    FillRect(m_hdcBuffer, &m_rcDimentions, (HBRUSH)GetStockObject(LTGRAY_BRUSH));
}

// This function returns the length of the string

// that is passed to it, used in the DrawText method.

int Buffer::GetStringLength( char* String )
{
    int Length;

    for(Length = 0; *String; String++, Length++);

    return Length;
}
The app:
#ifndef _WINDOWS_H_
#define _WINDOWS_H_
    #include <windows.h>
#endif
#include "Buffer.h"

const char game_name[] = "Bitmaps";

HWND game_window = NULL;

HINSTANCE game_instance;

Buffer bBuffer;

bool game_done = false;

HBITMAP hbmBall = NULL;
HBITMAP hbmMask = NULL;

void Wait( float Time )
{
    MSG msg;
    int Start = GetTickCount();
    while((GetTickCount() - Start) < (33 * Time))
    {
        if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
    }
}


HBITMAP CreateBitmapMask( HBITMAP hbmBitmap, COLORREF crTransparent )
{
    HDC hdcMem, hdcMem2;
    HBITMAP hbmMask;
    BITMAP bitmap;

    GetObject(hbmBitmap, sizeof(BITMAP), &bitmap);
    hbmMask = CreateBitmap(bitmap.bmWidth, bitmap.bmHeight, 1, 1, NULL);

    hdcMem = CreateCompatibleDC(0);
    hdcMem2 = CreateCompatibleDC(0);

    SelectObject(hdcMem, hbmBitmap);
    SelectObject(hdcMem2, hbmMask);

    COLORREF crOld = SetBkColor(hdcMem, crTransparent);

    BitBlt(hdcMem2, 0, 0, bitmap.bmWidth, bitmap.bmHeight, hdcMem, 0, 0, SRCCOPY);

    BitBlt(hdcMem, 0, 0, bitmap.bmWidth, bitmap.bmHeight, hdcMem2, 0, 0, SRCINVERT);

    SetBkColor(hdcMem, crOld);
    DeleteDC(hdcMem);
    DeleteDC(hdcMem2);

    return hbmMask;
}

LRESULT CALLBACK game_proc(HWND hwnd, UINT msg,WPARAM wParam,LPARAM lParam){

    switch(msg)
    {
    case WM_CREATE:
        hbmBall = (HBITMAP)LoadImage(NULL,"Redball.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
        hbmMask = CreateBitmapMask(hbmBall, RGB(255,255,255));
    case WM_SIZE:
        bBuffer.ResizeBuffer(hwnd);
        return 0;
    case WM_CLOSE:
		game_done = true;
		return 0;
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);
    }
}

int APIENTRY WinMain( HINSTANCE p_instance, HINSTANCE p_prev_instance, LPSTR p_cmd_line, int p_show )
{
    MSG msg;

	game_instance = p_instance;

	WNDCLASS window_class;

	window_class.style				= CS_OWNDC;
	window_class.cbClsExtra			= 0;
	window_class.cbWndExtra			= 0;
	window_class.hInstance			= game_instance;
	window_class.hIcon				= LoadIcon(NULL,IDI_WINLOGO);
	window_class.hCursor			= LoadCursor(NULL,IDC_ARROW);
	window_class.hbrBackground		= (HBRUSH)GetStockObject(LTGRAY_BRUSH);
	window_class.lpszMenuName		= NULL;
	window_class.lpszClassName		= "Game Class";
	window_class.lpfnWndProc		= game_proc;

    if(!RegisterClass(&window_class)){
        MessageBox(game_window, "Couldn''t register class", "Error", MB_OK | MB_ICONEXCLAMATION);
        return 0;
	}

    game_window = CreateWindow("Game Class",game_name,WS_OVERLAPPEDWINDOW | WS_VISIBLE,0,0,640,480,NULL,NULL,game_instance,NULL);

    bBuffer.CreateBuffer(game_window);

    int x = 100;
    int y = 100;
    char String[100];
    while(!game_done){
        if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
        if(GetKeyState(VK_UP)&0x80){
            y -= 10;
        }
        if(GetKeyState(VK_DOWN)&0x80){
            y += 10;
        }
        if(GetKeyState(VK_RIGHT)&0x80){
            x += 10;
        }
        if(GetKeyState(VK_LEFT)&0x80){
            x -= 10;
        }
        if(GetKeyState(''R'')&0x80){
            x = 100;
            y = 100;
        }
        bBuffer.ClearBuffer();
        wsprintf(String,"%d, %d",x,y);
        bBuffer.DrawText(String,"Comic Sans MS",20,RGB(255,255,255),1,0,0,0,0);
        bBuffer.DrawBitmap(hbmBall,hbmMask,x,y);
        bBuffer.DrawBufferToWindow();
        Wait(0.1);
    }

    if(!DestroyWindow(game_window)){
        MessageBox(game_window, "Couldn''t destroy window", "Error", MB_OK | MB_ICONEXCLAMATION);
        return 0;
	}

	game_window = NULL;

	if(!UnregisterClass("Game Class",game_instance)){
        MessageBox(game_window, "Couldn''t unregister class", "Error", MB_OK | MB_ICONEXCLAMATION);
        return 0;
	}

	return 0;
}
A lot of people wanted to see some code for my bitmap drawing to help me with the choppy movement, so here it is, HELP ME!!!

Share this post


Link to post
Share on other sites
Advertisement
<< A lot of people wanted to see some code for my bitmap drawing to help me with the choppy movement, so here it is, HELP ME!!! >>

Need to say more than HELP, too many messages in here, don''t have memory like elephants. Narrow down your problem, and ask very specific questions.

Phil P

Share this post


Link to post
Share on other sites
I don''t remember asking for this! I want my money back





--{You fight like a dairy farmer!}

Share this post


Link to post
Share on other sites

  • Advertisement