Advertisement

directx blitting help

Started by September 05, 2000 08:12 PM
1 comment, last by OoMMMoO 24 years, 3 months ago
Can someone please help me? Im trying to make a program that sets the back buffer to a certain color then flips the surfaces but right when I run my program it says my program did an illegal operation here is the code
    
/////////////////////////////////////////////////

//game.cpp

//

//Displaying .bmp's

/////////////////////////////////////////////////

#define WIN32_LEAN_AND_MEAN
//INCLUDES

#include<windows.h>
#include<windowsx.h>
#include<mmsystem.h>
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<stdarg.h>
#include<memory.h>
#include<conio.h>
#include<string.h>
#include<io.h>
#include<fcntl.h>
#include<malloc.h>
#include<time.h>
#include<ddraw.h>

//DEFINES

#define WINDOW_CLASS_NAME		"WinX Game"
#define SCREEN_WIDTH			800
#define SCREEN_HEIGHT			600
#define BPP						16

//MACROS

#define KEY_DOWN(vk_code)	((GetAsyncKeyState(vk_code) & 0x8000) ? 1:0)
#define KEY_UP(vk_code)	 	((GetAsyncKeyState(vk_code) & 0x8000) ? 0:1)
#define _16RGB(r,g,b)		((b%32)+((g%64)<<6)+((r%32)<<11))

//TYPES

typedef unsigned char	UCHAR;
typedef unsigned short	USHORT;

//GLOBALS

HWND				main_window_handle	=NULL;
HINSTANCE			main_instance		=NULL;

LPDIRECTDRAW		lpdd				=NULL;
LPDIRECTDRAWSURFACE	lpddsprimary		=NULL;
LPDIRECTDRAWSURFACE	lpddsback			=NULL;
LPDIRECTDRAWPALETTE	lpddpal				=NULL;
DDSURFACEDESC		ddsd;
DDSCAPS				ddscaps;
HRESULT				ddrval;
USHORT				*back_buffer		=NULL;

//FUNCTION PROTOTYPES

int GameInit();
int GameMain();
int GameShutdown();

//FUNCTIONS

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

	switch(msg)
	{
	case WM_CREATE:
		{
			return(0);
		}break;
	case WM_PAINT:
		{
			hdc=BeginPaint(hwnd,&ps);
			EndPaint(hwnd,&ps);
			return(0);
		}break;
	case WM_SETCURSOR:
		{
			SetCursor(NULL);
			return(0);
		}break;
	case WM_DESTROY:
		{
			PostQuitMessage(0);
			return(0);
		}break;
	}
	return(DefWindowProc(hwnd,msg,wparam,lparam));
}

//WINMAIN

int WINAPI WinMain(HINSTANCE hinstance,HINSTANCE hprevinstance,LPSTR lpcmdline,int ncmdshow)
{
	WNDCLASS	winclass;
	HDC			hdc;
	MSG			msg;
	PAINTSTRUCT	ps;
	HWND		hwnd;

	winclass.style=			CS_DBLCLKS|CS_OWNDC|
							CS_HREDRAW|CS_VREDRAW;
	winclass.lpfnWndProc=	WindowProc;
	winclass.cbClsExtra=	0;
	winclass.cbWndExtra=	0;
	winclass.hInstance=		hinstance;
	winclass.hIcon=			LoadCursor(NULL,IDI_APPLICATION);
	winclass.hCursor=		LoadCursor(NULL,IDC_ARROW);
	winclass.hbrBackground=	(HBRUSH)GetStockObject(BLACK_BRUSH);
	winclass.lpszMenuName=	NULL;
	winclass.lpszClassName=	WINDOW_CLASS_NAME;

//register class

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

//create window

	if(!(hwnd=CreateWindow(WINDOW_CLASS_NAME,"WinX Game",WS_POPUP|WS_VISIBLE,
							0,0,SCREEN_WIDTH,SCREEN_HEIGHT,NULL,NULL,hinstance,NULL)))
		return(0);

//save the window handle to a global

	main_window_handle=	hwnd;
	main_instance=		hinstance;

//Initialize game

	GameInit();

//main event loop

	while(1)
	{
		if(PeekMessage(&msg,NULL,NULL,0,PM_REMOVE))
		{
			if(msg.message==WM_QUIT)
				break;
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
//main game loop

		GameMain();
	}
	GameShutdown();
	return(msg.wParam);
}
//WINX GAME CONSOLE FUNCTIONS

int GameInit()
{
	srand(unsigned(time(NULL)));
	if(DirectDrawCreate(NULL,&lpdd,NULL)!=DD_OK)
	{
		GameShutdown();
		return(0);
	}
	if(lpdd->SetCooperativeLevel(main_window_handle,DDSCL_ALLOWMODEX|DDSCL_ALLOWMODEX|
													DDSCL_EXCLUSIVE|DDSCL_ALLOWREBOOT)!=DD_OK)
	{
		GameShutdown();
		return(0);
	}
	if(lpdd->SetDisplayMode(SCREEN_WIDTH,SCREEN_HEIGHT,BPP)!=DD_OK)
	{
		GameShutdown();
		return(0);
	}
//setup the flags for a complex primary surface

	ddsd.dwFlags=DDSD_CAPS|DDSD_BACKBUFFERCOUNT;
	ddsd.dwBackBufferCount=1;
	ddsd.ddsCaps.dwCaps=DDSCAPS_PRIMARYSURFACE|DDSCAPS_COMPLEX|DDSCAPS_FLIP;
//create primary surface

	if(lpdd->CreateSurface(&ddsd,&lpddsprimary,NULL)!=DD_OK)
	{
		GameShutdown();
		return(0);
	}
//setup the flags for the backbuffer

	ddsd.ddsCaps.dwCaps=DDSCAPS_BACKBUFFER;
//create the backbuffer

	if(lpddsprimary->GetAttachedSurface(&ddsd.ddsCaps,&lpddsback)!=NULL)
	{
		GameShutdown();
		return(0);
	}
	return(1);
}
//GameShutdown function

int GameShutdown()
{
	if(lpddsback!=NULL)
		lpddsback->Release();
	if(lpddsprimary!=NULL)
		lpddsprimary->Release();
	if(lpdd!=NULL)
		lpdd->Release();
	return(1);
}
//GameMain function

int GameMain()
{
	DDBLTFX		blit;
	
	if(KEY_DOWN(VK_ESCAPE))
		PostMessage(main_window_handle,WM_CLOSE,0,0);

	blit.dwFillColor=_16RGB((rand()%255),(rand()%255),(rand()%255));
//do screen fill

	lpddsprimary->Blt(NULL,NULL,NULL,DDBLT_WAIT|DDBLT_COLORFILL,&blit);
	while(lpddsprimary->Flip(NULL,DDFLIP_WAIT));

	Sleep(500);
	return(1);
}
     
Edited by - OoMMMoO on 9/5/00 8:28:29 PM
If I''m not mistaken, you have to fill out the dwSize member of a DDBLTFX before it is used. Try that and see if it works. Let me know how it goes.


----------------------------------------

I'm fat, you're ugly. I can lose weight.

------------------------------------

"We are the music makers, and the dreamers of the dreams."
- Willy Wonka
Advertisement
yeah, i think you forgot to clear out the memory of your ddraw structs and set the dwSize member

This topic is closed to new replies.

Advertisement