Just wanted to make sure im setting up my game in the ideal way.
From what I've learnt at Uni the last semester I figure using classes in separate .cpp/header files to get/set sprites is the way to go but any critique about my implementation methods is very welcomed!
Main Game Loop:#include "SFML/Graphics.hpp"
#include "cmath"
#include "kf/kf_log.h"
#include "qgf2d/system.h"
#include "qgf2d/anim.h"
#include "hayleigh/player.h"
#include "hayleigh/levels.h"
#include "hayleigh/text.h"
using namespace std;
using namespace qgf;
using namespace kf;
using namespace sf;
int main()
{
initDirectory();
Log::getDefault().addCout();
Log::getDefault().addFile("base.log");
kf_log("Start Tute 2 program.");
RenderWindow window(VideoMode(800, 600, 32), "Hayleigh_Blair GAM201 - Tutorial 2 - Animation");
sf::Clock clock; //Setup delta time clock
window.setMouseCursorVisible(false); //Hide default cursor
//Create Player
Player *aPlayer = new Player();
aPlayer->SetSprite();
aPlayer->SetAnim();
//Load Levels
Levels *theLevel = new Levels();
theLevel->SetLevel1();
//Load Text
TextHandler *printText = new TextHandler();
printText->SetFont();
//Begin Game Loop
while (window.isOpen())
{
float deltaT = clock.restart().asSeconds(); //reset clock, deltaT = clock data 'as Seconds'.
aPlayer->Update();
printText->BlendText(); //Fade in/out alpha & Color range
theLevel->HandleCursor(window); //Draw circle to cursor
Event ev;
while (window.pollEvent(ev))
{
if ((ev.type == Event::Closed) || ((ev.type == Event::KeyPressed) && (ev.key.code == Keyboard::Escape)))
{
window.close();
break;
}
}
//Play Idle Animation
aPlayer->Idle();
//Detect Input for aPlayer
aPlayer->move();
//Draw to Window
window.clear();
window.draw(theLevel->GetBG());
window.draw(theLevel->GetBGfill());
window.draw(theLevel->GetShapes());
window.draw(aPlayer->GetSprite());
window.draw(printText->GetLevelName(), sf::BlendAlpha);
window.draw(theLevel->GetCirc());
window.display();
}
return 0;
}
Player .cpp#include "hayleigh/player.h"
//Get Sprite (used by window.draw)
sf::Sprite Player::GetSprite() const
{
return playSprite;
};
//Set Sprite
void Player::SetSprite()
{
float speed_y = 0;
player = onGround;
playTexture.loadFromFile("data/gabe.png");
playSprite = sf::Sprite(playTexture);
playSprite.setPosition(sf::Vector2f(100,500));
playSprite.setOrigin(23, 63);
};
//Set Animation
void Player::SetAnim()
{
playAnim = qgf::Anim (&playSprite, 0, 0, 46, 64, 6, 2);
playAnim.addSequence("idle", 0, 5, 2.0f, true);
playAnim.addSequence("run", 6, 11, 0.8f, true);
}
//Start Idle animation
void Player::Idle()
{
playAnim.play("idle", false);
playAnim.update(deltaT);
}
//Update(Sync delta)
void Player::Update()
{
deltaT = clock.restart().asSeconds();
}
//Update animation
void Player::UpdateAnim()
{
playAnim.play("run", false);
playAnim.update(deltaT);
}
//Move Player
void Player::move()
{
//Move Left
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
kf::Vector2 position = playSprite.getPosition();
playSprite.setScale(-1,1);
position.x -= 120.0f * deltaT;
UpdateAnim();
playSprite.setPosition(position);
}
//Move Right
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
kf::Vector2 position = playSprite.getPosition();
playSprite.setScale(1,1);
position.x += 120.0f * deltaT;
UpdateAnim();
playSprite.setPosition(position);
}
//Jump
switch(player)
{
case onGround:
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
{
kf::Vector2 position = playSprite.getPosition();
if (position.y <= 460)
{
player = falling;
}
else if (position.y > 460)
{
position.y -= 120.0f * deltaT;
UpdateAnim();
playSprite.setPosition(position);
}
break;
}
case falling:
kf::Vector2 position = playSprite.getPosition();
if(position.y > 500)
{
player = onGround;
}
else if(position.y < 500)
{
position.y += 120.0f * deltaT;
UpdateAnim();
playSprite.setPosition(position);
}
break;
}
}
Player Header#ifndef PLAYER_H
#define PLAYER_H
#include "SFML/Graphics.hpp"
#include "cmath"
#include "qgf2d/system.h"
#include "qgf2d/anim.h"
class Player
{
public:
sf::Sprite Player::GetSprite() const;
void Player::SetSprite();
void Player::Update();
void Player::move();
void Player::Idle();
void Player::SetAnim();
void Player::UpdateAnim();
private:
float deltaT;
enum playerState {onGround, falling} player;
sf::Texture playTexture;
sf::Sprite playSprite;
sf::Clock clock;
qgf::Anim playAnim;
};
#endif PLAYER_H