What is wrong ?

Started by
1 comment, last by ChocoboX 19 years, 4 months ago
Heya.I'm trying to blit a back buffer on the primary surface.The code should fill the back buffer with pixels, then via the Blt function blit the back buffer on the primary surface.I get nothing than a black screen and after some time the program quits to windows.Wich happens because there is nothing to process I think.Can somebody please tell me what I am doing wrong ? Thank you in advance!

#define INIT_GUID
#define WIN32_LEAN_AND_MEAN // No MFC

#include <stdio.h>
#include <windows.h>
#include <ddraw.h>

#define SCREEN_WIDTH  1024
#define SCREEN_HEIGHT  768
#define BPP			    16

#define WND_CLASS_NAME "WINDCLASS"

//#define RGB16(r,g,b) ((b&31) + ((g & 31) << 5) + ((r & 31) << 10 ))
#define RGB16(r,g,b) ((b%32) + ((g%64) << 6) + ((r%32) << 11))
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)

LPDIRECTDRAW7			lpdd;
LPDIRECTDRAWSURFACE7    primary_surface;
LPDIRECTDRAWSURFACE7	back_buffer;
DDSURFACEDESC2			surf_desc;

RECT					dest_rect,src_rect;

HINSTANCE				app_hinstance;
HWND					glob_hwnd;

WORD					color;
WORD					*vid_surface;

LRESULT CALLBACK wndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
	PAINTSTRUCT ps;
	HDC			hdc;

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

	case WM_CLOSE:
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}

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

void gameInit(void) {
	//lpdd = primary_surface = back_buffer = NULL;
	
	DirectDrawCreateEx(NULL, (LPVOID*)&lpdd, IID_IDirectDraw7, NULL);
	lpdd->SetCooperativeLevel(glob_hwnd, DDSCL_EXCLUSIVE|DDSCL_ALLOWREBOOT|DDSCL_FULLSCREEN);
	lpdd->SetDisplayMode(SCREEN_WIDTH,SCREEN_HEIGHT, BPP,0,0);
	
	memset(&surf_desc, 0, sizeof(surf_desc));
	surf_desc.dwSize = sizeof(DDSURFACEDESC2);

	surf_desc.dwFlags = DDSD_CAPS|DDSD_BACKBUFFERCOUNT;
	surf_desc.dwBackBufferCount = 1;
	surf_desc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE|DDSCAPS_COMPLEX|DDSCAPS_FLIP;
	lpdd->CreateSurface(&surf_desc, &primary_surface, NULL);
	
	//surf_desc.dwFlags = DDSD_CAPS;
	memset(&surf_desc,0,sizeof(surf_desc));
	surf_desc.dwSize = sizeof(DDSURFACEDESC2);
	surf_desc.dwFlags = DDSD_CAPS;
	surf_desc.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER|DDSCAPS_COMPLEX|DDSCAPS_FLIP;
	primary_surface->GetAttachedSurface(&surf_desc.ddsCaps, &back_buffer);


	memset(&surf_desc,0,sizeof(surf_desc));
	surf_desc.dwSize = sizeof(DDSURFACEDESC2);
	back_buffer->Lock(NULL, &surf_desc, DDLOCK_SURFACEMEMORYPTR|DDLOCK_WAIT, NULL);
	vid_surface = (WORD*)surf_desc.lpSurface;
	for(int y = 0; y < SCREEN_HEIGHT; y++)
		for(int x = 0; x < SCREEN_WIDTH;x++) {
			color = RGB16(rand()%256, rand()%256, rand()%256);
			vid_surface[x + y * surf_desc.lPitch] = color;
		}
			
	back_buffer->Unlock(NULL);
}

int gameMain(void) {
	if(KEYDOWN(VK_ESCAPE) ) return 0;
	
	dest_rect.left = rand()%SCREEN_WIDTH;
	dest_rect.bottom = rand()%SCREEN_HEIGHT;
	dest_rect.top = rand()%SCREEN_WIDTH;
	dest_rect.right = rand()%SCREEN_HEIGHT;

	src_rect.left = rand()%SCREEN_WIDTH;
	src_rect.top = rand()%SCREEN_HEIGHT;
	src_rect.right = rand()%SCREEN_WIDTH;
	src_rect.bottom = rand()%SCREEN_HEIGHT;

	primary_surface->Flip(NULL,NULL);
	primary_surface->Blt(&dest_rect, back_buffer, &src_rect, DDBLT_WAIT, NULL);
	
	return 1;
}

void wndInit(WNDCLASSEX *wndclass, HWND *hwnd) {
	wndclass->cbSize		= sizeof(WNDCLASSEX);
	wndclass->style			= CS_VREDRAW|CS_HREDRAW;
	wndclass->lpfnWndProc	= wndProc;
	wndclass->cbClsExtra	= NULL;
	wndclass->cbWndExtra	= NULL;
	wndclass->hInstance		= app_hinstance;
	wndclass->hIcon			= LoadIcon(app_hinstance, IDI_APPLICATION);
	wndclass->hCursor		= LoadCursor(app_hinstance, IDC_ARROW);
	wndclass->hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wndclass->lpszClassName = WND_CLASS_NAME;
	wndclass->lpszMenuName  = NULL;
	wndclass->hIconSm       = LoadIcon(app_hinstance, IDI_APPLICATION);

	RegisterClassEx(wndclass);
	*hwnd = CreateWindowEx(NULL, WND_CLASS_NAME,
						  "DirectDraw surface blitting",
						  WS_POPUP|WS_VISIBLE,
						  0,0,
						  SCREEN_WIDTH,SCREEN_HEIGHT,
						  NULL,NULL,
						  app_hinstance,NULL);
}

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE previnstance, LPSTR lpcmdline, int ncmdshow) {
	
	int _L00P_ = true;
	WNDCLASSEX wndClass;
	HWND	   hwnd;
	MSG		   msg;

	wndInit(&wndClass,&hwnd);
	glob_hwnd = hwnd;
	app_hinstance = hinstance;

/*	ShowWindow(hwnd, ncmdshow);
	UpdateWindow(hwnd);
	ShowCursor(TRUE);*/

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

			TranslateMessage(&msg);
			DispatchMessage(&msg);

		}
		gameMain();
	}

	return msg.wParam;
}
	

Advertisement
primary_surface->GetAttachedSurface(&surf_desc.ddsCaps, &back_buffer);	memset(&surf_desc,0,sizeof(surf_desc));	surf_desc.dwSize = sizeof(DDSURFACEDESC2);	back_buffer->Lock(NULL, &surf_desc, DDLOCK_SURFACEMEMORYPTR|DDLOCK_WAIT, NULL);	vid_surface = (WORD*)surf_desc.lpSurface;	for(int y = 0; y < SCREEN_HEIGHT; y++)		for(int x = 0; x < SCREEN_WIDTH;x++) {			color = RGB16(rand()%256, rand()%256, rand()%256);			vid_surface[x + y * surf_desc.lPitch] = color;		}				back_buffer->Unlock(NULL);


Am I correctly plotting pixels ?Color is also a WORD.
The RGB16 macro function is defined as follows
#define RGB16(r,g,b) ((b%32) + ((g%64) << 6) + ((r%32) << 11))
Kinda fixed it.I've remplaced the manual pixel blitting routine with a call to Blt using a ddbltfx structure to randomly blit rectangles on the screen.Can someone tell me what I'm doing wrong in the manual routine ?

This topic is closed to new replies.

Advertisement