Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Problems with Sprite and Textures  (Read 1948 times)

0 Members and 1 Guest are viewing this topic.

timrik

  • Newbie
  • *
  • Posts: 2
    • View Profile
Problems with Sprite and Textures
« on: October 26, 2015, 11:11:41 pm »
Sorry for my english and my bad code, it is my first game with SFML. I am creating game like bomberman. Instead by pressing the space bar to display the dynamite, it displays a white square. I checked the name of the file 100 times. And if I create an object of class dynamite separately in maine.cpp, then everything is fine displays.when I try to portray as one of the elements of a vector, which is a field of a class player, displays white square. Plz help =)
main.cpp
 
#include <SFML/Graphics.hpp>
#include <cmath>
#include <iostream>
#include <vector>
//#include <iterator>
#include <conio.h>
#include "map.h"
#include "dinamite.h"
#include "enity.h"
 
using namespace sf;
using namespace std;
 
class Player : public Enity
{
public:
    float dx, dy, speed;
    int dir;
    int bomb_count;
    int planted_bomb_count;
    vector<Dinamite> dinamite;
 
    Player(String F, float X, float Y, float W, float H) : Enity(F, X, Y, W, H)
    {
        dir = 0;
        speed = 0;
        bomb_count = 1;
        planted_bomb_count = 0;
        sprite.setTextureRect(IntRect(15, 16, w, h));
        dinamite.push_back(Dinamite("dinamite.png", 0, 0, 48, 48));
    }
 
    void update(float time)
    {
        switch (dir)
        {
            case 0: dx = speed; dy = 0;   break;
            case 1: dx = -speed; dy = 0;   break;
            case 2: dx = 0; dy = speed;   break;
            case 3: dx = 0; dy = -speed;   break;
        }
 
        x += dx*time;
        y += dy*time;
 
        speed = 0;
        sprite.setPosition(x, y);
        interactionWithMap();
    }
 
    void interactionWithMap()
    {
 
        for (int i = y / 48; i < (y + h) / 48; i++)
        for (int j = x / 48; j<(x + w) / 48; j++)
        {
            if (TileMap[i][j] == '0')
            {
                if (dy>0)
                    y = i * 48 - h;
                if (dy<0)
                    y = i * 48 + 48;
                if (dx>0)
                    x = j * 48 - w;
                if (dx < 0)
                    x = j * 48 + 48;
            }
 
            if (TileMap[i][j] == 'v')
            {
                TileMap[i][j] = ' ';
                incBombCount();
            }
        }
    }
 
    void incBombCount()
    {
        bomb_count++;
        dinamite.push_back(Dinamite("dinamite.png", 0, 0, 48, 48));
    }
 
};
 
int main()
{
 
    randomBlockGenerate();
 
    RenderWindow window(VideoMode(624, 624), "Explosion");
 
    Player p("robot_white.png", 49, 49, 38, 40);
 
    /*Texture dinamite_texture;
    dinamite_texture.loadFromFile("images/dinamite.png");
 
    Sprite dinamite_sprite;
    dinamite_sprite.setTexture(dinamite_texture);*/

 
    Image map_image;
    map_image.loadFromFile("images/map1.png");
 
    Texture map_texture;
    map_texture.loadFromImage(map_image);
 
    Sprite map_sprite;
    map_sprite.setTexture(map_texture);
 
    double x, y = 0;
 
    float CurrentFrame = 0;
    Clock clock;
 
    while (window.isOpen())
    {
        float time = clock.getElapsedTime().asMicroseconds();
        clock.restart();
        time = time / 800;
 
        if (p.planted_bomb_count != 0)
            for (int i = 0; i < p.bomb_count; i++)
            {
                if (p.dinamite[i].planted)
                    p.dinamite[i].work_time = p.dinamite[i].clock.getElapsedTime().asMilliseconds();
            }
 
        Event event;
        while (window.pollEvent(event))
        {
            if (event.type == Event::Closed)
                window.close();
            if (event.type == Event::KeyReleased)
            if ((event.key.code == Keyboard::Space) && (p.planted_bomb_count != p.bomb_count))
            {
                for (int i = 0; i < p.bomb_count; i++)
                {
                    if (p.dinamite[i].work_time == 0)
                    {
                        x = int(round(p.x / 48)) * 48;
                        y = int(round(p.y / 48)) * 48;
                        p.dinamite[i].sprite.setPosition(x, y);
                        p.dinamite[i].clock.restart();
                        p.dinamite[i].planted = true;
                        p.planted_bomb_count++;
                        break;
                    }
                }
            }
        }
 
 
        for (int i = 0; i < HEIGHT_MAP; i++)
            for (int j = 0; j < WIDTH_MAP; j++)
            {
                if (TileMap[i][j] == ' ') map_sprite.setTextureRect(IntRect(0, 0, 48, 48));
                if (TileMap[i][j] == '0') map_sprite.setTextureRect(IntRect(48, 0, 48, 48));
                if (TileMap[i][j] == 's') map_sprite.setTextureRect(IntRect(96, 0, 48, 48));
                if (TileMap[i][j] == 'v') map_sprite.setTextureRect(IntRect(144, 0, 48, 48));
                map_sprite.setPosition(j * 48, i * 48);
                window.draw(map_sprite);
            }
 
           
            if (Keyboard::isKeyPressed(Keyboard::Left))
            {
                p.dir = 1; p.speed = 0.1;
                CurrentFrame += 0.005*time;
                if (CurrentFrame > 4) CurrentFrame -= 4;
                p.sprite.setTextureRect(IntRect(15 + 59 * int(CurrentFrame), 74, 38, 40));
            }
 
            if (Keyboard::isKeyPressed(Keyboard::Right))
            {
                p.dir = 0; p.speed = 0.1;
                CurrentFrame += 0.005*time;
                if (CurrentFrame > 4) CurrentFrame -= 4;
                p.sprite.setTextureRect(IntRect(15 + 59 * int(CurrentFrame), 133, 38, 40));
            }
 
            if (Keyboard::isKeyPressed(Keyboard::Up))
            {
                p.dir = 3; p.speed = 0.1;
                CurrentFrame += 0.005*time;
                if (CurrentFrame > 4) CurrentFrame -= 4;
                p.sprite.setTextureRect(IntRect(15 + 59 * int(CurrentFrame), 192, 38, 40));
 
            }
 
            if (Keyboard::isKeyPressed(Keyboard::Down))
            {
                p.dir = 2; p.speed = 0.1;
                CurrentFrame += 0.005*time;
                if (CurrentFrame > 4) CurrentFrame -= 4;
                p.sprite.setTextureRect(IntRect(15 + 59 * int(CurrentFrame), 15, 38, 40));
 
            }
 
            for (int i = 0; i < p.bomb_count; i++)
            {
 
                if (p.dinamite[i].planted)
                {
                    if (p.dinamite[i].work_time < 3000)
                    {      
                        window.draw(p.dinamite[i].sprite);
                    }
                    else
                    {
                        p.dinamite[i].planted = false;
                        p.dinamite[i].work_time = 0;
                        p.dinamite[i].clock.restart();
                        p.planted_bomb_count--;
                    }
                }
            }
 
 
        p.update(time);
 
        window.draw(p.sprite);
        window.display();
    }
 
    return 0;
}
 
dinamite.h
 
#ifndef DINAMITE_H
#define DINAMITE_H
 
#include <SFML/Graphics.hpp>
#include "enity.h"
 
class Dinamite : public Enity
{
public:
    Clock clock;
    int work_time;
    bool planted;
    Dinamite(String F, float X, float Y, float W, float H) : Enity(F, X, Y, W, H)
    {
        sprite.setTextureRect(IntRect(0, 0, w, h));
        work_time = 0;
        planted = false;
    }
};
 
#endif DINAMITE_H
 
enity.h
 
#ifndef ENITY_H
#define ENITY_H
 
#include <SFML/Graphics.hpp>
 
using namespace sf;
 
class Enity
{
public:
    float x, y, w, h;
    Image image;
    Texture texture;
    Sprite sprite;
    String file;
 
    Enity(String F, float X, float Y, float W, float H)
    {
        file = F;
        x = X; y = Y;
        w = W; h = H;
        image.loadFromFile("images/" + file);
        texture.loadFromImage(image);
        sprite.setTexture(texture);
    }
};
 
#endif ENITY_H
 
 map.h not needed


DarkRoku12

  • Full Member
  • ***
  • Posts: 203
  • Lua coder.
    • View Profile
    • Email
Re: Problems with Sprite and Textures
« Reply #1 on: October 27, 2015, 12:25:51 am »
Ensure that the texture of the dinamite is valid on the moment that the sprite is displayed.
Ensure that the textureRect's coordinates are the dinamite and not a white part of the texture.
You can make a test without custom classes (only use SFML classes) and when all is working gently ( little by little) insert custom classes.

I mentaly cannot debug 2 sheets of code.


If you speak spanish i can provide help with the project using sfml private messages.
I would like a spanish/latin community...
Problems building for Android? Look here

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Problems with Sprite and Textures
« Reply #2 on: October 27, 2015, 02:47:23 am »
I will let others comment on other issues with the code... but your problem is because std::vector<T> copies elements when it resizes. So your enity is copied (including the texture) but the sprite still has the pointer to the old texture location which no longer exists after the copy.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

timrik

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Problems with Sprite and Textures
« Reply #3 on: October 27, 2015, 07:32:31 pm »
Thanks guys!
zsbzsb, could you plz show me how to correct my code and how to create copy constructor plz?

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Problems with Sprite and Textures
« Reply #4 on: October 27, 2015, 07:36:39 pm »

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: Problems with Sprite and Textures
« Reply #5 on: October 29, 2015, 03:21:25 pm »
also it is too hard to be minimal code ;)

 

anything