Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[Box2d/SFML] User Data

Ostatnio zmodyfikowano 2014-06-01 21:15
Autor Wiadomość
pekfos
» 2014-05-16 21:06:25
Z tym wieszaniem to było niedopatrzenie, lecz dalej grafika nie pokrywa się z obiektem fizycznym.
???
P-110148
PCS
Temat założony przez niniejszego użytkownika
» 2014-05-16 21:13:40

Z tym wieszaniem to było niedopatrzenie, lecz dalej grafika nie pokrywa się z obiektem fizycznym.
Czego nie rozumiesz?
P-110150
DejaVu
» 2014-05-16 21:14:31
Czego nie rozumiesz?
Zapewne tego samego co ja. Skoro user data działa, to dlaczego ten temat nie został już zamknięty i tym samym rozwiązany?
P-110151
pekfos
» 2014-05-16 21:22:31
Czego nie rozumiesz?
Rozumiem. "Problem rozwiązany, ale dalej *coś kompletnie nie na temat*".
P-110153
PCS
Temat założony przez niniejszego użytkownika
» 2014-06-01 12:59:07
Pozwoliłem sobie otworzyć, gdyż sam nie rozwiązałem swego problemu, tutaj staraliście się mi pomóc, lecz w dalszym ciągu to mi za cholere nie chce działać, aby opisać mój problem zrobiłem krótki filmik.

 https://www.youtube.com/watch​?v=IhVAiVK3KlY

A o to mój cały kod - bawię się nim, także zdaję sobie sprawę że dużo w nim nie potrzebnych rzeczy.

C/C++
#include <stdio.h>
#include <iostream>
#include <SFML/Graphics.hpp>
#include <Box2D/Box2D.h>
#include <Windows.h>
#include <string>

using namespace std;
static const float SCALE = 30.f;

sf::RenderWindow * window = new sf::RenderWindow( sf::VideoMode( 800, 600, 32 ), "Test" );
void CreateBox( b2World & world );


enum StatusGry { MENU, GRA, WYJDZ };
enum Status { GO_UP, GO_RIGHT, GO_LEFT, Status_Count };
void SetUserData( void * data );

StatusGry status;
sf::Font czcionka;
////////////////////////
int frame;
int speed;
Status statusAni;
sf::Sprite characterS;
sf::Texture characterT;
bool animation;
void playAnimation( bool play );
std::vector < int > frame_count;

//////////////////////////////
void menuRun();
void single();
void update();
sf::Event event;
void petlaGlowna();
sf::Clock zegar;
b2Vec2 grav( 0.0f, - 3.0f );

////Ground1:
b2BodyDef groundDef;
b2Body * groundBody;
b2PolygonShape groundShape;
bool doSleep;
b2World world( grav, doSleep );
sf::Texture grassTexture;
sf::Sprite grassSprite;
////////////

/////////   Ground2:    //////////////

b2BodyDef boxDefs;
b2PolygonShape boxShapes;
b2FixtureDef boxFixtures;
b2Body * boxBodys;

sf::Texture grassTexture1;
sf::Sprite grassSprite1;

////////   Character:  //////////////
b2BodyDef boxDef;
b2PolygonShape boxShape;
b2FixtureDef boxFixture;
b2Body * boxBody;
sf::Texture character;
sf::Sprite box;





int main()
{
   
    if( !characterT.loadFromFile( "monkey.png" ) )
    {
        //MessageBox(NULL,"Textures not found!","ERROR",NULL);
        // return;
    }
    characterS.setTexture( characterT );
    characterS.setTextureRect( sf::IntRect( 0, 50, 26, 45 ) );
    statusAni = GO_RIGHT;
    int size[ Status_Count ] = { 3, 3 };
   
    for( int i = 0; i < Status_Count; i++ )
    {
        frame_count.push_back( size[ i ] );
    }
    frame = 0;
    animation = false;
   
    //Setup world.
    doSleep = false;
   
    /////////   Ground1:    /////////////////////
    groundDef.position.Set( 10.0f, - 500.0f );
    groundDef.type = b2_staticBody;
   
    groundShape.SetAsBox( 16, 16 );
    b2FixtureDef FixtureDef;
    FixtureDef.density = 0.f;
    FixtureDef.shape = & groundShape;
    groundBody = world.CreateBody( & groundDef );
   
    groundBody->CreateFixture( & FixtureDef );
   
   
    grassTexture.loadFromFile( "grass.png" );
    grassSprite.setTexture( grassTexture );
    grassSprite.setOrigin( 16, 16 );
    //////////////////////////////////////////////// ///////////
   
   
   
   
    ////////    Character:  ////////////////////////
    boxDef.position.Set( 200, - 400 );
    boxDef.type = b2_dynamicBody;
    boxShape.SetAsBox( 12, 24 );
   
    boxFixture.density = 0.0f;
    boxFixture.shape = & boxShape;
    boxFixture.friction = 10.0f;
   
    boxBody = world.CreateBody( & boxDef );
    boxBody->CreateFixture( & boxFixture );
   
    box.setTexture( character );
    characterS.setOrigin( 12, 24 );
    ///////////////////////////////////////////
   
   
    //////////  ladowanie czcionki: //////////////
   
    status = WYJDZ;
    if( !czcionka.loadFromFile( "c++/Projekty/MojaGra/font.otf" ) )
    {
        MessageBox( NULL, "Font not found!", "ERROR", NULL );
        //return;
    }
   
    status = MENU;
    menuRun();
   
}


void petlaGlowna()
{
    while( status != WYJDZ )
    {
        switch( status )
        {
        case MENU:
            menuRun();
            break;
           
        case GRA:
            //single();
            break;
        }
    }
}

void menuRun()
{
    sf::Text tytul( "Bike", czcionka, 100 );
    tytul.setStyle( sf::Text::Bold );
    tytul.setPosition( 1280 / 2 - tytul.getGlobalBounds().width / 2, 20 );
   
    const int ileNapisow = 2;
    sf::Text tekst[ ileNapisow ];
   
    std::string napisy[] = { "Graj", "Wyjdz" };
   
    for( int i = 0; i < ileNapisow; i++ )
    {
        tekst[ i ].setFont( czcionka );
        tekst[ i ].setCharacterSize( 50 );
       
        tekst[ i ].setString( napisy[ i ] );
        tekst[ i ].setPosition( 300 - tekst[ i ].getGlobalBounds().width / 2, 250 + i * 120 );
    }
   
    int selected = 0;
   
   
   
    while( status == MENU )
    {
       
        while( window->pollEvent( event ) )
        {
           
            //esc i klikniecie X
            if( event.type == sf::Event::Closed ||( event.type == sf::Event::KeyReleased &&
            event.key.code == sf::Keyboard::Escape ) )
            {
                status = WYJDZ;
               
            }
           
            if( event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Up )
            {
                selected--;
                if( selected == - 1 )
                     selected = 1;
               
            }
           
            if( event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Down )
            {
                selected++;
                if( selected == 2 )
                     selected = 0;
               
            }
           
            //gdy wciœniemy spacje dokonujemy wyboru co chcemy zrobic
            if( event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Space )
            {
                switch( selected )
                {
                case 0:
                    single();
                    break;
                case 1:
                    status = WYJDZ;
                    break;
                   
                }
            }
           
            for( int i = 0; i < 2; i++ )
            {
                if( i == selected )
                     tekst[ i ].setColor( sf::Color::Blue );
                else
                     tekst[ i ].setColor( sf::Color::White );
               
            }
            window->clear( sf::Color::Black );
           
            for( int i = 0; i < 2; i++ )
            {
               
               
                window->draw( tekst[ i ] );
            }
            window->display();
           
           
        }
    }
   
}

void single()
{
   
    sf::Sprite grass;
    CreateBox( world );
    b2Body * BodyIterator = world.GetBodyList();
    b2Vec2 boxPose = boxBodys->GetPosition();
   
   
    float32 timeStep = 1.0f / 60.0f;
    int32 velIt = 6;
    int32 posIt = 2;
   
    bool menu = false;
   
    while( !menu )
    {
        if( strcmp(( char * ) BodyIterator->GetUserData(), "Grass" ) )
        {
            grassTexture1.loadFromFile( "grass.png" );
            sf::Sprite grassSprite1;
            grassSprite1.setTexture( grassTexture1 );
            grassSprite1.setOrigin( 16, 16 );
            grassSprite1.setPosition( boxPose.x - 0.0f, -( boxPose.y + 0.0f ) );
            window->draw( grassSprite1 );
        }
       
        while( window->pollEvent( event ) )
        {
            if( event.type == sf::Event::KeyReleased && event.key.code == sf::Keyboard::Escape )
                 menu = true;
           
        }
        b2Vec2 vel = boxBody->GetLinearVelocity();
       
        if( sf::Keyboard::isKeyPressed( sf::Keyboard::Right ) )
        {
            statusAni = GO_RIGHT;
            vel.x = 2;
            boxBody->SetLinearVelocity( vel );
            playAnimation( true );
        }
        if( sf::Keyboard::isKeyPressed( sf::Keyboard::Left ) )
        {
            statusAni = GO_LEFT;
            vel.x = - 5;
            boxBody->SetLinearVelocity( vel );
            //playAnimation(true);
        }
        if( sf::Keyboard::isKeyPressed( sf::Keyboard::Up ) )
        {
            vel.y = 5;
            boxBody->SetLinearVelocity( vel );
        }
       
        if( zegar.getElapsedTime() > sf::seconds( 0.2 ) )
        {
            update();
            zegar.restart();
        }
       
        world.Step( timeStep, velIt, posIt );
        b2Vec2 groundPosition = groundBody->GetPosition();
        b2Vec2 boxPos = boxBody->GetPosition();
       
        window->clear( sf::Color( 100, 175, 255 ) );
       
        grassSprite.setPosition( groundPosition.x - 0.0f, -( groundPosition.y + 0.0f ) );
        window->draw( grassSprite );
       
       
        characterS.setPosition( boxPos.x - 0.0f, -( boxPos.y + 0.0f ) );
        window->draw( characterS );
       
        window->display();
    }
}

void update()
{
    if( animation )
    {
        if( frame < frame_count[ status ] )
        {
            frame++;
        }
        else
        {
            frame = 0;
        }
       
        characterS.setTextureRect( sf::IntRect( frame * 33, statusAni * 50, 26, 45 ) );
       
    }
}

void playAnimation( bool play )
{
    animation = play;
   
    if( animation == false )
    {
        frame = 0;
        characterS.setTextureRect( sf::IntRect( frame * 33, statusAni * 50, 26, 45 ) );
    }
}

void CreateBox( b2World & world )
{
    boxDefs.position.Set( 200, - 360 );
    boxDefs.type = b2_staticBody;
    boxDefs.userData =( void * ) "Grass";
   
    boxShapes.SetAsBox( 16, 16 );
   
    boxFixtures.density = 0.0f;
    boxFixtures.shape = & boxShapes;
    boxFixtures.friction = 10.0f;
   
    boxBodys = world.CreateBody( & boxDefs );
    boxBodys->CreateFixture( & boxFixtures );
   
}
P-111244
pekfos
» 2014-06-01 15:46:52
C/C++
void SetUserData( void * data );
Co to?

C/C++
if( strcmp(( char * ) BodyIterator->GetUserData(), "Grass" ) )
To nie zwraca prawdy, gdy teksty są sobie równe.
P-111262
PCS
Temat założony przez niniejszego użytkownika
» 2014-06-01 21:12:10

Co to?
Różnie kombinowałem z tym kodem, miało być w komentarzu.


To nie zwraca prawdy, gdy teksty są sobie równe.
A to?
C/C++
if( strcmp(( char * ) BodyIterator->GetUserData(), "Grass" ) == 0 )

P-111288
pekfos
» 2014-06-01 21:15:27
A to tak.
P-111289
1 « 2 »
Poprzednia strona Strona 2 z 2