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

Author Topic: Problem trying setup and draw window using multiple files  (Read 2870 times)

0 Members and 1 Guest are viewing this topic.

Sam42

  • Newbie
  • *
  • Posts: 16
    • View Profile
Problem trying setup and draw window using multiple files
« on: October 04, 2010, 07:07:59 pm »
I'm a beginner programmer looking to try and create a simple game engine in SFML.

I've successfully managed to compile a program that has a movable sprite on a screen while putting everything inside main, but when I want to split it into seperate source files I can't get it to work.

Currently I have three files main.cpp, myengine.h, myengine.cpp

main.cpp
Code: [Select]

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include "myengine.h"

int main()
{
    // Create window and initialise game
    MyEngine game;

    game.Init(800,600);

    // Start the game loop
    while (game.Running())
        {
            game.Events();
            game.Draw();
        }

    return EXIT_SUCCESS;

}


myengine.h
Code: [Select]

#ifndef MYENGINE_H
#define MYENGINE_H

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>

using namespace std;

class MyEngine
{
    private:

    public:

    void Init(int height, int width);
    void Events();
    void Input();
    void Update();
    void Draw();
    void Cleanup();

    sf::RenderWindow& GetWindow();
    sf::Event Event;
    sf::RenderWindow App;

    bool m_running;

bool Running()
{
    return m_running;
}
void Quit()
{
    m_running = false;
}

};

#endif


myengine.cpp
Code: [Select]

#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include "myengine.h"

void MyEngine::Init(int height, int width)
{
    sf::RenderWindow App(sf::VideoMode(height, width, 32), "MyEngine");
    App.SetActive();
    App.SetFramerateLimit(60);
    m_running = true;
}

void MyEngine::Events()
{
    while (App.GetEvent(Event))
    {
        // Close window : exit
        if (Event.Type == sf::Event::Closed)
        Quit();

    }
}

void MyEngine::Draw()
{
    App.Clear();
    App.Display();
}


The object is to just draw a black window to screen. I'm aware there are probably a plethora of problems with this code, but advice on which direction to look in would be greatly appreciated!

PeterWelzien

  • Newbie
  • *
  • Posts: 38
    • View Profile
Re: Problem trying setup and draw window using multiple file
« Reply #1 on: October 04, 2010, 07:42:29 pm »
Quote from: "Sam42"

void MyEngine::Init(int height, int width)
{
    sf::RenderWindow App(sf::VideoMode(height, width, 32), "MyEngine");
    App.SetActive();
    App.SetFramerateLimit(60);
    m_running = true;
}

You don't mention what's not working, but I guess the above code is the problem. The first line in MyEngine::Init() creates a new variable App that's local to that function. Instead of creating a new variable, use MyEngine::App instead:
Code: [Select]

void MyEngine::Init(int height, int width)
{
    App.Create(sf::VideoMode(height, width, 32), "MyEngine");
    App.SetActive();
    App.SetFramerateLimit(60);
    m_running = true;
}
/Peter Welzien

Sam42

  • Newbie
  • *
  • Posts: 16
    • View Profile
Problem trying setup and draw window using multiple files
« Reply #2 on: October 04, 2010, 08:10:24 pm »
Sorry, I should have supplied more information. The program would compile and run, but would spawn a window that promptly disappeared.

What you've suggested has fixed it perfectly, cheers!