2020-01-01 21:29:24 +01:00
|
|
|
#include "UtilityClass.h"
|
|
|
|
|
2020-06-12 19:46:11 +02:00
|
|
|
#include <cctype>
|
2020-07-19 21:43:29 +02:00
|
|
|
#include <SDL.h>
|
2020-01-01 21:29:24 +01:00
|
|
|
#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);
|
2020-02-20 05:58:12 +01:00
|
|
|
int x = 0;
|
2020-01-01 21:29:24 +01:00
|
|
|
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)
|
|
|
|
{
|
2020-07-03 11:37:15 +02:00
|
|
|
for (size_t i = 0; i < SDL_arraysize(splitseconds); i++)
|
2020-01-01 21:29:24 +01:00
|
|
|
{
|
2020-07-03 11:37:15 +02:00
|
|
|
splitseconds[i] = (i * 100) / 30;
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
slowsine = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2020-04-03 00:05:41 +02:00
|
|
|
std::string tempstring = "";
|
2020-07-03 04:35:21 +02:00
|
|
|
int temp = (t - (t % 30)) / 30;
|
2020-01-01 21:29:24 +01:00
|
|
|
if (temp < 60) //less than one minute
|
|
|
|
{
|
|
|
|
t = t % 30;
|
|
|
|
tempstring = String(temp) + ":" + twodigits(splitseconds[t]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-03 04:35:21 +02:00
|
|
|
int temp2 = (temp - (temp % 60)) / 60;
|
2020-01-01 21:29:24 +01:00
|
|
|
temp = temp % 60;
|
|
|
|
t = t % 30;
|
|
|
|
tempstring = String(temp2) + ":" + twodigits(temp) + ":" + twodigits(splitseconds[t]);
|
|
|
|
}
|
|
|
|
return tempstring;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string UtilityClass::number( int _t )
|
|
|
|
{
|
2020-07-03 11:37:53 +02:00
|
|
|
static const std::string ones_place[] = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
|
|
|
|
static const std::string tens_place[] = {"Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
|
|
|
|
static const std::string teens[] = {"Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
|
Abstract UtilityClass::number() to be more generic
Previously, it was a hardcoded list going up to fifty.
This time, it's less hardcoded. Most of the time, it will take a number,
find out its tens-place word, find out its ones-place word, and combine
the two together. There are special cases for the teens, and the numbers
zero through nine, and one hundred.
Also, now if it is given a negative value, it will just return "???"
instead.
If there are more than one hundred it will still say "Lots".
2020-01-23 15:45:50 +01:00
|
|
|
|
|
|
|
if (_t < 0)
|
|
|
|
{
|
|
|
|
return "???";
|
|
|
|
}
|
|
|
|
else if (_t > 100)
|
|
|
|
{
|
|
|
|
return "Lots";
|
|
|
|
}
|
|
|
|
else if (_t == 0)
|
|
|
|
{
|
|
|
|
return "Zero";
|
|
|
|
}
|
|
|
|
else if (_t == 100)
|
|
|
|
{
|
|
|
|
return "One Hundred";
|
|
|
|
}
|
|
|
|
else if (_t >= 1 && _t <= 9)
|
|
|
|
{
|
|
|
|
return ones_place[_t-1];
|
|
|
|
}
|
|
|
|
else if (_t >= 11 && _t <= 19)
|
|
|
|
{
|
|
|
|
return teens[_t-11];
|
|
|
|
}
|
|
|
|
else if (_t % 10 == 0)
|
|
|
|
{
|
|
|
|
return tens_place[(_t/10)-1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return tens_place[(_t/10)-1] + " " + ones_place[(_t%10)-1];
|
|
|
|
}
|
2020-01-01 21:29:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2020-04-17 23:52:09 +02:00
|
|
|
|
2020-06-16 02:31:54 +02:00
|
|
|
bool is_positive_num(const std::string& str, bool hex)
|
2020-04-17 23:52:09 +02:00
|
|
|
{
|
|
|
|
for (size_t i = 0; i < str.length(); i++)
|
|
|
|
{
|
2020-06-16 02:31:54 +02:00
|
|
|
if (hex)
|
2020-04-17 23:52:09 +02:00
|
|
|
{
|
2020-06-16 02:31:54 +02:00
|
|
|
if (!std::isxdigit(static_cast<unsigned char>(str[i])))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!std::isdigit(static_cast<unsigned char>(str[i])))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-17 23:52:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2020-06-21 23:25:23 +02:00
|
|
|
|
|
|
|
bool endsWith(const std::string& str, const std::string& suffix)
|
|
|
|
{
|
|
|
|
if (str.size() < suffix.size())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return str.compare(
|
|
|
|
str.size() - suffix.size(),
|
|
|
|
suffix.size(),
|
|
|
|
suffix
|
|
|
|
) == 0;
|
|
|
|
}
|