2007-11-04 03:34:51 +00:00
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
Copyright (C) 1999-2007 id Software, Inc. and contributors.
|
|
|
|
For a list of contributors, see the accompanying CONTRIBUTORS file.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
This file is part of GtkRadiant.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant 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 General Public License for more details.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with GtkRadiant; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
#include "qdata.h"
|
|
|
|
#include "flex.h"
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
#define MAXFILES 2048
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
typedef struct
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
int w;
|
|
|
|
int h;
|
|
|
|
int cw;
|
|
|
|
int ch;
|
|
|
|
int rw;
|
|
|
|
int index;
|
|
|
|
int depth;
|
|
|
|
int col;
|
|
|
|
int baseline;
|
|
|
|
char name[128];
|
2007-11-04 03:34:51 +00:00
|
|
|
} Coords;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int filenum;
|
|
|
|
int valid;
|
|
|
|
Coords in[MAXFILES];
|
|
|
|
Coords out;
|
|
|
|
char outscript[256];
|
|
|
|
char sourcedir[256];
|
|
|
|
char outusage[256];
|
|
|
|
char root[32];
|
|
|
|
|
|
|
|
int destsize = 0;
|
|
|
|
byte *pixels = NULL; // Buffer to load image
|
|
|
|
long *outpixels = NULL; // Buffer to store combined textures
|
|
|
|
long *usagemap = NULL; // Buffer of usage map
|
|
|
|
void *bmptemp = NULL; // Buffer of usage map
|
|
|
|
byte *map = NULL;
|
|
|
|
|
|
|
|
int xcharsize;
|
|
|
|
int ycharsize;
|
|
|
|
int dosort = 0;
|
|
|
|
int missed = 0;
|
|
|
|
int overlap = 0;
|
|
|
|
int nobaseline = 0;
|
|
|
|
int percent;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////
|
|
|
|
// Setting the char based usage map //
|
|
|
|
//////////////////////////////////////////////////
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
byte TryPlace( Coords *coord ){
|
|
|
|
int x, y;
|
|
|
|
byte entry = 0;
|
|
|
|
byte *mapitem;
|
|
|
|
|
|
|
|
mapitem = map + ( coord->x / xcharsize ) + ( ( coord->y / ycharsize ) * out.cw );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( y = 0; y < coord->ch; y++, mapitem += out.cw - coord->cw )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( x = 0; x < coord->cw; x++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( entry |= *mapitem++ & 8 ) {
|
|
|
|
return( entry );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
return( entry );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void SetMap( Coords *coord ){
|
|
|
|
int x, y;
|
|
|
|
byte *mapitem;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
mapitem = map + ( coord->x / xcharsize ) + ( ( coord->y / ycharsize ) * out.cw );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( y = 0; y < coord->ch; y++, mapitem += out.cw - coord->cw )
|
|
|
|
for ( x = 0; x < coord->cw; x++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
*mapitem++ |= 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////
|
|
|
|
// Setting the pixel based usage map //
|
|
|
|
//////////////////////////////////////////////////
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void CheckOverlap( Coords *coord ){
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
long *dest;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
x = coord->x;
|
|
|
|
y = coord->y;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
dest = (long *)( usagemap + x + ( y * out.w ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( y = 0; y < coord->h; y++, dest += out.w - coord->w )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( x = 0; x < coord->w; x++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( *dest++ ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
overlap++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void SetUsageMap( Coords *coord ){
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
long *dest;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
x = coord->x;
|
|
|
|
y = coord->y;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
dest = (long *)( usagemap + x + ( y * out.w ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( y = 0; y < coord->h; y++, dest += out.w - coord->w )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( x = 0; x < coord->w; x++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
*dest++ = coord->col;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////
|
|
|
|
// Flips the BMP image to the correct way up //
|
|
|
|
//////////////////////////////////////////////////
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void CopyLine( byte *dest, byte *src, int size ){
|
|
|
|
int x;
|
|
|
|
|
|
|
|
for ( x = 0; x < size; x++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
*dest++ = *src++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************/
|
|
|
|
/* Printing headers etc */
|
|
|
|
/****************************************************/
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void RemoveLeading( char *name ){
|
|
|
|
int i;
|
|
|
|
char temp[128];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = strlen( name ) - 1; i > 0; i-- )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( name[i] == '\\' ) || ( name[i] == '/' ) ) {
|
|
|
|
strcpy( temp, name + i + 1 );
|
|
|
|
strcpy( name, temp );
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void RemoveExt( char *name ){
|
|
|
|
while ( ( *name != '.' ) && *name )
|
2007-11-04 03:34:51 +00:00
|
|
|
name++;
|
|
|
|
*name = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************/
|
|
|
|
/* Misc calcualtions */
|
|
|
|
/****************************************************/
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int TotalArea(){
|
|
|
|
int i;
|
|
|
|
int total = 0;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < ( filenum + 2 ); i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
total += in[i].w * in[i].h;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
return( total );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************/
|
|
|
|
/* Setup and checking of all info */
|
|
|
|
/****************************************************/
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void InitVars(){
|
2007-11-04 03:34:51 +00:00
|
|
|
filenum = 0;
|
|
|
|
valid = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
dosort = 0;
|
|
|
|
missed = 0;
|
|
|
|
overlap = 0;
|
|
|
|
nobaseline = 0;
|
|
|
|
|
|
|
|
memset( outscript, 0, sizeof( outscript ) );
|
|
|
|
memset( outscript, 0, sizeof( sourcedir ) );
|
|
|
|
memset( outscript, 0, sizeof( outusage ) );
|
|
|
|
memset( outscript, 0, sizeof( root ) );
|
|
|
|
|
|
|
|
memset( in, 0, sizeof( in ) );
|
|
|
|
memset( &out, 0, sizeof( out ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
void Cleanup(){
|
|
|
|
if ( pixels ) {
|
|
|
|
free( pixels );
|
|
|
|
}
|
|
|
|
if ( usagemap ) {
|
|
|
|
free( usagemap );
|
|
|
|
}
|
|
|
|
if ( outpixels ) {
|
|
|
|
free( outpixels );
|
|
|
|
}
|
|
|
|
if ( bmptemp ) {
|
|
|
|
free( bmptemp );
|
|
|
|
}
|
|
|
|
if ( map ) {
|
|
|
|
free( map );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct glxy_s
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
float xl, yt, xr, yb;
|
|
|
|
int w, h, baseline;
|
2007-11-04 03:34:51 +00:00
|
|
|
} glxy_t;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int SaveScript( char *name ){
|
|
|
|
FILE *fp;
|
|
|
|
int i, j;
|
|
|
|
glxy_t buff;
|
|
|
|
|
|
|
|
if ( fp = fopen( name, "wb" ) ) {
|
|
|
|
for ( j = 0; j < filenum; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < filenum; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( in[i].index == j ) {
|
|
|
|
if ( in[i].depth ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
buff.xl = (float)in[i].x / (float)out.w;
|
|
|
|
buff.yt = (float)in[i].y / (float)out.h;
|
2012-03-17 20:01:54 +00:00
|
|
|
buff.xr = ( (float)in[i].w + (float)in[i].x ) / (float)out.w;
|
|
|
|
buff.yb = ( (float)in[i].h + (float)in[i].y ) / (float)out.h;
|
2007-11-04 03:34:51 +00:00
|
|
|
buff.w = in[i].w;
|
|
|
|
buff.h = in[i].h;
|
|
|
|
buff.baseline = in[i].baseline;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
memset( &buff, 0, sizeof( glxy_t ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
fwrite( &buff, 1, sizeof( glxy_t ), fp );
|
2007-11-04 03:34:51 +00:00
|
|
|
i = filenum;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
fclose( fp );
|
|
|
|
return( true );
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return( false );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int GetScriptInfo( char *name ){
|
|
|
|
FILE *fp;
|
|
|
|
char buffer[256];
|
|
|
|
char tempbuff[256];
|
|
|
|
char delims[] = {" \t,\n"};
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "Opening script file %s.\n", name );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( fp = fopen( name, "r" ) ) {
|
|
|
|
while ( fgets( buffer, 256, fp ) )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( strncmp( buffer, "//", 2 ) && strncmp( buffer, "\n", 1 ) ) {
|
|
|
|
strupr( buffer );
|
|
|
|
strcpy( tempbuff, buffer );
|
|
|
|
if ( strcmp( strtok( tempbuff, delims ), "OUTPUT" ) == 0 ) {
|
|
|
|
strcpy( out.name, strtok( NULL, delims ) );
|
|
|
|
strlwr( out.name );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
strcpy( tempbuff, buffer );
|
|
|
|
if ( strcmp( strtok( tempbuff, delims ), "SOURCEDIR" ) == 0 ) {
|
|
|
|
strcpy( tempbuff, strtok( NULL, delims ) );
|
|
|
|
strcpy( sourcedir, ExpandPathAndArchive( tempbuff ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
strcpy( tempbuff, buffer );
|
|
|
|
if ( strcmp( strtok( tempbuff, delims ), "DOSORT" ) == 0 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
dosort = 1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
strcpy( tempbuff, buffer );
|
|
|
|
if ( strcmp( strtok( tempbuff, delims ), "XCHARSIZE" ) == 0 ) {
|
|
|
|
xcharsize = strtol( strtok( NULL, delims ), NULL, 0 );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
strcpy( tempbuff, buffer );
|
|
|
|
if ( strcmp( strtok( tempbuff, delims ), "YCHARSIZE" ) == 0 ) {
|
|
|
|
ycharsize = strtol( strtok( NULL, delims ), NULL, 0 );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
strcpy( tempbuff, buffer );
|
|
|
|
if ( strcmp( strtok( tempbuff, delims ), "OUTSCRIPT" ) == 0 ) {
|
|
|
|
strcpy( outscript, strtok( NULL, delims ) );
|
|
|
|
strlwr( outscript );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
strcpy( tempbuff, buffer );
|
|
|
|
if ( strcmp( strtok( tempbuff, delims ), "OUTUSAGE" ) == 0 ) {
|
|
|
|
strcpy( outusage, strtok( NULL, delims ) );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
strcpy( tempbuff, buffer );
|
|
|
|
if ( strcmp( strtok( tempbuff, delims ), "POS" ) == 0 ) {
|
|
|
|
out.w = strtol( strtok( NULL, delims ), NULL, 0 );
|
|
|
|
out.h = strtol( strtok( NULL, delims ), NULL, 0 );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
strcpy( tempbuff, buffer );
|
|
|
|
if ( strcmp( strtok( tempbuff, delims ), "FILE" ) == 0 ) {
|
|
|
|
strcpy( in[filenum].name, strtok( NULL, delims ) );
|
|
|
|
in[filenum].x = strtol( strtok( NULL, delims ), NULL, 0 );
|
|
|
|
in[filenum].y = strtol( strtok( NULL, delims ), NULL, 0 );
|
|
|
|
in[filenum].col = strtol( strtok( NULL, delims ), NULL, 0 );
|
2007-11-04 03:34:51 +00:00
|
|
|
filenum++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
fclose( fp );
|
|
|
|
return( true );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "ERROR : Could not open script file.\n" );
|
|
|
|
return( false );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int CheckVars(){
|
|
|
|
int i;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( out.name[0] == 0 ) {
|
|
|
|
printf( "ERROR : No output name specified.\n" );
|
|
|
|
return( false );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( out.w <= 0 ) || ( out.h <= 0 ) ) {
|
|
|
|
printf( "ERROR : Invalid VRAM coordinates.\n" );
|
|
|
|
return( false );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( filenum == 0 ) {
|
|
|
|
printf( "ERROR : No input files specified.\n" );
|
|
|
|
return( false );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < filenum; i++ )
|
|
|
|
if ( in[i].name[0] == 0 ) {
|
|
|
|
printf( "ERROR : Input filename invalid.\n" );
|
|
|
|
return( false );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
return( true );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Makes sure texture is totally within the output area
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int CheckCoords( Coords *coord ){
|
|
|
|
if ( ( coord->x + coord->w ) > out.w ) {
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
if ( ( coord->y + coord->h ) > out.h ) {
|
|
|
|
return( false );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
return( true );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
// Gets the width, height, palette width and palette height of each BMP file
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int GetFileDimensions(){
|
|
|
|
int i;
|
|
|
|
int width, height;
|
|
|
|
char name[128];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < filenum; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
in[i].index = i;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
strcpy( name, sourcedir );
|
|
|
|
strcat( name, in[i].name );
|
|
|
|
printf( "Getting file dimensions, file : %s \r", in[i].name );
|
|
|
|
if ( FileExists( name ) ) {
|
|
|
|
LoadAnyImage( name, NULL, NULL, &width, &height );
|
2007-11-04 03:34:51 +00:00
|
|
|
in[i].depth = 32;
|
|
|
|
in[i].rw = width;
|
2012-03-17 20:01:54 +00:00
|
|
|
in[i].w = width; // makes it width in
|
2007-11-04 03:34:51 +00:00
|
|
|
in[i].h = height;
|
2012-03-17 20:01:54 +00:00
|
|
|
in[i].cw = ( in[i].w + ( xcharsize - 1 ) ) / xcharsize;
|
|
|
|
in[i].ch = ( in[i].h + ( ycharsize - 1 ) ) / ycharsize;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !CheckCoords( &in[i] ) && ( in[i].x >= 0 ) ) {
|
|
|
|
printf( "Error : texture %s out of bounds.\n", in[i].name );
|
|
|
|
return( false );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
valid++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
in[i].depth = 0;
|
|
|
|
in[i].x = -1;
|
|
|
|
in[i].y = -1;
|
|
|
|
in[i].w = 0;
|
|
|
|
in[i].h = 0;
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "\n\n" );
|
|
|
|
return( true );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Sorts files into order for optimal space finding
|
|
|
|
// Fixed position ones first, followed by the others in descending size
|
|
|
|
// The theory being that it is easier to find space for smaller textures.
|
|
|
|
// size = (width + height)
|
|
|
|
// For space finding it is easier to place a 32x32 than a 128x2
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
#define WEIGHT 0x8000
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void Swap( Coords *a, Coords *b ){
|
|
|
|
Coords c;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
c = *a;
|
|
|
|
*a = *b;
|
|
|
|
*b = c;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void SortInNames(){
|
|
|
|
int i, j;
|
|
|
|
int largest, largcount;
|
|
|
|
int size;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "Sorting filenames by size.\n\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0; j < filenum; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
largest = -1;
|
|
|
|
largcount = -1;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = j; i < filenum; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( in[i].depth ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
size = in[i].w + in[i].h;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
|
|
|
if ( ( in[i].x < 0 ) && ( size > largest ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
largcount = i;
|
|
|
|
largest = size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( largcount >= 0 ) && ( largcount != j ) ) {
|
|
|
|
Swap( &in[j], &in[largcount] );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int SetVars( char *name ){
|
|
|
|
if ( !GetScriptInfo( name ) ) {
|
|
|
|
return( false );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !CheckVars() ) {
|
|
|
|
return( false );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
destsize = out.w * out.h;
|
|
|
|
|
|
|
|
out.cw = out.w / xcharsize;
|
|
|
|
out.ch = out.h / ycharsize;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( usagemap = (long *)SafeMalloc( destsize * 4, "" ) ) == NULL ) {
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
if ( ( outpixels = (long *)SafeMalloc( destsize * 4, "" ) ) == NULL ) {
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
if ( ( bmptemp = (void *)SafeMalloc( destsize * 4, "" ) ) == NULL ) {
|
|
|
|
return( false );
|
|
|
|
}
|
|
|
|
if ( ( map = (byte *)SafeMalloc( destsize / ( xcharsize * ycharsize ), "" ) ) == NULL ) {
|
|
|
|
return( false );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( GetFileDimensions() == false ) {
|
|
|
|
return( false );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( dosort ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
SortInNames();
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
return( true );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
/****************************************************/
|
|
|
|
/* Actual copying routines */
|
|
|
|
/****************************************************/
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int FindCoords( Coords *coord ){
|
|
|
|
int tx, ty;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( coord->x >= 0 ) {
|
|
|
|
SetMap( coord );
|
|
|
|
return( true );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( ty = 0; ty < out.ch; ty++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( tx = 0; tx < out.cw; tx++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
coord->x = ( tx * xcharsize );
|
|
|
|
coord->y = ( ty * ycharsize );
|
|
|
|
|
|
|
|
if ( CheckCoords( coord ) && !TryPlace( coord ) ) {
|
|
|
|
SetMap( coord );
|
|
|
|
return( true );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
coord->x = -1;
|
|
|
|
coord->y = -1;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
return( false );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void CheckBaseline( int i ){
|
|
|
|
int y;
|
|
|
|
long *pix;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
in[i].baseline = -1;
|
|
|
|
pix = (long *)pixels;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( y = 0; y < in[i].h; y++, pix += in[i].w )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( *pix & 0x00ffffff ) == 0x00ff00ff ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
in[i].baseline = y;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pix = (long *)pixels;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( y = 0; y < in[i].w * in[i].h; y++, pix++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( ( *pix & 0x00ffffff ) == 0x00ff00ff ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
*pix = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( in[i].baseline == -1 ) {
|
|
|
|
printf( "\nERROR : %s has no baseline\n", in[i].name );
|
2007-11-04 03:34:51 +00:00
|
|
|
nobaseline++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void CopyToMain32( Coords *coord ){
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
long *source;
|
|
|
|
long *dest;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
x = coord->x;
|
|
|
|
y = coord->y;
|
|
|
|
|
|
|
|
source = (long *)pixels;
|
2012-03-17 20:01:54 +00:00
|
|
|
dest = (long *)( outpixels + x + ( y * out.w ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( y = 0; y < coord->h; y++, dest += out.w - coord->w )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( x = 0; x < coord->w; x++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
*dest++ = *source++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void CreateMain(){
|
|
|
|
int i, count;
|
|
|
|
int width, height;
|
|
|
|
char name[128];
|
|
|
|
|
|
|
|
for ( i = 0, count = 0; i < filenum; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( in[i].depth ) {
|
|
|
|
printf( "\rProcessing %d of %d (%d missed, %d overlapping, %d nobase)\r", count + 1, valid, missed, overlap, nobaseline );
|
2007-11-04 03:34:51 +00:00
|
|
|
count++;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !FindCoords( &in[i] ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
missed++;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
else
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
strcpy( name, sourcedir );
|
|
|
|
strcat( name, in[i].name );
|
|
|
|
LoadAnyImage( name, &pixels, NULL, &width, &height );
|
|
|
|
CheckBaseline( i );
|
|
|
|
CheckOverlap( &in[i] );
|
|
|
|
CopyToMain32( &in[i] );
|
|
|
|
SetUsageMap( &in[i] );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void Cmd_TextureMix(){
|
|
|
|
miptex32_t *qtex32;
|
|
|
|
char filename[1024];
|
|
|
|
int size;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
InitVars();
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GetScriptToken( false );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
strcpy( root, token );
|
|
|
|
RemoveExt( root );
|
|
|
|
RemoveLeading( root );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
strcpy( filename, ExpandPathAndArchive( token ) );
|
|
|
|
if ( SetVars( filename ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
// Create combined texture
|
2012-03-17 20:01:54 +00:00
|
|
|
percent = ( ( TotalArea() * 100 ) / ( out.w * out.h ) );
|
|
|
|
printf( "Total area consumed : %d%%\n", percent );
|
|
|
|
printf( "Texture resolution : %dx%d pixels.\n", xcharsize, ycharsize );
|
2007-11-04 03:34:51 +00:00
|
|
|
CreateMain();
|
|
|
|
|
|
|
|
// Save image as m32
|
2012-03-17 20:01:54 +00:00
|
|
|
sprintf( filename, "%spics/misc/%s.m32", gamedir, out.name );
|
|
|
|
qtex32 = CreateMip32( (unsigned *)outpixels, out.w, out.h, &size, false );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
qtex32->contents = 0;
|
|
|
|
qtex32->value = 0;
|
|
|
|
qtex32->scale_x = 1.0;
|
|
|
|
qtex32->scale_y = 1.0;
|
2012-03-17 20:01:54 +00:00
|
|
|
sprintf( qtex32->name, "misc/%s", out.name );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "\n\nwriting %s\n", filename );
|
|
|
|
SaveFile( filename, (byte *)qtex32, size );
|
|
|
|
free( qtex32 );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// Save out script file
|
2012-03-17 20:01:54 +00:00
|
|
|
sprintf( filename, "%spics/misc/%s.fnt", gamedir, outscript );
|
|
|
|
printf( "Writing %s as script file\n", filename );
|
|
|
|
if ( !SaveScript( filename ) ) {
|
|
|
|
printf( "Unable to save output script.\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
printf( "Everythings groovy.\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
Cleanup();
|
|
|
|
}
|
|
|
|
|
|
|
|
// end
|