Jump to content

  • Log In with Google      Sign In   
  • Create Account

TheSasquatch

Member Since 15 Aug 2011
Offline Last Active Jun 24 2014 05:57 PM

Topics I've Started

Game Engine Architecture, 2nd Edition

24 March 2014 - 08:02 PM

...is the Duke Nukem Forever of game programming books. I've been waiting for this thing for well over a year at this point, with the "release date" hovering several months in the future the whole time. I would have bought the first edition last March if there hadn't been a second edition coming out "in May 2013." Can't buy it now, either--the new edition's due out in July. I'm sure that'll happen. /frustrated rant


2D - Loading different tilesets by map

12 February 2013 - 10:40 PM

I'm writing a 2D room-based* platformer using XNA, and I'm trying to figure out the best way to load resources based on what the current map requires. I've yet to find a tutorial that goes this far--they usually load one small tileset and call it good enough--and a night of googling hasn't turned up anything particularly useful.

 

Basically, I need to (want to) dispose of the loaded tileset/textures and load just the ones the next map requests at every transition (ideally, I'll check if they're actually different first), without killing off the more universal textures (player sprites, etc.). I've seen a few forum posts saying the best way to do this is to create a second instance of ContentManager, but they never go into detail on how to do this correctly.

 

Currently, my XML map files each contain a string with the name of the tileset they're supposed to use, along with the names of all the background/parallax images for the map. I was planning on having my XmlReader method load the textures as their names were read, but then I realized I knew neither how to instantiate the second ContentManager, nor where to instantiate it--i.e., should it be in the MapFactory class that's loading the map, or in the static LevelManager that's calling the MapFactory (passing the new ContentManager as a parameter to the MapFactory, along with the name of the desired map)?

 

Any advice would be greatly appreciated, including "that's a terrible way to do that," if necessary. This is my first attempt at a game with more than a single map, after all.

 

*Ex. Castlevania, Metroid, but simpler (for now). I'd like to implement constant streaming eventually (chunks loaded as they are approached), but I want to get this part down first.


Winform w/ XNA viewport: disable input in viewport?

19 January 2013 - 08:52 PM

I'm writing a basic 2D tilemap editor for a hobby-level game, and while the game itself is going fairly well, I'm having some trouble with the Winform aspect (which is why I'm posting this in general programming rather than the game programming board).

 

The problem I'm having is that the picturebox which displays the XNA output (the map that's being edited) registers mouse clicks even when there's another window in front of it (for example, clicking OK on a save dialog box will edit whatever tile is behind it). I thought I'd fixed it with the marked addition below, but that just introduced a more confusing bug (it didn't seem like a particularly clean solution anyway, so I'm not surprised).

protected override void Update(GameTime gameTime)
    {

        Camera.Position = new Vector2(hscroll.Value, vscroll.Value);

        MouseState ms = Mouse.GetState();

        if ((ms.X > 0) && (ms.Y > 0) &&
            (ms.X < Camera.ViewPortWidth) &&
            (ms.Y < Camera.ViewPortHeight))
        {
            Vector2 mouseLoc = Camera.ScreenToWorld(
                new Vector2(ms.X, ms.Y));

            if (Camera.WorldRectangle.Contains(
                (int)mouseLoc.X, (int)mouseLoc.Y) **&& pictureBox.Capture**) //<--
            {
                if (ms.LeftButton == ButtonState.Pressed)
                  //....

Now it no longer edits the map through save/load dialogs, but instead refuses to acknowledge right clicks nine times out of ten--until the window is minimized and restored. After that it works exactly as expected. Anyone have any ideas? MSDN was less than helpful.


Tile engine design

16 January 2013 - 04:38 PM

I'm working on a 2D tile engine and looking for some general design and/or programming advice from more experienced programmers. What I want is an engine capable of handling both essentially limitless transition-free tilemaps (i.e., broken into chunks, loading the chunk the player is in and pre-loading adjacent chunks to make sure the player never sees an empty abyss at the edge of the screen) and Castlevania/Super Metroid style "rooms" of set size (for instance, an open overworld and segmented interior spaces). So, a few specific questions:

 

1. I've been using the Tile Engine and Level Editor from Kurt Jaegers' XNA 4.0 Game Development by Example as a starting point, but I'm finding they're fundamentally different from what I want to create (static tilemap class predefines a set const map size, tile size, number of layers, etc.)--rather than simply extending them as planned, I'm having to completely gut them to the point that I'm beginning to wonder if it would be more manageable to simply start fresh. I've managed to add support for maps with different properties (in such a way that the level editor still works, surprisingy), but it's messy. Thoughts?

 

2. Room-based tile engines are pretty easy to find info on, but I haven't been able to find any specific tutorials on how to code chunk-based tile maps. Anyone know of any? Specifically, I'm having trouble comprehending how to handle loading more than one tilemap, and how to get the camera to treat it as if it's all one giant map, only ceasing to scroll when it hits a place where there are no further chunks to load (the edge of the world). I assume I'll need to store the names (x,y coordinates) of all the chunks in an array to tell the engine which map files to load depending upon the coordinates of the chunk the player is in, but beyond that, I'm lost.


Learning DirectX, can't compile example code

18 August 2011 - 03:04 PM

I'm working through Beginning Game Programming (J. Harbour, 3rd Ed) and I've hit a roadblock. The code in the book won't compile, and after reading similar topics here (about the same sample program in the same book) none of the solutions that apparently worked for them are working for me. I tried Harbour's website, but it won't let me view any of the book's help boards without making an account and having it manually approved by an administrator (and three days later, no word from them).

Here's the full code, exactly as it appears in the book (and on the accompanying disc):

/*
	Beginning Game Programming, 3rd Ed.
	Chapter 3
	Direct3D_Windowed program
*/

#include <windows.h>
#include <d3d9.h>
#include <time.h>
#include <iostream>
using namespace std;

#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")

//program settings
const string APPTITLE = "Direct3D_Windowed";
const int SCREENW = 1024;
const int SCREENH = 768;

//Direct3D objects
LPDIRECT3D9 d3d = NULL; 
LPDIRECT3DDEVICE9 d3ddev = NULL; 

bool gameover = false;

//macro to detect key presses
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)


/**
 ** Game initialization function
 **/
bool Game_Init(HWND window)
{
	MessageBox(window, "Game_Init", "BREAKPOINT", 0);

	//initialize Direct3D
	d3d = Direct3DCreate9(D3D_SDK_VERSION);
	if (d3d == NULL)
	{
    	MessageBox(window, "Error initializing Direct3D", "Error", MB_OK);
    	return 0;
	}

	//set Direct3D presentation parameters
	D3DPRESENT_PARAMETERS d3dpp; 
	ZeroMemory(&d3dpp, sizeof(d3dpp));
	d3dpp.Windowed = TRUE;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
	d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
	d3dpp.BackBufferCount = 1;
	d3dpp.BackBufferWidth = SCREENW;
	d3dpp.BackBufferHeight = SCREENH;
	d3dpp.hDeviceWindow = window;

	//create Direct3D device
	d3d->CreateDevice(
    	D3DADAPTER_DEFAULT, 
    	D3DDEVTYPE_HAL, 
    	window,
    	D3DCREATE_SOFTWARE_VERTEXPROCESSING,
    	&d3dpp, 
    	&d3ddev);

	if (d3ddev == NULL)
	{
    	MessageBox(window, "Error creating Direct3D device", "Error", MB_OK);
    	return 0;
	}

	return true;
}

/**
 ** Game update function
 **/
void Game_Run(HWND hwnd)
{
	//make sure the Direct3D device is valid
	if (!d3ddev) return;

	//clear the backbuffer to bright green
	d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,255,0), 1.0f, 0);
	
	//start rendering
	if (d3ddev->BeginScene())
	{
    	//do something?
	
    	//stop rendering
    	d3ddev->EndScene();

    	//copy back buffer on the screen
    	d3ddev->Present(NULL, NULL, NULL, NULL);
	}

	//check for escape key (to exit program)
	if (KEY_DOWN(VK_ESCAPE))
    	PostMessage(hwnd, WM_DESTROY, 0, 0);
}

/**
 ** Game shutdown function
 **/
void Game_End(HWND hwnd)
{
	//display close message
	MessageBox(hwnd, "Program is about to end", "Game_End", MB_OK);

	//free memory
	if (d3ddev) d3ddev->Release();
	if (d3d) d3d->Release();
}


/**
 ** Windows event handling function
 **/
LRESULT WINAPI WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
	switch( msg )
	{
    	case WM_DESTROY:
        	gameover = true;
        	PostQuitMessage(0);
        	return 0;
	}
	return DefWindowProc( hWnd, msg, wParam, lParam );
}

/**
 ** Main Windows entry function
 **/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	//set the new window's properties
	//previously found in the MyRegisterClass function
	WNDCLASSEX wc;
	wc.cbSize = sizeof(WNDCLASSEX); 
	wc.style 		= CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc   = (WNDPROC)WinProc;
	wc.cbClsExtra 	= 0;
	wc.cbWndExtra 	= 0;
	wc.hInstance 	= hInstance;
	wc.hIcon 		= NULL;
	wc.hCursor   	= LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName  = NULL;
	wc.lpszClassName = APPTITLE.c_str();
	wc.hIconSm   	= NULL;
	RegisterClassEx(&wc);

	//create a new window
	//previously found in the InitInstance function
	HWND window = CreateWindow( APPTITLE.c_str(), APPTITLE.c_str(),
   	WS_OVERLAPPEDWINDOW,
   	CW_USEDEFAULT, CW_USEDEFAULT,
   	SCREENW, SCREENH,
   	NULL, NULL, hInstance, NULL);

	//was there an error creating the window?
	if (window == 0) return 0;

	//display the window
	ShowWindow(window, nCmdShow);
	UpdateWindow(window);
	
	//initialize the game
	if (!Game_Init(window)) return 0;


	// main message loop
	MSG message;
	while (!gameover)
	{
    	if (PeekMessage(&message, NULL, 0, 0, PM_REMOVE)) 
    	{
        	TranslateMessage(&message);
        	DispatchMessage(&message);
    	}

    	Game_Run(window);
	}

	Game_End(window);

	return message.wParam;

Other topics were resolved by linking the appropriate DirectX SDK libsand setting the Additional Dependencies, but doing so simply gives me a different (and longer) set of compiler errors, as follows:

1>------ Rebuild All started: Project: Direct3D_Windowed, Configuration: Debug Win32 ------
1>  winmain.cpp
1>winmain.obj : error LNK2019: unresolved external symbol __imp__MessageBoxA@16 referenced in function "bool __cdecl Game_Init(struct HWND__ *)" (?Game_Init@@YA_NPAUHWND__@@@Z)
1>winmain.obj : error LNK2019: unresolved external symbol __imp__PostMessageA@16 referenced in function "void __cdecl Game_Run(struct HWND__ *)" (?Game_Run@@YAXPAUHWND__@@@Z)
1>winmain.obj : error LNK2019: unresolved external symbol __imp__GetAsyncKeyState@4 referenced in function "void __cdecl Game_Run(struct HWND__ *)" (?Game_Run@@YAXPAUHWND__@@@Z)
1>winmain.obj : error LNK2019: unresolved external symbol __imp__DefWindowProcA@16 referenced in function "long __stdcall WinProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WinProc@@YGJPAUHWND__@@IIJ@Z)
1>winmain.obj : error LNK2019: unresolved external symbol __imp__PostQuitMessage@4 referenced in function "long __stdcall WinProc(struct HWND__ *,unsigned int,unsigned int,long)" (?WinProc@@YGJPAUHWND__@@IIJ@Z)
1>winmain.obj : error LNK2019: unresolved external symbol __imp__DispatchMessageA@4 referenced in function _WinMain@16
1>winmain.obj : error LNK2019: unresolved external symbol __imp__TranslateMessage@4 referenced in function _WinMain@16
1>winmain.obj : error LNK2019: unresolved external symbol __imp__PeekMessageA@20 referenced in function _WinMain@16
1>winmain.obj : error LNK2019: unresolved external symbol __imp__UpdateWindow@4 referenced in function _WinMain@16
1>winmain.obj : error LNK2019: unresolved external symbol __imp__ShowWindow@8 referenced in function _WinMain@16
1>winmain.obj : error LNK2019: unresolved external symbol __imp__CreateWindowExA@48 referenced in function _WinMain@16
1>winmain.obj : error LNK2019: unresolved external symbol __imp__RegisterClassExA@4 referenced in function _WinMain@16
1>winmain.obj : error LNK2019: unresolved external symbol __imp__GetStockObject@4 referenced in function _WinMain@16
1>winmain.obj : error LNK2019: unresolved external symbol __imp__LoadCursorA@8 referenced in function _WinMain@16
1>C:\Users\....\Beginning Game Programming\Direct3D_Windowed\Debug\Direct3D_Windowed.exe : fatal error LNK1120: 14 unresolved externals
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

The only thing I can think of would be that the software I'm using (VC++2010 Express, June 2010 DirectX SDK) and what the author was using (VC++2008, March 2009 DirectX SDK) are just incompatible. I can't really continue with the book without getting this to work. Any ideas?

PARTNERS