My window closes when I draw a sprite.
It's definitely the drawing part of it because when I keep it out of
my code, it works fine, except it doesn't draw my sprite of course.
Also I get this error when running: Segmentation fault (core dumped)
I don't know what that means :/ .
And here is my code:
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>
#include <string>
#include <iostream>
#include <vector>
using namespace std;
//create vars
sf::Color bgColour(20, 175, 215);
vector<sf::Sprite> tiles;
void CreateTile(string Texture, int x, int y)
{
sf::Vector2f Pos(x, y);
sf::Texture Ftexture;
Ftexture.loadFromFile(Texture);
sf::Sprite Tile;
Tile.setTexture(Ftexture);
Tile.setPosition(Pos);
tiles.push_back(Tile);
}
int main()
{
//create window
sf::RenderWindow window(sf::VideoMode(800, 600), "-\\\\-(Game)-//-");
CreateTile("Recources/grass.png", 40, 40);
//main loop
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
window.clear(bgColour);
window.draw(tiles[1]);
window.display();
}
return 0;
}
Thanks!
You are trying to acces an element on the vector that do not exist.
change this
window.draw(tiles[1]);
to this
window.draw(tiles[0]);
Related
Window immediately closes after showing up, even though it should wait for close event.
main.h:
#include <SFML/Graphics.hpp>
#include <iostream>
#include "window.h"
int main()
{
window::window mainwindow;
mainwindow.createwindow(800, 600);
while(mainwindow.window.pollEvent(mainwindow.events)){
if (mainwindow.events.type == sf::Event::Closed){
mainwindow.window.close();
}
}
return 0;
}
window.h:
#include <SFML/Graphics.hpp>
#include <iostream>
namespace window{
class window{
public:
sf::Window window;
sf::Event events;
void createwindow(int resx, int resy){
window.create(sf::VideoMode(resx, resy), "MainWindow", sf::Style::Default);
}
};
}
The code is based on this SFML tutorial : https://www.sfml-dev.org/tutorials/2.5/window-window.php
I am using SMFL 2-2.5.1-23.83 with IDE being code::blocks 20.03.
Thanks for all help
this is main.cpp
#include <SFML/Graphics.hpp>
#include <iostream>
#include<windows.h>
#include <string>
#include "class.cpp"
using namespace sf;
int main()
{
HWND hwnd = GetConsoleWindow();
ShowWindow(hwnd, 0);
RenderWindow window(sf::VideoMode(800, 450), "file", sf::Style::Close | sf::Style::Titlebar);
//objects
house ho;
//end objects
//inits
ho.init(10,10,10,window);
//end inits
while (window.isOpen())
{
window.clear(sf::Color::Black); //clear screan with black
sf::Event evnt;
while (window.pollEvent(evnt))
{
if (evnt.type == evnt.Closed)
{
window.close();
}
}
//draw
//end draw
window.display(); // display everything
}
return 0;
}
this is class.cpp
#include <SFML/Graphics.hpp>
class house {
private:
float rx, ry, rpop;
public:
void init(float x, float y, float pop,sf::RenderWindow window2) {
bool inittrue = false;
rx, ry, rpop = x, y, pop;
if (pop > 0) {
inittrue = true;
}
if (inittrue == true) {
sf::RectangleShape rectangle(sf::Vector2f(50.f, 40.f));
rectangle.setPosition(sf::Vector2f(x, y));
window2.draw(rectangle);
}
}
};
the error prints this
Severity Code Description Project File Line Suppression State
Error C2280 'sf::RenderWindow::RenderWindow(const sf::RenderWindow &)': attempting to reference a deleted function sfml C:\Users\Luka\Desktop\progrraming\sfml\sfml\sfml.cpp 19
thank you in advance
I'm new to SFML, and have been watching a tutorial that puts everything in a single main function. When making my own program, I tried to split it into multiple functions, but it isn't working properly, can anyone explain why this works:
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
sf::RenderWindow window(sf::VideoMode(512, 512), "window", sf::Style::Resize | sf::Style::Close);
while (window.isOpen())
{
sf::Event evnt;
while (window.pollEvent(evnt))
{
if (evnt.type == evnt.Closed)
{
window.close();
}
}
window.clear();
window.display();
}
return 0;
}
and this doesn't:
#include <SFML/Graphics.hpp>
#include <iostream>
sf::RenderWindow window;
void setup()
{
sf::RenderWindow window(sf::VideoMode(512, 512), "window", sf::Style::Resize | sf::Style::Close);
}
int main()
{
setup();
while (window.isOpen())
{
sf::Event evnt;
while (window.pollEvent(evnt))
{
if (evnt.type == evnt.Closed)
{
window.close();
}
}
window.clear();
window.display();
}
return 0;
}
They will both compile and run, but in the former, the window will stay open, and in the latter, it won't.
The window variable that you've declared inside setup() is shadowing the global window. object. Try the following:
void setup()
{
window.create(sf::VideoMode(512, 512), "window", sf::Style::Resize | sf::Style::Close);
}
Im am trying to make a clean and organized way of making sprites. The problem is that the position of the sprites are being read as zero and they are just being drawn in the top left corner.
Sprite.h
#ifndef Sprite_h
#define Sprite_h
#endif /* Sprite_h */
using namespace std;
bool show = true;
class Sprite{
public:
int Spritex;
int Spritey;
string name;
sf::Texture texture;
Sprite(string image, int x, int y){
x = Spritex;
y = Spritey;
texture.loadFromFile(image);
}
sf::Sprite getSprite() {
sf::Sprite sprite;
sprite.setTexture(texture);
sprite.setPosition(Spritex, Spritey);
return sprite;
}
void changeimage(string image);
};
void Sprite:: changeimage(string image){
texture.loadFromFile(image);
}
main.cpp
#include <iostream>
#include <SFML/Graphics.hpp>
#include "Character.h"
#include "Projectile.h"
#include "Sprite.h"
//Use std
using namespace std;
//Boolean to determine if screen will scroll
bool scroll = false;
//player that is part of Character class and computer that is part of Sprite class
Character player("/Users/danielrailic/Desktop/Xcode /NewGame/ExternalLibs/Sprites/Player.png");
Sprite computer("/Users/danielrailic/Desktop/Xcode /NewGame/ExternalLibs/Sprites/CompSprite.png", 1200, 100);
Sprite battery("/Users/danielrailic/Desktop/Xcode /NewGame/ExternalLibs/Sprites/battery4.png", 0, 0);
//boolean for whether to show weapon or not
bool showweapon;
//main loop
int main() {
int windowWidth = 5000;//width of window
int windowHeight = 5000;//height of window
sf::RenderWindow window(sf::VideoMode(windowWidth, windowHeight ), "Awesome Game" );//Make the window
//Setting up the dungeon back-round
sf::Texture dungeon;
dungeon.loadFromFile("/Users/danielrailic/Desktop/Xcode /NewGame/ExternalLibs/Sprites/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();
}
//Movement
if (moveChar == true){
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 player intersects with comp sprite, pick up comp sprite
if (player.getSprite().getGlobalBounds().intersects(computer.getSprite().getGlobalBounds())){
show = false;
player.hascomp = true;
}
//draw and window stuff
window.clear(sf::Color(255, 255, 255));
window.draw(backround);
if (show == true){
window.draw(computer.getSprite());
}
if (show == false){
window.draw(battery.getSprite());
}
window.draw(player.getSprite());
window.display();
window.setFramerateLimit(70);
}
}
If you have a question I will do my best to answer. Everything works except the spritex and spritey are being read as 0 for some reason. Thanks for any help.
You are writing to variables x and y here, which you never read from:
Sprite(string image, int x, int y){
x = Spritex;
y = Spritey;
texture.loadFromFile(image);
}
I assume you flipped the assignments, and should write
Sprite(string image, int x, int y){
Spritex = x;
Spritey = y;
}
or
Sprite(string image, int x, int y) : Spritex(x), Spritey(y) {
texture.loadFromFile(image);
}
If you turn up your warning levels, you will be warned about things like this, and also the non-initialized member you still have (name)
My window crashes when I try to use my function I created,
I use it to create a sprite on screen but for some reason it crashes.
I get the error:
Segmentation fault (core dumped)
and here's my code:
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <string>
#include <unistd.h>
#include <iostream>
#include <vector>
using namespace std;
vector<sf::Sprite*> Tiles;
void createTile (string TextureIN, int x, int y) {
sf::Vector2f Pos(x, y);
sf::Texture Texture;
Texture.loadFromFile(TextureIN);
sf::Sprite *Tile;
Tile->setTexture(Texture);
Tile->setPosition(Pos);
Tiles.push_back(Tile);
}
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
createTile("Recources/grass.png", 50, 50);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color::Blue);
for (int i; i < Tiles.size(); i++) {
window.draw(*Tiles[i]);
}
window.display();
}
return 0;
}
I had a working version before but my computer crashed and I forgot to
back it up >.<
Anyways, I hope you can help me with this issue.
You are creating a pointer without proper memory allocation.
So instead of
sf::Sprite *Tile;
use
sf::Sprite *Tile = new sf::Sprite;
Be sure to have a look on How to avoid memory leaks when using a vector of pointers to dynamically allocated objects in C++? as well.