CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2007
    Location
    Australia
    Posts
    151

    I am using WM_PAINT to draw a 2D array of ints into colors only draws 1 square - help

    Ok, I am making a little 2D game for fun, I have squares of colors on my window for pathways such as grass, dirt, this is what I have:

    (In my 2D iMap Array, the first 2 on the top line, is the one that gets drawn, but not the first two on the second line)

    Variables:
    Code:
    #define COLOR_VOID RGB(0, 0, 0)
    #define COLOR_WHITE RGB(255, 255, 255)
    #define COLOR_DIRT RGB(162, 42, 42)
    #define COLOR_GRASS RGB(151, 251, 151)
    #define COLOR_PERSON RGB(255, 0, 0)
    //VARIABLES
    const int iTileX = 20;
    const int iTileY = 20;
    const int iTileViewX = 14;
    const int iTileViewY = 14;
    const int iTileHeight = 70;
    const int iTileWidth = 70;
    int iPlayerX = 0;
    int iPlayerY = 0;
    COLORREF iMapColors[3] = {COLOR_VOID, COLOR_DIRT, COLOR_GRASS};
    
    int iMap[iTileX][iTileY] = {{0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    							{0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    							{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    							{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    							{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    							{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    							{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    							{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    							{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    							{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    							{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    							{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    							{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    							{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    							{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    							{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    							{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    							{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    							{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
    							{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
    And this is the actual code for painting it:
    Code:
    	case WM_PAINT:
    		{
    		hdc = BeginPaint(hWnd, &ps);
    		int xInt = 0;
    		int yInt = 0;
    		int tmpInt = 0;
    		for (int i = 0; i < iTileX; i++)
    		{
    			for (int k = 0; k < iTileY; k++)
    			{
    				HBRUSH hbr = CreateSolidBrush(iMapColors[iMap[i][k]]);
    				RECT rect;
    				SetRect(&rect, iTileWidth*yInt, iTileHeight*xInt, (iTileWidth*yInt)+iTileWidth, (iTileHeight*xInt)+iTileHeight);
    				FillRect(hdc, &rect, hbr);
    				if (yInt+1 % iTileViewY == 0)
    				{
    					yInt = -1;
    					xInt += 1;
    				}
    				yInt += 1;
    				tmpInt += 1;
    			}
    		}
    		EndPaint(hWnd, &ps);
    		break;
    		}
    Does anyone know the problem? it should draw more than one rectangle, it should draw 2...

    Cheers

  2. #2
    Join Date
    Dec 2007
    Location
    Australia
    Posts
    151

    Re: I am using WM_PAINT to draw a 2D array of ints into colors only draws 1 square -

    Fixed, I changed the SetRect line to SetRect(&rect, iTileWidth*k, iTileHeight*i, (iTileWidth*k)+iTileWidth, (iTileHeight*i)+iTileHeight);

    and it worked..

    Cheers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured