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 bullet start #43

Open
wants to merge 2 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
30 changes: 30 additions & 0 deletions src/game/bullet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "bullet.hpp"

#include <graphics/texture.hpp>

namespace game {

Bullet::Bullet(float xloc, float yloc, float angle) {
this->x = xloc;
this->y = yloc;
this->angle = angle;
this->vx = 0;
this->vy = 0;
}

void Bullet::render() {
// TODO: Change to actual bullet texture
tex::PLAYER_TEX->render(this->x, this->y, 0.01f, 0.04f,
tex::RenderBasis::MID, tex::RenderBasis::MID);
}

std::pair<float, float> Bullet::getvel() {
return std::pair(this->vx, this->vy);
}

void Bullet::setvel(std::pair<float, float> newvel) {
this->vx = newvel.first;
this->vy = newvel.second;
}

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

#include <vector>

#include <game/gameobject.hpp>
#include <game/renderable.hpp>

namespace game {

class Bullet : public GameObject, Renderable {

public:
void render() override;
Bullet(float xloc, float yloc, float angle);

float angle;

/* Bounce off the edges of the screen. */
void do_bounce(float screen_edge);

// Read and update velocities.
std::pair<float, float> getvel();
void setvel(std::pair<float, float> newvel);
};

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

namespace game {

class GameObject {
Expand Down
30 changes: 25 additions & 5 deletions src/game/movement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ float FRICTION = 0.01f;

/* Get the new X and Y speed of the player, given the current X and Y speed and
readings from GLFW of the currently pressed keys. */
std::pair<float, float> new_speed(GLFWwindow * wn, std::pair<float, float> old_speed) {
std::pair<float, float> new_speed(GLFWwindow *wn,
std::pair<float, float> old_speed) {

std::pair<float, float> ret = old_speed;

Expand All @@ -39,7 +40,7 @@ std::pair<float, float> new_speed(GLFWwindow * wn, std::pair<float, float> old_s

/* We slow down a tiny bit if nothing is pressed. */
if (hinc == 0 && vinc == 0) {
ret.first *= (1.0f - FRICTION);
ret.first *= (1.0f - FRICTION);
ret.second *= (1.0f - FRICTION);
return ret;
}
Expand All @@ -52,11 +53,10 @@ std::pair<float, float> new_speed(GLFWwindow * wn, std::pair<float, float> old_s

float update_frac = 1 / SLIPPERINESS;

ret.first = ret.first * (1 - update_frac) + ideal_x * update_frac;
ret.first = ret.first * (1 - update_frac) + ideal_x * update_frac;
ret.second = ret.second * (1 - update_frac) + ideal_y * update_frac;

return ret;

}

int FPS = 50;
Expand All @@ -83,7 +83,27 @@ void Player::do_bounce(float screen_edge) {
vy *= -1;
y = -screen_edge;
}

}

// TODO: abstract this out - issue #44
void Bullet::do_bounce(float screen_edge) {

if (x > screen_edge) {
vx *= -1;
x = screen_edge;
}
if (x < -screen_edge) {
vx *= -1;
x = -screen_edge;
}
if (y > screen_edge) {
vy *= -1;
y = screen_edge;
}
if (y < -screen_edge) {
vy *= -1;
y = -screen_edge;
}
}

} // namespace game
1 change: 1 addition & 0 deletions src/game/movement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <GLFW/glfw3.h>

#include <game/player.hpp>
#include <game/bullet.hpp>

namespace mvmt {

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

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

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

namespace sniper {

GLFWwindow * wn;
GLFWwindow *wn;

void init() {

Expand All @@ -27,47 +27,50 @@ 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() {

static float GAME_EDGE = 0.9f;

game::Player player(0, 0);

game::Bullet bullet(0.2, 0.2, 1);

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);
player.do_bounce(GAME_EDGE);
bullet.do_bounce(GAME_EDGE);

player.render();
bullet.render();

glfwSwapBuffers(wn);
glfwPollEvents();
glfwSwapBuffers(wn);
glfwPollEvents();

std::this_thread::sleep_until(start_of_frame + std::chrono::milliseconds(1000 / mvmt::FPS));

}
std::this_thread::sleep_until(
start_of_frame + std::chrono::milliseconds(1000 / mvmt::FPS));
}

gl::GAME_SHADER->unbind();

}

void cleanup() {
Expand All @@ -76,18 +79,16 @@ void cleanup() {
gl::unload_all_shaders();

glfwDestroyWindow(wn);
glfwTerminate();

glfwTerminate();
}

}
} // namespace sniper

int main() {

sniper::init();
sniper::mainloop();
sniper::cleanup();

return 0;

}