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 - Munchor

Pages: [1] 2
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 / Re: AW: Using CMake to build my project
« on: July 20, 2013, 01:03:32 pm »
The error just means, that CMake couldn't find SFML, which is needed by your project.
Where are your SFML libraries located can they be found by ld?

It seems I upgraded my operating system and it uninstalled SFML so I had to reinstall, thank you :)

4
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

5
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.

6
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.

7
Graphics / Re: sf::Text not appearing on the screen v2
« on: January 06, 2013, 10:40:25 pm »
I suggest that you add a draw method to the Player class and draw the debugText there.
The problem here is that you draw the debugText in Player::upate and then clear the screen in PlayState::draw so the debugText is cleared.

That was dumb of me. Thanks, it worked!

8
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?

9
Graphics / Re: [SOLVED] sf::Text not appearing on the screen
« on: December 30, 2012, 01:53:44 pm »
That was it, thank you!

10
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.

11
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.

12
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 :)

13
General / Re: No window appears
« on: August 28, 2012, 05:42:11 pm »
Edit: The next problem will be, now that your window isOpen the application will get stuck in the update() function at the while(window.isOpen()) loop and never reach the render function. ;)

I actually had thought of that already, but I knew *that* was not the problem. I am debug printing on render(), it is reaching there lots of times.

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

#include "Game.hpp"

#include <stdio.h>

void Game::setup() {
  title = "Explosonoid";
  window.create(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() {
  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;
}

Either way, I still fixed it to make sure, it's not working yet. Thank you for all the attention.

14
General / Re: No window appears
« on: August 28, 2012, 05:23:50 pm »
Mhm, I understand what's wrong and how to fix it, but I'm not really succeeding:
This is what you want:
window.create(sf::VideoMode(800, 600, 32), title.c_str());
The creation an other window doesn't make sense.

Also, in the Game.hpp file, should I declare the window as sf::Window or sf::RenderWindow? Thank you.
Depends what you want to do, but I guess you want to use all the features SFML provides thus sf::RenderWindow is what you want to use.

Edit: The next problem will be, now that your window isOpen the application will get stuck in the update() function at the while(window.isOpen()) loop and never reach the render function. ;)

Thanks a lot, but that didn't really work:

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

#include "Game.hpp"

#include <stdio.h>

void Game::setup() {
  title = "s";
  window.create(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;
}

The window is still not showing up :S

15
General / Re: No window appears
« on: August 28, 2012, 05:03:47 pm »
In setup() you create a temporary sf::RenderWindow object, thus you initialize the member window with the default constructor which doesn't really create the window.
See documentation:
Quote
Default constructor.

This constructor doesn't actually create the window, use the other constructors or call Create to do so.

Mhm, I understand what's wrong and how to fix it, but I'm not really succeeding:

I tried

window.create(sf::VideoMode(800, 600, 32), title.c_str());
window = sf:Window(sf::VideoMode(800, 600, 32), title.c_str());

And more things, but not luck.

sf::Window window;

Also, in the Game.hpp file, should I declare the window as sf::Window or sf::RenderWindow? Thank you.

Pages: [1] 2