🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

displaying a bitmap

Started by
1 comment, last by jobu 24 years, 8 months ago
I'm not very sure but shouldn't use call getDC from the directdraw pointer (lpdd) since you are creating a fullscreen app.

Also try checking return values of window functions.

Advertisement
alright when i run my program it doesnt display my bitmap for some reason. is it easier for my bitmap to be called from a resource file a just straight from a file?
i also dont know if i called it right.


// INCLUDES ///////////////////////////////////////////////
#define WIN32_LEAN_AND_MEAN

#include (windows.h)
#include (windowsx.h)
#include (stdio.h)
#include (math.h)
#include (ddraw.h)
#include "resource.h"


char bname[]="imagerc.bmp";
HBITMAP hbitmap;
static BITMAP bm;
HDC hdc;
HDC hmdc;
HWND hwnd;


int ginit(void *parms=NULL);
int gshutdown(void *parms=NULL);
int gmain(void *parms=NULL);
// DEFINES ////////////////////////////////////////////////

// defines for windows
#define WINDOW_CLASS_NAME "WINCLASS1"
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
#define swidth 640
#define sheight 480
#define bpp 16

// GLOBALS ////////////////////////////////////////////////
HWND main_window_handle = NULL; // save the window handle
LPDIRECTDRAW lpdd;
// FUNCTIONS //////////////////////////////////////////////
LRESULT CALLBACK WindowProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
// this is the main message handler of the system
PAINTSTRUCT ps; // used in WM_PAINT
HDC hdc; // handle to a device context

// what is the message
switch(msg)
{
case WM_CREATE:
{
// do initialization stuff here
return(0);
} break;

case WM_PAINT:
{
// simply validate the window
hdc = BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);
return(0);
} break;

case WM_DESTROY:
{
// kill the application
PostQuitMessage(0);
return(0);
} break;

default:break;

} // end switch

// process any messages that we didn't take care of
return (DefWindowProc(hwnd, msg, wparam, lparam));

} // end WinProc

// WINMAIN ////////////////////////////////////////////////
int WINAPI WinMain( HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{

WNDCLASS winclass; // this will hold the class we create
HWND hwnd; // generic window handle
MSG msg; // generic message
hbitmap=LoadBitmap(hinstance,bname);

// first fill in the window class stucture
winclass.style = CS_DBLCLKS | CS_OWNDC |
CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hinstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = GetStockObject(WHITE_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;

// register the window class
if (!RegisterClass(&winclass))
return(0);

// create the window
if (!(hwnd = CreateWindow(WINDOW_CLASS_NAME, // class
"Hello", // title
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
0,0, // x,y
640,480, // width, height
NULL, // handle to parent
NULL, // handle to menu
hinstance,// instance
NULL))) // creation parms
return(0);

// save the window handle in a global
main_window_handle = hwnd;
ginit();
// enter main event loop
while(1)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
// test if this is a quit
if (msg.message == WM_QUIT)
break;

// translate any accelerator keys
TranslateMessage(&msg);

// send the message to the window proc
DispatchMessage(&msg);
} // end if
gmain();
// main game processing goes here

} // end while
gshutdown();
// return to Windows like this
return(msg.wParam);
}


int ginit(void *parms)
{
// this function is where you do all the initialization
// for your game

// create object and test for error
if (DirectDrawCreate(NULL,&lpdd,NULL)!=DD_OK)
return(0);

// set cooperation level to windowed mode normal
if ((lpdd->SetCooperativeLevel(main_window_handle,
DDSCL_ALLOWMODEX | DDSCL_FULLSCREEN |
DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT))!=DD_OK)
return(0);

// set the display mode
if ((lpdd->SetDisplayMode(swidth,sheight,bpp))!=DD_OK)
return(0);

// return success
return(1);
} // end Game_Init

///////////////////////////////////////////////////////////

int gshutdown(void *parms)
{
// this function is where you shutdown your game and
// release all resources that you allocated

// release the directdraw object
if (lpdd!=NULL)
lpdd->Release();
DeleteDC(hmdc);
DeleteObject(hbitmap);
// return success
return(1);
} // end Game_Shutdown

///////////////////////////////////////////////////////////

int gmain(void *parms)
{

hdc=GetDC(hwnd);
hmdc=CreateCompatibleDC(hdc);
SelectObject(hmdc,hbitmap);
GetObject(hbitmap,sizeof(bm),(LPSTR) &bm);
BitBlt(hdc,0,0,bm.bmWidth,bm.bmHeight,hmdc,0,0,SRCCOPY);

if (KEY_DOWN(VK_ESCAPE) | | KEY_DOWN(VK_SPACE))
PostMessage(main_window_handle, WM_DESTROY,0,0);


ReleaseDC(hwnd,hdc);
// return success
return(1);
} // end Game_Main

///////////////////////////////////////////////////////////



the GetDC() function should work just fine. there is no conflict between DD and GDI.

i did notice a few things in your code that are very likely the problem, or likely to cause problems.

first, you are using LoadBitmap, and you are passing the instance handle of your application. since the name of your bitmap is "imagerc.bmp", which is a file name, your bitmap is never getting loaded, since you have to load the bitmap from file, without passing an instance handle.

LoadImage(NULL,bname,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);

should work.

next, ever game loop, you are creating a new Compatible DC, and selecting the main bitmap into it. this is bad. you're leaking GDI resources. create the compatible DC *ONCE* in your init function, select the bitmap into it *ONCE*.

also, you arent putting all of your toys away when you are done. you need to store the return value of the SelectObject in something like hbmOld in your ginit function, and restore it in your cleanup function. when a compatible DC is created, a 1x1 monochrome bitmap is created along with it, selected in that DC. to replace, it, you put it somewhere else for safekeeping, and restore it when you are done with the DC (you dont have to use DeleteObject on it, the call to DeleteDC takes care of it for you).

Get off my lawn!

This topic is closed to new replies.

Advertisement