Archived

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

DirectDraw problem

This topic is 4949 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

Actually, the problem here is most likely bigger than that but I''ve been staring at it for 3 days, along with the book, and it just makes no sense why my program is loading up then immediately shutting down. I was trying to plot pixels, which didn''t work, so I''ve removed all of that to try to narrow it down. I''m STUCK! There are 3 files WinMain.cpp GameMain.cpp GameGlobals.h
/////////////////////WinMain.cpp///////////////////////
 
#include <windows.h>
#include <windowsx.h>
#include <winuser.h>
#include <cstdio>
 
const int GAME_MENU	=			101;
const int GAME_STARTING =		102;
const int GAME_RUN =			103;
const int GAME_RESTART =		104;
const int GAME_EXIT =			105;
 
 
// Functions used here. Must declare them..
int GameMain();
int GameInit(HWND hWnd);
int SetGameState(int state);
 
// Globals used throughout WinMain and MsgHandler.
int		xSize, xPos;
int		ySize, yPos;
bool	bFocus;
 
// Get rid of windows components we wont need for game programming.
#define WIN32_LEAN_AND_MEAN
 
// Declare our message handler.
LRESULT CALLBACK MsgHandler(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
 
 
// WinMain function. The entry point for the application.
int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
	MSG			msg;
	HWND		hWnd;
 
	WNDCLASSEX winclass;
 
	winclass.cbSize=		sizeof(WNDCLASSEX);
	winclass.style=			CS_HREDRAW | CS_VREDRAW;
	winclass.lpfnWndProc=	MsgHandler;
	winclass.cbClsExtra=	0;
	winclass.cbWndExtra=	0;
	winclass.hInstance=		hinstance;
	winclass.hIcon=			LoadIcon(NULL, IDI_WINLOGO);
	winclass.hCursor=		LoadCursor(NULL, IDC_ARROW);
	winclass.hbrBackground=	(HBRUSH)GetStockObject(BLACK_BRUSH);
	winclass.lpszMenuName=	NULL;
	winclass.lpszClassName=	"winclass";
	winclass.hIconSm=		LoadIcon(NULL, IDI_WINLOGO);
 
	RegisterClassEx(&winclass);
 
 
	// Create the Window
	if (!(hWnd = CreateWindowEx(NULL,
		"winclass",
		"Lorium Engine Test Window",
		WS_POPUP | WS_VISIBLE,
		0,0,1024,768,
		NULL,
		NULL,
		hinstance,
		NULL)))
	return(0);
 
	// Render the window.
	ShowWindow(hWnd,nCmdShow);
 
	// Initialize the game.
	GameInit(hWnd);
 
	// Enter the main event loop.
	while(true) {
		// Check to see if a message is present.
		if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
		{			
			// If it''s a quit message, quit.
			if(msg.message == WM_QUIT)
				SetGameState(GAME_EXIT);
				break;
 
			if(msg.message == WM_KEYDOWN && msg.wParam == VK_ESCAPE)
				SetGameState(GAME_EXIT);
 
			// Standard Windows stuff.
			TranslateMessage(&msg);
			DispatchMessage(&msg);
 
			GameMain();
		}
	}
 
	return(msg.wParam);
 
}
 
 
// Windows message handler.
LRESULT CALLBACK MsgHandler(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam){
	switch(msg)
	{
	case WM_DESTROY:
		{
			PostQuitMessage(0);
			return(0);
		}break;
	case WM_SIZE:
		{
			xSize = LOWORD(lparam);
			ySize = HIWORD(lparam);
			return(0);
		}break;
	case WM_MOVE:
		{
			xPos = LOWORD(lparam);
			yPos = HIWORD(lparam);
			return(0);
		}break;
	case WM_ACTIVATE:
		{
			if (LOWORD(wparam)==WA_INACTIVE)
				bFocus = false;
			else
				bFocus = true;
			return(0);
		}break;
	case WM_PAINT:
		{
			PAINTSTRUCT ps;
			HDC hdc;
			hdc = BeginPaint(hwnd, &ps);;
			EndPaint(hwnd, &ps);
			return(0);
		}break;
	default: break;
	}
 
	return(DefWindowProc(hwnd, msg, wparam, lparam));
}
 
 
/////////////////////GameMain.cpp///////////////////////
#include "GameGlobals.h"
 
int SetGameState(int state);
 
 
int GameMain() {
 
	switch(GameState)
	{
	case GAME_MENU:
		{
			// The game''s main menu.
			SetGameState(GAME_STARTING);
		}break;
	case GAME_STARTING:
		{
			//Set things up right before the game begins. Loading screen.
			SetGameState(GAME_RUN);
		}break;
	case GAME_RUN:
		{
					//clear display
					//get input
					//perform logic and ai
					//play sounds
					//render frame
					//synchronize display
 
		}break;
	case GAME_RESTART:
	{
		// Clean things up before restarting.
		SetGameState(GAME_MENU);
	}break;
	case GAME_EXIT:
		{
			if (lpddsPrimary) {
				lpddsPrimary->Release();
				lpddsPrimary = NULL;
			}
 
			if (lpddsBack) {
				lpddsBack->Release();
				lpddsBack = NULL;
			}
 
			if (lpddsOffScreen) {
				lpddsOffScreen->Release();
				lpddsOffScreen = NULL;
			}
 
			if (lpdd7) {
				lpdd7->Release();
				lpdd7 = NULL;
			}
		}break;
	default: break;
	}//End switch
 
 
	return 1;
}
 
 
int GameInit(HWND hWnd) {
 
	if(FAILED(DirectDrawCreateEx(NULL, (void**)&lpdd7, IID_IDirectDraw7, NULL)))
		MessageBox(hWnd, "Unable to create DirectDraw object!", "DirectDrawCreateEx", MB_OK);
 
	// Initialize the object
	lpdd7->Initialize(NULL);
 
	// Set the cooperation level with Windows.
	if(FAILED(lpdd7->SetCooperativeLevel(hWnd, DDSCL_ALLOWREBOOT | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN)))
		MessageBox(hWnd, "Unable to set cooperative level!", "SetCooperativeLevel", MB_OK);
 
	// Set the display mode.
	if(FAILED(lpdd7->SetDisplayMode(1024, 768, 32, 0, 0)))
		MessageBox(hWnd, "Unable to set display mode!", "SetDisplayMode", MB_OK);
 
	// Set up the primary surface.
	INIT_DXSTRUCT (ddsd);
	ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
	ddsd.dwBackBufferCount = 1;
	ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |
		DDSCAPS_COMPLEX |
		DDSCAPS_FLIP |
		DDSCAPS_VIDEOMEMORY;
 
	// Create the primary surface.
	if(FAILED(lpdd7->CreateSurface(&ddsd, &lpddsPrimary, NULL)))
		MessageBox(hWnd, "Unable to create the primary surface!", "CreateSurface", MB_OK);
 
	// Get the attached back buffer surface.
	ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
	if(FAILED(lpddsPrimary->GetAttachedSurface(&ddsd.ddsCaps, &lpddsBack)))
		MessageBox(hWnd, "Unable to get the back buffer!", "GetAttachedSurface", MB_OK);
 
	// Set up the offscreen buffer.
	INIT_DXSTRUCT (ddsd);
	ddsd.dwFlags = DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
	ddsd.dwWidth = 400;
	ddsd.dwHeight = 300;
	ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY;
 
	// Create the offscreen buffer.
	if(FAILED(lpdd7->CreateSurface(&ddsd, &lpddsOffScreen, NULL)))
		MessageBox(hWnd, "Unable to create the offscreen surface!", "CreateSurface", MB_OK);
 
 
	return (1);
}
 
int SetGameState(int state) {
	GameState = state;
 
	return (1);
 
}
 
/////////////////////GameGlobals.cpp///////////////////////
 
#include <windows.h>
#include <ddraw.h>
 
// GameStates to control flow through the main game loop.
const int GAME_MENU	=			101;
const int GAME_STARTING =		102;
const int GAME_RUN =			103;
const int GAME_RESTART =		104;
const int GAME_EXIT =			105;
int GameState = GAME_MENU;
 
// DirectDraw globals.
#define INIT_DXSTRUCT(dxs) { ZeroMemory(&dxs, sizeof(dxs)); dxs.dwSize = sizeof(dxs); } // Zero out structures.
 
LPDIRECTDRAW7			lpdd7;
DDSURFACEDESC2			ddsd; // DirectDraw surface description structure.
LPDIRECTDRAWSURFACE7	lpddsPrimary; // Primary surface.
LPDIRECTDRAWSURFACE7	lpddsBack; // Back buffer surface.
LPDIRECTDRAWSURFACE7	lpddsOffScreen; // Offscreen buffer surface
 
#define RGB_32BIT(r, g, b) { (r << 16) | (g << 8) | (b)}
#define RGB_16BIT565(r, g, b)  {(r << 11) | (g << 5) | (b)}
#define RGB_16BIT555(r, g, b)  {(r << 10) | (g << 5) | (b)}   

Share this post


Link to post
Share on other sites
quote:
Original post by DesCr
// Enter the main event loop.
while(true) {
// Check to see if a message is present.
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
// If it''s a quit message, quit.
if(msg.message == WM_QUIT)
SetGameState(GAME_EXIT);
break;

if(msg.message == WM_KEYDOWN && msg.wParam == VK_ESCAPE)
SetGameState(GAME_EXIT);

// Standard Windows stuff.
TranslateMessage(&msg);
DispatchMessage(&msg);

GameMain();
}
}


Put your GameMain() outside of if (PeekMessage()). And you need braces too for the msg.message == WM_QUIT. It should be like this:

// Enter the main event loop.
while(true) {
// Check to see if a message is present.
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
// If it''s a quit message, quit.
if(msg.message == WM_QUIT)
{
SetGameState(GAME_EXIT);
break;
}

if(msg.message == WM_KEYDOWN && msg.wParam == VK_ESCAPE)
SetGameState(GAME_EXIT);

// Standard Windows stuff.
TranslateMessage(&msg);
DispatchMessage(&msg);
}

GameMain();

}

Share this post


Link to post
Share on other sites
Thank you! An entire week of racking my brain and it was only program flow. "break;" was being called all the time.

[edited by - DesCr on May 28, 2004 1:01:39 AM]

Share this post


Link to post
Share on other sites