diff --git a/assets/sniper-background-11-26-20-small.png b/assets/sniper-background-11-26-20-small.png new file mode 100644 index 0000000..965bfae Binary files /dev/null and b/assets/sniper-background-11-26-20-small.png differ diff --git a/src/game/background.cpp b/src/game/background.cpp new file mode 100644 index 0000000..308996a --- /dev/null +++ b/src/game/background.cpp @@ -0,0 +1,12 @@ +#include "background.hpp" + +#include + +namespace game { + +void Background::render() { + tex::BG_TEX->render(0, 0, 5.0, 5.0, tex::RenderBasis::MID, + tex::RenderBasis::MID); +} + +} // namespace game diff --git a/src/game/background.hpp b/src/game/background.hpp new file mode 100644 index 0000000..432494f --- /dev/null +++ b/src/game/background.hpp @@ -0,0 +1,12 @@ +#pragma once + +#include + +namespace game { + +class Background : public Renderable { + public: + void render(); +}; + +} // namespace game diff --git a/src/graphics/texture.cpp b/src/graphics/texture.cpp index b153b05..36ca0fb 100644 --- a/src/graphics/texture.cpp +++ b/src/graphics/texture.cpp @@ -73,13 +73,19 @@ namespace tex { std::shared_ptr GAME_TEX; std::shared_ptr PLAYER_TEX; +std::shared_ptr BG_TEX; + void load_all_textures() { GAME_TEX = std::make_shared("assets/texture-atlas.png"); PLAYER_TEX = std::make_shared(tex::GAME_TEX, 0.0, 0.75, 0.25, 0.25); + + _BACKGROUND_TEX = std::make_shared("assets/sniper-background-11-26-20-small.png"); + BG_TEX = std::make_shared(tex::_BACKGROUND_TEX, 0.0, 0.0, 1.0, 1.0); } void unload_all_textures() { GAME_TEX.reset(); + _BACKGROUND_TEX.reset(); } // Private -- render a texture atlas diff --git a/src/graphics/texture.hpp b/src/graphics/texture.hpp index dbed299..a09a27c 100644 --- a/src/graphics/texture.hpp +++ b/src/graphics/texture.hpp @@ -70,6 +70,9 @@ extern std::shared_ptr extern std::shared_ptr PLAYER_TEX; +static std::shared_ptr _BACKGROUND_TEX; +extern std::shared_ptr BG_TEX; + void load_all_textures(); void unload_all_textures(); diff --git a/src/main.cpp b/src/main.cpp index 95cabc3..e11388f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,16 +6,18 @@ #include #include -#include +#include #include #include #include #include +#include + namespace sniper { -GLFWwindow * wn; +GLFWwindow *wn; void init() { @@ -27,29 +29,29 @@ void init() { glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); wn = glfwCreateWindow(500, 500, "Sniper", nullptr, nullptr); - glfwMakeContextCurrent(wn); - gladLoadGL(); - glfwShowWindow(wn); + glfwMakeContextCurrent(wn); + gladLoadGL(); + glfwShowWindow(wn); gl::load_all_shaders(); tex::load_all_textures(); - } void mainloop() { + game::Background background; game::Player player(0, 0); - + gl::GAME_SHADER->bind(); while (!glfwWindowShouldClose(wn)) { - // We want each frame to last for exactly 1/50th second, - // so capture the starting time so we can sleep for the - // needed amount of time at the end of the frame. - auto start_of_frame = std::chrono::steady_clock::now(); + // We want each frame to last for exactly 1/50th second, + // so capture the starting time so we can sleep for the + // needed amount of time at the end of the frame. + auto start_of_frame = std::chrono::steady_clock::now(); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Update the player speed and position. player.setvel(mvmt::new_speed(wn, player.getvel())); @@ -57,17 +59,17 @@ void mainloop() { player.do_bounce(0.9f); + background.render(); player.render(); - glfwSwapBuffers(wn); - glfwPollEvents(); - - std::this_thread::sleep_until(start_of_frame + std::chrono::milliseconds(1000 / mvmt::FPS)); + glfwSwapBuffers(wn); + glfwPollEvents(); - } + std::this_thread::sleep_until( + start_of_frame + std::chrono::milliseconds(1000 / mvmt::FPS)); + } gl::GAME_SHADER->unbind(); - } void cleanup() { @@ -76,18 +78,16 @@ void cleanup() { gl::unload_all_shaders(); glfwDestroyWindow(wn); - glfwTerminate(); - + glfwTerminate(); } -} +} // namespace sniper int main() { - + sniper::init(); sniper::mainloop(); sniper::cleanup(); return 0; - }