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

Author Topic: [SOLVED]Issues with drawing a sprite  (Read 879 times)

0 Members and 1 Guest are viewing this topic.

Kiterosma

  • Newbie
  • *
  • Posts: 2
    • View Profile
[SOLVED]Issues with drawing a sprite
« on: May 25, 2013, 09:13:51 am »
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
Code: [Select]
vector<Drawable*> drawables;
RenderWindow * window;
And menu.cpp is provided here (there's some commented code from trying to debug):
Code: [Select]
#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:
Code: [Select]
#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!  :)
« Last Edit: May 29, 2013, 10:06:39 pm by Kiterosma »

Kiterosma

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Issues with drawing a sprite
« Reply #1 on: May 29, 2013, 05:37:54 am »
Evidently I did something very silly (among other things). I was initializing the Menu object before I loaded the necessary texture from the file. So that's that.