Initial commit

This commit is contained in:
Mattéo Delabre 2016-03-04 16:29:31 +01:00
parent bbee35624e
commit 0a2943c6bf
12 changed files with 85003 additions and 0 deletions

18
Makefile Normal file
View File

@ -0,0 +1,18 @@
CC=g++
CFLAGS=-Wall -std=c++11
TARGET=ptf
$(TARGET): main.o ball.o engine.o
$(CC) $(CFLAGS) -o build/$(TARGET) build/engine.o build/main.o build/ball.o -lsfml-graphics -lsfml-window -lsfml-system -lpthread
ball.o: ball.cpp
$(CC) $(CFLAGS) -o build/ball.o -c ball.cpp
engine.o: engine.cpp
$(CC) $(CFLAGS) -o build/engine.o -c engine.cpp
main.o: main.cpp
$(CC) $(CFLAGS) -o build/main.o -c main.cpp
clean:
rm build/*

52
ball.cpp Normal file
View File

@ -0,0 +1,52 @@
#include "ball.hpp"
#include "engine.hpp"
// TODO réécrire cette classe
Ball::Ball() {
if (!texture.loadFromFile("res/balle.png")) {
std::cerr << "Failed to load texture" << std::endl;
}
velocity.x = velocity.y = 0;
position.x = 150; position.y = 150;
sprite.setOrigin(12, 12);
sprite.setTexture(texture);
}
void Ball::update(float delta) {
int verticalPos = std::ceil(position.y);
// gravity
velocity.y += Engine::GRAVITY * delta;
// jump
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && verticalPos >= 150) {
velocity.y = -Engine::JUMP;
}
// go left/right
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
velocity.x -= Engine::MOVE;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
velocity.x += Engine::MOVE;
}
if (verticalPos > 150) {
position.y = 150;
velocity.y = -.3f * std::abs(velocity.y);
}
velocity.x *= .95f;
position += velocity * delta;
}
void Ball::draw(sf::RenderWindow& window) {
sprite.setPosition(position + sf::Vector2f(0, -12));
sprite.rotate(velocity.x / 10);
window.draw(sprite);
}

38
ball.hpp Normal file
View File

@ -0,0 +1,38 @@
#ifndef PTF_BALL_HPP
#define PTF_BALL_HPP
#include <SFML/Graphics.hpp>
#include <iostream>
class Ball {
protected:
sf::Vector2f position;
sf::Vector2f velocity;
float mass;
int charge;
public:
Ball();
void update(float delta);
void draw(sf::RenderWindow& window);
// getters et setters
sf::Vector2f getPosition() {
return position;
}
sf::Vector2f getVelocity() {
return velocity;
}
float getMass() {
return mass;
}
int getCharge() {
return charge;
}
};
#endif

BIN
build/ball.o Normal file

Binary file not shown.

BIN
build/engine.o Normal file

Binary file not shown.

BIN
build/main.o Normal file

Binary file not shown.

BIN
build/ptf Executable file

Binary file not shown.

117
engine.cpp Normal file
View File

@ -0,0 +1,117 @@
#include "engine.hpp"
#include <cmath>
/**
* Fonction d'initialisation de la classe Engine
*/
Engine::Engine() {
// initialise et ouvre la fenêtre
window.create(
sf::VideoMode(300, 150), "La cage de Jacob",
sf::Style::Default & ~sf::Style::Resize,
sf::ContextSettings(0, 0, 2)
);
window.setVerticalSyncEnabled(true);
// au démarrage, aucune touche n'est enfoncée
goLeftKey = false;
goRightKey = false;
// boucle d'événements sur la fenêtre
while (window.isOpen()) {
sf::Event event;
// traitement des évènements reçus
while (window.pollEvent(event)) {
// fermeture de la fenêtre
if (event.type == sf::Event::Closed) {
window.close();
}
// une touche a été enfoncée
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Left) {
goLeftKey = true;
}
if (event.key.code == sf::Keyboard::Right) {
goRightKey = true;
}
}
// une touche a été relâchée
if (event.type == sf::Event::KeyReleased) {
if (event.key.code == sf::Keyboard::Left) {
goLeftKey = false;
}
if (event.key.code == sf::Keyboard::Right) {
goRightKey = false;
}
}
}
// réinitialise l'horloge à zéro
// et stocke la différence de temps dans delta
float delta = clock.restart().asSeconds();
update(delta);
draw();
}
}
/**
* Mise à jour de la physique du jeu
*/
void Engine::update(float delta) {
for (int i = 0; i < balls.size(); i++) {
sf::Vector2f forces(0, 0);
// ajout de la force de gravité
forces += sf::Vector2f(0, Engine::GRAVITY);
// déplacement de la balle
if (goLeftKey) {
forces += sf::Vector2f(-Engine::MOVE, 0);
}
if (goRightKey) {
forces += sf::Vector2f(Engine::MOVE, 0);
}
// force d'attraction
for (int j = 0; j < balls.size(); j++) {
if (i != j) {
sf::Vector2f attraction(balls[i].getPosition() - balls[j].getPosition());
float distanceSquared = attraction.x * attraction.x + attraction.y * attraction.y;
attraction /= std::sqrt(distanceSquared);
attraction *= Engine::ATTRACTION * ((balls[i].getCharge() * balls[j].getCharge()) / distanceSquared);
forces += attraction;
}
}
// TODO: COLLISIONS
balls[i].update(forces, delta);
}
}
/**
* Dessine la scène du jeu couche par couche
*/
void Engine::draw() {
// couche de fond
window.clear(sf::Color(66, 165, 245));
// grille de blocs
for (int i = 0; i < blocks.size(); i++) {
blocks[i].draw();
}
// dessin des balles
for (int i = 0; i < balls.size(); i++) {
balls[i].draw();
}
}

33
engine.hpp Normal file
View File

@ -0,0 +1,33 @@
#ifndef PTF_ENGINE_HPP
#define PTF_ENGINE_HPP
#include <vector>
#include <SFML/Graphics.hpp>
#include "ball.hpp"
/**
* La classe principale Engine coordonne les éléments
* du jeu et organise le dessin des frames
*/
class Engine {
private:
sf::Clock clock;
sf::RenderWindow window;
bool goLeftKey;
bool goRightKey;
public:
static constexpr float GRAVITY = 10;
static constexpr float ATTRACTION = 10;
static constexpr float MOVE = 10;
std::vector<Ball> balls;
std::vector<std::vector<Block>> blocks;
Engine();
void draw();
void update(float delta);
};
#endif

12
main.cpp Normal file
View File

@ -0,0 +1,12 @@
#include "ball.hpp"
#include "engine.hpp"
int main() {
Engine engine;
Ball ball;
engine.balls.push_back(ball);
engine.start();
return 0;
}

BIN
res/balle.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.6 KiB

84733
res/balle.svg Normal file

File diff suppressed because it is too large Load Diff

After

Width:  |  Height:  |  Size: 6.2 MiB