Public Group

Making a 2D menu in DirectX

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

Recommended Posts

I am working on a game in Direct3D and I realise that there is some 2D capability in Direct3D but I would like to know what is the best, or a good way of making a 2D menu for this game.

Would I be best trying to make this menu in Direct3D? Or using another part of DirectX such as DirectDraw? Or even just the WindowsAPI or something?

Thanks.

Share on other sites
I don't see why the windows API can't do. It has all the tools you need and it's not like a menu is going to be graphically intensive so you can separate some of that game code from the "frame" (as I call it. e.x menus, options, instructions, etc.) code. :P

Share on other sites
Thanks for the quick response.

I agree that it should be possible using just the WinAPI but I don't know how I would go about making a game menu with it. The most i've really done with the windows API is opening a window.

All I really need is just a background image and then I can do the rest programmatically. However, I am not sure of how to go about loading a background image into a window, and I have searched around and found nothing. A little guidance would be much appreciated if you have the time to help.

Thanks

Share on other sites
Every commercial game I have worked on has done the UI with Direct3D directly. You do have options, like using a 3rd party GUI library such as CEGUI, or rolling your own. I wouldn't say writing a GUI for just the basic stuff isn't really that hard, but it can be a bit time consuming.

Share on other sites
Quote:
 Original post by boogyman19946I don't see why the windows API can't do. It has all the tools you need and it's not like a menu is going to be graphically intensive so you can separate some of that game code from the "frame" (as I call it. e.x menus, options, instructions, etc.) code. :P

Why would you do that to yourself? Direct3D is perfectly capable of handling 2d graphics.

Basically you'll load a surface/texture from an image file that is the size of your screen. Then you can render that surface to the screen (I use StretchRect() ) and give it a 0 Z value, or you can use the D3DXSprite class to batch multiple textures to the screen

There is also Direct2D now but I'm not sure if that is appropriate for full-screen menus or just text/hud type stuff. I don't really know much about it.

I couldn't find any DX9 or later tutorials on 2d programming, but I did this for one of my simple, early games. Maybe the source can help you: http://www.adamsepanski.com/personal/gamedev/kidkombat/ . Just download the source and check out the dxgraphics.h/cpp and game.h/cpp files. Here is some relevant source from my project (note that this is DirectX 9.0c, not sure what version you are using and how it's changed since then). It uses StretchRect() and the spritebatch. I usually use surfaces for large memory blocks (like a background), and textures for sprites or smaller objects, but I'm not sure how important this is these days.

////////in d3d object init area////////LPD3DXSPRITE sprite_handler;//create sprite handlerresult=D3DXCreateSprite(d3ddev, &sprite_handler);if(result!=D3D_OK){     cerr<<"Error Creating sprite handler\n";     return 0;}///////////end d3d object init area////////returns a surface loaded with an image from the given filename//transcolor 0 for no transparency masking else use d3dcolor_xrgb() to render as transparent color//as blackLPDIRECT3DSURFACE9 LoadSurface(char *filename, D3DCOLOR transcolor){	//temporary surface to be returned	LPDIRECT3DSURFACE9 image= NULL;	//holds info about image	D3DXIMAGE_INFO info;	HRESULT result;	//get bitmap properties from file	result=D3DXGetImageInfoFromFile(filename,&info);	if(result !=D3D_OK)	{		cerr<<"D3DXGetImageInfoFromFile Failed\n";		return NULL;}	//create a new surface	result = d3ddev->CreateOffscreenPlainSurface(info.Width,info.Height,		D3DFMT_X8R8G8B8,D3DPOOL_DEFAULT,		&image,NULL);	if(result!=D3D_OK)	{		cerr<<"CreateOffscreenPlainSurface failed\n";		return NULL;}	//load surface from file into newly created surface	result= D3DXLoadSurfaceFromFile( image,NULL,NULL,filename,NULL,D3DX_DEFAULT,transcolor,NULL);	if(result!=D3D_OK)	{		cerr<<"D3DXLoadSurfaceFromFile failed\n";		return NULL;}	cerr<<"LoadSurface() successful\n";	return image;}LPDIRECT3DTEXTURE9 LoadTexture(char *filename, D3DCOLOR transcolor){	//texture	LPDIRECT3DTEXTURE9 texture=NULL;	//struct for image info	D3DXIMAGE_INFO info;	HRESULT result;	//get width and height from file	result=D3DXGetImageInfoFromFile(filename, &info);	if(result!=D3D_OK)	{		cerr<<"D3DXGetImageInfoFromFile failed\n";		return NULL;	}	//create new texture from file	D3DXCreateTextureFromFileEx(d3ddev,filename,info.Width,info.Height,1,D3DPOOL_DEFAULT,		D3DFMT_UNKNOWN,D3DPOOL_DEFAULT,D3DX_DEFAULT,D3DX_DEFAULT,transcolor,&info,NULL,		&texture);	if(result!=D3D_OK)	{		cerr<<"CreateTextureFromFileEx failed\n";		return NULL;	}		cerr<<"LoadTexture() Successful\n";	return texture;}/////////in game init area/////////LPDIRECT3DSURFACE9 mainmenu_surface;//main menu surfaceLPDIRECT3DTEXTURE9 KidRightTexture;mainmenu_surface=LoadSurface("ssfrontmenu.jpg",0);KidRightTexture=LoadTexture("KidRight.bmp",D3DCOLOR_XRGB(255,0,255));/////////end game init area////////////in main game loop drawing area///////begin rendering scene	if(d3ddev->BeginScene()==D3D_OK)	{		//clear back buffer		d3ddev->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,255),1.0f,0);		//draw background to backbuffer		d3ddev->StretchRect(mainmenu_surface,NULL,backbuffer,NULL,D3DTEXF_NONE);sprite_handler->Begin(D3DXSPRITE_ALPHABLEND);		//position is a vector that stores sprite position in 3d coordinates		//these if cases check to see if the sprite should be displayed, then draws it using		//the spriter handler, with transparency                position.x=0;                position.y=0;		//render		sprite_handler->Draw(KidRightTexture,NULL,NULL,&position,D3DCOLOR_XRGB(255,255,255));//end sprite drawing		sprite_handler->End();		d3ddev->EndScene();	}//stop rendering scene	//present backbuffer onto primary	d3ddev->Present(NULL,NULL,NULL,NULL);//////end main game loop area/////////

Share on other sites
Quote:
 Original post by boogyman19946It has all the tools you need...

No. Trust me, it does not! [wink]

Quote:
 Original post by boogyman19946...it's not like a menu is going to be graphically intensive so you can separate some of that game code from the "frame" (as I call it. e.x menus, options, instructions, etc.) code. :P

Chances are, you are correct. However, what makes you assume this? I personally need graphically intensive menus in my game and Windows does not provide a way. Microsoft makes the same assumption as you, and because of that, guess what I'm doing? Yep, I'm writing my own menu manager.

Share on other sites
I have newer seen games (besides windows games (minesweeper, solitaire etc)) with native windows GUI. It just doesn't fit visually. But if you make an editor, it's better to use the native GUI.

Share on other sites
Quote:
Original post by maspeir
Quote:
 Original post by boogyman19946...it's not like a menu is going to be graphically intensive so you can separate some of that game code from the "frame" (as I call it. e.x menus, options, instructions, etc.) code. :P

Chances are, you are correct. However, what makes you assume this? I personally need graphically intensive menus in my game and Windows does not provide a way. Microsoft makes the same assumption as you, and because of that, guess what I'm doing? Yep, I'm writing my own menu manager.

Well, if you're going to make a menu the quality of a blu-ray movie, then that would be quite intense graphically, but why make such a beastly menu? Shouldn't you be more focused on making the game playable?

Share on other sites
Quote:
 Well, if you're going to make a menu the quality of a blu-ray movie, then that would be quite intense graphically, but why make such a beastly menu? Shouldn't you be more focused on making the game playable?
Many games have menus whose graphical quality is similar to the actual in-game graphics, and use the same rendering techniques as the rest of the game.

As others have said, just about every game out there uses the same rendering system for its menus as it does for in-game graphics. (I've never seen a game that used the native Windows API for menus.)

Share on other sites
^ Well, they probably do, frankly, there really is no reason to include two API's if one can do the job fine.

• 9
• 9
• 13
• 41
• 15