Click to See Complete Forum and Search --> : bitblt and bitmaps


g3RC4n
August 22nd, 2008, 09:16 AM
i want to make a bitmap class where i can draw a bitmap on screen

but i also want to be able to undraw the bitmap, so therefore i also want to know whats underneath the bitmap


#pragma once

#include <windows.h>

class bitmap{
public:
bitmap(HWND hwnd,HINSTANCE hinstance, LPCTSTR str):
_hwnd(hwnd),
_hdc(GetDC(_hwnd)),
_hbitmap(LoadBitmap(hinstance,str)),
_hdcmem(CreateCompatibleDC(_hdc)),
_hdcunder(CreateCompatibleDC(_hdc)),
x_pos(0),
y_pos(0){
SelectObject(_hdcmem,_hbitmap);
BITMAP bitmap;
GetObject(_hbitmap,sizeof(BITMAP),&bitmap);
x_size = bitmap.bmWidth;
y_size = bitmap.bmHeight;
}
~bitmap(){
DeleteDC(_hdcmem);
ReleaseDC(_hwnd,_hdc);
}
void draw(int x, int y){
BitBlt(_hdcunder,x_pos=x,y_pos=y,x_size,y_size,_hdc,0,0,SRCCOPY);
BitBlt(_hdc,x_pos,y_pos,x_size,y_size,_hdcmem,0,0,SRCCOPY);
}
void undraw(){
BitBlt(_hdc,x_pos,y_pos,x_size,y_size,_hdcunder,0,0,SRCCOPY);
}
private:
HWND _hwnd; //windows handle
HDC _hdc; //dc for painting
HBITMAP _hbitmap; //bitmap handle
HDC _hdcmem; //bitmap memory
HDC _hdcunder; //memory fro what was under the bitmap that i drew
int x_size, //metrics of bitmap
y_size;
int x_pos,//position of bitmap for undrawing it
y_pos;
};


/what i'm trying to do is store what is underneath the position where the bitmap will be drawin to be put in _hdcunder, the draw the bitmap, then when undraw() is called it will putback the original imiage, why doesn't this work?

Joeman
August 22nd, 2008, 09:34 AM
I haven't tried to run it, but I do see a few problems

Are you using LoadBitmap to load from a file on the hard drive?

BitBlt(_hdcunder,x_pos=x,y_pos=y,x_size,y_size,_hdc,0,0,SRCCOPY);


x_pos = x and y_pos = y will always equal to 1

x_pos=x;
y_pos=y;
BitBlt(_hdcunder,x_pos,y_pos,x_size,y_size,_hdc,0,0,SRCCOPY);

g3RC4n
August 22nd, 2008, 10:19 AM
no i'm loading them from a rc file

heres the test implementation

// bitmap_class.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "bitmap_class.h"

#include "bitmap.h"

#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name

// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);

// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;

// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_BITMAP_CLASS, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_BITMAP_CLASS));

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return (int) msg.wParam;
}



//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage are only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_BITMAP_CLASS));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_BITMAP_CLASS);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

return RegisterClassEx(&wcex);
}

//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst = hInstance; // Store instance handle in our global variable

hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

return TRUE;
}

//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
static int x(0),y(0);

HINSTANCE hinstance;

static bitmap* bmp;
static bitmap* backround;

switch (message)
{
case WM_CREATE:
hinstance = ((LPCREATESTRUCT) lParam)->hInstance;;
bmp = new bitmap(hWnd,((LPCREATESTRUCT) lParam)->hInstance,TEXT("bitmap_a"));
backround = new bitmap(hWnd,((LPCREATESTRUCT) lParam)->hInstance,TEXT("bitmap_b"));
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
backround->draw(100,100);
bmp->draw(x,y);
ValidateRect(hWnd,0);
break;
case WM_KEYDOWN:
bmp->undraw();
switch(wParam){
case VK_UP:
--y;
break;
case VK_DOWN:
++y;
break;
case VK_RIGHT:
++x;
break;
case VK_LEFT:
--x;
break;
default:
break;
}
bmp->draw(x,y);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}

Joeman
August 22nd, 2008, 10:36 AM
Did you try changing your first bitblt in your draw to this:

x_pos=x;
y_pos=y;
BitBlt(_hdcunder,x_pos,y_pos,x_size,y_size,_hdc,0,0,SRCCOPY);

g3RC4n
August 23rd, 2008, 07:21 AM
yep, and i'm pretty shore i don't need to as it should be evaluated to the value of x anyway as it's equivilent to



(x_pos=x,x_pos)

Joeman
August 23rd, 2008, 07:44 AM
oh right. I was thinking of an if statement :( where if( x = x_pos) would equal true. I will look better now :). I will reread the code. Can you upload a working project? It would make it much easier for me to run to debug.

EDIT: Nevermind. I have worked a small project out. I am trying to fix your problem. It seems like _hdcunder doesn't work correctly

Joeman
August 23rd, 2008, 11:25 AM
Okay. I figured it out for the most part :D.

Put this at the botoom of your constructor:

iBitmap = CreateCompatibleBitmap(_hdc, x_size, y_size);
SelectObject(_hdcunder, iBitmap);


Put this in your deconstructor:

DeleteObject(iBitmap);


Put this in your private variables:

HBITMAP iBitmap;

Joeman
August 24th, 2008, 06:13 AM
Also your first bitblt is wrong

Change:

BitBlt(_hdcunder,x_pos=x,y_pos=y,x_size,y_size,_hdc,0,0,SRCCOPY);


to


BitBlt(_hdcunder,0,0,x_size,y_size,_hdc,x_pos=x,y_pos=y,SRCCOPY);

g3RC4n
August 24th, 2008, 08:00 AM
thanks Joeman, works just like i wanted it too now :)

this is the final class

class bitmap{
public:
bitmap(HWND hwnd,HINSTANCE hinstance, LPCTSTR str):
_hwnd(hwnd),
_hdc(GetDC(_hwnd)),
_hbitmap(LoadBitmap(hinstance,str)),
_hdcmem(CreateCompatibleDC(_hdc)),
_hdcunder(CreateCompatibleDC(_hdc)),
x_pos(0),
y_pos(0){
SelectObject(_hdcmem,_hbitmap);
BITMAP bitmap;
GetObject(_hbitmap,sizeof(BITMAP),&bitmap);
x_size = bitmap.bmWidth;
y_size = bitmap.bmHeight;
iBitmap = CreateCompatibleBitmap(_hdc, x_size, y_size);
SelectObject(_hdcunder, iBitmap);
}
~bitmap(){
DeleteObject(iBitmap);
DeleteDC(_hdcunder);
DeleteDC(_hdcmem);
ReleaseDC(_hwnd,_hdc);
}
void draw(int x, int y){
x_pos = x;
y_pos = y;
BitBlt(_hdcunder,0,0,x_size,y_size,_hdc,x_pos=x,y_pos=y,SRCCOPY);
BitBlt(_hdc,x_pos,y_pos,x_size,y_size,_hdcmem,0,0,SRCCOPY);
}
void undraw(){
BitBlt(_hdc,x_pos,y_pos,x_size,y_size,_hdcunder,0,0,SRCCOPY);
}
private:
HWND _hwnd; //windows handle
HDC _hdc; //dc for painting
HBITMAP _hbitmap; //bitmap handle
HDC _hdcmem; //bitmap memory
HDC _hdcunder; //memory fro what was under the bitmap that i drew
int x_size, //metrics of bitmap
y_size;
int x_pos,//position of bitmap for undrawing it
y_pos;
HBITMAP iBitmap;
};