doodle  0.2
Intended to support teaching C++, doodle is a simple library that helps make a window and makes it easy to do some drawing.
Functions
Utility

Functions

std::string doodle::to_string (MouseButtons button) noexcept
 Convert MouseButtons enum to std::string. More...
 
std::string doodle::to_string (KeyboardButtons button) noexcept
 Convert KeboardButtons enum to std::string. More...
 
std::wstring doodle::to_wstring (MouseButtons button) noexcept
 Convert MouseButtons enum to std::wstring. More...
 
std::wstring doodle::to_wstring (KeyboardButtons button) noexcept
 Convert KeboardButtons enum to std::wstring. More...
 
double doodle::noise (double x, double y=0.0, double z=0.0) noexcept
 Returns the Perlin noise value at specified coordinates. More...
 
void doodle::seed_noise (unsigned long long new_seed) noexcept
 Sets the seed value for noise(). More...
 
void doodle::set_noise_detail (int perlin_octaves, double amplitude_falloff) noexcept
 Adjusts the character and level of detail produced by the Perlin noise function. More...
 
void doodle::seed_random (unsigned int seed) noexcept
 Sets the seed value for random(). More...
 
double doodle::random (double min_inclusive, double max_exclusive) noexcept
 Return a random floating-point number within the range [min,max) More...
 
double doodle::random (double max_exclusive) noexcept
 Return a random floating-point number within the range [0,max) More...
 
double doodle::random () noexcept
 Return a random floating-point number within the range [0,1) More...
 
int doodle::random (int min_inclusive, int max_exclusive) noexcept
 Return a random integer number within the range [min,max) More...
 
int doodle::random (int max_exclusive) noexcept
 Return a random integer number within the range [0,max) More...
 

Detailed Description

Helper functions to convert the input enums to strings.

Helper functions to get noise values.

Helper functions to get random numbers.

Function Documentation

◆ noise()

double doodle::noise ( double  x,
double  y = 0.0,
double  z = 0.0 
)
noexcept

Returns the Perlin noise value at specified coordinates.

Perlin noise is a random sequence generator producing a more natural ordered, harmonic succession of numbers compared to the standard random() function. It was invented by Ken Perlin in the 1980s and been used since in graphical applications to produce procedural images, natural motion, shapes, terrains etc.

The main difference to the random() function is that Perlin noise is defined in an infinite n-dimensional space where each pair of coordinates corresponds to a fixed semi-random value (fixed only for the lifespan of the program; see the seed_noise() function). doodle can compute 1D, 2D and 3D noise, depending on the number of coordinates given. The resulting value will always be between 0.0 and 1.0. The noise value can be animated by moving through the noise space as demonstrated in the example above. The 2nd and 3rd dimension can also be interpreted as time.

A way to adjust the character of the resulting sequence is the scale of the input coordinates. As the function works within an infinite space the value of the coordinates doesn't matter as such, only the distance between successive coordinates does (eg. when using noise() within a loop). As a general rule the smaller the difference between coordinates, the smoother the resulting noise sequence will be. Steps of 0.005-0.03 work best for most applications, but this will differ depending on use.

Parameters
xx-coordinate in noise space
yy-coordinate in noise space
zz-coordinate in noise space
Returns
Perlin noise value (between 0 and 1) at specified coordinates
using namespace doodle;
int main(void)
{
create_window(480, 360);
constexpr double noiseScale = 0.035;
while (!is_window_closed())
{
const int mouseX = get_mouse_x();
const int mouseY = get_mouse_y();
for (int x = 0; x < Width; ++x)
{
const double noiseValue = noise((mouseX + x) * noiseScale, mouseY * noiseScale);
const auto grey = noiseValue * 255.0;
draw_line(x * 1.0, mouseY - noiseValue * 80.0, x * 1.0, Height * 1.0);
}
}
return 0;
}
void set_outline_color(HexColor color) noexcept
Set the outline and lines of shapes to the specified HexColor.
void clear_background() noexcept
Clear the background to black.
bool create_window() noexcept
Create a default window.
int Width
System variable that stores the width of the drawing canvas. This value is set by the desired_width p...
bool is_window_closed() noexcept
Is the window closed?
int Height
System variable that stores the height of the drawing canvas. This value is set by the desired_height...
void update_window() noexcept
Update the doodle application.
int get_mouse_x() noexcept
Get the mouse's X position relative to the currently set doodle::FrameOfReference.
int get_mouse_y() noexcept
Get the mouse's Y position relative to the currently set doodle::FrameOfReference.
void draw_line(double x1, double y1, double x2, double y2) noexcept
Draws a line (a direct path between two points) to the screen.
void set_frame_of_reference(FrameOfReference frame_of_reference) noexcept
Change the coordinate system you would like to use when describing your primitives.
@ RightHanded_OriginBottomLeft
The Origin (0,0) is always in the bottom left of the screen. Positive x goes to the right and Positiv...
double noise(double x, double y=0.0, double z=0.0) noexcept
Returns the Perlin noise value at specified coordinates.
Definition: angle.hpp:11

◆ random() [1/5]

double doodle::random ( )
noexcept

Return a random floating-point number within the range [0,1)

It is the same as calling

random(0.0,1.0);
double random(double min_inclusive, double max_exclusive) noexcept
Return a random floating-point number within the range [min,max)
Returns
random number between 0 and 1
using namespace doodle;
int main(void)
{
create_window(480, 320);
constexpr double thickness = 30.0;
for (int i = 0; i < 10; ++i)
{
const double x = random() * (Width - thickness) + thickness;
const double y = random() * (Height - thickness) + thickness;
draw_ellipse(x, y, thickness);
}
while (!is_window_closed())
{
}
return 0;
}
void draw_ellipse(double x, double y, double width, double height=0) noexcept
Draws an ellipse (oval) to the screen.

◆ random() [2/5]

double doodle::random ( double  max_exclusive)
noexcept

Return a random floating-point number within the range [0,max)

It is the same as calling

random(0.0,max_exclusive);
Parameters
max_exclusivethe exclusive upper bound or the maximum value but won't be returned.
Returns
random number
#include <cmath>
using namespace doodle;
void DrawRandomLineOnCircle() noexcept
{
// 1st random point on circle
const double angle1 = random(TWO_PI);
const double x1 = 200.0 * std::cos(angle1);
const double y1 = 200.0 * std::sin(angle1);
// 2nd random point on circle
const double angle2 = random(TWO_PI);
const double x2 = 200.0 * std::cos(angle2);
const double y2 = 200.0 * std::sin(angle2);
draw_line(x1, y1, x2, y2);
}
int main(void)
{
create_window(400, 400);
while (!is_window_closed())
{
DrawRandomLineOnCircle();
DrawRandomLineOnCircle();
}
return 0;
}
constexpr double TWO_PI
Definition: angle.hpp:62

◆ random() [3/5]

double doodle::random ( double  min_inclusive,
double  max_exclusive 
)
noexcept

Return a random floating-point number within the range [min,max)

Parameters
min_inclusivethe inclusive lower bound or the minimum value to be returned
max_exclusivethe exclusive upper bound or the maximum value but won't be returned.
Returns
random number
using namespace doodle;
int main(void)
{
create_window(480, 360);
double trauma = 1.0;
constexpr double MaxAngle = QUARTER_PI / 2.0;
constexpr double MaxOffset = 50.0;
while (!is_window_closed())
{
clear_background(HexColor{0xFCF4EFFF});
{
trauma += 0.05;
trauma = std::clamp(trauma, 0.0, 1.0);
}
const double shake = trauma * trauma;
set_fill_color(HexColor{0xA0C7BADD});
draw_rectangle(0, 0, 32.0, 2.0 + trauma * Height);
set_fill_color(HexColor{0xD9E4CEDD});
draw_rectangle(32.0, 0, 32.0, 2.0 + shake * Height);
if (trauma > 0)
{
const double offsetX = MaxOffset * shake * random(-1.0, 1.0);
const double offsetY = MaxOffset * shake * random(-1.0, 1.0);
const double angle = MaxAngle * shake * random(-1.0, 1.0);
apply_translate(offsetX, offsetY);
apply_rotate(angle);
trauma -= DeltaTime;
trauma = std::clamp(trauma, 0.0, 1.0);
}
set_fill_color(HexColor{0x5FA8B9FF});
draw_ellipse(-50.0, 50.0, 32.0);
draw_ellipse(50.0, 50.0, 32.0);
set_fill_color(HexColor{0x1A344DFF});
draw_ellipse(0.0, -100.0, 50.0, 100.0);
}
return 0;
}
constexpr double QUARTER_PI
Definition: angle.hpp:50
void set_fill_color(HexColor color) noexcept
Sets the color used to fill shapes to the specified HexColor.
double DeltaTime
The system variable DeltaTime contains the time difference between the beginning of the previous fram...
bool KeyIsPressed
The boolean system variable KeyIsPressed is true if any key is pressed and false if no keys are press...
void draw_rectangle(double x, double y, double width, double height=0) noexcept
Draws a rectangle to the screen.
@ RightHanded_OriginCenter
The Origin (0,0) is always in the center of the screen. Positive x goes to the right and Positive y g...
void apply_rotate(double angle_in_radians) noexcept
Rotates a shape the amount specified by the angle in radians.
void apply_translate(double translate_x, double translate_y) noexcept
Specifies an amount to displace objects within the display window.

◆ random() [4/5]

int doodle::random ( int  max_exclusive)
noexcept

Return a random integer number within the range [0,max)

It is the same as calling

random(0,max_exclusive);
Parameters
max_exclusivethe exclusive upper bound or the maximum value but won't be returned.
Returns
random number
using namespace doodle;
int main(void)
{
create_window(480, 320);
for (int y = 0; y < Height; ++y)
{
const auto grey = random(50);
set_outline_color(5 * grey);
draw_line(0, y, 10.0 + grey * (Width / 50.0), y);
}
while (!is_window_closed())
{
}
return 0;
}

◆ random() [5/5]

int doodle::random ( int  min_inclusive,
int  max_exclusive 
)
noexcept

Return a random integer number within the range [min,max)

Parameters
min_inclusivethe inclusive lower bound or the minimum value to be returned
max_exclusivethe exclusive upper bound or the maximum value but won't be returned.
Returns
random number
using namespace doodle;
int main(void)
{
create_window(480, 320);
for (int y = 0; y < Height; ++y)
{
const int r = random(-Width / 2, Width / 2);
draw_line(Width / 2.0, y, Width / 2.0 + r, y);
}
while (!is_window_closed())
{
}
return 0;
}

◆ seed_noise()

void doodle::seed_noise ( unsigned long long  new_seed)
noexcept

Sets the seed value for noise().

By default, noise() produces different results each time the program is run. Set the value parameter to a constant to return the same pseudo-random numbers each time the software is run.

Parameters
new_seedThe seed value
#include <iostream>
using namespace doodle;
int main(void)
{
create_window(480, 360);
set_fill_color(255, 128);
constexpr double noiseScale = 0.35599;
seed_noise(13374269651);
using floats = std::vector<double>;
floats xValues, yValues;
for (int i = 0; i < 10; ++i)
{
const auto fx = noise(i * noiseScale);
const auto fy = noise(0, i * noiseScale);
std::cout << fx << ", " << fy << "\n";
xValues.push_back(60.0 + Width * 0.5 * fx);
yValues.push_back(60.0 + Height * 0.5 * fy);
}
while (!is_window_closed())
{
for (floats::size_type i = 0; i < xValues.size(); ++i)
{
draw_ellipse(xValues[i], yValues[i], 16.0);
}
}
return 0;
}
void no_outline()
Disables drawing the outline.
@ LeftHanded_OriginTopLeft
The Origin (0,0) is always in the top left of the screen. Positive x goes to the right and Positive y...
void seed_noise(unsigned long long new_seed) noexcept
Sets the seed value for noise().

Prints the following:

0.623153, 0.623153
0.658371, 0.443496
0.682836, 0.34331
0.54437, 0.33238
0.692192, 0.350942
0.361594, 0.444189
0.382214, 0.394488
0.533032, 0.33581
0.819142, 0.375306
0.750284, 0.347514

◆ seed_random()

void doodle::seed_random ( unsigned int  seed)
noexcept

Sets the seed value for random().

By default, random() produces different results each time the program is run. Set the seed parameter to a constant to return the same pseudo-random numbers each time the software is run.

Parameters
seedthe seed value
using namespace doodle;
int main(void)
{
create_window(480, 320);
seed_random(13374269);
for (int x = 0; x < Width; ++x)
{
const auto grey = random(256);
draw_line(x * 1.0, 0.0, x * 1.0, Height * 1.0);
}
while (!is_window_closed())
{
}
return 0;
}
void seed_random(unsigned int seed) noexcept
Sets the seed value for random().

◆ set_noise_detail()

void doodle::set_noise_detail ( int  perlin_octaves,
double  amplitude_falloff 
)
noexcept

Adjusts the character and level of detail produced by the Perlin noise function.

Similar to harmonics in physics, noise is computed over several octaves. Lower octaves contribute more to the output signal and as such define the overall intensity of the noise, whereas higher octaves create finer grained details in the noise sequence.

By default, noise is computed over 4 octaves with each octave contributing exactly half than its predecessor, starting at 50% strength for the 1st octave. This falloff amount can be changed by adding an additional function parameter. Eg. a falloff factor of 0.75 means each octave will now have 75% impact (25% less) of the previous lower octave. Any value between 0.0 and 1.0 is valid, however note that values greater than 0.5 might result in greater than 1.0 values returned by noise().

By changing these parameters, the signal created by the noise() function can be adapted to fit very specific needs and characteristics.

Parameters
perlin_octavesnumber of octaves to be used by the noise. default is 4
amplitude_fallofffalloff factor for each octave. default is 0.5
#include <exception>
#include <iostream>
using namespace doodle;
int main(void)
try
{
create_window(480, 320);
Image noiseImage{Width, Height};
constexpr double noiseScale = 0.02;
while (!is_window_closed())
{
const int mouseX = get_mouse_x();
const int mouseY = get_mouse_y();
for (int column = 0; column < noiseImage.GetWidth() / 2; ++column)
{
for (int row = 0; row < noiseImage.GetHeight(); ++row)
{
double noiseValue = noise((mouseX + column) * noiseScale, (mouseY + row) * noiseScale);
auto grey = noiseValue * 255;
noiseImage(column, row) = Color{grey};
set_noise_detail(8, 0.65);
noiseValue = noise((mouseX + column + Width / 2.0) * noiseScale, (mouseY + row) * noiseScale);
// because the falloff is > 0.5, we may get noise values > 1, so we need to clamp our values
grey = std::clamp(noiseValue * 255, 0.0, 255.0);
noiseImage(column + Width / 2, row) = Color{grey};
}
}
draw_image(noiseImage, 0, 0);
}
return 0;
}
catch (const std::exception& e)
{
std::cerr << e.what() << '\n';
return -1;
}
void draw_image(const Image &image, double x, double y) noexcept
Draw an entire image to the screen.
void set_image_mode(RectMode mode) noexcept
Modifies the location from which textures are drawn by changing the way in which parameters given to ...
@ Center
RectMode::CENTER interprets the first two parameters of draw_rectangle() as the shape's center point.
void set_noise_detail(int perlin_octaves, double amplitude_falloff) noexcept
Adjusts the character and level of detail produced by the Perlin noise function.

◆ to_string() [1/2]

std::string doodle::to_string ( KeyboardButtons  button)
noexcept

Convert KeboardButtons enum to std::string.

Parameters
buttonkeyboard button
Returns
The name of the button as a std::string

◆ to_string() [2/2]

std::string doodle::to_string ( MouseButtons  button)
noexcept

Convert MouseButtons enum to std::string.

Parameters
buttonmouse button
Returns
The name of the button as a std::string

◆ to_wstring() [1/2]

std::wstring doodle::to_wstring ( KeyboardButtons  button)
noexcept

Convert KeboardButtons enum to std::wstring.

Parameters
buttonkeyboard button
Returns
The name of the button as a std::wstring

◆ to_wstring() [2/2]

std::wstring doodle::to_wstring ( MouseButtons  button)
noexcept

Convert MouseButtons enum to std::wstring.

Parameters
buttonmouse button
Returns
The name of the button as a std::wstring