void BallRenderer::renderBalls(Engine::SpriteBatch& spriteBatch, const std::vector<Ball>& balls, const glm::mat4& projectionMatrix){ glClearColor(0.0f, 0.0f, 0.0f, 1.0f); if (_program == nullptr){ _program = std::make_unique<Engine::GLSLProgram>(); _program->compileShaders("Shaders/textureShading.vert", "Shaders/textureShading.frag"); _program->addAttribute("vertexPosition"); _program->addAttribute("vertexColor"); _program->addAttribute("vertexUV"); _program->linkShaders(); } _program->use(); spriteBatch.begin(); GLint textureUniform = _program->getUniformLocation("mySampler"); glUniform1i(textureUniform, 0); GLint pUniform = _program->getUniformLocation("P"); glUniformMatrix4fv(pUniform, 1, GL_FALSE, &projectionMatrix[0][0]); for (auto& ball : balls){ const glm::vec4 uvRect(0.0f, 0.0f, 1.0f, 1.0f); const glm::vec4 destRect(ball.position.x - ball.radius, ball.position.y - ball.radius, ball.radius * 2.0f, ball.radius * 2.0f); spriteBatch.draw(destRect, uvRect, ball.textureId, 0.0f, ball.color); } spriteBatch.end(); spriteBatch.renderBatch(); _program->unuse(); }
void RenderComponent::draw(const glm::vec4& posAndSize, Engine::SpriteBatch& spriteBatch) { if (!_visible) return; Engine::Color color = { 255, 255, 255, 255 }; glm::vec4 uv(0.0f, 0.0f, 1.0f, 1.0f); spriteBatch.draw(posAndSize, uv, _texture.id, 1.0f, color); }
void VelocityBallRenderer::renderBalls(Engine::SpriteBatch& spriteBatch, const std::vector<Ball>& balls, const glm::mat4& projectionMatrix){ glClearColor(0.0f, 0.0f, 0.0f, 1.0f); if (_program == nullptr){ _program = std::make_unique<Engine::GLSLProgram>(); _program->compileShaders("Shaders/textureShading.vert", "Shaders/textureShading.frag"); _program->addAttribute("vertexPosition"); _program->addAttribute("vertexColor"); _program->addAttribute("vertexUV"); _program->linkShaders(); } _program->use(); spriteBatch.begin(); GLint textureUniform = _program->getUniformLocation("mySampler"); glUniform1i(textureUniform, 0); GLint pUniform = _program->getUniformLocation("P"); glUniformMatrix4fv(pUniform, 1, GL_FALSE, &projectionMatrix[0][0]); for (auto& ball : balls){ const glm::vec4 uvRect(0.0f, 0.0f, 1.0f, 1.0f); const glm::vec4 destRect(ball.position.x - ball.radius, ball.position.y - ball.radius, ball.radius * 2.0f, ball.radius * 2.0f); Engine::Color color; float mult = 100.0f; GLubyte colorVal = (GLubyte)(glm::clamp(ball.velocity.x * mult, 0.0f, 255.0f)); color.r = 128.0f; color.g = (GLubyte)((ball.position.x / _screenWidth) * 255.0f); color.b = (GLubyte)((ball.position.y / _screenHeight) * 255.0f); color.a = colorVal; spriteBatch.draw(destRect, uvRect, ball.textureId, 0.0f, color); } spriteBatch.end(); spriteBatch.renderBatch(); _program->unuse(); }
void Player::draw(Engine::SpriteBatch& spriteBatch) { glm::vec4 destRect; b2Body* body = m_capsule.getBody(); destRect.x = body->GetPosition().x - m_drawDims.x / 2.0f; destRect.y = body->GetPosition().y - m_capsule.getDimensions().y / 2.0f; destRect.z = m_drawDims.x; destRect.w = m_drawDims.y; int tileIndex; int numTiles; float animSpeed = 0.2f; glm::vec2 velocity; velocity.x = body->GetLinearVelocity().x; velocity.y = body->GetLinearVelocity().y; // Calculate animation if (m_onGround) { if (m_isPunching) { numTiles = 4; tileIndex = 1; if (m_moveState != PlayerMoveState::PUNCHING) { m_moveState = PlayerMoveState::PUNCHING; m_animTime = 0.0f; } } else if (abs(velocity.x) > 1.0f && ((velocity.x > 0 && m_direction > 0) || (velocity.x < 0 && m_direction < 0))) { // Running numTiles = 6; tileIndex = 10; animSpeed = abs(velocity.x) * 0.025f; if (m_moveState != PlayerMoveState::RUNNING) { m_moveState = PlayerMoveState::RUNNING; m_animTime = 0.0f; } } else { // Standing still numTiles = 1; tileIndex = 0; m_moveState = PlayerMoveState::STANDING; } } else { // In the air if (m_isPunching) { numTiles = 1; tileIndex = 18; animSpeed *= 0.25f; if (m_moveState != PlayerMoveState::PUNCHING) { m_moveState = PlayerMoveState::PUNCHING; m_animTime = 0.0f; } } else if (abs(velocity.x) > 10.0f) { numTiles = 1; tileIndex = 10; m_moveState = PlayerMoveState::IN_AIR; } else if (velocity.y <= 0.0f) { // Falling numTiles = 1; tileIndex = 17; m_moveState = PlayerMoveState::IN_AIR; } else { // Rising numTiles = 1; tileIndex = 16; m_moveState = PlayerMoveState::IN_AIR; } } // Increment animation time m_animTime += animSpeed; // Check for punch end if (m_animTime > numTiles) { m_isPunching = false; } // Apply animation tileIndex = tileIndex + (int)m_animTime % numTiles; // Get the uv coordinates from the tile index glm::vec4 uvRect = m_texture.getUVs(tileIndex); // Check direction if (m_direction == -1) { uvRect.x += 1.0f / m_texture.dims.x; uvRect.z *= -1; } // Draw the sprite spriteBatch.draw(destRect, uvRect, m_texture.texture.id, 0.0f, m_color, body->GetAngle()); }