2020-01-01 21:29:24 +01:00
|
|
|
#include "UtilityClass.h"
|
|
|
|
|
|
|
|
#include "SDL.h"
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
/* Used by UtilityClass::GCString to generate a button list */
|
|
|
|
const char *GCChar(SDL_GameControllerButton button)
|
|
|
|
{
|
|
|
|
if (button == SDL_CONTROLLER_BUTTON_A)
|
|
|
|
{
|
|
|
|
return "A";
|
|
|
|
}
|
|
|
|
else if (button == SDL_CONTROLLER_BUTTON_B)
|
|
|
|
{
|
|
|
|
return "B";
|
|
|
|
}
|
|
|
|
else if (button == SDL_CONTROLLER_BUTTON_X)
|
|
|
|
{
|
|
|
|
return "X";
|
|
|
|
}
|
|
|
|
else if (button == SDL_CONTROLLER_BUTTON_Y)
|
|
|
|
{
|
|
|
|
return "Y";
|
|
|
|
}
|
|
|
|
else if (button == SDL_CONTROLLER_BUTTON_BACK)
|
|
|
|
{
|
|
|
|
return "BACK";
|
|
|
|
}
|
|
|
|
else if (button == SDL_CONTROLLER_BUTTON_GUIDE)
|
|
|
|
{
|
|
|
|
return "GUIDE";
|
|
|
|
}
|
|
|
|
else if (button == SDL_CONTROLLER_BUTTON_START)
|
|
|
|
{
|
|
|
|
return "START";
|
|
|
|
}
|
|
|
|
else if (button == SDL_CONTROLLER_BUTTON_LEFTSTICK)
|
|
|
|
{
|
|
|
|
return "L3";
|
|
|
|
}
|
|
|
|
else if (button == SDL_CONTROLLER_BUTTON_RIGHTSTICK)
|
|
|
|
{
|
|
|
|
return "R3";
|
|
|
|
}
|
|
|
|
else if (button == SDL_CONTROLLER_BUTTON_LEFTSHOULDER)
|
|
|
|
{
|
|
|
|
return "LB";
|
|
|
|
}
|
|
|
|
else if (button == SDL_CONTROLLER_BUTTON_RIGHTSHOULDER)
|
|
|
|
{
|
|
|
|
return "RB";
|
|
|
|
}
|
|
|
|
SDL_assert(0 && "Unhandled button!");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ss_toi( std::string _s )
|
|
|
|
{
|
|
|
|
std::istringstream i(_s);
|
|
|
|
int x;
|
|
|
|
i >> x;
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> split( const std::string &s, char delim, std::vector<std::string> &elems )
|
|
|
|
{
|
|
|
|
std::stringstream ss(s);
|
|
|
|
std::string item;
|
|
|
|
while(std::getline(ss, item, delim))
|
|
|
|
{
|
|
|
|
elems.push_back(item);
|
|
|
|
}
|
|
|
|
return elems;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::string> split( const std::string &s, char delim )
|
|
|
|
{
|
|
|
|
std::vector<std::string> elems;
|
|
|
|
return split(s, delim, elems);
|
|
|
|
}
|
|
|
|
|
|
|
|
UtilityClass::UtilityClass() :
|
|
|
|
glow(0),
|
|
|
|
glowdir(0)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < 30; i++)
|
|
|
|
{
|
|
|
|
splitseconds.push_back(int((i * 100) / 30));
|
|
|
|
}
|
|
|
|
|
|
|
|
slowsine = 0;
|
2020-01-11 01:37:23 +01:00
|
|
|
globaltemp = 0;
|
|
|
|
temp = 0;
|
|
|
|
temp2 = 0;
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string UtilityClass::String( int _v )
|
|
|
|
{
|
|
|
|
std::ostringstream os;
|
|
|
|
os << _v;
|
|
|
|
return(os.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string UtilityClass::GCString(std::vector<SDL_GameControllerButton> buttons)
|
|
|
|
{
|
|
|
|
std::string retval = "";
|
|
|
|
for (size_t i = 0; i < buttons.size(); i += 1)
|
|
|
|
{
|
|
|
|
retval += GCChar(buttons[i]);
|
|
|
|
if ((i + 1) < buttons.size())
|
|
|
|
{
|
|
|
|
retval += ",";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string UtilityClass::twodigits( int t )
|
|
|
|
{
|
|
|
|
if (t < 10)
|
|
|
|
{
|
|
|
|
return "0" + String(t);
|
|
|
|
}
|
|
|
|
if (t >= 100)
|
|
|
|
{
|
|
|
|
return "??";
|
|
|
|
}
|
|
|
|
return String(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string UtilityClass::timestring( int t )
|
|
|
|
{
|
|
|
|
//given a time t in frames, return a time in seconds
|
|
|
|
tempstring = "";
|
|
|
|
temp = (t - (t % 30)) / 30;
|
|
|
|
if (temp < 60) //less than one minute
|
|
|
|
{
|
|
|
|
t = t % 30;
|
|
|
|
tempstring = String(temp) + ":" + twodigits(splitseconds[t]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
temp2 = (temp - (temp % 60)) / 60;
|
|
|
|
temp = temp % 60;
|
|
|
|
t = t % 30;
|
|
|
|
tempstring = String(temp2) + ":" + twodigits(temp) + ":" + twodigits(splitseconds[t]);
|
|
|
|
}
|
|
|
|
return tempstring;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string UtilityClass::number( int _t )
|
|
|
|
{
|
2020-01-12 01:43:23 +01:00
|
|
|
const int BIGGEST_SMALL_NUMBER = 50;
|
|
|
|
const char* smallnumbers[] = {"Zero", "One", "Two", "Three",
|
|
|
|
"Four", "Five", "Six", "Seven", "Eight", "Nine",
|
|
|
|
"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen",
|
|
|
|
"Sixteen", "Seventeen", "Eighteen", "Nineteen", "Twenty", "Twenty One",
|
|
|
|
"Twenty Two", "Twenty Three", "Twenty Four", "Twenty Five",
|
|
|
|
"Twenty Six", "Twenty Seven", "Twenty Eight", "Twenty Nine",
|
|
|
|
"Thirty", "Thirty One", "Thirty Two", "Thirty Three", "Thirty Four",
|
|
|
|
"Thirty Five", "Thirty Six", "Thirty Seven", "Thirty Eight",
|
|
|
|
"Thirty Nine", "Forty Zero", "Forty One", "Forty Two", "Forty Three",
|
|
|
|
"Forty Four", "Forty Five", "Forty Six", "Forty Seven", "Forty Eight",
|
|
|
|
"Forty Nine", "Fifty"};
|
|
|
|
|
|
|
|
if(_t <= BIGGEST_SMALL_NUMBER) {
|
|
|
|
return smallnumbers[_t];
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
2020-01-12 01:43:23 +01:00
|
|
|
|
2020-01-01 21:29:24 +01:00
|
|
|
return "Lots";
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UtilityClass::intersects( SDL_Rect A, SDL_Rect B )
|
|
|
|
{
|
2020-01-12 15:09:20 +01:00
|
|
|
return (SDL_HasIntersection(&A, &B) == SDL_TRUE);
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void UtilityClass::updateglow()
|
|
|
|
{
|
|
|
|
slowsine++;
|
|
|
|
if (slowsine >= 64) slowsine = 0;
|
|
|
|
|
|
|
|
if (glowdir == 0) {
|
|
|
|
glow+=2;
|
|
|
|
if (glow >= 62) glowdir = 1;
|
|
|
|
}else {
|
|
|
|
glow-=2;
|
|
|
|
if (glow < 2) glowdir = 0;
|
|
|
|
}
|
|
|
|
}
|