Advertisement

Loading into memory

Started by March 21, 2000 12:19 AM
9 comments, last by Fwar 24 years, 5 months ago
Hey everyone, i''m really new at this programing, i''m trying to start off with C++ and the WINDOWS API... its really frusterating. I was wondering if anyone knows how to Load a picture into memory so when you change its position it doesn''t just blit the same bitmap again. I just want it to move, it really slows dowm my computer the way i have it set up. ANY HELP QUICK WOULD BE VERY APPRECIATED.
Could you please be a little more specific about the - "so when you change its position it doesn''t just blit the same bitmap" - part?

I don''t understand what you want to do. WinAPI is slow when it come to blitting big bitmaps out of memory, that''s a fact.

============================
Daniel Netz, Sentinel Design
"I'm not stupid, I'm from Sweden" - Unknown
============================Daniel Netz, Sentinel Design"I'm not stupid, I'm from Sweden" - Unknown
Advertisement
Sorry i wasn't specific. Ok i press any arrow key, and the bitmap moves. What it does is just blits another bitmap 5 pixels to the Right(or left, which ever arrow i press). Instead of that, i want the bitmap to shift positions, instead of re-blitting it. I'm using really small bitmaps if that makes you happy .

i hope this helps you understand it, and sorry i wasn't clear.

Edited by - Fwar on 3/21/00 12:56:11 AM
I don''t think you can just shift the bitmap without redrawing the screen ( the previous location of the bitmap and the new position)

If u are using small bitmaps, it wouldn''t be that small even with win32,... unless u are loading in the bitmap everytime u blt.

If so, just create a global HDC or HBITMAP, call LoadImage to load it in there, and use SelectObject to select it before bltting (maybe the selectobject is not necessary. Sorry, this is top of my head)
So what your saying is that you would do exactly as i am(which you are) but it will always go slow... damn, I'll try and explain it again, i know there is a way to get it to go fast. I'm thinking you have to load the bitmap (loadimage()) and when i press the arrow it loads it every time the button is pressed(repeat rate) 5 pixels in whatever direction, and you have to deleteDC or DeleteObject, but i don't know how, can someone write down whats wrong with this code.


case VK_LEFT: LoadThePicture0( hwnd );
xpos = xpos - 10;
if ( xpos <= 10 )
{
xpos = xpos + 10;
}
break;

THIS is when i press the down arrow key, then when it paints it on...


case WM_PAINT:
hdc = BeginPaint( hwnd, &ps ); GetClientRect( hwnd, ▭ );

LoadThePicture0( hwnd ); LoadThePicture( hwnd );
FillRect( hdc, &rr, (HBRUSH)GetStockObject ( WHITE_BRUSH ) );

BitBlt( hdc, xpos, ypos, IW, IH, hdcImage0, 0, 0, SRCCOPY );
BitBlt( hdc, 10, 300, IW, IH, hdcImage, 0, 0, SRCCOPY );

DeleteDC( hdc );
DeleteDC( hdcImage );
DeleteDC( hdcImage0 );
DeleteObject( hdc );
DeleteObject( hdcImage );
DeleteObject( hdcImage0 );
ReleaseDC( hwnd, hdc );
ReleaseDC( hwnd, hdcImage0 );
ReleaseDC( hwnd, hdcImage );
EndPaint( hwnd, &ps );
return FALSE;



Edited by - Fwar on 3/21/00 2:18:19 AM
Well, i can tell you a few things : first off you release the main HDC that you got by a call to GetDC (well at least you probably did ), with ReleaseDC(hwnd,hdc), secondly you delete the HDCs created with CreateDC and CreateCompatibleDC etc...with DeleteDC(hdcImage), and thirdly you delete all HBITMAPs with a call to DeleteObject(Image), so complete your deletions should be

ReleaseDC(hwnd,hdc); // In WM_PAINT where it is

// Usually I put these in WM_DESTROY, just a thought
DeleteDC(hdcImage);
DeleteDC(hdcImage0);
DeleteObject(Image); // Or whatever name for your images
DeleteObject(Image0);

also what are Image and Image0 exactly? If you're only blitting one bitmap I don't see any use for two images, but that's just me also, usually you just load each image once with a LoadBitmap (Borland Compilers) or LoadImage (MSVC, I think) (look up documentation for params) in WM_CREATE or WinMain, and then in WM_PAINT SelectObject each one into a [single] temp HDC, and then blit each one in succession like so :

HDC hdc;
HDC tempdc;
HBITMAP image;
HBITMAP image0;

case WM_CREATE:
hdc = GetDC(hWnd);
tempdc = CreateCompatibleDC(hdc);
image = LoadImage(...);
image0 = LoadImage(...);
break;
...
case WM_KEYDOWN:
switch(wParam){
case VK_LEFT:
if(xpos>10)
xpos-=10;
break;
}
break;
...
case WM_PAINT:
hdc = BeginPaint(hwnd,&ps);
GetClientRect(hwnd, ▭);
FillRect(hdc, ▭, (HBRUSH)GetStockObject(WHITE_BRUSH));
SelectObject(tempdc,image);
BitBlt( hdc, xpos, ypos, IW, IH, tempdc, 0, 0, SRCCOPY );
SelectObject(tempdc,image0);
BitBlt( hdc, 10, 300, IW, IH, tempdc, 0, 0, SRCCOPY );
EndPaint(hwnd,&ps);
ReleaseDC(hwnd,hdc);
break;


Sorry if my post doesn't make since I should've been in bed a few hours ago, well good night, hope this raving made at least some sense and helped a little

Oh, and one more thing that may help with flickering problems (works for me at least )

case WM_ERASEBKGND:
return TRUE;

Edited by - BraveZeus on 3/21/00 7:13:09 AM
Chris
Advertisement
Ok, BraveZues i must be doing something wrong when doing what you said cause it has the warnings that say the tempdc , and image''s aren''t being initiallized, what would you suggest??

Hey, Fwar, can you just send me your program source so I can look at it and maybe figure out what''s wrong? Sorry, but I just have no idea what it would be unless I look at all of your code I guess if you wanna just post it, that''d be fine with me, anyway, bye.
Chris
I meant moving the loading of the picture outside of the WM_PAINT but somewhere in the initialization (perhaps WM_CREATE)

I see u are loading the bitmap from the file everytime receive WM_PAINT, which will be VERY SLOW>
Hey Bravezeus thats a geat idea. i''ll send you my code, do you have an ICQ # or anything around that nature?

This topic is closed to new replies.

Advertisement