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