Author Topic: Bordered Pic Logic Fail  (Read 1540 times)

jitspoe

  • Administrator
  • Autococker
  • Posts: 18802
Bordered Pic Logic Fail
« on: November 03, 2012, 01:14:16 AM »
I should get to bed... but maybe while I'm sleeping, somebody could figure out what my logic failure in this code is:

Code: [Select]
void Draw_BorderedPic (bordered_pic_data_t *data, float x, float y, float w, float h, float scale, float alpha)
{
// TODO: Support rscripts?
image_t *gl = data->image;
int i;
float xdiff = 0.0f, ydiff = 0.0f;
float ytotal, xtotal;

if (!gl)
{
gl = r_notexture;

if (!gl)
{
ri.Con_Printf(PRINT_ALL, "NULL pic in Draw_BorderedPic.\n");
return;
}
}

ytotal =
(data->screencoords[2][3] - data->screencoords[2][1] +
data->screencoords[3][3] - data->screencoords[3][1] +
data->screencoords[4][3] - data->screencoords[4][1]) * scale;

if (ytotal < h)
ydiff = h - ytotal;

xtotal =
(data->screencoords[0][2] - data->screencoords[0][0] +
data->screencoords[1][2] - data->screencoords[1][0] +
data->screencoords[2][2] - data->screencoords[2][0]) * scale;

if (xtotal < w)
xdiff = w - xtotal;

GLSTATE_DISABLE_ALPHATEST;
GLSTATE_ENABLE_BLEND;
qglBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

if (alpha < 1.0f)
qglColor4f(1.0f, 1.0f, 1.0f, alpha);

GL_Bind(gl->texnum);

qglBegin(GL_QUADS);

// bordered pics are drawn in a spiral from upper left and around clockwise.
for (i = 0; i < BORDERED_PIC_COORD_COUNT; ++i)
{
float xstretch = 0.0f, ystretch = 0.0f;
float xoff = 0.0f, yoff = 0.0f;

if (i > 1 && i < 5) // right 3 parts
xoff = xdiff;

if (i > 3 && i < 7) // bottom 3 parts
yoff = ydiff;

// odd indexes (areas between the corners) get stretched to fit.
if (i & 1)
{
if (i & 2) // every other one gets stretched vertically.
ystretch = ydiff;
else
xstretch = xdiff;
}

if (i == 8) // last one gets stretched both ways
{
xstretch = xdiff;
ystretch = ydiff;
}

qglTexCoord2f(data->texcoords[i][0], data->texcoords[i][1]);
qglVertex2f(x + xoff + data->screencoords[i][0] * scale, y + yoff + data->screencoords[i][1] * scale);
qglTexCoord2f(data->texcoords[i][2], data->texcoords[i][1]);
qglVertex2f(x + xoff + data->screencoords[i][2] * scale + xstretch, y + yoff + data->screencoords[i][1] * scale);
qglTexCoord2f(data->texcoords[i][2], data->texcoords[i][3]);
qglVertex2f(x + xoff + data->screencoords[i][2] * scale + xstretch, y + yoff + data->screencoords[i][3] * scale + ystretch);
qglTexCoord2f(data->texcoords[i][0], data->texcoords[i][3]);
qglVertex2f(x + xoff + data->screencoords[i][0] * scale, y + yoff + data->screencoords[i][3] * scale + ystretch);
}

qglEnd();

if (alpha < 1.0f)
qglColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}

It basically draws 9 subsections of an image so you can have a dialog with fancy borders.  Problem is, it's going beyond the width and height in some cases (when the width and height should be large enough that it can draw inside those limits).

jitspoe

  • Administrator
  • Autococker
  • Posts: 18802
Re: Bordered Pic Logic Fail
« Reply #1 on: November 03, 2012, 10:36:17 AM »
I guess I should probably better explain how the system works.  Attached is a breakdown of the problematic borderpic.

Here are the actual coordinates:
Code: [Select]
0,0,224,24
224,0,231,24
231,0,245,24
231,24,245,62
107,62,245,88
14,62,107,88
0,62,14,88
0,24,14,62
14,24,231,62

(probably won't match the image exactly).

You pass in a position, width, height, and scale to the function.  The scale is 1.0 in this case, so it's not really a factor.  The code goes through and calculates the total width and height of the bpic, gets the difference, and stretches the center sections to fit the desired width and height.  The problem I'm having is that the image is ending up too large...

jitspoe

  • Administrator
  • Autococker
  • Posts: 18802
Re: Bordered Pic Logic Fail
« Reply #2 on: November 03, 2012, 12:38:42 PM »
Well, turns out that logic was fine.  I was adding padding elsewhere for the buttons, and forgot to remove all of it for the no-button bpics.