1 /*
2 Copyright © 2011-2012 Clint Bellanger
3 Copyright © 2012 Stefan Beller
4 Copyright © 2013 Henrik Andersson
5 Copyright © 2013 Kurt Rinnert
6 Copyright © 2012-2016 Justin Jacobs
7 
8 This file is part of FLARE.
9 
10 FLARE is free software: you can redistribute it and/or modify it under the terms
11 of the GNU General Public License as published by the Free Software Foundation,
12 either version 3 of the License, or (at your option) any later version.
13 
14 FLARE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
16 PARTICULAR PURPOSE.  See the GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License along with
19 FLARE.  If not, see http://www.gnu.org/licenses/
20 */
21 
22 /**
23  * Utils
24  *
25  * Various utility structures, enums, function
26  */
27 
28 #ifndef UTILS_H
29 #define UTILS_H
30 
31 #include <SDL.h>
32 #include <stdint.h>
33 #include <string>
34 #include <queue>
35 
36 typedef unsigned long SoundID;
37 typedef unsigned long StatusID;
38 
39 typedef size_t ItemID;
40 typedef size_t ItemSetID;
41 typedef size_t PowerID;
42 
43 class Avatar;
44 class FPoint; // needed for Point -> FPoint constructor
45 
46 class Point {
47 public:
48 	int x, y;
49 	Point();
50 	Point(int _x, int _y);
51 	explicit Point(const FPoint& _fp);
52 };
53 
54 class FPoint {
55 public:
56 	float x, y;
57 	FPoint();
58 	FPoint(float _x, float _y);
59 	explicit FPoint(Point _p);
60 	void align();
61 };
62 
63 class Rect {
64 public:
65 	int x, y, w, h;
66 	Rect();
67 	Rect(int _x, int _y, int _w, int _h);
68 	explicit Rect(const SDL_Rect& _r);
69 	operator SDL_Rect() const;
70 };
71 
72 class Color {
73 public:
74 	Uint8 r, g, b, a;
75 	Color();
76 	Color(Uint8 _r, Uint8 _g, Uint8 _b, Uint8 _a = 255);
77 	operator SDL_Color() const;
78 	bool operator ==(const Color &other);
79 	bool operator !=(const Color &other);
80 	uint32_t encodeRGBA();
81 	void decodeRGBA(const uint32_t encoded);
82 };
83 
84 class Timer {
85 private:
86 	unsigned current;
87 	unsigned duration;
88 public:
89 	enum {
90 		END = 0,
91 		BEGIN = 1
92 	};
93 
94 	Timer(unsigned _duration = 0);
95 	unsigned getCurrent();
96 	unsigned getDuration();
97 	void setCurrent(unsigned val);
98 	void setDuration(unsigned val);
99 	bool tick();
100 	bool isEnd();
101 	bool isBegin();
102 	void reset(int type);
103 	bool isWholeSecond();
104 };
105 
106 namespace Utils {
107 	// Alignment: For aligning objects. 0-8 are screen-relative, 9-17 are menu frame relative.
108 	enum {
109 		ALIGN_TOPLEFT = 0,
110 		ALIGN_TOP = 1,
111 		ALIGN_TOPRIGHT = 2,
112 		ALIGN_LEFT = 3,
113 		ALIGN_CENTER = 4,
114 		ALIGN_RIGHT = 5,
115 		ALIGN_BOTTOMLEFT = 6,
116 		ALIGN_BOTTOM = 7,
117 		ALIGN_BOTTOMRIGHT = 8,
118 		ALIGN_FRAME_TOPLEFT = 9,
119 		ALIGN_FRAME_TOP = 10,
120 		ALIGN_FRAME_TOPRIGHT = 11,
121 		ALIGN_FRAME_LEFT = 12,
122 		ALIGN_FRAME_CENTER = 13,
123 		ALIGN_FRAME_RIGHT = 14,
124 		ALIGN_FRAME_BOTTOMLEFT = 15,
125 		ALIGN_FRAME_BOTTOM = 16,
126 		ALIGN_FRAME_BOTTOMRIGHT = 17
127 	};
128 
129 	extern int LOCK_INDEX;
130 	extern bool LOG_FILE_INIT;
131 	extern bool LOG_FILE_CREATED;
132 	extern std::string LOG_PATH;
133 	extern std::queue<std::pair<SDL_LogPriority, std::string> > LOG_MSG;
134 
135 	FPoint screenToMap(int x, int y, float camx, float camy);
136 	Point mapToScreen(float x, float y, float camx, float camy);
137 	FPoint calcVector(const FPoint& pos, int direction, float dist);
138 	float calcDist(const FPoint& p1, const FPoint& p2);
139 	float calcTheta(float x1, float y1, float x2, float y2);
140 	unsigned char calcDirection(float x0, float y0, float x1, float y1);
141 	bool isWithinRadius(const FPoint& center, float radius, const FPoint& target);
142 	bool isWithinRect(const Rect& r, const Point& target);
143 
144 	std::string abbreviateKilo(int amount);
145 	void alignToScreenEdge(int alignment, Rect *r);
146 
147 	void logInfo(const char* format, ...);
148 	void logError(const char* format, ...);
149 	void logErrorDialog(const char* dialog_text, ...);
150 	void createLogFile();
151 	void Exit(int code);
152 
153 	void createSaveDir(int slot);
154 	void removeSaveDir(int slot);
155 
156 	Rect resizeToScreen(int w, int h, bool crop, int align);
157 
158 	size_t stringFindCaseInsensitive(const std::string &_a, const std::string &_b);
159 
160 	std::string floatToString(const float value, size_t precision);
161 	std::string getDurationString(const int duration, size_t precision);
162 
163 	std::string substituteVarsInString(const std::string &_s, Avatar* avatar);
164 
165 	FPoint clampDistance(float range, const FPoint& src, const FPoint& target);
166 
167 	bool rectsOverlap(const Rect &a, const Rect &b);
168 
169 	int rotateDirection(int direction, int val);
170 
171 	std::string getTimeString(const unsigned long time);
172 
173 	unsigned long hashString(const std::string& str);
174 
175 	char* strdup(const std::string& str);
176 
177 	void lockFileRead();
178 	void lockFileWrite(int increment);
179 	void lockFileCheck();
180 }
181 
182 #endif
183