Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: sf::Window in my own class trow Errors  (Read 2486 times)

0 Members and 1 Guest are viewing this topic.

adamas

  • Newbie
  • *
  • Posts: 2
    • View Profile
sf::Window in my own class trow Errors
« on: November 28, 2012, 09:43:39 pm »
First: please dont comment on my bad english. Okay  :P

Hello guys,

im relatively new to SFML. I learn a little bit C++ for myself and found this engine. It is really awesome, THX to the creator!

At the moment i work on a small project, just to understand 3d-programming and i got some cubes and a window and some movements to work. I try to put all my stuff in different classes and now im trying it with sf::Window. I got it into a simple class and can use it in the main() without problems. But when i put it in a global namespace i get a crash:

Unbehandelte Ausnahme bei 0x77c422b2 in grafik_test.exe: 0xC0000005: Zugriffsverletzung beim Schreiben an Position 0x00000004. (Must be Access Violation in English). Maybe anyone have an idea, why it works local but not global?

window.h
/////////////////////////////////////
#ifndef WINDOW_H
#define WINDOW_H
/////////////////////////////////////

#include "structs.h"
#include <SFML/OpenGL.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>

class cWindow{
private:
        sColor color;
        unsigned int maxFPS;
        unsigned int width;
        unsigned int height;
        sf::Window window;

public:
        // Konstruktor / Destruktor
        cWindow(void);
        ~cWindow(void);

        // Setter / Getter
        void setColor(float R, float G, float B);
        void setColor(sColor inputColors);
        void setMaxFPS(int fps);
        void setHeight(int newHeight);
        void setWidth(int newWidth);
       
        sColor getColor(void);
        int getMaxFPS(void);
        int getWidth(void);
        int getHeight(void);
        sf::Window* Ref(void);

        // Methoden
        int cWindow::init(void);
        int cWindow::display(void);
        int cWindow::clear(void);
};

#endif
 

window.cpp
#include "window.h"
#include <iostream>

// Konstruktor / Destruktor
cWindow::cWindow(void){
        this->setColor(0.0, 0.0, 0.0);
        this->setMaxFPS(60);
        this->setHeight(450);
        this->setWidth(900);
}
cWindow::~cWindow(void){

}

// Setter / Getter
void cWindow::setColor(float R, float G, float B){
        this->color.R = R;
        this->color.G = G;
        this->color.B = B;
}
void cWindow::setColor(sColor inputColors){
        this->color.R = inputColors.R;
        this->color.G = inputColors.G;
        this->color.B = inputColors.B;
}
void cWindow::setMaxFPS(int fps){
        this->maxFPS = fps;
}
void cWindow::setHeight(int newHeight){
        this->height = newHeight;
}
void cWindow::setWidth(int newWidth){
        this->width = newWidth;
}

sColor cWindow::getColor(void){
        return this->color;
}
int cWindow::getMaxFPS(void){
        return this->maxFPS;
}
int cWindow::getHeight(void){
        return this->height;
}
int cWindow::getWidth(void){
        return this->width;
}
sf::Window* cWindow::Ref(void){
        return &this->window;
}

// Methoden
int cWindow::init(void){
        this->window.setFramerateLimit(this->getMaxFPS());
        this->window.create(sf::VideoMode(this->getWidth(), this->getHeight(), 32), "Grafik Test", sf::Style::Close);

        glEnable(GL_DEPTH_TEST);
        //glClearColor(0.3, 0.4, 0.7, 0.0);
        glClearColor(this->getColor().R, this->getColor().G, this->getColor().B, 0.0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        return 0;
}

int cWindow::display(void){
        this->window.display();
        return 0;
}
int cWindow::clear(void){
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        return 0;
}
 

main.cpp
#include "main.h"

using namespace std;

int main(){
        Global::window.init();
        Global::log.write("It works!", 1);

// GAME_LOOP
        while(Engine::Events::running){
                // changes
                // events

                //while(window->getWindow()->pollEvent(Engine::Events::Event) && Engine::Events::running){
                //      Engine::Events::check();
                //}
        }
    return 1;
}
 

globals.h
/////////////////////////////////////
#ifndef GLOBALS_H
#define GLOBALS_H
/////////////////////////////////////

#include "log.h"
#include "camera.h"
#include "window.h"

namespace Global{
        cLog log;
        cCam camera;
        cWindow window;
};
#endif
 
« Last Edit: November 30, 2012, 01:22:42 am by adamas »

cf

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: sf::Window in my own class trow Errors
« Reply #1 on: November 28, 2012, 10:31:58 pm »
/////////////////////////////////////
#ifndef GLOBALS_H
#define GLOBALS_H
/////////////////////////////////////

#include "log.h"
#include "camera.h"
#include "window.h"

namespace Global{
        cLog log;
        cCam camera;
        cWindow window;
};
#endif
 

It seems like the global cWindow -- and it's internal sf::Window -- is created before SFML's global GlResource-mutex, so it crashes when trying to access it during the window's creation.

The solution would be to not create a global window like you're currently doing (since there's always a chance it'll be initialized before SFML).

Also, I believe creating it inside a header file like you're doing here would create a new window for each #include (and not one global shared one), so you should look into fixing that as well (either a totally new solution, hopefully fixing the previous problem as well, or declaring it as extern in the header and defining it in a .cpp-file).

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: sf::Window in my own class trow Errors
« Reply #2 on: November 28, 2012, 10:38:26 pm »
Welcome to the forum! :)

Quote
I learn a little bit C++ for myself and found this engine.
SFML is 'just' a multimedia library and by far not an engine/framework. ;)
I'm not trying to take away your motivation, but keep in mind that programming a game (specially a 3D game) isn't trivial at all and needs way more than 'a little bit C++' knowledge. At best you get a good book about C++ and read it front to back. :D

Well you've already identified your problem: global instances. (Side note: if you're using VS10 it probably runs 'fine' in release mode.)
The solution to it is, don't use global instances, in fact don't use any globals at all, but wrap things into a class, think about a better approach to accessing your window and pass things around with references. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

adamas

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: <SOLVED> sf::Window in my own class trow Errors
« Reply #3 on: November 29, 2012, 10:57:03 pm »
Hello,

thanks alot for the advice with the globals. I know, you should not use them, but the logfile for example i want to have global. But i changed some of my stuff and use some pointers and your advice with the .cpp files to initialize them. Now all works fine with the window!  :D

Quote
SFML is 'just' a multimedia library and by far not an engine/framework. ;)
Sorry, my mistake.. but its great anyway :)

Quote
I'm not trying to take away your motivation, but keep in mind that programming a game (specially a 3D game) isn't trivial at all and needs way more than 'a little bit C++' knowledge. At best you get a good book about C++ and read it front to back. :D
I know, a good game needs definitly more than some knowledge. I just want to try and have some fun with openGl and the SFML 2.0. Some cubes jumping around and a working camera... The result should not be a game. Just a window with some rendered stuff in it^^ I just evaluate the code by time. So... no you don't take my motivation away :)

Quote
The solution to it is, don't use global instances, in fact don't use any globals at all, but wrap things into a class, think about a better approach to accessing your window and pass things around with references. ;)
Thanks to you too, it works :)

but now i have a strange crash when closing the window/application. I dont use an ATI-Graphicscard. And it looks like it depends on a pointer, but dont know which one..