|
-
August 22nd, 2008, 09:16 AM
#1
bitblt and bitmaps
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
Code:
#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?
-
August 22nd, 2008, 09:34 AM
#2
Re: bitblt and bitmaps
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?
Code:
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
Code:
x_pos=x;
y_pos=y;
BitBlt(_hdcunder,x_pos,y_pos,x_size,y_size,_hdc,0,0,SRCCOPY);
0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000 0000 0000 0000 0000
-
August 22nd, 2008, 10:19 AM
#3
Re: bitblt and bitmaps
no i'm loading them from a rc file
heres the test implementation
Code:
// 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;
}
-
August 22nd, 2008, 10:36 AM
#4
Re: bitblt and bitmaps
Did you try changing your first bitblt in your draw to this:
Code:
x_pos=x;
y_pos=y;
BitBlt(_hdcunder,x_pos,y_pos,x_size,y_size,_hdc,0,0,SRCCOPY);
0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000 0000 0000 0000 0000
-
August 23rd, 2008, 07:21 AM
#5
Re: bitblt and bitmaps
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
-
August 23rd, 2008, 07:44 AM
#6
Re: bitblt and bitmaps
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
Last edited by Joeman; August 23rd, 2008 at 09:33 AM.
0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000 0000 0000 0000 0000
-
August 23rd, 2008, 11:25 AM
#7
Re: bitblt and bitmaps
Okay. I figured it out for the most part .
Put this at the botoom of your constructor:
Code:
iBitmap = CreateCompatibleBitmap(_hdc, x_size, y_size);
SelectObject(_hdcunder, iBitmap);
Put this in your deconstructor:
Code:
DeleteObject(iBitmap);
Put this in your private variables:
0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000 0000 0000 0000 0000
-
August 24th, 2008, 06:13 AM
#8
Re: bitblt and bitmaps
Also your first bitblt is wrong
Change:
Code:
BitBlt(_hdcunder,x_pos=x,y_pos=y,x_size,y_size,_hdc,0,0,SRCCOPY);
to
Code:
BitBlt(_hdcunder,0,0,x_size,y_size,_hdc,x_pos=x,y_pos=y,SRCCOPY);
0100 0111 0110 1111 0110 0100 0010 0000 0110 1001 0111 0011 0010 0000 0110 0110 0110 1111 0111 0010
0110 0101 0111 0110 0110 0101 0111 0010 0010 0001 0010 0001 0000 0000 0000 0000 0000 0000 0000 0000
-
August 24th, 2008, 08:00 AM
#9
Re: bitblt and bitmaps
thanks Joeman, works just like i wanted it too now 
this is the final class
Code:
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;
};
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|