Dragonfly 4.20
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 "EventMouse.h"
16#include "Line.h"
17#include "Object.h"
18#ifndef NO_NETWORK
19#include "NetworkNode.h"
20#endif
21#include "Particle.h"
22#include "Precipitation.h"
23#include "Vector.h"
24#include "ViewObject.h"
25
26namespace df {
27
28/// Return true if value is between min and max (inclusive).
29bool valueInRange(float value, float min, float max);
30
31/// Convert relative bounding Box for Object to absolute world Box.
33
34/// Convert relative bounding Box for Object to absolute world Box
35/// at postion where.
36Box getWorldBox(const Object *p_o, Vector where);
37
38/// Return distance between any two positions.
39float distance(Vector p1, Vector p2);
40
41/// Return distance between any two Objects.
42/// Return -1.0 if error.
43float distance(Object *p_o1, Object *p_o2);
44
45/// Return true of position is within Box.
47
48/// Return true if Box 1 completely contains Box 2.
49bool boxContainsBox(Box b1, Box b2);
50
51/// Return true if Line segments intersect.
52/// (Parallel line segments don't intersect).
53/// (Co-linear lines will NOT intersect).
54bool lineIntersectsLine(Line line1, Line line2);
55
56/// Return true if Line segment intersects Box.
58
59/// Return true if Boxes intersect.
60bool boxIntersectsBox(Box box1, Box box2);
61
62/// Return true if Circle intersects or contains Box.
64
65/// Launch splash screen. Return 0 if ok, else -1.
66int splash();
67
68/// Returns pretty-formatted time as char * string.
70
71/// Convert world position to view position.
73
74/// Convert view position to world position.
76
77/// Convert integer to string, returning string.
78std::string toString(int i);
79
80/// Convert float to string, returning string.
81std::string toString(float f);
82
83/// Convert character to string, returning string.
84std::string toString(char c);
85
86/// Convert boolean to string, returning string.
87std::string toString(bool b);
88
89/// Convert Dragonfly Color to string, returning string.
90std::string toString(Color color);
91
92/// Convert Path nodes (not index) to string, returning string.
93std::string toString(Path path);
94
95/// Convert Dragonfly key to string, returning string.
96std::string toString(Keyboard::Key key_val);
97
98/// Convert Solidness to string, returning string.
99std::string toString(Solidness solid);
100
101/// Convert ViewObjectLocation to string, returning string.
102std::string toString(ViewObjectLocation location);
103
104#ifndef NO_NETWORK
105// Convert MessageType to string, returning string.
106std::string toString(MessageType message);
107#endif
108
109/// Match key:value pair in string in str, returning value.
110/// Pairs separated by commas (,).
111/// If str is empty, use previously parsed string str.
112/// All whitespace is ignored.
113/// Return empty string if no match.
114std::string match(std::string str, std::string find);
115
116/// Add particles. Each parameter has average and spread.
117/// count - number to add
118/// position - location
119/// direction - direction to move [(0,0) for random]
120/// size - size (pixels)
121/// speed - speed (spaces/tick)
122/// age - age (ticks)
123/// opacity - how "see through" [0-255, 0 is transparent]
124/// r, g, b - color in RGB values
125/// Particle class - class to add (default is PARTICLE).
126/// Return 0 if ok, else -1.
127int addParticles(int count, int count_spread,
128 Vector position, float position_spread,
129 Vector direction, float direction_spread,
130 float size, float size_spread,
131 float speed, float speed_spread,
132 int age, int age_spread,
133 unsigned char opacity, char opacity_spread,
134 unsigned char r, unsigned char g, unsigned char b,
135 unsigned char color_spread,
136 ParticleClass particle_class=PARTICLE);
137
138/// Add particles of specific type.
139/// type - type of particle: SMOKE, SPARKS, RINGS, FIREWORKS.
140/// position - location
141/// scale - scale size (default 1.0)
142/// color - dragonfly color to use (default 'built-in')
143/// Return 0 if ok, else -1.
145 float scale=1.0, Color color=CUSTOM);
146
147/// Add environment particles of specific type.
148/// type - type of particle: RAIN, SNOW, STARFIELD.
149/// direction - direction particle travels: UP, DOWN, LEFT, RIGHT.
150/// scale - scale size (default 1.0)
151/// color - dragonfly color to use (default 'built-in')
152/// Return 0 if ok, else -1.
153int addParticles(PrecipitationType type, Direction direction, float scale=1.0,
154 Color color=CUSTOM);
155
156/// Set RGB color based on DF color.
157void colorToRGB(Color color, unsigned char &r, unsigned char &g,
158 unsigned char &b);
159
160/// Return SFML color based on DF color.
161sf::Color colorToSFML(Color color);
162
163/// Return normal between two colliding Objects.
164/// Object 1 caused collision with Object 2.
165/// Return (0,0) if no normal (no collision or
166/// Object 1 inside Object 2).
167Vector getNormal(const Object *p_o1, const Object *p_o2);
168
169/// Return bitmask as string.
170std::string maskToString(unsigned int mask);
171
172/// "Explode" a sprite frame into small, Particles.
173/// index - frame index
174/// position - location of frame
175/// position - location of frame
176/// age - how long it should last (in ticks)
177/// speed - speed (in spaces per tick) (negative to "implode")
178/// rotate - degrees to spin (per tick)
179/// Return 0 if ok, else -1.
180int explode(const Sprite *p_sprite, int index, Vector position, int age,
181 float speed, float rotate);
182
183// Compute rotation angle from vector.
184float vectorToAngle(df::Vector v);
185
186} // end of namespace df
187#endif // __UTILITY_H__
Definition: Box.h:16
Definition: Circle.h:12
Definition: Line.h:12
Definition: Object.h:57
Definition: Path.h:17
Definition: Sprite.h:17
Definition: Vector.h:12
Key
Keys Dragonfly recognizes.
Definition: EventKeyboard.h:25
An animation for a sprite.
Definition: Animation.h:15
Vector getNormal(const Object *p_o1, const Object *p_o2)
Return normal between two colliding Objects.
bool valueInRange(float value, float min, float max)
Return true if value is between min and max (inclusive).
bool circleIntersectsBox(Circle circle, Box b)
Return true if Circle intersects or contains Box.
int splash()
Launch splash screen. Return 0 if ok, else -1.
std::string match(std::string str, std::string find)
Match key:value pair in string in str, returning value.
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 boxContainsPosition(Box b, Vector p)
Return true of position is within Box.
sf::Color colorToSFML(Color color)
Return SFML color based on DF color.
void colorToRGB(Color color, unsigned char &r, unsigned char &g, unsigned char &b)
Set RGB color based on DF color.
Color
Colors Dragonfly recognizes.
Definition: Color.h:11
std::string toString(int i)
Convert integer to string, returning string.
std::string maskToString(unsigned int mask)
Return bitmask as string.
Vector viewToWorld(Vector view_pos)
Convert view position to world position.
bool boxContainsBox(Box b1, Box b2)
Return true if Box 1 completely contains Box 2.
bool lineIntersectsBox(Line line, Box b)
Return true if Line segment intersects Box.
char * getTimeString()
Returns pretty-formatted time as char * string.
ParticleClass
Particle classes.
Definition: Particle.h:23
ParticleType
Particle types.
Definition: Particle.h:14
bool lineIntersectsLine(Line line1, Line line2)
Return true if Line segments intersect.
Direction
Directions.
Definition: Particle.h:30
float distance(Vector p1, Vector p2)
Return distance between any two positions.
int explode(const Sprite *p_sprite, int index, Vector position, int age, float speed, float rotate)
"Explode" a sprite frame into small, Particles.
Solidness
Types of solidness of Object.
Definition: Object.h:26
Box getWorldBox(const Object *p_o)
Convert relative bounding Box for Object to absolute world Box.
bool boxIntersectsBox(Box box1, Box box2)
Return true if Boxes intersect.
Vector worldToView(Vector world_pos)
Convert world position to view position.
ViewObjectLocation
General location on screen.
Definition: ViewObject.h:26
PrecipitationType
Precipitation types.
Definition: Precipitation.h:14