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

Author Topic: Help with classes  (Read 1648 times)

0 Members and 1 Guest are viewing this topic.

Calneon

  • Newbie
  • *
  • Posts: 3
    • View Profile
Help with classes
« on: October 14, 2010, 05:49:46 pm »
I'm trying to get my head around classes. I want to create the sprite in the tutorial but implement it using a class. The tutorial has some examples on how to use sprites as classes but I'm not experienced enough with C++ to get them to work. Here is what I have so far..

Code: [Select]
// SFMLTest.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <SFML/Graphics.hpp>
#include "Mario.h"

int main()
{
    // Create the main rendering window
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");

Mario Mario(App);

    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        // Get elapsed time
        float ElapsedTime = App.GetFrameTime();

        // Clear screen
        App.Clear();

    // Draw Mario
    Mario.draw(App);

        // Display window contents on screen
        App.Display();
    }

    return EXIT_SUCCESS;
}


Code: [Select]
// Mario.h

#pragma once
#include <SFML/Graphics.hpp>

class Mario
{
public:

static bool Init(const std::string& ImageFile)
    {
        return Image.LoadFromFile("sprite.png");
    }

Mario(sf::RenderWindow &App);

~Mario(void);

void draw(sf::RenderWindow &App);

private :

    static sf::Image Image; // shared by every instance

    sf::Sprite Sprite; // one per instance

sf::RenderWindow &myWindow;
};


Code: [Select]
// Mario.cpp

#include "StdAfx.h"
#include "Mario.h"

Mario::Mario(sf::RenderWindow &App) : myWindow (App)
{
Sprite.SetImage(Image); // every sprite uses the same unique image
}

Mario::~Mario(void)
{
}

void Mario::draw(sf::RenderWindow &App)
{
App.Draw(Sprite);
}


I managed to iron out all the errors apart from this:

Quote
Error   1   error LNK2001: unresolved external symbol "private: static class sf::Image Mario::Image" (?Image@Mario@@0V0sf@@A)   Mario.obj   SFMLTest


I assume it's something to do with this code, because I don't really know what it means (I just copied it from the tutorial):

Code: [Select]
static bool Init(const std::string& ImageFile)
    {
        return Image.LoadFromFile("sprite.png");
    }


I've been trying to figure this out for a few hours before posting here, please help me :).

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Help with classes
« Reply #1 on: October 15, 2010, 11:14:43 am »
The problem is that you've declared a class variable ("static sf::Image Image") but never initialized it. In your CPP file, use something like the following:

Code: [Select]
sf::Image Mario::Image;

That initializes the class variable with its default constructor. The reason for this is that class variables exist per class, not object, and you have to initialize them at some place. That works different for member variables, since they're always initialized when the object is constructed (either automatically by the default contructor, or by the initializer list of your own constructors).

Also keep in mind that you only need to load the image once. You're currently doing it every time a Mario object is created.