FillRect

Started by
7 comments, last by UeberBobo 20 years, 6 months ago
im using fillRect to fill blocks in my pong clone like this: RECT block; for (int i = 0; i < 20; i++{ for (int u = 0; u < 20; u++{ block.left = i * blockwidth; block.right = block.left + blockwidth; block.top = u * blockheight; block.right = block.left + blockheight; FillRect(hdc, RECT, (HBRUSH)CreateSolidBrush(RGB(0,0,255))); } } //just wrote that here so there might be errors, dosnt matter it works the way i have it in my prog this is the just basic idea is there no easier way of doing this? all the block.left stuff etc seems unnecessary FillRect(hdc, {x1, y1, x1+a, y1+b}, (HBRUSH)CreateSolidBrush(RGB(0,0,255))); //something like this seems easier is there any way to do someting like this? maybe i can make i/u pointers to a RECT.left/top so block changes with i/u++? what would be the best way? thanx
//---------//Ueberbobo//*********
Advertisement
Well, you could do:

RECT Block = {x, y, x+height, y+height}  


If your paddle was an object, I would suggest that everytime you move the paddle, it would automatically update its own rectangle, given an x,y position (the upper left corner) and the width and height (the lower right)

For example:

class Paddle{public://not a good idea to make everything public, but it works for thisRECT MyRect;int xPos, yPos;int width, height;void MovePaddle();}void Paddle::MovePaddle(){//pretend its moving up//variables would have to be preinitialized with some value as wellyPos--;MyRect.top = yPos;MyRect.bottom = yPos+height;}

Thst's just a little aside, though. If you didn't use an OO method, I would probably do the RECT {a,b,c,d} method, where you initialize all the variables as you created it.


[edited by - Peon on October 12, 2003 1:10:19 PM]
Peon
if sorry if i was unclear what this is supposed to do is fill the screen with blocks to hit with the ball (yes this is breakout not pong but im stupid)

peon i dont understand what youre doing, i think you misunderstood me. what i want to to is reducing the...

block.left = i * blockwidth;
block.right = block.left + blockwidth;
block.top = u * blockheight;
block.right = block.left + blockheight;
FillRect(hdc, RECT, (HBRUSH)CreateSolidBrush(RGB(0,0,255)));

...part to one or at least fewer lines this doesnt feel like the best way to do it

[edited by - ueberbobo on October 12, 2003 1:27:02 PM]
//---------//Ueberbobo//*********
nothing fancy, but maybe it''ll help:
RECT clientRect;GetClientRect(hWnd, &clientRect);const SIZE gameArea = { clientRect.right  - clientRect.left,                        clientRect.bottom - clientRect.top };const SIZE blockSpacing = { 1,   2 };const SIZE blockSize    = { 100, 40 };const SIZE blockGrid    = { blockSize.cx + blockSpacing.cx,                            blockSize.cy + blockSpacing.cy };HBRUSH brush = CreateSolidBrush(RGB(0, 0, 255));for( int x = 0; x < gameArea.cx; x += blockGrid.cx ){    for( int y = 0; y < gameArea.cy; y += blockGrid.cy )    {        RECT rect = { x, y, x+blockSize.cx, y+blockSize.cy };        FillRect(hDC, &rect, &brush);    }}DeleteObject(brush); 

you could use Rectangle instead of FillRect, but you''ll need a pen as well as a brush. Rectangle takes 4 coords though, so you don''t have to keep re-creating the RECT.

p.s., don''t ever create any GDI objects inline like you have it coded. that''ll create a GDI memory leak since there''s no way for you to DeleteObject it later. if you actually tried to run that code i''m sure you''d crash Windows, or at least your app, pretty quickly. just keep in mind that you must delete everything you create.
as i said thats just a simplification of how my prog works. i dont have any leaks, not like that anyway

i dont get it can i init the RECT like that once every time the loop executes? is it deleted when the loop restarts?
didnt know that im a even bigger newb than i thought lol

i thought it seemed unnecessary to copy the values to a RECT structure every time. i was wondering if there were some way i could send the coords to the FillRect in some way without going through a RECT struct first, as i said perhaps by using pointers in some way...

[edited by - ueberbobo on October 12, 2003 2:53:49 PM]
//---------//Ueberbobo//*********
how about this version

RECT block;
HBRUSH brush;

brush = (HBRUSH) CreateSolidBrush(RGB(0, 0, 255));

block.left = 0;
block.right = blockwidth;
block.top = 0;
block.bottom = blockheight;

for(int y = 0; y < 20; ++y)
{
for(int x = 0; x < 20; ++)
{
OffsetRect(&block, blockwidth, blockheight);
FillRect(hdc, &block, brush);
}
}

DeleteObject(brush);
sorry about that, can''t edit as an ap.
wrote it a litte too fast..

RECT block;
HBRUSH brush;

brush = (HBRUSH) CreateSolidBrush(RGB(0, 0, 255));

block.left = 0;
block.right = blockwidth;
block.top = 0;
block.bottom = blockheight;

for(int y = 0; y < 20; ++y)
{
for(int x = 0; x < 20; ++)
{
OffsetRect(&block, blockwidth, 0);
FillRect(hdc, &block, brush);
}

OffsetRect(&block, -blockwidth * 20, blockheight);
}

DeleteObject(brush);

hope i got it right this time
me likes OffsetRect :D
thanx
//---------//Ueberbobo//*********
just realized im doing this in a stupid way. cant believe i didnt come up with this solution earlier
heres my code:

                for (blockRect.left = xSpace + 1; blockRect.left < xBlockAmount * blockWidth; blockRect.left += blockWidth){                  blockRect.right = blockRect.left + blockWidth - xSpace;   //xSpace is the space between blocks                                  for (blockRect.top = ySpace; blockRect.top < yBlockAmount * blockHeight; blockRect.top += blockHeight){                    blockRect.bottom = blockRect.top + blockHeight - ySpace;   //ySpace is the space between blocks                    FillRect(bufferdc, &blockRect, blueBrush);                  }               }


[edited by - ueberbobo on October 13, 2003 12:16:51 PM]

[edited by - ueberbobo on October 13, 2003 12:17:42 PM]
//---------//Ueberbobo//*********

This topic is closed to new replies.

Advertisement