#include "MyPongScene.h" #include #include #include #define OUTLINE_THICKNESS 25.0f #define OUTLINE_OFFSET 25.0f #define OUTLINE_INWARDS OUTLINE_OFFSET + OUTLINE_THICKNESS #define PADDLE_THICKNESS 25.0f #define PADDLE_HEIGHT 150.0f #define BALL_THICKNESS 25.0f #define HALF_BALL_THICKNESS 12.5f #define PADDLE_SPEED 5.0f MyPongScene::MyPongScene(Application& application) : Scene(application), m_outline_column(meshgenerator::gen_rect_p(DRAW_TRIANGLES, m_screen_size.x - 2 * OUTLINE_THICKNESS, OUTLINE_THICKNESS), DRAW_TRIANGLES), m_outline_row(meshgenerator::gen_rect_p(DRAW_TRIANGLES, OUTLINE_THICKNESS, m_screen_size.y - 2 * OUTLINE_THICKNESS), DRAW_TRIANGLES), m_ball(meshgenerator::gen_rect_p(DRAW_TRIANGLES, BALL_THICKNESS, BALL_THICKNESS), DRAW_TRIANGLES), m_paddle(meshgenerator::gen_rect_p(DRAW_TRIANGLES, PADDLE_THICKNESS, PADDLE_HEIGHT), DRAW_TRIANGLES), m_outline_column_batch(&m_outline_column, 2), m_outline_row_batch(&m_outline_row, 2), m_ball_batch(&m_ball, 1), m_paddle_batch(&m_paddle, 2), m_outline_top_pose(glm::vec3(0.0f, (m_screen_size.y - OUTLINE_THICKNESS) / 2.0f - OUTLINE_OFFSET, 0.0f)), m_outline_bottom_pose(glm::vec3(0.0f, -((m_screen_size.y - OUTLINE_THICKNESS) / 2.0f - OUTLINE_OFFSET), 0.0f)), m_outline_left_pose(glm::vec3(-((m_screen_size.x - OUTLINE_THICKNESS) / 2.0f - OUTLINE_OFFSET), 0.0f, 0.0f)), m_outline_right_pose(glm::vec3((m_screen_size.x - OUTLINE_THICKNESS) / 2.0f - OUTLINE_OFFSET, 0.0f, 0.0f)), m_ball_pose(glm::vec3(0.0f, 0.0f, 0.0f)), m_paddle_left_pose(glm::vec3(-(m_screen_size.x / 2.0f - OUTLINE_INWARDS - 3.5f * PADDLE_THICKNESS), 0.0f, 0.0f)), m_paddle_right_pose(glm::vec3(m_screen_size.x / 2.0f - OUTLINE_INWARDS - 3.5f * PADDLE_THICKNESS, 0.0f, 0.0f)), m_camera(m_screen_size) { m_pipeline.add_batch(&m_outline_column_batch); m_pipeline.add_batch(&m_outline_row_batch); m_pipeline.add_batch(&m_ball_batch); m_pipeline.add_batch(&m_paddle_batch); m_pipeline.set_camera(&m_camera); } void MyPongScene::init() { // Batches m_outline_column_batch.init(); m_outline_row_batch.init(); m_ball_batch.init(); m_paddle_batch.init(); // Set these once here since they will never change m_outline_column_batch.reset_rendered(); m_outline_column_batch.add_rendered(m_outline_top_pose); m_outline_column_batch.add_rendered(m_outline_bottom_pose); m_outline_row_batch.reset_rendered(); m_outline_row_batch.add_rendered(m_outline_left_pose); m_outline_row_batch.add_rendered(m_outline_right_pose); // Ball m_ball_speed = 250.0f; m_ball_direction = glm::normalize(glm::vec2(1.0f, 1.0f)); } void MyPongScene::update(float delta_time, clock_t clock) { m_ball_pose.translate(glm::vec3(m_ball_direction * m_ball_speed * delta_time, 0.0f)); vec3 ball_position = m_ball_pose.get_position(); // Bounce Top and Bottom if (ball_position.y + HALF_BALL_THICKNESS + OUTLINE_INWARDS > m_screen_size.y / 2.0f) { m_ball_direction.y = -glm::abs(m_ball_direction.y); } if (ball_position.y - HALF_BALL_THICKNESS - OUTLINE_INWARDS < -m_screen_size.y / 2.0f) { m_ball_direction.y = glm::abs(m_ball_direction.y); } // Bounce Left Right (temp) if (ball_position.x + HALF_BALL_THICKNESS + OUTLINE_INWARDS > m_screen_size.x / 2.0f) { m_ball_direction.x = -glm::abs(m_ball_direction.x); } if (ball_position.x - HALF_BALL_THICKNESS - OUTLINE_INWARDS < -m_screen_size.x / 2.0f) { m_ball_direction.x = glm::abs(m_ball_direction.x); } m_ball_batch.reset_rendered(); m_ball_batch.add_rendered(m_ball_pose); m_paddle_batch.reset_rendered(); m_paddle_batch.add_rendered(m_paddle_left_pose); m_paddle_batch.add_rendered(m_paddle_right_pose); } void MyPongScene::prerender() { m_camera.prerender(); m_outline_column_batch.prerender(); m_outline_row_batch.prerender(); m_ball_batch.prerender(); m_paddle_batch.prerender(); } void MyPongScene::render() { glutil::clear_screen(); m_pipeline.render(); }