So I'm building a game and it requires keyboard input. For some reason god know why the events didn't work so i moved on to the isKeyPressed function and it does not work at all. The code is bellow. Can some one please tell me what is wrong with my code?
HexGrid.hpp
#ifndef HEXGRID_HPP_
#define HEXGRID_HPP_
#include <SFML/Graphics.hpp>
#include <iostream>
#include <cmath>
class HexGrid {
public:
HexGrid(int height, int width, float radius, sf::Color color);
void draw(sf::RenderWindow &App);
private:
sf::CircleShape hexagon;
sf::Color defColor;
int defHeight;
int hexNumber;
int defWidth;
int yRow;
float defRadius;
float xPos;
float yPos;
float yKeyPos;
float xKeyPos;
sf::Vector2f keyPos;
sf::FloatRect hexBounds;
};
#endif
HexGrid.cpp
#include "HexGrid.hpp"
HexGrid::HexGrid(int height, int width, float radius, sf::Color color){
defHeight = height;
defWidth = width;
defRadius = radius;
defColor = color;
hexNumber = width * height;
yRow = 1;
}
void HexGrid::draw(sf::RenderWindow &App){
hexagon.setRadius(defRadius);
hexagon.setPointCount(6);
hexagon.setFillColor(defColor);
//sf::Vector2f poshex = hexagon.getPoint(2);
xPos = hexagon.getRadius();
yPos = hexagon.getRadius();
// xKeyPos = hexagon.getRadius();
// yKeyPos = hexagon.getRadius();
for(int i = 1; i <= hexNumber; ++i){
hexagon.setPosition(xPos, yPos);
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up) == true)
yKeyPos = yKeyPos - ( hexagon.getRadius() * 2 );
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down) == true)
yKeyPos = yKeyPos + ( hexagon.getRadius() * 2);
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right) == true)
xKeyPos = xKeyPos + ( hexagon.getRadius() * 2);
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) == true)
xKeyPos = xKeyPos - ( hexagon.getRadius() * 2);
hexBounds = hexagon.getGlobalBounds();
keyPos = sf::Vector2f(xKeyPos,yKeyPos);
if( i % defWidth == 0 && i != 0 ){
if (yRow % 2 == 0){
xPos = hexagon.getRadius();
yPos += hexagon.getRadius() * 1.6;
}
else{
xPos = hexagon.getRadius() * 1.822;
yPos += hexagon.getRadius() * 1.6;
}
++yRow;
}
else
xPos += hexagon.getRadius() * 1.82;
if( hexBounds.contains(keyPos) ){
hexagon.setOutlineThickness(5);
hexagon.setOutlineColor(sf::Color(50,50,50));
App.draw(hexagon);
}
else{
hexagon.setOutlineThickness(0);
App.draw(hexagon);
}
}
}
main.cpp
#include "HexGrid.hpp"
int main(){
sf::RenderWindow app(sf::VideoMode(800,600,32),"Tyrion: TCoW");
HexGrid hex(10,10,30.f,sf::Color(250,0,0));
while(app.isOpen()){
sf::Event evt;
while(app.pollEvent(evt)){
if(evt.type == sf::Event::Closed)
app.close();
}
app.clear(sf::Color(250,250,250));
hex.draw(app);
//HexGrid(5, 5, 30, sf::Color(50,50,50),app);
app.display();
}
return 0;
}