SFML white background underneath texture when moving - c++

I am maing a small game using SFML, anyway, my issue is that when rendering the sprite, and moving with float values. The sprite has a white background that 1 pixel one whichever side is moving gets shown.
Here is my Spritesheet class:
Spritesheet::Spritesheet(std::string t) {
this->texture.loadFromFile(t);
this->sprite.setTexture(this->texture);
}
sf::Sprite Spritesheet::getSprite(int x, int y, int width, int height) {
sf::Sprite spt;
spt.setTexture(this->texture);
spt.setTextureRect(sf::IntRect(x, y, width, height));
return spt;
}
void Spritesheet::setSprite(std::string t) {
this->texture.loadFromFile(t);
this->sprite.setTexture(this->texture);
}
And then the player class which is the class that draws the sprite:
Player::Player(int x, int y) {
// Some other code
this->spritesheet.setSprite("./res/img/tiles.png");
this->sprite = this->spritesheet.getSprite(48, 48, 16, 16);
this->sprite.setPosition(x, y);
this->sprite.scale(4, 4);
}
// Further down
void Player::render(RenderWindow& g) {
g.draw(this->sprite);
}
I have also tried using the sprite function setColor but that changes the texture color aswell.

I've had a problem similar to this before.
To fix it I had added a clock before the display so that I could be sure that the display was finished before I could display it again.
sf::Clock clock;
while (clock.getElapsedTime().asMilliseconds() < 100);
clock.restart();

maybe its FPS, i had a similar issue when i did not set a max fps, try window.setFramerateLimit(60); maybe it would fix it

Related

Setting up the card position on the window screen with class function

I don't want to hardcode the position for the card to be in the middle of the screen and we did a project like this without class. So though would be easy to just put what I did to make the card to be in the center but no matter what I did, the card stays at the top left corner.
I even notice at times if I put the rectSize or something the rectangle proportions changes and look like a square when maximizing the screen.
What am I doing wrong?
This is my background cpp file:
#include "background.h"
background::background() : background(450, 750)
{
}
background::background(float x, float y)
{
sf::RenderWindow window;
sf::RectangleShape rectangle;
sf::RectangleShape::setSize({x, y});
// sf::Vector2f center;
//
// sf::RectangleShape::setPosition({});
}
void setPostioning (sf::RenderWindow &window, sf::RectangleShape &rectangle, float x, float y)
{
sf::Vector2f rectSize ={x,y};
rectangle.setSize(rectSize);
sf::Vector2f center;
rectangle.setPosition({
center.x = window.getSize().x/2 - rectSize.x/2,
center.y = window.getSize().y/2 - rectSize.y/2
});
}
This is my header file of what I have done:
#include <SFML/Graphics.hpp>
class background : public sf::RectangleShape
{
public:
background();
background(float x, float y);
void setPostioning(sf::RenderWindow &window, sf::RectangleShape &rectangle, float x, float y);
};
And now this is my main main file
int main()
{
//set up of the window
sf::VideoMode videoMode(1280,1024,32);
sf::RenderWindow window(videoMode, "SFML Tutorial");//window will display name
window.setFramerateLimit(15);//frame rate
background b;
rank r;
Card Joker;
while(window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
//when window is running they can close it with the close button
if (event.type == sf::Event::Closed)
{
window.close();
}
//this if statement will make our card stay in the same ratio no matter what
if (event.type == sf::Event::Resized)
{
// update the view to the new size of the window and keep the center
window.setView(sf::View(window.getView().getCenter(),
sf::Vector2f((float) event.size.width, (float) event.size.height)));
}
}
//invoking and set up to be drawn and display on the window when running
window.clear(sf::Color::Black);
window.draw(Joker);
window.draw(r);
window.display();
}
So yes unsure why the position is not being set up or being taken from the window size or maybe it has to do with the rectSize that I did and being misread. I also think it has to do with the x and y as I set them up already with 450 nd 750.
It is tricky to help you without full code, cause I don't know how exactly did you want to use setPostioning.
After a small workaround, It finally appeared in the center of the screen.
Feel free to comment, if my example still doesn't satisfy your needs.
In the header file I added a reference to sf::RenderWindow, to use it in setPostioning.
Updated background.h:
class background : public sf::RectangleShape
{
public:
background(sf::RenderWindow &window);
background(sf::RenderWindow &window, float x, float y);
void setPostioning(float x, float y);
private:
// Added a refence to original window to have possibility use it in setPositioning
sf::RenderWindow& m_window;
};
In .cpp file I removed some redundant refs to sf::RectangleShape (cause you already inherited from it), and to sf::RenderWindod (cause a referene to it is stored inside class).
Updated background.cpp:
background::background(sf::RenderWindow &window) : background(window, 450, 750)
{
}
background::background(sf::RenderWindow &window, float x, float y) : m_window(window)
{
// sf::RectangleShape rectangle;
sf::RectangleShape::setSize({ x, y });
}
void background::setPostioning(float x, float y)
{
sf::Vector2f rectSize = { x,y };
// don't use rectangle from param, because you already inherited from sf::RectangleShape
//rectangle.setSize(rectSize);
setSize(rectSize);
sf::Vector2f center;
// again, don't use rectangle from param, because you already inherited from sf::RectangleShape
//rectangle.setPosition({
setPosition({
center.x = m_window.getSize().x / 2 - rectSize.x / 2,
center.y = m_window.getSize().y / 2 - rectSize.y / 2
});
}
In main function I called setPostioning before the event loop and added window.draw(b); to render your background.
Updated main function:
int main()
{
//set up of the window
sf::VideoMode videoMode(1280, 1024, 32);
sf::RenderWindow window(videoMode, "SFML Tutorial");//window will display name
window.setFramerateLimit(15);//frame rate
background b(window);
// use setPostioning
b.setPostioning(200.f, 200.f);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
//when window is running they can close it with the close button
if (event.type == sf::Event::Closed)
{
window.close();
}
//this if statement will make our card stay in the same ratio no matter what
if (event.type == sf::Event::Resized)
{
// update the view to the new size of the window and keep the center
window.setView(sf::View(window.getView().getCenter(),
sf::Vector2f((float)event.size.width, (float)event.size.height)));
}
}
//invoking and set up to be drawn and display on the window when running
window.clear(sf::Color::Black);
window.draw(b);
window.display();
}
}

Positioning a sprite at (0, 0) places the sprite off screen by a few pixels

When I position a sprite at 0, 0 (top left corner of the screen) it doesn't show. It's off screen by a few pixels. Here is a sample of my code.
Sprite s;
Texture t;
t.loadFromFile("Tiles.png");
t.setSmooth(false);
s.setTexture(t);
s.setTextureRect(IntRect(0, 0, 16, 16));
s.setPosition(0, 0);
window.draw(s);
window.display();
If I add
Style::None
to my RenderWindow, then the sprite is positioned properly at the top left of the screen.
Is there a way to position sprites ignoring the windows title bar? Or another way to fix this issue? The only way I can seem to get the sprite positioned correctly is if I guess how many pixels off it is and manually add an offset to the sprites position, but this is a very bad way of doing it and I can't get it positioned perfectly. If I set the position to (11, 64), the sprite is at about the top left of the screen.
Edit: Here is a full example piece of code I used to recreate the issue:
#include <SFML/Graphics.hpp>
using namespace sf;
const int WIDTH = 1000;
const int HEIGHT = 1000;
int main()
{
RenderWindow window(VideoMode(WIDTH, HEIGHT), "Window", Style::Titlebar | Style::Close);
RectangleShape square(Vector2f(100.f, 100.f));
square.setFillColor(Color::Red);
square.setPosition(0, 0);
while (window.isOpen())
{
Event windowEvent;
while (window.pollEvent(windowEvent))
{
if (windowEvent.type == Event::Closed)
{
window.close();
}
}
window.clear(Color::White);
window.draw(square);
window.display();
}
return 0;
}
When I run it, this is the result I get. https://i.imgur.com/bIOnb4L.png
Since posting this issue, I've realized that the problem is that when the title bar is enabled, it takes up space on my window, (0, 0) is behind the title bar. I still don't know how to fix it, though.

SFML Sprites not displaying at all

I have a method which should take a spritesheet which is 64x128 and turn it into 8x8 sprites.
after each texture is found from the stored Image, it is added to a sprite, and that sprite is added to a sprite array which is being called in my main method.
I have tested displaying a static sprite before, and my code worked (I displayed 1 8x8 sprite successfully)
However, when trying to display any of the 128 sprites in my list of sprites, now none are being rendered.
I believe the logic error may be in my MakeSprite method, but I'm unsure, and I can't see where the issue is.
[EDIT]: it seems that every sprite I call is returning the sprite at the very end of the spritesheet (sprite 128)
[Edit 2]: the texture (tex) of the previous sprite is overwritten by the next sprite to be spawned.
Below is a full verifiable working example of my code:
main.cpp
#include "Main.h"
int main() {
srand(time(NULL));
RenderWindow window(VideoMode(1280, 720), "Conduit");
MakeCircle(10, 100, 100, Color::White);
MakeCircle(30, 10, 100, Color::Cyan);
MakeCircle(100, 200, 100, Color::Magenta);
MakeCircle(100, 400, 100, Color::Cyan);
if (!LoadSpritesheet())
{
return 1;
}
while (window.isOpen()) {
Event event;
while (window.pollEvent(event)) {
if (event.type == Event::Closed)
window.close();
else if (event.key.code == Keyboard::Num1)
{
DrawRandomSprite(window);
}
}
window.clear();
DrawCircles(window);
int c = 0;
for (int i = 0; i < 128; i++)
{
Spritesheet.at(i).setPosition(c, c);
window.draw(Spritesheet.at(i));
c += 8;
}
window.display();
}
}
void DrawRandomSprite(RenderWindow &window)
{
//DEBUG METHOD: draws a random sprite for testing.
int sprite = rand() % 128 + 1;
Spritesheet.at(sprite).setPosition(rand() % 128 + 1, rand() % 128 + 1);
window.draw(Spritesheet.at(sprite));
}
void MakeCircle(float radius, float xpos, float ypos, Color color)
{
//makes a circle then adds it to the circle vector.
CircleShape shape;
shape.setRadius(radius);
shape.setPosition(xpos, ypos);
shape.setFillColor(color);
Circles.push_back(shape);
}
void DrawCircles(RenderWindow &window)
{
//Renders the circles in the circles vector.
for (int i = 0; i < Circles.size(); i++)
{
window.draw(Circles.at(i));
}
}
int LoadSpritesheet()
{
//make sure spritesheet exists, then loads it into an image.
Texture sheet;
if (!sheet.loadFromFile("Sprites/A.png"))
{
return 0;
}
else
{
sheetIMG = sheet.copyToImage();
SetMask();
MakeSprite(8, 4);
return 1;
}
}
void SetMask()
{
//creates a mask.
sheetIMG.createMaskFromColor(sf::Color(151, 56, 14, 0), 100);
}
void MakeSprite(int dimension, int scale)
{
//seperates the spritesheet into a list of 8x8 modular sprites.
int c = 0, r = 0;
do
{
for (int i = 0; i <= (sheetIMG.getSize().x * sheetIMG.getSize().y) / 64; i++)
{
if (r == 64)
break;
if (!tex.loadFromImage(sheetIMG, IntRect(c, r, dimension, dimension)))
break;
else
{
Sprite spr;
spr.setTexture(tex);
spr.setScale(scale, scale);
Spritesheet.push_back(spr);
c += dimension;
if (c == sheetIMG.getSize().x) { c = 0; r+=8; };
}
}
} while (r < sheetIMG.getSize().y);
}
Main.h
#pragma once
#include <SFML/Graphics.hpp>
#include <vector>
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
//standard and SFML namespaces;
using namespace std;
using namespace sf;
//===============================VARIBLES===========================
//the main spritesheet.
Texture tex;
Image sheetIMG;
//array to hold circles.
vector<CircleShape> Circles;
//array to hold sprites.
vector<Sprite> Spritesheet;
//===============================PROTOTYPES=============================
void DrawCircles(RenderWindow &window);
void MakeCircle(float radius, float xpos, float ypos, Color color);
void MakeSprite(int dimension, int scale);
void SetMask();
int LoadSpritesheet();
void DrawRandomSprite(RenderWindow &window);
There's two major issues with your code:
You're using global variables. While this might be okay with some libraries, with SFML this is not a good thing at all.
You're clearing the window after calling DrawRandomSprite. Therefore your sprite will never show up.
All your sprites have the same texture: tex. And as tex is a single global variable, when your loop finishes, it's set to the last texture. So all sprites will draw the last texture. You need to get rid of the globals and have one texture per sprite (or at least one per sprite that you want to have a different texture).

I'm making a Animal Crossing clone, but I'm having problems rendering the Player

Recently, I decided to make an Animal Crossing clone in
C++ and SFML 2.1. But I'm having some issues. The Player won't show up when commanded to be rendered. The program will compile and run just fine but the player just won't show up.
Here's my code:
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;
using namespace sf;
RenderWindow window(VideoMode(700, 500), "Animal Crossing: Old oak");
View view(FloatRect(1000, 1000, 300, 200));
class Villager{
public:
int x, y, w, h;
Sprite pl;
string loadDir;
Villager(int x, int y, int w, int h, Color c, string loadDir){
this->x = x;
this->y = y;
this->w = w;
this->h = h;
Image image;
image.loadFromFile(loadDir);
image.createMaskFromColor(Color::Magenta);
Texture tex;
tex.loadFromImage(image);
pl.setTexture(tex);
}
}
};
int main(){
Villager villager(1100, 1000, 100, 100, Color::Blue, "player.png");
view.zoom(5);
Image grasstexloader;
grasstexloader.loadFromFile("grass.png");
Texture grasstex;
grasstex.loadFromImage(grasstexloader);
Sprite grass;
grass.setTexture(grasstex);
while(window.isOpen()){
Event event;
while(window.pollEvent(event)){
if(event.type == Event::Closed)
window.close();
if(Keyboard::isKeyPressed(Keyboard::Up))
villager.moveUp();
if(Keyboard::isKeyPressed(Keyboard::Down))
villager.moveDown();
if(Keyboard::isKeyPressed(Keyboard::Left))
villager.moveLeft();
if(Keyboard::isKeyPressed(Keyboard::Right))
villager.moveRight();
if(Keyboard::isKeyPressed(Keyboard::Escape))
window.close();
}
window.setView(view);
window.draw(grass);
window.draw(villager.pl);
window.display();
window.clear();
}
}
I've been staring at this code for an hour now. But I just can't find an error!
Please help!
Edit: I solved the problem with the sprite not being visible, but the sprite is just white instead of the appropriate colors. It proboably has something to do with how I load the file. Please post any suggestions you have on how to fix this new problem!
Your sprite is rendered white because in your Villager constructor, you're giving a local Texture variable to setTexture, which then gets destructed at the end of the constructor scope.

How can I make this function return a sf::Texture? SFML2.0

void TileSheetManager::setTileSheet(const string textureName)
{
texture.loadFromFile(textureName);
}
sf::Sprite TileSheetManager::getTile(int left, int top, int width, int height)
{
sf::IntRect subRect;
subRect.left = left * 32;
subRect.top = top * 32;
subRect.width = width;
subRect.height = height;
sf::Sprite sprite(texture, subRect);
return sprite;
}
I need getTile() to return a sf::Texture yet I have no idea how I could do it.
I'm using SFML2.0 by the way.
According to the documentation here and here you should be able to
sf::Image fullImage = texture.copyToImage();
sf::Image croppedImage(width, height);
croppedImage.copy(fullImage, 0, 0, subRect);
sf::Texture returnTexture();
returnTexture.LoadFromImage(croppedImage);
The method getTile that you have at the moment does what it's supposed to. You have a tile management class that loads an entire spritesheet, and hands out cropped areas as sprites. Don't change this method just to solve this problem, your TileSheetManager class is structured well.
If you want to convert one of your sprites to a texture, you could try the following.
// Get a sprite from the Tile Manager.
sf::Sprite mySprite = tileSheetMgr.getTile(1,2,3,4);
// Create a new texture for the sprite returned.
sf::Texture spriteTexture;
// Generate the texture from the sprite's image.
spriteTexture.loadFromImage(*mySprite.getImage());