Skip to content

Commit

Permalink
use std::string instead of c strings for sign stuff, fixes #545
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob1 committed Mar 4, 2018
1 parent 883484e commit f9b5c6b
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 60 deletions.
2 changes: 1 addition & 1 deletion src/graphics/Renderer.cpp
Expand Up @@ -984,7 +984,7 @@ void Renderer::DrawSigns()
{
char type = 0;
std::string text = signs[i].getText(sim);
sign::splitsign(signs[i].text.c_str(), &type);
sign::splitsign(signs[i].text, &type);
signs[i].pos(text, x, y, w, h);
clearrect(x, y, w+1, h);
drawrect(x, y, w+1, h, 192, 192, 192, 255);
Expand Down
14 changes: 6 additions & 8 deletions src/gui/game/GameController.cpp
Expand Up @@ -631,7 +631,7 @@ bool GameController::MouseDown(int x, int y, unsigned button)
if (foundSignID != -1)
{
sign foundSign = gameModel->GetSimulation()->signs[foundSignID];
if (sign::splitsign(foundSign.text.c_str()))
if (sign::splitsign(foundSign.text))
return false;
}
}
Expand All @@ -655,22 +655,20 @@ bool GameController::MouseUp(int x, int y, unsigned button, char type)
if (foundSignID != -1)
{
sign foundSign = gameModel->GetSimulation()->signs[foundSignID];
const char* str = foundSign.text.c_str();
std::string str = foundSign.text;
char type;
int pos = sign::splitsign(str, &type);
if (pos)
{
ret = false;
if (type == 'c' || type == 't' || type == 's')
{
char buff[256];
strcpy(buff, str+3);
buff[pos-3] = 0;
std::string link = str.substr(3, pos-3);
switch (type)
{
case 'c':
{
int saveID = format::StringToNumber<int>(std::string(buff));
int saveID = format::StringToNumber<int>(link);
if (saveID)
OpenSavePreview(saveID, 0, false);
break;
Expand All @@ -679,12 +677,12 @@ bool GameController::MouseUp(int x, int y, unsigned button, char type)
{
// buff is already confirmed to be a number by sign::splitsign
std::stringstream uri;
uri << "http://powdertoy.co.uk/Discussions/Thread/View.html?Thread=" << buff;
uri << "http://powdertoy.co.uk/Discussions/Thread/View.html?Thread=" << link;
Platform::OpenURI(uri.str());
break;
}
case 's':
OpenSearch(buff);
OpenSearch(link);
break;
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/gui/game/GameView.cpp
Expand Up @@ -1744,25 +1744,23 @@ void GameView::OnTick(float dt)
int foundSignID = c->GetSignAt(mousePosition.X, mousePosition.Y);
if (foundSignID != -1)
{
const char* str = c->GetSignText(foundSignID).c_str();;
std::string str = c->GetSignText(foundSignID);
char type = '\0';
int pos = sign::splitsign(str, &type);
if (type == 'c' || type == 't' || type == 's')
{
char buff[256];
strcpy(buff, str+3);
buff[pos-3] = 0;
std::string linkSign = str.substr(3, pos-3);
std::stringstream tooltip;
switch (type)
{
case 'c':
tooltip << "Go to save ID:" << buff;
tooltip << "Go to save ID:" << linkSign;
break;
case 't':
tooltip << "Open forum thread " << buff << " in browser";
tooltip << "Open forum thread " << linkSign << " in browser";
break;
case 's':
tooltip << "Search for " << buff;
tooltip << "Search for " << linkSign;
break;
}
ToolTip(ui::Point(0, Size.Y), tooltip.str());
Expand Down
2 changes: 1 addition & 1 deletion src/gui/game/SignTool.cpp
Expand Up @@ -184,7 +184,7 @@ void SignWindow::DoDraw()
char type = 0;
Graphics * g = GetGraphics();
std::string text = currentSign.getText(sim);
sign::splitsign(currentSign.text.c_str(), &type);
sign::splitsign(currentSign.text, &type);
currentSign.pos(text, x, y, w, h);
g->clearrect(x, y, w+1, h);
g->drawrect(x, y, w+1, h, 192, 192, 192, 255);
Expand Down
77 changes: 35 additions & 42 deletions src/simulation/Sign.cpp
@@ -1,3 +1,5 @@
#include <iomanip>
#include <sstream>
#include "Sign.h"
#include "graphics/Graphics.h"
#include "simulation/Simulation.h"
Expand All @@ -12,51 +14,45 @@ sign::sign(std::string text_, int x_, int y_, Justification justification_):

std::string sign::getText(Simulation *sim)
{
char buff[256];
char signText[256];
sprintf(signText, "%s", text.substr(0, 255).c_str());

if(signText[0] && signText[0] == '{')
std::stringstream signTextNew;
if (text[0] && text[0] == '{')
{
if (!strcmp(signText,"{p}"))
if (text == "{p}")
{
float pressure = 0.0f;
if (x>=0 && x<XRES && y>=0 && y<YRES)
if (x >= 0 && x < XRES && y >= 0 && y < YRES)
pressure = sim->pv[y/CELL][x/CELL];
sprintf(buff, "Pressure: %3.2f", pressure); //...pressure
signTextNew << std::fixed << std::showpoint << std::setprecision(2) << "Pressure: " << pressure;
}
else if (!strcmp(signText,"{aheat}"))
else if (text == "{aheat}")
{
float aheat = 0.0f;
if (x>=0 && x<XRES && y>=0 && y<YRES)
if (x >= 0 && x < XRES && y >= 0 && y < YRES)
aheat = sim->hv[y/CELL][x/CELL];
sprintf(buff, "%3.2f", aheat-273.15);
signTextNew << std::fixed << std::showpoint << std::setprecision(2) << aheat-273.15f;
}
else if (!strcmp(signText,"{t}"))
else if (text == "{t}")
{
if (x>=0 && x<XRES && y>=0 && y<YRES && sim->pmap[y][x])
sprintf(buff, "Temp: %4.2f", sim->parts[ID(sim->pmap[y][x])].temp-273.15); //...temperature
if (x >= 0 && x < XRES && y >= 0 && y < YRES && sim->pmap[y][x])
signTextNew << std::fixed << std::showpoint << std::setprecision(2) << "Temp: " << sim->parts[ID(sim->pmap[y][x])].temp-273.15f;
else
sprintf(buff, "Temp: 0.00"); //...temperature
signTextNew << "Temp: 0.00";
}
else
{
int pos = splitsign(signText);
int pos = splitsign(text);
if (pos)
{
strcpy(buff, signText+pos+1);
buff[strlen(signText)-pos-2]=0;
}
signTextNew << text.substr(pos+1, text.length()-pos-2);
else
strcpy(buff, signText);
signTextNew << text;
}
}
else
{
strcpy(buff, signText);
signTextNew << text;
}

return std::string(buff);
return signTextNew.str();
}

void sign::pos(std::string signText, int & x0, int & y0, int & w, int & h)
Expand All @@ -68,46 +64,43 @@ void sign::pos(std::string signText, int & x0, int & y0, int & w, int & h)
y0 = (y > 18) ? y - 18 : y + 4;
}

int sign::splitsign(const char* str, char * type)
int sign::splitsign(std::string str, char * type)
{
if (str[0]=='{' && (str[1]=='c' || str[1]=='t' || str[1]=='b' || str[1]=='s'))
if (str[0] == '{' && (str[1] == 'c' || str[1] == 't' || str[1] == 'b' || str[1] == 's'))
{
const char* p = str+2;
// signs with text arguments
size_t strIndex = 2;
// Signs with text arguments
if (str[1] == 's')
{
if (str[2]==':')
if (str[2] == ':')
{
p = str+4;
while (*p && *p!='|')
p++;
strIndex = 3;
while (strIndex < str.length() && str[strIndex] != '|')
strIndex++;
}
else
return 0;
}
// signs with number arguments
// Signs with number arguments
if (str[1] == 'c' || str[1] == 't')
{
if (str[2]==':' && str[3]>='0' && str[3]<='9')
if (str[2] == ':' && str[3] >= '0' && str[3] <= '9')
{
p = str+4;
while (*p>='0' && *p<='9')
p++;
strIndex = 4;
while (str[strIndex] >= '0' && str[strIndex] <= '9')
strIndex++;
}
else
return 0;
}

if (*p=='|')
if (str[strIndex] == '|')
{
int r = p-str;
while (*p)
p++;
if (p[-1] == '}')
if (str[str.length() - 1] == '}')
{
if (type)
*type = str[1];
return r;
return strIndex;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/simulation/Sign.h
Expand Up @@ -17,7 +17,7 @@ class sign
std::string getText(Simulation *sim);
void pos(std::string signText, int & x0, int & y0, int & w, int & h);

static int splitsign(const char* str, char * type = NULL);
static int splitsign(std::string str, char * type = NULL);
};

#endif

0 comments on commit f9b5c6b

Please sign in to comment.