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
Related
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;
}
I got a LNK1561 entry point must be defined error i tried somethings my self as stsyem settings to console and it still doesn't work. Here is my code for every class the SDl.h is from the SDL.h donwload page.
main.cpp:
#include <iostream>
#include "MainGame.h"
int main(int argc, char** argv) {
std::cout << "Enter any ket to quit...";
int a;
std::cin >> a;
return 0;
}
MainGame.cpp:
#include "MainGame.h"
MainGame::MainGame()
{
_window = nullptr;
_screenHeight = 1028;
_screenWidth = 768;
}
MainGame::~MainGame()
{
}
void MainGame::run() {
InitSystems();
}
void MainGame::InitSystems() {
SDL_Init(SDL_INIT_EVERYTHING);
_window = SDL_CreateWindow("title", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, 1028, 768, SDL_WINDOW_OPENGL);
}
MainGame.h:
#pragma once
#include <SDL/SDL.h>
class MainGame
{
public:
MainGame();
~MainGame();
void run();
void InitSystems();
private:
SDL_Window* _window;
int _screenWidth;
int _screenHeight;
};
Allt his code is to open an Windowed frame on your computer en open an console with the text Press Any ket to quit... If i remove the SDL.h include and the SDL code it all works if i put the include back and not the SDL code it gives the error again.
Have you tried using MainGame in your main function? Chances that the compiler assumes that it is never used and you do not get the #include <SDL/SDL.h> directive to work. Also, consider changing #include <SDL/SDL.h> to #include "SDL/SDL.h".
Also you might consider using SDL_SetMainReady for the cases that you are not using SDL_Main as the entry point.
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]);
I looked for some tutorials for game development, I found one where I'm supposed to create a pong clone, but I can't even create the window, I get the following:
"Unhandled exception at 0xEEFFEE01 in Pang.exe: 0xC0000005: Access violation reading location 0xEEFFEE01."
I'm using SFML 1.6 and Visual Studio 2013.
Here is my code.
Game.cpp
#include "stdafx.h"
#include "Game.h"
void Game::Start(void)
{
if (_gameState != Uninitialized)
return;
_mainWindow.Create(sf::VideoMode(1024, 768, 32), "Pang!");
_gameState = Game::Playing;
while (!IsExiting())
{
GameLoop();
}
_mainWindow.Close();
}
bool Game::IsExiting()
{
if (_gameState == Game::Exiting)
return true;
else
return false;
}
void Game::GameLoop()
{
sf::Event currentEvent;
while (_mainWindow.GetEvent(currentEvent))
{
switch (_gameState)
{
case Game::Playing:
{
_mainWindow.Clear(sf::Color(255, 0, 0));
_mainWindow.Display();
if (currentEvent.Type == sf::Event::Closed)
{
_gameState = Game::Exiting;
}
break;
}
}
}
}
Game::GameState Game::_gameState = Uninitialized;
sf::RenderWindow Game::_mainWindow;
Game.h
#pragma once
#include "SFML/Window.hpp"
#include "SFML/Graphics.hpp"
class Game
{
public:
static void Start();
private:
static bool IsExiting();
static void GameLoop();
enum GameState {
Uninitialized, ShowingSplash, Paused,
ShowingMenu, Playing, Exiting
};
static GameState _gameState;
static sf::RenderWindow _mainWindow;
};
Pang.cpp
#include "stdafx.h"
#include "Game.h"
int _tmain(int argc, _TCHAR* argv[])
{
Game::Start();
return 0;
}
I know there's some code that has nothing to do with my problem, the error occurs when it reaches this line in Game.cpp
_mainWindow.Display();
I'm new on this so any help, any good tutorials or starting point to start learning will be good.
Some general SFML advice: Update to SFML 2.0, especially if you are using VS2013. Here are the official SFML tutorials http://www.sfml-dev.org/tutorials/2.0/
It looks like you just copied your code from here http://en.sfml-dev.org/forums/index.php?topic=10855.0 if this is true you should have realized that the code did not work, if not then the person who copied the code and used it in their tutorial should have noticed, and the fact they didn't is worrying about the state of the rest of their tutorials
Okay so the issue is you can't have a static RenderWindow, and with that your entire game class should not be static, it just isn't good programming practice. What you need to do is remove the static identifier from every declaration in your Game.h header file. For instance:
This
static void Start();
Needs to look like this
void Start();
Next, your Pang.cpp should look like this
//#include "stdafx.h" you don't need this precompiled header
#include "Game.h"
int main(int argc, char* argv[])
{
Game myGame;
myGame.Start();
return 0;
}
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();
}
}