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

Author Topic: Help With Space Invaders Style Game Class. Just started SFML.  (Read 6174 times)

0 Members and 1 Guest are viewing this topic.

Infinity Squared Software

  • Newbie
  • *
  • Posts: 8
  • New Mission: Decline this mission.
    • View Profile
    • Infinity Squared Software
    • Email
Help With Space Invaders Style Game Class. Just started SFML.
« on: November 25, 2015, 07:41:51 pm »
Hey Guys!
I just recently started using SFML with C++ and I really enjoy it. However, because I'm new, I have already run into a problem. I am trying to recreate Space Invaders. To create the enemy, I assume my options are to create the enemy, and then copy and paste that code again and again. My other reasonable and cleaner option was to create a class. My problem is, I can't get my classes to work in SFML. Can someone help?

The first thing I do is create a class and then create an object:
        //Creating an Enemy
        class Enemy
        {
        public:
                Enemy::Enemy()
                {
                        sf::RectangleShape shape(sf::Vector2f(25, 25));
                }
        };
       
        Enemy enemyOne;

After that, I call the enemyOne object in the window.draw()
window.draw(enemyOne);

However I get the error:
Severity   Code   Description   Project   File   Line
Error   C2664   'void sf::RenderTarget::draw(const sf::Vertex *,size_t,sf::PrimitiveType,const sf::RenderStates &)': cannot convert argument 1 from 'main::Enemy' to 'const sf::Drawable &'   SFML Test   c:\users\calvin\documents\visual studio 2015\projects\sfml test\sfml test\source.cpp   71

and I get this error:
Severity   Code   Description   Project   File   Line
Error (active)      no instance of overloaded function "sf::RenderWindow::draw" matches the argument list   SFML Test   c:\Users\Calvin\Documents\Visual Studio 2015\Projects\SFML Test\SFML Test\Source.cpp   71

What am I doing wrong and how can I fix it?

Any help would be GREATLY appreciated. I have been making games with game engines (Unity, GM, etc.) and really want to start using SFML.
« Last Edit: November 25, 2015, 07:47:40 pm by Infinity Squared Software »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Help With Space Invaders Style Game Class. Just started SFML.
« Reply #1 on: November 25, 2015, 08:23:52 pm »
To create the enemy, I assume my options are to create the enemy, and then copy and paste that code again and again.
Loops exist for a reason. Don't copy and paste chunks of code.
Write a loop that creates the required number of instances of your enemies and stores them in a container. Or just create the cotainer with the correct number of default constructed enemies. There are many better options than just repeating the same block of code.

 
My other reasonable and cleaner option was to create a class. My problem is, I can't get my classes to work in SFML.
 ...
After that, I call the enemyOne object in the window.draw()
window.draw(enemyOne);

However I get the error:
Severity   Code   Description   Project   File   Line
Error   C2664   'void sf::RenderTarget::draw(const sf::Vertex *,size_t,sf::PrimitiveType,const sf::RenderStates &)': cannot convert argument 1 from 'main::Enemy' to 'const sf::Drawable &'   SFML Test   c:\users\calvin\documents\visual studio 2015\projects\sfml test\sfml test\source.cpp   71

 ...

What am I doing wrong and how can I fix it?
The error explains exactly what the problem is; draw() expects a sf::Drawable but your class does not inherit from it.
To use the class like you are trying to do it should inherit sf::Drawable and implement the draw() function. This is explained in detail in the tutorials and API docs. I suggest you read them.

Infinity Squared Software

  • Newbie
  • *
  • Posts: 8
  • New Mission: Decline this mission.
    • View Profile
    • Infinity Squared Software
    • Email
Re: Help With Space Invaders Style Game Class. Just started SFML.
« Reply #2 on: November 25, 2015, 08:55:39 pm »
Thanks for the reply! Where is the documentation for what you are talking about?

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Help With Space Invaders Style Game Class. Just started SFML.
« Reply #3 on: November 25, 2015, 09:22:04 pm »
It's linked from the front page of the main SFML website (the "Learn" link) and points here.
The specific bits of the documentation I was refering to are these:
 http://www.sfml-dev.org/tutorials/2.3/graphics-draw.php
 http://www.sfml-dev.org/documentation/2.3.2/classsf_1_1Drawable.php

There is also the wiki for even more documentation goodies and example source code: https://github.com/SFML/SFML/wiki
« Last Edit: November 25, 2015, 09:25:08 pm by Jesper Juhl »

Infinity Squared Software

  • Newbie
  • *
  • Posts: 8
  • New Mission: Decline this mission.
    • View Profile
    • Infinity Squared Software
    • Email
Re: Help With Space Invaders Style Game Class. Just started SFML.
« Reply #4 on: November 25, 2015, 10:35:39 pm »
Sorry to keep hassling you. :-[

So I read the documentation on sf::Drawable.

first I added:
#include <Drawable.hpp>

That gives me this error:
Severity   Code   Description   Project   File   Line
Error (active)      cannot open source file "Drawable.hpp"   SFML Test   c:\Users\Calvin\Documents\Visual Studio 2015\Projects\SFML Test\SFML Test\Source.cpp   4

The thing is, I have graphics.hpp imported, libs and include directories are set up, and I have set the linker options, for debug which I am in.

For the class I did this:
        //Creating an Enemy
        class Enemy : sf::Drawable
        {
        public:
        private:
                virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
                {
                        sf::RectangleShape enemyShape(sf::Vector2f(35, 15));
                        target.draw(enemyShape);
                }
        };

        Enemy enemy;

This doesn't cause any errors, but it also doesn't accomplish anything more.

Lastly, I added this at the end:
window.draw(enemy);
 

This causes this error:
Severity   Code   Description   Project   File   Line
Error (active)      conversion to inaccessible base class "sf::Drawable" is not allowed   SFML Test   c:\Users\Calvin\Documents\Visual Studio 2015\Projects\SFML Test\SFML Test\Source.cpp   70

and this:
Severity   Code   Description   Project   File   Line
Error   C2243   'type cast': conversion from 'main::Enemy *' to 'const sf::Drawable &' exists, but is inaccessible   SFML Test   c:\users\calvin\documents\visual studio 2015\projects\sfml test\sfml test\source.cpp   70

Once again, sorry if I'm plaguing your life. I looked at SFML a couple years ago and decided it was way to complicated, now, I'm ready to learn. Please let me know if I can give you any more info.

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: Help With Space Invaders Style Game Class. Just started SFML.
« Reply #5 on: November 25, 2015, 10:48:26 pm »
you need to make like this:
class Enemy : public sf::Drawable

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Help With Space Invaders Style Game Class. Just started SFML.
« Reply #6 on: November 25, 2015, 10:49:10 pm »
For the class I did this:
        //Creating an Enemy
        class Enemy : sf::Drawable
 
Sorry to be harsh, but learn C++.
The default inheritance type (which you are using here) for structs is 'public' for classes it is 'private'. What you want is
class Enemy : public sf::Drawable

Once again, sorry if I'm plaguing your life.
It's ok. But please read up on basic C++. Your problems are not really SFML related, but more related to a lack of understanding of the C++ language.
« Last Edit: November 25, 2015, 10:50:56 pm by Jesper Juhl »

Infinity Squared Software

  • Newbie
  • *
  • Posts: 8
  • New Mission: Decline this mission.
    • View Profile
    • Infinity Squared Software
    • Email
Re: Help With Space Invaders Style Game Class. Just started SFML.
« Reply #7 on: November 25, 2015, 10:51:57 pm »
OK. I have just recently been diving into OOP stuff. I watch some more thenewboston. Thanks for the help. If I run into anymore problems with this, I'll just read docs and wath C++ videos. THANKS!

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Help With Space Invaders Style Game Class. Just started SFML.
« Reply #8 on: November 25, 2015, 11:25:11 pm »
If you want some quality reading material then here are some recomendations:

 A Tour of C++
 Effective Modern C++
 SFML Game Development

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Help With Space Invaders Style Game Class. Just started SFML.
« Reply #9 on: November 27, 2015, 03:17:49 am »
You seem to be creating the rectangle shape within local scope of a method (constructor or draw). This means that the rectangle shape won't last any longer than that method and not be available to any other method (unless called from the one it is in).

It's likely that you'll want to make the rectangle shape a member of the class instead (rather than it only existing in the constructor or being recreated every time draw is called, which is likely to be every frame!)
« Last Edit: November 27, 2015, 03:19:41 am by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Tigre Pablito

  • Full Member
  • ***
  • Posts: 226
    • View Profile
    • Email
Re: Help With Space Invaders Style Game Class. Just started SFML.
« Reply #10 on: November 28, 2015, 04:26:00 am »
Hi Infinity Squared Software

Though I donĀ“t know much about C++, I think this concerns more to the SFML library, precisely to the signature of the draw() method (or function). It returns nothing and it takes a 'drawable' such as RectangleShape or a Sprite. Then you shouldn't pass as an argument an object of the class Enemy.

You could add to that class an atribute or member that is 'drawable' and load into it the shape you want to be displayed and call draw() with that Enemy object's member as the argument. Something like this:

window.draw(enemyOne.image);
(I'm not sure if it is as above or: window.draw(enemyOne->image) )
beeing 'image' a class Enemy member of any 'drawable' type

Respecting to the repetition of code, i suggest, as Jesper Juhl said, to use loops (for, while), and matrices (or vectors, or arrays) with them; I try to write an example: (let's suppose the Enemy class constructor recieves to ints)

int vector[][] = new int[5][2] {{34,21},{53,82},{27,36},{14,45},{31,63}};
int a;
Enemy enemies[] = new Enemy[5];    /* replace the magic number */
for (a = 0; a < 5; a++)
    enemies[a] = new Enemy(vector[a][0], vector[a][1]);

then you could display the 5 enemies as below (in real life there would be many more):

for (a = 0; a < 5; a++)
    window.draw(enemies[a].image);

supposing you have declared before into

class Enemy {
public:
      Enemy:Enemy(int a, int b)
      {
            .....
      }
      sf::RectangleShape image;
      ....
}

Something I don't know if you have already done is the class Enemy logic (movement, attack, destruction, etc)

I can't help you with C++, but perhaps you would like to review some issues about it, such as loops, vectors o matrices (arrays), ... and i suppose what concerns to class structure, pointers, ... etc

what i tried to illustrate for you are alghoritms, a logical piece of code that helps us to do what we need (more efficiently), they are neutral to every language. About them, perhaps i can help you further ... i'm afraid C++ not

good luck!
Pablo
Bs As - Argentina