Jump to content
  • Advertisement

Archived

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

vbuser

Getting Error on bltfast

This topic is 5578 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

can anyone tell me where the problem is in this code? I''m sure there is problem with bltfast of directdraw in line#268 All have to do is link the ddraw.lib and dxguid.lib to the project.And the code is totally complies nicely.the program jump out to windows when doing bltfast.This is the major problblem since I''ve learnt DirectX...
// DD4.cpp : Defines the entry point for the application.

//


#include "stdafx.h"
#include "resource.h"
#include "DD4.h"
#include <ddraw.h>
#include <mmsystem.h>
#include <stdio.h>
#define MAX_LOADSTRING 100
#define INITGUID

// Global Variables:

HINSTANCE hInst;								// current instance

TCHAR szTitle[MAX_LOADSTRING];					// The title bar text

TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name



//Additional Variables

LPDIRECTDRAW7 DDraw=NULL;
LPDIRECTDRAWSURFACE7 DDSPrimary =NULL;
LPDIRECTDRAWSURFACE7 DDSBack = NULL;
LPDIRECTDRAWSURFACE7 DDSPic = NULL;
int DDError = 0;

// Forward declarations of functions included in this code module:

ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LPDIRECTDRAWSURFACE7 bitmap_surface(LPCTSTR file_name);
int					DDrawCreate(HWND hWnd);
int					DDCreateScreen();
int					DDFlip();
void				CleanUp();

LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

//CleanUp Functions

void CleanUp()
{
	DDraw->Release;
	DDraw = NULL;
	DDSPrimary->Release;
	DDSPrimary = NULL;
	DDSBack->Release;
	DDSBack = NULL;
	DDSPic->Release;
	DDSPic = NULL;
}

//Create Surface From File

LPDIRECTDRAWSURFACE7 bitmap_surface(LPCTSTR file_name)
{
	//========================

	HDC hdc;
	HBITMAP bit;
	LPDIRECTDRAWSURFACE7 surf;
	//========================


	bit=(HBITMAP)LoadImage(NULL,file_name,IMAGE_BITMAP,0,0,LR_DEFAULTSIZE|LR_LOADFROMFILE);

	if(!bit)
		//fail to load

		return NULL;

	//get bitmap dimensions

	BITMAP bitmap;
	GetObject(bit,sizeof(BITMAP),&bitmap);
	int surf_width = bitmap.bmWidth;
	int surf_height = bitmap.bmHeight;


	//create ddsurfacedesc

	HRESULT ddrval;
	DDSURFACEDESC2 ddsd;
	ZeroMemory (&ddsd,sizeof(ddsd));
	ddsd.dwSize = sizeof(DDSURFACEDESC2);
	ddsd.dwFlags = DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT;
	ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN|DDSCAPS_SYSTEMMEMORY;
	ddsd.dwWidth = surf_width;
	ddsd.dwHeight = surf_height;

	//create

	ddrval=DDraw->CreateSurface(&ddsd,&surf,NULL);

	if (ddrval!= DD_OK)
	{
		DeleteObject(bit);
		return NULL;
	}
	else
	{
		surf->GetDC(&hdc);
		HDC bit_dc=CreateCompatibleDC(hdc);

		SelectObject(bit_dc,bit);
		BitBlt(hdc,0,0,surf_width,surf_height,bit_dc,0,0,SRCCOPY);

		//release DC

		surf->ReleaseDC(hdc);
		DeleteDC(bit_dc);
	}
	//clear bitmap

	DeleteObject(bit);
	return surf;
}

//Create DirectDraw

int DDrawCreate(HWND hWnd)
{
	HRESULT hRet;
	//DirectDraw Create

	hRet = DirectDrawCreateEx(NULL,(LPVOID*) &DDraw,IID_IDirectDraw7,NULL);
	if (hRet != DD_OK)
	{
		return 1; //Fail Creating DirectDraw

	}

	//Set Cooperative

	hRet = DDraw->SetCooperativeLevel (hWnd,DDSCL_FULLSCREEN|DDSCL_EXCLUSIVE);
	if (hRet !=DD_OK)
	{
		return 2; //Fail Setting Full Screen

	}

	return 0;
}

//Create Primary and BackBuffer Surface

int DDCreateScreen()
{
	HRESULT hRet;
	DDSURFACEDESC2 ddsdPrimary;
	DDSCAPS2 ddscaps;
	ZeroMemory(&ddsdPrimary,sizeof(ddsdPrimary));
	ddsdPrimary.dwSize=sizeof(ddsdPrimary);
	ddsdPrimary.dwFlags=DDSD_CAPS|DDSD_BACKBUFFERCOUNT;
	ddsdPrimary.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE|DDSCAPS_FLIP|DDSCAPS_COMPLEX;
	ddsdPrimary.dwBackBufferCount = 1;
	hRet = DDraw->CreateSurface (&ddsdPrimary,&DDSPrimary,NULL);
	if (hRet != DD_OK)
	{
		return 3; //Error Creating Primary Surface

	}

	ddscaps.dwCaps=DDSCAPS_BACKBUFFER;
	hRet=DDSPrimary->GetAttachedSurface(&ddscaps,&DDSBack);
	if (hRet != DD_OK)
	{
		return 4; //Error Creating BackBufer

	}
	return 0;
}

//Flip with Primary Surface and Back Buffer

int DDFlip()
{
	DDSPrimary->Flip (0,DDFLIP_WAIT);
	return 0;
}

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
 	// TODO: Place code here.

	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings

	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_DD4, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:

	if (!InitInstance (hInstance, nCmdShow)) 
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_DD4);

	// Main message loop:

	while (GetMessage(&msg, NULL, 0, 0)) 
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) 
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}



//

//  FUNCTION: MyRegisterClass()

//

//  PURPOSE: Registers the window class.

//

//  COMMENTS:

//

//    This function and its usage are only necessary if you want this code

//    to be compatible with Win32 systems prior to the ''RegisterClassEx''

//    function that was added to Windows 95. It is important to call this function

//    so that the application will get ''well formed'' small icons associated

//    with it.

//

ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX); 

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, (LPCTSTR)IDI_DD4);
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= (LPCTSTR)IDC_DD4;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

	return RegisterClassEx(&wcex);
}

//

//   FUNCTION: InitInstance(HANDLE, int)

//

//   PURPOSE: Saves instance handle and creates main window

//

//   COMMENTS:

//

//        In this function, we save the instance handle in a global variable and

//        create and display the main program window.

//

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable


   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   LPRECT rect1;
   //SetRect(&rect1,0,0,640,480);

   DDrawCreate(hWnd);
   DDCreateScreen();
   DDSPic = bitmap_surface("005.bmp");
   if (!DDSPic)
	   return 5;//Error Creating Surface

   
   if (DDSBack->BltFast (0,0,DDSPic,NULL,DDBLTFAST_WAIT) != DD_OK)
	   return 6;//Error bltfast


   DDFlip();
   
   return TRUE;
}

//

//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)

//

//  PURPOSE:  Processes messages for the main window.

//

//  WM_COMMAND	- process the application menu

//  WM_PAINT	- Paint the main window

//  WM_DESTROY	- post a quit message and return

//

//

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message) 
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam); 
		wmEvent = HIWORD(wParam); 
		// Parse the menu selections:

		switch (wmId)
		{
		case IDM_ABOUT:
			DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: Add any drawing code here...

		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		CleanUp();
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

// Message handler for about box.

LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case WM_INITDIALOG:
		return TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) 
		{
			EndDialog(hDlg, LOWORD(wParam));
			return TRUE;
		}
		break;
	}
	return FALSE;
}

/[source]

Share this post


Link to post
Share on other sites
Advertisement
You''re not checking the return values from your DDrawCreate() and DDCreateScreen() functions. If one or both of them fails, you are going to get strange results when you try to access DDSBack (or any other DirectDraw object).

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!