diff --git a/CharcoalBuiltin/MeshGenerator.h b/CharcoalBuiltin/MeshGenerator.h index 7b8011b..47c4035 100644 --- a/CharcoalBuiltin/MeshGenerator.h +++ b/CharcoalBuiltin/MeshGenerator.h @@ -650,6 +650,79 @@ namespace charcoal // TODO: Mesh* gen_cube(const DrawMode& draw_mode, float width, float height, float depth); + template + Mesh* gen_rect_p(const DrawMode& draw_mode, float width, float height, float offset_x = 0.0f, float offset_y = 0.0f) + { + float half_width = width / 2.0f; + float half_height = height / 2.0f; + + Mesh* mesh; + + Position pos0(offset_x - half_width, offset_y - half_height, 0.0f); + Position pos1(offset_x - half_width, offset_y + half_height, 0.0f); + Position pos2(offset_x + half_width, offset_y - half_height, 0.0f); + Position pos3(offset_x + half_width, offset_y + half_height, 0.0f); + + switch (draw_mode) + { + case DrawMode::DRAW_TRIANGLES: + mesh = MeshFactory::create_mesh(4, 6); + + mesh->vertices[0].set_position(pos0); + mesh->vertices[1].set_position(pos1); + mesh->vertices[2].set_position(pos2); + mesh->vertices[3].set_position(pos3); + + mesh->indices[0] = 1; + mesh->indices[1] = 3; + mesh->indices[2] = 0; + mesh->indices[3] = 0; + mesh->indices[4] = 3; + mesh->indices[5] = 2; + + return mesh; + case DrawMode::DRAW_TRIANGLE_STRIP: + mesh = MeshFactory::create_mesh(4, 4); + + mesh->vertices[0].set_position(pos0); + mesh->vertices[1].set_position(pos1); + mesh->vertices[2].set_position(pos2); + mesh->vertices[3].set_position(pos3); + + mesh->indices[0] = 1; + mesh->indices[1] = 3; + mesh->indices[2] = 0; + mesh->indices[3] = 2; + + return mesh; + case DrawMode::DRAW_TRIANGLE_FAN: + mesh = MeshFactory::create_mesh(4, 4); + + mesh->vertices[0].set_position(pos0); + mesh->vertices[1].set_position(pos1); + mesh->vertices[2].set_position(pos2); + mesh->vertices[3].set_position(pos3); + + mesh->indices[0] = 1; + mesh->indices[1] = 3; + mesh->indices[2] = 2; + mesh->indices[3] = 0; + + return mesh; + case DrawMode::DRAW_POINTS: // TODO: Point, lines, line_strip, line_loop + case DrawMode::DRAW_LINES: + case DrawMode::DRAW_LINE_STRIP: + case DrawMode::DRAW_LINE_LOOP: + case DrawMode::DRAW_LINE_STRIP_ADJACENCY: + case DrawMode::DRAW_LINES_ADJACENCY: + case DrawMode::DRAW_TRIANGLE_STRIP_ADJACENCY: + case DrawMode::DRAW_TRIANGLES_ADJACENCY: + case DrawMode::DRAW_PATCHES: + default: + throw EXCEPTION("Unable to gen_rect_pt for current draw mode: " + std::to_string(draw_mode)); + } + } + template Mesh* gen_rect_pt(const DrawMode& draw_mode, float width, float height, float offset_x = 0.0f, float offset_y = 0.0f) { diff --git a/Example/Example.vcxproj b/Example/Example.vcxproj index 19573f5..667f2db 100644 --- a/Example/Example.vcxproj +++ b/Example/Example.vcxproj @@ -26,6 +26,7 @@ + @@ -38,6 +39,7 @@ + diff --git a/Example/Example.vcxproj.filters b/Example/Example.vcxproj.filters index 2fadeda..ffc44ba 100644 --- a/Example/Example.vcxproj.filters +++ b/Example/Example.vcxproj.filters @@ -45,6 +45,9 @@ Source Files + + Source Files + @@ -80,5 +83,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/Example/MyApplication.cpp b/Example/MyApplication.cpp index f51a001..e5a15c2 100644 --- a/Example/MyApplication.cpp +++ b/Example/MyApplication.cpp @@ -8,7 +8,8 @@ MyApplication::MyApplication(int width, int height) m_simple_cube_scene(*this), m_builtin_basic_cube_scene(*this), m_builtin_textured_scene(*this), - m_builtin_lit_scene(*this) + m_builtin_lit_scene(*this), + m_pong_scene(*this) {} void MyApplication::init() @@ -20,6 +21,7 @@ void MyApplication::init() m_builtin_basic_cube_scene.init(); m_builtin_textured_scene.init(); m_builtin_lit_scene.init(); + m_pong_scene.init(); m_p_current_scene = &m_basic_scene; m_p_current_scene->use(); @@ -55,6 +57,10 @@ void MyApplication::update(float delta_time, clock_t clock) { swap_scene(&m_builtin_lit_scene); } + else if (m_glfw_input_manager.is_key_pressed(GLFW_KEY_8)) + { + swap_scene(&m_pong_scene); + } m_p_current_scene->update(delta_time, clock); } diff --git a/Example/MyApplication.h b/Example/MyApplication.h index 044e9b6..87a68de 100644 --- a/Example/MyApplication.h +++ b/Example/MyApplication.h @@ -10,6 +10,7 @@ #include "MyBuiltinCubeScene.h" #include "MyBuiltinTexturedScene.h" #include "MyBuiltinLitScene.h" +#include "MyPongScene.h" using namespace charcoal; @@ -40,5 +41,6 @@ private: MyBuiltinCubeScene m_builtin_basic_cube_scene; MyBuiltinTexturedScene m_builtin_textured_scene; MyBuiltinLitScene m_builtin_lit_scene; + MyPongScene m_pong_scene; }; diff --git a/Example/MyPongScene.cpp b/Example/MyPongScene.cpp new file mode 100644 index 0000000..f8a3983 --- /dev/null +++ b/Example/MyPongScene.cpp @@ -0,0 +1,102 @@ +#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 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)); + + 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(); +} + diff --git a/Example/MyPongScene.h b/Example/MyPongScene.h new file mode 100644 index 0000000..345e565 --- /dev/null +++ b/Example/MyPongScene.h @@ -0,0 +1,53 @@ +#pragma once + +#include +#include +#include +#include + +using namespace charcoal; +using namespace charcoal::builtin; + +class MyPongScene : public Scene +{ +public: + MyPongScene(Application& application); + + void init() override; + + void use() override {} + + void unuse() override {} + + void update(float delta_time, clock_t clock) override; + + void prerender() override; + + void render() override; + +private: + basic::Renderable m_outline_column; + basic::Renderable m_outline_row; + basic::Renderable m_ball; + basic::Renderable m_paddle; + + basic::Batch m_outline_column_batch; + basic::Batch m_outline_row_batch; + basic::Batch m_ball_batch; + basic::Batch m_paddle_batch; + + Poseable m_outline_top_pose; + Poseable m_outline_bottom_pose; + Poseable m_outline_left_pose; + Poseable m_outline_right_pose; + + Poseable m_ball_pose; + Poseable m_paddle_left_pose; + Poseable m_paddle_right_pose; + + builtin::Camera2D m_camera; + basic::Pipeline m_pipeline; + + float m_ball_speed; + glm::vec2 m_ball_direction; +}; \ No newline at end of file