I'm very new to SFML and game programming in general, and I'm only just beginning to make a structure for a basic game. I made a class called Application which runs the main loop for the game. Then, I made an abstract class called Element which is responsible for updating game objects and drawing to the screen. The Menu object is a child of Element, which is what I'm using to represent the main menu. It's member variables, which it inherits from Element, are
vector<Drawable*> drawables;
RenderWindow * window;
And menu.cpp is provided here (there's some commented code from trying to debug):
#include "menu.h"
#include <iostream>
Menu::Menu(RenderWindow * w)
{
window = w;
buildMainMenu();
}
void Menu::draw()
{/*
for(int i=0; i<drawables.size(); i++)
{
window->draw(*drawables[i]);
cout<<"drawing"<<endl;
}*/
window->draw(*drawables[0]);
}
void Menu::update()
{
Event event;
while (window->pollEvent(event))
{
switch(event.type)
{
case Event::Closed: window->close();
break;
}
}
}
void Menu::buildMainMenu()
{
Drawable* background = new Sprite(preLoad::MainMenu);
drawables.push_back(background);
}
This brings me to my conundrum. When I use my Menu object to draw the sprite to the screen, nothing is outputted to the screen. I checked that all of the functions were being called; in fact, the update function works perfectly (after all, the code was directly from the beginning of the SFML tutorial). Here's the kicker: I attempted to replicate the exact same code within the main loop in application.cpp, and the sprite was drawn to the screen, no problem. I even put the sprite creation in a function to check if the sprite pointer going out of scope was having an adverse effect. Also, I used "elem->window" instead of just "window" for drawing the sprite to see if that had an effect, but it still worked just fine. "elem->draw()" is commented out for debugging. If you are wondering, "preLoad::MainMenu" is the static instance of the texture for the menu.
application.cpp:
#include "application.h"
#include <iostream>
#include <vector>
using namespace preLoad;
Application::Application()
{
window = new RenderWindow(VideoMode(800, 600), "The Game",Style::Titlebar|Style::Close);
elem = new Menu(window);
}
Application::~Application()
{
delete elem;
delete window;
}
bool Application::initialize()
{
bool result = true;
WorkingDirectory = "file path";
result = result && MainMenu.loadFromFile(WorkingDirectory+"MainMenu.png");
return true;
}
void func(vector<Drawable*> & d)
{
Drawable* background = new Sprite(preLoad::MainMenu);
d.push_back(background);
}
void Application::run()
{
vector<Drawable*> drawables;
func(drawables);
while(window->isOpen())
{
window->clear();
if(elem!=NULL)
{
elem->update();
//elem->draw();
}
elem->window->draw(*drawables[0]);
window->display();
}
}
I've picked apart the logic of this code over and over, but I simply can't figure out what is going wrong in the Menu class. Just as a reiteration, I'm a complete noob at this type of programming, so any and all advice is welcome! I am likely to completely change this initial structure I've created, but for the sake of my sanity I need to put this to rest. Thanks in advance for your time!