First: please dont comment on my bad english. Okay
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