Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Help with drawing a movable sprite in game states  (Read 1148 times)

0 Members and 1 Guest are viewing this topic.

MrJBRPG

  • Newbie
  • *
  • Posts: 1
    • View Profile
Help with drawing a movable sprite in game states
« on: January 22, 2015, 07:51:50 pm »
Using: Windows Visual Studio 2013 C++ on Windows 8.1 SFML version 2.1

Right now I am working on a solo project, and I am nearly finished with getting a small ship to show up on screen. I understood some of the coding structures from Project Shmup and City Builder Tutorial. However, I am having a pointer-based problem to get the player sprite to show up on screen.



Here are some code snippets relevant to how I am treating the player sprite.


Game.cpp



#include <stack>


#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>


#include "game.hpp"
#include "state.hpp"
#include "textureManager.hpp"


void Game::loadTextures(){


   texmgr.loadTexture("playerSprite", "media/images/player.png");
   texmgr.loadTexture("background", "media/images/background.png");
}


void Game::pushState(State* state)
{
   this->states.push(state);


   return;
}


void Game::popState()
{
   delete this->states.top();
   this->states.pop();


   return;
}


void Game::changeState(State* state)
{
   if (!this->states.empty())
      popState();
   pushState(state);


   return;
}


State* Game::peekState()
{
   if (this->states.empty()) return nullptr;
   return this->states.top();
}


void Game::gameLoop()
{
   sf::Clock clock;


   while (this->window.isOpen())
   {
      sf::Time elapsed = clock.restart();
      float dt = elapsed.asSeconds();


      if (peekState() == nullptr) continue;
      peekState()->handleInput();
      peekState()->update(dt);
      this->window.clear(sf::Color::Black);
      peekState()->draw(dt);
      this->window.display();
   }
}


Game::Game()
{
   this->loadTextures();


   this->window.create(sf::VideoMode(800, 600), "Solo Project Ver 0.0");
   this->window.setFramerateLimit(60);


   this->background.setTexture(this->texmgr.getRef("background"));
}


Game::~Game()
{
   while (!this->states.empty()) popState();
}




stateGame.cpp


#include <SFML\Graphics.hpp>


#include "state_game.hpp"
#include "game.hpp"
#include "player.hpp"


StateGame::StateGame(Game* game){
   
   this->game = game;
   sf::Vector2f pos = sf::Vector2f(this->game->window.getSize());
   this->hudView.setSize(pos);
   this->gameView.setSize(pos);
   pos *= 0.5f;
   this->hudView.setCenter(pos);
   this->gameView.setCenter(pos);


}


void StateGame::draw(const float dt){


   this->game->window.setView(this->gameView);


   this->game->window.clear(sf::Color::Black);
   this->game->window.draw(this->game->background);
   this-> game->window.draw(this->game->playerSprite);
}


void StateGame::update(const float dt){


}


void StateGame::handleInput(){
   sf::Event event;


   while (this->game->window.pollEvent(event)){


      if (event.type == sf::Event::Closed){
         game->window.close();
      }


      bool dirButtons[4] = {false, false, false, false};
      if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
         dirButtons[0] = true;
      }
      if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
         dirButtons[1] = true;
      }
      if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
         dirButtons[2] = true;
      }
      if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
         dirButtons[3] = true;
      }
      gamePlayer->move(dirButtons);
   }
}


player.cpp



#include "player.hpp"
#include "textureManager.hpp"


Player::Player(){
   this->playerSprite.setTexture(this->texmgr.getRef("playerSPrite"));


   this->lives = 3;
   this->speed = 200;
   this->firepower = 1;


}


void Player::move(bool* dirButtons){
   // move ship based on arrow keys pressed


   double vel_x = 0;
   double vel_y = 0;


   vel_x = -speed * double(dirButtons[0]); // left
   vel_x += speed * double(dirButtons[2]); // right


   vel_y = -speed * double(dirButtons[1]); // up
   vel_y += speed * double(dirButtons[3]); // down


   double sqrt2 = 1.41; // square root of 2


   if ((dirButtons[0] || dirButtons[2]) && (dirButtons[1] || dirButtons[3])){
      vel_x /= sqrt2;
      vel_y /= sqrt2;
   }
   this->playerSprite.move(vel_x, vel_y);
}


double Player::getSpeed(){
   return this->speed;
}




G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: Help with drawing a movable sprite in game states
« Reply #1 on: January 22, 2015, 08:02:53 pm »
And the problem is?

 

anything