Skip to main content
GameDev.net gamedev.net
🔒 Locked

Plugging the right Data Type

Started by Alpha_ProgDes Jun 16, 2007 at 4:38 PM 1 replies 650+ views
Original Post
Alpha_ProgDes
Alpha_ProgDes
I'm using Win32 and a bastardized mix of C and C++ (wait! hold flame!). I'm using GetDeviceCaps to determined whether the screen is 16 or 32 bpp and then make my video buffer WORD* or DWORD*, respectively.

void Game_Main()
{
    
	
	DWORD *video_buffer = NULL;  // here
	long lPitch;
	int x, y;
	DDSURFACEDESC ddsd;

	memset(&ddsd, 0, sizeof(ddsd));
	ddsd.dwSize = sizeof(ddsd);
	
	G.lpDDSBack->Lock(NULL, &ddsd, DDLOCK_SURFACEMEMORYPTR|DDLOCK_WAIT, NULL);

		//DRAWING HERE
		video_buffer = (DWORD*)ddsd.lpSurface;  //here
		lPitch = ddsd.lPitch >> 2;

		
		for (y = 0; y < G.SCREEN_HEIGHT; ++y)    
		{        
			for (x = 0; x < G.SCREEN_WIDTH; ++x)        
			{            
				video_buffer[x + y*lPitch] = RGB32COLOR(15,63,15);        
			}    
		}
		

	G.lpDDSBack->Unlock(NULL);

	while(G.lpDDSPrimary->Flip(NULL, DDFLIP_WAIT) != DD_OK);
	
	Sleep(66);
Now I'm not sure how to go about this. I can't see how to use template but I have the feeling a switch statement and void* is going to show up somewhere in my solution. Is there a more "elegant" way to solve this? Am I clear as mud? [smile]
Beginner in Game Development?  Read here. And read here.  
Deyja
Deyja
int bpp = what_the_hell_is_the_bpp();void* video_buffer;if (bpp == 16) video_buffer = (void*) new WORD[size];else if (bpp == 32) video_buffer = (void*) new DWORD[size];etc.
ToohrVyk
ToohrVyk
I separate BPP-dependent code into a distinct template function, with the template argument being a "strategy" class describing the various traits of the BPP.

template <typename BPP_Strategy>void Draw(DDSURFACEDESC & desc){  typedef typename BPP_Strategy::pixel pixel;  pixel *video_buffer = reinterpret_cast<pixel*>(ddsd.lpSurface);  int lPitch = ddsd.lPitch / sizeof(pixel);		  for (int y = 0; y < G.SCREEN_HEIGHT; ++y)        for (int x = 0; x < G.SCREEN_WIDTH; ++x)              video_buffer[x + y*lPitch] = BPP_Strategy::RGB(15,63,15);        }		struct BPP32{  typedef DWORD pixel;  pixel RGB(int r, int g, int b) { return RGB32COLOR(r,g,b); }};struct BPP16{  typedef WORD pixel;  pixel RGB(int r, int g, int b) { return RGB16COLOR(r,g,b); }};void Game_Main(){	DDSURFACEDESC ddsd = { 0 };	ddsd.dwSize = sizeof(ddsd);		G.lpDDSBack->Lock(NULL, &ddsd, DDLOCK_SURFACEMEMORYPTR|DDLOCK_WAIT, NULL);        if (bpp == 32)          Draw<BPP32>(ddsd);        else if (bpp == 16)          Draw<BPP16>(ddsd);        else          assert(false);	G.lpDDSBack->Unlock(NULL);	while(G.lpDDSPrimary->Flip(NULL, DDFLIP_WAIT) != DD_OK);

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.