Archived

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

gameprogrammerwiz

-DICTIONAIRY- DirectX: To be a pain in the a**

Recommended Posts

(pulling out hair) nothing works! i cant get any directx to work on my own! i have 3 books and i''ve spent thousands of hours on the net for months trying to figure this out. how can you people understand this!? does anyone know of a tutorial that starts from the absolute BEGINNING of directx and works up SLOWLY so that my teeny-tiny brain can understand this? i''m working on a isometric tile-base RPG game. i got one little demo to work...without bitblt or scrolling. its been my dream for a couple of years now to write my own game. ive spent a lot of time on this and i dont want to give up. am i going to have to resort to useing CreateDIBSection? please help! -mike

Share this post


Link to post
Share on other sites
It''s not that hard. You just have to make sure you check all return codes and initialise everything properly, otherwise you will end up getting blank screens, or it bombs back out to Windows, etc etc. The Andre LaMothe ''...for Dummies'' book is good for the basics. It has many shortcomings but I don''t think any other book is quite as good at getting you started. Also see this this tutorial on this esteemed site. But if you want more help, you''ll have to be a little specific as to what you are having a problem with, as DirectX is fairly well documented and we would just end up quoting from the documentation. Check out the samples that come with the SDK. Try and work them out. If you don''t know what a certain function does, look it up. Try to follow the code.

Sory I can''t be of more help.

Share this post


Link to post
Share on other sites

I agree with Kylotan.
Compile and make sure one of the samples from the SDK work.
Then cut out all you don´t need and then you have a nice working skeleton.

I understand how you feel...It went very slowly for me too. I just didn´t get it...I felt like giving up but fortunately I did not !

-- There IS something rotten in the state of Denmark --

Share this post


Link to post
Share on other sites
I have found directX to be extremely easy. I went and got the book Sam''s Teach Yourself DirectX 7 in 24 Hours. Now the name may sound dum, bits its an excellent book. In 2 weeks, after opening the book, I was able to make a decent hex tile engine, and incorporate direct draw, direct sound, and direct input, and i not even half way through the book yet. I highly recommend you look at it, and just so you know, i am totally new to this stuff, i basically dont even know how to use classes yet! And i was still able to understand it, so get the book, and you will see the light.

Possibility

Share this post


Link to post
Share on other sites
heh

well im writing a couple of tut for beggining game development with DX

first article (is a little about design) is already on gamedev.net desks

50% finished on the 2nd

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hey, thanks Kylotan and everyone else! I got this to work, it doesnt do anything, but it inits directx and without bugs! (for once) I used the tutorial Kylotan pointed me to. And also Andre''s tutorial also. I remmed out the parts of the code that andre used to make the little dots on the screen. I tyed to use the code in the tutoial that Kylotan pointed me to to display a tile, but once again it didnt work. I may have put the chunk of code in the wrong place, or didnt get all of the code. im not sure...can someone help me now with displaying a tile? I wrote a sub (not included here) that would read a map file and determine what tile and where to put it...so i sort of jumped ahead of myself.
heres the code:
===============

#define WIN32_LEAN_AND_MEAN
#include
#include
#include
#include
#include
#include
#include
#include

#define WINDOW_CLASS_NAME "WINDOW_CLASS"
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define SCREEN_BPP 8
#define MAX_COLORS 256

typedef unsigned char UCHAR;

#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

int DD_Init(HWND hwnd);
int DD_Shutdown(void);
//int Set_Pal_Entry(int index, int red, int green, int blue);

void Game_Init(void);
void Game_Main(void);
void Game_Shutdown(void);

LPDIRECTDRAW lpdd = NULL;
LPDIRECTDRAWSURFACE lpddsprimary = NULL;
LPDIRECTDRAWPALETTE lpddpal = NULL;
//PALETTEENTRY color_palette[256];
DDSURFACEDESC ddsd;
DDSCAPS ddscaps;
HRESULT ddrval;
HWND main_window_handle = NULL;
UCHAR *video_buffer = NULL;

int DD_Init(HWND hwnd)
{
int index;

if (DirectDrawCreate(NULL,&lpdd,NULL)!=DD_OK)
{
DD_Shutdown();
return(0);
}

if (lpdd->SetCooperativeLevel(hwnd, DDSCL_ALLOWREBOOT / DDSCL_EXCLUSIVE /
DDSCL_FULLSCREEN / DDSCL_ALLOWMODEX)!=DD_OK)
{
DD_Shutdown();
return(0);
}

if (lpdd->SetDisplayMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP)!=DD_OK)
{
DD_Shutdown();
return(0);
}

ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;

if
(lpdd->CreateSurface(&ddsd,&lpddsprimary,NULL)!=DD_OK)
{
DD_Shutdown();
return(0);
}

/*memset(color_palette,0,256*sizeof(PALETTEENTRY));

for (index=0; index<256; index++)
{
if ((index / 64)==0)
{
color_palette[index].peRed = index*4;
color_palette[index].peGreen = index*4;
color_palette[index].peBlue = index*4;
}
else
if ((index / 64)==1)
color_palette[index].peRed = (index%64)*4;
else
if ((index / 64)==2)
color_palette[index].peGreen = (index%64)*4;
else
if ((index / 64)==3)
color_palette[index].peBlue = (index%64)*4;

color_palette[index].peFlags = PC_NOCOLLAPSE;

}

if (lpdd->CreatePalette((DDPCAPS_8BIT / DDPCAPS_INITIALIZE),color_palette,&lpddpal,NULL)!=DD_OK)
{
DD_Shutdown();
return(0);
}

lpddsprimary->SetPalette(lpddpal);*/

return(1);
}

int DD_Shutdown(void)
{
if (lpdd)
{
if(lpddsprimary)
{
lpddsprimary->Release();
lpddsprimary = NULL;
}

lpdd->Release();
lpdd = NULL;

return(1);

}
else
return(0);
}

/*int Set_Pal_Entry(int index, int red, int green, int blue)
{
PALETTEENTRY color;

color.peRed = (BYTE)red;
color.peGreen = (BYTE)green;
color.peBlue = (BYTE)blue;
color.peFlags = PC_NOCOLLAPSE;

lpddpal->SetEntries(0,index,1,&color);

memcpy(&color_palette[index],
&color,
sizeof(PALETTEENTRY));

return(1);

}*/

LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{

HDC hdc;
PAINTSTRUCT ps;

switch(msg)
{
case WM_CREATE:
{
return(0);
} break;

case WM_PAINT:
{
hdc = BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);

return(0);
} break;

case WM_DESTROY:
{
PostQuitMessage(0);
return(0);
} break;

default:break;

}
return (DefWindowProc(hwnd, msg, wparam, lparam));

}

int WINAPI WinMain( HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{
WNDCLASSEX winclass;
HWND hwnd;
MSG msg;

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.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = NULL; //GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;

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

if (!(hwnd = CreateWindowEx(WS_EX_TOPMOST,
WINDOW_CLASS_NAME,
"ddtest",
WS_VISIBLE / WS_POPUP,
0,0,
GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN),
NULL,
NULL,
hinstance,
NULL)))
return(0);

ShowCursor(0);

main_window_handle = hwnd;

if (!DD_Init(hwnd))
{
DestroyWindow(hwnd);
return(0);
}

Game_Init();

while(1)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;

TranslateMessage(&msg);

DispatchMessage(&msg);
}
else
{

memset(&ddsd,0,sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
lpddsprimary->Lock(NULL,&ddsd,DDLOCK_SURFACEMEMORYPTR,NULL);
video_buffer = (UCHAR *)ddsd.lpSurface;

Game_Main();

lpddsprimary->Unlock(ddsd.lpSurface);

}

}

DD_Shutdown();
Game_Shutdown();
return(msg.wParam);

}

void Game_Init(void)
{

}

void Game_Shutdown(void)
{

}

void Game_Main(void)
{
if (KEY_DOWN(VK_ESCAPE))
PostMessage(main_window_handle,WM_CLOSE,0,0);

/*if (KEY_DOWN(VK_RIGHT))
if (++px>SCREEN_WIDTH-8) px=8;

if (KEY_DOWN(VK_LEFT))
if (--px<8) px=SCREEN_WIDTH-8;

if (KEY_DOWN(VK_UP))
if (--py<8) py=SCREEN_HEIGHT-8;

if (KEY_DOWN(VK_DOWN))
if (++py>SCREEN_HEIGHT-8) py=8;

if (KEY_DOWN(''C''))
{
}*/
}

Share this post


Link to post
Share on other sites
to display a tile you wanna first load the bitmap onto a new ddraw surface, so first create the surface

LPDIRECTDRAWSURFACE7 CreateSurface(DWORD width, DWORD height)
{
DDSURFACEDESC2 ddsd; // working description
LPDIRECTDRAWSURFACE7 lpdds; // temporary surface

memset(&ddsd,0,sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS / DDSD_WIDTH / DDSD_HEIGHT;
ddsd.dwWidth = width;
ddsd.dwHeight = height;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN; //DDSCAPS_SYSTEMMEMORY if we want;

lpdd->CreateSurface(&ddsd,&lpdds,NULL);
return(lpdds);
}

then you want to load a bitmap from file onto it:
HRESULT CopyBMPToSurface( LPDIRECTDRAWSURFACE7 pddsSurface, DWORD width, DWORD height, char file[ FILE_LEN ]){
BITMAP bm;
HBITMAP hbm;
HDC hdcBitmap,
hdcTexture;
DDSURFACEDESC2 ddsdesc;

ddsdesc.dwSize = sizeof(ddsdesc);
ddsdesc.dwFlags = DDSD_HEIGHT / DDSD_WIDTH;
pddsSurface->GetSurfaceDesc(&ddsdesc);
// Create a bitmap and load the pddsSurface file into it. Check the
// executable's resource first.
hbm = (HBITMAP)LoadImage( GetModuleHandle(NULL), file,
IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION );
if( NULL == hbm ){
// If not in the resource, try to load the bitmap as a file. Real code
// would try to find the bitmap among many file paths.
hbm = (HBITMAP)LoadImage( NULL, file, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE/LR_CREATEDIBSECTION );
if( NULL == hbm ) return NULL;
}
// Now, copy the bitmap to the pddsSurface surface. To do this, we are creating
// a DC for the bitmap and a DC for the surface, so we can use the BitBlt()
// call to copy the actual bits.
hdcBitmap = CreateCompatibleDC( NULL );
if( NULL == hdcBitmap ){
pddsSurface->Release();
return NULL;
}
SelectObject( hdcBitmap, hbm );

// Get the bitmap structure (to extract width, height, and bpp)
GetObject( hbm, sizeof(BITMAP), &bm );

DWORD x, y;
x = (ddsdesc.dwWidth / 2) - (width / 2);
y = (ddsdesc.dwHeight / 2) - (height / 2);
// Get a DC for the surface
if( SUCCEEDED( pddsSurface->GetDC( &hdcTexture ) ) ){
// Copy the bitmap image to the surface.
StretchBlt( hdcTexture, 0, 0, width, height,
hdcBitmap, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY );
pddsSurface->ReleaseDC( hdcTexture );
}
DeleteDC( hdcBitmap );
DeleteObject(hbm);
return S_OK;
}

then you want to set the color key (the color that won't be bltted):
void SetColorKey(LPDIRECTDRAWSURFACE7 texture, BYTE r, BYTE g, BYTE b){
DDCOLORKEY ddck;

ddck.dwColorSpaceLowValue = DDColorMatch(texture, RGB(r, g, b));
ddck.dwColorSpaceHighValue = ddck.dwColorSpaceLowValue;
texture->SetColorKey(DDCKEY_SRCBLT, &ddck);
}

and then you want to bltfast(or blt) it to the back buffer:
void RenderTile(RECT *srect, RECT *drect, LPDIRECTDRAWSURFACE7 texture){
lpddsback->BltFast( drect->left, drect->top,
texture,
srect,
DDBLTFAST_WAIT / DDBLTFAST_SRCCOLORKEY);
}

oh yeah, above i used the function DDColorMatch.. here it is:
DWORD DDColorMatch(IDirectDrawSurface7 *pdds, COLORREF rgb)
{
COLORREF rgbT;
HDC hdc;
DWORD dw = CLR_INVALID;
DDSURFACEDESC2 ddsd;
HRESULT hres;

// use GDI SetPixel to color match for us
if (rgb != CLR_INVALID && pdds->GetDC(&hdc) == DD_OK)
{
rgbT = GetPixel(hdc, 0, 0); // save current pixel value
SetPixel(hdc, 0, 0, rgb); // set our value
pdds->ReleaseDC(hdc);
}

// now lock the surface so we can read back the converted color
ddsd.dwSize = sizeof(ddsd);
while ((hres = pdds->Lock(NULL, &ddsd, 0, NULL)) == DDERR_WASSTILLDRAWING)
;

if (hres == DD_OK)
{
dw = *(DWORD *)ddsd.lpSurface; // get DWORD
if(ddsd.ddpfPixelFormat.dwRGBBitCount < 32)
dw &= (1 << ddsd.ddpfPixelFormat.dwRGBBitCount)-1; // mask it to bpp
pdds->Unlock(NULL);
}

// now put the color that was there back.
if (rgb != CLR_INVALID && pdds->GetDC(&hdc) == DD_OK){
SetPixel(hdc, 0, 0, rgbT);
pdds->ReleaseDC(hdc);
}

return dw;
}

you can change all of the lpdirectdrawsurface7's to lpdirectdrawsurface and change the ddsurfacedesc2 to ddsurfacedesc. This should compile then.

reply if you have questions.

Edited by - iwasbiggs on 4/3/00 10:03:48 PM

Share this post


Link to post
Share on other sites
ok, when i originaly compiled it, it had 33 errors...i fixed all but 8 of them, now its giving me this in the output:
======================
C:\Windows\Desktop\ddtest\main.cpp(44) : warning C4101: ''index'' : unreferenced local variable
C:\Windows\Desktop\ddtest\main.cpp(302) : error C2601: ''CreateSurface'' : local function definitions are illegal
C:\Windows\Desktop\ddtest\main.cpp(317) : error C2065: ''FILE_LEN'' : undeclared identifier
C:\Windows\Desktop\ddtest\main.cpp(317) : error C2057: expected constant expression
C:\Windows\Desktop\ddtest\main.cpp(317) : error C2466: cannot allocate an array of constant size 0
C:\Windows\Desktop\ddtest\main.cpp(318) : error C2601: ''CopyBMPToSurface'' : local function definitions are illegal
C:\Windows\Desktop\ddtest\main.cpp(358) : error C2601: ''SetColorKey'' : local function definitions are illegal
C:\Windows\Desktop\ddtest\main.cpp(367) : error C2601: ''RenderTile'' : local function definitions are illegal
C:\Windows\Desktop\ddtest\main.cpp(372) : error C2601: ''DDColorMatch'' : local function definitions are illegal
======================

im really sorry that i keep asking more and more questions...i fell like you guys are programming my game for me.

#define WIN32_LEAN_AND_MEAN
#include windows.h
#include windowsx.h
#include mmsystem.h
#include ddraw.h
#include stdlib.h
#include stdio.h
#include math.h
#include float.h
#include windef.h

#define WINDOW_CLASS_NAME "WINDOW_CLASS"
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define SCREEN_BPP 8
#define MAX_COLORS 256

typedef unsigned char UCHAR;

#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)

int DD_Init(HWND hwnd);
int DD_Shutdown(void);
//int Set_Pal_Entry(int index, int red, int green, int blue);

void Game_Init(void);
void Game_Main(void);
void Game_Shutdown(void);
void Draw_Image(void);

LPDIRECTDRAW lpdd = NULL;
LPDIRECTDRAWSURFACE lpddsprimary = NULL;
LPDIRECTDRAWPALETTE lpddpal = NULL;
//PALETTEENTRY color_palette[256];
DDSURFACEDESC ddsd;
DDSCAPS ddscaps;
HRESULT ddrval;
HWND main_window_handle = NULL;
UCHAR *video_buffer = NULL;

int DD_Init(HWND hwnd)
{
int index;

if (DirectDrawCreate(NULL,&lpdd,NULL)!=DD_OK)
{
DD_Shutdown();
return(0);
}

if (lpdd->SetCooperativeLevel(hwnd, DDSCL_ALLOWREBOOT / DDSCL_EXCLUSIVE /
DDSCL_FULLSCREEN / DDSCL_ALLOWMODEX)!=DD_OK)
{
DD_Shutdown();
return(0);
}

if (lpdd->SetDisplayMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP)!=DD_OK)
{
DD_Shutdown();
return(0);
}

ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;

if
(lpdd->CreateSurface(&ddsd,&lpddsprimary,NULL)!=DD_OK)
{
DD_Shutdown();
return(0);
}

/*memset(color_palette,0,256*sizeof(PALETTEENTRY));

for (index=0; index<256; index++)
{
if ((index / 64)==0)
{
color_palette[index].peRed = index*4;
color_palette[index].peGreen = index*4;
color_palette[index].peBlue = index*4;
}
else
if ((index / 64)==1)
color_palette[index].peRed = (index%64)*4;
else
if ((index / 64)==2)
color_palette[index].peGreen = (index%64)*4;
else
if ((index / 64)==3)
color_palette[index].peBlue = (index%64)*4;

color_palette[index].peFlags = PC_NOCOLLAPSE;

}

if (lpdd->CreatePalette((DDPCAPS_8BIT / DDPCAPS_INITIALIZE),color_palette,&lpddpal,NULL)!=DD_OK)
{
DD_Shutdown();
return(0);
}

lpddsprimary->SetPalette(lpddpal);*/

return(1);
}

int DD_Shutdown(void)
{
if (lpdd)
{
if(lpddsprimary)
{
lpddsprimary->Release();
lpddsprimary = NULL;
}

lpdd->Release();
lpdd = NULL;

return(1);

}
else
return(0);
}

/*int Set_Pal_Entry(int index, int red, int green, int blue)
{
PALETTEENTRY color;

color.peRed = (BYTE)red;
color.peGreen = (BYTE)green;
color.peBlue = (BYTE)blue;
color.peFlags = PC_NOCOLLAPSE;

lpddpal->SetEntries(0,index,1,&color);

memcpy(&color_palette[index],
&color,
sizeof(PALETTEENTRY));

return(1);

}*/

LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{

HDC hdc;
PAINTSTRUCT ps;

switch(msg)
{
case WM_CREATE:
{
return(0);
} break;

case WM_PAINT:
{
hdc = BeginPaint(hwnd,&ps);
EndPaint(hwnd,&ps);

return(0);
} break;

case WM_DESTROY:
{
PostQuitMessage(0);
return(0);
} break;

default:break;

}
return (DefWindowProc(hwnd, msg, wparam, lparam));

}

int WINAPI WinMain( HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{
WNDCLASSEX winclass;
HWND hwnd;
MSG msg;

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.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = NULL; //GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;

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

if (!(hwnd = CreateWindowEx(WS_EX_TOPMOST,
WINDOW_CLASS_NAME,
"ddtest",
WS_VISIBLE / WS_POPUP,
0,0,
GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN),
NULL,
NULL,
hinstance,
NULL)))
return(0);

ShowCursor(0);

main_window_handle = hwnd;

if (!DD_Init(hwnd))
{
DestroyWindow(hwnd);
return(0);
}

Game_Init();

while(1)
{
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;

TranslateMessage(&msg);

DispatchMessage(&msg);
}
else
{

memset(&ddsd,0,sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
lpddsprimary->Lock(NULL,&ddsd,DDLOCK_SURFACEMEMORYPTR,NULL);
video_buffer = (UCHAR *)ddsd.lpSurface;

Game_Main();

lpddsprimary->Unlock(ddsd.lpSurface);

}

}

DD_Shutdown();
Game_Shutdown();
return(msg.wParam);

}

void Game_Init(void)
{

}

void Game_Shutdown(void)
{

}

void Game_Main(void)
{
if (KEY_DOWN(VK_ESCAPE))
PostMessage(main_window_handle,WM_CLOSE,0,0);

/*if (KEY_DOWN(VK_RIGHT))
if (++px>SCREEN_WIDTH-8) px=8;

if (KEY_DOWN(VK_LEFT))
if (--px<8) px=SCREEN_WIDTH-8;

if (KEY_DOWN(VK_UP))
if (--py<8) py=SCREEN_HEIGHT-8;

if (KEY_DOWN(VK_DOWN))
if (++py>SCREEN_HEIGHT-8) py=8;

if (KEY_DOWN(''C''))
{
}*/
}

void Draw_Image(void)
{
LPDIRECTDRAWSURFACE CreateSurface(DWORD width, DWORD height)
{
DDSURFACEDESC ddsd;
LPDIRECTDRAWSURFACE lpdds;

memset(&ddsd,0,sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
ddsd.dwFlags = DDSD_CAPS / DDSD_WIDTH / DDSD_HEIGHT;
ddsd.dwWidth = width;
ddsd.dwHeight = height;
ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;

lpdd->CreateSurface(&ddsd,&lpdds,NULL);
return(lpdds);
}

HRESULT CopyBMPToSurface(LPDIRECTDRAWSURFACE pddsSurface, DWORD width, DWORD height, char file[FILE_LEN])
{
BITMAP bm;
HBITMAP hbm;
HDC hdcBitmap,
hdcTexture;
DDSURFACEDESC ddsdesc;

ddsdesc.dwSize = sizeof(ddsdesc);
ddsdesc.dwFlags = DDSD_HEIGHT / DDSD_WIDTH;
pddsSurface->GetSurfaceDesc(&ddsdesc);
hbm = (HBITMAP)LoadImage( GetModuleHandle(NULL), "c:/windows/desktop/image.bmp", IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION );
if( NULL == hbm )
{
hbm = (HBITMAP)LoadImage( NULL, "c:/windows/desktop/image.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE/LR_CREATEDIBSECTION );
if( NULL == hbm ) return NULL;
}
hdcBitmap = CreateCompatibleDC( NULL );
if( NULL == hdcBitmap )
{
pddsSurface->Release();
return NULL;
}
SelectObject( hdcBitmap, hbm );
GetObject( hbm, sizeof(BITMAP), &bm );

DWORD x, y;
x = (ddsdesc.dwWidth / 2) - (width / 2);
y = (ddsdesc.dwHeight / 2) - (height / 2);
if( SUCCEEDED( pddsSurface->GetDC( &hdcTexture ) ) )
{
StretchBlt( hdcTexture, 0, 0, width, height,
hdcBitmap, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY );
pddsSurface->ReleaseDC( hdcTexture );
}
DeleteDC( hdcBitmap );
DeleteObject(hbm);
return S_OK;
}

void SetColorKey(LPDIRECTDRAWSURFACE texture, BYTE r, BYTE g, BYTE b)
{
DDCOLORKEY ddck;

ddck.dwColorSpaceLowValue = DDColorMatch(texture, RGB(r, g, b));
ddck.dwColorSpaceHighValue = ddck.dwColorSpaceLowValue;
texture->SetColorKey(DDCKEY_SRCBLT, &ddck);
}

void RenderTile(RECT *srect, RECT *drect, LPDIRECTDRAWSURFACE texture)
{
lpddsback->BltFast( drect->left, drect->top, texture, srect, DDBLTFAST_WAIT / DDBLTFAST_SRCCOLORKEY);
}

DWORD DDColorMatch(IDirectDrawSurface *pdds, COLORREF rgb)
{
COLORREF rgbT;
HDC hdc;
DWORD dw = CLR_INVALID;
DDSURFACEDESC ddsd;
HRESULT hres;

if (rgb != CLR_INVALID && pdds->GetDC(&hdc) == DD_OK)
{
rgbT = GetPixel(hdc, 0, 0);
SetPixel(hdc, 0, 0, rgb);
pdds->ReleaseDC(hdc);
}

ddsd.dwSize = sizeof(ddsd);
while ((hres = pdds->Lock(NULL, &ddsd, 0, NULL)) == DDERR_WASSTILLDRAWING);

if (hres == DD_OK)
{
dw = *(DWORD *)ddsd.lpSurface;
if(ddsd.ddpfPixelFormat.dwRGBBitCount < 32)
dw &= (1 << ddsd.ddpfPixelFormat.dwRGBBitCount)-1;
pdds->Unlock(NULL);
}

if (rgb != CLR_INVALID && pdds->GetDC(&hdc) == DD_OK)
{
SetPixel(hdc, 0, 0, rgbT);
pdds->ReleaseDC(hdc);
}

return dw;
}
}

Share this post


Link to post
Share on other sites