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.


Topics - Dante12129

Pages: [1]
1
Graphics / Problem with Texture::create in custom class
« on: November 29, 2013, 02:44:44 am »
I am creating a custom class, called TexturedSprite, to ease the process of loading textures/ setting a sprite's texture/ drawing the sprite. It basically holds a Texture pointer and sprite and autmatically takes care of the relations between them. The problem is when I try to load a file into the texture.

Here are the definitions of my loadFromFile function and constructor that takes a string parameter:

bool TexturedSprite::loadFromFile(const std::string& filename, const sf::IntRect& area) { return m_texture->loadFromFile(filename, area); m_sprite.setTexture(*m_texture); }
TexturedSprite::TexturedSprite(const std::string& filename)
    {
        //Load the texture
        if(!m_texture->loadFromFile(filename))
           std::cout << "Failed to load file for textured sprite: " << filename << std::endl;
        m_sprite.setTexture(*m_texture); //Set the sprite to the texture
    }

I ran my program in gdb and the call stack says the problem is at sf::Texture::create, specifically line 125:
m_size.x        = width;
Here is my call stack:
#0 68ED9585     sf::Texture::create(this=0xbaadf00dbaadf00d, width=950, height=600) (C:\SFML-2.1\src\SFML\Graphics\Texture.cpp:125)
#1 68ED9A2A     sf::Texture::loadFromImage(this=0xbaadf00dbaadf00d, image=..., area=...) (C:\SFML-2.1\src\SFML\Graphics\Texture.cpp:192)
#2 68ED97F7     sf::Texture::loadFromFile(this=0xbaadf00dbaadf00d, filename=..., area=...) (C:\SFML-2.1\src\SFML\Graphics\Texture.cpp:160)
#3 00402349     whack::TexturedSprite::TexturedSprite(this=0x6f7708, filename=...) (C:\Users\Main\Documents\Programs\WhackGame\src\TexturedSprite.cpp:12)
#4 00401A73     whack::IntroState::IntroState(this=0x6f76f0, window=...) (C:\Users\Main\Documents\Programs\WhackGame\src\IntroState.cpp:12)
#5 004016DA     whack::Game::Game(this=0x22fbf0) (C:\Users\Main\Documents\Programs\WhackGame\src\Game.cpp:8)
#6 00401512     main() (C:\Users\Main\Documents\Programs\WhackGame\main.cpp:5)
 

Here is the code I'm calling it from (it crashes whether I use the initializer list or loadFromFile function:
IntroState::IntroState(sf::RenderWindow& window) : m_window(&window), m_needs_change(false, dte::StateChangeType::Next) //,m_background("Resources/Images/intro_background.png"), m_logo("Resources/Images/logo.png")
    {
        if(!m_background.loadFromFile("Resources/Images/intro_background.png"))
            std::cerr << "Unable to load background texture for intro state." << std::endl;
        if(!m_logo.loadFromFile("Resources/Images/logo.png"))
            std::cerr << "Unable to load background texture for intro state." << std::endl;
    }

It works fine if I do the textures and sprites separately. I am using TDM gcc 4.8.1, Windows 7 64-bit, and CB 12.11. I have included my TexturedSprite files and IntroState files in case you need any more information.

2
Graphics / Strange problem
« on: November 11, 2013, 11:32:51 pm »
I am having a problem drawing both a texture and the ball in my Pong game. When I don't load something into the texture and draw the ball, it will work. If I load something into the texture but don't draw the ball, it still works. But if I try to do them both, it'll crash after I close the window and the program is exiting. I have all my linking right and the image for the texture is in the right place.
Here's my code:

main.cpp
#include <SFML/Graphics.hpp>
#include "include/Ball.hpp"

int main()
{
    //Create the main window
    sf::RenderWindow main_window(sf::VideoMode(950, 600), "Epicyoobed Pong");
    main_window.setKeyRepeatEnabled(false);
    main_window.setFramerateLimit(60);

    //Load the background
    sf::Texture background_tex;
    if(!background_tex.loadFromFile("background.png"))
        return EXIT_FAILURE;
    sf::Sprite background(background_tex);

    //Create the ball
    pong::Ball main_ball(sf::Vector2f(50, 50));

    //Create the stuff for frame tracking
    sf::Clock delta_clock;
    sf::Time delta_time;

        //Start the game loop
    while (main_window.isOpen())
    {
        //Update the delta
        delta_time = delta_clock.getElapsedTime();
        delta_clock.restart();

        //Process events
        sf::Event event;
        while(main_window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed)
                main_window.close();
        }

        //Logic
        main_ball.ScreenCollide(main_window);
        main_ball.update(delta_time);

        //Render
        main_window.clear();
        main_window.draw(background);
        main_window.draw(main_ball);
        main_window.display();
    }

    return EXIT_SUCCESS;
}
Ball.hpp
#ifndef BALL_HPP
#define BALL_HPP

#include <SFML/Graphics.hpp>

namespace pong
{
    class Ball : public sf::Drawable
    {
        public:
            Ball();
            Ball(sf::Vector2f start_pos);
            ~Ball();

            //Setters
            void setVelocity(sf::Vector2f new_vel);

            //Collision Handling
            void ScreenCollide(const sf::RenderWindow& screen);

            //Main loop methods
            void update(const sf::Time& dt);
            void draw(sf::RenderTarget& target, sf::RenderStates states) const;

        private:
            //Position and velocity of the ball
            sf::Vector2f m_pos = {0, 0};
            sf::Vector2f m_vel = {500, 500};

            //The visual representation
            sf::CircleShape m_circle;
    };
}

#endif // BALL_HPP
Ball.cpp
#include "../include/Ball.hpp"

#include <iostream>

namespace pong
{
    Ball::Ball() = default;
    Ball::Ball(sf::Vector2f start_pos) : m_pos(start_pos), m_circle(15) { m_circle.setFillColor(sf::Color::Blue); }
    Ball::~Ball() = default;

    void Ball::setVelocity(sf::Vector2f new_vel) { m_vel = new_vel; }

    void Ball::ScreenCollide(const sf::RenderWindow& screen)
    {
        if(m_pos.x < 0 || (m_pos.x > screen.getSize().x - m_circle.getRadius() * 2))
            m_vel.x = -m_vel.x;
        if(m_pos.y < 0 || (m_pos.y > screen.getSize().y - m_circle.getRadius() * 2))
            m_vel.y = -m_vel.y;
    }

    void Ball::update(const sf::Time& dt) { m_circle.setPosition(m_pos += (m_vel * dt.asSeconds())); }
    void Ball::draw(sf::RenderTarget& target, sf::RenderStates states) const { target.draw(m_circle, states); }
}

I am on Windows 7 with with the TDM MinGW 4.8.1 and I'm using CB as my IDE.

3
General / Error with multidimensional array
« on: June 22, 2013, 04:28:39 am »
I am having trouble accessing a one-dimensional vector as a multi-dimensional one.
I have a MultiArray template class, and it is accessed using the function operator. It accesses elements through the expression row * m_columns + col. The row and col are the two inputs to the operator while the m_columns is the width/number of columns in the array. There is also a version that accesses the array with one number. Its underlying representation is a one-dimensional vector.

The problem is when I try to load a level:
void Map::LoadFromFile(const std::string& filename)
    {
        std::ifstream map_file(filename);

        std::cout << m_map.GetHeight() << ", " << m_map.GetWidth() << std::endl;
        std::cout <<  m_map_dimensions.y << ", " <<  m_map_dimensions.x << std::endl;
        if(map_file.is_open())
        {
            for(unsigned int i = 0; i < m_map_dimensions.y; i++)
            {
                for(unsigned int j = 0; j < m_map_dimensions.x; j++)
                {
                    int current_tile;
                    map_file >> current_tile;

                    try
                    {
                            m_map(i, j) = current_tile;
                    }
                    catch(std::exception& e)
                    {
                        std::cerr << "Vector overflow in map loading." << std::endl;
                        exit(EXIT_FAILURE);
                    }
                }
            }
        }
        else
            std::cerr << "Couldn't load map file: " << filename << "." << std::endl;
    }

(The m_map is a MultiArray<int> and the m_map_dimensions an sf::Vector2u)

The dimensions of the level are 20 * 15 (screen dimensions divided by height dimensions), so I made a map file that looks like this:
Quote
01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00


but it loads it skewed and only ever accesses up to element 229 in the MultiArray.

This is how it loads the file:
Quote
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

The graphical output code is outputting this messed-up array in the graphical representation of this using a spritesheet, so I know it is working correctly.

4
Graphics / Creating a font in initializer list
« on: June 14, 2013, 09:30:51 am »
I have a state machine in my game in order to handle different menus, gameplay sequences, credits and etc. Each state stores a reference to the main font in the game. The problem is that I am using a game class so that I can keep the code in my main down to a minimum. This class stores the actual font. The thing is that I initialize my state manager with the first state, so I use the initializer list. The thing is that I have to initialize the font before it, but I can't get it to work.

The variables are declared like this:
sf::RenderWindow main_window;
sf::Font main_font;

dte::StateManager m_state_manager;
 

And the game class' constructor looks like this:
Game::Game() : main_window(sf::VideoMode(640, 480), "Animal Instincts"), main_font(), m_state_manager(new MenuState(main_window, main_font)) { main_font.loadFromFile("Unispacerg.ttf"); }
 
The state manager has to take its first state in its constructor in order to work properly, as I have no function to initialize it later.

And the MenuState's constructor:
MenuState::MenuState(sf::RenderWindow& window, sf::Font& font) : m_window_ptr(&window), m_font(font), m_needs_change(false), m_welcome_message("Welcome", m_font, 30)
    {
        m_welcome_message.setColor(sf::Color::White);
        dte::CenterTextHor(m_welcome_message, *m_window_ptr);

        std::cout << "Menu state created." << std::endl;
    }
 

The problem is that this code will only show the text if I cause a new Menustate to be created. How would I go about initializing the font in the initializer list so that it will work the first time?

5
Network / Trouble connecting to remote computer
« on: June 14, 2013, 07:58:35 am »
I was working on a simple client/server test where a server sends a message to the client. The problem is that it'll work fine when I use localhost, but when me and my friend try it using each others' IPs it doesn't work. I am reading the IP and port from a text file. We are only changing the IP of the client since the server only uses the port. I also tried connecting to the server running on his machine with Telnet, but it still wouldn't work. We tried both the public and IPv4 IP addresses of our computers. What does it take to connect to a remote computer?

Client Code:
#include <fstream>
#include <iostream>
#include <sstream>
#include <boost/lexical_cast.hpp>
#include <SFML/Network.hpp>

int main()
{
    std::fstream connection_info_file("AP.txt");

    std::string server_ip;
    std::string port;
    std::getline(connection_info_file, server_ip);
    std::getline(connection_info_file, port);

    std::stringstream ip_buffer;
    for(auto itr = server_ip.find("=") + 2; itr < server_ip.size(); itr++)
    {
        ip_buffer << server_ip.at(itr);
    }
    server_ip = ip_buffer.str();

    std::stringstream port_buffer;
    for(auto itr = port.find("=") + 2; itr < port.size(); itr++)
    {
        port_buffer << port.at(itr);
    }
    port = port_buffer.str();

    std::cout << server_ip << std::endl;
    std::cout << port << std::endl;

    std::cout << "Welcome to the client." << std::endl;

    sf::TcpSocket server;
    sf::Socket::Status status = server.connect(server_ip, boost::lexical_cast<unsigned short>(port));
    if(status != sf::Socket::Done)
    {
        std::cerr << "Error connecting." << std::endl;
        return EXIT_FAILURE;
    }

    sf::Packet data;
    server.receive(data);

    std::string message;
    if(data)
    {
        std::cout << "Getting data from: " << server.getRemoteAddress() << std::endl;
        data >> message;
        std::cout << message << std::endl;
    }
    else
    {
        std::cerr << "Data received not valid." << std::endl;
        return EXIT_FAILURE;
    }

    sf::Clock delay;
    while(delay.getElapsedTime().asMilliseconds() < 5000);
    return EXIT_SUCCESS;
}
 
Server Code:
#include <fstream>
#include <iostream>
#include <sstream>
#include <boost/lexical_cast.hpp>
#include <SFML/Network.hpp>

int main()
{
    std::fstream connection_info_file("AP.txt");

    std::string server_ip;
    std::string port;
    std::getline(connection_info_file, server_ip);
    std::getline(connection_info_file, port);

    std::stringstream ip_buffer;
    for(auto itr = server_ip.find("=") + 2; itr < server_ip.size(); itr++)
    {
        ip_buffer << server_ip.at(itr);
    }
    server_ip = ip_buffer.str();

    std::stringstream port_buffer;
    for(auto itr = port.find("=") + 2; itr < port.size(); itr++)
    {
        port_buffer << port.at(itr);
    }
    port = port_buffer.str();

    std::cout << server_ip << std::endl;
    std::cout << port << std::endl;

    std::cout << "Welcome to the server." << std::endl;

    sf::TcpListener listener;
    if (listener.listen(boost::lexical_cast<unsigned short>(port)) != sf::Socket::Done)
    {
        std::cerr << "Error listening to socket." << std::endl;
        return EXIT_FAILURE;
    }

    sf::TcpSocket client;
    if (listener.accept(client) != sf::Socket::Done)
    {
        std::cerr << "Couldn't connect to client." << std::endl;
        return EXIT_FAILURE;
    }
    else
    {
        sf::Packet data;
        data << "Hello there.";

        std::cout << "Connected to: " << client.getRemoteAddress() << std::endl;
        client.send(data);
        std::cout << "Data sent." << std::endl;
    }

    sf::Clock delay;
    while(delay.getElapsedTime().asMilliseconds() < 5000);
    return EXIT_SUCCESS;
}
 

6
SFML wiki / CodingMadeEasy Tutorials
« on: May 30, 2013, 04:20:59 am »
I noticed that the link for CodingMadeEasy's SFML tutorials link to his 1.6 tutorials; I think this should be changed to his 2.0 tutorials.

7
Graphics / Vector of sprites
« on: May 22, 2013, 07:28:47 am »
Hello, I'm making a spritesheet using a std::vector of sprites. It is declared as
std::vector<sf::Sprite> m_sprites
in my Spritesheet. My loading function adds sprites by adding a sprite to the array with the whole texture and a rectangle of that sprite's postion, like so:
for(int i = 0; i < m_sprites_per_column; i++)
            {
                for(int j = 0; j < m_sprites_per_row; j++)
                {
                    m_sprites.push_back(sf::Sprite(m_texture, sf::IntRect(j*sprite_dimensions.x, i*sprite_dimensions.y, sprite_dimensions.x, sprite_dimensions.y)));
                }
            }
I have checked and all the sprite and spritesheet dimensions are correct and that the texture loads correctly. I also make sure to reserve a vector with the sprites per row and column so that it doesn't reallocate. The problem is that whenever it draws the screen, it just draws white (which is what I cleared it with). Here's my drawing code:
window_ptr->clear(sf::Color::White);

            for(unsigned int i = 0; i < terrain.GetDimensions().x /terrain.GetSpritesheet().GetSpriteWidth(); i++)
            {
                for(unsigned int j = 0; j < terrain.GetDimensions().y / terrain.GetSpritesheet().GetSpriteHeight(); j++)
                {
                    sf::Sprite current_sprite = terrain.GetSpritesheet().GetSprite(i * (terrain.GetDimensions().x / terrain.GetSpritesheet().GetSpriteWidth()) + j);
                    current_sprite.setPosition(sf::Vector2f(i*32, j*32));
                    window_ptr->draw(current_sprite);
                }
            }

            window_ptr->display();
Terrain is an instance of my Map class that loads a spritesheet and map file so you can draw the map based on the spritesheet.

8
Audio / Memory leak with music
« on: May 19, 2013, 02:00:30 am »
I am trying to load a music file. Loading and playing and using it work fine, but when I have an sf::Music object in my program, it gives me a return status of -2147418113.

Here's my code:
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>

int main()
{
        sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");

        sf::Music background_music;
        background_music.openFromFile("Five Armies.ogg");

        while (window.isOpen())
        {
            //Handle events
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }
                //Logic

                //Render
        }

        return 0;
}

 

I am using MinGW 4.7.1 on Windows 7 with Code::Blocks as my IDE.

I have narrowed it down to the music because if I comment out the music and loading it will work without leaking. And it gives me the same return value whether I play it or not.

Pages: [1]
anything