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

Author Topic: Drawing sprites from class  (Read 2103 times)

0 Members and 1 Guest are viewing this topic.

simson1952

  • Newbie
  • *
  • Posts: 3
    • View Profile
Drawing sprites from class
« on: March 03, 2012, 08:22:28 pm »
I use codeblock and I tried many things to only show on the screen a sprite from a class. If I only use the main it works but I plan a bigger project so I need to be able to draw those sprites.

What I copy-paste here is the last try I did to make it work. The error is : undefined reference to Snake::Snake()' wich is my class

Theres nothing in the map.cpp for the moment so its not important.

main
Code: [Select]
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include "snake.h"
#include "map.h"
#include <list>
#include <iostream>
#include <vector>



int main()
{


 int h = 45;
 int l = 45;

sf::RenderWindow App(sf::VideoMode(20*h, 20*l, 32), "Snake O Tron");
App.SetFramerateLimit(60);



Snake test;

std::vector<sf::Sprite> serpent;


std::vector<std::vector<int> > map(h, std::vector<int> (l,0));

 // 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();
        }




App.Clear();


for(int i = 0; i<serpent.size(); i++)
{
    App.Draw(serpent[i]);
}





        App.Display();
    }

    return EXIT_SUCCESS;
}


snake.cpp
Code: [Select]
#include "snake.h"

using namespace std;
using namespace sf;

Snake::Snake()
{
    tete.Create(20, 20, Color(200,0,0));
    head.SetImage()(tete);
    head.setPosition(200.f, 200.f);
    affiche.push_back(head);
}



vector<Sprite> Snake::get_aff() const
{
    return affiche;
}



snake.h
Code: [Select]
#ifndef SNAKE_H_INCLUDED
#define SNAKE_H_INCLUDED

#include <SFML/Graphics.hpp>
#include <list>
#include <iostream>
#include <vector>


class Snake
{
    public:

    Snake();//constructeur
    std::vector<sf::Sprite>get_aff() const;
    std::vector<sf::Sprite> affiche;

    protected:



    private:

    sf::Image tete;
    sf::Sprite head;


};


#endif // SNAKE_H_INCLUDED

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
Drawing sprites from class
« Reply #1 on: March 03, 2012, 09:04:41 pm »
Did you try to completely rebuild your project?

In the following line you should get an error because of the brackets
Quote
head.SetImage()(tete);


If you don't get this error then this code is not compiled. If it is not compiled then it can of course not be found by the linker, hence the undefined reference error.
TGUI: C++ SFML GUI

simson1952

  • Newbie
  • *
  • Posts: 3
    • View Profile
Drawing sprites from class
« Reply #2 on: March 03, 2012, 09:55:49 pm »
I did both things but none of them worked.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Drawing sprites from class
« Reply #3 on: March 03, 2012, 10:09:46 pm »
Make sure the file really is compiled. Did you add it to your project in the configuration you currently use (release/debug)?

If nothing helps, create a new project and add the files again.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

simson1952

  • Newbie
  • *
  • Posts: 3
    • View Profile
Drawing sprites from class
« Reply #4 on: March 05, 2012, 03:15:40 am »
I really don't know how, but after reinstalling codeblock a couple of times and playing with settings for a couples of hours it finally works.

Thanks for helping.

 

anything