Question about math... rendering varying sized boxes [Archive] - C Board

PDA

View Full Version : Question about math... rendering varying sized boxes


Raigne
04-23-2008, 02:29 AM
Ok I have done this before, but I can't remember exactly how.
I want to be able to render a box of varying sizes(width,height) and also varying scale. Reason for both is to preserve resolution as much as possible.

I have the following variables with are the sprites or images that I want to blit

hgeSprite* TopLeft;
hgeSprite* TopMiddle;
hgeSprite* TopRight;

hgeSprite* LeftCenter;
hgeSprite* Center;
hgeSprite* RightCenter;

hgeSprite* BottomLeft;
hgeSprite* BottomMiddle;
hgeSprite* BottomRight;

Cleary-
TopLeft-Rendered once per frame
TopMiddle-Rendered as many times as the box is wide
TopRight-Rendered once per frame

LeftCenter-Rendered as many times as the box is high
Center-Rendered before each sprite of the box ( the box's background )
RightCenter-Render as many times as the box is high

BottomLeft-Rendered once per frame
BottomMiddle-Rendered as many times as the box is wide
BottomRight-Rendered once per frame

I thought that I should render the corners first and then fill in the rest in a loop. Unless I am blind mentally and I cant see a way to loop the entire render without a million if statements.

NOTE: I think the main problem is that the windows can be scaled at creation, or runtime. so that also makes the code a little different.

All sprites start at 32x32pixels
This is what I have for rendering the corners-

//Scaled position
float tpos_x = pos_x*scale_x;
float tpos_y = pos_y*scale_y;

//scaled section size
float section = (32.0f*scale_x);

float linksize_h = ((32.0f*scale_x)*links_h);
float linksize_v = ((32.0f*scale_y)*links_v);

/* Render corners */

//Top Left corner
Center->RenderEx(tpos_x,tpos_y,0,scale_x,scale_y);
TopLeft->RenderEx(tpos_x,tpos_y,0,scale_x,scale_y);

//Top Right corner
Center->RenderEx((tpos_x+section)+linksize_h,tpos_y,0,scal e_x,scale_y);
TopRight->RenderEx((tpos_x+section)+linksize_h,tpos_y,0,scal e_x,scale_y);

//Bottom Left corner
Center->RenderEx(tpos_x,(tpos_y+section)+linksize_v,0,scal e_x,scale_y);
BottomLeft->RenderEx(tpos_x,(tpos_y+section)+linksize_v,0,scal e_x,scale_y);

//Bottom Right corner
Center->RenderEx((tpos_x+section)+linksize_h,(tpos_y+secti on)+linksize_v,0,scale_x,scale_y);
BottomRight->RenderEx((tpos_x+section)+linksize_h,(tpos_y+secti on)+linksize_v,0,scale_x,scale_y);


so I wrote this so far, but I can seem to get the rest figured out. I was hoping someone could help me with this. I will be extremely grateful for any assistance. Thank you.

Raigne
04-23-2008, 03:36 AM
-Rewritten below-

h3ro
04-23-2008, 06:01 AM
instead of rendering pixel by pixel, you can render line by line which is a looot faster.

What API are you using?

Raigne
04-23-2008, 01:22 PM
I am rendering line by line or rather (sprite by sprite).

Calling the render function does not actual render it. It adds it to a render tree, and then when the frame is over the tree is dumped and rendered.

Do you think I should store the position of the window render in a vector then just rmake the vect if position of the window changes?

Raigne
04-23-2008, 03:36 PM
Ok I rewrote the rendering loop. To fix a problem with positioning. Although I still get the spacing problem with I render with any scale other than 0.5f and 1.0f. I don't know why though.

void UI_Window::Draw()
{
const float NO_ROTATION = 0.0f;
//each section of the window scaled
float Section = (32.0f*scale_x);

//Total width and height of window
float Width = (Section*(links_h+1));
float Height= (Section*(links_v+1));

//Render loop
for ( int x = (int)pos_x; x <= (int)(pos_x+(Width-Section)); x += (int)Section )
{
for ( int y = (int)pos_y; y <= (int)(pos_y+(Height-Section)); y += (int)Section )
{
//Render background
Center->RenderEx((float)x,(float)y,NO_ROTATION,scale_x,sca le_y);

/* Links */
//Render Top middle
if ( ( x > pos_x && x < (pos_x+(Width-Section)) ) && y == pos_y )
TopMiddle->RenderEx((float)x,(float)y,NO_ROTATION,scale_x,sca le_y);

//Render Left center
if ( x == pos_x && ( y > pos_y && y < (pos_y+(Height-Section)) ) )
LeftCenter->RenderEx((float)x,(float)y,NO_ROTATION,scale_x,sca le_y);

//Render Bottom Middle
if ( ( x > pos_x && x < (pos_x+(Width-Section)) ) && y == (pos_y+(Height-Section)) )
BottomMiddle->RenderEx((float)x,(float)y,NO_ROTATION,scale_x,sca le_y);

//Render Right center
if ( x == (pos_x+(Width-Section)) && ( y > pos_y && y < (pos_y+(Height-Section)) ) )
RightCenter->RenderEx((float)x,(float)y,NO_ROTATION,scale_x,sca le_y);

/* Corners */
//Render the TopLeft corner
if ( x == pos_x && y == pos_y )
TopLeft->RenderEx((float)x,(float)y,NO_ROTATION,scale_x,sca le_y);

//Render the TopRight corner
if ( x == (pos_x+(Width-Section)) && y == pos_y )
TopRight->RenderEx((float)x,(float)y,NO_ROTATION,scale_x,sca le_y);

//Render the BottomLeft corner
if ( x == pos_x && y == (pos_y+(Height-Section)) )
BottomLeft->RenderEx((float)x,(float)y,NO_ROTATION,scale_x,sca le_y);

//Render the BottomRight corner
if ( x == (pos_x+(Width-Section)) && y == (pos_y+(Height-Section)) )
BottomRight->RenderEx((float)x,(float)y,NO_ROTATION,scale_x,sca le_y);
}
}
};


That is for rendering the window.

Raigne
04-23-2008, 03:44 PM
Here are some screen of the windows. Cut out the rest for size reasons.

This is the window at 1.5 scale. Clearly it isn't drawing like I want. Not that I will need a window this big.8083

This is the window at 0.5 scale. This looks good to me.
8084

Edit: I know the window graphic doesn't look good now, but it works until I spend time to make a better one.