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