implement generic property value as a union instead of void*

This commit is contained in:
mniip 2014-01-15 02:15:11 +04:00 committed by jacob1
parent 8f2fbcd9e1
commit 69c9be0e99
6 changed files with 40 additions and 43 deletions

View File

@ -99,25 +99,21 @@ void PropertyWindow::SetProperty()
Client::Ref().SetPref("Prop.Type", property->GetOption().second);
Client::Ref().SetPref("Prop.Value", textField->GetText());
if(tool->propValue)
free(tool->propValue);
tool->propValue=malloc(sizeof(int));
int tempInt;
unsigned int tempUInt;
float tempFloat;
std::string value = textField->GetText();
try {
switch(properties[property->GetOption().second].Type)
{
case StructProperty::Integer:
case StructProperty::ParticleType:
{
int v;
if(value.length() > 2 && value.substr(0, 2) == "0x")
{
//0xC0FFEE
std::stringstream buffer;
buffer.exceptions(std::stringstream::failbit | std::stringstream::badbit);
buffer << std::hex << value.substr(2);
buffer >> tempInt;
buffer >> v;
}
else if(value.length() > 1 && value[0] == '#')
{
@ -125,7 +121,7 @@ void PropertyWindow::SetProperty()
std::stringstream buffer;
buffer.exceptions(std::stringstream::failbit | std::stringstream::badbit);
buffer << std::hex << value.substr(1);
buffer >> tempInt;
buffer >> v;
}
else
{
@ -137,15 +133,15 @@ void PropertyWindow::SetProperty()
#ifdef DEBUG
std::cout << "Got type from particle name" << std::endl;
#endif
tempInt = type;
v = type;
}
else
{
std::stringstream buffer(value);
buffer.exceptions(std::stringstream::failbit | std::stringstream::badbit);
buffer >> tempInt;
buffer >> v;
}
if (property->GetOption().first == "type" && (tempInt < 0 || tempInt >= PT_NUM || !sim->elements[tempInt].Enabled))
if (property->GetOption().first == "type" && (v < 0 || v >= PT_NUM || !sim->elements[v].Enabled))
{
new ErrorMessage("Could not set property", "Invalid Particle Type");
return;
@ -155,22 +151,25 @@ void PropertyWindow::SetProperty()
{
std::stringstream buffer(value);
buffer.exceptions(std::stringstream::failbit | std::stringstream::badbit);
buffer >> tempInt;
buffer >> v;
}
}
#ifdef DEBUG
std::cout << "Got int value " << tempInt << std::endl;
std::cout << "Got int value " << v << std::endl;
#endif
*(int *)tool->propValue = tempInt;
tool->propValue.Integer = v;
break;
}
case StructProperty::UInteger:
{
unsigned int v;
if(value.length() > 2 && value.substr(0, 2) == "0x")
{
//0xC0FFEE
std::stringstream buffer;
buffer.exceptions(std::stringstream::failbit | std::stringstream::badbit);
buffer << std::hex << value.substr(2);
buffer >> tempUInt;
buffer >> v;
}
else if(value.length() > 1 && value[0] == '#')
{
@ -178,28 +177,28 @@ void PropertyWindow::SetProperty()
std::stringstream buffer;
buffer.exceptions(std::stringstream::failbit | std::stringstream::badbit);
buffer << std::hex << value.substr(1);
buffer >> tempUInt;
buffer >> v;
}
else
{
std::stringstream buffer(value);
buffer.exceptions(std::stringstream::failbit | std::stringstream::badbit);
buffer >> tempUInt;
buffer >> v;
}
#ifdef DEBUG
std::cout << "Got uint value " << tempUInt << std::endl;
std::cout << "Got uint value " << v << std::endl;
#endif
*(unsigned int *)tool->propValue = tempUInt;
tool->propValue.UInteger = v;
break;
}
case StructProperty::Float:
{
std::stringstream buffer(value);
buffer.exceptions(std::stringstream::failbit | std::stringstream::badbit);
buffer >> tempFloat;
buffer >> tool->propValue.Float;
#ifdef DEBUG
std::cout << "Got float value " << tempFloat << std::endl;
std::cout << "Got float value " << tool->propValue.Float << std::endl;
#endif
*(float *)tool->propValue = tempFloat;
}
break;
default:
@ -243,8 +242,6 @@ void PropertyTool::OpenWindow(Simulation *sim)
void PropertyTool::SetProperty(Simulation *sim, ui::Point position)
{
if(!propValue)
return;
if(position.X<0 || position.X>XRES || position.Y<0 || position.Y>YRES)
return;
int i = sim->pmap[position.Y][position.X];
@ -255,14 +252,14 @@ void PropertyTool::SetProperty(Simulation *sim, ui::Point position)
switch (propType)
{
case StructProperty::Float:
*((float*)(((char*)&sim->parts[i>>8])+propOffset)) = *((float*)propValue);
*((float*)(((char*)&sim->parts[i>>8])+propOffset)) = propValue.Float;
break;
case StructProperty::ParticleType:
case StructProperty::Integer:
*((int*)(((char*)&sim->parts[i>>8])+propOffset)) = *((int*)propValue);
*((int*)(((char*)&sim->parts[i>>8])+propOffset)) = propValue.Integer;
break;
case StructProperty::UInteger:
*((unsigned int*)(((char*)&sim->parts[i>>8])+propOffset)) = *((unsigned int*)propValue);
*((unsigned int*)(((char*)&sim->parts[i>>8])+propOffset)) = propValue.UInteger;
break;
default:
break;

View File

@ -82,12 +82,11 @@ class PropertyTool: public Tool
{
public:
PropertyTool():
Tool(0, "PROP", "Property Edit. Click to alter the properties of elements in the field.", 0xfe, 0xa9, 0x00, "DEFAULT_UI_PROPERTY", NULL),
propValue(NULL)
Tool(0, "PROP", "Property Edit. Click to alter the properties of elements in the field.", 0xfe, 0xa9, 0x00, "DEFAULT_UI_PROPERTY", NULL)
{
}
StructProperty::PropertyType propType;
void *propValue;
PropertyValue propValue;
size_t propOffset;
void OpenWindow(Simulation *sim);

View File

@ -367,7 +367,7 @@ bool Simulation::FloodFillPmapCheck(int x, int y, int type)
return (pmap[y][x]&0xFF) == type;
}
int Simulation::flood_prop_2(int x, int y, size_t propoffset, void * propvalue, StructProperty::PropertyType proptype, int parttype, char * bitmap)
int Simulation::flood_prop_2(int x, int y, size_t propoffset, PropertyValue propvalue, StructProperty::PropertyType proptype, int parttype, char * bitmap)
{
int x1, x2, i, dy = 1;
x1 = x2 = x;
@ -396,16 +396,16 @@ int Simulation::flood_prop_2(int x, int y, size_t propoffset, void * propvalue,
continue;
switch (proptype) {
case StructProperty::Float:
*((float*)(((char*)&parts[i>>8])+propoffset)) = *((float*)propvalue);
*((float*)(((char*)&parts[i>>8])+propoffset)) = propvalue.Float;
break;
case StructProperty::ParticleType:
case StructProperty::Integer:
*((int*)(((char*)&parts[i>>8])+propoffset)) = *((int*)propvalue);
*((int*)(((char*)&parts[i>>8])+propoffset)) = propvalue.Integer;
break;
case StructProperty::UInteger:
*((unsigned int*)(((char*)&parts[i>>8])+propoffset)) = *((unsigned int*)propvalue);
*((unsigned int*)(((char*)&parts[i>>8])+propoffset)) = propvalue.UInteger;
break;
default:
@ -426,7 +426,7 @@ int Simulation::flood_prop_2(int x, int y, size_t propoffset, void * propvalue,
return 1;
}
int Simulation::flood_prop(int x, int y, size_t propoffset, void * propvalue, StructProperty::PropertyType proptype)
int Simulation::flood_prop(int x, int y, size_t propoffset, PropertyValue propvalue, StructProperty::PropertyType proptype)
{
int r = 0;
char * bitmap = (char *)malloc(XRES*YRES); //Bitmap for checking

View File

@ -72,10 +72,6 @@ public:
int portal_rx[8];
int portal_ry[8];
int wireless[CHANNELS][2];
//PROP tool property to draw (TODO)
//void *prop_value;
//StructProperty::PropertyType proptype;
//size_t prop_offset;
//Gol sim
int CGOL;
int GSPEED;
@ -138,8 +134,8 @@ public:
void create_gain_photon(int pp);
void kill_part(int i);
bool FloodFillPmapCheck(int x, int y, int type);
int flood_prop(int x, int y, size_t propoffset, void * propvalue, StructProperty::PropertyType proptype);
int flood_prop_2(int x, int y, size_t propoffset, void * propvalue, StructProperty::PropertyType proptype, int parttype, char * bitmap);
int flood_prop(int x, int y, size_t propoffset, PropertyValue propvalue, StructProperty::PropertyType proptype);
int flood_prop_2(int x, int y, size_t propoffset, PropertyValue propvalue, StructProperty::PropertyType proptype, int parttype, char * bitmap);
int flood_water(int x, int y, int i, int originaly, int check);
int FloodINST(int x, int y, int fullc, int cm);
void detach(int i);

View File

@ -28,4 +28,10 @@ struct StructProperty
}
};
union PropertyValue {
int Integer;
unsigned int UInteger;
float Float;
};
#endif

View File

@ -73,8 +73,7 @@ int Element_BANG::update(UPDATE_FUNC_ARGS)
{
if ((pmap[y][x]>>8 == i))
{
int tempvalue = 2;
sim->flood_prop(x, y, offsetof(Particle, tmp), &tempvalue, StructProperty::Integer);
sim->flood_prop(x, y, offsetof(Particle, tmp), (PropertyValue){.Integer = 2}, StructProperty::Integer);
}
parts[i].tmp = 2;
}