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

Author Topic: Classes and SFML  (Read 5766 times)

0 Members and 1 Guest are viewing this topic.

Inflammatory Nugget

  • Newbie
  • *
  • Posts: 21
    • View Profile
Classes and SFML
« on: May 20, 2013, 02:57:01 pm »
Greetings,

I'm somewhat new to SFML and I am having some trouble with using classes together with SFML: I am trying to create a class for bullets. This is what I have so far:

#include <SFML\graphics.hpp>

using namespace sf;

class bullet{
private:
        CircleShape bullet_shape;
        float x1, y1;
        unsigned short x2, y2;
public:
        void create( float, float, unsigned short, unsigned short);    
};

//bullet creation
void bullet::create( float kult_x, float kult_y, unsigned short mouse_x, unsigned short mouse_y){
        /*Kult is the name of the player character. Kult_x is the position of Kult on the x-axis
        while kult_y is the position of Kult on the y-axis*/

        x1 = kult_x;
        y1 = kult_y;
        /*Mouse_x is the position of the mouse on the x-axis and mouse_y
        is the position of the mouse on the y-axis */

        x2 = mouse_x;
        y2 = mouse_y;
        bullet_shape.setRadius( 2 );
        bullet_shape.setFillColor( Color::Black );
        bullet_shape.setPosition( x1, y1 );
        //the below line is temporary
        bullet_shape.move( 3, 3);
}
 

This is a file of its own, for the purpose of good organisation.

I am clueless on what to do next. I have tried making some searches on the web, but to no avail. Any help will be appreciated.
« Last Edit: May 20, 2013, 04:21:31 pm by Inflammatory Nugget »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Classes and SFML
« Reply #1 on: May 20, 2013, 03:04:38 pm »
Welcome to the SFML community! :)

The question is, what do you want to do next?
Unless you know what the plan is and tell us what you want to do and where you're stuck we can't help much further...

As for the given code, you should always use the code=cpp tag when posting things in the forum, you should always use forward slashes / (yes VS supports this) and you should in most cases never use using namespace, especially not in a header. Besides those syntactic things, you should also rather make use of the constructor with its initialization list itself to initialize the object and not use a separate create function! ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Inflammatory Nugget

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Classes and SFML
« Reply #2 on: May 20, 2013, 03:36:35 pm »
Thank you, eXpl0it3r, for your fast response.

What I'm trying to do is create a reusable class that only requires the 4 variables ( float kult_x, float kult_y, unsigned short mouse_x, unsigned short mouse_y) to create an ingame bullet. However I have no idea on how to actually create a way to put it on the screen correctly. I do apologise if I am being unclear.

This is what I have now after taking your advice for the code:

#include <SFML\graphics.hpp>

class bullet{
private:
        sf::CircleShape bullet_shape;
        float x1, y1;
        unsigned short x2, y2;
public:
        bullet( float, float, unsigned short, unsigned short); 
};

//bullet creation
bullet::bullet( float kult_x, float kult_y, unsigned short mouse_x, unsigned short mouse_y){
        /*Kult is the name of the player character. Kult_x is the position on the x-axis
        while kult_y is the position of Kult on the y-axis*/

        x1 = kult_x;
        y1 = kult_y;
        /*Mouse_x is the position of the mouse on the x-axis and mouse_y
        is the position of the mouse on the y-axis */

        x2 = mouse_x;
        y2 = mouse_y;
        bullet_shape.setRadius( 2 );
        bullet_shape.setFillColor( sf::Color::Black );
        bullet_shape.setPosition( x1, y1 );
        //the below line is temporary
        bullet_shape.move( 3, 3);
}

Also, how do I use the code=cpp tag?
« Last Edit: May 20, 2013, 05:57:34 pm by Inflammatory Nugget »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Classes and SFML
« Reply #3 on: May 20, 2013, 03:46:49 pm »
I've only spotted one implementation change... ;)

To use the code=cpp tag, you simply write [ code=cpp ]// Your code[ /code ] (without the spaces).
#include <SFML/Graphics.hpp>

class bullet{
private:
   sf::CircleShape bullet_shape;
   float x1, y1;
   unsigned short x2, y2;
public:
   bullet( float, float, unsigned short, unsigned short);  
};

//bullet creation
bullet::bullet( float kult_x, float kult_y, unsigned short mouse_x, unsigned short mouse_y)
   /*Kult is the name of the player character. Kult_x is the position on the x-axis
   while kult_y is the position of Kult on the y-axis*/

   x1(kult_x),
   y1(kult_y),
   /*Mouse_x is the position of the mouse on the x-axis and mouse_y
   is the position of the mouse on the y-axis */

   x2(mouse_x),
   y2(mouse_y)
{
   bullet_shape.setRadius( 2 );
   bullet_shape.setFillColor( Color::Black );
   bullet_shape.setPosition( x1, y1 );
   //the below line is temporary
   bullet_shape.move( 3, 3);
}

Fixed backslash \ to forward slash /.
Fixed case sensitivity graphics.hpp to Graphics.hpp.
Fixed no use of the initialization list.

Next I'll have to implement a game loop and then you might want to add an draw function to the class, which can be called within the game loop.

If you're new to C++, I highly recommend you to read a good book about C++ first and stay with simple console applications for the first few weeks/month. C++ is not a language that can be learned with trial and error. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Inflammatory Nugget

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Classes and SFML
« Reply #4 on: May 20, 2013, 04:22:29 pm »
Actually, 2 changes were made: the use of 'sf::' instead of 'using namespace sf' and I removed the seperate function.

I have the game loop in the main file.

Anyway, as you pointed out, I need this draw function for my class: this is where I am really stuck. I don't know how to return the CircleShape correctly and then use sf::window.draw to put it on the screen.

Also, thanks for pointing out the use of [ code = cpp ], I have implemented this in my previous posts.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Classes and SFML
« Reply #5 on: May 20, 2013, 04:33:34 pm »
Actually, 2 changes were made: the use of 'sf::' instead of 'using namespace sf' and I removed the seperate function.
Oh right! :)

The easiest way to implement it, would be to pass the window as reference to the draw function and within the function call window.draw(bullet_shape). In the game loop, you'll then simply have to class bullet_instance.draw(window).
If you rather want a different syntax, like window.draw(bullet_instance) you can go and derive your bullet class from sf::Drawable and implement the one and only function from sf::Drawable, the draw() function (you can take a look at the tutorials and the documentation).
On a latter point you might want to redesign everything to keep the logic and the graphics even more separated.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Inflammatory Nugget

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Classes and SFML
« Reply #6 on: May 20, 2013, 06:34:04 pm »
Declaration of bullet array, before the game loop:
bullet b[ 20 ];
In the display function:
b[ i ].draw( &window );

In bullet.h:
void bullet::draw( sf::RenderWindow window_instance ){
        window_instance.draw( bullet_shape );
}

After compiling I get the following errors:
1>c:\users\frank\documents\visual studio 2010\projects\cppapplication\main.cpp(354): error C2664: 'bullet::draw' : cannot convert parameter 1 from 'sf::RenderWindow *' to 'sf::RenderWindow'
1>          No constructor could take the source type, or constructor overload resolution was ambiguous


I had to revert to the seperate create function because it's not possible to declare variables in functions. My gameloop is set up this way and I like it as it is. So now bullet.h looks like this:
#include <SFML/Graphics.hpp>

class bullet{
private:
        sf::CircleShape bullet_shape;
        float x1, y1;
        unsigned short x2, y2;
public:
        void create( float, float, unsigned short, unsigned short);
        void draw( sf::RenderWindow );
};

//bullet creation
void bullet::create( float kult_x, float kult_y, unsigned short mouse_x, unsigned short mouse_y){
        /*Kult is the name of the player character. Kult_x is the position on the x-axis
        while kult_y is the position of Kult on the y-axis*/

        x1 = kult_x;
        y1 = kult_y;
        /*Mouse_x is the position of the mouse on the x-axis and mouse_y
        is the position of the mouse on the y-axis */

        x2 = mouse_x;
        y2 = mouse_y;
        bullet_shape.setRadius( 2 );
        bullet_shape.setFillColor( sf::Color::Black );
        bullet_shape.setPosition( x1, y1 );
        //the below line is temporary
        bullet_shape.move( 3, 3);
}

void bullet::draw( sf::RenderWindow window_instance ){
        window_instance.draw( bullet_shape );
}
« Last Edit: May 20, 2013, 06:35:37 pm by Inflammatory Nugget »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Classes and SFML
« Reply #7 on: May 20, 2013, 07:53:17 pm »
would be to pass the window as reference to the draw function and within the function call.

It seems you're missing a bit of basic C++ knowledge, you might want to read up on it with a good C++ book. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Inflammatory Nugget

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Classes and SFML
« Reply #8 on: May 20, 2013, 08:45:40 pm »
That reply left me frustrated.

Thanks for the help you have offered, though.

Roose Bolton of the Dreadfort

  • Full Member
  • ***
  • Posts: 113
  • Full-time Procrastinator, Part-time programmer.
    • View Profile
    • Personal Portfolio/Website/Blog
Re: Classes and SFML
« Reply #9 on: May 21, 2013, 02:01:02 pm »
You should read up a bit on C++ and pointers etc as its a critical part of the language..

but so your not frustrated..

when you pass something by value (what your doing) your making a copy of that object in the function and then when you process functions on that object etc, your applying that to the COPIED object and not the original that you passed in.

if you want to draw it to the original render window that you passed in, you have to pass by reference so;

void bullet::draw( sf::RenderWindow &window_instance ){
    window_instance.draw( bullet_shape );
}
 

This then passes the reference to object you need and doesn't make a copy.

You can also pass by pointer but this is less safe as you can also pass in NULL pointers this way.. but they both have their pro's & con's; I personally pass by pointer because references hide that you are changing data stored someplace else, where as pointers make it very obvious (->)..

it can be quite difficult to grasp when coming from other languages as in most everything is a pointer.. so you have to think less about it
« Last Edit: May 21, 2013, 02:11:58 pm by Tally »
Trying so very hard to finish at-least one project.

Watch out for the RAII police, they will get you.

www.bantersaurus-games.com

MorleyDev

  • Full Member
  • ***
  • Posts: 219
  • "It is not enough for code to work."
    • View Profile
    • http://www.morleydev.co.uk/
Re: Classes and SFML
« Reply #10 on: May 21, 2013, 02:43:40 pm »
I personally pass by pointer because references hide that you are changing data stored someplace else, where as pointers make it very obvious (->)..

The counter-argument most commonly presented to this is that having a pointer parameter in C++ carries the implication that you can pass null to the function, which may be incorrect :) But even then, in general I recommend avoiding using such out parameters anyway where you can, for they are ugly and in C++ can be confusing. Mutating input arguments is a strong code smell (code smell means something in code that indicates a weakness in design).

What having to do that here suggests, to me, is that a bullet should not possess knowledge of how to draw itself. This is a responsibility which the bullet should not have, but should be the job of something else.
« Last Edit: May 21, 2013, 05:38:02 pm by MorleyDev »
UnitTest11 - A unit testing library in C++ written to take advantage of C++11.

All code is guilty until proven innocent, unworthy until tested, and pointless without singular and well-defined purpose.

 

anything