RigsofRods
Soft-body Physics Simulation
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Utils.h
Go to the documentation of this file.
1 /*
2  This source file is part of Rigs of Rods
3  Copyright 2005-2012 Pierre-Michel Ricordel
4  Copyright 2007-2012 Thomas Fischer
5  Copyright 2013-2016 Petr Ohlidal
6 
7  For more information, see http://www.rigsofrods.org/
8 
9  Rigs of Rods is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License version 3, as
11  published by the Free Software Foundation.
12 
13  Rigs of Rods is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with Rigs of Rods. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
28 #pragma once
29 
30 #include "Application.h"
31 
32 #include "utf8/checked.h"
33 #include "utf8/unchecked.h"
34 
35 #include <MyGUI.h>
36 
37 namespace RoR {
38 
39 Ogre::String sha1sum(const char *key, int len);
40 
41 Ogre::String HashData(const char *key, int len);
42 
43 std::string tryConvertUTF(const char* buffer);
44 
45 std::string formatBytes(double bytes);
46 
47 std::time_t getTimeStamp();
48 
49 Ogre::String getVersionString(bool multiline = true);
50 
51 inline void replaceString(std::string& str, std::string searchString, std::string replaceString)
52 {
53  std::string::size_type pos = 0;
54  while ((pos = str.find(searchString, pos)) != std::string::npos)
55  {
56  str.replace(pos, searchString.size(), replaceString);
57  pos++;
58  }
59 }
60 
61 Ogre::Real Round(Ogre::Real value, unsigned short ndigits = 0);
62 
63 std::string SanitizeUtf8String(std::string const& str_in);
64 std::string SanitizeUtf8CString(const char* start, const char* end = nullptr);
65 
66 std::wstring Utf8ToWideChar(std::string input_utf8);
67 
68 inline std::string& TrimStr(std::string& s) { Ogre::StringUtil::trim(s); return s; }
69 std::string Sha1Hash(std::string const & data);
70 
71 std::string JoinStrVec(Ogre::StringVector tokens, const std::string& delim);
72 
73 // for std::vector
74 template <class T, class A, class Predicate>
75 inline void EraseIf(std::vector<T, A>& c, Predicate pred)
76 {
77  c.erase(std::remove_if(c.begin(), c.end(), pred), c.end());
78 }
79 
83 {
84 public:
85  World2ScreenConverter(Ogre::Matrix4 view_mtx, Ogre::Matrix4 proj_mtx, Ogre::Vector2 screen_size):
86  m_view_matrix(view_mtx), m_projection_matrix(proj_mtx), m_screen_size(screen_size)
87  {}
88 
90  Ogre::Vector3 Convert(Ogre::Vector3 world_pos)
91  {
92  Ogre::Vector3 view_space_pos = m_view_matrix * world_pos;
93  Ogre::Vector3 clip_space_pos = m_projection_matrix * view_space_pos; // Clip space pos is [-1.f, 1.f]
94  float screen_x = ((clip_space_pos.x / 2.f) + 0.5f) * m_screen_size.x;
95  float screen_y = (1.f - ((clip_space_pos.y / 2.f) + 0.5f)) * m_screen_size.y;
96  return Ogre::Vector3(screen_x, screen_y, view_space_pos.z);
97  }
98 
99 private:
100  Ogre::Matrix4 m_view_matrix;
101  Ogre::Matrix4 m_projection_matrix;
102  Ogre::Vector2 m_screen_size;
103 };
104 
105 bool IsDistanceWithin(Ogre::Vector3 const& a, Ogre::Vector3 const& b, float max);
106 
107 std::string PrintMeshInfo(std::string const& title, Ogre::MeshPtr mesh);
108 
109 void CvarAddFileToList(CVar* cvar, const std::string& filename);
110 void CvarRemoveFileFromList(CVar* cvar, const std::string& filename);
111 
112 void SplitBundleQualifiedFilename(const std::string& bundleQualifiedFilename, std::string& out_bundleName, std::string& out_filename);
113 
114 } // namespace RoR
RoR::getVersionString
Ogre::String getVersionString(bool multiline=true)
Definition: Utils.cpp:82
RoR::World2ScreenConverter::m_projection_matrix
Ogre::Matrix4 m_projection_matrix
Definition: Utils.h:101
RoR::PrintMeshInfo
std::string PrintMeshInfo(std::string const &title, Ogre::MeshPtr mesh)
RoR::Utf8ToWideChar
std::wstring Utf8ToWideChar(std::string input_utf8)
Definition: Utils.cpp:149
RoR::SanitizeUtf8String
std::string SanitizeUtf8String(std::string const &str_in)
Definition: Utils.cpp:120
RoR::Round
Ogre::Real Round(Ogre::Real value, unsigned short ndigits=0)
Definition: Utils.cpp:101
RoR::SplitBundleQualifiedFilename
void SplitBundleQualifiedFilename(const std::string &bundleQualifiedFilename, std::string &out_bundleName, std::string &out_filename)
Definition: Utils.cpp:239
RoR::HashData
Ogre::String HashData(const char *key, int len)
Definition: Utils.cpp:54
RoR::formatBytes
std::string formatBytes(double bytes)
Definition: Utils.cpp:67
RoR::TrimStr
std::string & TrimStr(std::string &s)
Definition: Utils.h:68
RoR::World2ScreenConverter::Convert
Ogre::Vector3 Convert(Ogre::Vector3 world_pos)
Definition: Utils.h:90
RoR::replaceString
void replaceString(std::string &str, std::string searchString, std::string replaceString)
Definition: Utils.h:51
RoR::getTimeStamp
std::time_t getTimeStamp()
Definition: Utils.cpp:77
RoR::World2ScreenConverter
< Keeps data close for faster access.
Definition: Utils.h:82
RoR::World2ScreenConverter::m_screen_size
Ogre::Vector2 m_screen_size
Definition: Utils.h:102
Application.h
Central state/object manager and communications hub.
RoR::tryConvertUTF
std::string tryConvertUTF(const char *buffer)
Definition: Utils.cpp:61
RoR::CvarAddFileToList
void CvarAddFileToList(CVar *cvar, const std::string &filename)
Definition: Utils.cpp:217
RoR::CVar
Quake-style console variable, defined in RoR.cfg or crated via Console UI and scripts.
Definition: CVar.h:52
RoR::SanitizeUtf8CString
std::string SanitizeUtf8CString(const char *start, const char *end=nullptr)
Definition: Utils.cpp:128
RoR::JoinStrVec
std::string JoinStrVec(Ogre::StringVector tokens, const std::string &delim)
Definition: Utils.cpp:157
RoR::World2ScreenConverter::m_view_matrix
Ogre::Matrix4 m_view_matrix
Definition: Utils.h:100
RoR::Sha1Hash
std::string Sha1Hash(std::string const &data)
Definition: Utils.cpp:141
RoR::CvarRemoveFileFromList
void CvarRemoveFileFromList(CVar *cvar, const std::string &filename)
Definition: Utils.cpp:227
RoR::World2ScreenConverter::World2ScreenConverter
World2ScreenConverter(Ogre::Matrix4 view_mtx, Ogre::Matrix4 proj_mtx, Ogre::Vector2 screen_size)
Definition: Utils.h:85
RoR::EraseIf
void EraseIf(std::vector< T, A > &c, Predicate pred)
Definition: Utils.h:75
RoR
Definition: AppContext.h:36
RoR::sha1sum
Ogre::String sha1sum(const char *key, int len)
Definition: Utils.cpp:46
RoR::IsDistanceWithin
bool IsDistanceWithin(Ogre::Vector3 const &a, Ogre::Vector3 const &b, float max)
Definition: Utils.cpp:169