diff --git a/Assets/shaders/pipe_shader.frag b/Assets/shaders/pipe_shader.frag new file mode 100644 index 0000000..9f9ec80 --- /dev/null +++ b/Assets/shaders/pipe_shader.frag @@ -0,0 +1,5 @@ + +void main() +{ + gl_FragColor = vec4(1.0,0.0,1.0,1.0); +} \ No newline at end of file diff --git a/Assets/shaders/pipe_shader.vert b/Assets/shaders/pipe_shader.vert new file mode 100644 index 0000000..9b656e0 --- /dev/null +++ b/Assets/shaders/pipe_shader.vert @@ -0,0 +1,7 @@ +void main() +{ + vec4 vertex = gl_ModelViewMatrix * gl_Vertex; + gl_Position = gl_ProjectionMatrix * vertex; + gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; + gl_FrontColor = gl_Color; +} \ No newline at end of file diff --git a/Bird.cpp b/Bird.cpp new file mode 100644 index 0000000..cb4266a --- /dev/null +++ b/Bird.cpp @@ -0,0 +1,29 @@ +#include +#include "Bird.h" + + +Bird::Bird(float x, float y, const std::shared_ptr> texture_list) : position(x, y), texture_list(texture_list), momentum(1.0) +{ + sprite = sf::Sprite(texture_list->at(0)); + sprite.setPosition(position); +} + +void Bird::impulse(float p) { + momentum = -p; +} + +void Bird::tick(float step) { + momentum += gravity * step; // Impart gravity + position.y += momentum; + sprite.setPosition(position); +} + +void Bird::draw(sf::RenderTarget &target, sf::RenderStates states) const { + target.draw(sprite, states); +} + +bool Bird::collides(sf::FloatRect bounds) +{ + return sprite.getGlobalBounds().intersects(bounds); +} + diff --git a/Bird.h b/Bird.h new file mode 100644 index 0000000..742e069 --- /dev/null +++ b/Bird.h @@ -0,0 +1,30 @@ +#ifndef FLOPPY_BIRD_BIRD_H +#define FLOPPY_BIRD_BIRD_H + +#include +#include +#include +#include + +const float gravity = 9.8; + +class Bird : public sf::Drawable { + + sf::Sprite sprite; + std::shared_ptr> texture_list; + + sf::Vector2f position; + float momentum; + +public: + Bird(float x, float y, const std::shared_ptr> texture_list); + + void impulse(float p); + void tick(float step); + bool collides(sf::FloatRect bounds); + + virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const; +}; + + +#endif //FLOPPY_BIRD_BIRD_H diff --git a/CMakeLists.txt b/CMakeLists.txt index a8c0271..8cf38a5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ set(SFML_ROOT root CACHE STRING "User specified path") set(SFML_COMPONENTS graphics window system network audio) set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}) -find_package(SFML 2.1 COMPONENTS ${SFML_COMPONENTS} REQUIRED) +find_package(SFML 2.5 COMPONENTS ${SFML_COMPONENTS} REQUIRED) message(STATUS "SFML found: ${SFML_FOUND}") # Include the directories for the main program, GL, CL and SFML's headers @@ -22,7 +22,7 @@ include_directories(include) file(GLOB SOURCES "*.cpp") file(GLOB HEADERS "*.h") -add_executable(${PNAME} ${SOURCES}) +add_executable(${PNAME} ${SOURCES} Bird.cpp Bird.h Pipe.cpp Pipe.h) # Link CL, GL, and SFML target_link_libraries (${PNAME} ${SFML_LIBRARIES} ${SFML_DEPENDENCIES}) diff --git a/Pipe.cpp b/Pipe.cpp new file mode 100644 index 0000000..a156f8c --- /dev/null +++ b/Pipe.cpp @@ -0,0 +1,38 @@ +#include "Pipe.h" + +#include + +Pipe::Pipe(float x, float y, + std::shared_ptr pipe_top_texture_s, + std::shared_ptr pipe_shaft_texture_s, + std::shared_ptr pipe_shaft_shader_s) : + position(x, y), + pipe_top_texture(std::move(pipe_top_texture_s)), + pipe_shaft_texture(std::move(pipe_shaft_texture_s)), + pipe_shaft_shader(std::move(pipe_shaft_shader_s)), + momentum(1.0) +{ + pipe_top = sf::Sprite(*pipe_top_texture); + pipe_shaft = sf::Sprite(*pipe_shaft_texture); + + pipe_top.setPosition(position); +} + +void Pipe::tick(float step, float speed) { + position.x += step * speed; + pipe_top.setPosition(position); + + auto pos = pipe_top.getPosition(); + pos.y += 25; + pipe_shaft.setPosition(pos); +} + +void Pipe::draw(sf::RenderTarget &target, sf::RenderStates states) const{ + states.shader = &*pipe_shaft_shader; + target.draw(pipe_top, states); + target.draw(pipe_shaft, states); +} + +bool Pipe::collides(sf::FloatRect bounds) { + return false; +} diff --git a/Pipe.h b/Pipe.h new file mode 100644 index 0000000..6cbca3b --- /dev/null +++ b/Pipe.h @@ -0,0 +1,33 @@ +#ifndef FLOPPY_BIRD_PIPE_H +#define FLOPPY_BIRD_PIPE_H + + +#include +#include +#include +#include + +class Pipe : public sf::Drawable { + + sf::Sprite pipe_top; + sf::Sprite pipe_shaft; + + std::shared_ptr pipe_top_texture; + std::shared_ptr pipe_shaft_texture; + + std::shared_ptr pipe_shaft_shader; + + sf::Vector2f position; + float momentum; + +public: + Pipe(float x, float y, std::shared_ptr pipe_top_texture, std::shared_ptr pipe_shaft_texture, std::shared_ptr shader); + + void tick(float step, float speed); + bool collides(sf::FloatRect bounds); + + virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const; +}; + + +#endif //FLOPPY_BIRD_PIPE_H diff --git a/main.cpp b/main.cpp index 5aedb3a..146e1d7 100644 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,9 @@ #include #include #include +#include +#include "Bird.h" +#include "Pipe.h" #ifdef linux #elif defined _WIN32 @@ -35,16 +38,18 @@ int main() int pipe_dist = 200; sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "flappy"); - + + std::shared_ptr> texture_list = std::make_shared>(); + for (int i = 0; i < 4; i++) { + sf::Texture t; + t.loadFromFile("../Assets/bird.png", sf::IntRect(0, i*12, 34, 24)); + texture_list->push_back(t); + } + // Init flappy - sf::Texture flappy_texture[4] = { sf::Texture(), sf::Texture(), sf::Texture(), sf::Texture() }; - for (int i = 0; i < 4; i++) { - flappy_texture[i].loadFromFile("../Assets/bird.png", sf::IntRect(0, i*12, 34, 24)); - } + Bird bird(WINDOW_X/2, WINDOW_Y/2, texture_list); - sf::RectangleShape flappy(sf::Vector2f(34, 24)); - flappy.setTexture(&flappy_texture[0],false); - flappy.setPosition(WINDOW_X / 2, WINDOW_Y / 2); + std::list pipes; // Init world sf::Texture background = sf::Texture(); background.loadFromFile("../Assets/sky.png"); @@ -61,6 +66,12 @@ int main() double frame_time = 0.0, elapsed_time = 0.0, delta_time = 0.0, accumulator_time = 0.0, current_time = 0.0; float speed = 250; + std::shared_ptr shader = std::make_shared(); + if (!shader->loadFromFile("/home/mrh/source/floppy-bird/Assets/shaders/pipe_shader.vert", "/home/mrh/source/floppy-bird/Assets/shaders/pipe_shader.frag")) + { + std::cout << "asodijfoqijwef" << std::endl; + } + while (window.isOpen()) { sf::Event event; // Handle input @@ -72,22 +83,23 @@ int main() speed += event.mouseWheelScroll.delta * 20; if(event.type == sf::Event::KeyPressed) if (event.key.code == sf::Keyboard::Space) - momentum = -2; + bird.impulse(3.0); if (event.type == sf::Event::MouseButtonPressed) { if (event.mouseButton.button == sf::Mouse::Right) pipe_dist -= 10; if (event.mouseButton.button == sf::Mouse::Middle) pipe_dist += 10; if (event.mouseButton.button == sf::Mouse::Left) - momentum = -2; + bird.impulse(3.0); } } elapsed_time = elap_time(); // Handle time delta_time = elapsed_time - current_time; current_time = elapsed_time; - if (delta_time > 0.02f) - delta_time = 0.02f; + if (delta_time > 0.02f) { + delta_time = 0.02f; + } accumulator_time += delta_time; while ((accumulator_time - step_size) >= step_size) { // While the frame has sim time, update @@ -96,13 +108,17 @@ int main() if (pipe_down_sprite.getPosition().x < -pipe_down_sprite.getGlobalBounds().width) { pipe_down_sprite.setPosition(WINDOW_X, rgen(rng)); pipe_up_sprite.setPosition(WINDOW_X, pipe_down_sprite.getPosition().y + pipe_dist); + + pipes.emplace_back(100, 100, std::make_shared(pipe_up), std::make_shared(pipe_shaft), shader); + std::cout << "added one" << std::endl; } else { pipe_up_sprite.setPosition(pipe_up_sprite.getPosition().x - step_size * speed, pipe_up_sprite.getPosition().y); pipe_down_sprite.setPosition(pipe_down_sprite.getPosition().x - step_size * speed, pipe_down_sprite.getPosition().y); } - if (background_sprite.getPosition().x + background_sprite.getGlobalBounds().width < WINDOW_X) - background_sprite.setPosition(0, 0); + if (background_sprite.getPosition().x + background_sprite.getGlobalBounds().width < WINDOW_X) { + background_sprite.setPosition(0, 0); + } else background_sprite.setPosition(background_sprite.getPosition().x - step_size * (speed - 100), background_sprite.getPosition().y); if (land_sprite.getPosition().x + 10 + land_sprite.getGlobalBounds().width < WINDOW_X) @@ -110,40 +126,33 @@ int main() else land_sprite.setPosition(land_sprite.getPosition().x - step_size * speed, land_sprite.getPosition().y); - sf::Vector2f f_pos = flappy.getPosition(); - sf::FloatRect f_rec = flappy.getGlobalBounds(); - sf::Vector2f p_pos = pipe_up_sprite.getPosition(); - sf::FloatRect p_rec = pipe_up_sprite.getGlobalBounds(); - // Check collisions - if (f_pos.y > land_sprite.getPosition().y) { - f_pos = sf::Vector2f(WINDOW_X / 2, p_pos.y - pipe_dist / 2); - momentum = -2; - std::cout << "\ndead " + std::to_string(flappy.getPosition().y); - } - - if (f_pos.x < p_pos.x + p_rec.width && - f_pos.x + f_rec.width > p_pos.x && - (f_pos.y > p_pos.y || f_pos.y < p_pos.y - pipe_dist) && - (f_rec.height + f_pos.y > p_pos.y || f_rec.height + f_pos.y < p_pos.y + p_rec.height - pipe_dist)) { - - f_pos = sf::Vector2f(WINDOW_X / 2, p_pos.y - pipe_dist / 2); - momentum = -2; - std::cout << "\ndead " + std::to_string(flappy.getPosition().y); - } - - - momentum += g * step_size; // Impart gravity - f_pos.y += momentum; - flappy.setPosition(f_pos); + if (bird.collides(pipe_up_sprite.getGlobalBounds()) || + bird.collides(pipe_down_sprite.getGlobalBounds()) || + bird.collides(land_sprite.getGlobalBounds()) + ) + { + //std::cout << "dead!!!!!!!!" << std::endl; + } + + bird.tick(step_size); + for (auto pipe = pipes.begin(); pipe != pipes.end(); ++pipe) + { + pipe->tick(step_size, speed); + } } - window.draw(background_sprite); // Render + window.draw(background_sprite, &*shader); // Render window.draw(land_sprite); - window.draw(flappy); + window.draw(bird); window.draw(pipe_up_sprite); window.draw(pipe_down_sprite); + for (auto ptr = pipes.begin(); ptr != pipes.end(); ++ptr) + { + window.draw(*ptr); + } + pipe_shaft_sprite.setPosition(pipe_up_sprite.getPosition()); // Render the bottom pipe int y_pos = pipe_up_sprite.getPosition().y + pipe_up_sprite.getGlobalBounds().height; while (y_pos < WINDOW_Y) {