This repository has been archived by the owner on Dec 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 225
/
SimpleBitmapEffect.cpp
141 lines (116 loc) · 4.47 KB
/
SimpleBitmapEffect.cpp
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//--------------------------------------------------------------------------------------
// SimpleBitmapEffect.cpp
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//
#include "pch.h"
#include "MemoryBuffer.h"
using namespace Platform;
using namespace Windows::Foundation::Collections;
using namespace Windows::Foundation;
using namespace Windows::Devices::Lights;
using namespace Windows::Devices::Lights::Effects;
using namespace Windows::UI;
using namespace Windows::System;
// All Effects use Ticks; below multiplers transform units to Ticks.
constexpr uint32_t c_secondsMultiplier = 10'000'000;
constexpr uint32_t c_millisecondsMultiplier = 10'000;
SimpleBitmapEffect::SimpleBitmapEffect(
Windows::Devices::Lights::LampArray^ lampArray) :
m_flipBitmap(false)
{
m_lampArray = lampArray;
m_allLampIndexes = ref new Platform::Array<int>(lampArray->LampCount);
for (int i = 0; i < lampArray->LampCount; i++)
{
m_allLampIndexes[i] = i;
}
Start();
}
SimpleBitmapEffect::~SimpleBitmapEffect()
{
}
void SimpleBitmapEffect::Start()
{
m_playList = ref new LampArrayEffectPlaylist();
m_playList->RepetitionMode = LampArrayRepetitionMode::Forever;
// This effect will apply to every Lamp on the device.
Platform::Array<int>^ allLampIndexes = ref new Platform::Array<int>(m_lampArray->LampCount);
for (int i = 0; i < m_lampArray->LampCount; i++)
{
allLampIndexes[i] = i;
}
LampArrayBitmapEffect^ bitmapEffect = ref new LampArrayBitmapEffect(m_lampArray, allLampIndexes);
// Update handler is triggered; once the playlist starts the effect, at every UpdateInternal, when duration expires.
bitmapEffect->BitmapRequested += ref new TypedEventHandler<LampArrayBitmapEffect^, LampArrayBitmapRequestedEventArgs^>(this, &SimpleBitmapEffect::UpdateBitmap);
Windows::Foundation::TimeSpan span;
span.Duration = 1000LL * static_cast<long long>(c_secondsMultiplier);
bitmapEffect->Duration = span;
span.Duration = c_secondsMultiplier;
bitmapEffect->UpdateInterval = span;
m_playList = ref new LampArrayEffectPlaylist();
m_playList->RepetitionMode = LampArrayRepetitionMode::Forever;
m_playList->Append(bitmapEffect);
m_playList->Start();
}
void SimpleBitmapEffect::Stop()
{
if (m_playList)
{
m_playList->Stop();
}
}
bool SimpleBitmapEffect::ContainsLampArray(Windows::Devices::Lights::LampArray^ lampArray)
{
return lampArray->DeviceId == m_lampArray->DeviceId;
}
void SimpleBitmapEffect::UpdateBitmap(
LampArrayBitmapEffect^ effect,
LampArrayBitmapRequestedEventArgs^ args)
{
auto bitmap = ref new Windows::Graphics::Imaging::SoftwareBitmap(Windows::Graphics::Imaging::BitmapPixelFormat::Rgba8,
static_cast<int>(effect->SuggestedBitmapSize.Width), static_cast<int>(effect->SuggestedBitmapSize.Height));
{
auto buffer = bitmap->LockBuffer(Windows::Graphics::Imaging::BitmapBufferAccessMode::Write);
{
Windows::Foundation::IMemoryBufferReference^ reference = buffer->CreateReference();
{
Microsoft::WRL::ComPtr<Windows::Foundation::IMemoryBufferByteAccess> bufferByteAccess;
HRESULT result = reinterpret_cast<IInspectable*>(reference)->QueryInterface(IID_PPV_ARGS(&bufferByteAccess));
if (result == S_OK)
{
byte* bitmapBytes;
uint32_t capacity;
bufferByteAccess->GetBuffer(&bitmapBytes, &capacity);
// Bitmap is an array of RGBA8 colors, so color is 4bytes. Array of this size MUST be passed for UpdateBitmap
uint32_t bitmapByteCount = (uint32_t)(effect->SuggestedBitmapSize.Height * effect->SuggestedBitmapSize.Width) * 4;
// Set 'top' half of bitmap to blue, bottom to yellow
// Lamps that are 'in the middle' geometrically will be interpolated by averaging the surrounding colors.
for (uint32_t i = 0; i < bitmapByteCount; i += 4)
{
bool renderBlue = m_flipBitmap ^ (i < bitmapByteCount / 2);
if (renderBlue)
{
// Blue. All colors must be in RGBA8 format.
bitmapBytes[i] = 0x00; // R
bitmapBytes[i + 1] = 0x00; // G
bitmapBytes[i + 2] = 0xFF; // B
bitmapBytes[i + 3] = 0xFF; // A
}
else
{
// Yellow. All colors must be in RGBA8 format.
bitmapBytes[i] = 0xFF; // R
bitmapBytes[i + 1] = 0xFF; // G
bitmapBytes[i + 2] = 0x00; // B
bitmapBytes[i + 3] = 0xFF; // A
}
}
}
}
}
}
m_flipBitmap = !m_flipBitmap;
args->UpdateBitmap(bitmap);
}