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.


Topics - Munchor

Pages: [1]
1
Graphics / Rotating sprite around center
« on: July 24, 2013, 08:09:41 pm »
Hi,

I want to rotate my sprite around its center but I want it to be drawn with its origin as (0, 0). So I have this:

Code: [Select]
  setOrigin(texture.getSize().x / 2, texture.getSize().y / 2);
  rotate(-90);
  setOrigin(0, 0);

However, it does not seem to be working - the sprite is rotated exactly the same way as if I didn't have that first line. Any idea of what I'm doing wrong? I understand this might not be possible hence I'm posting here in the forums.

2
Window / If for mouse wheel scrolling
« on: July 20, 2013, 01:14:36 pm »
I know I can use this event[1] to listen to a mouse wheel scroll event, but I'd rather use an "if" statement to listen to it like I can do with button events:

if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
{

}

Is there any way to do so? I tried to use isButtonPressed(sf::Mouse::Middle) but that listens to the scroll wheel button being down not it being scrolled (which is the right thing to listen to of course).

[1]: http://sfml-dev.org/documentation/2.0/structsf_1_1Event_1_1MouseWheelEvent.php

3
General / Using CMake to build my project
« on: July 18, 2013, 05:13:32 pm »
So I have this[1] project and it always built just fine but now I decide to try and rebuild it and it doesn't work any more. I get the following error:

Code: [Select]
CMake Error at CMakeLists.txt:9 (find_package):
  By not providing "FindSFML.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "SFML", but
  CMake did not find one.

  Could not find a package configuration file provided by "SFML" (requested
  version 2.0) with any of the following names:

    SFMLConfig.cmake
    sfml-config.cmake

  Add the installation prefix of "SFML" to CMAKE_PREFIX_PATH or set
  "SFML_DIR" to a directory containing one of the above files.  If "SFML"
  provides a separate development package or SDK, be sure it has been
  installed.


-- Configuring incomplete, errors occurred!

I am using Ubuntu 13.04 and I'm thinking this could be happening because I have a new CMake version and that's why it doesn't work any more. My CMake version is 2.8.10.1, any ideas on what I need to fix?

Thank you in advance.

[1]: https://github.com/davidgomes/2dplatformer

4
Window / Window changing position
« on: January 12, 2013, 06:52:37 pm »
  window.create(sf::VideoMode(800, 640, 1), title.c_str(), sf::Style::Titlebar | sf::Style::Close);
  window.setFramerateLimit(60);
  window.setPosition(sf::Vector2i(sf::VideoMode::getDesktopMode().width / 2 - 400,
                                  sf::VideoMode::getDesktopMode().height / 2 - 320));
 

That's the code with the which I create my window. The window opens on (0, 0) and then jumps to the center of the screen.

Is there a way to make it open on the center right at the start? I don't really like the window "bouncing" position.

Thanks.

5
Graphics / Drawing different image than the one I have
« on: January 08, 2013, 10:30:31 pm »
I have a certain image on res/player.png. I open the image with an Image Viewer, and I get the very image.

Now, I have this class, which in the end, with a ::draw() method draws it. (note that Player is an sf::Sprite).

Code: [Select]
#include "Player.hpp"

using namespace std;

Player::Player(Game *_game, Tilemap *_map)
{
  game = _game;
  map = _map;

  /* Prepare debug label */
  debugText.setFont(game->arialFont);
  debugText.setCharacterSize(12);
  debugText.setPosition(2, 14);
  debugText.setColor(sf::Color::Red);

  /* Load image */
  sf::Texture texture;
  if (!texture.loadFromFile("res/player.png")) {
    printf("Error loading player image.\n");
  }

  width = 32;
  height = 32;

  x = 20;
  y = 20;

  vx = 0.f;
  vy = 0.f;

  setTexture(texture);
}

void Player::update()
{
  debugText.setString("");

  if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
    vx = 3;
  }

  if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
    vx = -3;
  }

  /* Jumpity jumpy jump */
  if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
    if (map->raw[(int) floor(floor(y + height + 0.5) / 8)][(int) floor(x / 8)]) {
      vy = -6;
    }
  }

  if (!map->raw[(int) floor((y + height + 1) / 8)][(int) (floor(x / 8))]) {
    vy += 0.2;
  }

  if (vx > 0) {
    for (int i = 0; i < floor(vx); i++) {
      if (!map->raw[(int) floor(y / 8)][(int) floor((x + width) / 8)])
        x++;
      else
        break;
    }
  } else if (vx < 0) {
    for (int i = 0; i < floor(-vx); i++) {
      if (!map->raw[(int) floor(y / 8)][(int) (floor((x - 1) / 8))])
        x--;
      else
        break;
    }
  }

  if (vy > 0) {
    for (int i = 0; i < floor(vy); i++) {
      if (y + height - floor(y + height) == 0.5) {
        system("clear");
        printf("\n\n\n\n*******\n****************************\n*************** HERE\n\n");
        y + 0.001;
      }
     
      if (!map->raw[(int) floor(floor(y + height + 0.5) / 8)][(int) floor(x / 8)] &&
          !map->raw[(int) floor(floor(y + height + 0.5) / 8)][(int) floor((x + width - 1) / 8)])
        y++;
      else
        break;
    }
  } else if (vy < 0) {
    y += vy;
  }

  //printf("%lf, %lf\n", y + height, floor(y + height + 0.5));

  /* Apply friction */
  if (vx > 0) vx -= 0.1;
  if (vx < 0) vx += 0.1;
  if (abs(vx) < 0.15) vx = 0;

  setPosition(x, y);
}

void Player::draw() {
  game->window.draw(*this);
  game->window.draw(debugText);
}

And the image I get on the screen when playing the Player is a white square (32 * 32).

Any ideas on why this might be happening? Thanks. I tried renaming the image, making the project countless times.

6
Graphics / [SOLVED] sf::Text not appearing on the screen v2
« on: January 06, 2013, 08:43:08 pm »
#include "Player.hpp"

using namespace std;

Player::Player(Game *_game, Tilemap *_map)
{
  game = _game;
  map = _map;

  /* Prepare debug label */
  debugText.setFont(game->arialFont);
  debugText.setCharacterSize(80);
  debugText.setPosition(16, 16);
  debugText.setColor(sf::Color::Red);

  /* Load image */
  sf::Texture texture;
  if (!texture.loadFromFile("res/player.png")) {
    printf("Error loading player image.\n");
  }

  width = 32;
  height = 32;

  x = 20;
  y = 20;

  vx = 0.f;
  vy = 0.f;

  setTexture(texture);
}

void Player::update()
{
  if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
    vx = 2;
  }

  if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
    vx = -2;
  }

  if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
    vy = -3;
  }

  if (!map->raw[(int) floor((y + height) / 8)][(int) floor(x / 8)]) {
    vy += 0.4;
  }

  if (vx > 0) {
    for (int i = 0; i < floor(vx); i++) {
      if (!map->raw[(int) floor(y / 8)][(int) floor((x + width) / 8)])
        x++;
    }
  } else if (vx < 0) {
    for (int i = 0; i > ceil(vx); i--) {
      if (!map->raw[(int) floor(y / 8)][(int) (floor((x + width) / 8) - 4)])
        x--;
    }
  }

  if (vy > 0) {
    for (int i = 0; i < floor(vy); i++) {
      if (!map->raw[(int) floor((y + height) / 8)][(int) floor(x / 8)])
        y++;
    }
  } else if (vy < 0) {
    y += vy;
  }

  /* Apply friction */
  if (vx > 0) vx -= 0.1;
  if (vx < 0) vx += 0.1;
  if (abs(vx) < 0.15) vx = 0;

  if (vy > 0) vy -= 0.1;
  if (vy < 0) vy += 0.1;
  if (abs(vy) < 0.15) vy = 0;

  setPosition(x, y);

  debugText.setString("ss");
  game->window.draw(debugText);
}
 

That is my Player.cpp file.

I'm drawing that string, but it's nowhere to be seen on the screen.

On another class:

#include "PlayState.hpp"

using namespace std;

PlayState::PlayState(Game *_game)
{
  game = _game;

  /* Set up FPS string */
  fps.setFont(game->arialFont);
  fps.setCharacterSize(12);
  fps.setPosition(2, 2);
  fps.setColor(sf::Color::Red);

  currentMap = new Tilemap("res/map1.txt", &(game->window));
  player = new Player(_game, currentMap);
}

void PlayState::setup() { }

void PlayState::update()
{
  sf::Event event;
  while (game->window.pollEvent(event)) {
    if (event.type == sf::Event::Closed) {
      game->window.close();
    } else if (event.type == sf::Event::KeyPressed) {
      if (event.key.code == sf::Keyboard::Escape) {
        game->window.close();
      }
    }
  }

  player->update();
}

void PlayState::draw()
{
  /* Clear the screen */
  game->window.clear();

  /* Draw the player */
  game->window.draw(*player);
 
  /* Draw the map */
  currentMap->draw(400, 320);

  /* Draw FPS */
  stringstream fpsStream;
  fpsStream << ceil(game->fps);
  fps.setString(fpsStream.str());
  game->window.draw(fps);

  /* Display */
  game->window.display();
}
 

I have some very similar code for drawing sf::Text fps. I'm using the same font (game->arialFont) and giving it the same color, but I can't see sf::Text debugText (I can see sf::Text fps).

Any ideas?

7
Graphics / [SOLVED] sf::Text not appearing on the screen
« on: December 30, 2012, 04:37:49 am »
First of all, here's my code:

#include "PlayState.hpp"

using namespace std;

PlayState::PlayState(Game *_game) {
  game = _game;

  /* Set up FPS string */
  fps.setCharacterSize(12);
  fps.setPosition(400, 320);
  fps.setColor(sf::Color::Red);
 
  player = new Player();

  currentMap = new Tilemap("res/map1.txt", &(game->window));
}

void PlayState::setup() { }

void PlayState::update() {
  sf::Event event;
  while (game->window.pollEvent(event)) {
    if (event.type == sf::Event::Closed) {
      game->window.close();
    } else if (event.type == sf::Event::KeyPressed) {
      if (event.key.code == sf::Keyboard::Escape) {
        game->window.close();
      }
    }
  }

  player->update();
}

void PlayState::draw() {
  game->window.clear();

  /* Draw the player */
  //game->window.draw(*player);

  /* Draw the map */
  //currentMap->draw(400, 320);

  /* Draw FPS */
  fps.setString("Hi");
  game->window.draw(fps);

  game->window.display();
}
 

The only thing I'm drawing to the screen is a sf::Text, but it's not appearing on the screen. I'm using built-from-git SFML. I tried an older build, and it worked, I could see the sf::Text. When I changed its color, though, I got a floating point exception.

Any ideas on what I'm doing wrong?

EDIT
I also tried to dynamically allocate the sf::Text fps; and then do "fps = new sf::Text();" and what not, but it also didn't work.

8
General / SFML Makefiles
« on: August 31, 2012, 12:13:23 pm »
Hey there,

LIBS=-lsfml-graphics -lsfml-window -lsfml-system

all:
        @echo "** Building the game"
        g++ -c "State.cpp" -o State.o
        g++ -c "PlayState.cpp" -o PlayState.o
        g++ -c "Game.cpp" -o Game.o
        g++ -c "Object.hpp" -o Object.o
        g++ -c "Player.hpp" -o Player.o
        g++ -o thegame Game.o State.o PlayState.o Object.o Player.o $(LIBS)

clean:
        @echo "** Removing object files and executable..."
        rm -f thegame

install:
        @echo '** Installing...'
        cp thegame /usr/bin

uninstall:
        @echo '** Uninstalling...'
        rm thegame

That's the makefile I've been using for my SFML Project.

Code: [Select]
** Building the game
g++ -c "State.cpp" -o State.o
g++ -c "PlayState.cpp" -o PlayState.o
g++ -c "Game.cpp" -o Game.o
g++ -c "Object.hpp" -o Object.o
g++ -c "Player.hpp" -o Player.o
g++ -o thegame Game.o State.o PlayState.o Object.o Player.o -lsfml-graphics -lsfml-window -lsfml-system
/usr/bin/ld:Object.o: file format not recognized; treating as linker script
/usr/bin/ld:Object.o:1: syntax error
collect2: error: ld returned 1 exit status
make: *** [all] Error 1

It hasn't quite been working, though. Do you guys also use Makefiles, any tips? Thank you.

9
Graphics / sf::RenderWindow draw image
« on: August 30, 2012, 08:40:57 pm »
I'm not being able to draw an sf::Image to a sf::RenderWindow.

Code: [Select]
PlayState.cpp: In member function ‘virtual void PlayState::render()’:
PlayState.cpp:30:34: error: no matching function for call to ‘sf::RenderWindow::draw(sf::Image&)’
PlayState.cpp:30:34: note: candidates are:
In file included from /usr/include/SFML/Graphics/RenderTexture.hpp:33:0,
                 from /usr/include/SFML/Graphics.hpp:39,
                 from PlayState.cpp:3:
/usr/include/SFML/Graphics/RenderTarget.hpp:186:10: note: void sf::RenderTarget::draw(const sf::Drawable&, const sf::RenderStates&)
/usr/include/SFML/Graphics/RenderTarget.hpp:186:10: note:   no known conversion for argument 1 from ‘sf::Image’ to ‘const sf::Drawable&’
/usr/include/SFML/Graphics/RenderTarget.hpp:197:10: note: void sf::RenderTarget::draw(const sf::Vertex*, unsigned int, sf::PrimitiveType, const sf::RenderStates&)
/usr/include/SFML/Graphics/RenderTarget.hpp:197:10: note:   candidate expects 4 arguments, 1 provided
make: *** [all] Error 1

void PlayState::render() {
  game->window.clear();
  game->window.draw(player->image);
  game->window.display();
}

That is my code. First of all, isn't sf::Image an sf::Drawable? Secondly, how would I go about passing the sf::RenderStates?

Thank you.






Oh wait, just read this, it all make sense now :)

10
General / No window appears
« on: August 28, 2012, 04:51:31 pm »
Hi there everyone, first day doing SFML, here's my Game.cpp and my Game.hpp:

Game.cpp
Code: [Select]
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>

#include "Game.hpp"

#include <stdio.h>

void Game::setup() {
  title = "s";
  sf::RenderWindow window(sf::VideoMode(800, 600, 32), title.c_str());
  sf::CircleShape shape(100.f);
  shape.setFillColor(sf::Color::Green);

  start();
}

void Game::start()
{
  /* Handle game-related variables here */
 
  run();
}

void Game::run() {
  running = true;

  while (running) {
    update();
    render();
  }
}

void Game::update() {
  while (window.isOpen())
  {
    sf::Event event;
    while (window.pollEvent(event))
    {
      if (event.type == sf::Event::Closed)
        window.close();
    }
  }
}

void Game::render() {
  printf("Starting render...\n");
  window.clear();
  window.draw(shape);
  window.display();
}

int main(int argc, char *argv[]) {
  Game game;
  game.start();
  game.run();

  return 0;
}

Game.hpp
Code: [Select]
#ifndef PAXLURE_HPP
#define PAXLURE_HPP

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

#include <string>

using namespace std;

class Game {
  private:
    string title;
    sf::RenderWindow window;
    sf::CircleShape shape;

    bool running;

  public:
    void setup();
    void start();
    void run();
    void update();
    void render();
};

#endif

For some reason the window is not being drawn, but the ::render() function is being called. I run the program and the window just doesn't show up.

Any ideas why? Thank you in advance.

Pages: [1]