How to set and change the view of class [Interceptor] in sf::Event /sfml

-2

I'd like to start with explaining what's the problem. Code is below the problem's explanation.

I'm writing the game, which:

-have View splitted for 9 parts (3x3)

-have background and class of car[Interceptor] (car can drive forward, backward, rotate etc.)

PROBLEM Car is able to translate and rotate on the background, and it's visible for player, but camera is not following the car and if we see one of nine screens (3x3) as a main one, while car is outside of this view (f.e. car has moved from screen 1 to screen 2) camera will not follow the car, but will stay in the position of the first screen.

Here's the thing i've made up - while I change in Interceptor "translate" into "move" camera will follow you but only forward and backward. So if you go straight, camera will change the screen 1st into 2nd, and 3rd. If you going backward camera will also change the screen from 3rd to 1st one. But if you rotate car, camera dont see it. It's not visible for it. I couldn't find other word for "rotate" which would be visible for "View".

I guess there is a problem with inheritance but i don't know how to declare to sf::Event my class, and whatsmore if I make public sf::View for my class there is a lot of problems caused by ambiguity.

I'd be grateful if you read this one and be able to find solution, how rotate an object in the way, that 1st screen will be able to follow the car into screen 4th or 9th.

This is my 1st post in here, so if I made smth wrong feel free to let me know about it. I hope code will be 'readable'. Thanks in advance. Igor

Code - Main

 #include <iostream>
 #include <SFML/Graphics.hpp>
 #include <cmath>
 #include <vector>
 #include "obejcts.cpp"
 using namespace std;

 sf::IntRect views[9];                                                                                    
 sf::Vector2f calculateCenter(sf::IntRect &r) { return sf::Vector2f(r.left+r.width/2,r.top + r.height/2); }  
 int getView(sf::Vector2f &vec) {                                                                           
    for (int i=0;i<9;i++) {
    if (views[i].contains(vec.x, vec.y)) return i;
     }
 return -1;
 }

int main() {
  setlocale(LC_ALL, "");                                      
  sf::RenderWindow W;                                         
  W.create(sf::VideoMode(800,600,32),"Game");                
  W.setActive(true);                                          
  W.setFramerateLimit(30);                                    
  W.setPosition(sf::Vector2i(0,0));

  int index=0;
    for (int x=0;x<3;x++) {
      for (int y=0;y<3;y++) {
         views[index].left = x*800;
         views[index].top = y*600;
         views[index].width = 800;
         views[index].height = 600;
      index++;
      }
    }

  sf::View cam(sf::Vector2f(800/2,600/2),sf::Vector2f(800,600));
  sf::View mapCam(sf::Vector2f(3*800/2, 3*600/2),sf::Vector2f(3*800,3*600));

  sf::View secCam(sf::Vector2f(800*3/2,600*3/2),sf::Vector2f(800*3,600*3));
  secCam.setViewport(sf::FloatRect(0,0,0.2,0.2));        

 sf::Texture tx;
 tx.loadFromFile("map.png");
 sf::Sprite background(tx);
 background.setPosition(0,0);
                                                            /
 Interceptor i (sf::Vector2f(185,130),"car_final.png",400,200);
 i.setPosition(sf::Vector2f(100,400));

sf::Event e;

  int iset,lasti;
    while (W.isOpen()) {
          while (W.pollEvent(e)) {
              if (e.type == sf::Event::Closed || 
             (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape) )) W.close();
          }
    sf::Vector2f ipos(i.getPosition());                                                     
    iset = getView(ipos);                                                                   
        if (iset!=-1 && iset!=lasti) {                                                        
           sf::Vector2f newc(calculateCenter(views[iset]));                                   
           cam.setCenter(   newc.x, newc.y );                                                 
           lasti = iset;                                          
        }

    if (iset==-1) i.setPosition(cam.getCenter());

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::RControl)) mapCam.rotate(1);
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::RShift)) mapCam.rotate(-1);
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::LControl)) W.setView(mapCam);
    else W.setView(cam);

    W.clear(sf::Color(0xffffffff));
    W.draw(background);
    i.checkEvent();
    W.draw(i,i.t);


    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Tab)) {
    W.setView(secCam);
    W.draw(background);
    W.draw(i,i.t);
    }

W.display();
}

return 0;
}

Code - Objects

#include <SFML/Graphics.hpp>
#include <vector>
#include <cmath>
#include <vector>

class Interceptor : public sf::RectangleShape{
  private:
    void _setFramePosition() {
      this->animFramePosition.push_back(sf::Vector2i(0,1*this->yFrameSize));
      this->animFramePosition.push_back(sf::Vector2i(0,2*this->yFrameSize));
      this->animFramePosition.push_back(sf::Vector2i(0,3*this->yFrameSize));
      this->stopFramePosition = sf::Vector2i(0,0);
    }

  public:
    int xFrameSize=200;
    int yFrameSize=200;
    size_t actualAnimIndex=0; 
    sf::Vector2i stopFramePosition;
    bool isMove = false; 
    bool isBackwardMove = false;
    bool isLRotate = false;
    bool isRRotate = false;
    int speed = 0;
    sf::Transform t;
    std::vector <sf::Vector2i> animFramePosition;
    bool stop = false;


Interceptor(const sf::Vector2f &vec, std::string filename, int xFrameSize, int yFrameSize)  {

    this->setSize(vec);
    this->xFrameSize=xFrameSize;
    this->yFrameSize=yFrameSize;
    this->_setFramePosition();
    sf::Texture *tex = new sf::Texture;
    tex->loadFromFile(filename);
    this->setTexture(tex,true);
    this->updateTextureRect();
    this->setOrigin(this->getSize().x/2,this->getSize().y/2);
    this->t = sf::Transform::Identity;
    this->t.rotate(this->getRotation()); 

}

/** Drawing Texture */

void updateTextureRect() {


  if (this->isMove) {
     int all = this->animFramePosition.size();
     this->actualAnimIndex = (this->actualAnimIndex + 5) % all;
     this->setTextureRect(sf::IntRect(
        this->animFramePosition[this->actualAnimIndex].x, this->animFramePosition[this->actualAnimIndex].y, this->xFrameSize, this->yFrameSize
     ));
     this->stop = false;


     //this->t.translate(sf::Vector2f((this->speed), 0 )) ; !!! PROBLEM HERE!!!
     this->move(sf::Vector2f((this->speed), 0 )) ;

  }


  else if (this->isBackwardMove) {
    int all = this->animFramePosition.size();
    this->actualAnimIndex = (this->actualAnimIndex + 5) % all;
    this->setTextureRect(sf::IntRect(
        this->animFramePosition[this->actualAnimIndex].x, this->animFramePosition[this->actualAnimIndex].y, this->xFrameSize, this->yFrameSize
     ));
   this->stop = false;
    this->move(sf::Vector2f(((-4)), 0 )) ;
  }

  else {


     if (!this->stop) {
        this->setTextureRect(sf::IntRect(
           this->stopFramePosition.x, this->stopFramePosition.y, this->xFrameSize, this->yFrameSize
        ));
        this->stop = true;

     }
  }
  if (this->isLRotate) this->t.rotate(-5,this->getPosition());
  if (this->isRRotate) this->t.rotate(5,this->getPosition());

}


/** Event test */

  void checkEvent() {



   if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
     this->isMove=true;
     if (sf::Keyboard::isKeyPressed(sf::Keyboard::LShift)) this->speed = 8;
     else this->speed = 6;
  }
  else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
    this->isBackwardMove= true;

  }

    else {
     this->isMove=false;
     this->isBackwardMove=false;
     this->speed = 0;
  }
  if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
     this->isLRotate = true;
  } else {
     this->isLRotate = false;
  }
  if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
     this->isRRotate = true;
  } else {
     this->isRRotate = false;
  }

  this->updateTextureRect();
 }

 };
c++
inheritance
view
rotation
sfml
asked on Stack Overflow Nov 4, 2018 by IKlauz

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0