-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbackground.cpp
More file actions
85 lines (64 loc) · 1.61 KB
/
background.cpp
File metadata and controls
85 lines (64 loc) · 1.61 KB
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
76
77
78
79
80
81
82
83
84
85
#include "background.hpp"
#include <SFML\Graphics\RenderWindow.hpp>
#include <SFML\Graphics\RenderTexture.hpp>
#include "particle.hpp"
Background::Background() : width(800), height(600)
{
test_loadmap();
texture.loadFromImage(image);
sprite.setTexture(texture);
}
#include <iostream>
void Background::test_loadmap()
{
// test todo load map
sf::Texture t1;
t1.loadFromFile("media/tree.png");
sf::Sprite s1;
s1.setTexture(t1);
sf::RenderTexture rt;
rt.create(800, 600);
rt.clear(sf::Color::Transparent);
s1.setPosition(0, 400);
rt.draw(s1);
s1.setPosition(450, 400);
rt.draw(s1);
t1.loadFromFile("media/gascan.png");
s1.setPosition(600, 50);
rt.draw(s1);
rt.display();
sf::Texture texture = rt.getTexture();
image = texture.copyToImage();
for (int x = 0; x < 150; x++)
for (int y = 0; y < 150; y++)
image.setPixel(x + 30, y + 100, sf::Color(128, 0, 128));
}
Background::~Background()
{
}
bool Background::isOutBound(short x, short y) const
{
return x < 0 || y < 0 || x >= width || y >= height;
}
void Background::reset()
{
test_loadmap();
}
void Background::setPixel(unsigned short x, unsigned short y, const sf::Color & color)
{
if (!isOutBound(x,y))
image.setPixel(x, y, color);
}
sf::Color Background::getPixel(unsigned short x, unsigned short y) const
{
return isOutBound(x, y) ? sf::Color::Transparent : image.getPixel(x, y);
}
sf::Color Background::getPixel(const sf::Vector2f & pos) const
{
return isOutBound(pos.x, pos.y) ? sf::Color::Transparent : image.getPixel(pos.x, pos.y);
}
void Background::draw(sf::RenderWindow * window)
{
texture.update(image);
window->draw(sprite);
}