Hello, I have strange problem while trying to declare Texture object inside class.
What I have:
class main_menu {
public:
void initialization() {
t_main_menu_background.loadFromFile("external resources/artassets/menu/backgrounds/main_menu_background.jpg");
main_menu_background.setTexture(t_main_menu_background);
main_menu_background.setPosition(0, 0);
main_menu_background = system_global_environment.set_scale_of_sprite_based_on_window_size(main_menu_background);
menu_loading_screen.f_welcome_to_loading.loadFromFile("external resources/artassets/menu/sprites/loading_screen_text_eng.png");
f_main_logo.loadFromFile("external resources/artassets/menu/sprites/logo_with_icon_eng.png");
menu_loading_screen.t_welcome_to_loading.loadFromImage(menu_loading_screen.f_welcome_to_loading);
t_main_logo.loadFromImage(f_main_logo);
menu_loading_screen.welcome_to.setTexture(menu_loading_screen.t_welcome_to_loading);
menu_loading_screen.loading.setTexture(menu_loading_screen.t_welcome_to_loading);
main_logo.setTexture(t_main_logo);
menu_loading_screen.welcome_to.setTextureRect(IntRect(0, 0, 500, 125));
menu_loading_screen.loading.setTextureRect(IntRect(0, 125, 500, 250));
//menu_loading_screen.loading = system_global_environment.set_scale_of_sprite_based_on_window_size(menu_loading_screen.loading);
//menu_loading_screen.welcome_to = system_global_environment.set_scale_of_sprite_based_on_window_size(menu_loading_screen.welcome_to);
main_logo = system_global_environment.set_scale_of_sprite_based_on_window_size(main_logo);
menu_loading_screen.phase = 1;
menu_loading_screen.transparency = 0;
CurrentMenu = 1;
PreviousMenu = 0;
}
void draw_menu() {
switch (CurrentMenu) {
case 1:
switch (menu_loading_screen.phase) {
case 1:
menu_loading_screen.transparency = menu_loading_screen.transparency + 1;
break;
}
system_global_environment.get_p_window()->draw(main_menu_background);
system_global_environment.get_p_window()->draw(main_logo);
system_global_environment.get_p_window()->draw(menu_loading_screen.loading);
system_global_environment.get_p_window()->draw(menu_loading_screen.welcome_to);
break;
case 2:
break;
}
}
private:
int CurrentMenu,PreviousMenu;
Image f_main_logo;
Texture t_main_logo;
Texture t_main_menu_background;
Sprite main_menu_background;
Sprite main_logo;
struct loading_screen {
Image f_welcome_to_loading;
Texture t_welcome_to_loading;
Sprite welcome_to;
Sprite loading;
int phase;
int transparency;
} menu_loading_screen;
} system_main_menu;
That supposed to be a menu class that declaring images, textures and sprites for menu and draws it, and i have "Unhandled exception at 0x779522E2 (ntdll.dll) in Project137.exe: 0xC0000005: Access violation writing location 0x00000004." on the line where system_main_menu object is being declared.
Problem solves only if I will remove that 3 lines with declaring textures: Texture t_main_logo, Texture t_main_menu_background and Texture t_welcome_to_loading and usage of them.
For example this code won't work:
class main_menu {
public:
private:
Texture t_main_logo;
Texture t_main_menu_background;
Texture t_welcome_to_loading;
} system_main_menu;
It works if I declare texture outside class (in main() for example) but still doesnt work if I declare them in any other class.
Strange thing is that code worked fine until I started to use cout to get some variables values, then that error appeared, and when I used ctrl+z to undo code I wrote that problem didn't disappear. I'm not sure if this problem connected to SFML itself (probably my software problem) but I googled that problem I have and found out they all have very different reasons. Sorry if provided not enough info about problem, feel free to ask for more.
I use SFML 2.4.2 x32 in VS 2017, precompiled build by Nightlybuilds (
here).