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.
Public Types | Public Member Functions | Static Public Member Functions | Friends | List of all members
doodle::Image Class Reference

Image class to store a 2D array of RGBA colors. Also manages an image on the Graphics Card. More...

#include <image.hpp>

Public Types

enum class  FileType { PNG , BMP , TGA }
 Lists all of the supported image file formats. More...
 

Public Member Functions

 Image (int width, int height, bool smooth_it=false)
 Construct an empty image with a provided resolution. More...
 
 Image (const std::filesystem::path &file_path, bool smooth_it=false)
 Create an Image based off of an image file. The supported image formats are defined by the Image::FileType enum. More...
 
 Image ()
 Creates an empty image. More...
 
void SaveToFile (const std::filesystem::path &file_path, FileType file_type=FileType::PNG) const
 Save the Image as an image file on disk. More...
 
int GetWidth () const noexcept
 Return the width of the image. More...
 
int GetHeight () const noexcept
 Return the height of the image. More...
 
int GetNumberOfColors () const noexcept
 Return the total number of colors in the image. More...
 
bool IsSmooth () const noexcept
 Tell whether the smooth filter is enabled or not. More...
 
Color operator[] (int index) const
 Get a specific color from the image. More...
 
Coloroperator[] (int index)
 Get a specific color from the image, so that you can change the image contents. Usage of this will trigger creating a new GPU texture when the Image is drawn via draw_image(). More...
 
Color operator() (int column, int row) const
 Get a specific color from the image. More...
 
Coloroperator() (int column, int row)
 Get a specific color from the image, so that you can change the image contents. Usage of this will trigger creating a new GPU texture when the Image is drawn via draw_image(). More...
 
Colorbegin ()
 Returns an iterator to the first color of the Image. More...
 
Colorend ()
 Returns an iterator to the color following the last color of the Image. More...
 
const Colorbegin () const
 Returns a const iterator to the first color of the Image. More...
 
const Colorend () const
 Returns a const iterator to the color following the last color of the Image. More...
 

Static Public Member Functions

static int MaxImageSize () noexcept
 Get the maximum image size allowed. More...
 

Friends

void draw_image (const Image &image, double x, double y) noexcept
 Draw an entire image to the screen. More...
 
void draw_image (const Image &image, double x, double y, double width, double height) noexcept
 Draw an entire image to the screen and resize it to a custom size. More...
 
void draw_image (const Image &image, double x, double y, double width, double height) noexcept
 Draw an entire image to the screen and resize it to a custom size. More...
 
void draw_image (const Image &image, double x, double y, double width, double height, int texel_x, int texel_y) noexcept
 Draw a subsection of the image to the screen. More...
 
void draw_image (const Image &image, double x, double y, double width, double height, int texel_x, int texel_y, int texel_width, int texel_height) noexcept
 Draw a subsection of the image to the screen. More...
 
Image end_drawing_to_image (bool smooth_texture)
 End a session of drawing to an image. More...
 

Detailed Description

Image class to store a 2D array of RGBA colors. Also manages an image on the Graphics Card.

The Image class is useful for creating images programmatically. It is also useful for loading and creating image files

Definition at line 29 of file image.hpp.

Member Enumeration Documentation

◆ FileType

Lists all of the supported image file formats.

Enumerator
PNG 

Portable Network Graphics file format.

BMP 

Bitmap file format.

TGA 

Targa file format.

Definition at line 149 of file image.hpp.

150  {
154  PNG,
158  BMP,
162  TGA
163  };

Constructor & Destructor Documentation

◆ Image() [1/3]

doodle::Image::Image ( int  width,
int  height,
bool  smooth_it = false 
)

Construct an empty image with a provided resolution.

Parameters
widthhow wide to make the image
heighthow tall to make the image
smooth_itWhen the filter is activated, the texture appears smoother so that pixels are less noticeable. However if you want the texture to look exactly the same as its source file, you should leave it disabled. The smooth filter is disabled by default.
Exceptions
std::runtime_errorif width/height is <= 0 or is too large
#include <cmath>
#include <exception>
#include <iostream>
using namespace doodle;
int main(void)
try
{
create_window(480, 320);
Image noiseImage{Width, Height};
while (!is_window_closed())
{
for (int column = 0; column < noiseImage.GetWidth(); ++column)
{
for (int row = 0; row < noiseImage.GetHeight(); ++row)
{
const auto c = 255.0 * noise(column * 0.02, row * 0.02, ElapsedTime / 4.0);
const auto grey = std::round(c);
noiseImage(column, row) = Color{grey};
}
}
draw_image(noiseImage, 0, 0);
}
return 0;
}
catch (const std::exception& e)
{
std::cerr << e.what() << '\n';
return -1;
}
Image()
Creates an empty image.
friend void draw_image(const Image &image, double x, double y) noexcept
Draw an entire image to the screen.
double ElapsedTime
Returns the number of seconds since starting the program. This information is often used for timing e...
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.
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.
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

◆ Image() [2/3]

doodle::Image::Image ( const std::filesystem::path &  file_path,
bool  smooth_it = false 
)
explicit

Create an Image based off of an image file. The supported image formats are defined by the Image::FileType enum.

Parameters
file_pathpath to an image file
smooth_itWhen the filter is activated, the texture appears smoother so that pixels are less noticeable. However if you want the texture to look exactly the same as its source file, you should leave it disabled. The smooth filter is disabled by default.
Exceptions
std::runtime_errorif it cannot find the file or if it can't read it

The following example uses this orange hero image file.

#include <exception>
#include <iostream>
using namespace doodle;
int main(void)
try
{
create_window(360, 180);
constexpr bool Smooth = true;
const Image orangeHero{"orange_hero.png", !Smooth};
const Image orangeHeroSmooth{"orange_hero.png", Smooth};
while (!is_window_closed())
{
clear_background(HexColor{0x3546530A});
draw_image(orangeHero, -64, 0, 128, 128, 8, 0, 16, 16);
draw_image(orangeHeroSmooth, 64, 0, 128, 128, 8, 0, 16, 16);
}
return 0;
}
catch (const std::exception& e)
{
std::cerr << e.what() << '\n';
return -1;
}
void clear_background() noexcept
Clear the background to black.

◆ Image() [3/3]

doodle::Image::Image ( )

Creates an empty image.

Member Function Documentation

◆ begin() [1/2]

Color* doodle::Image::begin ( )

Returns an iterator to the first color of the Image.

Usage of this will trigger creating a new GPU texture when the Image is drawn via draw_image().

Having a begin/end interface enables the Image class to be used with for range loops and the std <algorithm> functions.

Returns
iterator to the first element

The following example uses this orange hero image file.

#include <algorithm>
#include <exception>
#include <iostream>
using namespace doodle;
Image GenerateNoiseImage(int width, int height)
{
Image pattern{width, height};
int x = 0, y = 0;
double offset = random(1000.0);
auto generate_noise = [x, y, width, offset]() mutable -> Color {
++x;
if (x >= width)
x = 0, ++y;
return Color{128.0 * noise(offset + x * 0.4, offset + y * 0.4, offset)};
};
std::generate(pattern.begin(), pattern.end(), generate_noise);
return pattern;
}
Image GenerateCombinedImage(const Image& orangeHero, const Image& pattern)
{
Image combinedImage{orangeHero.GetWidth(), orangeHero.GetHeight()};
auto combine = [](Color left, Color right) -> Color {
return Color{left.red + right.red, left.green + right.green, left.blue + right.blue, left.alpha};
};
std::transform(orangeHero.begin(), orangeHero.end(), pattern.begin(), combinedImage.begin(), combine);
return combinedImage;
}
int main(void)
try
{
create_window(480, 360);
const Image orangeHero{"orange_hero.png"};
const Image pattern = GenerateNoiseImage(orangeHero.GetWidth(), orangeHero.GetHeight());
const Image combinedImage = GenerateCombinedImage(orangeHero, pattern);
while (!is_window_closed())
{
draw_image(pattern, -35, 0);
draw_image(orangeHero, 0, 0);
draw_image(combinedImage, 35, 0);
}
return 0;
}
catch (const std::exception& e)
{
std::cerr << e.what() << '\n';
return -1;
}
void apply_scale(double scale) noexcept
Uniformly increases or decreases the size of a shape by expanding and contracting vertices.
double random(double min_inclusive, double max_exclusive) noexcept
Return a random floating-point number within the range [min,max)

◆ begin() [2/2]

const Color* doodle::Image::begin ( ) const

Returns a const iterator to the first color of the Image.

Having a begin/end interface enables the Image class to be used with for range loops and the std <algorithm> functions.

Returns
const iterator to the first element

The following example uses this orange hero image file.

#include <exception>
#include <iostream>
#include <numeric>
using namespace doodle;
int main(void)
try
{
create_window(240, 180);
const Image orangeHero{"orange_hero.png"};
int num_colors = 0;
auto add_colors = [&num_colors](Color& sum, Color current_color) -> Color& {
if (current_color.alpha < 255)
return sum;
sum.red += current_color.red;
sum.green += current_color.green;
sum.blue += current_color.blue;
++num_colors;
return sum;
};
// const begin/end allows us to use std::accumulate
Color average_color = std::accumulate(orangeHero.begin(), orangeHero.end(), Color{}, add_colors);
average_color.red /= num_colors;
average_color.green /= num_colors;
average_color.blue /= num_colors;
while (!is_window_closed())
{
clear_background(average_color);
draw_image(orangeHero, 0, 0, 128, 128, 8, 0, 16, 16);
}
return 0;
}
catch (const std::exception& e)
{
std::cerr << e.what() << '\n';
return -1;
}

◆ end() [1/2]

Color* doodle::Image::end ( )

Returns an iterator to the color following the last color of the Image.

This color acts as a placeholder; attempting to access it results in undefined behavior.

Having a begin/end interface enables the Image class to be used with for range loops and the std <algorithm> functions.

Returns
iterator to a color following the last color

The following example uses this orange hero image file.

#include <exception>
#include <iostream>
using namespace doodle;
Image GenerateNoiseImage(int width, int height)
{
Image pattern{width, height};
int x = 0, y = 0;
const double offset = random(1000.0);
for (auto& color : pattern) // begin/end allows us to express this
{
++x;
if (x >= width)
x = 0, ++y;
color = Color{128.0 * noise(offset + x * 0.4, offset + y * 0.4, offset)};
}
return pattern;
}
Image GenerateCombinedImage(const Image& orangeHero, const Image& pattern)
{
Image combinedImage{orangeHero.GetWidth(), orangeHero.GetHeight()};
const int size = combinedImage.GetNumberOfColors();
for (int i = 0; i < size; ++i)
{
const Color left = orangeHero[i];
const Color right = pattern[i];
combinedImage[i] = Color{left.red + right.red, left.green + right.green,
left.blue + right.blue, left.alpha};
}
return combinedImage;
}
int main(void)
try
{
create_window(480, 360);
const Image orangeHero{"orange_hero.png"};
const Image pattern = GenerateNoiseImage(orangeHero.GetWidth(), orangeHero.GetHeight());
const Image combinedImage = GenerateCombinedImage(orangeHero, pattern);
while (!is_window_closed())
{
draw_image(pattern, -35, 0);
draw_image(orangeHero, 0, 0);
draw_image(combinedImage, 35, 0);
}
return 0;
}
catch (const std::exception& e)
{
std::cerr << e.what() << '\n';
return -1;
}

◆ end() [2/2]

const Color* doodle::Image::end ( ) const

Returns a const iterator to the color following the last color of the Image.

This color acts as a placeholder; attempting to access it results in undefined behavior.

Having a begin/end interface enables the Image class to be used with for range loops and the std <algorithm> functions.

Returns
const iterator to a color following the last color

The following example uses this orange hero image file.

#include <exception>
#include <iostream>
using namespace doodle;
int main(void)
try
{
create_window(240, 180);
const Image orangeHero{"orange_hero.png"};
Color average_color;
int num_colors = 0;
for (const auto& color : orangeHero) // const begin/end allows us to express this loop
{
if (color.alpha < 255)
continue;
++num_colors;
average_color.red += color.red;
average_color.green += color.green;
average_color.blue += color.blue;
}
average_color.red /= num_colors;
average_color.green /= num_colors;
average_color.blue /= num_colors;
while (!is_window_closed())
{
clear_background(average_color);
draw_image(orangeHero, 0, 0, 128, 128, 8, 0, 16, 16);
}
return 0;
}
catch (const std::exception& e)
{
std::cerr << e.what() << '\n';
return -1;
}

◆ GetHeight()

int doodle::Image::GetHeight ( ) const
noexcept

Return the height of the image.

Returns
height of the Image

◆ GetNumberOfColors()

int doodle::Image::GetNumberOfColors ( ) const
noexcept

Return the total number of colors in the image.

Returns
the total number of colors in the Image

◆ GetWidth()

int doodle::Image::GetWidth ( ) const
noexcept

Return the width of the image.

Returns
width of the Image

◆ IsSmooth()

bool doodle::Image::IsSmooth ( ) const
noexcept

Tell whether the smooth filter is enabled or not.

Returns
True if smoothing is enabled, false if it is disabled

◆ MaxImageSize()

static int doodle::Image::MaxImageSize ( )
staticnoexcept

Get the maximum image size allowed.

This maximum size is defined by the graphics driver. You can expect a value of 1024 pixels for low-end graphics card, and up to 8192 pixels or more for newer hardware.

Returns
Maximum size allowed for images, in pixels
Precondition
Since this involves talking with the GPU a connection must already be established, so create_window() must have already been called.

◆ operator()() [1/2]

Color& doodle::Image::operator() ( int  column,
int  row 
)

Get a specific color from the image, so that you can change the image contents. Usage of this will trigger creating a new GPU texture when the Image is drawn via draw_image().

Parameters
columnShould be 0 \(\leq\) column \(<\) GetWidth()
rowShould be 0 \(\leq\) row \(<\) GetHeight()
Returns
a reference of the color at that position
Exceptions
std::out_of_rangeif column or row is out of range.

The following example uses this orange hero image file.

#include <exception>
#include <iostream>
using namespace doodle;
constexpr void operator+=(Color& c, Color o) noexcept
{
c.red += o.red, c.green += o.green, c.blue += o.blue, c.alpha += o.alpha;
}
Image CreateBlurImage(const Image& source)
{
using std::clamp;
const int width = source.GetWidth(), height = source.GetHeight();
Image image{width + 2, height + 2};
auto lookup = [&source, width, height](int column, int row) -> Color {
if (column < 0 || column >= width || row < 0 || row >= height)
return Color{255, 0};
return source(column, row); // const access
};
for (int row = 0; row < height + 2; ++row)
{
for (int column = 0; column < width + 2; ++column)
{
Color sum{0, 0};
for (int row_offset = -2; row_offset < 1; ++row_offset)
for (int column_offset = -2; column_offset < 1; ++column_offset)
sum += lookup(column + column_offset, row + row_offset);
sum.red /= 9, sum.green /= 9, sum.blue /= 9, sum.alpha /= 9;
image(column, row) = sum; // write access
}
}
return image;
}
int main(void)
try
{
create_window(480, 360);
const Image orangeHero{"orange_hero.png"};
const Image blurredHero = CreateBlurImage(orangeHero);
while (!is_window_closed())
{
draw_image(orangeHero, -32, 0);
draw_image(blurredHero, 32, 0);
}
return 0;
}
catch (const std::exception& e)
{
std::cerr << e.what() << '\n';
return -1;
}

◆ operator()() [2/2]

Color doodle::Image::operator() ( int  column,
int  row 
) const

Get a specific color from the image.

Parameters
columnShould be 0 \(\leq\) column \(<\) GetWidth()
rowShould be 0 \(\leq\) row \(<\) GetHeight()
Returns
a copy of the color at that position
Exceptions
std::out_of_rangeif column or row is out of range.

The following example uses this orange hero image file.

#include <exception>
#include <iostream>
using namespace doodle;
constexpr void operator+=(Color& c, Color o) noexcept
{
c.red += o.red, c.green += o.green, c.blue += o.blue, c.alpha += o.alpha;
}
Image CreateBlurImage(const Image& source)
{
using std::clamp;
const int width = source.GetWidth(), height = source.GetHeight();
Image image{width + 2, height + 2};
auto lookup = [&source, width, height](int column, int row) -> Color {
if (column < 0 || column >= width || row < 0 || row >= height)
return Color{255, 0};
return source(column, row); // const access
};
for (int row = 0; row < height + 2; ++row)
{
for (int column = 0; column < width + 2; ++column)
{
Color sum{0, 0};
for (int row_offset = -2; row_offset < 1; ++row_offset)
for (int column_offset = -2; column_offset < 1; ++column_offset)
sum += lookup(column + column_offset, row + row_offset);
sum.red /= 9, sum.green /= 9, sum.blue /= 9, sum.alpha /= 9;
image(column, row) = sum; // write access
}
}
return image;
}
int main(void)
try
{
create_window(480, 360);
const Image orangeHero{"orange_hero.png"};
const Image blurredHero = CreateBlurImage(orangeHero);
while (!is_window_closed())
{
draw_image(orangeHero, -32, 0);
draw_image(blurredHero, 32, 0);
}
return 0;
}
catch (const std::exception& e)
{
std::cerr << e.what() << '\n';
return -1;
}

◆ operator[]() [1/2]

Color& doodle::Image::operator[] ( int  index)

Get a specific color from the image, so that you can change the image contents. Usage of this will trigger creating a new GPU texture when the Image is drawn via draw_image().

Parameters
indexShould be 0 \(\leq\) index \(<\) GetNumberOfColors()
Returns
a reference of the color at that position
Exceptions
std::out_of_rangeif index is out of range.

The following example uses this orange hero image file.

#include <exception>
#include <iostream>
using namespace doodle;
double calculate_luminance(Color color) noexcept
{
// based off of http://alienryderflex.com/hsp.html
const double r = color.red;
const double g = color.green;
const double b = color.blue;
return sqrt(0.299 * r * r + 0.587 * g * g + 0.114 * b * b);
}
Image CreateGreyImage(const Image& source)
{
Image image{source.GetWidth(), source.GetHeight()};
const int size = image.GetNumberOfColors();
for (int i = 0; i < size; ++i)
{
const Color original = source[i];
// writable access
image[i] = Color{calculate_luminance(original), original.alpha};
}
return image;
}
int main(void)
try
{
create_window(480, 360);
const Image orangeHero{"orange_hero.png"};
const Image greyHero = CreateGreyImage(orangeHero);
while (!is_window_closed())
{
draw_image(greyHero, -32, 0);
draw_image(orangeHero, 32, 0);
}
return 0;
}
catch (const std::exception& e)
{
std::cerr << e.what() << '\n';
return -1;
}

◆ operator[]() [2/2]

Color doodle::Image::operator[] ( int  index) const

Get a specific color from the image.

Parameters
indexShould be 0 \(\leq\) index \(<\) GetNumberOfColors()
Returns
a copy of the color at that position
Exceptions
std::out_of_rangeif index is out of range.

The following example uses this orange hero image file.

#include <exception>
#include <iostream>
using namespace doodle;
double calculate_luminance(Color color) noexcept
{
// based off of http://alienryderflex.com/hsp.html
const double r = color.red;
const double g = color.green;
const double b = color.blue;
return sqrt(0.299 * r * r + 0.587 * g * g + 0.114 * b * b);
}
Image CreateGreyImage(const Image& source)
{
Image image{source.GetWidth(), source.GetHeight()};
const int size = image.GetNumberOfColors();
for (int i = 0; i < size; ++i)
{
const Color original = source[i]; // const access
image[i] = Color{calculate_luminance(original), original.alpha};
}
return image;
}
int main(void)
try
{
create_window(480, 360);
const Image orangeHero{"orange_hero.png"};
const Image greyHero = CreateGreyImage(orangeHero);
while (!is_window_closed())
{
draw_image(greyHero, -32, 0);
draw_image(orangeHero, 32, 0);
}
return 0;
}
catch (const std::exception& e)
{
std::cerr << e.what() << '\n';
return -1;
}

◆ SaveToFile()

void doodle::Image::SaveToFile ( const std::filesystem::path &  file_path,
FileType  file_type = FileType::PNG 
) const

Save the Image as an image file on disk.

You should not be giving a directory file path and you will need to add the image extension yourself.

Parameters
file_pathfile path to save the image file to.
file_typethe type of the image file to create

Friends And Related Function Documentation

◆ draw_image [1/5]

void draw_image ( const Image image,
double  x,
double  y 
)
friend

Draw an entire image to the screen.

The width and height of the image will be based off of the image width and image height.

Parameters
imageimage object to draw
xx-coordinate of the rectangle to draw the image.
yy-coordinate of the rectangle to draw the image.

The following example uses this orange hero image file.

#include <exception>
#include <iostream>
using namespace doodle;
int main(void)
try
{
create_window(240, 180);
const Image orangeHero{"orange_hero.png"};
while (!is_window_closed())
{
clear_background(HexColor{0x3546530A});
draw_image(orangeHero, 0, 0);
}
return 0;
}
catch (const std::exception& e)
{
std::cerr << e.what() << '\n';
return -1;
}

◆ draw_image [2/5]

void draw_image ( const Image image,
double  x,
double  y,
double  width,
double  height 
)
friend

Draw an entire image to the screen and resize it to a custom size.

Parameters
imageimage object to draw
xx-coordinate of the rectangle to draw the image.
yy-coordinate of the rectangle to draw the image.
widthhow wide to draw the image
heighthow tall to draw the image

The following example uses this orange hero image file.

#include <exception>
#include <iostream>
using namespace doodle;
int main(void)
try
{
create_window(240, 180);
const Image orangeHero{"orange_hero.png"};
while (!is_window_closed())
{
clear_background(HexColor{0x3546530A});
draw_image(orangeHero, 0, 0, 64, 128);
}
return 0;
}
catch (const std::exception& e)
{
std::cerr << e.what() << '\n';
return -1;
}

◆ draw_image [3/5]

void draw_image ( const Image image,
double  x,
double  y,
double  width,
double  height 
)
friend

Draw an entire image to the screen and resize it to a custom size.

Parameters
imageimage object to draw
xx-coordinate of the rectangle to draw the image.
yy-coordinate of the rectangle to draw the image.
widthhow wide to draw the image
heighthow tall to draw the image

The following example uses this orange hero image file.

#include <exception>
#include <iostream>
using namespace doodle;
int main(void)
try
{
create_window(240, 180);
const Image orangeHero{"orange_hero.png"};
while (!is_window_closed())
{
clear_background(HexColor{0x3546530A});
draw_image(orangeHero, 0, 0, 64, 128);
}
return 0;
}
catch (const std::exception& e)
{
std::cerr << e.what() << '\n';
return -1;
}

◆ draw_image [4/5]

void draw_image ( const Image image,
double  x,
double  y,
double  width,
double  height,
int  texel_x,
int  texel_y 
)
friend

Draw a subsection of the image to the screen.

The subsection is defined is defined by the box

Image
  (0,0)
    +---------------------------------------------------------------------+
    |                                                                     |
    |(texel_x,texel_y)                                                    |
    |        o--------------------------------+                           |
    |        |                                |                           |
    |        |                                |                           |
    |        |                                |                           |
    |        |                                |                           |
    |        |                                |                           |
    |        +--------------------------------o                           |
    |                           (texel_x+width,texel_y+height)            |
    |                                                                     |
    |                                                                     |
    |                                                                     |
    +---------------------------------------------------------------------+
                                                              (image_width,image_height)
Parameters
imageimage object to draw
xx-coordinate of the rectangle to draw the image.
yy-coordinate of the rectangle to draw the image.
widthhow wide to draw the image
heighthow tall to draw the image
texel_xHorizontally, where in image to start reading colors from the image. 0 is the far left and image width is the far right.
texel_yVertically, where in image to start reading colors from the image. 0 is the far top and image height is the far bottom.

The following example uses this orange hero image file.

#include <exception>
#include <iostream>
using namespace doodle;
int main(void)
try
{
create_window(240, 180);
const Image orangeHero{"orange_hero.png"};
while (!is_window_closed())
{
clear_background(HexColor{0x3546530A});
draw_image(orangeHero, 0, 0, 16, 16, 8, 0);
}
return 0;
}
catch (const std::exception& e)
{
std::cerr << e.what() << '\n';
return -1;
}

◆ draw_image [5/5]

void draw_image ( const Image image,
double  x,
double  y,
double  width,
double  height,
int  texel_x,
int  texel_y,
int  texel_width,
int  texel_height 
)
friend

Draw a subsection of the image to the screen.

The subsection is defined is defined by the box

  Image
(0,0)
  +---------------------------------------------------------------------+
  |                                                                     |
  |(texel_x,texel_y)                                                    |
  |        o--------------------------------+                           |
  |        |                                |                           |
  |        |                                |                           |
  |        |                                |                           |
  |        |                                |                           |
  |        |                                |                           |
  |        +--------------------------------o                           |
  |                     (texel_x+texel_width,texel_y+texel_height)      |
  |                                                                     |
  |                                                                     |
  |                                                                     |
  +---------------------------------------------------------------------+
                                                            (image_width,image_height)
Parameters
imageimage object to draw
xx-coordinate of the rectangle to draw the image.
yy-coordinate of the rectangle to draw the image.
widthhow wide to draw the image
heighthow tall to draw the image
texel_xHorizontally, where in image to start reading colors from the image. 0 is the far left and image width is the far right.
texel_yVertically, where in image to start reading colors from the image. 0 is the far top and image height is the far bottom.
texel_widthwidth of image subsection to read the colors from
texel_heightheight of image subsection to read the colors from

The following example uses this orange hero image file.

#include <exception>
#include <iostream>
using namespace doodle;
int main(void)
try
{
create_window(240, 180);
const Image orangeHero{"orange_hero.png"};
while (!is_window_closed())
{
clear_background(HexColor{0x3546530A});
draw_image(orangeHero, 0, 0, 128, 128, 8, 0, 16, 16);
}
return 0;
}
catch (const std::exception& e)
{
std::cerr << e.what() << '\n';
return -1;
}

◆ end_drawing_to_image

Image end_drawing_to_image ( bool  smooth_texture)
friend

End a session of drawing to an image.

Parameters
smooth_textureShould the texture use a smooth filtering when being drawn
Returns
An Image representing what was drawn.
using namespace doodle;
Image GenerateOFaceImage(bool smooth_filter);
int main(void)
{
create_window(480, 320);
const Image smiley_nosmooth = GenerateOFaceImage(false);
const Image smiley_smooth = GenerateOFaceImage(true);
while (!is_window_closed())
{
clear_background(100, 149, 237);
for (int i = 0; i < 20; i++)
draw_ellipse(0, -Height / 2.0 + i * 20.0, 10);
draw_image(smiley_nosmooth, 0, 0);
draw_image(smiley_smooth, -Width / 3.0, 0.0, 200, 200);
draw_image(smiley_nosmooth, Width / 3.0, 0.0, 200, 200);
}
return 0;
}
Image GenerateOFaceImage(bool smooth_filter)
{
constexpr double diameter = 100.0;
constexpr double size_scale = 0.25;
constexpr int image_width = static_cast<int>(diameter);
constexpr int image_height = static_cast<int>(diameter);
begin_drawing_to_image(image_width, image_height);
set_fill_color(255, 255, 0);
draw_ellipse(0, 0, diameter);
set_fill_color(255); // eyes
draw_ellipse(-67 * size_scale, 67 * size_scale, diameter / 10);
draw_ellipse(67 * size_scale, 67 * size_scale, diameter / 10);
draw_ellipse(-67 * size_scale, 67 * size_scale, diameter / 20);
draw_ellipse(67 * size_scale, 67 * size_scale, diameter / 20);
set_outline_width(3.0); // mouth
draw_ellipse(0, -67 * size_scale, diameter / 2 * size_scale, 90 * size_scale);
return end_drawing_to_image(smooth_filter);
}
friend Image end_drawing_to_image(bool smooth_texture)
End a session of drawing to an image.
void set_fill_color(HexColor color) noexcept
Sets the color used to fill shapes to the specified HexColor.
void no_fill() noexcept
Disables filling geometry.
void begin_drawing_to_image(int image_width, int image_height, bool apply_antialiasing=true)
Redirect all draw command to draw to an image.
void set_outline_width(double line_width) noexcept
Sets the width of the outline used for lines and the border around shapes. All widths are set in unit...
void pop_settings() noexcept
The pop_settings() function restores to the previous style settings and transformations,...
void draw_ellipse(double x, double y, double width, double height=0) noexcept
Draws an ellipse (oval) to the screen.
void push_settings() noexcept
The push_settings() function saves the current drawing style settings and transformations,...

The documentation for this class was generated from the following file: