Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add background #45

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/sniper-background-11-26-20-small.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions src/game/background.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "background.hpp"

#include <graphics/texture.hpp>

namespace game {

void Background::render() {
tex::BG_TEX->render(0, 0, 5.0, 5.0, tex::RenderBasis::MID,
tex::RenderBasis::MID);
}

} // namespace game
12 changes: 12 additions & 0 deletions src/game/background.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include <game/renderable.hpp>

namespace game {

class Background : public Renderable {
public:
void render();
};

} // namespace game
6 changes: 6 additions & 0 deletions src/graphics/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,19 @@ namespace tex {
std::shared_ptr<gl::TextureAtlas> GAME_TEX;
std::shared_ptr<tex::Texture> PLAYER_TEX;

std::shared_ptr<tex::Texture> BG_TEX;

void load_all_textures() {
GAME_TEX = std::make_shared<gl::TextureAtlas>("assets/texture-atlas.png");
PLAYER_TEX = std::make_shared<tex::Texture>(tex::GAME_TEX, 0.0, 0.75, 0.25, 0.25);

_BACKGROUND_TEX = std::make_shared<gl::TextureAtlas>("assets/sniper-background-11-26-20-small.png");
BG_TEX = std::make_shared<tex::Texture>(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
Expand Down
3 changes: 3 additions & 0 deletions src/graphics/texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ extern std::shared_ptr<gl::TextureAtlas>

extern std::shared_ptr<tex::Texture> PLAYER_TEX;

static std::shared_ptr<gl::TextureAtlas> _BACKGROUND_TEX;
extern std::shared_ptr<tex::Texture> BG_TEX;

void load_all_textures();
void unload_all_textures();

Expand Down
46 changes: 23 additions & 23 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
#include <vector>

#include <glad/glad.h>
#include <GLFW/glfw3.h>

#include <game/background.hpp>
#include <game/movement.hpp>
#include <game/player.hpp>
#include <graphics/shader.hpp>
#include <graphics/texture.hpp>

#include <GLFW/glfw3.h>

namespace sniper {

GLFWwindow * wn;
GLFWwindow *wn;

void init() {

Expand All @@ -27,47 +29,47 @@ 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()));
player.do_pos_updates();

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() {
Expand All @@ -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;

}