2007-11-25 19:42:40 +00:00
|
|
|
/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */
|
2007-11-18 12:03:59 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
2008-03-17 01:41:11 +00:00
|
|
|
#include <math.h>
|
2008-08-14 17:19:13 +00:00
|
|
|
|
|
|
|
#include <base/system.h>
|
|
|
|
#include <base/math.hpp>
|
|
|
|
|
2008-01-19 10:57:25 +00:00
|
|
|
#include <engine/e_client_interface.h>
|
2008-08-27 15:48:50 +00:00
|
|
|
#include "skins.hpp"
|
2007-11-18 12:03:59 +00:00
|
|
|
|
2008-08-27 15:48:50 +00:00
|
|
|
SKINS::SKINS()
|
2007-11-18 12:03:59 +00:00
|
|
|
{
|
2008-08-27 15:48:50 +00:00
|
|
|
num_skins = 0;
|
|
|
|
}
|
2007-11-18 12:03:59 +00:00
|
|
|
|
2008-08-27 15:48:50 +00:00
|
|
|
void SKINS::skinscan(const char *name, int is_dir, void *user)
|
2007-11-18 12:03:59 +00:00
|
|
|
{
|
2008-08-27 15:48:50 +00:00
|
|
|
SKINS *self = (SKINS *)user;
|
2007-11-18 12:03:59 +00:00
|
|
|
int l = strlen(name);
|
2008-08-27 15:48:50 +00:00
|
|
|
if(l < 4 || is_dir || self->num_skins == MAX_SKINS)
|
2007-11-18 12:03:59 +00:00
|
|
|
return;
|
|
|
|
if(strcmp(name+l-4, ".png") != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
char buf[512];
|
2008-02-11 21:49:26 +00:00
|
|
|
str_format(buf, sizeof(buf), "data/skins/%s", name);
|
2007-11-18 12:03:59 +00:00
|
|
|
IMAGE_INFO info;
|
|
|
|
if(!gfx_load_png(&info, buf))
|
|
|
|
{
|
|
|
|
dbg_msg("game", "failed to load skin from %s", name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-08-27 15:48:50 +00:00
|
|
|
self->skins[self->num_skins].org_texture = gfx_load_texture_raw(info.width, info.height, info.format, info.data, info.format, 0);
|
2007-11-18 12:03:59 +00:00
|
|
|
|
2008-03-17 01:41:11 +00:00
|
|
|
int body_size = 96; // body size
|
2007-11-18 12:03:59 +00:00
|
|
|
unsigned char *d = (unsigned char *)info.data;
|
2008-03-17 01:41:11 +00:00
|
|
|
int pitch = info.width*4;
|
|
|
|
|
|
|
|
// dig out blood color
|
|
|
|
{
|
|
|
|
int colors[3] = {0};
|
|
|
|
for(int y = 0; y < body_size; y++)
|
|
|
|
for(int x = 0; x < body_size; x++)
|
|
|
|
{
|
|
|
|
if(d[y*pitch+x*4+3] > 128)
|
|
|
|
{
|
|
|
|
colors[0] += d[y*pitch+x*4+0];
|
|
|
|
colors[1] += d[y*pitch+x*4+1];
|
|
|
|
colors[2] += d[y*pitch+x*4+2];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-27 15:48:50 +00:00
|
|
|
self->skins[self->num_skins].blood_color = normalize(vec3(colors[0], colors[1], colors[2]));
|
2008-03-17 01:41:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// create colorless version
|
2007-11-18 12:03:59 +00:00
|
|
|
int step = info.format == IMG_RGBA ? 4 : 3;
|
2008-03-23 12:36:24 +00:00
|
|
|
|
|
|
|
// make the texture gray scale
|
2007-11-18 12:03:59 +00:00
|
|
|
for(int i = 0; i < info.width*info.height; i++)
|
|
|
|
{
|
|
|
|
int v = (d[i*step]+d[i*step+1]+d[i*step+2])/3;
|
|
|
|
d[i*step] = v;
|
|
|
|
d[i*step+1] = v;
|
|
|
|
d[i*step+2] = v;
|
|
|
|
}
|
2008-03-17 01:41:11 +00:00
|
|
|
|
2007-11-18 12:03:59 +00:00
|
|
|
|
2007-11-26 22:26:33 +00:00
|
|
|
if(1)
|
|
|
|
{
|
|
|
|
int freq[256] = {0};
|
|
|
|
int org_weight = 0;
|
|
|
|
int new_weight = 192;
|
2008-03-23 12:36:24 +00:00
|
|
|
|
|
|
|
// find most common frequence
|
|
|
|
for(int y = 0; y < body_size; y++)
|
|
|
|
for(int x = 0; x < body_size; x++)
|
|
|
|
{
|
|
|
|
if(d[y*pitch+x*4+3] > 128)
|
|
|
|
freq[d[y*pitch+x*4]]++;
|
|
|
|
}
|
|
|
|
|
2007-11-26 22:26:33 +00:00
|
|
|
for(int i = 1; i < 256; i++)
|
|
|
|
{
|
|
|
|
if(freq[org_weight] < freq[i])
|
|
|
|
org_weight = i;
|
|
|
|
}
|
|
|
|
|
2008-03-23 12:36:24 +00:00
|
|
|
// reorder
|
2007-11-26 22:26:33 +00:00
|
|
|
int inv_org_weight = 255-org_weight;
|
|
|
|
int inv_new_weight = 255-new_weight;
|
2008-03-17 01:41:11 +00:00
|
|
|
for(int y = 0; y < body_size; y++)
|
|
|
|
for(int x = 0; x < body_size; x++)
|
2007-11-26 22:26:33 +00:00
|
|
|
{
|
|
|
|
int v = d[y*pitch+x*4];
|
|
|
|
if(v <= org_weight)
|
|
|
|
v = (int)(((v/(float)org_weight) * new_weight));
|
|
|
|
else
|
|
|
|
v = (int)(((v-org_weight)/(float)inv_org_weight)*inv_new_weight + new_weight);
|
|
|
|
d[y*pitch+x*4] = v;
|
|
|
|
d[y*pitch+x*4+1] = v;
|
|
|
|
d[y*pitch+x*4+2] = v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-08-27 15:48:50 +00:00
|
|
|
self->skins[self->num_skins].color_texture = gfx_load_texture_raw(info.width, info.height, info.format, info.data, info.format, 0);
|
2007-11-18 12:03:59 +00:00
|
|
|
mem_free(info.data);
|
|
|
|
|
|
|
|
// set skin data
|
2008-08-27 15:48:50 +00:00
|
|
|
strncpy(self->skins[self->num_skins].name, name, min((int)sizeof(self->skins[self->num_skins].name),l-4));
|
|
|
|
dbg_msg("game", "load skin %s", self->skins[self->num_skins].name);
|
|
|
|
self->num_skins++;
|
2007-11-18 12:03:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-08-27 15:48:50 +00:00
|
|
|
void SKINS::init()
|
2007-11-18 12:03:59 +00:00
|
|
|
{
|
|
|
|
// load skins
|
2008-08-27 15:48:50 +00:00
|
|
|
num_skins = 0;
|
|
|
|
fs_listdir("data/skins", skinscan, this);
|
2007-11-18 12:03:59 +00:00
|
|
|
}
|
|
|
|
|
2008-08-27 15:48:50 +00:00
|
|
|
int SKINS::num()
|
2007-11-18 12:03:59 +00:00
|
|
|
{
|
|
|
|
return num_skins;
|
|
|
|
}
|
|
|
|
|
2008-08-27 15:48:50 +00:00
|
|
|
const SKINS::SKIN *SKINS::get(int index)
|
2007-11-18 12:03:59 +00:00
|
|
|
{
|
|
|
|
return &skins[index%num_skins];
|
|
|
|
}
|
|
|
|
|
2008-08-27 15:48:50 +00:00
|
|
|
int SKINS::find(const char *name)
|
2007-11-18 12:03:59 +00:00
|
|
|
{
|
|
|
|
for(int i = 0; i < num_skins; i++)
|
|
|
|
{
|
|
|
|
if(strcmp(skins[i].name, name) == 0)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-11-18 14:24:34 +00:00
|
|
|
// these converter functions were nicked from some random internet pages
|
|
|
|
static float hue_to_rgb(float v1, float v2, float h)
|
|
|
|
{
|
|
|
|
if(h < 0) h += 1;
|
|
|
|
if(h > 1) h -= 1;
|
|
|
|
if((6 * h) < 1) return v1 + ( v2 - v1 ) * 6 * h;
|
|
|
|
if((2 * h) < 1) return v2;
|
|
|
|
if((3 * h) < 2) return v1 + ( v2 - v1 ) * ((2.0f/3.0f) - h) * 6;
|
|
|
|
return v1;
|
|
|
|
}
|
|
|
|
|
2008-08-27 15:48:50 +00:00
|
|
|
static vec3 hsl_to_rgb(vec3 in)
|
2007-11-18 14:24:34 +00:00
|
|
|
{
|
|
|
|
float v1, v2;
|
|
|
|
vec3 out;
|
|
|
|
|
|
|
|
if(in.s == 0)
|
|
|
|
{
|
|
|
|
out.r = in.l;
|
|
|
|
out.g = in.l;
|
|
|
|
out.b = in.l;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(in.l < 0.5f)
|
|
|
|
v2 = in.l * (1 + in.s);
|
|
|
|
else
|
|
|
|
v2 = (in.l+in.s) - (in.s*in.l);
|
|
|
|
|
|
|
|
v1 = 2 * in.l - v2;
|
|
|
|
|
|
|
|
out.r = hue_to_rgb(v1, v2, in.h + (1.0f/3.0f));
|
|
|
|
out.g = hue_to_rgb(v1, v2, in.h);
|
|
|
|
out.b = hue_to_rgb(v1, v2, in.h - (1.0f/3.0f));
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2008-08-27 15:48:50 +00:00
|
|
|
vec4 SKINS::get_color(int v)
|
2007-11-18 14:24:34 +00:00
|
|
|
{
|
|
|
|
vec3 r = hsl_to_rgb(vec3((v>>16)/255.0f, ((v>>8)&0xff)/255.0f, 0.5f+(v&0xff)/255.0f*0.5f));
|
|
|
|
return vec4(r.r, r.g, r.b, 1.0f);
|
|
|
|
}
|