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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - lockandstrike

Pages: 1 [2] 3
16
System / isKeyPressed() function not working
« on: November 12, 2013, 11:19:43 am »
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;
}
 

17
Graphics / Re: Loading tile maps
« on: August 12, 2013, 12:19:50 pm »
I've followed some of his tutorials so I can tell you one thing you are doing wrong your'e using the easy way. Always go for the Intermediate or the Hard way because they are harder but more effective. Anyway here as code that should work:

#include <SFML/Graphics.hpp>
#include <iostream>
#include <cctype>
#include <fstream>
#include <vector>
#include <string>

int main()
{
    std::ifstream openfile("Map.txt");

    sf::Texture tileTexture;
    sf::Sprite tiles;

    std::vector<std::vector<sf::Vector2i> > map;
    std::vector<sf::Vector2i> tempMap;

    while(openfile.is_open()){
       
        std::string tileLocation;
        openfile >> tileLocation;

        if(!tileTexture.loadFromFile(tileLocation)){
            return EXIT_FAILURE;
        }
        tiles.setTexture(tileTexture);

        while(!openfile.eof()){

            std::string tile;
            openfile >> tile;

            char x = tile[0], y = tile [2];
            if(!isdigit(x) || !isdigit(y)){
                tempMap.push_back(sf::Vector2i(-1,-1));    
            }
            else{
                tempMap.push_back(sf::Vector2i(x - '0', y - '0'));
            }

            if (openfile.peek() == '/n'){
                map.push_back(tempMap);
                temMap.clear();
            }
        }
       
        map.push_back(tempMap);
    }

    sf::RenderWindow Window (sf::VideoMode(800,600,32),"SFML");

    while(!Window.isOpen()){

        sf::Event event;

        if(event.type == sf::Event::Closed){
            Window.close();
        }

        Window.clear();

        for(int i = 0; i < map.size(); i++){
            for(int j = 0; j < map[i].size(); j++){
                if(map[i][j].x != -1 && map[i][j].y != -1){
                    tiles.setPosition(j * 32, i * 32);
                    tiles.setTextureRect(sf::IntRect(map[i][j].x * 32, map[i][j].y * 32, 32, 32));
                    Window.draw(tiles);
                }
            }
        }

        Window.display();
       
    }
}
 

And this how your Map.txt file should be:

Map.png
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x
x,x x,x x,x x,x 0,1 1,0 1,1 x,x x,x x,x
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x
0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0

This one is the one I use but it's not the most effective. If you want the most effective of them all watch codingmadeeasy tutorial on Loading Tile Maps[Hard];

18
General / Re: Game window not working
« on: August 11, 2013, 10:34:54 pm »

Then, I created a new console app, went to its project build options and in the linker setting tab added these:
for debug,

sfml-graphics-d
sfml-system-d
sfml-window-d

for release,
sfml-graphics
sfml-system
sfml-window


The gcc toolchain requires that libs are declared in a certain order. Because of sfml-graphics lib depending on sfml-sytem and sfml-window try to add the libs in this order:

For debug:

sfml-system-d
sfml-window-d
sfml-graphics-d

For release:

sfml-system
sfml-window
sfml-graphics


And also i don't know how to do it in Code::Blocks but add the 'SFML_DYNAMIC' preprocessor to the project settings.

19
General / Re: Game window not working
« on: August 10, 2013, 09:50:30 pm »
It's already been said, but in your code, where there is a "if(GameWindow.pollEvent(GameEvent))" statement there should be a "while(GameWindow.pollEvent(GameEvent))" statement. Change the code and post the result of that.

20
Graphics / Re: Loading charset
« on: August 09, 2013, 11:43:43 am »
Thanks Ivan. It worked. I can finally load all my textures right. Thanks alot.

21
Graphics / Re: Loading charset
« on: August 08, 2013, 09:12:35 pm »
I'm gonna paste the entire main.cpp code just dont give a crap about commented code.

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


const int AppHeight = 540;
const int AppWidth = 1020;

void defCharset(sf::Sprite *, sf::Sprite *, sf::Sprite *, std::string);

int main()
{
  sf::RenderWindow Window(sf::VideoMode(AppWidth,AppHeight,32),"TESTE", sf::Style::Close | sf::Style::Titlebar);

  //sf::Music backgroundMusic;

  /*if(!backgroundMusic.openFromFile("Resource/FromHere.ogg")){
          return EXIT_FAILURE;
    }*/

////////////////////////////////////////////////////////////////////////

  float x = 90;
  float y = 40;

  int mute = 0;

  sf::Sprite gmChar_sm;
  sf::Sprite gmChar_s1;
  sf::Sprite gmChar_s2;

  defCharset(&gmChar_sm, &gmChar_s1, &gmChar_s2,"Resource/charsetfinal.png");


  gmChar_sm.setPosition(x,y);

////////////////////////////////////////////////////////////////////////



  //backgroundMusic.play();

  while(Window.isOpen()){

    sf::Event event;
    while(Window.pollEvent(event)){

      switch(event.type){

      case sf::Event::Closed:
          Window.close();
          break;

      case sf::Event::KeyPressed:
          switch(event.key.code){
          case sf::Keyboard::Escape:
                  Window.close();
                  break;

          case sf::Keyboard::Down:

                  y++;

                  gmChar_sm.setPosition(x,y);

                  break;

          case sf::Keyboard::Up:

                  y--;

                  gmChar_sm.setPosition(x,y);

                  break;

          case sf::Keyboard::Right:

                  x++;

                  gmChar_sm.setPosition(x,y);

                  break;

          case sf::Keyboard::Left:

                  x--;

                  gmChar_sm.setPosition(x,y);

                  break;

          case sf::Keyboard::M:
                  if (mute == 0){
                          mute++;
                  }
                  else if(mute == 1){
                          mute--;
                  }

                  if(mute == 1){
                          //backgroundMusic.stop();
                  }
                  else if(mute == 0){
                          //backgroundMusic.play();
                  }

                  break;
          }
          break;

      case sf::Event::LostFocus:
          //backgroundMusic.pause();
          break;

      case sf::Event::GainedFocus:
          if(mute == 0){
                  //backgroundMusic.play();
          }

          else{

          }
          break;

      default:
          break;

      }

      Window.clear(sf::Color(250,0,0));
      Window.draw(gmChar_sm);
      Window.display();

    }
  }
}
 

22
Graphics / Re: Loading charset
« on: August 08, 2013, 07:45:07 pm »
I think I've made a breakthrough but I'm still getting the error I posted in the OP. The game opens and closes in the blink of an eye. But anyway here is my code. If could tell what I'm doing wrong I'd be really appreciated.

main.cpp

#include <SFML/Graphics.hpp>

void defCharset(sf::Sprite *,sf::Sprite *,sf::Sprite *,std::string );

//RenderWindow and bla,bla,bla...

sf::Sprite gmChar_sm;
sf::Sprite gmChar_s1;
sf::Sprite gmChar_s2;

defCharset(&gmChar_sm, &gmChar_s1, &gmChar_s2, "Resource/charsetfinal.png");

//game loop and all the other stuff
 

chars.cpp

#include <SFML/Graphics.hpp>

void defCharset(sf::Sprite *gmCharacther_semiIdle,sf::Sprite *gmCharacther_stage1,sf::Sprite *gmCharacther_stage2, std::string filename)
{
        sf::Texture gmChar;
        if (!gmChar.loadFromFile(filename)){
                //
        }

        gmCharacther_semiIdle->setTexture(gmChar);
        gmCharachter_stage1->setTexture(gmChar);
        gmCharachter_stage2->setTexture(gmChar);

}

23
Graphics / Re: Loading charset
« on: August 08, 2013, 05:51:54 pm »
so what if i use pointers?

24
Graphics / Re: [SOLVED]Loading charset
« on: August 08, 2013, 04:57:23 pm »
Ok so how do i write a function that's equivalent to the one i initially wrote but that doesn't causes problems?

25
Graphics / Re: [SOLVED]Loading charset
« on: August 08, 2013, 04:50:17 pm »
Can you tell me why the 2nd code doesn't work?

26
Graphics / [SOLVED]Loading charset
« on: August 08, 2013, 04:29:50 pm »
So I'm doing this project and for my character sprite i'll be using an RPG Maker charset. For my project more understandable i've got it divided in functions. When i load the png file that contains my charset it doesnt show anything. It goes like an enormous white square. Here's the code:

#include <SFML/Graphics.hpp>

sf::Texture loadStuff()
{
          sf::Texture gmChar;

          if(!gmChar.loadFromFile("Resource/charsetfinal.png")){
                  //return EXIT_FAILURE;
          }
                  return gmChar;


}

void defCharset(sf::Sprite *gmCharacther_semiIdle, sf::Sprite *gmCharachter_stage1, sf::Sprite *gmCharachter_stage2)
{

  sf::Texture gmChar;

  gmChar = loadStuff();


  gmCharacther_semiIdle->setTexture(gmChar);
  gmCharachter_stage1->setTexture(gmChar);
  gmCharachter_stage2->setTexture(gmChar);

}
 

so after the blank square i tried putting it all in the same function. But with that the game didn't even start. Here's the code for that:

#include <SFML/Graphics.hpp>

void defCharset(sf::Sprite *gmCharacther_semiIdle, sf::Sprite *gmCharachter_stage1, sf::Sprite *gmCharachter_stage2)
{

  sf::Texture gmChar;

  if(!gmChar.loadFromFile("Resource/charsetfinal.png")){
     //return EXIT_FAILURE;
  }


  gmCharacther_semiIdle->setTexture(gmChar);
  gmCharachter_stage1->setTexture(gmChar);
  gmCharachter_stage2->setTexture(gmChar);

}



PS: This is only part of the program. I think it's obvious but...

27
Graphics / Re: Flashing Text
« on: August 06, 2013, 08:47:18 pm »
Ok thank you very much. It's such a simple solution. Should have thought of that. :facepalm:

28
Graphics / Re: Flashing Text
« on: August 06, 2013, 08:38:28 pm »
Still everything you said in the first paragraph didn't change a thing because what I want isn't simply changing the color once when it's selected, I want it to continuously change the color of the text, not the outline, until it's been deselected.

29
Graphics / Flashing Text
« on: August 06, 2013, 08:13:17 pm »
So hi. I'm starting my game and I'm doing the menu. I've decided the way I'm going images for the background and text for the actual button. So what i would like is when the user is going through the menu with the arrow keys the button witch is currently selecting is highlighted. The highlight would be the text flashing. By flashing I mean switching between 2 colors. I have no idea how to so any help is tremendously appreciated.  ;)

30
General / Re: Help with a switch statement
« on: August 06, 2013, 06:09:02 pm »
Thanks to all I've managed to solve the problem. The thread can be locked.

Pages: 1 [2] 3
anything