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

Pages: [1]
1
General discussions / SFML: X11 Window Class does not free X11 properly!
« on: October 12, 2010, 08:14:45 pm »
graphitemaster: As I told Trina last night, the only time you really need to use free is when you have an associated new as well. Just because something has a pointer doesn't mean you always need to free it, which is what it appears that you are assuming.

Perhaps reading up on pointers and memory allocation/dynamic memory a little more might make things clearer.

I am, however, glad to see that your picking through the source code has uncovered a few bugs. :)

2
Audio / Loading Audio
« on: May 30, 2010, 08:04:17 am »
Is it better to load all audio for a "level" all at once, or to load them as needed on the fly? The type of audio I'm thinking of would be, for example, random sound effects that are used maybe once or twice in the level.

Wil

ETA: Let me further elaborate. If, for example, I have a spatial sound effect that is toward the end of the level (say, the sound of a waterfall), should I have the audio looping the entire time and running or should I have the audio loaded but not playing until they are within distance or should I load the audio and play when they are within distance at the same time?

3
General discussions / Yet another Box2D Positioning Issue
« on: May 27, 2010, 10:41:59 pm »
The reason was because I didn't know about GetFrameTime() haha. That works much better without having to limit the FPS.

4
General discussions / Yet another Box2D Positioning Issue
« on: May 27, 2010, 10:06:12 pm »
Thank you very much! I had to work with your code a little to get it to work (fought with a segmentation fault for about an HOUR lol), but now everything lines up and looks great! I'll go ahead and post the working code so that those who have trouble with this same thing in the future at least have a working program to start with. :)

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

// Box2D Header
#include <Box2D/Box2D.h>

#define PIXELS_METER 30.f
#define PI 3.1415926

double degreeToRadian(double degree) {
        double radian = 0;
        radian = degree * (PI/180);
        return radian;
}

double radianToDegree(double radian) {
        double degree = 0;
        degree = radian * (180/PI);
        return degree;
}

b2Body * CreateBody(b2World *World, b2BodyType type, sf::Shape &Shape, const sf::Vector2f &Position, const sf::Vector2f &Size)
{
   // Create the body
      b2BodyDef def;
      def.type = type;
      def.position.Set(Position.x/PIXELS_METER, Position.y/PIXELS_METER);
      b2Body* Body = World->CreateBody(&def);
      b2PolygonShape pol;
      pol.SetAsBox(Size.x/PIXELS_METER, Size.y/PIXELS_METER);
      b2FixtureDef fix;
      fix.shape = &pol;
      fix.density = 1.0f;
      fix.friction = 0.3f;
      Body->CreateFixture(&fix);
      Shape = sf::Shape::Rectangle(0.f, 0.f, Size.x*2, Size.y*2, sf::Color::White);
      Shape.SetCenter(Size);
      Shape.SetPosition(Position);

      return Body;
}

void Synchronize(b2Body *Body, sf::Shape &Shape)
{
   Shape.SetRotation(-radianToDegree(Body->GetAngle()));
   Shape.SetPosition(Body->GetPosition().x*PIXELS_METER, Body->GetPosition().y*PIXELS_METER);
}

int main() {

    // Create the SFML Window
sf::RenderWindow Game(sf::VideoMode(800, 600, 32), "SFML Blank Window");
sf::Event Event;
Game.SetFramerateLimit(60);

    // Create the world
    b2Vec2 Gravity(0.f, 10.f);
    b2World World(Gravity, true);

    // Create the body and the shape
    sf::Shape Shape;
    b2Body *Body = CreateBody(&World, b2_dynamicBody, Shape, sf::Vector2f(100.f, 100.f), sf::Vector2f(50.f, 50.f));

    //Body->SetTransform(Body->GetPosition(), 45);

    sf::Shape ground;
    b2Body *groundBody = CreateBody(&World, b2_staticBody, ground, sf::Vector2f(10.f, 550.f), sf::Vector2f(800.f, 10.f));

    while (Game.IsOpened()) {
        while (Game.GetEvent(Event)) {
            if (Event.Type == sf::Event::Closed)
                Game.Close();
        }

        World.Step(1.f/60.f, 10, 1);

        Synchronize(Body, Shape);
        Synchronize(groundBody, ground);

        Game.Clear();
        Game.Draw(Shape);
        Game.Draw(ground);
        Game.Display();
    }

    return EXIT_SUCCESS;
}

5
General discussions / Yet another Box2D Positioning Issue
« on: May 27, 2010, 10:52:47 am »
It seems that all the threads I could find on this were code-less and were eventually solved without explaining HOW they were solved. I'm attempting to get Box2D and SFML to play nicely together, and they do seem to for the most part, but I'm having a TINY issue in getting everything to line up as they should. My box object likes to hover about 9 pixels above my ground object and I can't seem to figure out why.

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

// Box2D Header
#include <Box2D/Box2D.h>

int main() {

    // Create the SFML Window
sf::RenderWindow Game(sf::VideoMode(800, 600, 32), "SFML Blank Window");
Game.SetFramerateLimit(60);

    //Create the SFML Event handler
    sf::Event Event;

    // Create the SFLM shape
    sf::Shape box = sf::Shape::Rectangle(0, 0, 50, 50, sf::Color(127, 0, 0, 255));
    //box.SetCenter(25, 25);
    sf::Shape ground = sf::Shape::Rectangle(0, 0, 800, 10, sf::Color(0, 127, 0, 255));
    //box.SetCenter(400, 5);

    // Set the box position
    box.SetPosition(100, 100);
    ground.SetPosition(0, 580);

    // Create the image buffer
    sf::Image image;
    if (!image.LoadFromFile("box.jpg"))
        return EXIT_FAILURE;

    // Create the SFML sprite
    sf::Sprite sprite;

    // Set the sprites image
    sprite.SetImage(image);

    // Set up world properties
    bool doSleep = true;
    b2Vec2 gravity(0, 10.0f);
    int iterations = 10;
    float scale = 30;
    float timeStep = 1.0f / 60.0f;

    // Create the world
    b2World world(gravity, doSleep);

    // Set up box properties
    b2BodyDef bodyDef;
    bodyDef.type = b2_dynamicBody;
    bodyDef.position.Set(100/scale, 100/scale);
    b2Body* body = world.CreateBody(&bodyDef);
    b2PolygonShape dynamicBox;
    dynamicBox.SetAsBox(50/scale, 50/scale);
    b2FixtureDef fixtureDef;
    fixtureDef.shape = &dynamicBox;
    fixtureDef.density = 1.0f;
    fixtureDef.friction = 0.3f;
    body->CreateFixture(&fixtureDef);

    //body->SetTransform(body->GetPosition(), 10);

    // Create ground
    b2BodyDef groundBodyDef;
    groundBodyDef.position.Set(0/scale, 580/scale);
    b2Body* groundBody = world.CreateBody(&groundBodyDef);
    b2PolygonShape groundBox;
    groundBox.SetAsBox(800/scale, 10/scale);
    b2FixtureDef groundFixDef;
    groundFixDef.shape = &groundBox;
    groundFixDef.restitution = 0.5f;
    groundBody->CreateFixture(&groundFixDef);

    while (Game.IsOpened()) {
        while (Game.GetEvent(Event)) {
            if (Event.Type == sf::Event::Closed)
                Game.Close();
        }

        world.Step(timeStep, 10, 1);
        world.ClearForces();

        b2Vec2 pos = body->GetPosition();
        float32 angle = body->GetAngle();

        box.SetPosition(pos.x*scale, pos.y*scale);
        box.SetRotation(-(angle*(180/b2_pi)));

        Game.Clear();

        Game.Draw(sprite);
        Game.Draw(box);
        Game.Draw(ground);

        Game.Display();
    }

    return EXIT_SUCCESS;
}


Any help would be appreciated. Thank you!

Pages: [1]
anything