-
Notifications
You must be signed in to change notification settings - Fork 0
/
CPicture.cxx
119 lines (101 loc) · 2.45 KB
/
CPicture.cxx
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "CPicture.hxx"
#include "CGraph.hxx"
#include "IModificator.hxx"
#include "TimFunction.hxx"
#include <cstring>
#include <cmath>
#include <thread>
#include <vector>
#define SIZE 600
#define HALF_SIZE SIZE / 2
CPicture::CPicture()
{
_NewData = new char*[SIZE];
for (int i = 0; i < SIZE; i++){
_NewData[i] = new char[SIZE];
memset(_NewData[i],0,SIZE);
};
_OldData = new char*[SIZE];
for (int i = 0; i < SIZE; i++){
_OldData[i] = new char[SIZE];
memset(_OldData[i],0,SIZE);
};
_AvailableThreads = std::thread::hardware_concurrency();
FunctionCount = 0;
};
void CPicture::Init()
{
_InitBackground();
}
CPicture::~CPicture()
{
for (int i = 0; i < SIZE; i++)
delete [] _NewData[i];
delete [] _NewData;
for (int i = 0; i < SIZE; i++)
delete [] _OldData[i];
delete [] _OldData;
};
TPictureData CPicture::GetData()
{
return _OldData;
};
void CPicture::ThreadUpdate(int start, int end)
{
for (int i = start; i < end; i++)
for (int j = 0; j < SIZE; j++)
for (auto mod : _Modificators)
if (_Function->Function(mod->GetTransformX(i - HALF_SIZE, j - HALF_SIZE),
mod->GetTransformY(i - HALF_SIZE, j - HALF_SIZE)))
{
//_NewData[i][j] = 15;
_Graph->ApplyGraphToAr(i, j, _NewData, _OldData);
}
};
void CPicture::Update()
{
std::vector<thread> threads;
for (int i = 0; i < _AvailableThreads; i++)
threads.push_back(std::thread(&CPicture::ThreadUpdate, this, SIZE / _AvailableThreads * i, SIZE / _AvailableThreads * (i + 1)));
for (auto &th : threads)
th.join();
_SwapDataArrays();
};
void CPicture::ReadBackgroundFunctions(fstream& Stream){
Stream >> FunctionCount;
for (int i = 0; i < FunctionCount; i++)
{
IBackgroundFunction* temp = new EllipseFunction();
temp->ReadFromStream(Stream);
_BackgroundFuncs.push_back(temp);
}
};
void CPicture::_InitBackground()
{
for (int i = 0; i < SIZE; i++)
for (int j = 0; j < SIZE; j++)
for (int k = 0; k < FunctionCount; k++)
if (_BackgroundFuncs[k]->Function(i, j))
_NewData[i][j] = _BackgroundFuncs[k]->Color;
for (auto func : _BackgroundFuncs)
delete func;
_BackgroundFuncs.clear();
_SwapDataArrays();
};
void CPicture::_SwapDataArrays()
{
for (int i = 0; i < SIZE; i++)
memcpy(_OldData[i], _NewData[i], SIZE);
};
void CPicture::SetFunction(IFunction *Function)
{
_Function = Function;
};
void CPicture::SetGraph(CGraph* Graph)
{
_Graph = Graph;
};
void CPicture::AddModificator(IModificator* Modificator)
{
_Modificators.push_back(Modificator);
};