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

Author Topic: DisplayingSprites using classes(tutorial help)  (Read 5732 times)

0 Members and 1 Guest are viewing this topic.

Syntactic Fructose

  • Jr. Member
  • **
  • Posts: 80
  • Overflowing stacks and eating snacks
    • View Profile
DisplayingSprites using classes(tutorial help)
« on: April 12, 2012, 09:41:37 pm »
I've been learning sfml lately and im trying to create just really simple games and applications to help learn. I saw on the sprite tutorial that puting the image and sprite into a class is much better programming and habit s. Here is the code:

UPDATED:
Code: [Select]
#ifndef TANK_H
#define TANK_H
#include <SFML/Graphics.hpp>

class Tank
{
    public:
        Tank(const Tank& Copy);
        bool LoadFile(const std::string ImageFile);
    protected:
    private:
        static sf::Image Image;
        sf::Sprite Tank1;
};

#endif // TANK_H


Code: [Select]
#include "Tank.h"
#include <iostream>
#include <SFML/Graphics.hpp>
Tank::Tank(const Tank& Copy):
Image(Copy.Image),
Tank1(Copy.Tank1)
{
    Tank1.SetImage(Image);
}

bool Tank::LoadFile(const std::string ImageFile)
{
    if(!Image.LoadFromFile(ImageFile))
    {
        return 1;
    }
    return 0;
}



But how would i go about loading an image and sprite into the class? I want to make a Tank object that holds the image i could display in the main loop, here is my guess:
main.cpp
Code: [Select]
#include <iostream>
#include "Tank.h"
#include <SFML/Graphics.hpp>


int main()
{
   sf::RenderWindow Window(sf::VideoMode(800,600,32), "TANKWARS");
   Tank Playerone(/* ??? */); //im still confused on how to declare this!
   if(!Playerone.LoadFile("Tank.tga"))
   {
       return EXIT_FAILURE;
   }
}

« Last Edit: April 13, 2012, 04:17:41 am by need4sleep »

Digital

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: DisplayingSprites using classes(tutorial help)
« Reply #1 on: April 13, 2012, 01:06:22 am »
I'm assuming you're following along to the 1.6 sprite tutorial and a few things caught my eye.

1. In main, I believe you're calling your Tank's copy constructor and it's passing in itself. You should just be calling the default constructor. Does it compile currently?
2. Make sure the sf::Image inside of the Tank class is static to avoid creating a new sf::Image each time you instantiate a Tank. You normally only want one sf::Image that is shared by every Tank instance.
3. To load an image, your Tank class needs to know what to load and have a way to load it. So one option is to have a public init method in your Tank class where you pass in the path of the file to load as a string, and within that method, you call the static sf::Image's file load method. Then the first time you create a Tank (inside of main in this case), you would subsequently call the init method as well to load the image.

Hope that helps.  :)
« Last Edit: April 13, 2012, 01:08:44 am by Digital »

Syntactic Fructose

  • Jr. Member
  • **
  • Posts: 80
  • Overflowing stacks and eating snacks
    • View Profile
Re: DisplayingSprites using classes(tutorial help)
« Reply #2 on: April 13, 2012, 04:16:38 am »
Thanks for the reply! But im still just confused on how to go about declaring a tank object, could i ask you to maybe just write one line of code declaring the object in main? i have no clue what to put in for the parameter(does not work with zero). Ill update my main post with the code

Digital

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: DisplayingSprites using classes(tutorial help)
« Reply #3 on: April 13, 2012, 07:49:37 am »
Code: [Select]
int main()
{
    sf::RenderWindow Window(sf::VideoMode(800,600,32), "TANKWARS");
    Tank Playerone = Tank();               // calls default constructor to create a local instance
    if(!Playerone.LoadFile("Tank.tga"))
        return EXIT_FAILURE;
}

Oh, and as you are making the sf::Image static, you should remove "Image(Copy.Image)" from the copy constructor since there is no need to copy it (and it would cause a compiler error I believe). And the rest should be ok I think but I can't be sure since I'm using SFML 2.0 and can't test. Hope it works.  :)
« Last Edit: April 13, 2012, 07:53:26 am by Digital »

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: DisplayingSprites using classes(tutorial help)
« Reply #4 on: April 13, 2012, 01:18:04 pm »
i have no clue what to put in for the parameter

I think you dont need any parameter

  Tank playerOne;
  if (!playerOne.LoadFile ("tank.png"))
  ....
 

then, player two can be created from player one using your copy constructor (I guess that´s why you wrote it)  :)

  Tank playerTwo (playerOne);
 
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

Syntactic Fructose

  • Jr. Member
  • **
  • Posts: 80
  • Overflowing stacks and eating snacks
    • View Profile
Re: DisplayingSprites using classes(tutorial help)
« Reply #5 on: April 13, 2012, 08:59:51 pm »
Digital, your code did not work, and neither did mateandmetal. It just continues to give me this error:

Code: [Select]
J:\SFML\SFML_SmallGame\main.cpp||In function 'int main()':|
J:\SFML\SFML_SmallGame\main.cpp|9|error: no matching function for call to 'Tank::Tank()'|
J:\SFML\SFML_SmallGame\Tank.h|8|note: candidates are: Tank::Tank(const Tank&)|
||=== Build finished: 1 errors, 0 warnings ===|

Because i have no parameters when i create an object. Help!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: DisplayingSprites using classes(tutorial help)
« Reply #6 on: April 13, 2012, 09:01:27 pm »
You must define a default constructor too; currently you can only create Tank instances by copying other instances.
Laurent Gomila - SFML developer

Syntactic Fructose

  • Jr. Member
  • **
  • Posts: 80
  • Overflowing stacks and eating snacks
    • View Profile
Re: DisplayingSprites using classes(tutorial help)
« Reply #7 on: April 13, 2012, 09:09:14 pm »
You must define a default constructor too; currently you can only create Tank instances by copying other instances.

What would the default constructor consist of? just nothing? then i would call a tank object using that object i created?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: DisplayingSprites using classes(tutorial help)
« Reply #8 on: April 13, 2012, 09:13:07 pm »
Hmm... Don't you think you should learn C++ first? :P
Laurent Gomila - SFML developer

Syntactic Fructose

  • Jr. Member
  • **
  • Posts: 80
  • Overflowing stacks and eating snacks
    • View Profile
Re: DisplayingSprites using classes(tutorial help)
« Reply #9 on: April 13, 2012, 09:24:58 pm »
Hmm... Don't you think you should learn C++ first? :P

It not that i have no knowledge of c++(been learning it for 6 months now), its all of the SFML functions and types that confuse me. I read about how sprites and images should be handled in a class, but as this is my first real week of learning about SFML i know very little. I just like to learn the right way first instead of having to change my habits.

about the program: i added in the overloaded constructor and used that as a parameter to my first tank object, that works fine. Problem now is its telling me there is an undefined reference to sf::Image, most likely me just missing something obvious. Thanks for the help

Digital

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: DisplayingSprites using classes(tutorial help)
« Reply #10 on: April 13, 2012, 11:24:45 pm »
Problem now is its telling me there is an undefined reference to sf::Image, most likely me just missing something obvious. Thanks for the help
The undefined reference means there is a linking problem. You want to make sure you are linking the correct SFML library files in the linker options of whatever IDE you're using. Check out the Getting Started tutorials for how to set up the libs.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: DisplayingSprites using classes(tutorial help)
« Reply #11 on: April 14, 2012, 09:12:21 am »
Quote
Problem now is its telling me there is an undefined reference to sf::Image
Google "C++ static member" ;)
Laurent Gomila - SFML developer