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

funkcja z SFML

Ostatnio zmodyfikowano 2020-03-20 18:10
Autor Wiadomość
Xgrod
Temat założony przez niniejszego użytkownika
funkcja z SFML
» 2020-03-18 18:26:34
Mam taką funkcję
C/C++
void Okno::add_to_button( int type_event, vector < string > typ_button, vector < Vector2f > wymiary, vector < Vector2f > position,
vector < string > name, vector < Vector2i > align, unsigned int CharacterSize, Color col, int args )
{
    cout << name.size() << endl;
    return;
    // return służy za szukanie błędu
    // reszta kodu
Program się uruchamia ale wyskakuje okno że program przestała działać.Po takiej zmianie:
C/C++
void Okno::add_to_button( int type_event, vector < string > typ_button, vector < Vector2f > wymiary, vector < Vector2f > position,
vector < string > name, vector < Vector2i > align, unsigned int CharacterSize, Color col, int args )
{
    return;
Program działa normalnie. Ogólnie bez tego return program przestaje działać w różnych miejscach wykonywania funkcji.
Ktoś może wyjaśnić na czym polega błąd ?
Wybaczcie za nazwę tematu ale nie miałem pomysłu na nazwę.
P-176399
pekfos
» 2020-03-18 21:46:15
Problem jest gdzie indziej. Chociaż ta funkcja wygląda słabo z tym całym kopiowaniem wektorów, jest poprawna.
P-176401
Xgrod
Temat założony przez niniejszego użytkownika
» 2020-03-19 12:16:51
Zrobiłem taki test usunąłem funkcję i klasę doprowadzając projekt do momentu w którym wszystko działało i po uruchomieniu aplikacja przestawała działać (jak testowałem kod z tą funkcją w której nie było kodu raz projekt uruchamiał się normalnie a raz wyskakiwało okienko że przestawał działać. Pytanie jest to raczej błąd w kodzie czy jakiś błąd programu Code::Blocks. Bo sam nie wiem co się dzieje z tym projektem.
P-176407
pekfos
» 2020-03-19 20:25:08
Na pewno błąd w Twoim kodzie. Podaj go trochę.
P-176413
Xgrod
Temat założony przez niniejszego użytkownika
» 2020-03-20 14:16:35
Korzystam z własnej biblioteki statycznej. Jej nie podam całej(zawiera 1500 lini).
C/C++
bool folder( const char * sciezka )
{
    DIR * dir;
    bool save = false;
    if(( dir = opendir( sciezka ) ) ) { save = true; }
    closedir( dir );
    delete dir;
    return save;
}
template < template < class t1, class co_t1 = std::allocator < t1 >> class T1, template < class t2, class co_t2 = std::allocator < t2 >> class T2 >
void path_sort( const char * sciezka, T1 < std::pair < std::string, std::string >>& save, T2 < std::string >& file_extension )
{
    T1 < std::pair < std::string, std::string >> file;
    dirent * name = new dirent;
    DIR * name_ID = new DIR;
    std::string t1, t2, t;
    auto search_extension =[ & ]()
    {
        if( file_extension.size() == 0 ) { return true; }
        size_t poz_extension;
        for( auto i: file_extension )
        {
            if(( poz_extension = t2.find( i ) ) != std::string::npos ) { return true; }
        }
        return false;
    };
    if( name_ID = opendir( sciezka ) )
    {
        while( name = readdir( name_ID ) )
        {
            t1 = sciezka;
            t2 = name->d_name;
            t = t1 + t2 + "/";
            const char * ged;
            ged = t.c_str();
            if( folder( ged ) )
            {
                if( t2[ 0 ] != '.' ) { save.push_back( std::pair < std::string, std::string >( t, "" ) ); }
            }
            else
            {
                if( search_extension() == true ) { file.push_back( std::pair < std::string, std::string >( t1 + t2, t2 ) ); }
            }
        }
        for( auto i: file )
        {
            save.push_back( i );
        }
    }
    delete name;
    delete name_ID;
}
template < template < class t1, class co_t1 = std::allocator < t1 >> class T1, template < class t2, class co_t2 = std::allocator < t2 >> class T2 >
void find_files( std::string name, T1 < std::pair < std::string, std::string >>& files, T2 < std::string >& file_extension )
{
    const char * path_name = name.c_str();
    T1 < T1 < std::pair < std::string, std::string >>> read_data;
    read_data.push_back( T1 < std::pair < std::string, std::string >>() );
    if( folder( path_name ) == true )
    {
        path_sort( path_name, read_data.back(), file_extension );
        for(;; )
        {
            if( read_data.size() == 0 ) { break; }
            if( read_data.back().front().second == "" )
            {
                const char * path = read_data.back().front().first.c_str();
                read_data.push_back( T1 < std::pair < std::string, std::string >>() );
                path_sort( path, read_data.back(), file_extension );
            }
            else
            {
                for( auto i: read_data.back() )
                {
                    files.push_back( i );
                }
                read_data.back().clear();
            }
            if( read_data.size() == 0 ) { break; }
            for(;; )
            {
                if( read_data.back().size() == 0 )
                {
                    read_data.pop_back();
                    if( read_data.size() == 0 ) { break; }
                    read_data.back().erase( read_data.back().begin() );
                }
                else { break; }
            }
        }
    }
    delete path_name;
}
P-176426
Xgrod
Temat założony przez niniejszego użytkownika
» 2020-03-20 14:18:01
main.cpp
C/C++
#include "gra.h"
#include <SFML/Graphics.hpp>
#include <dirent.h>
using namespace std;
using namespace sf;
using namespace sr;
int main( int Size, char ** lok )
{
    auto folder =[ & lok ]()
    {
        string plu = lok[ 0 ];
        plu.erase( plu.begin() + plu.find_last_of( char( 92 ), string::npos ), plu.end() );
        plu.erase( plu.begin() + plu.find_last_of( char( 92 ), string::npos ), plu.end() );
        plu.erase( plu.begin() + plu.find_last_of( char( 92 ), string::npos ) + 1, plu.end() );
        for( int i = 0; i < plu.size(); i++ )
        {
            if( plu[ i ] == 92 ) { plu[ i ] = '/'; }
        }
        return plu;
    };
    sciezka_gry = folder();
    float rozmiar = 2;
    int klatki = 0;
    RenderWindow okno;
    ustawienia_okna( okno );
    srand( time( NULL ) );
    //setlocale(LC_ALL,"");
    vector < string > rozszerzenie { ".png" };
    vector < vector < pair < string, string >>> sciezki;
    sciezki.push_back( vector < pair < string, string >>() );
    vector < New_Texture > tekstura;
    vector < New_Sprite > grafika;
    vector < New_Image > baza_wszystkiego; /// Z grafika_wszystkiego
    New_Image gracz;
    Image qe;
    qe.loadFromFile( "kursor.png" );
    Cursor cursor;
    cursor.loadFromPixels( qe.getPixelsPtr(), Vector2u( 15, 25 ), Vector2u( 1, 2 ) );
    okno.setMouseCursor( cursor );
    find_files( sciezka_gry + "ekran/PNG/", sciezki.back(), rozszerzenie );
    //Okno test(sciezki.back(),sciezka_gry+"ekran/Font/GameFont.ttf");
    //test.create_panel(Okno::grey,Vector2f(250,200),Vector2f(okno.getSize().x/2-125,okno.getSize().y/2-100),"GRA",Vector2i(-1,8),24,Color::Black);
    /*test.add_to_button(Okno::Click,{Okno::blue+"button02",Okno::blue+"button02"},
    {Vector2f(0,40),Vector2f(0,40)},{Vector2f(0,10),Vector2f(0,60)},
    {"PLAY","OPTIONS"},{Vector2i(-1,5),Vector2i(-1,5)},24,Color::Black,3);*/
    sciezki.push_back( vector < pair < string, string >>() );
    find_files( sciezka_gry + "Postacie/", sciezki.back(), rozszerzenie );
    load_files( baza_wszystkiego, sciezki.back() );
    auto stairs =[]( New_Image & first, New_Image & second, int length, bool safe )
    {
        New_Image e( safe );
        e = second;
        for( int i = 1; i < length; i++ )
        { e *=( first * i ) + second; }
        return e;
    };
    gracz += baza_wszystkiego[ 0 ];
    //gracz=stairs(baza_wszystkiego[0],baza_wszystkiego[1],8,true);
    tekstura.push_back( New_Texture( gracz ) );
    for( int i = 0; i < tekstura.size(); i++ )
    {
        grafika.push_back( New_Sprite( tekstura[ i ] ) );
        grafika.back().determine_the_size( 2 );
        grafika.back().setPosition( 50, 30 );
    }
    /*
    vector <int> nowa={10,11,9,11};
    grafika.back().stworz_kolejnosc(nowa,Keyboard::A);
    nowa={4,3,5,3};
    grafika.back().stworz_kolejnosc(nowa,Keyboard::D);*/
    Event e;
    while( okno.isOpen() )
    {
        while( okno.pollEvent( e ) )
        {
            if( e.type == Event::Closed ||( Keyboard::isKeyPressed( Keyboard::Escape ) ) ) { okno.close(); }
            //if(e.type==Event::KeyPressed)grafika.back().status[e.key.code]=true;
            //if(e.type==Event::KeyReleased)grafika.back().status[e.key.code]=false;
            if(( e.type == Event::MouseButtonPressed ) &&( e.mouseButton.button == Mouse::Left ) )
            {
                rozmiar *= 0.9;
                grafika.back().determine_the_size( rozmiar );
            }
            if(( e.type == Event::MouseButtonPressed ) &&( e.mouseButton.button == Mouse::Right ) )
            {
                rozmiar += 0.05;
                grafika.back().determine_the_size( rozmiar );
            }
            if(( e.type == Event::MouseButtonReleased ) &&( e.mouseButton.button == Mouse::Left ) )
            {
            }
        }
        //if(klatki%grafika.back().fast_change==0)grafika.back().ruch(okno);
        okno.clear( Color::White );
        for( int j = 0; j < grafika.size(); )
        {
            okno.draw( grafika[ j ] );
            j++;
        }
        //test.draw(okno);
        okno.display();
    }
    return 0;
}
P-176427
Xgrod
Temat założony przez niniejszego użytkownika
» 2020-03-20 14:19:09
gra.h
C/C++
#include "a/macro_def.h"
#include "a //rysuj.h"
#include <SFML/Graphics.hpp>
#ifndef gra_hpp
#define gra_hpp
using namespace sr;
using namespace sf;
using namespace std;
static string sciezka_gry;
void ustawienia_okna( RenderWindow & okno );
template < typename Typ > Typ wart_bewz( Typ & x );
bool test_kolizji( Vector2f a, Vector2f b, Vector2f c, Vector2f d );
Vector2f ruch( float kat, float przesuniecie ); /*
template <typename T,typename ... Args>
auto trans_v(Args ... args)
{
string test=typeid(T).name();
size_t t1;
if((t1=test.find("Vector2"))!=string::npos)
{
vector<T> s;
vector<float> zmien{args...};
for(int i=0,j=1;i<zmien.size();)
{
s.emplace_back(zmien[i],zmien[j]);
i+=2;
j+=2;
}
return s;
}
vector<T> s{args...};
return s;
}*/
class Text_1_0
    : public Text
{
public:
    Text_1_0()
        : Text()
    { }
    Text_1_0( const String & strin, const Font & font, unsigned int charactersize )
        : Text( strin, font, charactersize )
    { }
    Vector2f getSize()
    {
        return Vector2f( this->getGlobalBounds().width, this->getGlobalBounds().height );
    }
};
/*struct data_okno
{
unsigned int CharacterSize;
Color color;
data_okno(unsigned int charactersize=20,Color color=Color::White)
{
CharacterSize=charactersize;
this->color=color;
}
void load(unsigned int charactersize,Color color)
{
CharacterSize=charactersize;
this->color=color;
}
};*/
class Player
    : public New_Sprite
{
    struct frame
    {
        vector < int > nr_klatki;
        vector < int > special_frame;
        Uint16 ID_Keyboard;
        int wsk_nr;
        frame()
        {
            wsk_nr = 0;
        }
    };
public:
    vector < IntRect > nr_postac;
    vector < frame > kolejnosc;
    map < Uint16, bool > status;
    int fast_change;
    int frame_date, scene_date;
    int nr_klatki;
    bool player_go;
    Player( New_Texture & T1 )
        : New_Sprite( T1 )
    {
        player_go = false;
        nr_klatki = 4;
        fast_change = 8;
        for( auto i: wymiary )
             nr_postac.push_back( IntRect( i.polozenie.x, i.polozenie.y, i.wymiary.x, i.wymiary.y ) );
       
        frame_date = 0;
        scene_date = 0;
        //this->setTextureRect(nr_postac[4]);
    }
private:
    int search_order( Uint16 ID );
    void change_frame( int cha1 );
public:
    void stworz_kolejnosc( vector < int >& kol, Uint16 ID );
    void ruch( RenderWindow & okno );
};
class Okno
{
    struct Napisy
    {
        Sprite_1_0 NS1;
        Text_1_0 data;
        Napisy( New_Texture & lad, Font & font )
        {
            NS1.setTexture( lad );
            data.setFont( font );
        }
        Napisy() = default;
    };
    struct Button_Click
    {
        int active;
        vector < Sprite_1_0 > NS1;
        vector < Text_1_0 > data;
        Button_Click() = default;
        Button_Click( int change ) { active = change; }
    };
public:
    enum Type_Event
    {
        Click,
        Click_Change,
        Move,
        Hover
    };
    static const string blue;
    static const string green;
    static const string grey;
    static const string red;
    static const string yellow;
    static const string buton;
    static const string box;
    static const string slider;
    Font typ_text;
    Napisy main_panel;
    Vector2f positions;
    vector < Button_Click > button_click;
    map < string, SoundBuffer > Sprite_Buffer;
    vector < Sound > sound;
    vector < New_Texture > grafiki;
    template < template < class t, class co_t = allocator < t >> class T >
    Okno( T < pair < string, string >>& zal, string styl )
    {
        vector < New_Image > e;
        load_files( e, zal );
        for( auto i: e )
        {
            grafiki.push_back( New_Texture( i ) );
        }
        typ_text.loadFromFile( styl );
    }
    void create_panel( string color_panel, Vector2f wymiary, Vector2f position = Vector2f( 0, 0 ), string name = ""
    , Vector2i align = Vector2i( 0, 0 ), unsigned int CharacterSize = 20, Color col = Color::Black );
    template < typename...Args >
    void add_to_button( int type_event, vector < string > typ_button, vector < Vector2f > wymiary, vector < Vector2f > position,
    vector < string > name, vector < Vector2i > align, unsigned int CharacterSize, Color col, Args...args );
    //void create_sound(string ID,SoundBuffer& soundB);
    void draw( RenderWindow & okno );
};
#include "window.cpp"
#endif // gra_hpp[/code]
P-176428
Xgrod
Temat założony przez niniejszego użytkownika
» 2020-03-20 14:20:28
gra.cpp
C/C++
#include "gra.h"
#include <SFML/Graphics.hpp>
using namespace std;
using namespace sf;
using namespace sr;
const string Okno::blue = "blue_";
const string Okno::green = "gren_";
const string Okno::grey = "grey_";
const string Okno::red = "red_";
const string Okno::yellow = "yellow_";
const string Okno::buton = "button";
const string Okno::box = "box";
const string Okno::slider = "slider";
int Player::search_order( Uint16 ID )
{
    for( int i = 0; i < kolejnosc.size(); i++ )
    {
        if( this->kolejnosc[ i ].ID_Keyboard == ID )
             return i;
       
    }
    return 0;
}
void Player::change_frame( int cha1 )
{
    if( kolejnosc[ cha1 ].special_frame.size() > 0 )
    { }
    else
    {
        if( frame_date + 1 >= kolejnosc[ cha1 ].nr_klatki.size() ) { frame_date = 0; }
        else
        { frame_date++; }
        this->setTextureRect( nr_postac[ kolejnosc[ cha1 ].nr_klatki[ frame_date ] ] );
        nr_klatki = kolejnosc[ cha1 ].nr_klatki[ frame_date ];
        scene_date = cha1;
    }
}
void Player::stworz_kolejnosc( vector < int >& kol, Uint16 ID )
{
    this->kolejnosc.push_back( frame() );
    this->kolejnosc.back().nr_klatki = kol;
    kol.clear();
    this->kolejnosc.back().ID_Keyboard = ID;
}
void Player::ruch( RenderWindow & okno )
{
    int r;
    if(( status[ Keyboard::A ] == true ) &&( okno.setActive() == true ) )
    {
        r = search_order( Keyboard::A );
        this->move( - 20, 0 );
        change_frame( r );
    }
    if(( status[ Keyboard::D ] == true ) &&( okno.setActive() == true ) )
    {
        r = search_order( Keyboard::D );
        this->move( 20, 0 );
        change_frame( r );
    } /*
    if((status[Keyboard::W])&&(okno.setActive()==true))
    {
    r=search_order(Keyboard::W);
    change_frame(r);
    }*/
}
void Okno::create_panel( string color_panel, Vector2f wymiary, Vector2f position, string name, Vector2i align, unsigned int CharacterSize, Color col )
{
    color_panel += "panel.png";
    for( int i = 0; i < grafiki.size(); i++ )
    {
        if( grafiki[ i ].wymiary[ 0 ].ID == color_panel )
        {
            main_panel.NS1.setTexture( grafiki[ i ] ); break; }
    }
    if( main_panel.NS1.getSize().x == 0 || main_panel.NS1.getSize().y == 0 )
    {
        cout << main_panel.NS1.getSize().x << " " << main_panel.NS1.getSize().y << " error" << endl;
        return;
    }
    main_panel.NS1.setScale( wymiary.x / main_panel.NS1.getSize().x, wymiary.y / main_panel.NS1.getSize().y );
    main_panel.NS1.setPosition( position );
    if( name == "" ) { return; }
    if( align.x < 0 && align.y < 0 ) { return; }
    main_panel.data.setFont( typ_text );
    main_panel.data.setCharacterSize( CharacterSize );
    main_panel.data.setFillColor( col );
    main_panel.data.setString( name );
    if( align.x < 0 )
    {
        if( main_panel.data.getSize().x > main_panel.NS1.getSize().x )
        {
            main_panel.NS1.setScale( main_panel.data.getSize().x / main_panel.NS1.getSize().x, 1 );
        }
        if( main_panel.data.getSize().y + align.y > main_panel.NS1.getSize().y )
        {
            main_panel.NS1.setScale( 1,( main_panel.data.getSize().y + align.y ) / main_panel.NS1.getSize().y );
        }
        main_panel.data.setPosition
        (( main_panel.NS1.getSize().x - main_panel.data.getSize().x ) / 2 + position.x, position.y + align.y );
    }
    else
    {
        if( main_panel.data.getSize().x + align.x > main_panel.NS1.getSize().x )
        {
            main_panel.NS1.setScale(( main_panel.data.getSize().x + align.x ) / main_panel.NS1.getSize().x, 1 );
        }
        if( main_panel.data.getSize().y + align.y > main_panel.NS1.getSize().y )
        {
            main_panel.NS1.setScale( 1,( main_panel.data.getSize().y + align.y ) / main_panel.NS1.getSize().y );
        }
        main_panel.data.setPosition( align.x + position.x, align.y + position.y );
    }
    positions = main_panel.NS1.getPosition();
}
void Okno::draw( RenderWindow & okno )
{
    okno.draw( main_panel.NS1 );
    okno.draw( main_panel.data );
    for( auto i: button_click )
    {
        for( int j = 0; j < i.NS1.size(); j++ )
        {
            okno.draw( i.NS1[ j ] );
        }
        for( int j = 0; j < i.data.size(); j++ )
        {
            okno.draw( i.data[ j ] );
        }
    }
}
P-176429
« 1 » 2
  Strona 1 z 2 Następna strona