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

[SFML 2.0] wczytanie textury z pliku

Ostatnio zmodyfikowano 2013-10-06 10:19
Autor Wiadomość
MrPoxipol
» 2013-10-01 19:37:36
Całe 170 lini ?
Eee? 170 linii to dużo?
Sprawdź czy plik jest widziany z poziomu katalogu roboczego aplikacji.

/edit:
Sprawdziłem u siebie.
Działa..
Może po prostu nie rysujesz tego sprajta?

Testowy kod
C/C++
#include <SFML/Graphics.hpp>

class Graphics
{
public:
    sf::Sprite sprite;
    sf::Texture texture;
   
    void setGraphic( const char[] );
};

void Graphics::setGraphic( const char file[] )
{
    if( !texture.loadFromFile( file ) )
         return;
   
    sprite.setTexture( texture );
}

int main()
{
    sf::RenderWindow win( sf::VideoMode( 100, 100 ), "Okno", sf::Style::Default );
    Graphics spr;
    spr.setGraphic( "img.bmp" );
   
    while( win.isOpen() )
    {
        sf::Event event;
        while( win.pollEvent( event ) )
        {
            if( event.type == sf::Event::Closed )
                 win.close();
           
        }
       
        win.clear();
        win.draw( spr.sprite );
        win.display();
    }
    return 0;
}
g++ -omain main.cpp -IkatalogIncludeSFML -LkatalogLibSFML -lsfml-graphics -lsfml-window -lsfml-system

PS
Nie 'hight' tylko height :P
P-93003
Szary
Temat założony przez niniejszego użytkownika
» 2013-10-04 11:23:30
Ktoś prosił żebym wkleił całość...
C/C++
#pragma once
#include <stdlib.h>
#include <cstdlib>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Window.hpp>
#include <iostream>
#include <string>
#include <math.h>
#include <cmath>
using namespace std;
class Graphics
{
public:
    double x, y;
    double fast;
    int width, height;
   
    sf::Sprite sprite;
    sf::Texture texture;
   
    Graphics( const char[], int, int, int, int, double );
    Graphics();
    ~Graphics( void );
   
   
    void setGraphic( const char[], int, int, int, int, double );
    bool collision( Graphics enemy );
   
private:
   
};

#include "StdAfx.h"
#include "Graphics.h"


Graphics::Graphics( const char file[], int _x, int _y, int _width, int _height, double _fast = 0 )
    : x( _x )
    , y( _y )
    , fast( _fast )
    , width( _width )
    , height( _height )
{
    if( texture.loadFromFile( file ) ) {
        cout << "nie mozna wczytac " << file << endl;
        return;
    }
    sprite.setTexture( texture );
}

Graphics::Graphics()
{
}

Graphics::~Graphics( void )
{
}

bool Graphics::collision( Graphics enemy )
{
    if( sqrt(( abs( pow( x + width / 2 - enemy.x + enemy.width / 2, 2 ) ) +( abs( pow( y + height / 2 - enemy.y + enemy.width / 2, 2 ) ) ) ) )
    <= sqrt(( abs( pow(( width / 2 ) -( enemy.width / 2 ), 2.0 ) ) +( abs( pow(( height / 2 ) -( enemy.width / 2 ), 2.0 ) ) ) ) ) )
         return true;
   
    return false;
}

void Graphics::setGraphic( const char file[], int _x, int _y, int _width, int _height, double _fast = 0 )
{
   
    if( !texture.loadFromFile( file ) ) {
        cout << "nie mozna wczytac " << file << endl;
        return;
    }
   
    sprite.setTexture( texture );
    x = _x;
    y = _y;
    height = _height;
    width = _height;
    fast = _fast;
}
C/C++
#include "StdAfx.h"
#include "Menus.h"

#pragma once
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class Menus
{
public:
    int record;
   
    Menus( void );
    ~Menus( void );
   
    bool Save( int );
    int Load();
};

Menus::Menus( void )
{
    record = Load();
}


Menus::~Menus( void )
{
}


bool Menus::Save( int milage )
{
    ofstream file( "distance.txt" );
   
    if( !file.is_open() )
    {
        return false;
    }
   
    file << milage;
   
    cout << "Zapisano do pliku: " << milage << endl;
   
    file.close();
    return true;
}

int Menus::Load()
{
    ifstream file( "distance.txt" );
   
    //tu prawdopodobnie trzeba wyłapać jakiś wyjątek, albo jakimś rzucić
    if( !file.is_open() ) {
        cout << "Nie udało się otworzyć pliku!" << endl;
        return 0;
    }
   
    //przesunięcie wewnętrznego wskaźnika pozycji na początek pliku
    file.clear();
    file.seekg( 0, ios::beg );
   
    int milage;
   
    file >> milage;
    cout << "Odczytano z pliku: " << milage << endl;
   
    file.close();
   
    return milage;
}

C/C++
// SFML_2.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include <sstream>
#include "Graphics.h"
#include "Menus.h"
using namespace std;

Graphics pigs[ 3 ], ground[ 3 ], sky[ 2 ],
player( "graphics/gracz.png", 50, 290, 45, 60, 0 ), menu( "menu.png", 0, 0, 800, 600, 0 ),
out( "graphics/exit.jpg", 50, 550, 88, 70, 0 ), start( "graphics/start.jpg", 700, 200, 145, 79, 0 );
Menus file;

int main()
{
    for( int iK = 0; iK < 3; iK++ )
         pigs[ iK ].setGraphic( "graphics/swinie.png", rand() % 1000 + 1000, rand() % 350, 60, 60, 0.2 );
   
    ground[ 0 ].setGraphic( "graphics/kafelek.png", 0, 350, 465, 255, 0.1 );
    ground[ 1 ].setGraphic( "graphics/kafelek.png", 464, 350, 465, 255, 0.1 );
    ground[ 2 ].setGraphic( "graphics/kafelek.png", 928, 350, 465, 255, 0.1 );
   
    sky[ 0 ].setGraphic( "graphics/tlo.png", 0, 0, 800, 357, 0.05 );
    sky[ 0 ].setGraphic( "graphics/tlo.png", 800, 0, 800, 357, 0.05 );
   
   
    int distance = 0;
    sf::RenderWindow window( sf::VideoMode( 800, 600 ), "GAME" );
   
   
    menu.sprite.setPosition( sf::Vector2f( menu.x, menu.y ) );
    out.sprite.setPosition( sf::Vector2f( out.x, out.y ) );
    start.sprite.setPosition( sf::Vector2f( start.x, start.y ) );
   
   
    /*sf::SoundBuffer buffer;
    if (!buffer.loadFromFile("sample.ogg"))
    return -1;
   
    sf::Sound sound;
    sound.setBuffer(buffer);
    sound.play();
       */
    while( window.isOpen() )
    {
        sf::Event event;
        while( window.pollEvent( event ) )
        {
            if( event.type == sf::Event::Closed )
                 window.close();
            else if( event.key.code == sf::Keyboard::Escape && !event.KeyReleased )
                 window.close();
           
            if( event.type == sf::Event::MouseButtonPressed )
            {
                //obliczenie obszaru sprite'a
                sf::FloatRect areaStart( start.sprite.getGlobalBounds() );
                sf::FloatRect areaOut( out.sprite.getGlobalBounds() );
               
                //określenie pozycji myszki
                sf::Vector2f mause( sf::Mouse::getPosition( window ) );
               
                if( areaOut.contains( mause ) )
                     window.close();
                else if( areaStart.contains( mause ) )
                {
                    sf::Font font;
                    if( !font.loadFromFile( "graphics/arial.ttf" ) )
                         return - 1;
                   
                    //***********
                    bool gameOver = false;
                    while( !gameOver )
                    {
                        window.pollEvent( event );
                        for( int iK = 0; iK < 3; iK++ ) {
                            /*double milage = sqrt((abs(77 - pigs[iK].x + 30)*abs(77 - pigs[iK].x + 30)) + (abs((y + 30) - (pigs[iK].y + 30))*abs((y + 30) - (pigs[iK].y + 30))));
                            if(milage <= 7)
                            */
                            gameOver = player.collision( pigs[ iK ] );
                        }
                       
                        std::ostringstream StrP2;
                        StrP2 << distance / 100;
                        std::string scoreP2( StrP2.str() );
                       
                        sf::Text text;
                        text.setFont( font );
                        text.setCharacterSize( 50 );
                        text.setString( scoreP2 );
                       
                        for( int iK = 0; iK < 3; iK++ ) {
                            if( pigs[ iK ].x < - 60 ) {
                                pigs[ iK ].x = rand() % 1000 + 1000;
                                pigs[ iK ].y = rand() % 290;
                            }
                        }
                       
                        ground[ 0 ].sprite.setPosition( sf::Vector2f( ground[ 0 ].x, ground[ 0 ].y ) );
                        ground[ 1 ].sprite.setPosition( sf::Vector2f( ground[ 1 ].x, ground[ 1 ].y ) );
                        ground[ 2 ].sprite.setPosition( sf::Vector2f( ground[ 2 ].x, ground[ 2 ].y ) );
                        sky[ 0 ].sprite.setPosition( sf::Vector2f( sky[ 0 ].x, sky[ 0 ].y ) );
                        sky[ 1 ].sprite.setPosition( sf::Vector2f( sky[ 1 ].x, sky[ 1 ].y ) );
                        player.sprite.setPosition( sf::Vector2f( player.x, player.y ) );
                        text.setPosition( 700, 20 );
                       
                        for( int iK = 0; iK < 3; iK++ )
                             pigs[ iK ].sprite.setPosition( sf::Vector2f( pigs[ iK ].x, pigs[ iK ].y ) );
                       
                       
                        if( event.type == sf::Event::Closed )
                             window.close();
                       
                        if( event.key.code == sf::Keyboard::Escape && event.KeyPressed )
                             break;
                       
                        if( event.key.code == sf::Keyboard::Space && player.y > 0 && event.type == event.KeyPressed )
                             player.y -= player.fast;
                        else if( player.y < 295 && event.type == event.KeyReleased )
                             player.y += player.fast;
                       
                        window.clear();
                        window.draw( sky[ 0 ].sprite );
                        window.draw( sky[ 1 ].sprite );
                        window.draw( ground[ 0 ].sprite );
                        window.draw( ground[ 1 ].sprite );
                        window.draw( ground[ 2 ].sprite );
                        window.draw( player.sprite );
                       
                       
                        for( int iK = 0; iK < 3; iK++ )
                             window.draw( pigs[ iK ].sprite );
                       
                        window.draw( text );
                        window.display();
                        ejl
                       
                        distance++;
                       
                        for( int iK = 0; iK < 3; iK++ )
                             pigs[ iK ].x -= pigs[ iK ].fast;
                       
                        if( sky[ 1 ].x <= 0 || sky[ 0 ].x <= - 800 ) {
                            sky[ 1 ].x = 800;
                            sky[ 0 ].x = 0;
                        }
                       
                        if( ground[ 0 ].x <= - 464 || ground[ 1 ].x <= 0 || ground[ 2 ].x <= 464 ) {
                            ground[ 0 ].x = 0;
                            ground[ 1 ].x = 464;
                            ground[ 2 ].x = 928;
                        }
                    }
                }
            }
        }
        window.clear();
        window.draw( menu.sprite );
        window.draw( start.sprite );
        window.draw( out.sprite );
        window.display();
    }
    return 0;
}

Wklejam te obrazki prawie w każdy folder i nic! Dalej nie może ich znaleźć a ja nie mam pojęcia dlaczego.. pomocy mądry ludu!
P-93079
MrPoxipol
» 2013-10-04 20:36:53
Skopiuj sobie exeka do jakiegoś folderu i tam zrób całą strukturę katalogów. Odpal normalnie (bez IDE), bo nie wiem jaki możesz mieć ustawiony katalog roboczy.
P-93108
Szary
Temat założony przez niniejszego użytkownika
» 2013-10-05 15:28:11
Zrobiłem tak, że działa ale nie rozwiązałem problemu!(WHAT??)

Jedyną zmiana to taka, że wczytanie textury z pliku, jest bezpośrednio w main'ie a nie w klasie.
Jeżeli ktoś ma pomysł, jest w stanie mi wytłumaczyć dlaczego w klasie nie działa a w main'ie działa, to bardzo poproszę. Jutro zamknę temat.
P-93166
RazzorFlame
» 2013-10-06 10:19:03
Jesli uruchamiasz go z poziomu Code::Blocks (przez wcisniecie Compile & Run albo Run) to wtedy to nie ma znaczenia czy w main czy w klasie. Uruchom przez normalny dwuklik exeka.
P-93223
1 « 2 »
Poprzednia strona Strona 2 z 2