| 1 |
/* |
| 2 |
* Copyright (C) 2005-2021 Team Kodi (https://kodi.tv) |
| 3 |
* Copyright (C) 2005 Joakim Eriksson <[email protected]> |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSE.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "main.h" |
| 10 |
#include "pingpong.h" |
| 11 |
#include "timer.h" |
| 12 |
|
| 13 |
#include <kodi/addon-instance/Screensaver.h> |
| 14 |
#include <time.h> |
| 15 |
#include <string.h> |
| 16 |
|
| 17 |
class ATTR_DLL_LOCAL CScreensaverPingPong |
| 18 |
: public kodi::addon::CAddonBase, |
| 19 |
public kodi::addon::CInstanceScreensaver |
| 20 |
{ |
| 21 |
public: |
| 22 |
CScreensaverPingPong(); |
| 23 |
|
| 24 |
virtual bool Start() override; |
| 25 |
virtual void Stop() override; |
| 26 |
virtual void Render() override; |
| 27 |
|
| 28 |
private: |
| 29 |
CPingPong m_pingPong; |
| 30 |
CTimer* m_timer; |
| 31 |
CRenderD3D m_render; |
| 32 |
CRGBA m_col[3]; |
| 33 |
float m_ballspeed[2]; |
| 34 |
}; |
| 35 |
|
| 36 |
//////////////////////////////////////////////////////////////////////////// |
| 37 |
// Kodi has loaded us into memory, we should set our core values |
| 38 |
// here and load any settings we may have from our config file |
| 39 |
// |
| 40 |
CScreensaverPingPong::CScreensaverPingPong() |
| 41 |
: m_timer(nullptr) |
| 42 |
{ |
| 43 |
static const float C[5][3] = {{1.0, 1.0, 1.0}, |
| 44 |
{0.5, 0.5, 0.5}, |
| 45 |
{1.0, 0.0, 0.0}, |
| 46 |
{0.0, 1.0, 0.0}, |
| 47 |
{0.0, 0.0, 1.0}}; |
| 48 |
int c = kodi::addon::GetSettingInt("paddle1"); |
| 49 |
if (c < 5) |
| 50 |
m_col[0].Set(C[c][0], C[c][1], C[c][2], 1.0); |
| 51 |
c = kodi::addon::GetSettingInt("paddle2"); |
| 52 |
if (c < 5) |
| 53 |
m_col[1].Set(C[c][0], C[c][1], C[c][2], 1.0); |
| 54 |
c = kodi::addon::GetSettingInt("ball"); |
| 55 |
if (c < 5) |
| 56 |
m_col[2].Set(C[c][0], C[c][1], C[c][2], 1.0); |
| 57 |
|
| 58 |
m_ballspeed[0] = kodi::addon::GetSettingFloat("ballspeedx"); |
| 59 |
m_ballspeed[1] = kodi::addon::GetSettingFloat("ballspeedy"); |
| 60 |
|
| 61 |
m_render.m_Width = Width(); |
| 62 |
m_render.m_Height = Height(); |
| 63 |
m_render.Init(Device()); |
| 64 |
} |
| 65 |
|
| 66 |
//////////////////////////////////////////////////////////////////////////// |
| 67 |
// Kodi tells us we should get ready to start rendering. This function |
| 68 |
// is called once when the screensaver is activated by Kodi. |
| 69 |
// |
| 70 |
bool CScreensaverPingPong::Start() |
| 71 |
{ |
| 72 |
srand(time(NULL)); |
| 73 |
|
| 74 |
m_pingPong.m_Paddle[0].m_Col = m_col[0]; |
| 75 |
m_pingPong.m_Paddle[1].m_Col = m_col[1]; |
| 76 |
m_pingPong.m_Ball.m_Col = m_col[2]; |
| 77 |
m_pingPong.m_Ball.m_Vel.Set(m_ballspeed[0]*m_render.m_Width, m_ballspeed[1]*m_render.m_Height, 0.0); |
| 78 |
|
| 79 |
m_timer = new CTimer(); |
| 80 |
m_timer->Init(); |
| 81 |
if (!m_pingPong.RestoreDevice(&m_render)) |
| 82 |
{ |
| 83 |
Stop(); |
| 84 |
return false; |
| 85 |
} |
| 86 |
|
| 87 |
return true; |
| 88 |
} |
| 89 |
|
| 90 |
//////////////////////////////////////////////////////////////////////////// |
| 91 |
// Kodi tells us to stop the screensaver we should free any memory and release |
| 92 |
// any resources we have created. |
| 93 |
// |
| 94 |
void CScreensaverPingPong::Stop() |
| 95 |
{ |
| 96 |
m_pingPong.InvalidateDevice(&m_render); |
| 97 |
SAFE_DELETE(m_timer); |
| 98 |
} |
| 99 |
|
| 100 |
//////////////////////////////////////////////////////////////////////////// |
| 101 |
// Kodi tells us to render a frame of our screensaver. This is called on |
| 102 |
// each frame render in Kodi, you should render a single frame only - the DX |
| 103 |
// device will already have been cleared. |
| 104 |
// |
| 105 |
void CScreensaverPingPong::Render() |
| 106 |
{ |
| 107 |
m_render.Begin(); |
| 108 |
m_timer->Update(); |
| 109 |
m_pingPong.Update(m_timer->GetDeltaTime()); |
| 110 |
m_pingPong.Draw(&m_render); |
| 111 |
} |
| 112 |
|
| 113 |
#ifdef WIN32 |
| 114 |
const BYTE PixelShader[] = |
| 115 |
{ |
| 116 |
68, 88, 66, 67, 18, 124, |
| 117 |
182, 35, 30, 142, 196, 211, |
| 118 |
95, 130, 91, 204, 99, 13, |
| 119 |
249, 8, 1, 0, 0, 0, |
| 120 |
124, 1, 0, 0, 4, 0, |
| 121 |
0, 0, 48, 0, 0, 0, |
| 122 |
124, 0, 0, 0, 188, 0, |
| 123 |
0, 0, 72, 1, 0, 0, |
| 124 |
65, 111, 110, 57, 68, 0, |
| 125 |
0, 0, 68, 0, 0, 0, |
| 126 |
0, 2, 255, 255, 32, 0, |
| 127 |
0, 0, 36, 0, 0, 0, |
| 128 |
0, 0, 36, 0, 0, 0, |
| 129 |
36, 0, 0, 0, 36, 0, |
| 130 |
0, 0, 36, 0, 0, 0, |
| 131 |
36, 0, 0, 2, 255, 255, |
| 132 |
31, 0, 0, 2, 0, 0, |
| 133 |
0, 128, 0, 0, 15, 176, |
| 134 |
1, 0, 0, 2, 0, 8, |
| 135 |
15, 128, 0, 0, 228, 176, |
| 136 |
255, 255, 0, 0, 83, 72, |
| 137 |
68, 82, 56, 0, 0, 0, |
| 138 |
64, 0, 0, 0, 14, 0, |
| 139 |
0, 0, 98, 16, 0, 3, |
| 140 |
242, 16, 16, 0, 1, 0, |
| 141 |
0, 0, 101, 0, 0, 3, |
| 142 |
242, 32, 16, 0, 0, 0, |
| 143 |
0, 0, 54, 0, 0, 5, |
| 144 |
242, 32, 16, 0, 0, 0, |
| 145 |
0, 0, 70, 30, 16, 0, |
| 146 |
1, 0, 0, 0, 62, 0, |
| 147 |
0, 1, 73, 83, 71, 78, |
| 148 |
132, 0, 0, 0, 4, 0, |
| 149 |
0, 0, 8, 0, 0, 0, |
| 150 |
104, 0, 0, 0, 0, 0, |
| 151 |
0, 0, 1, 0, 0, 0, |
| 152 |
3, 0, 0, 0, 0, 0, |
| 153 |
0, 0, 15, 0, 0, 0, |
| 154 |
116, 0, 0, 0, 0, 0, |
| 155 |
0, 0, 0, 0, 0, 0, |
| 156 |
3, 0, 0, 0, 1, 0, |
| 157 |
0, 0, 15, 15, 0, 0, |
| 158 |
122, 0, 0, 0, 0, 0, |
| 159 |
0, 0, 0, 0, 0, 0, |
| 160 |
3, 0, 0, 0, 2, 0, |
| 161 |
0, 0, 3, 0, 0, 0, |
| 162 |
122, 0, 0, 0, 1, 0, |
| 163 |
0, 0, 0, 0, 0, 0, |
| 164 |
3, 0, 0, 0, 2, 0, |
| 165 |
0, 0, 12, 0, 0, 0, |
| 166 |
83, 86, 95, 80, 79, 83, |
| 167 |
73, 84, 73, 79, 78, 0, |
| 168 |
67, 79, 76, 79, 82, 0, |
| 169 |
84, 69, 88, 67, 79, 79, |
| 170 |
82, 68, 0, 171, 79, 83, |
| 171 |
71, 78, 44, 0, 0, 0, |
| 172 |
1, 0, 0, 0, 8, 0, |
| 173 |
0, 0, 32, 0, 0, 0, |
| 174 |
0, 0, 0, 0, 0, 0, |
| 175 |
0, 0, 3, 0, 0, 0, |
| 176 |
0, 0, 0, 0, 15, 0, |
| 177 |
0, 0, 83, 86, 95, 84, |
| 178 |
65, 82, 71, 69, 84, 0, |
| 179 |
171, 171 |
| 180 |
}; |
| 181 |
#endif |
| 182 |
|
| 183 |
void CRenderD3D::Init(void* pContext) |
| 184 |
{ |
| 185 |
#ifdef WIN32 |
| 186 |
m_pContext = reinterpret_cast<ID3D11DeviceContext*>(pContext); |
| 187 |
ID3D11Device* pDevice = nullptr; |
| 188 |
m_pContext->GetDevice(&pDevice); |
| 189 |
|
| 190 |
CD3D11_BUFFER_DESC vbDesc(sizeof(TRenderVertex) * 5, D3D11_BIND_VERTEX_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE); |
| 191 |
pDevice->CreateBuffer(&vbDesc, nullptr, &m_pVBuffer); |
| 192 |
|
| 193 |
pDevice->CreatePixelShader(PixelShader, sizeof(PixelShader), nullptr, &m_pPShader); |
| 194 |
|
| 195 |
SAFE_RELEASE(pDevice); |
| 196 |
#endif |
| 197 |
} |
| 198 |
|
| 199 |
void CRenderD3D::Begin(void) |
| 200 |
{ |
| 201 |
#ifdef WIN32 |
| 202 |
m_pContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP); |
| 203 |
UINT strides = sizeof(TRenderVertex), offsets = 0; |
| 204 |
m_pContext->IASetVertexBuffers(0, 1, &m_pVBuffer, &strides, &offsets); |
| 205 |
m_pContext->PSSetShader(m_pPShader, NULL, 0); |
| 206 |
#endif |
| 207 |
} |
| 208 |
|
| 209 |
void CRenderD3D::DrawQuad(TRenderVertex* verts) |
| 210 |
{ |
| 211 |
#ifdef WIN32 |
| 212 |
TRenderVertex buff[] = { verts[0], verts[1], verts[2], verts[3] }; |
| 213 |
D3D11_MAPPED_SUBRESOURCE res = {}; |
| 214 |
if (SUCCEEDED(m_pContext->Map(m_pVBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &res))) |
| 215 |
{ |
| 216 |
memcpy(res.pData, buff, sizeof(buff)); |
| 217 |
m_pContext->Unmap(m_pVBuffer, 0); |
| 218 |
} |
| 219 |
m_pContext->Draw(4, 0); |
| 220 |
#endif |
| 221 |
} |
| 222 |
|
| 223 |
void CRenderD3D::Release(void) |
| 224 |
{ |
| 225 |
#ifdef WIN32 |
| 226 |
SAFE_RELEASE(m_pPShader); |
| 227 |
SAFE_RELEASE(m_pVBuffer); |
| 228 |
#endif |
| 229 |
} |
| 230 |
|
| 231 |
ADDONCREATOR(CScreensaverPingPong); |