Skip to content

Commit 3badb36

Browse files
authored
Merge pull request #122 from ptwohig/buffix/unity-il2cpp
Buffix/unity il2cpp
2 parents b3d68c1 + 335ccb9 commit 3badb36

25 files changed

+1995
-940
lines changed

Example_CPP/Screen_Capture_Example.cpp

+101-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "ScreenCapture.h"
2+
#include "ScreenCapture_C_API.h"
23
#include "internal/SCCommon.h" //DONT USE THIS HEADER IN PRODUCTION CODE!!!! ITS INTERNAL FOR A REASON IT WILL CHANGE!!! ITS HERE FOR TESTS ONLY!!!
34
#include <algorithm>
45
#include <atomic>
@@ -19,6 +20,94 @@
1920
#include "lodepng.h"
2021
/////////////////////////////////////////////////////////////////////////
2122

23+
void TestCopyContiguous()
24+
{
25+
26+
constexpr auto VALID(static_cast<unsigned char>(0xFF));
27+
constexpr auto INVALID(static_cast<unsigned char>(0xFE));
28+
constexpr auto PIXEL_DEPTH(sizeof(SL::Screen_Capture::ImageBGRA));
29+
constexpr unsigned WIDTH(256), HEIGHT(256);
30+
31+
std::vector<SL::Screen_Capture::ImageBGRA> strided;
32+
33+
for (unsigned row(0); row < HEIGHT; ++row)
34+
{
35+
for (unsigned col(0); col < WIDTH; ++col)
36+
{
37+
strided.push_back(SL::Screen_Capture::ImageBGRA{VALID, VALID, VALID, VALID});
38+
}
39+
}
40+
41+
auto bytes = strided.size() * PIXEL_DEPTH;
42+
std::vector<unsigned char> contiguous(bytes, static_cast<unsigned char>(0));
43+
auto image = SL::Screen_Capture::Image{{ 0, 0, WIDTH, HEIGHT }, 0, true, strided.data() };
44+
45+
auto result = SCL_Utility_CopyToContiguous(contiguous.data(), &image);
46+
auto distance = std::distance(contiguous.data(), static_cast<unsigned char*>(result));
47+
48+
if (distance != (WIDTH * HEIGHT * PIXEL_DEPTH)) std::abort();
49+
50+
auto const begin(contiguous.begin()), end(contiguous.end());
51+
52+
for (auto current(begin); current != end; ++current)
53+
{
54+
if (*current != VALID) std::abort();
55+
}
56+
57+
}
58+
59+
void TestCopyNonContiguous()
60+
{
61+
62+
constexpr auto VALID(static_cast<unsigned char>(0xFF));
63+
constexpr auto INVALID(static_cast<unsigned char>(0xFE));
64+
constexpr auto PIXEL_DEPTH(sizeof(SL::Screen_Capture::ImageBGRA));
65+
constexpr unsigned WIDTH(256), HEIGHT(256), PADDING(64), STRIDE_IN_BYTES((WIDTH + PADDING) * PIXEL_DEPTH);
66+
67+
std::vector<SL::Screen_Capture::ImageBGRA> strided;
68+
69+
for (unsigned row(0); row < HEIGHT; ++row)
70+
{
71+
72+
for (unsigned col(0); col < WIDTH; ++col)
73+
{
74+
strided.push_back(SL::Screen_Capture::ImageBGRA{VALID, VALID, VALID, VALID});
75+
}
76+
77+
for (unsigned pad(0); pad < PADDING; ++pad)
78+
{
79+
strided.push_back(SL::Screen_Capture::ImageBGRA{INVALID, INVALID, INVALID, INVALID});
80+
}
81+
82+
}
83+
84+
auto bytes = strided.size() * PIXEL_DEPTH;
85+
std::vector<unsigned char> contiguous(bytes, static_cast<unsigned char>(0));
86+
auto image = SL::Screen_Capture::Image{{ 0, 0, WIDTH, HEIGHT }, STRIDE_IN_BYTES, false, strided.data() };
87+
88+
auto result = SCL_Utility_CopyToContiguous(contiguous.data(), &image);
89+
auto distance = std::distance(contiguous.data(), static_cast<unsigned char*>(result));
90+
91+
// Ensures that the pointer incremented by only the amount written.
92+
if (distance != (WIDTH * HEIGHT * PIXEL_DEPTH)) std::abort();
93+
94+
auto const begin(contiguous.begin());
95+
auto contiguousEnd(begin), absoluteEnd(contiguous.end());
96+
97+
std::advance(contiguousEnd, WIDTH * HEIGHT * PIXEL_DEPTH);
98+
99+
for (auto current(begin); current != contiguousEnd; ++current)
100+
{
101+
if (*current != VALID) std::abort();
102+
}
103+
104+
for (auto current(contiguousEnd); current != absoluteEnd; ++current)
105+
{
106+
if (*current != 0) std::abort();
107+
}
108+
109+
}
110+
22111
void ExtractAndConvertToRGBA(const SL::Screen_Capture::Image &img, unsigned char *dst, size_t dst_size)
23112
{
24113
assert(dst_size >= static_cast<size_t>(SL::Screen_Capture::Width(img) * SL::Screen_Capture::Height(img) * sizeof(SL::Screen_Capture::ImageBGRA)));
@@ -73,7 +162,7 @@ void createframegrabber()
73162
return mons;
74163
})
75164
->onFrameChanged([&](const SL::Screen_Capture::Image &img, const SL::Screen_Capture::Monitor &monitor) {
76-
//std::cout << "Difference detected! " << img.Bounds << std::endl;
165+
// std::cout << "Difference detected! " << img.Bounds << std::endl;
77166
// Uncomment the below code to write the image to disk for debugging
78167
/*
79168
auto r = realcounter.fetch_add(1);
@@ -135,18 +224,17 @@ void createpartialframegrabber()
135224
framgrabber =
136225
SL::Screen_Capture::CreateCaptureConfiguration([]() {
137226
auto mons = SL::Screen_Capture::GetMonitors();
227+
auto newmons = std::vector<SL::Screen_Capture::Monitor>();
138228
std::cout << "Library is requesting the list of monitors to capture!" << std::endl;
139229
for (auto &m : mons) {
140-
// capture just a 512x512 square... USERS SHOULD MAKE SURE bounds are
141-
// valid!!!!
142-
SL::Screen_Capture::OffsetX(m, SL::Screen_Capture::OffsetX(m) + 512);
143-
SL::Screen_Capture::OffsetY(m, SL::Screen_Capture::OffsetY(m) + 512);
144-
SL::Screen_Capture::Height(m, 512);
145-
SL::Screen_Capture::Width(m, 512);
146-
147-
std::cout << m << std::endl;
230+
if (SL::Screen_Capture::Height(m) >= 512 * 2 && SL::Screen_Capture::Width(m) >= 512 * 2) {
231+
SL::Screen_Capture::Height(m, 512);
232+
SL::Screen_Capture::Width(m, 512);
233+
std::cout << m << std::endl;
234+
newmons.push_back(m);
235+
}
148236
}
149-
return mons;
237+
return newmons;
150238
})
151239
->onFrameChanged([&](const SL::Screen_Capture::Image &img, const SL::Screen_Capture::Monitor &monitor) {
152240
// Uncomment the below code to write the image to disk for debugging
@@ -279,6 +367,9 @@ int main()
279367
std::cout << "Starting Capture Demo/Test" << std::endl;
280368
std::cout << "Testing captured monitor bounds check" << std::endl;
281369

370+
TestCopyContiguous();
371+
TestCopyNonContiguous();
372+
282373
auto goodmonitors = SL::Screen_Capture::GetMonitors();
283374
for (auto &m : goodmonitors) {
284375
std::cout << m << std::endl;

0 commit comments

Comments
 (0)