Jump to content
  • Advertisement

Archived

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

nickwinters

Help using Mirus (Game Programming All in One by Bruno De Sousa)

This topic is 5517 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''m using Game Programming All in One by Bruno De Sousa to learn DirectX. I''m currently on Chapter 13 (developing Mirus, a graphics library that has drawLine, drawSquare, etc. so DirectX commands don''t have to be called every time). The book doesn''t make it clear how it should be implemented. I finally got the window to display, but it won''t clear the screen to a color, and it won''t draw a line on it. I''ve uploaded all the files to www.geocities.com/incorrigibleideal/DrawLineTest.zip Anyone see what''s wrong? One theory I have is that when the HWND is sent to direct X, it''s creating a copy of it, so the window being displayed is not the same as the one directX is trying to draw on. I tried passing them by reference, but then the pointers started creating compile errors all over mrScreen. Thanks. -Nick

Share this post


Link to post
Share on other sites
Advertisement
I just figured out how to post code in those text boxes, so here''s the code that''s in the .zip file above. All the weird data types are defined in mrDataTypes.cpp, as are the error names in mrError32.cpp. These are conventions in the book.

mrScreen.h

   /* ''mrScreen.h'' */

/* Mirus base types header */
#include "mrDatatypes.h"
/* Mirus error definitions header */
#include "mrTimer.h"
/* Mirus error definitions header */
#include "mrError.h"
/* Windows header file */
#include <windows.h>
/* DirectX Graphics header file */
#include <d3d8.h>
/* C++ math header file */
#include <math.h>
/* Assert header file */
#include <assert.h>
#include "mrError.h"

#include "CustomWindow.h"

/* Include this file only once */
#pragma once

/* Mirus custom vertex structure */
class mrVertex
{
public:
/* Transformed postion */
FLOAT m_fX, m_fY, m_fZ, m_fRHW;
/* Color */
DWORD m_iColor;
/* Texture coordinates */
FLOAT m_ftU, m_ftV;
};

/* Mirus custom vertex type */
#define D3DFVF_MIRUSVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1)

/* Mirus screen class */
class mrScreen
{
protected:
LPDIRECT3D8 m_pkD3D;
LPDIRECT3DDEVICE8 m_pkD3DDevice;

mrUInt32 m_iFPS;
HWND m_hWindow;

mrTimer m_kTimer;

mrUInt32 m_iFormat;

/* Singleton */
static mrScreen * m_pkSingleton;

public:
/* Constructors / Destructor */
mrScreen (void);
mrScreen (HWND hWindow);

~mrScreen (void);

/* Screen manipulation routines */
mrError32 Init (HWND hWindow);
mrError32 SetMode (mrUInt32 iFullscreen, mrUInt16 iWidth,
mrUInt16 iHeight, mrUInt16 iDepth,
bool bHardware);

/* Render routines */
mrError32 Clear (mrUInt8 iRed, mrUInt8 iGreen, mrUInt8 iBlue,
mrUInt8 iAlpha);
mrError32 StartFrame (void);
mrError32 EndFrame (void);

/* Draw routines */
mrError32 DrawLine (mrReal32 fX1, mrReal32 fY1,
mrReal32 fX2, mrReal32 fY2,
mrUInt8 iRed, mrUInt8 iGreen,
mrUInt8 iBlue, mrUInt8 iAlpha);
mrError32 DrawRectangle (mrReal32 fX1, mrReal32 fY1,
mrReal32 fX2, mrReal32 fY2,
mrUInt8 iRed, mrUInt8 iGreen,
mrUInt8 iBlue, mrUInt8 iAlpha);
mrError32 DrawCircle (mrReal32 fCenterX, mrReal32 fCenterY,
mrReal32 iRadius, mrUInt8 iRed,
mrUInt8 iGreen, mrUInt8 iBlue, mrUInt8 iAlpha,
mrUInt32 iVertices);

/* Miscellaneous routines */
mrBool32 IsModeSupported (mrUInt16 iWidth, mrUInt16 iHeight,
mrUInt16 iDepth);
void ShowCursor (mrUInt32 iShowCursor);

/* Maintaince methods */
LPDIRECT3DDEVICE8 GetDevice (void);
mrUInt32 GetFPS (void);
mrUInt32 GetFormat (void);

mrUInt32 GetBitdepth (void);

/* Singleton */

static mrScreen * GetSingleton (void);
};


mrScreen.cpp

   /* ''mrScreen.cpp'' */

/* Complement header file */
#include "mrScreen.h"

/* Singleton object */
mrScreen * mrScreen::m_pkSingleton = NULL;

/* Default constructor */
mrScreen::mrScreen (void)
{
m_pkD3D = NULL;
m_pkD3DDevice = NULL;
m_hWindow = NULL;
m_iFPS = 0;

assert (!m_pkSingleton);
m_pkSingleton = this;
}

/* Assigment constructor */
mrScreen::mrScreen (HWND hWindow)
{
m_pkD3D = NULL;
m_pkD3DDevice = NULL;
m_iFPS = 0;
m_hWindow = hWindow;

assert (!m_pkSingleton);
m_pkSingleton = this;

Init (hWindow);
}

/* Default destructor */
mrScreen::~mrScreen (void)
{
if (NULL != m_pkD3DDevice)
{
m_pkD3DDevice->Release ();
m_pkD3DDevice = NULL;
}
if (NULL != m_pkD3D)
{
m_pkD3D->Release ();
m_pkD3D = NULL;
}
m_iFPS = 0;

assert (m_pkSingleton);
m_pkSingleton = NULL;
}

/* Initialize Direct3D */
mrError32 mrScreen::Init (HWND hWindow)
{
/* Create Direct3D object */
m_pkD3D = Direct3DCreate8 (D3D_SDK_VERSION);
m_hWindow = hWindow;

if (NULL == m_pkD3D)
{
return mrErrorInitDirect3D;
}
return mrNoError;
}


/* Sets the display mode / create the Direct3D device */
mrError32 mrScreen::SetMode (mrUInt32 iFullscreen, mrUInt16 iWidth,
mrUInt16 iHeight, mrUInt16 iDepth,
bool bHardware)
{
D3DPRESENT_PARAMETERS kPresentParams;
mrUInt32 iDeviceType;

/* Set type of device to create (hardware or software */
if (!bHardware)
{
iDeviceType = D3DDEVTYPE_REF;
}
else
{
iDeviceType = D3DDEVTYPE_HAL;
}

/* Reset present parameters and set swap effect */
ZeroMemory (&kPresentParams, sizeof (D3DPRESENT_PARAMETERS) );
kPresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD;

/* If windowed mode */
if (!iFullscreen)
{
D3DDISPLAYMODE kCurrentMode;

/* Get current mode information */
if (FAILED (m_pkD3D->GetAdapterDisplayMode (D3DADAPTER_DEFAULT,
&kCurrentMode) ))
{
return mrErrorGetAdapterDisplayMode;
}

/* Set windowed mode and backbuffer format compatible with
current format */
kPresentParams.Windowed = true;
kPresentParams.BackBufferCount = 1;
kPresentParams.BackBufferFormat = kCurrentMode.Format;

/* Try to create device */
if (FAILED (m_pkD3D->CreateDevice (D3DADAPTER_DEFAULT,
(D3DDEVTYPE) iDeviceType,
m_hWindow,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&kPresentParams, &m_pkD3DDevice ) ))
{
return mrErrorCreateDevice;
}
}
else
{
/* Set full screen mode and full screen information */
kPresentParams.Windowed = false;
kPresentParams.BackBufferCount = 1;
kPresentParams.BackBufferWidth = iWidth;
kPresentParams.BackBufferHeight = iHeight;

kPresentParams.FullScreen_RefreshRateInHz =
D3DPRESENT_RATE_DEFAULT;
kPresentParams.FullScreen_PresentationInterval =
D3DPRESENT_INTERVAL_DEFAULT;

/* If 16 bit, try to create the device using different 16 bit pixel
color formats */
if (iDepth == 16)
{
kPresentParams.BackBufferFormat = D3DFMT_R5G6B5;
if (FAILED (m_pkD3D->CreateDevice (D3DADAPTER_DEFAULT,
(D3DDEVTYPE) iDeviceType,
m_hWindow,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&kPresentParams, &m_pkD3DDevice ) ))
{
kPresentParams.BackBufferFormat = D3DFMT_X1R5G5B5;
if (FAILED (m_pkD3D->CreateDevice (D3DADAPTER_DEFAULT,
(D3DDEVTYPE) iDeviceType,
m_hWindow,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&kPresentParams, &m_pkD3DDevice ) ))
{
kPresentParams.BackBufferFormat = D3DFMT_A1R5G5B5;
if (FAILED (m_pkD3D->CreateDevice (D3DADAPTER_DEFAULT,
(D3DDEVTYPE) iDeviceType,
m_hWindow,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&kPresentParams, &m_pkD3DDevice ) ))
{
return mrErrorCreateDevice;
}
}
}
}
/* If 32 bit, try to create the device using different pixel 32
color formats */
else
{
kPresentParams.BackBufferFormat = D3DFMT_A8R8G8B8;
if (FAILED (m_pkD3D->CreateDevice (D3DADAPTER_DEFAULT,
(D3DDEVTYPE) iDeviceType,
m_hWindow,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&kPresentParams, &m_pkD3DDevice ) ))
{
kPresentParams.BackBufferFormat = D3DFMT_X8R8G8B8;
if (FAILED (m_pkD3D->CreateDevice (D3DADAPTER_DEFAULT,
(D3DDEVTYPE) iDeviceType,
m_hWindow,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&kPresentParams, &m_pkD3DDevice ) ))
{
return mrErrorCreateDevice;
}
}
}

}
m_iFormat = kPresentParams.BackBufferFormat;

/* Set render states */
m_pkD3DDevice->SetRenderState (D3DRS_CULLMODE, D3DCULL_NONE);
m_pkD3DDevice->SetRenderState (D3DRS_LIGHTING, FALSE);
m_pkD3DDevice->SetRenderState (D3DRS_ALPHABLENDENABLE, TRUE);
m_pkD3DDevice->SetRenderState (D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
m_pkD3DDevice->SetRenderState (D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);

/* Set texture color states */
m_pkD3DDevice->SetTextureStageState (0, D3DTSS_COLOROP, D3DTOP_MODULATE);
m_pkD3DDevice->SetTextureStageState (0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);

return mrNoError;
}

/* Clear the window to color */
mrError32 mrScreen::Clear (mrUInt8 iRed, mrUInt8 iGreen, mrUInt8 iBlue,
mrUInt8 iAlpha)
{
mrUInt32 iColor;
iColor = D3DCOLOR_RGBA (iRed, iGreen, iBlue, iAlpha);

/* Clear the screen to certain color */
if (FAILED (m_pkD3DDevice->Clear (0, NULL, D3DCLEAR_TARGET, iColor,
0, 0) ))
{
return mrErrorClear;
}
return mrNoError;
}

/* Start rendering */
mrError32 mrScreen::StartFrame (void)
{
m_kTimer.Update ();

/* Start rendering */
if (FAILED (m_pkD3DDevice->BeginScene () ))
{
return mrErrorBeginScene;
}
return mrNoError;
}

/* End rendering */
mrError32 mrScreen::EndFrame (void)
{
/* End rendering */
if (FAILED (m_pkD3DDevice->EndScene () ))
{
return mrErrorEndScene;
}

/* Present data to the screen */
if (FAILED (m_pkD3DDevice->Present (NULL, NULL, NULL, NULL) ))
{
return mrErrorPresent;
}

/* Calculate framer per second */
m_kTimer.Update ();
m_iFPS = (mrUInt32) (1 / m_kTimer.GetDelta ());

return mrNoError;
}


mrError32 mrScreen::DrawLine (mrReal32 fX1, mrReal32 fY1,
mrReal32 fX2, mrReal32 fY2,
mrUInt8 iRed, mrUInt8 iGreen,
mrUInt8 iBlue, mrUInt8 iAlpha)
{
mrUInt32 iColor;
iColor = D3DCOLOR_RGBA (iRed, iGreen, iBlue, iAlpha);

/* Create rectangle vertices */
mrVertex kVertices [] =
{ /* x, y, z, w, color, texture coordinates (u,v) */
{fX1, fY1, 0, 1.0f, iColor, 0, 0},
{fX2, fY2, 0, 1.0f, iColor, 0, 0}
};

/* Draw the line */
mrScreen::GetSingleton ()->GetDevice ()->SetVertexShader (
D3DFVF_MIRUSVERTEX);
if (FAILED (mrScreen::GetSingleton ()->GetDevice ()->DrawPrimitiveUP (
D3DPT_LINELIST, 2, kVertices,
sizeof (mrVertex))) )
{
return mrErrorDrawPrimitive;
}

return mrNoError;
}

mrError32 mrScreen::DrawRectangle (mrReal32 fX1, mrReal32 fY1,
mrReal32 fX2, mrReal32 fY2,
mrUInt8 iRed, mrUInt8 iGreen,
mrUInt8 iBlue, mrUInt8 iAlpha)
{
mrUInt32 iColor;
iColor = D3DCOLOR_RGBA (iRed, iGreen, iBlue, iAlpha);

/* Create rectangle vertices */
mrVertex kVertices [] =
{ /* x, y, z, w, color, texture coordinates (u,v) */
{fX1, fY1, 0, 1.0f, iColor, 0, 0},
{fX2, fY1, 0, 1.0f, iColor, 0, 0},
{fX2, fY2, 0, 1.0f, iColor, 0, 0},
{fX1, fY2, 0, 1.0f, iColor, 0, 0},
{fX1, fY1, 0, 1.0f, iColor, 0, 0}
};

/* Draw the line */
mrScreen::GetSingleton ()->GetDevice ()->SetVertexShader (
D3DFVF_MIRUSVERTEX);
if (FAILED (mrScreen::GetSingleton ()->GetDevice ()->DrawPrimitiveUP (
D3DPT_LINESTRIP, 4, kVertices,
sizeof (mrVertex))) )
{
return mrErrorDrawPrimitive;
}

return mrNoError;
}

mrError32 mrScreen::DrawCircle (mrReal32 iCenterX, mrReal32 iCenterY,
mrReal32 iRadius, mrUInt8 iRed,
mrUInt8 iGreen, mrUInt8 iBlue,
mrUInt8 iAlpha, mrUInt32 iVertices)
{
mrUInt32 iColor;
iColor = D3DCOLOR_RGBA (iRed, iGreen, iBlue, iAlpha);

mrVertex * pkVertices;
/* Allocate needed vertices */
pkVertices = new mrVertex [iVertices + 1];

mrReal32 fAngle = 0;
mrReal32 fComplete;

mrUInt32 iVertex;

/* Calculate each vertice position */
for (iVertex = 0; iVertex < iVertices; iVertex ++)
{
/* Percentage of circle already drawn */
fComplete = (mrReal32)iVertex / (mrReal32)iVertices;
pkVertices [iVertex].m_fX = (mrReal32) ((mrReal32)iCenterX +
((mrReal32)iRadius * cos (6.2831f*fComplete)));
pkVertices [iVertex].m_fY = (mrReal32) ((mrReal32)iCenterY +
((mrReal32)iRadius * sin (6.2831f*fComplete)));

pkVertices [iVertex].m_fZ = 0;
pkVertices [iVertex].m_fRHW = 1.0f;
pkVertices [iVertex].m_iColor = iColor;
pkVertices [iVertex].m_ftU = 0;
pkVertices [iVertex].m_ftV = 0;
}

/* Close the circle */
pkVertices [iVertex].m_fX = pkVertices [0].m_fX;
pkVertices [iVertex].m_fY = pkVertices [0].m_fY;

pkVertices [iVertex].m_fZ = 0;
pkVertices [iVertex].m_fRHW = 1.0f;
pkVertices [iVertex].m_iColor = iColor;
pkVertices [iVertex].m_ftU = 0;
pkVertices [iVertex].m_ftV = 0;

/* Draw the circle */
mrScreen::GetSingleton ()->GetDevice ()->SetVertexShader (
D3DFVF_MIRUSVERTEX);
if (FAILED (mrScreen::GetSingleton ()->GetDevice ()->DrawPrimitiveUP (
D3DPT_LINESTRIP, iVertices,
pkVertices, sizeof (mrVertex))) )
{
return mrErrorDrawPrimitive;
}
delete [] pkVertices;
return mrNoError;
}

/* Check if mode is supported */
mrBool32 mrScreen::IsModeSupported (mrUInt16 iWidth, mrUInt16 iHeight,
mrUInt16 iDepth)
{
mrUInt32 iNumberOfModes;
mrUInt32 iMode;
D3DDISPLAYMODE kMode;

/* Get number of available modes */
iNumberOfModes = m_pkD3D->GetAdapterModeCount (D3DADAPTER_DEFAULT);

/* For each mode check if mode is equal */
for (iMode = 0; iMode < iNumberOfModes; iMode ++)
{
/* Get mode information */
m_pkD3D->EnumAdapterModes (D3DADAPTER_DEFAULT, iMode, &kMode);

/* Compare dimensions */
if ((iWidth == kMode.Width) && (iHeight == kMode.Height))
{
/* Compare bit depth */
if (iDepth == 16)
{
if ((kMode.Format == D3DFMT_R5G6B5) ||
(kMode.Format == D3DFMT_X1R5G5B5) ||
(kMode.Format == D3DFMT_A1R5G5B5) )
{
return mrTrue;
}
}
else
{
if ((kMode.Format == D3DFMT_A8R8G8B8) ||
(kMode.Format == D3DFMT_X8R8G8B8))
{
return mrTrue;
}
}
}
}

return mrFalse;
}

/* Shows or hides the cursor */
void mrScreen::ShowCursor (mrUInt32 iShowCursor)
{
m_pkD3DDevice->ShowCursor (iShowCursor);
}

/* Returns the Direct3D device */
LPDIRECT3DDEVICE8 mrScreen::GetDevice (void)
{
return m_pkD3DDevice;
}

/* Returns the frames per second */
mrUInt32 mrScreen::GetFPS (void)
{
return m_iFPS;
}

/* Returns the backbuffer format */
mrUInt32 mrScreen::GetFormat (void)
{
return m_iFormat;
}

/* Returns the backbuffer depth */
mrUInt32 mrScreen::GetBitdepth (void)
{
mrUInt32 iBitdepth;

switch (m_iFormat)
{
case D3DFMT_R5G6B5:
case D3DFMT_X1R5G5B5:
case D3DFMT_A1R5G5B5:
iBitdepth = 16;
break;
case D3DFMT_A8R8G8B8:
case D3DFMT_X8R8G8B8:
iBitdepth = 32;
break;
}

return iBitdepth;
}

/* Returns the mrScreen singleton */

mrScreen * mrScreen::GetSingleton (void)
{
assert (m_pkSingleton);
return m_pkSingleton;
}


mrWindow.h

   /* ''mrWindow.h'' */

/* Mirus base types header */
#include "mrDatatypes.h"
/* Mirus error definitions header */
#include "mrError.h"
/* Windows header file */
#include <windows.h>

/* Include this file only once */
#pragma once

/* Mirus window framework */
class mrWindow
{
protected:
WNDCLASS m_kWndClass;
HWND m_hWindow;
MSG m_kMessage;

public:
/* Constructor / Destructor */
mrWindow (void);
~mrWindow (void);

/* Window manipulation functions */
mrError32 Create (HINSTANCE hInstance, LPSTR szTitle,
mrInt iWidth = CW_USEDEFAULT,
mrInt iHeight = CW_USEDEFAULT,
mrUInt32 iStyle = WS_OVERLAPPEDWINDOW | WS_VISIBLE);
static LRESULT CALLBACK WndProc (HWND hWindow, UINT iMessage,
WPARAM wParam, LPARAM lParam);
void Run (void);

/* Custom functions */
virtual mrBool32 MessageHandler (UINT iMessage, WPARAM wParam,
LPARAM lParam);
virtual mrBool32 Frame (void) = 0;

/* More functions */

void SetPosition (mrInt iWidth, mrInt iHeight);
POINT GetPosition (void);
void SetSize (mrInt iWidth, mrInt iHeight);
POINT GetSize (void);
void Show (mrInt iShow);
HWND GetHandle (void);
};


mrWindow.cpp

   /* ''mrWindow.cpp'' */

/* Complement header file */
#include "mrWindow.h"

/* Default constructor */
mrWindow::mrWindow (void)
{
/* Do nothing */
}

/* Default destructor */
mrWindow::~mrWindow (void)
{
/* Do nothing */
}

/* Create the window */
mrError32 mrWindow::Create (HINSTANCE hInstance, LPSTR szTitle, mrInt iWidth,
mrInt iHeight, mrUInt32 iStyle)
{
/* ''Visual'' proprieties */
m_kWndClass.hCursor = LoadCursor (NULL, IDC_ARROW);
m_kWndClass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
m_kWndClass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);

/* System proprieties */
m_kWndClass.hInstance = hInstance;
m_kWndClass.lpfnWndProc = WndProc;
m_kWndClass.lpszClassName = "Mirus Window";

/* Extra proprieties */
m_kWndClass.lpszMenuName = NULL;

m_kWndClass.cbClsExtra = NULL;
m_kWndClass.cbWndExtra = NULL;
m_kWndClass.style = NULL;

/* Try to register class */
if (!RegisterClass (&m_kWndClass))
{
return mrErrorRegisterClass;
}

/* Create the window */
m_hWindow = CreateWindow ("Mirus Window", szTitle, iStyle, CW_USEDEFAULT,
CW_USEDEFAULT, iWidth, iHeight,
NULL, NULL, hInstance, (void *) this);
SetWindowText (m_hWindow, szTitle);

return mrNoError;
}

/* Normal message handler - direct messages to our own*/
LRESULT CALLBACK mrWindow::WndProc (HWND hWindow, UINT iMessage,
WPARAM wParam, LPARAM lParam)
{
mrWindow * pkWindow = NULL;
mrBool32 bProcessed = mrFalse;

switch (iMessage)
{
/* Window is creating - set custom information */
case WM_NCCREATE:
SetWindowLong (hWindow, GWL_USERDATA,
(long)((LPCREATESTRUCT(lParam))->lpCreateParams));
break;
/* Window message - Let our handler process it */
default:
pkWindow = (mrWindow *) GetWindowLong (hWindow, GWL_USERDATA);
if (NULL != pkWindow)
{
bProcessed = pkWindow->MessageHandler (iMessage, wParam, lParam);
}
break;
}
/* Message not processed - let windows handle it */
if (mrFalse == bProcessed)
{
return DefWindowProc (hWindow, iMessage, wParam, lParam);
}
return 0;
}

/* Real time message loop */
void mrWindow::Run (void)
{
while (1)
{
/* Query to see if there is any message in the queue */
if (PeekMessage (&m_kMessage, m_hWindow, 0, 0, PM_REMOVE))
{
/* If it is the WM_QUIT message, quit the loop */
if (WM_QUIT == m_kMessage.message)
{
break;
}
/* Process the message normally */
else
{
TranslateMessage (&m_kMessage);
DispatchMessage (&m_kMessage);
}
}
/* No message, do frame */
else
{
if (mrFalse == Frame ())
{
return;
}
}
}
}

/* Our message handler */
mrBool32 mrWindow::MessageHandler (UINT iMessage, WPARAM wParam,
LPARAM lParam)
{
switch (iMessage)
{
/* Close window */
case WM_CLOSE:
PostQuitMessage (0);
return mrTrue;
break;
/* Not handled - let Windows handle */
default:
return mrFalse;
break;
}
}

/* Set window position */
void mrWindow::SetPosition (mrInt iX, mrInt iY)
{
/* Set window position */
SetWindowPos(m_hWindow, HWND_TOP, iX, iY, 0, 0, SWP_NOSIZE);
}

/* Get window position */
POINT mrWindow::GetPosition (void)
{
RECT rcWindow;
POINT pPosition;
/* Get window position */
GetWindowRect (m_hWindow, &rcWindow);

pPosition.x = rcWindow.left;
pPosition.y = rcWindow.top;

return pPosition;
}

/* Set window size */
void mrWindow::SetSize (mrInt iWidth, mrInt iHeight)
{
/* Set window position */
SetWindowPos(m_hWindow, HWND_TOP, 0, 0, iWidth, iHeight, SWP_NOMOVE);
}

/* Gt window size */
POINT mrWindow::GetSize (void)
{
RECT rcWindow;
POINT pSize;
/* Get window position */
GetWindowRect (m_hWindow, &rcWindow);

pSize.x = rcWindow.right - rcWindow.left;
pSize.y = rcWindow.bottom - rcWindow.top;

return pSize;
}

/* Modify window state */
void mrWindow::Show (mrInt iShow)
{
/* Change window visibility */
ShowWindow (m_hWindow, iShow);
}

/* Return window handle */

HWND mrWindow::GetHandle (void)
{
return m_hWindow;
}


CustomWindow.h

  #include "mrWindow.h"
#pragma once
class CustomWindow:public mrWindow
{
public:
CustomWindow() { };
~CustomWindow() { };
mrBool32 Frame() { return mrTrue; };
};


mrTimer.h

   /* ''mrDatatypes.h'' */

/* Mirus base types header */
#include "mrDatatypes.h"
/* Windows header file */
#include <windows.h>
/* Time header file */
#include <time.h>

/* Include this file only once */
#pragma once

/* Mirus timer class */
class mrTimer
{
protected:
/* Hardware timer variables */
LARGE_INTEGER m_iFrequency;
LARGE_INTEGER m_iLastQuery;
LARGE_INTEGER m_iDelta;

/* Time and date variables */
tm * m_pkTime;

public:
/* Constructor / Destructor */
mrTimer (void);
~mrTimer (void);

/* Update the time variables */
void Update (void);

/* Return the timer information */

mrReal32 GetDelta (void);
mrUInt32 GetSeconds (void);
mrUInt32 GetMinutes (void);
mrUInt32 GetHours (void);
mrUInt32 GetDay (void);
mrUInt32 GetMonth (void);
mrUInt32 GetYear (void);
};


mrTimer.cpp

   /* ''mrWindow.cpp'' */

/* Complement header file */
#include "mrTimer.h"

/* Default constructor */
mrTimer::mrTimer (void)
{
/* Get the hardware clock frequency and current count */
QueryPerformanceFrequency (&m_iFrequency);
QueryPerformanceCounter (&m_iLastQuery);
}

/* Default destructor */
mrTimer::~mrTimer (void)
{
m_iFrequency.QuadPart = 0;
m_iLastQuery.QuadPart = 0;
}

/* Update timer */
void mrTimer::Update (void)
{
LARGE_INTEGER kTempTimer;
time_t iTempTimeDate;

/* Get current timer information and calculate difference */
QueryPerformanceCounter (&kTempTimer);
m_iDelta.QuadPart = kTempTimer.QuadPart - m_iLastQuery.QuadPart;

/* Save current timer information */
m_iLastQuery.QuadPart = kTempTimer.QuadPart;

/* Get current time and date */
time (&iTempTimeDate);
m_pkTime = localtime (&iTempTimeDate);
}

/* Get delta time from last update */
mrReal32 mrTimer::GetDelta (void)
{
/* Convert to float and calculate delta in seconds */
return (mrReal32)(m_iDelta.QuadPart) /
(mrReal32)(m_iFrequency.QuadPart);
}

/* Get system seconds */
mrUInt32 mrTimer::GetSeconds (void)
{
return m_pkTime->tm_sec;
}

/* Get system minutes */
mrUInt32 mrTimer::GetMinutes (void)
{
return m_pkTime->tm_min;
}

/* Get system hours */
mrUInt32 mrTimer::GetHours (void)
{
return m_pkTime->tm_hour;
}

/* Get system day */
mrUInt32 mrTimer::GetDay (void)
{
return m_pkTime->tm_mday;
}

/* Get system month */
mrUInt32 mrTimer::GetMonth (void)
{
return m_pkTime->tm_mon;
}

/* Get system year */

mrUInt32 mrTimer::GetYear (void)
{
return m_pkTime->tm_year;
}


mrDataTypes.h

   /* ''mrDatatypes.h'' */

/* Include this file only once */
#pragma once

/* Basic type definitions */
typedef char mrInt8;
typedef unsigned char mrUInt8;
typedef short mrInt16;
typedef unsigned short mrUInt16;
typedef long mrInt32;
typedef unsigned long mrUInt32;
typedef int mrInt;
typedef unsigned int mrUInt;

typedef float mrReal32;
typedef double mrReal64;

/* Composed definitions */

enum mrBool32
{
mrFalse = 0,
mrTrue = 1,

mrBool32_Force32 = 0xFFFFFFFF
};


mrError.h

   /* ''mrError.h'' */

/* Include this file only once */
#pragma once

/* Error codes */

enum mrError32
{
mrNoError = 0,
mrErrorRegisterClass = 1,
mrErrorInitDirect3D,
mrErrorGetAdapterDisplayMode,
mrErrorCreateDevice,
mrErrorPresent,
mrErrorBeginScene,
mrErrorEndScene,
mrErrorClear,
mrErrorDrawPrimitive,
mrError32_Force32 = 0xFFFFFFFF
};


Mirus.cpp (Where Main method is)

  // Mirus.cpp : Defines the entry point for the application.

//


#include "stdafx.h"
#include "mrScreen.h"
#include "mrDatatypes.h"
#include "mrWindow.h"
#include "CustomWindow.h"
#include<iostream.h>
#include<fstream.h>

void write_error(mrError32 theerror, ofstream Outfile);



int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{

CustomWindow kWindow;
kWindow.Create(hInstance, "Draw a Stupid Line!");
kWindow.SetSize(640, 480);
kWindow.SetPosition(0, 0);
kWindow.Run();
HWND blah=kWindow.GetHandle();
ofstream Outfile("error.txt");
mrError32 anerror;
mrScreen Screen;
anerror=Screen.Init(blah);
if(anerror==mrNoError)
{
anerror=Screen.SetMode(false, 1024, 768, 32, false);
if(anerror==mrNoError)
{
anerror=Screen.Clear(255, 255, 255, 255);
if(anerror==mrNoError)
{
anerror=Screen.StartFrame();
if(anerror==mrNoError)
{
anerror=Screen.DrawLine(10, 10, 60, 34, 0, 0 ,0 ,255);
if(anerror==mrNoError)
{
anerror=Screen.EndFrame();
if(anerror==mrNoError)
{
}
else
{
Outfile<<"EndFrame Error"<<endl;
}
}
else
{
Outfile<<"DrawLine Error"<<endl;
}
}
else
{
Outfile<<"StartFrame Error"<<endl;
}
}
else
{
Outfile<<"Clear Error"<<endl;
}
}
else
{
Outfile<<"SetMode Error"<<endl;
}
}
else
{
Outfile<<"Init Error"<<endl;
}
write_error(anerror, Outfile);
return 0;
}



void write_error(mrError32 theerror, ofstream Outfile)
{

switch(theerror)
{
case mrErrorInitDirect3D:
Outfile<<"mrErrorInitDirect3D"<<endl;
break;
case mrErrorGetAdapterDisplayMode:
Outfile<<"mrErrorGetAdapterDisplayMode"<<endl;
break;
case mrErrorCreateDevice:
Outfile<<"mrErrorCreateDevice"<<endl;
break;
case mrErrorPresent:
Outfile<<"mrErrorPresent"<<endl;
break;
case mrErrorBeginScene:
Outfile<<"mrErrorBeginScene"<<endl;
break;
case mrErrorEndScene:
Outfile<<"mrErrorEndScene"<<endl;
break;
case mrErrorClear:
Outfile<<"mrErrorClear"<<endl;
break;
case mrErrorDrawPrimitive:
Outfile<<"mrErrorDrawPrimitive"<<endl;
break;
case mrNoError:
Outfile<<"mrNoError"<<endl;
break;
default:
Outfile<<"You''re on crack."<<endl;
break;
}
}

Share this post


Link to post
Share on other sites
Hi there,
I am reading same book . Just wondering, how did you solve this problem?
Supo

Share this post


Link to post
Share on other sites
Wow, only took a year to get a response. lol!

I actually don''t remember how, or if I solved it. I believe there was a method that returned the correct HWND. Try going to nexe.gamedev.net. I remember that site helped me solve the problem.

I finally gave up on DirectX, particularly because, IMHO, it''s not very newbie friendly. I''ve now moved to OpenGL (LWJGL, with is a Java binding to OpenGL, because I prefer Java) which is easier to learn. I''ve easily been able to create 3-d drawings, add textures, move them around, and take mouse and keyboard input. After I''ve learned the theory behind graphics in computer games, then maybe I''ll go back and try DirectX.

Another way to learn is to use one of the many open-source game engines that are out there. Writing a game from scratch with DX is not a trivial task in any way.

Good luck!

-Nick

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!