SFML Sprite isn't showing on the window even though it isn't giving me any errors - sfml

I'm working with a ref class and using SFML for the graphics, so at the beginning the problem I encounter was facing non-managed members in a managed class, I fixed that problem but now, I'm unable to see the sprite in the window i'm working
#include <SFML/Graphics.hpp>
#pragma make_public(sf::Sprite)
using namespace sf;
using namespace System::Collections::Generic;
namespace GameModel {
public ref class Grass {
public:
Sprite* Sprite;
Texture* Texture;
int Size;
Grass(int size) {
Size = size;
Texture = new sf::Texture();
Sprite = new sf::Sprite(*Texture);
Texture->loadFromFile("Imagenes/Grass.png");
//Image is 500x500px
Sprite->setTexture(*Texture,true);
Sprite->setScale(float(Size / 500), float(Size / 500));
}
void Draw(sf::RenderTarget& rt) {
rt.draw(*Sprite);
}
void Move(float x, float y) {
Sprite->setPosition(x, y);
}
};
public ref class Garden {
public:
List <Grass^>^ Grass;
Garden() {
Grass = gcnew List<GameModel::Grass^>();
}
};
}
using namespace GameModel;
int main(){
sf::RenderWindow window(sf::VideoMode(800, 800), "SFML works!");
Grass^ T = gcnew Grass(25);
T->Move(25, 0);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
T->Draw(window);
window.display();
}
return 0;
}

Your render window should be drawing your sprites, not the opposite.
This should work :
window.draw(T);

I fixed the problem, turns out the function setScale wasn't really working, so I changed the member:
int Size;// I changed this
float Size;// for this
after that the function work just fine and the project work as it should

Related

C++ and SFML, no instance of the overloaded function "sf::RenderWindow.draw()" matches the argument list

I am currently trying to make a simple pong game with SFML in C++. I consistently get the error in the main file.
#include <iostream>
#include "Ball.h"
Ball b;
int main()
{
// create the window
sf::RenderWindow window(sf::VideoMode(1200, 600), "My window");
// run the program as long as the window is open
while (window.isOpen())
{
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (window.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
}
// clear the window with black color
window.clear(sf::Color::Black);
// draw everything here...
window.draw(b);
// end the current frame
window.display();
}
return 0;
}
This is the ball.h file:
#pragma once
#include <iostream>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
class Ball
{
public:
double x, y;
int Radius;
void Render(int Rad, sf::Color BallColour) {
sf::CircleShape shape(Rad);
// set the shape color to green
shape.setFillColor(BallColour);
}
};
I am unsure as to why the error occurs. Any help would be appreciated.
You had not call function of render, you just wrote window.draw(b), and programm dont know what do you mean. In Ball.h you should write:
#pragma once
#include <iostream>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
class Ball
{
public:
double x, y;
int Radius;
void Render(int Rad, RenderWindow& window) {
sf::CircleShape shape(Rad);
// set the shape color to green
shape.setFillColor(sf::Color::Green);
shape.setPosition(600, 300);//SETTING POSITION OF THE BALL
window.draw(shape);//if you know what is reference on variable, you will understand what is it
}
};
And in your main.cpp you shhould call your function Render:
#include <iostream>
#include "Ball.h"
int main()
{
// create the window
sf::RenderWindow window(sf::VideoMode(1200, 600), "My window");
Ball b; //I advice you to create object of class in main function
// run the program as long as the window is open
while (window.isOpen())
{
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (window.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
}
// clear the window with black color
window.clear(sf::Color::Black);
b.Render(10, window);// give the function reference on window
// end the current frame
window.display();
}
return 0;
}
sf::RenderWindow::draw requires an implementation of sf::Drawable. You can either pass sf::CircleShape (it's a sf::Drawable) directly or implement sf::Drawable and delegate the call.
class Ball : public sf::Drawable {
public:
double x, y;
int Radius;
void draw(sf::RenderTarget& target, sf::RenderStates states) const override {
sf::CircleShape shape(Radius);
shape.setFillColor(BallColour);
shape.draw(target, states);
}
};

Falling sand simulation collision detection C++ and SFML

I could really use some help. I am trying to make a falling sand simulation and have some basic code down, but I can't figure how to do collision detection. Here is my code so far:
//SFML Include
#include <SFML/Graphics.hpp>
//Include Vectors
#include <vector>
//Main Loop
int main()
{
//Window Init
sf::RenderWindow window(sf::VideoMode(720, 480, 32), "Yip Yip Physics", sf::Style::Default);
//Global Envoirmental Variables
static float gravity = 0.25;
static float frict = 3;
//Particle Structures
struct Particle
{
//Shape
sf::RectangleShape rect;
//Cell Position
sf::Vector2f cell_pos;
//Vel and frict
float slide_vel = 3;
float grv_vel = 0;
//Init Particle (size, color and origin)
void init()
{
rect.setSize(sf::Vector2f(3, 3));
rect.setFillColor(sf::Color::Green);
rect.setOrigin(rect.getSize());
}
};
//Particle Setup
std::vector<Particle> particles;
//Window Loop
while (window.isOpen())
{
//Update
if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left))
{
//Make and Position Particle
Particle part;
part.rect.setPosition(sf::Vector2f(sf::Mouse::getPosition(window)));
//Initalize the Particle and add to vector
part.init();
particles.push_back(part);
}
//Pixel Update
for (auto& p : particles)
{
//Draw in window
window.draw(p.rect);
}
//Display
window.display();
//Event Variable
sf::Event event;
//Window Event System
while (window.pollEvent(event))
{
//Close Window
if (event.type == sf::Event::Closed)
{
window.close();
}
}
//Window Clear
window.clear(sf::Color::Black);
}
//Return 0
return 0;
}
The code works in making the particles, putting them in a vector and drawing them to the screen, but I don't know where to go for collision. I don't know where to look and would really appreciate help on it or direction on where to find it. I have heard a bit about cellular automata but have no idea how to do it or if it's the best option.

How to draw an object onto sfml window when a function is called?

I have a character sprite moving across the screen and when I press 'a' I want the program to draw the sword sprite onto the screen and make the sword go away when I am not pressing 'a'. Currently I have an attack function that sets a showweapon boolean as true, and a if statement that is supposed to draw the weapon onto the screen but nothing happens. Is there a better way to do this?
main.cpp
#include <iostream>
#include <SFML/Graphics.hpp>
#include "Character.h"
#include "Projectile.h"
#include "Weapon.h"
using namespace std;
bool scroll = false;
Character player("/Users/danielrailic/Desktop/Xcode /NewGame/ExternalLibs/Player.png");
Weapon woodsword("/Users/danielrailic/Desktop/Xcode /NewGame/ExternalLibs/WoodSword.png");
bool showweapon;
int main() {
// insert code here...
int windowWidth = 5000;
int windowHeight = 5000;
sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight ), "Awesome Game" );
sf::Texture dungeon;
dungeon.loadFromFile("/Users/danielrailic/Desktop/Xcode /NewGame/ExternalLibs/DungeonBack.png");
sf::Sprite backround;
backround.setTexture(dungeon);
while (window.isOpen()){
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (window.pollEvent(event)){
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
player.left();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
player.right();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
player.forward();
}
if (sf:: Keyboard::isKeyPressed(sf::Keyboard::Down)){
player.backward();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::LShift))
{
player.Highspeed();
}
else{
player.Lowspeed();
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
woodsword.attack();
}
window.clear(sf::Color(255, 255, 255));
window.draw(backround);
if(showweapon == true){
window.draw(woodsword.getSprite());
window.display();
cout << "Hello";
}
window.draw(player.getSprite());
window.display();
window.setFramerateLimit(70);
}
}
I am also not receiving the "hello" message
weapon.h
class Weapon : Character{
bool showweapon;
public:
sf::Texture texture;
//Constructor
Weapon(string wep)
: Character(wep){
texture.loadFromFile(wep);
showweapon = false;
}
sf::Sprite getSprite() {
sf::Sprite sprite;
sprite.setTexture(texture);
sprite.setTextureRect(sf::IntRect(0, 0, 100, 100));
sprite.setPosition(x_pos, y_pos);
return sprite;
}
//Methods
void attack();
};
void Weapon::attack(){
showweapon = true;
}
If you need to see anything else let me know. Thanks for any help!
I changed the if statement to : if (woodsword.showweapon == true) and I also made a sheath method in the weapon class that sets showweapon false so the sword goes away when I don't press 'a'. Thanks for everyones help.

SFML update function not moving sprite

The current update function should be moving the sprite to the left 10 pixels every time it's called, but the sprite (cup1Sprite) is static and not moving. I tried using sprite.rotate(); and the sprite rotated every frame (which was expected), so something is wrong with the way I've written the update function for updating the sprite position. Can anyone suggest what's going wrong?
Code:
DrawCups.h
class DrawCups
{
public:
DrawCups(sf::RenderWindow& window);
~DrawCups();
void update();
void drawCup1();
private:
sf::RenderWindow& _window;
//Cup1
sf::Texture _cup1; // the texture which will contain our pixel data
sf::Sprite _cup1Sprite; // the sprite which will actually draw it
};
Update function in DrawCups.cpp
void DrawCups::update()
{
sf::Vector2f pos = this->_cup1Sprite.getPosition();
pos.x+=10;
this->_cup1Sprite.setPosition(pos);
}
main.cpp
int main()
{
sf::RenderWindow window(sf::VideoMode(1366, 768), "Sorting Algorithm Visualisation: SFML");
window.setFramerateLimit(60);
DrawCups drawToWindow(window);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
drawToWindow.update();
window.clear(sf::Color::White);
drawToWindow.drawBench();
drawToWindow.drawCup1();
window.display();
}
return 0;
}

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.