Jump to content
  • Advertisement
Sign in to follow this  
ThePointingMan

Multiple Windows with Direct3d

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

I've been trying to create a level creation program for a simple platforming game I am making in c++. I figured I may as well just create something like what is used in rpg maker where you pick a tile and draw it in your world on a grid, but before I can do that, I need to figure out how i can draw in both windows., my world window and my tile picker window I'll give some more of my source code if necessary, just ask, but what I am primarily wandering is do I need to create a separate d3d device for each window which I believe i have done, but possibly failed at? I've already made something where two windows pop up, and they are both supposed to have a little green box that dis-appears when I click, and re-appears when I release the mouse button, however only one gets a little green box and the other doesn't. The thing that changes which window gets the green box is which gets the function Loadgraphics first in the winmain.cpp file.

Here's some source code if you wanna have a look. These are all the files that I think could be related to the issue

//windowcreator.h
#include "Directx.h"
struct WindowCreator
{
LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;
D3DPRESENT_PARAMETERS d3dpp;
LPD3DXSPRITE d3dspt;
//DDevice d3dstuff;
HWND hWnd;
bool Windowed;
int xStore;
int yStore;
int Width;
int Height;
WindowCreator();
WindowCreator(HWND hwnd, DWORD styles, int x, int y, int w, int h, char const * caption, char const * className, HINSTANCE hInstance);
~WindowCreator();
void SetVisible(int cmdShow)
{ // Makes the window visible after it is created
::ShowWindow(hWnd, cmdShow);
::UpdateWindow(hWnd);
}

};


//winmain
#include "global.h"
bool big=false;
// Starting Point
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
OBJECT tiles;
DWORD styles=WS_SYSMENU;
Window MainWindow(WindowProc, TEXT("KillGore McStabGuts 3"), hInstance);
MainWindow.Register();
WindowCreator gw(NULL,styles,0,0,1024,1024,TEXT("KillGore McStabGuts 3"), TEXT("KillGore McStabGuts 3"), hInstance);
gw.SetVisible(nCmdShow);
if(gw.hWnd==NULL)
{
MessageBox(NULL, "Could not create main box.", "Error", MB_OK | MB_ICONERROR);
}
styles=WS_SYSMENU;
Window ObjectWindow(WindowProc, TEXT("KillGore McStabGuts"), hInstance);
ObjectWindow.Register();
WindowCreator ow(NULL,styles,1024,0,512,1024,TEXT("KillGore McStabGuts"), TEXT("KillGore McStabGuts"), hInstance);
ow.SetVisible(nCmdShow);
if(ow.hWnd==NULL)
{
MessageBox(NULL, "Could not create main box.", "Error", MB_OK | MB_ICONERROR);
}
InitDirect3D(&gw);
InitDirectInput(hInstance, &gw);
InitDirect3D(&ow);
InitDirectInput(hInstance, &ow);

LoadGraphics(&gw);
LoadGraphics(&ow);
MainLoop(&tiles,&gw,&ow);

CloseDirect3D(&gw);
CloseDirect3D(&ow);
CloseDirectInput();

return 0;
}
// Check for Messages and Handle
bool HandleMessages()
{
static MSG msg;

if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
return FALSE;

TranslateMessage(&msg);
DispatchMessage(&msg);
}

return TRUE;
}
//resize the window
void ClientResize(HWND hWnd, int nWidth, int nHeight)
{
RECT rcClient, rcWindow;
POINT ptDiff;
GetClientRect(hWnd, &rcClient);
GetWindowRect(hWnd, &rcWindow);
ptDiff.x = (rcWindow.right - rcWindow.left) - rcClient.right;
ptDiff.y = (rcWindow.bottom - rcWindow.top) - rcClient.bottom;
MoveWindow(hWnd,rcWindow.left, rcWindow.top, nWidth + ptDiff.x, nHeight + ptDiff.y, TRUE);
}
// Process the Messages
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_CREATE:

break;
case WM_KEYDOWN:
{
switch(wParam)
{
break;
case VK_ESCAPE:
PostQuitMessage(0);
return 0;
break;
}
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;
}

return DefWindowProc (hWnd, message, wParam, lParam);
}


//render.cpp
#include "global.h"

SPRITE green;

void LoadGraphics(WindowCreator *gw)
{
LoadSprite(&green, TEXT("green.bmp"), 32, 32, 1, 1, gw);
return;
}

void Render(OBJECT *tiles, WindowCreator* gw)
{
StartRender(gw);
if(tiles->viseable==true)
{
DrawSprite(&green,0,32,32,1,gw);
}
EndRender(gw);

return;
}


//direct3d.cpp
#include "global.h"

/*LPDIRECT3D9 d3d = NULL;
LPDIRECT3DDEVICE9 d3ddev = NULL;
D3DPRESENT_PARAMETERS d3dpp;
LPD3DXSPRITE d3dspt;
*/
//Create Direct3D and the Direct3D Device
void InitDirect3D(WindowCreator* gw)
{
gw->d3d = Direct3DCreate9(D3D_SDK_VERSION);

ZeroMemory(&gw->d3dpp, sizeof(gw->d3dpp));
gw->d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
gw->d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
gw->d3dpp.Windowed = gw->Windowed;
gw->d3dpp.BackBufferWidth = gw->Width;
gw->d3dpp.BackBufferHeight = gw->Height;

gw->d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
gw->hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&gw->d3dpp,
&gw->d3ddev);

D3DXCreateSprite(gw->d3ddev, &gw->d3dspt);

return;
}

// Close the Device and Direct3D
void CloseDirect3D(WindowCreator* gw)
{
gw->d3ddev->Release();
gw->d3d->Release();

return;
}

// Start rendering
void StartRender(WindowCreator* gw)
{
gw->d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
gw->d3ddev->BeginScene();
gw->d3dspt->Begin(D3DXSPRITE_ALPHABLEND);

return;
}

// Stop rendering
void EndRender(WindowCreator* gw)
{
gw->d3dspt->End();
gw->d3ddev->EndScene();
gw->d3ddev->Present(NULL, NULL, NULL, NULL);

return;
}

// Load a graphic into a SPRITE object
void LoadSprite(SPRITE* pSprite, LPCTSTR File, int width, int height, int cols, int rows, WindowCreator* gw)
{
D3DXCreateTextureFromFileEx(gw->d3ddev,
File,
D3DX_DEFAULT,
D3DX_DEFAULT,
D3DX_DEFAULT,
NULL,
D3DFMT_A8R8G8B8,
D3DPOOL_MANAGED,
D3DX_DEFAULT,
D3DX_DEFAULT,
D3DCOLOR_XRGB(255, 1, 252),
NULL,
NULL,
&pSprite->tex);

pSprite->width = width;
pSprite->height = height;
pSprite->cols = cols;
pSprite->rows = rows;

return;
}

// Draw a frame from a SPRITE object
void DrawSprite(SPRITE* pSprite, int Frame, float x, float y, float z, WindowCreator* gw)
{
RECT FrameBox;
FrameBox.left = (Frame % pSprite->cols) * pSprite->width;
FrameBox.top = (Frame / pSprite->cols) * pSprite->height;
FrameBox.right = FrameBox.left + pSprite->width;
FrameBox.bottom = FrameBox.top + pSprite->height;

D3DXVECTOR3 position(x, y, z);

gw->d3dspt->Draw(pSprite->tex, &FrameBox, NULL, &position, D3DCOLOR_XRGB(255, 255, 255));

return;
}
/*void DrawSprite(SPRITE* pSprite, float x, float y, float z,int locx, int locy, int width, int hieght, WindowCreator* gw)
{
RECT FrameBox;
FrameBox.left = locx;
FrameBox.top = locy;
FrameBox.right = locx+width;
FrameBox.bottom = locy+hieght;
D3DXVECTOR3 position(x, y, z);

gw->d3dspt->Draw(pSprite->tex, &FrameBox, NULL, &position, D3DCOLOR_XRGB(255, 255, 255));

return;
}
void DrawSprite(SPRITE* pSprite, int Frame, float x, float y, float z,int cols, int rows,int locx, int locy, int width, int hieght, WindowCreator* gw)
{
RECT FrameBox;
FrameBox.left = (Frame % cols) * width;
FrameBox.top = (Frame / cols) * hieght;
FrameBox.right = FrameBox.left + width;
FrameBox.bottom = FrameBox.top + hieght;
D3DXVECTOR3 position(x, y, z);

gw->d3dspt->Draw(pSprite->tex, &FrameBox, NULL, &position, D3DCOLOR_XRGB(255, 255, 255));

return;
}*/

Share this post


Link to post
Share on other sites
Advertisement

I've been trying to create a level creation program for a simple platforming game I am making in c++. I figured I may as well just create something like what is used in rpg maker where you pick a tile and draw it in your world on a grid, but before I can do that, I need to figure out how i can draw in both windows., my world window and my tile picker window I'll give some more of my source code if necessary, just ask, but what I am primarily wandering is do I need to create a separate d3d device for each window which I believe i have done, but possibly failed at? I've already made something where two windows pop up, and they are both supposed to have a little green box that dis-appears when I click, and re-appears when I release the mouse button, however only one gets a little green box and the other doesn't. The thing that changes which window gets the green box is which gets the function Loadgraphics first in the winmain.cpp file.

Here's some source code if you wanna have a look. These are all the files that I think could be related to the issue

//windowcreator.h
#include "Directx.h"
struct WindowCreator
{
LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;
D3DPRESENT_PARAMETERS d3dpp;
LPD3DXSPRITE d3dspt;
//DDevice d3dstuff;
HWND hWnd;
bool Windowed;
int xStore;
int yStore;
int Width;
int Height;
WindowCreator();
WindowCreator(HWND hwnd, DWORD styles, int x, int y, int w, int h, char const * caption, char const * className, HINSTANCE hInstance);
~WindowCreator();
void SetVisible(int cmdShow)
{ // Makes the window visible after it is created
::ShowWindow(hWnd, cmdShow);
::UpdateWindow(hWnd);
}

};


//winmain
#include "global.h"
bool big=false;
// Starting Point
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
OBJECT tiles;
DWORD styles=WS_SYSMENU;
Window MainWindow(WindowProc, TEXT("KillGore McStabGuts 3"), hInstance);
MainWindow.Register();
WindowCreator gw(NULL,styles,0,0,1024,1024,TEXT("KillGore McStabGuts 3"), TEXT("KillGore McStabGuts 3"), hInstance);
gw.SetVisible(nCmdShow);
if(gw.hWnd==NULL)
{
MessageBox(NULL, "Could not create main box.", "Error", MB_OK | MB_ICONERROR);
}
styles=WS_SYSMENU;
Window ObjectWindow(WindowProc, TEXT("KillGore McStabGuts"), hInstance);
ObjectWindow.Register();
WindowCreator ow(NULL,styles,1024,0,512,1024,TEXT("KillGore McStabGuts"), TEXT("KillGore McStabGuts"), hInstance);
ow.SetVisible(nCmdShow);
if(ow.hWnd==NULL)
{
MessageBox(NULL, "Could not create main box.", "Error", MB_OK | MB_ICONERROR);
}
InitDirect3D(&gw);
InitDirectInput(hInstance, &gw);
InitDirect3D(&ow);
InitDirectInput(hInstance, &ow);

LoadGraphics(&gw);
LoadGraphics(&ow);
MainLoop(&tiles,&gw,&ow);

CloseDirect3D(&gw);
CloseDirect3D(&ow);
CloseDirectInput();

return 0;
}
// Check for Messages and Handle
bool HandleMessages()
{
static MSG msg;

if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
return FALSE;

TranslateMessage(&msg);
DispatchMessage(&msg);
}

return TRUE;
}
//resize the window
void ClientResize(HWND hWnd, int nWidth, int nHeight)
{
RECT rcClient, rcWindow;
POINT ptDiff;
GetClientRect(hWnd, &rcClient);
GetWindowRect(hWnd, &rcWindow);
ptDiff.x = (rcWindow.right - rcWindow.left) - rcClient.right;
ptDiff.y = (rcWindow.bottom - rcWindow.top) - rcClient.bottom;
MoveWindow(hWnd,rcWindow.left, rcWindow.top, nWidth + ptDiff.x, nHeight + ptDiff.y, TRUE);
}
// Process the Messages
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_CREATE:

break;
case WM_KEYDOWN:
{
switch(wParam)
{
break;
case VK_ESCAPE:
PostQuitMessage(0);
return 0;
break;
}
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;
}

return DefWindowProc (hWnd, message, wParam, lParam);
}


//render.cpp
#include "global.h"

SPRITE green;

void LoadGraphics(WindowCreator *gw)
{
LoadSprite(&green, TEXT("green.bmp"), 32, 32, 1, 1, gw);
return;
}

void Render(OBJECT *tiles, WindowCreator* gw)
{
StartRender(gw);
if(tiles->viseable==true)
{
DrawSprite(&green,0,32,32,1,gw);
}
EndRender(gw);

return;
}


//direct3d.cpp
#include "global.h"

/*LPDIRECT3D9 d3d = NULL;
LPDIRECT3DDEVICE9 d3ddev = NULL;
D3DPRESENT_PARAMETERS d3dpp;
LPD3DXSPRITE d3dspt;
*/
//Create Direct3D and the Direct3D Device
void InitDirect3D(WindowCreator* gw)
{
gw->d3d = Direct3DCreate9(D3D_SDK_VERSION);

ZeroMemory(&gw->d3dpp, sizeof(gw->d3dpp));
gw->d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
gw->d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
gw->d3dpp.Windowed = gw->Windowed;
gw->d3dpp.BackBufferWidth = gw->Width;
gw->d3dpp.BackBufferHeight = gw->Height;

gw->d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
gw->hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&gw->d3dpp,
&gw->d3ddev);

D3DXCreateSprite(gw->d3ddev, &gw->d3dspt);

return;
}

// Close the Device and Direct3D
void CloseDirect3D(WindowCreator* gw)
{
gw->d3ddev->Release();
gw->d3d->Release();

return;
}

// Start rendering
void StartRender(WindowCreator* gw)
{
gw->d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
gw->d3ddev->BeginScene();
gw->d3dspt->Begin(D3DXSPRITE_ALPHABLEND);

return;
}

// Stop rendering
void EndRender(WindowCreator* gw)
{
gw->d3dspt->End();
gw->d3ddev->EndScene();
gw->d3ddev->Present(NULL, NULL, NULL, NULL);

return;
}

// Load a graphic into a SPRITE object
void LoadSprite(SPRITE* pSprite, LPCTSTR File, int width, int height, int cols, int rows, WindowCreator* gw)
{
D3DXCreateTextureFromFileEx(gw->d3ddev,
File,
D3DX_DEFAULT,
D3DX_DEFAULT,
D3DX_DEFAULT,
NULL,
D3DFMT_A8R8G8B8,
D3DPOOL_MANAGED,
D3DX_DEFAULT,
D3DX_DEFAULT,
D3DCOLOR_XRGB(255, 1, 252),
NULL,
NULL,
&pSprite->tex);

pSprite->width = width;
pSprite->height = height;
pSprite->cols = cols;
pSprite->rows = rows;

return;
}

// Draw a frame from a SPRITE object
void DrawSprite(SPRITE* pSprite, int Frame, float x, float y, float z, WindowCreator* gw)
{
RECT FrameBox;
FrameBox.left = (Frame % pSprite->cols) * pSprite->width;
FrameBox.top = (Frame / pSprite->cols) * pSprite->height;
FrameBox.right = FrameBox.left + pSprite->width;
FrameBox.bottom = FrameBox.top + pSprite->height;

D3DXVECTOR3 position(x, y, z);

gw->d3dspt->Draw(pSprite->tex, &FrameBox, NULL, &position, D3DCOLOR_XRGB(255, 255, 255));

return;
}
/*void DrawSprite(SPRITE* pSprite, float x, float y, float z,int locx, int locy, int width, int hieght, WindowCreator* gw)
{
RECT FrameBox;
FrameBox.left = locx;
FrameBox.top = locy;
FrameBox.right = locx+width;
FrameBox.bottom = locy+hieght;
D3DXVECTOR3 position(x, y, z);

gw->d3dspt->Draw(pSprite->tex, &FrameBox, NULL, &position, D3DCOLOR_XRGB(255, 255, 255));

return;
}
void DrawSprite(SPRITE* pSprite, int Frame, float x, float y, float z,int cols, int rows,int locx, int locy, int width, int hieght, WindowCreator* gw)
{
RECT FrameBox;
FrameBox.left = (Frame % cols) * width;
FrameBox.top = (Frame / cols) * hieght;
FrameBox.right = FrameBox.left + width;
FrameBox.bottom = FrameBox.top + hieght;
D3DXVECTOR3 position(x, y, z);

gw->d3dspt->Draw(pSprite->tex, &FrameBox, NULL, &position, D3DCOLOR_XRGB(255, 255, 255));

return;
}*/



I would try the old fashioned way of doing things and make multiple applications that would communicate with each other. Each one can specialize and there would be one controller application. That is just a suggestion because it is popular in unix.

Share this post


Link to post
Share on other sites
I believe you only need a single D3D device, you just need a separate swap chain for each window.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!