
/*
    Crystal Space 3d format converter 


    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

//////////////////////////////////////////////////////////////////////

#include <stdio.h>
#include <stdlib.h>
#include "ase2spr.h"

extern int scale;
extern float scalex;
extern float scaley;
extern float scalez;

// Writes an SPR file with ONE frame
int converter::spr_write(FILE * fileout, const char* objectname)
{
    spr_write_header(fileout, objectname);
    spr_write_frame(fileout, 1);
    spr_write_footer(fileout, 1);

    return 0;
}

/*
  Write the header.
*/
int converter::spr_write_header(FILE * fileout, const char* objectname)
{
    fprintf(fileout, "MESHFACT '%s' (\n", objectname);
    fprintf(fileout,
        "  PLUGIN ('crystalspace.mesh.loader.factory.sprite.3d')\n");
    fprintf(fileout, "  PARAMS (\n");
    fprintf(fileout, "    MATERIAL ('%sskin')\n", objectname);

    return 0;
}

#define FRAMENAME   "stand"

/*
  Write the FRAME block with any given number of frames.
*/
int converter::spr_write_frame(FILE * fileout, int framenumber)
{
    int line = 0;
    int j, jj;

    fprintf(fileout, "    FRAME '%s%d' (\n\n", FRAMENAME, framenumber);

    //XXX: is this correct?
    line = line + 3;

    // Scale model if requested
    if (scale)
    scale_model(scalex, scaley, scalez);

    // build a list of all the vertex needed for each face
    for (j = 0; j < num_face; j++) {
        // for each face print 3 vertexes (THIS MUST BE OPTIMIZED!)
        for (jj = 0; jj < 3; jj++) {
            // vertex on T face
            int vertex = (int) face_uv[jj][0][j];

            // invert y and z
            fprintf(fileout, "      V (%f,%f,%f:%f,%f)\n",
            cor3[0][face[jj][j]], cor3[2][face[jj][j]], cor3[1][face[jj][j]], vertex_uv[0][0][vertex], 1-(vertex_uv[1][0][vertex]) );
        }
    }

    fprintf(fileout, "    )\n\n");    // FRAME

    return 0;
}

/*
  Write the action, triangles, closing parenthesis.
*/
int converter::spr_write_footer(FILE * fileout, int numberOfFrames)
{
    int j;

    int line = 0;
    char *actionType = "stand";    // action 'stand', 'run'

    /*
       Write the ACTION block.
     */
    if (numberOfFrames == 1)
    fprintf(fileout, "    ACTION 'stand' ( F ('stand1', 100) ) \n\n");
    else {
    fprintf(fileout, "    ACTION '%s' (", actionType);
    for (j = 1; j <= numberOfFrames; j++) {
        fprintf(fileout, "      F ('%s%d', 100)\n", actionType, j);
    }
    fprintf(fileout, "    )\n\n");
    }

    // Write the TRIANGLE block.
    int count = 0;
    for (iface = 0; iface < num_face; iface++) {
    // invert vertex2 and vertex3
    fprintf(fileout, "    TRIANGLE (%d,%d,%d)\n", count, count+2, count+1);
    count = count + 3;
    line = line + 1;
    }

    /*
       Write the close block.
     */
    fprintf(fileout, "  SMOOTH(0)\n");
    fprintf(fileout, "  )\n");        // PARAMS
    fprintf(fileout, ")\n");        // MESHFACT

    return 0;
}

