Debian Codegraph @

src/pingpong.cpp

raw
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
12 #include <cstddef>
13 #include <vector>
14
15 #define NUMQUADS 3
16
17 #define TOPANDBOTTOM 200.0f
18 #define PADDLEPOSX 20.0f
19 #define PADDLEMAXSPEED 500.0f
20
21 ////////////////////////////////////////////////////////////////////////////
22 //
23 CPingPong::CPingPong()
24 {
25 #ifndef WIN32
26 std::string fraqShader = kodi::addon::GetAddonPath("resources/shaders/" GL_TYPE_STRING "/frag.glsl");
27 std::string vertShader = kodi::addon::GetAddonPath("resources/shaders/" GL_TYPE_STRING "/vert.glsl");
28 if (!LoadShaderFiles(vertShader, fraqShader) || !CompileAndLink())
29 return;
30
31 glGenBuffers(1, &m_vertexVBO);
32 glGenBuffers(1, &m_indexVBO);
33 #endif
34 }
35
36 ////////////////////////////////////////////////////////////////////////////
37 //
38 CPingPong::~CPingPong()
39 {
40 #ifndef WIN32
41 glBindBuffer(GL_ARRAY_BUFFER, 0);
42 glDeleteBuffers(1, &m_vertexVBO);
43 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
44 glDeleteBuffers(1, &m_indexVBO);
45 #endif
46 }
47
48 ////////////////////////////////////////////////////////////////////////////
49 //
50 bool CPingPong::RestoreDevice(CRenderD3D* render)
51 {
52 m_Width = render->m_Width;
53 m_Height = render->m_Height;
54 m_Paddle[0].m_Pos.Set( PADDLEPOSX, render->m_Height/2, 0.0f);
55 m_Paddle[1].m_Pos.Set(render->m_Width-PADDLEPOSX, render->m_Height/2, 0.0f);
56
57 m_Ball.m_Pos.Set((render->m_Width-2*PADDLEPOSX)/2.0+PADDLEPOSX, render->m_Height/2, 0.0f);
58 topy = 7*render->m_Height/8;
59 bottomy = render->m_Height/8;
60
61 #ifndef WIN32
62 m_projMat = glm::ortho(0.0f, float(m_Width), float(m_Height), 0.0f);
63 #endif
64 return true;
65 }
66
67 ////////////////////////////////////////////////////////////////////////////
68 //
69 void CPingPong::InvalidateDevice(CRenderD3D* render)
70 {
71 render->Release();
72 }
73
74 ////////////////////////////////////////////////////////////////////////////
75 //
76 void CPingPong::Update(f32 dt)
77 {
78 // The paddle 'ai'. If you now can call it that
79 for (int i = 0; i < 2; i++)
80 {
81 f32 speed = 0.5f;
82 // If the ball is moving toward us then meet up with it (quickly)
83 if (DotProduct(m_Ball.m_Vel, m_Paddle[i].m_Pos) > 0.0f)
84 {
85 speed = 1.0f;
86 }
87
88 if (m_Ball.m_Pos.y > m_Paddle[i].m_Pos.y)
89 m_Paddle[i].m_Pos.y += PADDLEMAXSPEED*dt*speed;
90 else
91 m_Paddle[i].m_Pos.y -= PADDLEMAXSPEED*dt*speed;
92 }
93
94 // Perform collisions
95 if (m_Ball.m_Pos.y > topy)
96 m_Ball.m_Vel.y *= -1.0f;
97 if (m_Ball.m_Pos.y < bottomy)
98 m_Ball.m_Vel.y *= -1.0f;
99
100 if ((m_Ball.m_Pos.x-m_Ball.m_Size.x) < (m_Paddle[0].m_Pos.x+m_Paddle[0].m_Size.x))
101 m_Ball.m_Vel.x *= -1.0f;
102 if ((m_Ball.m_Pos.x+m_Ball.m_Size.x) > (m_Paddle[1].m_Pos.x-m_Paddle[1].m_Size.x))
103 m_Ball.m_Vel.x *= -1.0f;
104
105 m_Ball.m_Pos.x += m_Ball.m_Vel.x*dt;
106 m_Ball.m_Pos.y += m_Ball.m_Vel.y*dt;
107 }
108
109 ////////////////////////////////////////////////////////////////////////////
110 //
111 bool CPingPong::Draw(CRenderD3D* render)
112 {
113 // Fill in the vertex buffers with the quads
114 std::vector<TRenderVertex> vert(4*4);
115 TRenderVertex* vert2 = AddQuad(&vert[0], m_Ball.m_Pos, m_Ball.m_Size, m_Ball.m_Col);
116 vert2 = AddQuad(vert2, m_Paddle[0].m_Pos, m_Paddle[0].m_Size, m_Paddle[0].m_Col);
117 vert2 = AddQuad(vert2, m_Paddle[1].m_Pos, m_Paddle[1].m_Size, m_Paddle[1].m_Col);
118
119 #ifndef WIN32
120 EnableShader();
121
122 GLubyte idx[3*8];
123 for (size_t j = 0; j < 4; ++j)
124 {
125 idx[6*j] = 4*j;
126 idx[6*j+1] = 4*j+1;
127 idx[6*j+2] = 4*j+2;
128 idx[6*j+3] = 4*j+2;
129 idx[6*j+4] = 4*j+3;
130 idx[6*j+5] = 4*j;
131 }
132
133 glBindBuffer(GL_ARRAY_BUFFER, m_vertexVBO);
134 glBufferData(GL_ARRAY_BUFFER, sizeof(TRenderVertex)*12, &vert[0], GL_STATIC_DRAW);
135
136 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexVBO);
137 glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLubyte)*24, idx, GL_STATIC_DRAW);
138
139 glVertexAttribPointer(m_aPosition, 4, GL_FLOAT, 0, sizeof(TRenderVertex), BUFFER_OFFSET(offsetof(TRenderVertex, pos)));
140 glVertexAttribPointer(m_aColor, 4, GL_FLOAT, 0, sizeof(TRenderVertex), BUFFER_OFFSET(offsetof(TRenderVertex, col)));
141
142 glEnableVertexAttribArray(m_aPosition);
143 glEnableVertexAttribArray(m_aColor);
144
145 glDrawElements(GL_TRIANGLES, 24, GL_UNSIGNED_BYTE, 0);
146
147 glDisableVertexAttribArray(m_aPosition);
148 glDisableVertexAttribArray(m_aColor);
149
150 DisableShader();
151 #else
152 render->DrawQuad(&vert[0]);
153 render->DrawQuad(&vert[4]);
154 render->DrawQuad(&vert[8]);
155 #endif
156 return true;
157 }
158
159 ////////////////////////////////////////////////////////////////////////////
160 // Adds a quad to a vertex buffer
161 //
162 TRenderVertex* CPingPong::AddQuad(TRenderVertex* vert, const CVector& pos, const CVector& size, const CRGBA& col)
163 {
164 vert->pos = CVector(pos.x-size.x, pos.y+size.y, 0.0f); vert->col = col; vert++;
165 vert->pos = CVector(pos.x-size.x, pos.y-size.y, 0.0f); vert->col = col; vert++;
166 vert->pos = CVector(pos.x+size.x, pos.y+size.y, 0.0f); vert->col = col; vert++;
167 vert->pos = CVector(pos.x+size.x, pos.y-size.y, 0.0f); vert->col = col; vert++;
168 return vert;
169 }
170
171
172 #ifndef WIN32
173 void CPingPong::OnCompiledAndLinked()
174 {
175 m_uProjMatrix = glGetUniformLocation(ProgramHandle(), "u_modelViewProjectionMatrix");
176 m_aPosition = glGetAttribLocation(ProgramHandle(), "a_position");
177 m_aColor = glGetAttribLocation(ProgramHandle(), "a_color");
178 }
179
180 bool CPingPong::OnEnabled()
181 {
182 glUniformMatrix4fv(m_uProjMatrix, 1, GL_FALSE, glm::value_ptr(m_projMat));
183 return true;
184 }
185 #endif