Hello. I've looked on screen scrolling and one of the way to do it is to use the sf::View::setViewport(sf::floatRect( left co-dt, top co-dt, rect width, rect height).
But the problem now is that even after I write the code as below (following some tutorials on SFML 1.6, and I understand how it works, at least on SFML 1.6), the camera still doesn't move even after I walk the character towards the center of the screen.
The sf:floatRect effects is there (appears like it zoomed out of the screen, not that I intended to do so), but still the screen isn't scrolling like I thought it would. I think I'm confused this using the setFromRect SFML 1.6 with setViewPort SFML 2.0
Here's the codes:
Main.cpp
Code:
#include <SFML/Graphics.hpp>
#include <iostream>
#include "Global.h"
#include "Player.h"
#include "Map.h"
int main()
{
sf::RenderWindow Window(sf::VideoMode(ScreenWidth,ScreenHeight,32),"SFML ADVANCED SPRITE TUTORIAL 2" );
Player player;
Camera camera;
Map map;
player.LoadTexture();
player.Initialize();
camera.Initialize();
map.loadMap();
while(Window.isOpen())
{
sf::Event Event;
while(Window.pollEvent(Event))
{
if(Event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
Window.close();
}
}//END EVENT LOOP
player.Update(Window);
camera.Update( player.getCameraX() , player.getCameraY() );
Window.clear();
map.Draw(Window);
player.Draw(Window);
Window.display();
system("cls");
}
return 0;
}
Animation.h
Code:
#ifndef ANIMATION_H
#define ANIMATION_H
class Animation
{
public:
Animation();
virtual ~Animation();
void setTexture(sf::Texture &tempTexture);
void Draw(sf::RenderWindow &Window);
void Update(sf::RenderWindow &Window);
void Initialize(float &x, float &y, int amountOfFramesX, int amountOfFramesY, float textureWidth, float textureHeight);
float getPositionX(float x);
float getPositionY(float y);
float getFrameHeight();
float getFrameWidth();
bool getMoveActive();
void setMoveActive(bool MoveActive);
void setCurrentFrameY(int FrameY);
float getClock();
protected:
private:
sf::Texture tempTexture;
sf::Sprite tempSprite;
sf::Clock clock;
float x, y;
float move_x;
float move_y;
float frameHeight, frameWidth;
int amountOfFramesX, amountOfFramesY;
int textureWidth, textureHeight;
bool MoveActive_activated;
int currentFrameX, currentFrameY;
float frameTime;
};
#endif // ANIMATION_H
Animation.cpp
Code:
#include <SFML/Graphics.hpp>
#include <iostream>
#include "Animation.h"
Animation::Animation()
{
//ctor
}
Animation::~Animation()
{
//dtor
}
bool Animation::getMoveActive()
{
return MoveActive_activated;
}
float Animation::getClock()
{
return clock.getElapsedTime().asSeconds();
}
void Animation::setMoveActive(bool MoveActive)
{
MoveActive_activated = MoveActive;
}
void Animation::setCurrentFrameY(int FrameY)
{
currentFrameY = FrameY;
}
void Animation::Initialize(float &x, float &y, int amountOfFramesX , int amountOfFramesY, float textureWidth, float textureHeight)
{
this-> x = x;
this-> y = y;
this-> amountOfFramesX = amountOfFramesX;
this-> amountOfFramesY = amountOfFramesY;
this-> textureWidth = textureWidth;
this-> textureHeight = textureHeight;
currentFrameY = 0;
}
float Animation::getFrameWidth()
{
frameWidth = (float) textureWidth / amountOfFramesX;
return frameWidth;
}
float Animation::getFrameHeight()
{
frameHeight = (float) textureHeight / amountOfFramesY;
return frameHeight;
}
float Animation::getPositionX(float x)
{
move_x = x;
return move_x;
}
float Animation::getPositionY(float y)
{
move_y = y;
return move_y;
}
void Animation::Update(sf::RenderWindow &Window)
{
std::cout << "setPosition move_x: " << move_x << std::endl;
std::cout << "setPosition move_y: " << move_y << std::endl;
std::cout << "frameWidth: " << getFrameWidth() << std::endl;
std::cout << "frameHeight: " << getFrameHeight() << std::endl;
std::cout << "Texture width: " << textureWidth << std::endl;
std::cout << "Texture height: " << textureHeight << std::endl;
std::cout << "Amount of frames X: " << amountOfFramesX << std::endl;
std::cout << "Amount of frames Y: " << amountOfFramesY << std::endl;
std::cout << "-----------" << std::endl;
std::cout << "currentFrameX: " << currentFrameX << std::endl;
std::cout << "currentFrameY: " << currentFrameY << std::endl << std::endl;
std::cout << "In tempSprite.setTextureRect(sf::IntRect(x1,y1,frameWidth,frameHeight)) : " << std::endl << std::endl;
std::cout << "x1: " << currentFrameX * frameWidth << std::endl;
std::cout << "y1: " << currentFrameY * frameHeight << std::endl;
std::cout << "frameHeight: " << frameWidth << std::endl;
std::cout << "frameWidth: " << frameHeight << std::endl;
std::cout << "frameTime: " << frameTime << std::endl;
if(MoveActive_activated)
{
frameTime = getClock();
std::cout <<"[MoveActive_activated is now TRUE] " << std::endl;
if ( frameTime > 0.2) //change to next currentFrameX after 2 second.
{
currentFrameX++;
clock.restart();
if( currentFrameX >= amountOfFramesX)
{
currentFrameX = 0;
}
}
}
else
{
currentFrameX = 1;
}
tempSprite.setTextureRect(sf::IntRect( currentFrameX * frameWidth , currentFrameY * frameHeight , frameWidth , frameHeight ));
tempSprite.setPosition(move_x,move_y);
}
void Animation::setTexture(sf::Texture &tempTexture)
{
tempSprite.setTexture(tempTexture);
}
void Animation::Draw(sf::RenderWindow &Window)
{
Window.draw(tempSprite);
}
Player.h
Code:
#ifndef PLAYER_H
#define PLAYER_H
#include "Animation.h"
#include "Camera.h"
class Player
{
public:
Player();
virtual ~Player();
void LoadTexture();
void Initialize();
void Draw(sf::RenderWindow &Window);
void Update(sf::RenderWindow &Window);
float getCameraX();
float getCameraY();
protected:
Animation playerAnimation;
Camera camera;
private:
sf::Texture playerTexture;
int textureWidth, textureHeight;
float x , y;
float moveSpeed;
};
#endif // PLAYER_H
Player.cpp
Code:
#include <SFML/Graphics.hpp>
#include <iostream>
#include "Player.h"
#include "Animation.h"
Player::Player()
{
//ctor
}
Player::~Player()
{
//dtor
}
void Player::LoadTexture()
{
if(!playerTexture.loadFromFile("C:/Program Files (x86)/CodeBlocks/Projects/SFML ANIMATION ADVANCED TUTORIAL 2/resources/Sprite animation/$Cloaked dude.png"))
{
std::cout << "Error loading image\n";
}
else
{
playerAnimation.setTexture(playerTexture);
std::cout << "Loaded the image.png" << std::endl;
// system("pause");
}
}
void Player::Initialize()
{
std::cout << "Initializing from Player::Initialize()..." << std::endl;
x = 0;
y = 0;
moveSpeed = 25;
textureWidth = playerTexture.getSize().x;
textureHeight = playerTexture.getSize().y;
playerAnimation.Initialize(x,y,3,4,textureWidth,textureHeight);
}
void Player::Update(sf::RenderWindow &Window)
{
if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)) //UP
{
playerAnimation.setMoveActive(true);
y -= moveSpeed * playerAnimation.getClock();
playerAnimation.setCurrentFrameY(3);
std::cout << "Moved up" << std::endl;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)) //DOWN
{
playerAnimation.setMoveActive(true);
y += moveSpeed * playerAnimation.getClock();
playerAnimation.setCurrentFrameY(0);
std::cout << "Moved down" << std::endl;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) //LEFT
{
playerAnimation.setMoveActive(true);
x -= moveSpeed * playerAnimation.getClock();;
playerAnimation.setCurrentFrameY(1);
std::cout << "Moved left" << std::endl;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)) //RIGHT
{
playerAnimation.setMoveActive(true);
x += moveSpeed * playerAnimation.getClock();
playerAnimation.setCurrentFrameY(2);
std::cout << "Moved right" << std::endl;
}
else
{
playerAnimation.setMoveActive(false);
std::cout <<"[MoveActive_activated is now FALSE] " << std::endl;
}
std::cout << "x-position: " << x << std::endl;
std::cout << "y-position: " << y << std::endl;
std::cout << "moveSpeed : " << moveSpeed << std::endl;
playerAnimation.getPositionX(x);
playerAnimation.getPositionY(y);
playerAnimation.Update(Window);
camera.Update(x,y);
}
void Player::Draw(sf::RenderWindow &Window)
{
playerAnimation.Draw(Window);
std::cout << "Player::Draw is ON" << std::endl;
}
float Player::getCameraX()
{
return x;
}
float Player::getCameraY()
{
return y;
}
Camera.h
Code:
#ifndef CAMERA_H
#define CAMERA_H
#include <SFML/Graphics.hpp>
class Camera
{
public:
Camera();
virtual ~Camera();
void Initialize();
void Update(float playerX, float playerY);
void Draw(sf::RenderWindow &Window);
void setView(sf::View CameraPosition);
sf::View getView();
protected:
sf::View CameraPosition;
float cameraX , cameraY;
private:
};
#endif // CAMERA_H
Camera.cpp
Code:
#include <SFML/Graphics.hpp>
#include <iostream>
#include "Player.h"
#include "Camera.h"
#include "Global.h"
Camera::Camera()
{
//ctor
}
Camera::~Camera()
{
//dtor
}
void Camera::Initialize()
{
cameraX = 0;
cameraY = 0;
}
void Camera::Update(float playerX, float playerY)
{
cameraX = -(ScreenWidth/2) + playerX;
cameraY = -(ScreenHeight/2) + playerY;
if(cameraX < 0)
{cameraX = 0;}
if(cameraY < 0)
{cameraY = 0;}
CameraPosition.setViewport(sf::FloatRect( playerX , playerY , ScreenWidth , ScreenHeight ));
setView(CameraPosition);
std::cout << "---CAMERA UPDATE---" << std::endl;
std::cout << "playerX: " << playerX << std::endl;
std::cout << "playerY: " << playerY << std::endl;
std::cout << "cameraX: " << cameraX << std::endl;
std::cout << "cameraY: " << cameraY << std::endl;
}
//void Camera::Draw(sf::RenderWindow &Window)
//{
// Window.setView(CameraPosition);
//}
void Camera::setView(sf::View CameraPosition)
{
this-> CameraPosition = CameraPosition;
}
sf::View Camera::getView()
{
return CameraPosition;
}
Map.h
Code:
#ifndef MAP_H
#define MAP_H
#include <SFML/Graphics.hpp>
#include <iostream>
#include "Global.h"
#include "Camera.h"
class Map
{
public:
Map();
virtual ~Map();
void loadMap();
void Draw(sf::RenderWindow &Window);
protected:
Camera camera;
private:
sf::Texture map;
sf::Sprite mapSprite;
sf::View mapPosition;
};
#endif // MAP_H
Map.cpp
Code:
#include "Map.h"
Map::Map()
{
//ctor
}
Map::~Map()
{
//dtor
}
void Map::loadMap()
{
if(map.loadFromFile("C:/Program Files (x86)/CodeBlocks/Projects/SFML ANIMATION ADVANCED TUTORIAL 2/resources/Background/CodingMadeEasy Background.png"))
mapSprite.setTexture(map);
}
void Map::Draw(sf::RenderWindow &Window)
{
mapPosition = camera.getView();
Window.setView(mapPosition);
Window.draw(mapSprite);
}
Global.h
Code:
#ifndef GLOBAL_H_INCLUDED
#define GLOBAL_H_INCLUDED
#define ScreenWidth 800
#define ScreenHeight 600
#endif // GLOBAL_H_INCLUDED
If you're wondering who's tutorial, its CodingMadeEasy's tuts. I try to avoid reading