MFC Drawing Circle instead of square in tutorial code

Hi,

Found a good beginner's tutorial to learn Visual C++: http://www.cprogramming.com/tutorial/game_programming/same_game_part1.html with source code: http://www.cprogramming.com/tutorial/game_programming/samegame-part5.zip.

I am trying to figure out, as a beginner, how to change the drawing so that instead of squares, the game draws circles. Can figure out that using Ellipse in the code results in circle outlines, but the fill remains as square color shapes.

The drawing code from the tutorial is below. I would appreciate any help to substitute filled circles for filled squares.

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// CSameGameView drawing
void CSameGameView::OnDraw(CDC* pDC)
{
  //  First get a pointer to the document
  CSameGameDoc* pDoc = GetDocument();
  ASSERT_VALID(pDoc);
  if(!pDoc)
    return;
  //  Save the current state of the device context
  int nDCSave = pDC->SaveDC();
  //  Get the client rectangle
  CRect rcClient;
  GetClientRect(&rcClient);
  //  Get the background color of the board
  COLORREF clr = pDoc->GetBoardSpace(-1, -1);
  //	Draw the background first
  pDC->FillSolidRect(&rcClient, clr);
  //  Create the brush for drawing
  CBrush br;
  br.CreateStockObject(HOLLOW_BRUSH);
  CBrush* pbrOld = pDC->SelectObject(&br);
  //	Draw the squares
  for(int row = 0; row < pDoc->GetRows(); row++)
  {
    for(int col = 0; col < pDoc->GetColumns(); col++)
    {
      //  Get the color for this board space
      clr = pDoc->GetBoardSpace(row, col);
      //  Calculate the size and position of this space
      CRect rcBlock;
      rcBlock.top = row * pDoc->GetHeight();
      rcBlock.left = col * pDoc->GetWidth();
      rcBlock.right = rcBlock.left + pDoc->GetWidth();
      rcBlock.bottom = rcBlock.top + pDoc->GetHeight();
      //  Fill in the block with the correct color
      pDC->FillSolidRect(&rcBlock, clr);// How to change the fill color and create circles, not squares?
      //  Draw the block outline
      pDC->Rectangle(&rcBlock); //change to Ellipse?
    }
  }
  //  Restore the device context settings
  pDC->RestoreDC(nDCSave);
  br.DeleteObject();
}


Thank you in advance.
Last edited on
The reason why they draw the rect twice is on line 21. For some reason they don't want a second brush. Read this on msdn:

http://msdn.microsoft.com/en-us/library/aa293743%28v=vs.60%29.aspx

You need the brush for the interior of both: Rectangle and Ellipse

If you create the brush and select it (don't forget to select the old brush afterwards) you can omit line 36. You may create the brush from clr
Thank you.

I will check the MSDN code item and see if I can figure out how to change the code to draw filled circles instead of squares. (I tried simply omitting line 36 and got a black debug screen. So, I must be doing something wrong as a beginner.)
It seems that line 36 is simply clearing the background. So leave it like it is.

If you want to draw a filled ellipse you need to change of course line 38 and line 20/21.
Instead of HOLLOW_BRUSH you may change it to a solid brush with the color you want.
Thank you again. I don't think that this can be changed easily. The way this is working is to color the background squares and then draw an outline of each square (HOLLOW_BRUSH) above.

So, I think that the code would have to be more completely re-written.
Then I suggest: do not change the rectangle, just add the ellipse after line 38 (with it's own brush)
I will try to do this as suggested, see if I can get it to work.

Thank you again.
Topic archived. No new replies allowed.