mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-11-22 16:52:31 +01:00
feat: spliting files into folders, adding minimalExample code and chapter 1 from SFML Game Development
This commit is contained in:
parent
775e1c50f1
commit
5fddef2c6c
BIN
SFMLEngine/makingAGameTick/app
Executable file
BIN
SFMLEngine/makingAGameTick/app
Executable file
Binary file not shown.
BIN
SFMLEngine/makingAGameTick/constants
Normal file
BIN
SFMLEngine/makingAGameTick/constants
Normal file
Binary file not shown.
19
SFMLEngine/makingAGameTick/constants.hpp
Normal file
19
SFMLEngine/makingAGameTick/constants.hpp
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#ifndef CONSTANTS_HPP
|
||||||
|
#define CONSTANTS_HPP
|
||||||
|
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
||||||
|
// 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
|
||||||
102
SFMLEngine/makingAGameTick/game.cpp
Normal file
102
SFMLEngine/makingAGameTick/game.cpp
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
#include <vector>
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
#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<bool, sf::Keyboard::Key> > 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();
|
||||||
|
}
|
||||||
BIN
SFMLEngine/makingAGameTick/game.o
Normal file
BIN
SFMLEngine/makingAGameTick/game.o
Normal file
Binary file not shown.
6
SFMLEngine/makingAGameTick/makefile
Normal file
6
SFMLEngine/makingAGameTick/makefile
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
compile:./game.cpp
|
||||||
|
g++ -c ./game.cpp
|
||||||
|
g++ game.o -o app -lsfml-graphics -lsfml-window -lsfml-system
|
||||||
|
|
||||||
|
run:
|
||||||
|
./app
|
||||||
BIN
SFMLEngine/minimalExample/app
Executable file
BIN
SFMLEngine/minimalExample/app
Executable file
Binary file not shown.
34
SFMLEngine/minimalExample/example.cpp
Normal file
34
SFMLEngine/minimalExample/example.cpp
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
// SFML is split into 5 modules:
|
||||||
|
// #include <SFML/Graphics.hpp>
|
||||||
|
// #include <SFML/Audio.hpp>
|
||||||
|
// #include <SFML/System.hpp>
|
||||||
|
// #include <SFML/Window.hpp>
|
||||||
|
// #include <SFML/Network.hpp>
|
||||||
|
// We can include them like this or we can include just a specific header file:
|
||||||
|
// #include <SFML/Audio/Sound.hpp>
|
||||||
|
// Each module is compiled to separate library, it can be built for release or debug, linked statically or dynamically
|
||||||
|
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
SFMLEngine/minimalExample/example.o
Normal file
BIN
SFMLEngine/minimalExample/example.o
Normal file
Binary file not shown.
6
SFMLEngine/minimalExample/makefile
Normal file
6
SFMLEngine/minimalExample/makefile
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
compile:./example.cpp
|
||||||
|
g++ -c ./example.cpp
|
||||||
|
g++ example.o -o app -lsfml-graphics -lsfml-window -lsfml-system
|
||||||
|
|
||||||
|
run:
|
||||||
|
./app
|
||||||
Loading…
Reference in New Issue
Block a user