• Advertisement

Archived

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

help in putting a bitmap

This topic is 5936 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

hi! please help i''m stuck for a week now figuring out how to load and show a bitmap to the screen. please mention step by step including the functions that i''ll be using. thanks!

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
please mention libray/API u r using.. (eg: DirectX, OpenGL, etc..)

(always anonymous)

Share this post


Link to post
Share on other sites
okey this is what i did,

// somewhere in the winmain(HISNTANCE hInstance......):
HBITMAP bmp;
bmp = LoadBitmap(hInstance, "xxxxxx.bmp");
//in window procedure:
HDC hdc;
//in the WM_PAINT:
GetDC(&hdc);
SelectObject(hdc, bmp);
GetObject(bmp, sizeof(BITMAP), &bmp);
BitBlt(hdc, 100, 100, 300, 300, hdc, 0, 0, SRCCOPY);

there! can''t get to show it on my screen

Share this post


Link to post
Share on other sites
You aren''t using directx here. You''re attempting to use GDI, which is windows'' internal graphics interface. Its very slow compared to directx. What you want to do is set up directx, and texture a quad with your bitmap. If you look in the directx sdk docs, there are tutorials with code on exactly how to do this.
Good luck.

"You are too useless. And now I must beat you." - English subtitle in a Honk Kong Movie.

Share this post


Link to post
Share on other sites
lunarss is right, there''s no DX in your code. However, if you want to try GDI before tackling DX (which might be a good idea), read about BitBlt in the manual. The problem is that you''re trying to BitBlt a DC to itself (using the same argument for hdcDest and hdcSrc). You need to create an off-screen DC, select the bitmap into that, and blit it to your window''s DC.

Try something like this for the WM_PAINT handler:

HDC hTempDC = CreateCompatibleDC(NULL); // Create an off-screen DC
HBITMAP hOldBmp = (HBITMAP)SelectObject(hTempDC, bmp); // Select the bitmap
BitBlt(hDC, 100, 100, 300, 300, hTempDC, 0, 0, SRCCOPY); //Blit
SelectObject(hTempDC, hOldBmp); // Restore the off-screen DC
DeleteDC(hTempDC); // Delete the off-screen DC

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hi! actually i was using DX i just omitted the stuffs like

if (lpDDSPrimary->GetDC(&hdc) == DD_OK) { etc.,

to simplify things, actually i know basically how it is done, i just can''t figure it out how to blit my "own" bitmaps. maybe it has something to do with the resource tab?? coz what i do is,
right-click on resource folder on the workspace manager, click on add files to folder and chose one of my bitmap say for example xxxx.bmp, then on my code whereas the line:

hBitmap = LoadBitmap (hInstance, "xxxx.bmp");

i put on the 2nd parameter the filename of my bitmap, is that wrong? actually i already tried copy and paste from someone else code already like for example:

hBitmap = LoadBitmap (hInstance, TEXT ("Bricks")) ;

and i just replaced that 2nd parameter with my bitmap''s filename and there it is, doesn''t work!

p.s. my bmp file also resides on the same directory.

Share this post


Link to post
Share on other sites
Sorry, I don''t get it. Do you load the bitmap from disk or from a resource?

Check the manual on handling errors from LoadBitmap(). The error code will probably tell you something about what''s wrong.

Share this post


Link to post
Share on other sites
i think that''s my problem Ulf O, what i did is, i inserted a bmp file in my workspace and i used that bmp filename on the second parameter of the LoadBitmap() function i mean just try mention anything it might help me solve my problem, thanks

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
plz help! i really can''t figure it out! okey i want to load my bitmap from a resource file. this is what''s inside my .rc file:

// resource.rc
IDB_BITMAP1 BITMAP DISCARDABLE "bitmap1.bmp"

// then in my WM_CREATE, i did this
HBITMAP hbitmap = LoadBitmap(hInstance, IDB_BITMAP1);
// which returns error indicating cannot convert char to int something like that,

// so i changed it to
HBITMAP hbitmap = LoadBitmap(hInstance, "bitmap1.bmp");
GetObject (hBitmap, sizeof (BITMAP), &bitmap) ; // do i have to use this?
// then it works no error, but still it doesn''t blit my bitmap! Please tell me what''s wrong with it, already been through with a lot of books and all of them offers almost the same style on blitting a bitmap why can''t i make mine work! thanks!

Share this post


Link to post
Share on other sites
To load from a resource, try
HBITMAP hbitmap = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP1));

Why LoadBitmap(hInstance, "bitmap1.bmp") doesn''t work, I really can''t know. Have you tried it with full file path? You have to call GetLastError() and look up the error code in MSDN or the Error Lookup utility (ERRLOOK.EXE).

But first of all, go to MSDN (http://msdn.microsoft.com/library/default.asp) and search for LoadBitmap. Spend 5 minutes reading the article.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
okey i got it! i mean, i know now why my line

HBITMAP hbitmap = LoadBitmap(hInstance, IDB_BITMAP1);

doesn''t work! because it''s second parameter is not fit to retrieve this kind of file then i tried using MFC''s LoadBitmap, they have the parameter which you can then use your bitmap''s ID to load your bitmap! Then there I was able to blit now my bitmap. But the problem is, i don''t want to use MFC as for now coz it''s another learning curve. So how will i use Windows GDI''s function LoadBitmap to load my bitmap? and what''s LoadImage for? Thanks!

And another thing, hope you guys are patient on my questions, does the characters/monsters/terrains say for example in Diablo 2, are first done by freehand then they port it to the computer by scanning it then they use for example Photoshop to edit and enhance their drawings and then save their work as .bmp file and then lastly in their c/c++ codes they blit these .bmp to the screen?

Share this post


Link to post
Share on other sites
Sorry wasn''t able to read first your post anyway okey i''ll try your code,

HBITMAP hbitmap = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BITMAP1));

hope this thing works, just ignore my first question thanks!

Share this post


Link to post
Share on other sites
HAHAHA it worked!!!! hahaha! Hehe
thanks! anyway, um is that function/style is still being used nowadays? or are there any other faster more efficient ways?

Share this post


Link to post
Share on other sites
hi mickey

what I did was create an extra surface, so now I have 3 surfaces
Front-buffer, Back-buffer, tiles-buffer
then I loaded my bitmaps onto the tiles-buffer
then when I need a tile I Blit it to the back-buffer
and at the end of my game loop I Flip my back-buffer with my Frontbuffer
I think this is a good way
If any1 else knows a better way pls tell so, as I''m also a newbie and willing to learn more...

Da Cobra

Share this post


Link to post
Share on other sites
yup that''s good, anyway, i don''t get it how you load your bitmaps unto your code, is it this line where you load your bitmaps?

LoadImage(G.hinstance, File, IMAGE_BITMAP, 0, 0,
LR_CREATEDIBSECTION |LR_LOADFROMFILE)

where file is the name of your bitmap? and how come you don''t use rc files?

anyway your code is great, you have a lot of wrappers for window''s functions right?

Share this post


Link to post
Share on other sites
mickey: Sure it''s being used. LoadBitmap() should be fast enough in most cases, unless you want to write your own bitmap loader that''s optimized for some bit depth etc.

I hope you don''t mind a piece of general advice, but you seem to be using an awful lot of trial and error when coding. I suggest you get into the habit of reading the MSDN article before using a new function. Then maybe you won''t have to spend an entire week trying to figure out how to load a bitmap...

Share this post


Link to post
Share on other sites
Hi! Do i always have to use the MAKEINTRESOURCE macro whenever i want to load my own bitmap or is there any other more efficient and faster way?

Share this post


Link to post
Share on other sites
I do i read those articles, i also have a dozen books here about programming. Dunno maybe i really just can''t understand that part. I''ve already read more than 10 articles about blitting a bitmap and I didn''t know it''s just that simple, MAKEINTRESOURCE!

Share this post


Link to post
Share on other sites

  • Advertisement