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

[SFML] dzieleni kodu na pliki

Ostatnio zmodyfikowano 2020-01-12 20:28
Autor Wiadomość
R0ut4
Temat założony przez niniejszego użytkownika
[SFML] dzieleni kodu na pliki
» 2020-01-11 21:13:18
Nie ma to jak szybki powrót.
wywołanie funckji w main():
C/C++
98: void Ruch_postaci( player );

Ruch:
C/C++
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
void Ruch_postaci( sf::Sprite player )
{
    if( sf::Keyboard::isKeyPressed( sf::Keyboard::W ) )
    {
        int top = player.getTextureRect().top;
        player.setTextureRect( sf::IntRect( 200,( top + 200 ) % 600, 100, 200 ) );
        player.move( 0, - 5 );
    }
    if( sf::Keyboard::isKeyPressed( sf::Keyboard::S ) )
    {
        int top = player.getTextureRect().top;
        player.setTextureRect( sf::IntRect( 100,( top + 200 ) % 600, 100, 200 ) );
        player.move( 0, 5 );
    }
    if( sf::Keyboard::isKeyPressed( sf::Keyboard::A ) )
    {
        int top = player.getTextureRect().top;
        player.setTextureRect( sf::IntRect( 100,( top + 200 ) % 600, - 100, 200 ) );
        player.move( - 5, 0 );
    }
    if( sf::Keyboard::isKeyPressed( sf::Keyboard::D ) )
    {
        int top = player.getTextureRect().top;
        player.setTextureRect( sf::IntRect( 0,( top + 200 ) % 600, 100, 200 ) );
        player.move( 5, 0 );
    }
    if( sf::Keyboard::isKeyPressed( sf::Keyboard::E ) )
    {
        int top = player.getTextureRect().top;
        player.setTextureRect( sf::IntRect( 300,( top + 200 ) % 600, 100, 200 ) );
    }
}

BŁĄD:
||=== Build: Debug in platformowka (compiler: GNU GCC Compiler) ===|
C:\Users\Routa\Desktop\platformowka\main.cpp||In function 'int main()':|
C:\Users\Routa\Desktop\platformowka\main.cpp|98|error: variable or field 'Ruch_postaci' declared void|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

Czemu tak się dzieje i jak to obejść?
P-176032
pekfos
» 2020-01-12 01:53:08
variable or field 'Ruch_postaci' declared void
Zupełnie jakby dla kompilatora to nie wyglądało jak wywołanie.
» Kurs C++ » Poziom 2Funkcje - pierwsze starcie lekcja
P-176033
R0ut4
Temat założony przez niniejszego użytkownika
» 2020-01-12 13:27:02
BŁĄD:
||=== Build: Debug in platformowka (compiler: GNU GCC Compiler) ===|
obj\Debug\sterowanie.o||In function `ZN2sf4RectIiEC1Eiiii':|
C:\Users\Routa\Desktop\platformowka\sterowanie.cpp|4|multiple definition of `Ruch_postaci(sf::Sprite)'|
obj\Debug\main.o:C:\Users\Routa\Desktop\platformowka\sterowanie.cpp|4|first defined here|
||error: ld returned 1 exit status|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
Ruch_postaci( player );

C/C++
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
void Ruch_postaci( sf::Sprite player )
{
    if( sf::Keyboard::isKeyPressed( sf::Keyboard::W ) )
    {
        int top = player.getTextureRect().top;
        player.setTextureRect( sf::IntRect( 200,( top + 200 ) % 600, 100, 200 ) );
        player.move( 0, - 5 );
    }
    if( sf::Keyboard::isKeyPressed( sf::Keyboard::S ) )
    {
        int top = player.getTextureRect().top;
        player.setTextureRect( sf::IntRect( 100,( top + 200 ) % 600, 100, 200 ) );
        player.move( 0, 5 );
    }
    if( sf::Keyboard::isKeyPressed( sf::Keyboard::A ) )
    {
        int top = player.getTextureRect().top;
        player.setTextureRect( sf::IntRect( 100,( top + 200 ) % 600, - 100, 200 ) );
        player.move( - 5, 0 );
    }
    if( sf::Keyboard::isKeyPressed( sf::Keyboard::D ) )
    {
        int top = player.getTextureRect().top;
        player.setTextureRect( sf::IntRect( 0,( top + 200 ) % 600, 100, 200 ) );
        player.move( 5, 0 );
    }
    if( sf::Keyboard::isKeyPressed( sf::Keyboard::E ) )
    {
        int top = player.getTextureRect().top;
        player.setTextureRect( sf::IntRect( 300,( top + 200 ) % 600, 100, 200 ) );
    }
}
P-176035
pekfos
» 2020-01-12 14:55:04
Nie dołącza się plików .cpp przez #include.
» Kurs C++Dzielenie kodu na kilka plików źródłowych lekcja
P-176036
R0ut4
Temat założony przez niniejszego użytkownika
» 2020-01-12 16:39:56
Sprawdzę w domu, ale test.cpp z poprzedniego problemu dołączyłem przez #include i działa, ale jest taka różnica że tam jest sama klasa, a tutaj "czysty" void
P-176038
pekfos
» 2020-01-12 17:05:19
Pliki nagłówkowe nie są kompilowane, a pliki .cpp są. Dołączając plik .cpp do drugiego tworzysz sytuację, w której fragment kodu jest kompilowany dwa razy. Nie wszystko może występować w takim kontekście.
P-176039
R0ut4
Temat założony przez niniejszego użytkownika
» 2020-01-12 17:24:47
Coś ewidentnie spier****
main:
C/C++
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include "test.hpp"
#include "sterowanie.hpp"

enum Stan
{
    Menu, Wybor, Gra
};

void move();
void setPosition();

int main()
{
    sf::RenderWindow aplikacja( sf::VideoMode( 820, 720 ), "GRA" );
    aplikacja.setFramerateLimit( 30 );
    Stan stan = Menu;
   
    Obiekt pdl;
   
    //Tworzenie tekstur
    sf::Texture menu_textura;
    sf::Texture wybor_textura;
    sf::Texture gra_textura;
    sf::Texture player_texture;
    sf::Texture drzewo_texture;
    //Wczytywanie tekstur
    menu_textura.loadFromFile( "tekstura-menu.png" );
    wybor_textura.loadFromFile( "wybor-tekstura.png" );
    gra_textura.loadFromFile( "gra-tekstura.png" );
    player_texture.loadFromFile( "tekstura-gracza.png" );
    drzewo_texture.loadFromFile( "tekstura-drzewo.png" );
    //Tworzenie sprite
    sf::Sprite menu( menu_textura );
    sf::Sprite wybor( wybor_textura );
    sf::Sprite gra( gra_textura );
    sf::Sprite player( player_texture, sf::IntRect( 0, 0, 100, 200 ) );
    sf::Sprite drzewo( drzewo_texture );
   
   
    drzewo.setPosition( 200, 200 );
    player.setPosition( 100, 100 );
   
    while( aplikacja.isOpen() )
    {
        sf::Event event;
        while( aplikacja.pollEvent( event ) )
        {
            if( event.type == sf::Event::Closed )
                 aplikacja.close();
           
        }
        switch( stan )
        {
        case Menu:
            // OBSLUGA ZDARZEM W MENU
            if( sf::Keyboard::isKeyPressed( sf::Keyboard::Enter ) )
            {
                stan = Gra;
            }
            break;
           
        case Wybor:
            // OBSLUGA ZDARZEN W WYBORZE
           
            break;
        case Gra:
            // OBSLUGA ZDARZEN W GRZE
            /*if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
                        {
                            int top = player.getTextureRect().top;
                            player.setTextureRect(sf::IntRect(200, (top+200)%600, 100, 200));
                            player.move (0, -5);
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
                        {
                            int top = player.getTextureRect().top;
                            player.setTextureRect(sf::IntRect(100, (top+200)%600, 100, 200));
                            player.move (0, 5);
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
                        {
                            int top = player.getTextureRect().top;
                            player.setTextureRect(sf::IntRect(100, (top+200)%600, -100, 200));
                            player.move (-5, 0);
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
                        {
                            int top = player.getTextureRect().top;
                            player.setTextureRect(sf::IntRect(0, (top+200)%600, 100, 200));
                            player.move (5, 0);
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::E))
                        {
                            int top = player.getTextureRect().top;
                            player.setTextureRect(sf::IntRect(300, (top+200)%600, 100, 200));
                        }*/
            Ruch_postaci( player );
            break;
        }
       
        aplikacja.clear();
       
        switch( stan )
        {
        case Menu:
            // WYSWIETLANIE W MENU
            aplikacja.draw( menu );
            aplikacja.draw( pdl );
            break;
           
        case Wybor:
            // WYSWIETLANIE W WYBORZE POSTACI
            aplikacja.draw( wybor );
            break;
        case Gra:
            // WYSWIETLANIE W GRZE
            aplikacja.draw( gra );
            aplikacja.draw( drzewo );
            aplikacja.draw( player );
            break;
        }
        aplikacja.display();
    } //while
    return 0;
}
sterowanie.cpp i .hpp
C/C++
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include "sterowanie.hpp"

void Ruch_postaci( sf::Sprite player )
{
    if( sf::Keyboard::isKeyPressed( sf::Keyboard::W ) )
    {
        int top = player.getTextureRect().top;
        player.setTextureRect( sf::IntRect( 200,( top + 200 ) % 600, 100, 200 ) );
        player.move( 0, - 5 );
    }
    if( sf::Keyboard::isKeyPressed( sf::Keyboard::S ) )
    {
        int top = player.getTextureRect().top;
        player.setTextureRect( sf::IntRect( 100,( top + 200 ) % 600, 100, 200 ) );
        player.move( 0, 5 );
    }
    if( sf::Keyboard::isKeyPressed( sf::Keyboard::A ) )
    {
        int top = player.getTextureRect().top;
        player.setTextureRect( sf::IntRect( 100,( top + 200 ) % 600, - 100, 200 ) );
        player.move( - 5, 0 );
    }
    if( sf::Keyboard::isKeyPressed( sf::Keyboard::D ) )
    {
        int top = player.getTextureRect().top;
        player.setTextureRect( sf::IntRect( 0,( top + 200 ) % 600, 100, 200 ) );
        player.move( 5, 0 );
    }
    if( sf::Keyboard::isKeyPressed( sf::Keyboard::E ) )
    {
        int top = player.getTextureRect().top;
        player.setTextureRect( sf::IntRect( 300,( top + 200 ) % 600, 100, 200 ) );
    }
}
C/C++
#ifndef sterowanie.hpp
#define sterowanie.hpp

void Ruch_postaci( sf::Sprite player )

#endif // sterowanie

test.cpp i .hpp
C/C++
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include "test.hpp"

class Obiekt
    : public sf::Drawable
{
private:
    virtual void draw( sf::RenderTarget & target, sf::RenderStates states ) const
    {
        sf::VertexArray kwadrat( sf::Quads, 4 );
        kwadrat[ 0 ].position = sf::Vector2f( 100, 100 );
        kwadrat[ 1 ].position = sf::Vector2f( 300, 100 );
        kwadrat[ 2 ].position = sf::Vector2f( 300, 300 );
        kwadrat[ 3 ].position = sf::Vector2f( 100, 300 );
       
        sf::Texture textura;
        textura.loadFromFile( "podloga.jpg" );
       
        kwadrat[ 0 ].texCoords = sf::Vector2f( 0, 0 );
        kwadrat[ 1 ].texCoords = sf::Vector2f( 0, 100 );
        kwadrat[ 2 ].texCoords = sf::Vector2f( 100, 100 );
        kwadrat[ 3 ].texCoords = sf::Vector2f( 100, 0 );
       
        states.texture = & textura;
        target.draw( kwadrat, states );
    }
};
C/C++
#ifndef test.hpp
#define test.hpp

class Obiekt
    : public sf::Drawable
{
private:
    virtual void draw( sf::RenderTarget & target, sf::RenderStates states ) const;
};
#endif // test

BŁĄD:
||=== Build: Debug in platformowka (compiler: GNU GCC Compiler) ===|
C:\Users\Routa\Desktop\platformowka\sterowanie.hpp|1|warning: extra tokens at end of #ifndef directive|
C:\Users\Routa\Desktop\platformowka\sterowanie.hpp|2|warning: missing whitespace after the macro name|
C:\Users\Routa\Desktop\platformowka\test.hpp|1|warning: extra tokens at end of #ifndef directive|
C:\Users\Routa\Desktop\platformowka\test.hpp|1|warning: extra tokens at end of #ifndef directive|
C:\Users\Routa\Desktop\platformowka\test.hpp|2|warning: missing whitespace after the macro name|
C:\Users\Routa\Desktop\platformowka\sterowanie.hpp|1|warning: extra tokens at end of #ifndef directive|
C:\Users\Routa\Desktop\platformowka\sterowanie.hpp|2|warning: missing whitespace after the macro name|
C:\Users\Routa\Desktop\platformowka\test.hpp|2|warning: missing whitespace after the macro name|
C:\Users\Routa\Desktop\platformowka\sterowanie.cpp|5|error: expected initializer before 'void'|
C:\Users\Routa\Desktop\platformowka\test.cpp|5|error: redefinition of 'class Obiekt'|
C:\Users\Routa\Desktop\platformowka\test.hpp|4|error: previous definition of 'class Obiekt'|
C:\Users\Routa\Desktop\platformowka\main.cpp|6|error: expected initializer before 'enum'|
C:\Users\Routa\Desktop\platformowka\main.cpp||In function 'int main()':|
C:\Users\Routa\Desktop\platformowka\main.cpp|18|error: 'Stan' was not declared in this scope|
C:\Users\Routa\Desktop\platformowka\main.cpp|53|error: 'stan' was not declared in this scope|
C:\Users\Routa\Desktop\platformowka\main.cpp|55|error: 'Menu' was not declared in this scope|
C:\Users\Routa\Desktop\platformowka\main.cpp|59|error: 'Gra' was not declared in this scope|
C:\Users\Routa\Desktop\platformowka\main.cpp|63|error: 'Wybor' was not declared in this scope|
C:\Users\Routa\Desktop\platformowka\main.cpp|67|error: 'Gra' was not declared in this scope|
C:\Users\Routa\Desktop\platformowka\main.cpp|98|error: 'Ruch_postaci' was not declared in this scope|
C:\Users\Routa\Desktop\platformowka\main.cpp|104|error: 'stan' was not declared in this scope|
C:\Users\Routa\Desktop\platformowka\main.cpp|106|error: 'Menu' was not declared in this scope|
C:\Users\Routa\Desktop\platformowka\main.cpp|112|error: 'Wybor' was not declared in this scope|
C:\Users\Routa\Desktop\platformowka\main.cpp|116|error: 'Gra' was not declared in this scope|
||=== Build failed: 15 error(s), 8 warning(s) (0 minute(s), 0 second(s)) ===|
P-176040
pekfos
» 2020-01-12 17:31:12
C:\Users\Routa\Desktop\platformowka\sterowanie.hpp|1|warning: extra tokens at end of #ifndef directive|
C:\Users\Routa\Desktop\platformowka\sterowanie.hpp|2|warning: missing whitespace after the macro name|
C:\Users\Routa\Desktop\platformowka\test.hpp|1|warning: extra tokens at end of #ifndef directive|
C:\Users\Routa\Desktop\platformowka\test.hpp|1|warning: extra tokens at end of #ifndef directive|
C:\Users\Routa\Desktop\platformowka\test.hpp|2|warning: missing whitespace after the macro name|
C:\Users\Routa\Desktop\platformowka\sterowanie.hpp|1|warning: extra tokens at end of #ifndef directive|
C:\Users\Routa\Desktop\platformowka\sterowanie.hpp|2|warning: missing whitespace after the macro name|
C:\Users\Routa\Desktop\platformowka\test.hpp|2|warning: missing whitespace after the macro name|
Makro powinno mieć poprawną C++ową nazwę. test.hpp nie jest poprawną nazwą. Lepiej używaj TEST_HPP.

C:\Users\Routa\Desktop\platformowka\sterowanie.cpp|5|error: expected initializer before 'void'|
Ciekawe co jest przed tym voidem o którym mowa w błędzie.
C/C++
#include "sterowanie.hpp"

void Ruch_postaci( sf::Sprite player )
A w sterowanie.hpp
C/C++
#ifndef sterowanie.hpp
#define sterowanie.hpp

void Ruch_postaci( sf::Sprite player )

#endif // sterowanie
Brak średnika. To chyba nie było takie trudne.
P-176041
« 1 » 2 3
  Strona 1 z 3 Następna strona