Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[svn] Some heroic coding, since I can't figure out how to get printf(…
…) to output

 floats _exactly_ how I'd want them.

--HG--
branch : trunk
  • Loading branch information
icculus committed Mar 22, 2008
1 parent c4e14f2 commit bcedc4f
Showing 1 changed file with 35 additions and 2 deletions.
37 changes: 35 additions & 2 deletions d3d2glsl.c
Expand Up @@ -959,11 +959,44 @@ static void emit_D3D_SETP(Context *ctx)
emit_D3D_opcode_dss(ctx, op);
} // emit_D3D_SETP

// !!! FIXME: this is sort of nasty.
static void floatstr(Context *ctx, char *buf, size_t bufsize, float f)
{
const size_t len = snprintf(buf, bufsize, "%f", f);
if (len >= bufsize)
fail(ctx, "BUG: internal buffer is too small");
else
{
char *end = buf + len;
char *ptr = strchr(buf, '.');
if (ptr == NULL)
return; // done.

while (--end != ptr)
{
if (*end != '0')
{
end++;
break;
} // if
} // while
*end = '\0'; // chop extra '0' or all decimal places off.
} // else
} // floatstr

static void emit_D3D_DEF(Context *ctx)
{
const char *dst0 = make_D3D_destarg_string(ctx, 0);
const float *x = (const float *) ctx->dwords; // !!! FIXME: could be int?
output_line(ctx, "def%s, %f, %f, %f, %f", dst0, x[0], x[1], x[2], x[3]);
const float *val = (const float *) ctx->dwords; // !!! FIXME: could be int?
char val0[32];
char val1[32];
char val2[32];
char val3[32];
floatstr(ctx, val0, sizeof (val0), val[0]);
floatstr(ctx, val1, sizeof (val1), val[1]);
floatstr(ctx, val2, sizeof (val2), val[2]);
floatstr(ctx, val3, sizeof (val3), val[3]);
output_line(ctx, "def%s, %s, %s, %s, %s", dst0, val0, val1, val2, val3);
} // emit_D3D_DEF

static void emit_D3D_DEFI(Context *ctx)
Expand Down

0 comments on commit bcedc4f

Please sign in to comment.