int Game_Init(void *parms = NULL, int num_parms = 0)
{
InitDirectDraw(800,600,16);
CreateSurfaceWithBitmap(character,"sprite3.bmp",40,40,DDSCAPS_VIDEOMEMORY);
RECT charrect;
charrect.left = 0;
charrect.top = 0;
charrect.right = 40;
charrect.bottom = 40;
primsurf->BltFast(0,0,character,&charrect,DDBLTFAST_WAIT);
return(1);
} // end Game_Init
////////////////////////////////////////////////////////
//ENGINE CPP FILE
////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
#define WIN32_LEAN_AND_MEAN
#include<ddraw.h>
#include"VinEngine.h"
#include<windows.h>
#include<winuser.h>
LPDIRECTDRAW4 ddraw4obj;
LPDIRECTDRAWSURFACE4 primsurf,
backsurf;
DDSURFACEDESC2 surfdesc;
DDCOLORKEY color_key;
DDBLTFX bltfxstruct;
HBITMAP bitmapheader;
int Screen_Width,
Screen_Height,
Screen_BPP;
extern HWND main_window_handle;
extern HINSTANCE hinstance_app;
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
int InitDirectDraw(int width, int height, int bpp)
// Creates DirectDraw and creates a double buffer
{
LPDIRECTDRAW ddraw1obj; //ddraw 1.0
DirectDrawCreate( NULL, &ddraw1obj, NULL); //create the object
ddraw1obj->QueryInterface(IID_IDirectDraw4,(LPVOID *)&ddraw4obj); //get the ddraw 4.0 with 1.0
ddraw1obj->Release(); //dump ddraw 1.0 obj, we using 4.0 now
ddraw1obj = NULL; //set it to null just for the hell of it
//from now on we can always use ddraw 4.0, which is really directx 6.0
ddraw4obj->SetCooperativeLevel( main_window_handle, //windows stuff
DDSCL_EXCLUSIVE| //don''t let anything interrupt ddraw
DDSCL_FULLSCREEN| //fullscreen is nice
DDSCL_ALLOWMODEX); //modex is some vga thing
ddraw4obj->SetDisplayMode(width, height, bpp, 0, 0); //simply set the display,bpp is bits per pixel
ShowCursor(FALSE); //don''t want to see windows cursor
DDSURFACEDESC2 surfdesc; //the surface description
memset(&surfdesc,0,sizeof(surfdesc)); //empty out the surface description
surfdesc.dwSize=sizeof(surfdesc); // set size
surfdesc.dwFlags= DDSD_CAPS | DDSD_BACKBUFFERCOUNT; //setting these enable dwCaps and dwBackBufferCount
surfdesc.dwBackBufferCount = 1; //set backbuffercount to 1, double buffer including primary surface
surfdesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP; //ddsCaps is just required to get to dwCaps
//the settings above are required if you want to make a double buffer
if(FAILED(ddraw4obj->CreateSurface(&surfdesc,&primsurf,NULL))) //create the primary surface from the ddraw4 object
return(0);
surfdesc.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER; // set the description to backbuffer
if(FAILED(primsurf->GetAttachedSurface(&surfdesc.ddsCaps,&backsurf))) //now using the description(set to back buffer) we attach the backsurf(declared at top) to primsurf
return(0);
//make the following global so we can use em in main program
width = Screen_Width;
height = Screen_Height;
bpp = Screen_BPP;
return(1);
}
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
LPDIRECTDRAWSURFACE4 CreateOffscreenSurface(LPDIRECTDRAWSURFACE4 offscreen,
int width,
int height,
int mem_flags)
//Creates an offscreen surface
{
memset(&surfdesc,0,sizeof(surfdesc));
surfdesc.dwSize=sizeof(surfdesc);
surfdesc.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
surfdesc.dwWidth = width;
surfdesc.dwHeight = height;
surfdesc.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | mem_flags;
if(FAILED(ddraw4obj->CreateSurface(&surfdesc, &offscreen, NULL)))
return(0);
color_key.dwColorSpaceLowValue = 0;
color_key.dwColorSpaceHighValue = 0;
offscreen->SetColorKey(DDCKEY_SRCBLT, &color_key);
return(offscreen);
}
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
int FlipSurfaces()
// flips primary and back surfaces
{
// flip surfaces
primsurf->Flip(NULL, NULL);
return(1);
}
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
int FillSurface(LPDIRECTDRAWSURFACE4 surface, int color)
//fills a surface with color
{
//clear out ddbltfx struct
memset(&bltfxstruct,0,sizeof(bltfxstruct));
bltfxstruct.dwSize=sizeof(bltfxstruct);
bltfxstruct.dwFillColor = color;
if(FAILED(surface->Blt(NULL,NULL,NULL,DDBLT_COLORFILL | DDBLT_WAIT, &bltfxstruct)))
return(0);
return(1);
}
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
int Draw_Text_GDI(char *text, int x, int y, COLORREF color, LPDIRECTDRAWSURFACE4 surface)
{
HDC devicecontext;
surface->GetDC(&devicecontext);
SetTextColor(devicecontext,color);
SetBkMode(devicecontext,TRANSPARENT);
TextOut(devicecontext,x,y,text,strlen(text));
surface->ReleaseDC(devicecontext);
return(1);
}
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
int LoadBitmapOnSurface(LPDIRECTDRAWSURFACE4 surface, HBITMAP hbitmap, int x, int y, int width
,int height)
{
HDC hdcImage;
HDC devicecontext;
BITMAP bitmap;
if(surface == NULL || hbitmap == NULL)
return(0);
surface->Restore();
hdcImage = CreateCompatibleDC(NULL);
SelectObject(hdcImage, hbitmap);
GetObject(hbitmap, sizeof(bitmap), &bitmap);
width = width == 0 ? bitmap.bmWidth : width;
height = height == 0 ? bitmap.bmHeight : height;
surface->GetDC(&devicecontext);
BitBlt(devicecontext,x,y,width,height,hdcImage,0,0,SRCCOPY);
surface->ReleaseDC(devicecontext);
DeleteDC(hdcImage);
return(1);
}
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
int CreateSurfaceWithBitmap(LPDIRECTDRAWSURFACE4 offscreen,char *filename, int width, int height
,int mem_flags)
{
CreateOffscreenSurface(offscreen,width,height,mem_flags);
bitmapheader = LoadImage(NULL,filename,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
LoadBitmapOnSurface(offscreen,bitmapheader,0,0,width,height);
DeleteObject(bitmapheader);
return(1);
}
//////////////////////////////////////////////////////////////////////////////////////////
bitmap loading simple error
I get the following error when trying to load a bitmap:--------------------Configuration: TestEngine - Win32 Debug--------------------
Compiling...
VinEngine.cpp
C:\MyProjects\TestEngine\VinEngine.cpp(191) : error C2440: ''='' : cannot convert from ''void *'' to ''struct HBITMAP__ *''
Conversion from ''void*'' to pointer to non-''void'' requires an explicit cast
Error executing cl.exe.
TestEngine.exe - 1 error(s), 0 warning(s)
Whew, a bit too much code perhaps
If I''m not mistaken the error occurrs on the line reading
(Again, if the problem occurred somewhere else, please mark the line or something, it''s not that easy to figure out which line 191 is.... Hope this helps though )
If I''m not mistaken the error occurrs on the line reading
bitmapheader = LoadImage (NULL,filename,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);[/source]In the CreateSurfaceWithBitmap function (am I correct?).The compiler complains about not being able to implicitly cast the pointer types, so all you need is an explicit cast, such as:[source]bitmapheader = (HBITMAP)LoadImage(NULL,filename,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
(Again, if the problem occurred somewhere else, please mark the line or something, it''s not that easy to figure out which line 191 is.... Hope this helps though )
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement