Hi... So I created a small game by writing all my code in the main function. It worked. But to make it more object oriented, I defined a function in a class for the menu screen to initialize the screen elements and a function in the same class to draw them on the screen. And then I called these functions from the main function. No errors and successful execution, but it is not drawing anything on the screen. I am extremely sorry for posting the whole code, I am not able to pinpoint the problem. Please bear with me. Thank you.
MENU.H :
#include "headers.h"
class Menu
{
public:
// Initialize Menu Screen
int InitMenu(sf::RenderWindow &window, sf::Texture &menutex, sf::Texture &playtex, sf::Texture &quittex, sf::Sprite &menu, sf::Sprite &play, sf::Sprite &quit, sf::FloatRect &menubounds, sf::FloatRect &playbounds, sf::FloatRect &quitbounds, sf::VideoMode &resolution);
// Display Menu Screen
int Show(sf::RenderWindow &window, sf::Sprite &menu, sf::Sprite &play, sf::Sprite &quit, sf::FloatRect &playbounds, sf::FloatRect &quitbounds);
};
MENU.CPP :
#include "menu.h"
int Menu::InitMenu(sf::RenderWindow &window, sf::Texture &menutex, sf::Texture &playtex, sf::Texture &quittex, sf::Sprite &menu, sf::Sprite &play, sf::Sprite &quit, sf::FloatRect &menubounds, sf::FloatRect &playbounds, sf::FloatRect &quitbounds, sf::VideoMode &resolution)
{
// Loading Menu Background texture and setting sprite
if(!menutex.loadFromFile("images/menu.png"))
return EXIT_FAILURE;
menu.setTexture(menutex);
// Loading Play Button texture and setting sprite
if(!playtex.loadFromFile("images/play.png"))
return EXIT_FAILURE;
play.setTexture(playtex);
// Loading quit Button texture and setting sprite
if(!quittex.loadFromFile("images/quit.png"))
return EXIT_FAILURE;
quit.setTexture(quittex);
// Menu Background orientation
menu.setOrigin(menubounds.width/2, menubounds.height/2);
menu.setScale(resolution.getDesktopMode().width/menubounds.width, resolution.getDesktopMode().height/menubounds.height);
menu.setPosition(resolution.getDesktopMode().width/2, resolution.getDesktopMode().height/2);
// Play Button orientation
play.setOrigin(playbounds.width/2, playbounds.height/2);
play.setPosition(resolution.getDesktopMode().width/2 - playbounds.width, resolution.getDesktopMode().height/2 - playbounds.height);
// Exit Button orientation
quit.setOrigin(quitbounds.width/2, quitbounds.height/2);
quit.setPosition(resolution.getDesktopMode().width/2 + quitbounds.width, resolution.getDesktopMode().height/2 - quitbounds.height);
return 0;
}
int Menu::Show(sf::RenderWindow &window, sf::Sprite &menu, sf::Sprite &play, sf::Sprite &quit)
{
// Draw the sprites and display
window.draw(menu);
window.draw(play);
window.draw(quit);
return 0;
}
MAIN.CPP :
#include "oneheader.h"
int main()
{
// Creating the window
sf::VideoMode resolution;
sf::RenderWindow Game(resolution.getDesktopMode(), "Game", sf::Style::Fullscreen);
// Initializing textures and sprites
sf::Texture menutex, playtex, quittex;
sf::Sprite menu, play, quit;
// Getting bounding box of each entity
sf::FloatRect menubounds = menu.getGlobalBounds();
sf::FloatRect playbounds = play.getGlobalBounds();
sf::FloatRect quitbounds = quit.getGlobalBounds();
Menu mainMenu;
mainMenu.InitMenu(Game, menutex, playtex, quittex, menu, play, quit, menubounds, playbounds, quitbounds, resolution);
while(Game.isOpen())
{
sf::Event event;
while(Game.pollEvent(event))
{
if(event.type == sf::Event::Closed)
Game.close();
if(event.key.code == sf::Keyboard::Escape)
Game.close();
}
Game.clear();
mainMenu.Show(Game, menu, play, quit, playbounds, quitbounds);
Game.display();
}
return 0;
}
Another weird problem I encountered is the exit button and the play button are being drawn on exactly the same spot despite being given different positions and the menu background isn't being drawn. A black screen is being displayed. Also, if I give a wrong file name to the exit button, instead of exiting, the menu background and the play button are being displayed without any of the transformations applied to them. I don't know why it's doing that. Please help me on this. It's a sincere and humble request from my side. I shall really really really appreciate it. Thank you.