Dragonfly  4.3
A text-based game engine
utility.h
1 //
2 // Utility functions to support Dragonfly and its games.
3 //
4 // Functions here do not use any attributes of any classes, so
5 // can stand alone.
6 //
7 
8 #ifndef __UTILITY_H__
9 #define __UTILITY_H__
10 
11 // Engine includes
12 #include "Box.h"
13 #include "Circle.h"
14 #include "EventKeyboard.h"
15 #include "Line.h"
16 #include "Object.h"
17 #include "Particle.h"
18 #include "Precipitation.h"
19 #include "Vector.h"
20 
21 namespace df {
22 
23 /// Return true if value is between min and max (inclusive).
24 bool valueInRange(float value, float min, float max);
25 
26 /// Convert relative bounding Box for Object to absolute world Box.
27 Box getWorldBox(const Object *p_o);
28 
29 /// Convert relative bounding Box for Object to absolute world Box
30 /// at postion where.
31 Box getWorldBox(const Object *p_o, Vector where);
32 
33 /// Return distance between any two positions.
34 float distance(Vector p1, Vector p2);
35 
36 /// Return distance between any two Objects.
37 /// Return -1.0 if error.
38 float distance(Object *p_o1, Object *p_o2);
39 
40 /// Return true of position is within Box.
41 bool boxContainsPosition(Box b, Vector p);
42 
43 /// Return true if Line segments intersect.
44 /// (Parallel line segments don't intersect).
45 /// (Co-linear lines will NOT intersect).
46 bool lineIntersectsLine(Line line1, Line line2);
47 
48 /// Return true if Line segment intersects Box.
49 bool lineIntersectsBox(Line line, Box b);
50 
51 /// Return true if Boxes intersect.
52 bool boxIntersectsBox(Box box1, Box box2);
53 
54 /// Return true if Circle intersects or contains Box.
55 bool circleIntersectsBox(Circle circle, Box b);
56 
57 /// Launch splash screen. Return 0 if ok, else -1.
58 int splash();
59 
60 /// Returns pretty-formatted time as char * string.
61 char *getTimeString();
62 
63 /// Convert world position to view position.
64 Vector worldToView(Vector world_pos);
65 
66 /// Convert view position to world position.
67 Vector viewToWorld(Vector view_pos);
68 
69 /// Convert integer to string, returning string.
70 std::string toString(int i);
71 
72 /// Convert float to string, returning string.
73 std::string toString(float f);
74 
75 /// Convert character to string, returning string.
76 std::string toString(char c);
77 
78 /// Convert boolean to string, returning string.
79 std::string toString(bool b);
80 
81 /// Convert Dragonfly Shape to string, returning string.
82 std::string toString(Shape shape);
83 
84 /// Convert Path nodes (not index) to string.
85 std::string toString(Path path);
86 
87 /// Convert Dragonfly key to string.
88 std::string toString(Keyboard::Key key_val);
89 
90 /// Match key:value pair in string in str, returning value.
91 /// Pairs separated by commas (,).
92 /// If str is empty, use previously parsed string str.
93 /// All whitespace is ignored.
94 /// Return empty string if no match.
95 std::string match(std::string str, std::string find);
96 
97 /// Add particles. Each parameter has average and spread.
98 /// count - number to add
99 /// position - location
100 /// direction - direction to move [(0,0) for random]
101 /// size - size (pixels)
102 /// speed - speed (spaces/tick)
103 /// age - age (ticks)
104 /// opacity - how "see through" [0-255, 0 is transparent]
105 /// r, g, b - color in RGB values
106 /// Particle class - class to add (default is PARTICLE).
107 /// Return 0 if ok, else -1.
108 int addParticles(int count, int count_spread,
109  Vector position, float position_spread,
110  Vector direction, float direction_spread,
111  float size, float size_spread,
112  float speed, float speed_spread,
113  int age, int age_spread,
114  unsigned char opacity, char opacity_spread,
115  unsigned char r, unsigned char g, unsigned char b,
116  unsigned char color_spread,
117  ParticleClass particle_class=PARTICLE);
118 
119 /// Add particles of specific type.
120 /// type - type of particle: SMOKE, SPARKS, RINGS, FIREWORKS.
121 /// position - location
122 /// scale - scale size (default 1.0)
123 /// color - dragonfly color to use (default 'built-in')
124 /// Return 0 if ok, else -1.
125 int addParticles(ParticleType type, Vector position,
126  float scale=1.0, Color color=CUSTOM);
127 
128 /// Add environment particles of specific type.
129 /// type - type of particle: RAIN, SNOW, STARFIELD.
130 /// scale - scale size (default 1.0)
131 /// color - dragonfly color to use (default 'built-in')
132 /// Return 0 if ok, else -1.
133 int addParticles(PrecipitationType type, Direction direction, float scale=1.0,
134  Color color=CUSTOM);
135 
136 /// Set RGB color based on DF color.
137 void colorToRGB(Color color, unsigned char &r, unsigned char &g,
138  unsigned char &b);
139 
140 /// Return SFML color based on DF color.
141 sf::Color colorToSFML(Color color);
142 
143 /// Return normal between two colliding Objects.
144 /// Object 1 caused collision with Object 2.
145 /// Return (0,0) if no normal (no collision or
146 /// Object 1 inside Object 2).
147 Vector getNormal(const Object *p_o1, const Object *p_o2);
148 
149 } // end of namespace df
150 #endif // __UTILITY_H__
void colorToRGB(Color color, unsigned char &r, unsigned char &g, unsigned char &b)
Set RGB color based on DF color.
Vector viewToWorld(Vector view_pos)
Convert view position to world position.
int addParticles(int count, int count_spread, Vector position, float position_spread, Vector direction, float direction_spread, float size, float size_spread, float speed, float speed_spread, int age, int age_spread, unsigned char opacity, char opacity_spread, unsigned char r, unsigned char g, unsigned char b, unsigned char color_spread, ParticleClass particle_class=PARTICLE)
Add particles.
bool lineIntersectsLine(Line line1, Line line2)
Return true if Line segments intersect.
sf::Color colorToSFML(Color color)
Return SFML color based on DF color.
ParticleClass
Particle classes.
Definition: Particle.h:26
Vector getNormal(const Object *p_o1, const Object *p_o2)
Return normal between two colliding Objects.
Direction
Directions.
Definition: Particle.h:33
A 2-d bounding box.
Definition: Box.h:10
char * getTimeString()
Returns pretty-formatted time as char * string.
int splash()
Launch splash screen. Return 0 if ok, else -1.
bool lineIntersectsBox(Line line, Box b)
Return true if Line segment intersects Box.
ParticleType
Particle types.
Definition: Particle.h:17
PrecipitationType
Precipitation types.
Definition: Precipitation.h:14
Key
Keys Dragonfly recognizes.
Definition: EventKeyboard.h:25
std::string toString(int i)
Convert integer to string, returning string.
bool boxIntersectsBox(Box box1, Box box2)
Return true if Boxes intersect.
Color
Colors Dragonfly recognizes.
Definition: Color.h:11
float distance(Vector p1, Vector p2)
Return distance between any two positions.
bool valueInRange(float value, float min, float max)
Return true if value is between min and max (inclusive).
Vector worldToView(Vector world_pos)
Convert world position to view position.
bool boxContainsPosition(Box b, Vector p)
Return true of position is within Box.
Box getWorldBox(const Object *p_o)
Convert relative bounding Box for Object to absolute world Box.
std::string match(std::string str, std::string find)
Match key:value pair in string in str, returning value.
bool circleIntersectsBox(Circle circle, Box b)
Return true if Circle intersects or contains Box.