diff --git a/SFMLEngine/app b/SFMLEngine/helloWorld/app similarity index 100% rename from SFMLEngine/app rename to SFMLEngine/helloWorld/app diff --git a/SFMLEngine/hello.cpp b/SFMLEngine/helloWorld/hello.cpp similarity index 100% rename from SFMLEngine/hello.cpp rename to SFMLEngine/helloWorld/hello.cpp diff --git a/SFMLEngine/hello.o b/SFMLEngine/helloWorld/hello.o similarity index 100% rename from SFMLEngine/hello.o rename to SFMLEngine/helloWorld/hello.o diff --git a/SFMLEngine/makefile b/SFMLEngine/helloWorld/makefile similarity index 100% rename from SFMLEngine/makefile rename to SFMLEngine/helloWorld/makefile diff --git a/SFMLEngine/makingAGameTick/app b/SFMLEngine/makingAGameTick/app new file mode 100755 index 0000000..47706ba Binary files /dev/null and b/SFMLEngine/makingAGameTick/app differ diff --git a/SFMLEngine/makingAGameTick/constants b/SFMLEngine/makingAGameTick/constants new file mode 100644 index 0000000..c8be05a Binary files /dev/null and b/SFMLEngine/makingAGameTick/constants differ diff --git a/SFMLEngine/makingAGameTick/constants.hpp b/SFMLEngine/makingAGameTick/constants.hpp new file mode 100644 index 0000000..2fd510f --- /dev/null +++ b/SFMLEngine/makingAGameTick/constants.hpp @@ -0,0 +1,19 @@ +#ifndef CONSTANTS_HPP +#define CONSTANTS_HPP + +#include + +// Player constants +const float PLAYER_RADIUS = 40; +const float PLAYER_X_POSITION = 100; +const float PLAYER_Y_POSITION = 100; +const sf::Color PLAYER_COLOR = sf::Color::Cyan; + +// Movement constants +// const sf::Vector2f INITIAL_MOVEMENT (0.f, 0.f); +const float MOVING_UP_SPEED = -1; +const float MOVING_DOWN_SPEED = -MOVING_UP_SPEED; +const float MOVING_RIGHT_SPEED = 1; +const float MOVING_LEFT_SPEED = -MOVING_RIGHT_SPEED; + +#endif // CONSTANTS_HPP diff --git a/SFMLEngine/makingAGameTick/game.cpp b/SFMLEngine/makingAGameTick/game.cpp new file mode 100644 index 0000000..58c97aa --- /dev/null +++ b/SFMLEngine/makingAGameTick/game.cpp @@ -0,0 +1,102 @@ +#include +#include +#include "constants.hpp" + +class Game +{ + public: + Game(); // Sets up player radius, position and fill color + void run(); // runs the processEvents, update and render methods + + private: + void processEvents(); // playerInput, mainLoop + void update(); // code that updates the game + void render(); // code that renders the game + void handlePlayerInput(sf::Keyboard::Key key, bool isPressed); + bool mIsMovingUp, mIsMovingRight, mIsMovingLeft, mIsMovingDown; + private: + sf::RenderWindow mWindow; + sf::CircleShape mPlayer; +}; + +Game::Game() : mWindow(sf::VideoMode(640, 480), "SFML Application"), mPlayer() +{ + mPlayer.setRadius(PLAYER_RADIUS); + mPlayer.setPosition(PLAYER_X_POSITION, PLAYER_Y_POSITION); + mPlayer.setFillColor(PLAYER_COLOR); + bool mIsMovingUp = false; + bool mIsMovingRight = false; + bool mIsMovingLeft = false; + bool mIsMovingDown = false; +} + +void Game::run() +{ + while (mWindow.isOpen()) + { + processEvents(); + update(); + render(); + } +} + +/* +const std::vector < pair > PLAYER_MOVEMENT = +{ + {mIsMovingUp, sf::Keyboard::W}, + {mIsMovingDown, sf::Keyboard::S}, + {mIsMovingLeft, sf::Keyboard::A}, + {mIsMovingRight, sf::Keyboard::D} +} +*/ + +void Game::handlePlayerInput(sf::Keyboard::Key key, bool isPressed) +{ + if (key == sf::Keyboard::W) mIsMovingUp = isPressed; + else if (key == sf::Keyboard::S) mIsMovingDown = isPressed; + else if (key == sf::Keyboard::A) mIsMovingLeft = isPressed; + else if (key == sf::Keyboard::D) mIsMovingRight = isPressed; +} + +void Game::processEvents() +{ + sf::Event event; + while (mWindow.pollEvent(event)) // mainLoop/gameLoop + { + // each time while loop iterates it means that wwe got a new event registered by the window. + switch (event.type) + { + case sf::Event::KeyPressed: + handlePlayerInput(event.key.code, true); + break; + case sf::Event::KeyReleased: + handlePlayerInput(event.key.code, false); + break; + case sf::Event::Closed: + mWindow.close(); + break; + } + } +} + +void Game::update() +{ + sf::Vector2f movement (0.f, 0.f); + movement.y += mIsMovingUp * MOVING_UP_SPEED + mIsMovingDown * MOVING_DOWN_SPEED; + movement.x += mIsMovingLeft * MOVING_LEFT_SPEED + mIsMovingRight * MOVING_RIGHT_SPEED; + + mPlayer.move(movement); +} + +void Game::render() +{ + mWindow.clear(); + mWindow.draw(mPlayer); + mWindow.display(); +} + +int main() +{ + Game game; + game.run(); +} diff --git a/SFMLEngine/makingAGameTick/game.o b/SFMLEngine/makingAGameTick/game.o new file mode 100644 index 0000000..ee9dfc6 Binary files /dev/null and b/SFMLEngine/makingAGameTick/game.o differ diff --git a/SFMLEngine/makingAGameTick/makefile b/SFMLEngine/makingAGameTick/makefile new file mode 100644 index 0000000..0886b99 --- /dev/null +++ b/SFMLEngine/makingAGameTick/makefile @@ -0,0 +1,6 @@ +compile:./game.cpp + g++ -c ./game.cpp + g++ game.o -o app -lsfml-graphics -lsfml-window -lsfml-system + +run: + ./app diff --git a/SFMLEngine/minimalExample/app b/SFMLEngine/minimalExample/app new file mode 100755 index 0000000..5c0970c Binary files /dev/null and b/SFMLEngine/minimalExample/app differ diff --git a/SFMLEngine/minimalExample/example.cpp b/SFMLEngine/minimalExample/example.cpp new file mode 100644 index 0000000..86420c8 --- /dev/null +++ b/SFMLEngine/minimalExample/example.cpp @@ -0,0 +1,34 @@ +// SFML is split into 5 modules: +// #include +// #include +// #include +// #include +// #include +// We can include them like this or we can include just a specific header file: +// #include +// Each module is compiled to separate library, it can be built for release or debug, linked statically or dynamically + +#include + +int main() +{ + sf::RenderWindow window(sf::VideoMode(640, 480), "SFML Application"); + sf::CircleShape shape; + const float CIRCLE_RADIUS = 40; + shape.setRadius(CIRCLE_RADIUS); + const float CIRCLE_POSITION_X = 100; + const float CIRCLE_POSITION_Y = 100; + shape.setPosition(CIRCLE_POSITION_X, CIRCLE_POSITION_Y); + shape.setFillColor(sf::Color::Cyan); + while(window.isOpen()) + { + sf::Event event; + while(window.pollEvent(event)) + { + if(event.type == sf::Event::Closed) window.close(); + } + window.clear(); + window.draw(shape); + window.display(); + } +} diff --git a/SFMLEngine/minimalExample/example.o b/SFMLEngine/minimalExample/example.o new file mode 100644 index 0000000..93f26bf Binary files /dev/null and b/SFMLEngine/minimalExample/example.o differ diff --git a/SFMLEngine/minimalExample/makefile b/SFMLEngine/minimalExample/makefile new file mode 100644 index 0000000..9e16676 --- /dev/null +++ b/SFMLEngine/minimalExample/makefile @@ -0,0 +1,6 @@ +compile:./example.cpp + g++ -c ./example.cpp + g++ example.o -o app -lsfml-graphics -lsfml-window -lsfml-system + +run: + ./app