Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


At the gate to Window Graphics


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 LeLorrain   Members   -  Reputation: 130

Like
0Likes
Like

Posted 30 November 2012 - 09:47 AM

Hello people,

I am new to windows graphic on my way to learn directx by myself, and, of course, I hit a brick wall!

To start in the tutorial I use, the display of my rendering is done with GDI and all I get is a black window. I wonder if any of you would be nice enough to take a look at my code and tell me what I did wrong.

Please, no comment on the use of GDI, I'll get to display with DirectX in the next lesson. The first lesson was to teach me about rendering 3D on a 2D screen and the various transformations to do it, although we use DirectX to handle the matrices.

From what I see, the vertex coordinates at the "DrawLine" method are correct and fit in the window Rect. The objects are two rotating cubes in wireframe.

Here is the relevant code:

   bool
   GameApp::BuildBuffer	   ( long width, long height )
   {
	  HDC	  hDC   = ::GetDC (m_hWnd);
	  if (NULL == m_hDC) {
		 m_hDC    = ::CreateCompatibleDC (hDC);
	  }
	  m_hBackBuffer   = ::CreateCompatibleBitmap (hDC, width, height);
	  if (NULL == m_hBackBuffer) {
		 return false;
	  }
	  m_hSavedBuffer  = (HBITMAP)::SelectObject (hDC, m_hBackBuffer);
	  ::ReleaseDC (m_hWnd, hDC);
	  ::SetBkMode (m_hDC, TRANSPARENT);
	  return true;
   }
   void
   GameApp::SetupGameState    ( void )
   {
	  /********************************\
	   *    View matrix with:
	   *
	   *    Right    = ( 1, 0, 0 )
	   *    Up	   = ( 0, 1, 0 )
	   *    Eye	  = ( 0, 0, 1 )
	   *    origin   = ( 0, 0, 0 )
	   *    (window origin)
	   *
	  \********************************/
	  //    Set the View matrice to Identity
	  D3DXMatrixIdentity (&m_View);
	  /********************************\
	   *
	   *    Perspective Matrix with:
	   *
	   *    FOV:	 60°
	   *    Aspect:  Width / Height
	   *    Near:    1.01
	   *    Far:  1000.00
	   *
	  \********************************/
	  //    Set the Perspective Matrix
	  D3DXMatrixPerspectiveFovLH (&m_Projection, (float)D3DXToRadian (60.0), (float)m_ViewWidth / (float)m_ViewHeight, 1.01, 1000.0);
	  return;
   }
   int
   GameApp::BeginGame		 ( void )
   {
	  MSG   msg;
	  int   rc    = 0;
	  //    Start main loop
	  while (true) {
		 if (::PeekMessage (&msg, NULL, 0, 0,PM_REMOVE)) {
		    if (WM_QUIT == msg.message) {
			   rc    = msg.wParam;
			   break;
		    }
		    ::TranslateMessage (&msg);
		    ::DispatchMessage  (&msg);
		 } else {
		    FrameAdvance ();
		 }
	  }
	  return rc;
   }
	  void
   GameApp::FrameAdvance   ( void )
   {
	  //    Limit to 60 frames per seconds
	  m_Timer.LockFPS (60);
	  //    Animate the objects
	  AnimateObjects ((float)m_Timer.GetIntervalSeconds());
	  //    Clear the buffer
	  ClearBuffer (0x00FFFFFF);
	  //    Draw objects
	  for (size_t i = 0; i < m_Objects.size(); ++i) {
		 Mesh  *  mesh  = &m_Objects[i]->m_Mesh;
		 //    Loop on each polygon
		 for (size_t j = 0; j < mesh->m_Polygon.size(); ++j) {
			   DrawPolygon (&mesh->m_Polygon[j], &m_Objects[i]->m_World);
		 }
	  }
	  //    Add the frames per second
	  stringstream   ss;
	  ss << "f/s " << (int)m_Timer.GetFPS() << ends;
	  TextOut (m_hDC, 5, 5, ss.str().c_str(), ss.str().size());
	  //    Display all
	  DisplayScene ();
	  //    Success!
	  return;
   }
   void
   GameApp::AnimateObjects    ( float deltaTime )
   {
	  for (size_t i = 0; i < m_Objects.size(); ++i) {
		 if (m_Objects[i]->m_Rotation) {
		    D3DXMATRIX  rotate;
		    D3DXMatrixRotationYawPitchRoll (&rotate, (float)(D3DXToRadian (75.0) * deltaTime), (float)(D3DXToRadian (75.0) * deltaTime), (float)(D3DXToRadian (75.0) * deltaTime));
		    m_Objects[i]->m_World   = rotate * m_Objects[i]->m_World;
		 }
	  }
	  return;
   }
   void
   GameApp::ClearBuffer    ( D3DCOLOR colour )
   {
	  //    Setup the brush to paint the buffer
	  LOGBRUSH logBrush;
	  logBrush.lbColor	 = (COLORREF)(0x00FFFFFF & colour);
	  logBrush.lbStyle	 = BS_SOLID;
	  logBrush.lbHatch	 = NULL;
	  //    Create the brush
	  HBRUSH   hBrush	  = ::CreateBrushIndirect (&logBrush);
	  //    Put it into the frame buffer
	  HBRUSH   hOldBrush   = (HBRUSH)::SelectObject (m_hDC, hBrush);
	  LOGPEN   pen;
	  //    Setup the pen invisible
	  pen.lopnStyle	    = PS_NULL;
	  pen.lopnWidth.x	  = 1;
	  pen.lopnWidth.y	  = 1;
	  pen.lopnColor	    = 0;
	  //    Create the pen
	  HPEN	 hPen	    = ::CreatePenIndirect (&pen);
	  //    Put it into the frame buffer
	  HPEN	 hOldPen	 = (HPEN)::SelectObject (m_hDC, hPen);
	  //    Draw a rectangle covering the Window
	  ::Rectangle (m_hDC, m_ViewX, m_ViewY, (m_ViewX + m_ViewWidth), (m_ViewY + m_ViewHeight));
	  //    Restore the old brush
	  ::SelectObject (m_hDC, hOldBrush);
	  //    Destroy Rendering brush
	  ::DeleteObject (hBrush);
	  //    Restore the old pen
	  ::SelectObject (m_hDC, hOldPen);
	  //    Destroy Rendering pen
	  ::DeleteObject (hPen);
	  return;
   }
   void
   GameApp::DrawPolygon	   ( Polygon * polygon, const D3DXMATRIX * world)
   {
	  D3DXVECTOR3	  vertex0, vertex1;
	  size_t   n  = polygon->m_Vertex.size();
	  //    Loop thru each vector
	  for (size_t i = 0; i < n + 1; ++i) {
		 //    Transform first vertex to world space
		 vertex1  = (polygon->m_Vertex[i % n]).SetD3Dvector();
		 D3DXVec3TransformCoord (&vertex1, &vertex1, world);
		 //    Transform to view space
		 D3DXVec3TransformCoord (&vertex1, &vertex1, &m_View);
		 //    Convert to screen space
		 D3DXVec3TransformCoord (&vertex1, &vertex1, &m_Projection);
		 //    Convert to window coords
		 vertex1.x  = m_ViewX + m_ViewWidth  * (vertex1.x + 1.0) / 2;
		 vertex1.y  = m_ViewY + m_ViewHeight * (1.0 - vertex1.y) / 2;
		 //    If this is not the first, draw
		 if (i > 0) {
		    DrawLine (vertex0, vertex1, 0x00FFFFFF);
		 }
		 vertex0  = vertex1;
	  }
	  //    Success!
	  return;
   }
   void
   GameApp::DrawLine	   ( const D3DXVECTOR3 &v0, const D3DXVECTOR3 &v1, const D3DCOLOR colour )
   {
	  LOGPEN   pen;
	  //    Setup the rendering pen
	  pen.lopnStyle	 = PS_SOLID;
	  pen.lopnWidth.x   = 2;
	  pen.lopnWidth.y   = 1;
	  pen.lopnColor	 = (COLORREF)(0x00FFFFFF & colour);
	  //    Create the rendering pen
	  HPEN	 hPen	 = ::CreatePenIndirect (&pen);
	  //    Put it into the frame buffer
	  HPEN	 hOldPen  = (HPEN)::SelectObject (m_hDC, hPen);
	  //    Draw the line segment
	  ::MoveToEx (m_hDC, (long)v0.x, (long)v0.y, NULL);
	  LineTo (m_hDC, (long)v1.x, (long)v1.y);
	  //    Restore the old pen
	  ::SelectObject (m_hDC, hOldPen);
	  //    Destroy Rendering pen
	  ::DeleteObject (hPen);
	  //    Success!
	  return;
   }
   void
   GameApp::DisplayScene   ( void )
   {
	  HDC   hDC   = ::GetDC (m_hWnd);
	  //    Move the back buffer to the screen
	  ::BitBlt (hDC, m_ViewX, m_ViewY, m_ViewWidth, m_ViewHeight, m_hDC, m_ViewX, m_ViewY, SRCCOPY);
	  //    Cleanup
	  ::ReleaseDC (m_hWnd, hDC);
	  return;
   }


Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS