Archived

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

Aggro5288

Loading Bitmap Problem

Recommended Posts

Hi all ... I just started working with bitmaps in dx ... ive made pong in gdi and am now trying to do it in DX ... to start ive basically copy and pasted code from other things to start out, when I compile it I only get a black screen, do you see what's wrong with it? I am gonna include all of it so I don't leave anything out
  
// INCLUDES ///////////////////////////////////////////////


#define WIN32_LEAN_AND_MEAN   

#define INITGUID

#include <windows.h>   //windows
#include <windowsx.h> 
#include <mmsystem.h>
#include <iostream.h> // C++
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h> 
#include <math.h>
#include <io.h>
#include <fcntl.h>


#include <ddraw.h> // Directdraw
#include "t3dlib1.h"



// prototypes



// DEFINES ////////////////////////////////////////////////


// defines for windows 

#define WINDOW_CLASS_NAME "WINCLASS1"

// default screen size

#define SCREEN_WIDTH    640  // size of screen
#define SCREEN_HEIGHT   480
#define SCREEN_BPP      8    // bits per pixel
#define MAX_COLORS      256  // maximum colors
#define BITMAP_ID            0x4D42 // universal id for a bitmap
#define MAX_COLORS_PALETTE   256




// TYPES //////////////////////////////////////////////////////


// basic unsigned types

typedef unsigned short USHORT;
typedef unsigned short WORD;
typedef unsigned char  UCHAR;
typedef unsigned char  BYTE;

// MACROS /////////////////////////////////////////////////


#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEYUP(vk_code)   ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

// initializes a direct draw struct

#define DD_INIT_STRUCT(ddstruct) { memset(&ddstruct,0,sizeof(ddstruct)); ddstruct.dwSize=sizeof(ddstruct); }

// GLOBALS ////////////////////////////////////////////////

HWND      main_window_handle = NULL; // globally track main window

HINSTANCE hinstance_app      = NULL; // globally track hinstance

BITMAP_FILE           bitmap;                // holds the bitmap 




BITMAP_IMAGE paddle1;  

char buffer[80];                     // general printing buffer


// 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

char buffer[80];        // used to print strings


// what is the message 

switch(msg)
	{	
	case WM_CREATE: 
        {
		// do initialization stuff here

        // return success

		return(0);
		} break;
   
	case WM_PAINT: 
		{
		// simply validate the window 

   	    hdc = BeginPaint(hwnd,&ps);	 
        
        // end painting

        EndPaint(hwnd,&ps);

        // return success

		return(0);
   		} break;

	case WM_DESTROY: 
		{

		// kill the application, this sends a WM_QUIT message 

		PostQuitMessage(0);

        // return success

		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





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


int Game_Main(void *parms = NULL, int num_parms = 0)
{
// this is the main loop of the game, do all your processing

// here


// for now test if user is hitting ESC and send WM_CLOSE

if (KEYDOWN(VK_ESCAPE))
   SendMessage(main_window_handle,WM_CLOSE,0,0);




if (FAILED(lpddsprimary->Lock(NULL, &ddsd,
                   DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,
                   NULL)))
   {
   // error

   return(0);
   } // end if



// clear the drawing surface

DDraw_Fill_Surface(lpddsback, 0);

// lock the back buffer

DDraw_Lock_Back_Surface();

// draw the background reactor image

Draw_Bitmap(&paddle1, back_buffer, back_lpitch, 0);

// unlock the back buffer

DDraw_Unlock_Back_Surface();

// now ddsd.lPitch is valid and so is ddsd.lpSurface


// make a couple aliases to make code cleaner, so we don't

// have to cast

int mempitch        = (int)ddsd.lPitch;
UCHAR *video_buffer = (UCHAR *)ddsd.lpSurface;

// copy the bitmap image to the primary buffer line by line

// note this is a good candidate operation to make into a function - hint!


// lock the primary surface

lpddsprimary->Lock(NULL,&ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,NULL);

// get video pointer to primary surfce

UCHAR *primary_buffer = (UCHAR *)ddsd.lpSurface;       

// test if memory is linear

if (ddsd.lPitch == SCREEN_WIDTH)
   {
   // copy memory from double buffer to primary buffer

   memcpy((void *)primary_buffer, (void *)bitmap.buffer, SCREEN_WIDTH*SCREEN_HEIGHT);
   } // end if

else
   { // non-linear


   // make copy of source and destination addresses

   UCHAR *dest_ptr = primary_buffer;
   UCHAR *src_ptr  = bitmap.buffer;

   // memory is non-linear, copy line by line

   for (int y=0; y < SCREEN_HEIGHT; y++)
       {
       // copy line

       memcpy((void *)dest_ptr, (void *)src_ptr, SCREEN_WIDTH);

       // advance pointers to next line

       dest_ptr+=ddsd.lPitch;
       src_ptr +=SCREEN_WIDTH;
       } // end for


   } // end else


// now unlock the primary surface

if (FAILED(lpddsprimary->Unlock(NULL)))
   return(0);



// title

//Draw_Text_GDI("REVENGE OF PING", 275,30, 

  //            RGB(255,255,255), lpddsprimary);


// title

//Draw_Text_GDI("REVENGE OF PING", 275,30, 

//              RGB(255,255,255), lpddsprimary);


// flip the surfaces

DDraw_Flip();
  
// now unlock the primary surface

if (FAILED(lpddsprimary->Unlock(NULL)))
   return(0);

// sleep a bit

Sleep(30);

// return success or failure or your own return code here

return(1);

} // end Game_Main


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


int Game_Init(void *parms = NULL, int num_parms = 0)
{
// this is called once after the initial window is created and

// before the main event loop is entered, do all your initialization

// here


// create IDirectDraw interface 7.0 object and test for error

if (FAILED(DirectDrawCreateEx(NULL, (void **)&lpdd, IID_IDirectDraw7, NULL)))
   return(0);

// set cooperation to full screen

if (FAILED(lpdd->SetCooperativeLevel(main_window_handle, 
                                      DDSCL_FULLSCREEN | DDSCL_ALLOWMODEX | 
                                      DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT)))
   {
   // error

   return(0);
   } // end if


// set display mode to 640x480x8

if (FAILED(lpdd->SetDisplayMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,0,0)))
   {
   // error

   return(0);
   } // end if



// clear ddsd and set size

memset(&ddsd,0,sizeof(ddsd)); 
ddsd.dwSize = sizeof(ddsd);

// enable valid fields

ddsd.dwFlags = DDSD_CAPS;

// request primary surface

ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;

// create the primary surface

if (FAILED(lpdd->CreateSurface(&ddsd, &lpddsprimary, NULL)))
   {
   // error

   return(0);
   } // end if


// build up the palette data array

for (int color=1; color < 255; color++)
    {
    // fill with random RGB values

    palette[color].peRed   = rand()%256;
    palette[color].peGreen = rand()%256;
    palette[color].peBlue  = rand()%256;

    // set flags field to PC_NOCOLLAPSE

    palette[color].peFlags = PC_NOCOLLAPSE;
    } // end for color


// now fill in entry 0 and 255 with black and white

palette[0].peRed   = 0;
palette[0].peGreen = 0;
palette[0].peBlue  = 0;
palette[0].peFlags = PC_NOCOLLAPSE;

palette[255].peRed   = 255;
palette[255].peGreen = 255;
palette[255].peBlue  = 255;
palette[255].peFlags = PC_NOCOLLAPSE;

// create the palette object

if (FAILED(lpdd->CreatePalette(DDPCAPS_8BIT | DDPCAPS_ALLOW256 | 
                                DDPCAPS_INITIALIZE, 
                                palette,&lpddpal, NULL)))
{
// error

return(0);
} // end if



// load the background

Load_Bitmap_File(&bitmap8bit, "pongthing.bmp");

// set the palette to background image palette

Set_Palette(bitmap8bit.palette);

// create and load the reactor bitmap image

Create_Bitmap(&paddle1, 0,0, 50, 85);
Load_Image_Bitmap(&paddle1,&bitmap8bit,0,0,BITMAP_EXTRACT_MODE_ABS);
Unload_Bitmap_File(&bitmap8bit);


// finally attach the palette to the primary surface

if (FAILED(lpddsprimary->SetPalette(lpddpal)))
    {
    // error

    return(0);
    } // end if


// load the 8-bit image

if (!Load_Bitmap_File(&bitmap,"pongthing.bmp"))
   return(0);

// load it's palette into directdraw

if (FAILED(lpddpal->SetEntries(0,0,MAX_COLORS_PALETTE,bitmap.palette)))
   return(0);

// clean the surface

DDraw_Fill_Surface(lpddsprimary,0);

// return success or failure or your own return code here

return(1);

} // end Game_Init


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


int Game_Shutdown(void *parms = NULL, int num_parms = 0)
{
// this is called after the game is exited and the main event

// loop while is exited, do all you cleanup and shutdown here


// first the palette

if (lpddpal)
   {
   lpddpal->Release();
   lpddpal = NULL;
   } // end if


// now the primary surface

if (lpddsprimary)
   {
   lpddsprimary->Release();
   lpddsprimary = NULL;
   } // end if



// now blow away the IDirectDraw4 interface

if (lpdd)
   {
   lpdd->Release();
   lpdd = NULL;
   } // end if


// unload the bitmap file, we no longer need it

Unload_Bitmap_File(&bitmap);


// return success or failure or your own return code here

return(1);

} // end Game_Shutdown


// WINMAIN ////////////////////////////////////////////////


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

WNDCLASSEX winclass; // this will hold the class we create

HWND	   hwnd;	 // generic window handle

MSG		   msg;		 // generic message

HDC        hdc;      // graphics device context


// first fill in the window class stucture

winclass.cbSize         = sizeof(WNDCLASSEX);
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	= (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName	= NULL;
winclass.lpszClassName	= WINDOW_CLASS_NAME;
winclass.hIconSm        = LoadIcon(NULL, IDI_APPLICATION);

// save hinstance in global

hinstance_app = hinstance;

// register the window class

if (!RegisterClassEx(&winclass))
	return(0);

// create the window

if (!(hwnd = CreateWindowEx(NULL,                  // extended style

                            WINDOW_CLASS_NAME,     // class

						    "DirectDraw Full-Screen Demo", // title

						    WS_POPUP | WS_VISIBLE,
					 	    0,0,	  // initial x,y

						    SCREEN_WIDTH,SCREEN_HEIGHT,  // initial width, height

						    NULL,	  // handle to parent 

						    NULL,	  // handle to menu

						    hinstance,// instance of this application

						    NULL)))	// extra creation parms

return(0);

// save main window handle

main_window_handle = hwnd;

// initialize game here

Game_Init();

// enter main event loop

while(TRUE)
	{
    // test if there is a message in queue, if so get it

	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

    
       // main game processing goes here

       Game_Main();
       
	} // end while


// closedown game here

Game_Shutdown();

// return to Windows like this

return(msg.wParam);

} // end WinMain


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

   
[edited by - aggro5288 on March 6, 2003 9:21:20 PM]

Share this post


Link to post
Share on other sites
Hey I always have problems displaying bitmaps in ddraw, and my code seems quite the same as yours, Aggro5288.

But I can never find the problem, and it''s soo frustrating!
Please, anyone who can help us?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
take a look at the code in the dx7 framework file d3dtextr.cpp. it is ddraw applicable and it always works. TextureContainer::CopyBitmapToSurface would be one of the functions you''d be interested in.

Share this post


Link to post
Share on other sites
grr ... I can''t figure this out ... I updated the code a bit, as a last resort I might try that function Anonymous told me, but I wanna stay in ddraw for now ...

  // revenge.cpp

// Programmer: Jason Koebler

//// Source Code Pong in direct x (title in works)

// Will use t3dgame engines made by Andre'' Lamothe


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

// / // / / /

// / // / / /

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

// // / /

// // / /

// // / /

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



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

//////////////////////////////VERSION 1.0 ///////////////////////////////////////


// INCLUDES ///////////////////////////////////////////////


#define WIN32_LEAN_AND_MEAN

#define INITGUID

#include <windows.h> //windows
#include <windowsx.h>
#include <mmsystem.h>
#include <iostream.h> // C++
#include <conio.h>
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <math.h>
#include <io.h>
#include <fcntl.h>


#include <ddraw.h> // Directdraw
#include "t3dlib1.h"



// prototypes



// DEFINES ////////////////////////////////////////////////


// defines for windows

#define WINDOW_CLASS_NAME "WINCLASS1"

// default screen size

#define SCREEN_WIDTH 640 // size of screen
#define SCREEN_HEIGHT 480
#define SCREEN_BPP 8 // bits per pixel
#define MAX_COLORS 256 // maximum colors
#define BITMAP_ID 0x4D42 // universal id for a bitmap
#define MAX_COLORS_PALETTE 256




// TYPES //////////////////////////////////////////////////////


// basic unsigned types

typedef unsigned short USHORT;
typedef unsigned short WORD;
typedef unsigned char UCHAR;
typedef unsigned char BYTE;

// MACROS /////////////////////////////////////////////////


#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

// initializes a direct draw struct

#define DD_INIT_STRUCT(ddstruct) { memset(&ddstruct,0,sizeof(ddstruct)); ddstruct.dwSize=sizeof(ddstruct); }

// GLOBALS ////////////////////////////////////////////////

HWND main_window_handle = NULL; // globally track main window

HINSTANCE hinstance_app = NULL; // globally track hinstance

BITMAP_FILE bitmap; // holds the bitmap




BITMAP_IMAGE paddle1;

char buffer[80]; // general printing buffer


// 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

char buffer[80]; // used to print strings


// what is the message

switch(msg)
{
case WM_CREATE:
{
// do initialization stuff here

// return success

return(0);
} break;

case WM_PAINT:
{
// simply validate the window

hdc = BeginPaint(hwnd,&ps);

// end painting

EndPaint(hwnd,&ps);

// return success

return(0);
} break;

case WM_DESTROY:
{

// kill the application, this sends a WM_QUIT message

PostQuitMessage(0);

// return success

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





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


int Game_Main(void *parms = NULL, int num_parms = 0)
{
// this is the main loop of the game, do all your processing

// here


// for now test if user is hitting ESC and send WM_CLOSE

if (KEYDOWN(VK_ESCAPE))
SendMessage(main_window_handle,WM_CLOSE,0,0);




if (FAILED(lpddsprimary->Lock(NULL, &ddsd,
DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,
NULL)))
{
// error

return(0);
} // end if



// clear the drawing surface

DDraw_Fill_Surface(lpddsback, 0);

// lock the back buffer

DDraw_Lock_Back_Surface();

// draw the background reactor image

Draw_Bitmap(&paddle1, back_buffer, back_lpitch, 0);

// unlock the back buffer

DDraw_Unlock_Back_Surface();

// now ddsd.lPitch is valid and so is ddsd.lpSurface


// make a couple aliases to make code cleaner, so we don''t

// have to cast

int mempitch = (int)ddsd.lPitch;
UCHAR *video_buffer = (UCHAR *)ddsd.lpSurface;

// copy the bitmap image to the primary buffer line by line

// note this is a good candidate operation to make into a function - hint!


// lock the primary surface

lpddsprimary->Lock(NULL,&ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT,NULL);

// get video pointer to primary surfce

UCHAR *primary_buffer = (UCHAR *)ddsd.lpSurface;

// test if memory is linear

if (ddsd.lPitch == SCREEN_WIDTH)
{
// copy memory from double buffer to primary buffer

memcpy((void *)primary_buffer, (void *)bitmap.buffer, SCREEN_WIDTH*SCREEN_HEIGHT);
} // end if

else
{ // non-linear


// make copy of source and destination addresses

UCHAR *dest_ptr = primary_buffer;
UCHAR *src_ptr = bitmap.buffer;

// memory is non-linear, copy line by line

for (int y=0; y < SCREEN_HEIGHT; y++)
{
// copy line

memcpy((void *)dest_ptr, (void *)src_ptr, SCREEN_WIDTH);

// advance pointers to next line

dest_ptr+=ddsd.lPitch;
src_ptr +=SCREEN_WIDTH;
} // end for


} // end else


// now unlock the primary surface

if (FAILED(lpddsprimary->Unlock(NULL)))
return(0);



// title

Draw_Text_GDI("REVENGE OF PING", 275,30,
RGB(255,255,255), lpddsprimary);

// title

Draw_Text_GDI("REVENGE OF PING", 275,30,
RGB(255,255,255), lpddsprimary);

// flip the surfaces

DDraw_Flip();

// now unlock the primary surface

if (FAILED(lpddsprimary->Unlock(NULL)))
return(0);

// sleep a bit

Sleep(30);

// return success or failure or your own return code here

return(1);

} // end Game_Main


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


int Game_Init(void *parms = NULL, int num_parms = 0)
{
// this is called once after the initial window is created and

// before the main event loop is entered, do all your initialization

// here


int index; // looping var

char filename[80]; // used to build up files names


// initialize directdraw

DDraw_Init(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP);


// load in paddle1 image

Load_Bitmap_File(&bitmap8bit, "PONGTHING.BMP");



// set the palette to the palette of the generators

Set_Palette(bitmap8bit.palette);


// unload the map bitmap

Unload_Bitmap_File(&bitmap8bit);


// create and load the reactor bitmap image

//Create_Bitmap(&paddle1, 0,0, 50, 85);

//Load_Image_Bitmap(&paddle1,&bitmap8bit,0,0,BITMAP_EXTRACT_MODE_ABS);

//Unload_Bitmap_File(&bitmap8bit);



// finally attach the palette to the primary surface

if (FAILED(lpddsprimary->SetPalette(lpddpal)))
{
// error

return(0);
} // end if


// load the 8-bit image

if (!Load_Bitmap_File(&bitmap,"pongthing.bmp"))
return(0);

// load it''s palette into directdraw

if (FAILED(lpddpal->SetEntries(0,0,MAX_COLORS_PALETTE,bitmap.palette)))
return(0);

// clean the surface

DDraw_Fill_Surface(lpddsprimary,0);

// return success or failure or your own return code here

return(1);

} // end Game_Init


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


int Game_Shutdown(void *parms = NULL, int num_parms = 0)
{
// this is called after the game is exited and the main event

// loop while is exited, do all you cleanup and shutdown here


// first the palette

if (lpddpal)
{
lpddpal->Release();
lpddpal = NULL;
} // end if


// now the primary surface

if (lpddsprimary)
{
lpddsprimary->Release();
lpddsprimary = NULL;
} // end if



// now blow away the IDirectDraw4 interface

if (lpdd)
{
lpdd->Release();
lpdd = NULL;
} // end if




// unload the bitmap file, we no longer need it

Unload_Bitmap_File(&bitmap);


// return success or failure or your own return code here

return(1);

} // end Game_Shutdown


// WINMAIN ////////////////////////////////////////////////


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

WNDCLASSEX winclass; // this will hold the class we create

HWND hwnd; // generic window handle

MSG msg; // generic message

HDC hdc; // graphics device context


// first fill in the window class stucture

winclass.cbSize = sizeof(WNDCLASSEX);
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 = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

// save hinstance in global

hinstance_app = hinstance;

// register the window class

if (!RegisterClassEx(&winclass))
return(0);

// create the window

if (!(hwnd = CreateWindowEx(NULL, // extended style

WINDOW_CLASS_NAME, // class

"DirectDraw Full-Screen Demo", // title

WS_POPUP | WS_VISIBLE,
0,0, // initial x,y

SCREEN_WIDTH,SCREEN_HEIGHT, // initial width, height

NULL, // handle to parent

NULL, // handle to menu

hinstance,// instance of this application

NULL))) // extra creation parms

return(0);

// save main window handle

main_window_handle = hwnd;

// initialize game here

Game_Init();

// enter main event loop

while(TRUE)
{
// test if there is a message in queue, if so get it

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


// main game processing goes here

Game_Main();

} // end while


// closedown game here

Game_Shutdown();

// return to Windows like this

return(msg.wParam);

} // end WinMain


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

Share this post


Link to post
Share on other sites
Digging through my old DX apps, I found this function


  

LPDIRECTDRAWSURFACE7 SurfaceFromBitmap(LPCTSTR file_name)
{
HDC hdc;
HBITMAP bit;
LPDIRECTDRAWSURFACE7 surf;

// load the interface bitmap


bit=(HBITMAP) LoadImage(NULL,file_name,IMAGE_BITMAP,0,0,
LR_DEFAULTSIZE|LR_LOADFROMFILE);
if (!bit)

// failed to load, return failure to caller


return NULL;

// get bitmap dimensions


BITMAP bitmap;
GetObject( bit, sizeof(BITMAP), &bitmap );
int surf_width=bitmap.bmWidth;
int surf_height=bitmap.bmHeight;

// create surface


HRESULT ddrval;
DDSURFACEDESC2 ddsd;
ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize = sizeof(DDSURFACEDESC2);
ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT ;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
ddsd.dwWidth = surf_width;
ddsd.dwHeight = surf_height;

// attempt to create surface


ddrval=DD->CreateSurface(&ddsd,&surf,NULL);

// created ok?


if (ddrval!=DD_OK) {

// no, release the bitmap and return failure to caller


DeleteObject(bit);
return NULL;

} else {

// yes, get a DC for the surface


surf->GetDC(&hdc);

// generate a compatible DC


HDC bit_dc=CreateCompatibleDC(hdc);

// blit the interface to the surface


SelectObject(bit_dc,bit);
BitBlt(hdc,0,0,surf_width,surf_height,bit_dc,0,0,SRCCOPY);

// release the DCs


surf->ReleaseDC(hdc);
DeleteDC(bit_dc);

// save the dimensions if rectangle pointer provided



}

// clear bitmap


DeleteObject(bit);

// return pointer to caller


return surf;
}



Hope this helped!

Share this post


Link to post
Share on other sites