-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas.h
75 lines (61 loc) · 1.57 KB
/
canvas.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#ifndef _CANVAS_H
#define _CANVAS_H
#include <SFML/Graphics/Color.hpp>
#include <SFML/Graphics/Image.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Texture.hpp>
/*
* The Canvas class.
*/
class Canvas {
// Canvas size
int width;
int height;
// Image
sf::Image myImage;
public:
/*
* Canvas
*
* INPUT:
*
*
* DESCRIPTION:
*
*/
Canvas( int w , int h ) : width(w), height(h) {
myImage.create( w, h, sf::Color(0, 0, 0) );
}
void clear() {
// set all pixels to black
for (int i = 0; i < width; ++i)
for (int j = 0; j < height; ++j)
myImage.setPixel (i, j, sf::Color(0, 0, 0));
}
// Creating myImage -> Texture -> Sprite
void draw( sf::RenderWindow &window ) {
sf::Texture tex;
tex.loadFromImage( myImage );
sf::Sprite toDraw;
toDraw.setTexture( tex );
// Add your image as a sprite
window.draw( toDraw );
}
void setPixel ( int x, int y, double r, double g, double b ) {
// Wrap color values if they are over 1
if (r > 1.0) r = 1.0;
if (g > 1.0) g = 1.0;
if (b > 1.0) b = 1.0;
// RGB values
unsigned int R = (unsigned int)(r * 255);
unsigned int G = (unsigned int)(g * 255);
unsigned int B = (unsigned int)(b * 255);
// Set color
myImage.setPixel (x, y, sf::Color (R, G, B));
}
void savePicture() {
myImage.saveToFile("test.png");
}
};
#endif