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

Pages: [1]
1
Graphics / Strange behaviour in SFML 2.0
« on: August 10, 2012, 02:40:47 pm »
OK, so I was creating a game which was going just fine, until I encountered a very strange problem.

My project uses Box2D and SFML, which has always given beginners loads of trouble, but I am not exactly a beginner. In my project, I have a component based architecture and then a player class which binds everything together. The problem is, that my sprite does not follow the position of it's body. It goes where it is supposed to be but then does not move. This might be a bit confusing, but everything will be clearer later on. This is my main code:

Code: [Select]
#include <iostream>
#include <SFML/Graphics.hpp>
#include <Box2D/Box2D.h>

#include "Player.h"

using namespace std;

int main()
{
    sf::RenderWindow *window = new sf::RenderWindow(sf::VideoMode(800, 600, 32), "Quanta");
    sf::FloatRect viewRect(0, 0, 800/50, 600/50);
    sf::View gameView(viewRect);


    b2Vec2 gravity(0.0f, -10.0f);
    b2World *world = new b2World(gravity);

    Player mainPlayer(*window, *world);

    sf::RectangleShape testShape;
    testShape.setFillColor(sf::Color(0, 0, 0));
    testShape.setSize(sf::Vector2f(1.28, 1.28));

    while(window->isOpen())
    {
        sf::Event ev;
        while(window->pollEvent(ev))
        {
            if(ev.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                window->close();
        }

        window->clear(sf::Color(100, 175, 255));

        world->Step(1.0f/60.0f, 6, 2);

        window->setView(gameView);
        mainPlayer.UpdatePlayer(*window);
        //window->draw(testShape); //This is required, you'll see why later on.
        window->display();
    }

    delete window;
    delete world;

    return 0;
}

And this is the code from UpdatePlayer():

Code: [Select]
void Player::UpdatePlayer(sf::RenderWindow& window)
{
    b2Vec2 bodyPosition = _playerBody.body->GetPosition();
    sf::Vector2f spritePosition(bodyPosition.x, bodyPosition.y);
    sf::Vector2u spriteSize(1.28f, 1.28f);

    _renderPlayer.UpdateSprite(window, playerSprite, spritePosition, spriteSize); //Graphics component will update the sprite
}

And from UpdateSprite():

Code: [Select]
void GraphicsComponent::UpdateSprite(sf::RenderWindow& window, sf::Sprite &objectSprite, sf::Vector2f positions,
                                     sf::Vector2u objectSize)
{
    objectSprite.setPosition(positions.x - (objectSize.x / 2), -(positions.y + (objectSize.y /2)));
    window.draw(objectSprite);
}

Now, this used to work in SFML 1.6, but now, the sprite does not fall. It just stays at the position at which it's Box2D body was initialized. To see what was going on, I outputted the position of the Box2D body and it shows that the body is moving. However, the sprite is not following it's position.

BUT, if I uncomment the line which says "window->draw(testShape)" , then the sprite falls! This means, that the sprite updates it's position only when there is something else being drawn to the screen as well! I don't know if this is supposed to happen, but it didn't in SFML 1.6 so I am now very confused.

2
General discussions / Script to compile latest SFML
« on: June 07, 2012, 12:29:12 pm »
To keep up with the latest SFML sources (and to practice my Python skills), I have made a script which downloads and compiles SFML. It is for Linux (and possibly for Mac). Here is the Python source code:

#!/usr/bin/env python

import os

ask = raw_input("This is a script written to compile the latest version of SFML.\nLets face it, bleeding edge technology rocks!\ \n\nDo you want to continue?(y/n) ")

if ask is 'y':
    print("\n\nDownloading source...\n\n")
    download = os.system("wget -O SFML_src https://github.com/SFML/SFML/tarball/master")
       
    if download == 0:
            print("\nExtracting tarball...\n\n")
            extract = os.system("tar -zxvf ./SFML_src")
               
            if extract == 0:
                    rename = raw_input("\n\nPlease rename SFML source file (LaurentGomila-SFML...) to SFML.\nType OK to continue")
                       
                    if rename is 'OK' or 'ok' or 'Ok':
                            print("\n\nInstalling dependencies...\n\n")
                            depinst = os.system("sudo apt-get install cmake libpthread-stubs0-dev libx11-dev \
                            libxrandr-dev libfreetype6-dev libglew1.6-dev libgl1-mesa-dev libjpeg8-dev libsndfile1-dev\
                            libopenal-dev")
                               
                            if depinst == 0:
                                print("\n\nBuilding SFML...\n\n")
                                config = os.system("cmake ./SFML")
                                   
                                if config == 0:
                                    print("\n\nCompiling...\n\n")
                                    makeSF = os.system("make")
                                       
                                    if makeSF == 0:
                                        print("\n\nInstalling...\n\n")
                                        installSF = os.system("sudo make install")
                                           
                                        if installSF == 0:
                                            print("\n\nRunning ldconfig...\n\n")
                                            os.system("sudo ldconfig")
                                            finish = raw_input("\n\nSFML installed successfully. Press enter to finish.")
                                            if finish == '\n':
                                                pass                                   
                                            else:
                                                print("An error occured while making SFML")
                                        else:
                                            print("\n\nAn error occured while compiling SFML source with cmake\n\n")
                                else:
                                    print("\n\nAn error occured while installing required dependencies.\n\n")
                        else:
                            print("\n\nAn error occurred. Check if the source file was renamed\n\n")
                else:
                    print("\n\nAn error occured while extreacting the file\n\n")                                                       
        else:
            print("\n\nAn error occured while downloading.\n\n")
else:
    exit()
 

I'm still learning Python (I use C++ mostly, but Python can do somethings quicker, like this), so this script can probably be improved loads but I wrote it because it's going to be handy in the future.
Note: at one point, it will ask you to rename the extracted file (called LaurentGomila-SFML-[loads of letters and numbers]) to SFML.
Warning: This will overwrite your previous SFML installation.

[attachment deleted by admin]

Pages: [1]