SFML library not importing for C++ - c++

I have tried to use SFML but it does not work
The error is to do with importing the library, i couldn't figure out to install it, i tried multiple tutorials, but all have failed so far.
#include <iostr>
#include <SFML/Network.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>
//using namespace sf;
int main() {
//window
sf::Window window(sf::Videomode(640, 480), "My first game", sf::Style::Titlebar | sf::Style:: Close);
sf::Event ev;
//Game loop
while (window.pollEvent(ev)) {
//Event polling
switch () {
}
}
return 0;
}

Related

SFML 2.0 - Window closes immediately after showing up

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

SDL Window won't pop up

My window won't pop up.
Console appears, and without opening window just says that "Application ended"
Here is my code:
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_image.h>
#include <windows.h>
using namespace std;
SDL_Window * okno;
SDL_Surface * ekran;
SDL_Event zdarzenie;
int frame = 0;
int main(int argc, char*args[])
{
SDL_Init(SDL_INIT_EVERYTHING);
return 0;
okno = SDL_CreateWindow("SDL_TEST", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, NULL);
ekran = SDL_GetWindowSurface(okno);
}
return 0; is your bug. After the return the program ends because main() ends. No lines in main are executed after the return. You certainly did not want to end before you called SDL_CreateWindow.
Can a function continue after a return statement?
Change the code to
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_image.h>
#include <windows.h>
using namespace std;
SDL_Window * okno;
SDL_Surface * ekran;
SDL_Event zdarzenie;
int frame = 0;
int main(int argc, char*args[])
{
SDL_Init(SDL_INIT_EVERYTHING);
okno = SDL_CreateWindow("SDL_TEST", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, NULL);
ekran = SDL_GetWindowSurface(okno);
}
In c++ return 0; can be omitted from main.
Can I omit return from main in C?
Also I would expect this code to issue a warning (about unreachable code) on many compilers. If it did not warn you may need to turn up the warning level of your compiler. If it did warn you need to pay more attention to the warnings..
You may also want to remove the global variables and instead make your variables local to main. Global variables are usually considered a bad practice.
Are global variables bad?
Also SDL_Init() can fail. You may want to check its return value and quit on failure logging the error.
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
return 1;
}
https://wiki.libsdl.org/SDL_Init

Window closes when adding a sprite

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.

Segmentation fault when drawing

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]);

Window refuses to render

I recently got started trying to use SFML. For some reason my simple program will not render the window. I've tried throwing everything into main to see if there was an error in my code that had to do with multiple files etc. but to no avail.
I'll launch my program and nothing will appear.
What's the problem?
//main.h
#ifndef MAIN_H
#define MAIN_H
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <fstream>
using namespace std;
using namespace sf;
class game
{
public:
void startLoop(int SCREEN_W, int SCREEN_H, string SCREEN_NAME);
void log(const string logging);
game()
{
QUIT = false;
pendingFile.open("Log.txt", ios::out);
pendingFile << "---Brain Bread Log---";
}
~game()
{
pendingFile.close();
}
private:
bool QUIT;
ofstream pendingFile;
};
#endif
//main.cpp
#include "main.h"
void game::log(const string logging)
{
pendingFile << logging;
}
void game::startLoop(int SCREEN_W, int SCREEN_H, string SCREEN_NAME)
{
Window Game(VideoMode(SCREEN_W, SCREEN_H, 32), SCREEN_NAME);
while(QUIT == false)
{
Game.Display();
}
}
int main(int argc, char* argv[])
{
game gameObj;
gameObj.startLoop(800, 600, "Brain Bread");
return 0;
}
I tried your code and it behaves exactly as I expect it to - that is to say that an iconless window with a black body pops up and it doesn't respond to events. Is that what you're getting? If not, you might need to rebuild SFML.
You might want to try introducing event-handling so that your startLoop looks more like this:
void game::startLoop(int SCREEN_W, int SCREEN_H, string SCREEN_NAME)
{
// Init stuff
while (Game.IsOpened())
{
sf::Event newEvent;
while (Game.GetEvent(newEvent))
{
// Process event
}
// Do graphics stuff
Game.Display();
}
}