Skip to content

Commit

Permalink
Fix some problems noted in http://www.viva64.com/en/b/0298/
Browse files Browse the repository at this point in the history
I've left some of the less important items, like SearchView.cpp "'then' statement is equivalent to the 'else' statement", and RequestBroker::Request::~Request, because I don't feel like spending a few days entirely rewriting those files at the moment (which is what I'd end up doing if I started fixing minor problems and refactoring...)

GameSave::readOPS - not changed. At some point we may have to move to a larger type for element IDs (probably two or four bytes), but PT_NUM isn't likely to be raised to the maximum value of that type immediately, so this check will be needed then. There should be an elements[partsData[i]].Enabled check in there too, but it might be a bit difficult - I'm not sure how to access a Simulation object from GameSave::readOPS...

Notes on changes:

Graphics::textsize, Element_FRZW::Element_FRZW - typos

Button::Draw - the extra case was originally used to invert the icon (draw it in black instead of in white) when the button was clicked. However, the icon colour is now automatically set depending on the background colour. (Note similar conditions "if(Enabled) { if(isButtonDown || (isTogglable && toggle)) " near the start of the function - same logic but in a different place, setting icon colour indirectly).

Simulation::transform_save - unused redundant function, everything uses GameSave::Transform which does much the same thing.

PreviewView::NotifySaveChanged - should be height==YRES/2, it's checking whether the preview image is the correct size, and resizing it if it isn't.

Element_FWRK::update - no idea why that line was there, even though it was my commit that originally added it...
  • Loading branch information
jacksonmj committed Dec 25, 2014
1 parent 3ea2d80 commit 236ff08
Show file tree
Hide file tree
Showing 15 changed files with 37 additions and 203 deletions.
6 changes: 3 additions & 3 deletions src/PowderToySDL.cpp
Expand Up @@ -321,7 +321,7 @@ int SDLOpen()
SDL_SysWMinfo SysInfo;
SDL_VERSION(&SysInfo.version);
if(SDL_GetWMInfo(&SysInfo) <= 0) {
printf("%s : %d\n", SDL_GetError(), SysInfo.window);
printf("%s : %p\n", SDL_GetError(), SysInfo.window);
exit(-1);
}
HWND WindowHandle = SysInfo.window;
Expand Down Expand Up @@ -968,10 +968,10 @@ int main(int argc, char * argv[])
throw std::runtime_error("Invalid Save ID");

SaveInfo * newSave = Client::Ref().GetSave(saveId, 0);
GameSave * newGameSave = new GameSave(Client::Ref().GetSaveData(saveId, 0));
newSave->SetGameSave(newGameSave);
if(!newSave)
throw std::runtime_error("Could not load save");
GameSave * newGameSave = new GameSave(Client::Ref().GetSaveData(saveId, 0));
newSave->SetGameSave(newGameSave);

gameController->LoadSave(newSave);
delete newSave;
Expand Down
2 changes: 1 addition & 1 deletion src/client/requestbroker/APIRequest.cpp
Expand Up @@ -103,7 +103,7 @@ RequestBroker::ProcessResponse APIRequest::Process(RequestBroker & rb)
std::strcpy(userName, format::NumberToString<int>(user.ID).c_str());
std::strcpy(userSession, user.SessionID.c_str());
HTTPContext = http_multipart_post_async((char*)URL.c_str(), postNames, postData, postLength, userName, NULL, userSession);
delete userSession;
delete[] userSession;
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/client/requestbroker/WebRequest.cpp
Expand Up @@ -103,7 +103,7 @@ RequestBroker::ProcessResponse WebRequest::Process(RequestBroker & rb)
std::strcpy(userName, format::NumberToString<int>(user.ID).c_str());
std::strcpy(userSession, user.SessionID.c_str());
HTTPContext = http_multipart_post_async((char*)URL.c_str(), postNames, postData, postLength, userName, NULL, userSession);
delete userSession;
delete[] userSession;
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/graphics/Graphics.cpp
Expand Up @@ -826,7 +826,7 @@ void Graphics::textsize(const char * s, int & width, int & height)
}
else if (*s == '\x0F')
{
if(!s[1] || !s[2] || !s[1]) break;
if(!s[1] || !s[2] || !s[3]) break;
s+=3;
}
else if (*s == '\b')
Expand Down
33 changes: 9 additions & 24 deletions src/gui/game/GameController.cpp
Expand Up @@ -1069,20 +1069,15 @@ void GameController::OpenLocalSaveWindow(bool asCurrent)
{
Simulation * sim = gameModel->GetSimulation();
GameSave * gameSave = sim->Save();
gameSave->paused = gameModel->GetPaused();
gameSave->gravityMode = sim->gravityMode;
gameSave->airMode = sim->air->airMode;
gameSave->edgeMode = sim->edgeMode;
gameSave->legacyEnable = sim->legacy_enable;
gameSave->waterEEnabled = sim->water_equal_test;
gameSave->gravityEnable = sim->grav->ngrav_enable;
gameSave->aheatEnable = sim->aheat_enable;
if(!gameSave)
{
new ErrorMessage("Error", "Unable to build save.");
}
else
{
sim->SaveSimOptions(gameSave);
gameSave->paused = gameModel->GetPaused();

std::string filename = "";
if (gameModel->GetSaveFile())
filename = gameModel->GetSaveFile()->GetDisplayName();
Expand Down Expand Up @@ -1278,20 +1273,15 @@ void GameController::OpenSaveWindow()
{
Simulation * sim = gameModel->GetSimulation();
GameSave * gameSave = sim->Save();
gameSave->paused = gameModel->GetPaused();
gameSave->gravityMode = sim->gravityMode;
gameSave->airMode = sim->air->airMode;
gameSave->edgeMode = sim->edgeMode;
gameSave->legacyEnable = sim->legacy_enable;
gameSave->waterEEnabled = sim->water_equal_test;
gameSave->gravityEnable = sim->grav->ngrav_enable;
gameSave->aheatEnable = sim->aheat_enable;
if(!gameSave)
{
new ErrorMessage("Error", "Unable to build save.");
}
else
{
sim->SaveSimOptions(gameSave);
gameSave->paused = gameModel->GetPaused();

if(gameModel->GetSave())
{
SaveInfo tempSave(*gameModel->GetSave());
Expand Down Expand Up @@ -1331,20 +1321,15 @@ void GameController::SaveAsCurrent()
{
Simulation * sim = gameModel->GetSimulation();
GameSave * gameSave = sim->Save();
gameSave->paused = gameModel->GetPaused();
gameSave->gravityMode = sim->gravityMode;
gameSave->airMode = sim->air->airMode;
gameSave->edgeMode = sim->edgeMode;
gameSave->legacyEnable = sim->legacy_enable;
gameSave->waterEEnabled = sim->water_equal_test;
gameSave->gravityEnable = sim->grav->ngrav_enable;
gameSave->aheatEnable = sim->aheat_enable;
if(!gameSave)
{
new ErrorMessage("Error", "Unable to build save.");
}
else
{
gameSave->paused = gameModel->GetPaused();
sim->SaveSimOptions(gameSave);

if(gameModel->GetSave())
{
SaveInfo tempSave(*gameModel->GetSave());
Expand Down
9 changes: 1 addition & 8 deletions src/gui/interface/Button.cpp
Expand Up @@ -134,14 +134,7 @@ void Button::Draw(const Point& screenPos)
if(Appearance.icon)
{
if(Enabled)
if(isButtonDown || (isTogglable && toggle))
{
g->draw_icon(Position.X+iconPosition.X, Position.Y+iconPosition.Y, Appearance.icon, 255, iconInvert);
}
else
{
g->draw_icon(Position.X+iconPosition.X, Position.Y+iconPosition.Y, Appearance.icon, 255, iconInvert);
}
g->draw_icon(Position.X+iconPosition.X, Position.Y+iconPosition.Y, Appearance.icon, 255, iconInvert);
else
g->draw_icon(Position.X+iconPosition.X, Position.Y+iconPosition.Y, Appearance.icon, 180, iconInvert);
}
Expand Down
2 changes: 1 addition & 1 deletion src/gui/interface/Engine.cpp
Expand Up @@ -217,7 +217,7 @@ void Engine::Tick()

void Engine::Draw()
{
if(lastBuffer && !(state_->Position.X == 0 && state_->Position.Y == 0 && state_->Size.X == width_ && state_->Size.Y == height_))
if(lastBuffer && !(state_ && state_->Position.X == 0 && state_->Position.Y == 0 && state_->Size.X == width_ && state_->Size.Y == height_))
{
g->Acquire();
g->Clear();
Expand Down
2 changes: 1 addition & 1 deletion src/gui/options/OptionsView.cpp
Expand Up @@ -226,7 +226,7 @@ OptionsView::OptionsView():
char* workingDirectory = new char[FILENAME_MAX+strlen(openCommand)];
sprintf(workingDirectory, "%s\"%s\"", openCommand, getcwd(NULL, 0));
system(workingDirectory);
delete workingDirectory;
delete[] workingDirectory;
}
};
ui::Button * dataFolderButton = new ui::Button(ui::Point(8, Size.Y-38), ui::Point(90, 16), "Open Data Folder");
Expand Down
2 changes: 1 addition & 1 deletion src/gui/preview/PreviewView.cpp
Expand Up @@ -446,7 +446,7 @@ void PreviewView::NotifySaveChanged(PreviewModel * sender)
{
savePreview = SaveRenderer::Ref().Render(save->GetGameSave(), false, true);

if(savePreview && savePreview->Buffer && !(savePreview->Width == XRES/2 && savePreview->Width == YRES/2))
if(savePreview && savePreview->Buffer && !(savePreview->Width == XRES/2 && savePreview->Height == YRES/2))
{
pixel * oldData = savePreview->Buffer;
float factorX = ((float)XRES/2)/((float)savePreview->Width);
Expand Down
13 changes: 1 addition & 12 deletions src/lua/LuaScriptInterface.cpp
Expand Up @@ -2766,18 +2766,7 @@ int LuaScriptInterface::fileSystem_exists(lua_State * l)
if(stat(filename, &s) == 0)
#endif
{
if(s.st_mode & S_IFDIR)
{
exists = true;
}
else if(s.st_mode & S_IFREG)
{
exists = true;
}
else
{
exists = true;
}
exists = true;
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/simulation/SaveRenderer.cpp
Expand Up @@ -35,7 +35,7 @@ SaveRenderer::SaveRenderer(){
VideoBuffer * SaveRenderer::Render(GameSave * save, bool decorations, bool fire)
{
int width, height;
VideoBuffer * tempThumb;
VideoBuffer * tempThumb = NULL;
width = save->blockWidth;
height = save->blockHeight;
bool doCollapse = save->Collapsed();
Expand Down
160 changes: 14 additions & 146 deletions src/simulation/Simulation.cpp
Expand Up @@ -296,6 +296,19 @@ GameSave * Simulation::Save(int fullX, int fullY, int fullX2, int fullY2)
return newSave;
}

void Simulation::SaveSimOptions(GameSave * gameSave)
{
if (!gameSave)
return;
gameSave->gravityMode = gravityMode;
gameSave->airMode = air->airMode;
gameSave->edgeMode = edgeMode;
gameSave->legacyEnable = legacy_enable;
gameSave->waterEEnabled = water_equal_test;
gameSave->gravityEnable = grav->ngrav_enable;
gameSave->aheatEnable = aheat_enable;
}

Snapshot * Simulation::CreateSnapshot()
{
Snapshot * snap = new Snapshot();
Expand Down Expand Up @@ -1550,151 +1563,6 @@ int Simulation::FloodParts(int x, int y, int fullc, int cm, int flags)
return created_something;
}

void *Simulation::transform_save(void *odata, int *size, matrix2d transform, vector2d translate)
{
void *ndata;
unsigned char (*bmapo)[XRES/CELL] = (unsigned char (*)[XRES/CELL])calloc((YRES/CELL)*(XRES/CELL), sizeof(unsigned char));
unsigned char (*bmapn)[XRES/CELL] = (unsigned char (*)[XRES/CELL])calloc((YRES/CELL)*(XRES/CELL), sizeof(unsigned char));
Particle *partst = (Particle*)calloc(sizeof(Particle), NPART);
sign *signst = (sign*)calloc(MAXSIGNS, sizeof(sign));
unsigned (*pmapt)[XRES] = (unsigned (*)[XRES])calloc(YRES*XRES, sizeof(unsigned));
float (*fvxo)[XRES/CELL] = (float (*)[XRES/CELL])calloc((YRES/CELL)*(XRES/CELL), sizeof(float));
float (*fvyo)[XRES/CELL] = (float (*)[XRES/CELL])calloc((YRES/CELL)*(XRES/CELL), sizeof(float));
float (*fvxn)[XRES/CELL] = (float (*)[XRES/CELL])calloc((YRES/CELL)*(XRES/CELL), sizeof(float));
float (*fvyn)[XRES/CELL] = (float (*)[XRES/CELL])calloc((YRES/CELL)*(XRES/CELL), sizeof(float));
float (*vxo)[XRES/CELL] = (float (*)[XRES/CELL])calloc((YRES/CELL)*(XRES/CELL), sizeof(float));
float (*vyo)[XRES/CELL] = (float (*)[XRES/CELL])calloc((YRES/CELL)*(XRES/CELL), sizeof(float));
float (*vxn)[XRES/CELL] = (float (*)[XRES/CELL])calloc((YRES/CELL)*(XRES/CELL), sizeof(float));
float (*vyn)[XRES/CELL] = (float (*)[XRES/CELL])calloc((YRES/CELL)*(XRES/CELL), sizeof(float));
float (*pvo)[XRES/CELL] = (float (*)[XRES/CELL])calloc((YRES/CELL)*(XRES/CELL), sizeof(float));
float (*pvn)[XRES/CELL] = (float (*)[XRES/CELL])calloc((YRES/CELL)*(XRES/CELL), sizeof(float));
int i, x, y, nx, ny, w, h, nw, nh;
vector2d pos, tmp, ctl, cbr;
vector2d vel;
vector2d cornerso[4];
unsigned char *odatac = (unsigned char *)odata;
//if (parse_save(odata, *size, 0, 0, 0, bmapo, vxo, vyo, pvo, fvxo, fvyo, signst, partst, pmapt)) //TODO: Implement
{
free(bmapo);
free(bmapn);
free(partst);
free(signst);
free(pmapt);
free(fvxo);
free(fvyo);
free(fvxn);
free(fvyn);
free(vxo);
free(vyo);
free(vxn);
free(vyn);
free(pvo);
free(pvn);
return odata;
}
w = odatac[6]*CELL;
h = odatac[7]*CELL;
// undo any translation caused by rotation
cornerso[0] = v2d_new(0,0);
cornerso[1] = v2d_new(w-1,0);
cornerso[2] = v2d_new(0,h-1);
cornerso[3] = v2d_new(w-1,h-1);
for (i=0; i<4; i++)
{
tmp = m2d_multiply_v2d(transform,cornerso[i]);
if (i==0) ctl = cbr = tmp; // top left, bottom right corner
if (tmp.x<ctl.x) ctl.x = tmp.x;
if (tmp.y<ctl.y) ctl.y = tmp.y;
if (tmp.x>cbr.x) cbr.x = tmp.x;
if (tmp.y>cbr.y) cbr.y = tmp.y;
}
// casting as int doesn't quite do what we want with negative numbers, so use floor()
tmp = v2d_new(floor(ctl.x+0.5f),floor(ctl.y+0.5f));
translate = v2d_sub(translate,tmp);
nw = floor(cbr.x+0.5f)-floor(ctl.x+0.5f)+1;
nh = floor(cbr.y+0.5f)-floor(ctl.y+0.5f)+1;
if (nw>XRES) nw = XRES;
if (nh>YRES) nh = YRES;
// rotate and translate signs, parts, walls
for (i=0; i<MAXSIGNS; i++)
{
if (!signst[i].text[0]) continue;
pos = v2d_new(signst[i].x, signst[i].y);
pos = v2d_add(m2d_multiply_v2d(transform,pos),translate);
nx = floor(pos.x+0.5f);
ny = floor(pos.y+0.5f);
if (nx<0 || nx>=nw || ny<0 || ny>=nh)
{
signst[i].text[0] = 0;
continue;
}
signst[i].x = nx;
signst[i].y = ny;
}
for (i=0; i<NPART; i++)
{
if (!partst[i].type) continue;
pos = v2d_new(partst[i].x, partst[i].y);
pos = v2d_add(m2d_multiply_v2d(transform,pos),translate);
nx = floor(pos.x+0.5f);
ny = floor(pos.y+0.5f);
if (nx<0 || nx>=nw || ny<0 || ny>=nh)
{
partst[i].type = PT_NONE;
continue;
}
partst[i].x = nx;
partst[i].y = ny;
vel = v2d_new(partst[i].vx, partst[i].vy);
vel = m2d_multiply_v2d(transform, vel);
partst[i].vx = vel.x;
partst[i].vy = vel.y;
}
for (y=0; y<YRES/CELL; y++)
for (x=0; x<XRES/CELL; x++)
{
pos = v2d_new(x*CELL+CELL*0.4f, y*CELL+CELL*0.4f);
pos = v2d_add(m2d_multiply_v2d(transform,pos),translate);
nx = pos.x/CELL;
ny = pos.y/CELL;
if (nx<0 || nx>=nw/CELL || ny<0 || ny>=nh/CELL)
continue;
if (bmapo[y][x])
{
bmapn[ny][nx] = bmapo[y][x];
if (bmapo[y][x]==WL_FAN)
{
vel = v2d_new(fvxo[y][x], fvyo[y][x]);
vel = m2d_multiply_v2d(transform, vel);
fvxn[ny][nx] = vel.x;
fvyn[ny][nx] = vel.y;
}
}
vel = v2d_new(vxo[y][x], vyo[y][x]);
vel = m2d_multiply_v2d(transform, vel);
vxn[ny][nx] = vel.x;
vyn[ny][nx] = vel.y;
pvn[ny][nx] = pvo[y][x];
}
//ndata = build_save(size,0,0,nw,nh,bmapn,vxn,vyn,pvn,fvxn,fvyn,signst,partst); //TODO: IMPLEMENT
free(bmapo);
free(bmapn);
free(partst);
free(signst);
free(pmapt);
free(fvxo);
free(fvyo);
free(fvxn);
free(fvyn);
free(vxo);
free(vyo);
free(vxn);
free(vyn);
free(pvo);
free(pvn);
return ndata;
}

void Simulation::orbitalparts_get(int block1, int block2, int resblock1[], int resblock2[])
{
resblock1[0] = (block1&0x000000FF);
Expand Down Expand Up @@ -4905,7 +4773,7 @@ void Simulation::update_particles()//doesn't update the particles themselves, bu
emp_decor = 0;
}
sandcolour = (int)(20.0f*sin((float)sandcolour_frame*(M_PI/180.0f)));
sandcolour_frame = (sandcolour_frame++)%360;
sandcolour_frame = (sandcolour_frame+1)%360;

memset(pmap, 0, sizeof(pmap));
memset(pmap_count, 0, sizeof(pmap_count));
Expand Down
2 changes: 1 addition & 1 deletion src/simulation/Simulation.h
Expand Up @@ -114,6 +114,7 @@ class Simulation
int Load(int x, int y, GameSave * save);
GameSave * Save();
GameSave * Save(int x1, int y1, int x2, int y2);
void SaveSimOptions(GameSave * gameSave);
SimulationSample GetSample(int x, int y);

Snapshot * CreateSnapshot();
Expand Down Expand Up @@ -189,7 +190,6 @@ class Simulation

int GetParticleType(std::string type);

void *transform_save(void *odata, int *size, matrix2d transform, vector2d translate);
void orbitalparts_get(int block1, int block2, int resblock1[], int resblock2[]);
void orbitalparts_set(int *block1, int *block2, int resblock1[], int resblock2[]);
int get_wavelength_bin(int *wm);
Expand Down
2 changes: 1 addition & 1 deletion src/simulation/elements/FRZW.cpp
Expand Up @@ -31,7 +31,7 @@ Element_FRZW::Element_FRZW()
Description = "Freeze water. Hybrid liquid formed when Freeze powder melts.";

State = ST_LIQUID;
Properties = TYPE_LIQUID||PROP_LIFE_DEC;
Properties = TYPE_LIQUID | PROP_LIFE_DEC;

LowPressure = IPL;
LowPressureTransition = NT;
Expand Down

0 comments on commit 236ff08

Please sign in to comment.