How the Pong.cpp file know the implementation of the functions in Bat.cpp if it doesn't include Bat.cpp? Is that magic? I want to know what's happening? I am coming from Python programming language and its obvious in Python classes because their implementation of functions come with the signatures.
Here are all the files I am working with:
Bat.h
#pragma once
#include <SFML/Graphics.hpp>
using namespace sf;
class Bat
{
private:
Vector2f m_Position;
// A RectangleShape object
RectangleShape m_Shape;
float m_Speed = 1000.0f;
bool m_MovingRight = false;
bool m_MovingLeft = false;
public:
Bat(float startX, float startY);
FloatRect getPosition();
RectangleShape getShape();
void moveLeft();
void moveRight();
void stopLeft();
void stopRight();
void update(Time dt);
};
Bat.cpp
#include "Bat.h"
// This is the constructor and it is called when we create an object
Bat::Bat(float startX, float startY)
{
m_Position.x = startX;
m_Position.y = startY;
m_Shape.setSize(sf::Vector2f(100, 5));
m_Shape.setPosition(m_Position);
}
FloatRect Bat::getPosition()
{
return m_Shape.getGlobalBounds();
}
RectangleShape Bat::getShape()
{
return m_Shape;
}
void Bat::moveLeft()
{
m_MovingLeft = true;
}
void Bat::moveRight()
{
m_MovingRight = true;
}
void Bat::stopLeft()
{
m_MovingLeft = false;
}
void Bat::stopRight()
{
m_MovingRight = false;
}
void Bat::update(Time dt)
{
if (m_MovingLeft)
{
m_Position.x -= m_Speed * dt.asSeconds();
}
if (m_MovingRight)
{
m_Position.x += m_Speed * dt.asSeconds();
}
m_Shape.setPosition(m_Position);
}
Pong.cpp
#include "Bat.h"
#include <sstream>
#include <cstdlib>
#include <SFML/Graphics.hpp>
int main()
{
// Create a video mode object
VideoMode vm(1920, 1080);
// Create and open a window for the game
RenderWindow window(vm, "Pong", Style::Fullscreen);
int score = 0;
int lives = 3;
// Create a bat at the bottom center of the screen
Bat bat(1920 / 2, 1080 - 20);
// We will add a ball in the next chapter
// Create a Text object called HUD
Text hud;
// A cool retro-style font
Font font;
font.loadFromFile("fonts/DS-DIGI.ttf");
// Set the font to our retro-style
hud.setFont(font);
// Make it nice and big
hud.setCharacterSize(75);
// Choose a color
hud.setFillColor(Color::White);
hud.setPosition(20, 20);
// Here is our clock for timing everything
Clock clock;
while (window.isOpen())
{
/*
Handle the player input
****************************
****************************
****************************
*/
/*
Update the bat, the ball and the HUD
*****************************
*****************************
*****************************
*/
/*
Draw the bat, the ball and the HUD
*****************************
*****************************
*****************************
*/
}
return 0;
}