Window refuses to render - c++

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();
}
}

Related

Sprite not showing sfml

I want to display my player in the window but my player sprite is not showing in the window. I am new to c++. I want to learn classes, inheritence, composition, etc in this way.I have 3 files Player.cpp, Game.cpp and main.cpp. I am using main.cpp to call Game.cpp using a fuction called Run().
Got nothing to try.
Player.cpp
#include "Player.hpp"
#include "string.h"
#include <iostream>
void Player::initPlayer()
{
const char* playerTexturePath = "/Users/don/Desktop/sfmlgames/game1/img/MCmid.png";
if(!mPlayerTexture.loadFromFile(playerTexturePath))
{
std::cout << "mPlayerTexturePath not found!!" << std::endl;
}
mPlayerSprite.setTexture(mPlayerTexture);
mPlayerSprite.setPosition(100.f, 100.f);
mPlayerSprite.setScale(1.f, 1.f);
}
void Player::draw_player(sf::RenderWindow &win)
{
win.draw(mPlayerSprite);
}
Game.cpp
#include "Game.hpp"
#include <iostream>
Player player1;
//Constructor: Create a window and Player
Game::Game() : mWindow(sf::VideoMode(640, 480), "Game")
{
//Frame rate 60
mWindow.setFramerateLimit(60);
player1.initPlayer();
}
//Game loop
void Game::Run()
{
while(mWindow.isOpen())
{
render();
events();
update();
}
}
void Game::events()
{
sf::Event event;
while (mWindow.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
mWindow.close();
}
}
}
void Game::update()
{
}
void Game::render()
{
mWindow.clear(sf::Color::Red);
player1.draw_player(mWindow);
mWindow.display();
}
main.cpp
#include "Game.hpp"
int main()
{
Game game;
game.Run();
}
I don't think I will need to give code to hpp files.
The problem was with image I dont know why. I used another image and it worked fine.

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

LNK1561 entry point must be defined - SDL

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.

Displaying window with sfml

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;
}

C++ Set Console Text Color in Windows from Static Method

Is it possible to use a static method within a class to set the text color of a console for a Windows C++ application? My code looks like this:
Display.h:
class Display {
public:
Display();
static void setTextColor(int color);
};
Display.cpp:
#include <windows.h>
#include "Display.h"
void Display::setTextColor(int color) {
HANDLE cHandle;
cHandle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(cHandle, color);
}
main.cpp:
#include <iostream>
#include "Display.h"
int main(int argc, char *argv[]) {
std::cout << "HI"; //Outputs default color
Display::setTextColor(9);
std::cout << "HI"; //Still default color
return 0;
}
Can anybody please explain why this static method will not update the text color? This excat code will work if placed inline.
Thank you for your time.