Archived

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

gameprogrammerwiz

errors with directdraw

Recommended Posts

Im getting some errors when i try to compile this. I dont know how to fix them, please help! here are the errors: ==================== main.cpp 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 ==================== #define WIN32_LEAN_AND_MEAN #include #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); 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
Hi, your problem is not a DX prob, what you did is you defined functions in other functions. Look at the line before your CreateSurface function:

void Draw_Image(void)
{
LPDIRECTDRAWSURFACE CreateSurface(DWORD width, DWORD height)
{

Notice the CreateSurface function is declared inside the Draw_Image function. You have to close the Draw_Image and all your errors will go away, except for the FILE_LEN error where it expects a constant that has not been declared.

I recommend you indent stuff for better reading, if you have then you should have had no problem finding the error.

void main( void )
{
do_stuff( paramater );
if( done )
exit(0);
// note how indentation makes it easier to to read the
// code
}

OneEyeLeftThanNone - bored out of my mind

oups forgot to use the code tags to get the spacing

Edited by - OneEyeLessThanNone on 4/5/00 9:44:23 AM

Share this post


Link to post
Share on other sites
Also here is a problem in your code.
if (lpdd->SetCooperativeLevel(hwnd, DDSCL_ALLOWREBOOT / DDSCL_EXCLUSIVE / DDSCL_FULLSCREEN / DDSCL_ALLOWMODEX) !=DD_OK)

You cant use a / in between the flags. You must use a / called a "pipe." Also i have seen this problem in other areas where flags are used. Now maybe you did have this it just didnt copy right i dont know. Also this could be a coping problem also but At the beginning you have a bunch of #include''s and nothing after them. So insead of #include you have #include

Share this post


Link to post
Share on other sites