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

Author Topic: didii's simple classes  (Read 5407 times)

0 Members and 1 Guest are viewing this topic.

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
didii's simple classes
« on: November 12, 2012, 05:54:54 am »
I made a few simple classes I'm proud of even they don't do anything remotely useful at the time. For now I have created 3 classes:
  • Clickable: detects if mouse is hovering over the object or is clicked on with a certain mouse button.
  • Interactable: detects if object is hovering over the object or a certain button is pressed while hovering.
  • TileMap: Creates a map of tiles given by the user (more explanation below).
The classes Clickable and Interactable are meant to be parents of other objects to make them clickable or interactable, whereas TileMap should be fully functional on its own.

Here are all files (.h and .cpp in a .zip). If you just want to see what the functions do, click here (executable in .zip).
The program will draw a map on the screen, and will create an Interactable and a Clickable on the same place as the map. Moving your mouse or the red box over the map will trigger an output on the output screen. Move the red box with the arrow keys. More outputs are triggered when hovering and pressing the right button: for the mouse it's the left click, for the box it's space or enter.


Clickable
Key functions:
bool IsHovering();                 // return true when mouse on box
bool StartHovering();              // return true when mouse enters box
bool StopHovering();               // return true when mouse leaves box
bool IsClicked(sf::Mouse::Button); // return true when clicked on box
Not much to say about it, it's pretty obvious what it does and how it does it. IsHovering() is called for every other function mentioned above. None of these functions handle events, this is why the function IsClicked() is best called after testing if event == sf::Event::MouseButtonPressed.

Interactable
Key functions:
bool IsHovering();
bool StartHovering();
bool StopHovering();
bool InteractButton(sf::Keyboard::Key);
So basically the same as Clickable, but only accepts keyboard input. The biggest difference is that Interactable works with an interaction box or point. This means for example that you can create an invisible box ahead of your character. When that box hovers over an Interactable, IsHovering() will return true. The argument of InteractButton() should always be sf::Event.key.code. InteractButton() does not handle events.

TileMap
Key functions:
void Draw();  // draws map on app
This class uses a texture where all tiles are located in. It needs to know the size of the tiles and the width and height of the map in tiles. When it is given an int m[width*height], where m[i+j*height] (with i and j from a for-loop) has the value for which tile it should display at that position, it will draw the map on the given sf::RenderWindow.




If you wish to give me some advice on the functions, I know it's a little bit too much at once to handle. Just pick one random and give all you've got on that one. You should only need to look at the key functions (under // <class> functions in every file) and the variables under protected: or private:.

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: didii's simple classes
« Reply #1 on: November 12, 2012, 12:44:37 pm »
They're not that great. :-\
First two are easily replaced by sfgui or tgui(didn't look at them yet) and your tile map design with sprite that gets drawn a lot of times is much less effective than vertex array approach. Also.. theres is new in your code, and there is no custom d-tor or any delete so you have a memory leak..  :(  Also you're using cstyle casts.
Back to C++ gamedev with SFML in May 2023

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: didii's simple classes
« Reply #2 on: November 12, 2012, 01:12:03 pm »
They're not that great. :-\
First two are easily replaced by sfgui or tgui(didn't look at them yet) and your tile map design with sprite that gets drawn a lot of times is much less effective than vertex array approach. Also.. theres is new in your code, and there is no custom d-tor or any delete so you have a memory leak..  :(  Also you're using cstyle casts.
Thanks for the comment :) Didn't expect them to be great though. These are my first classes I've ever written in C++ with SFML.
Well, first off: I never heard about sfgui or tgui, but I will take a look at them, thanks for referring.
Oh, totally forgot about the memory leaks :-[
Can I know why some things are better than other like:
- What is the idea behind the vertex array approach?
- What is wrong with the c-style casts? I've learned in school that casting using the syntax (int)(5.3f) is more efficient than int(5.3f) because it's build in the compiler or something. (It wasn't really explained)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: didii's simple classes
« Reply #3 on: November 12, 2012, 01:59:19 pm »
Well, first off: I never heard about sfgui or tgui, but I will take a look at them, thanks for referring.
Those are two GUI library, but one doesn't always need a full blown GUI, so a simpler approach may fit your needs better at the moment.

- What is the idea behind the vertex array approach?
A VertexArray has to only once call the OpenGL draw function (SFML is built on top of OpenGL), thus it's way more efficient than having to call it as many times as tiles exist. Also since then everything is nicely composed of vertices the GPU will be extremely fast. (GPUs are built to handle millions of vertices per second).

- What is wrong with the c-style casts? I've learned in school that casting using the syntax (int)(5.3f) is more efficient than int(5.3f) because it's build in the compiler or something. (It wasn't really explained)
Those are both C cast and I don't think one is more efficient than the other one, it's just a syntactic sugar.
What FRex meant (but didn't explain!) was that you should use static_cast<int>(5.4f) which is the C++ way of casting.
« Last Edit: November 12, 2012, 04:13:16 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: didii's simple classes
« Reply #4 on: November 12, 2012, 04:10:54 pm »
You should use const correctness

bool IsClicked(sf::Mouse::Button);
 

with const correctness:

bool IsClicked(const sf::Mouse::Button) const;
 
« Last Edit: November 13, 2012, 07:30:27 pm by mateandmetal »
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: didii's simple classes
« Reply #5 on: November 12, 2012, 04:35:11 pm »
Whether to make arguments passed by value const or not is a matter of taste. Some people like it and find it useful, others don't like it at all.

You don't need to confuse beginners with that; I personally wouldn't have answered if it was just to say that... unless your intention was to start a troll ;)

A more important modification for const correctness would be to make the IsXxx and Draw functions const.
Laurent Gomila - SFML developer

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: didii's simple classes
« Reply #6 on: November 12, 2012, 07:22:47 pm »
Quote
What FRex meant (but didn't explain!)
>:( ;D

C casts cast whatever to whatever, whenever and however they can(it's actually like static, const and reinterpret casts together, using whichever fits the needs at the moment), new style casts are safer if you mess up because compiler stops with error: http://www.stroustrup.com/bs_faq2.html#static-cast

There's example code on the wiki for a tiled map with vertex arrays(since someone some time ago said there is none, not even a basic one, now there is one.. ;D).

Quote
Well, first off: I never heard about sfgui or tgui
See attached image. ;D


[attachment deleted by admin]
« Last Edit: November 12, 2012, 07:27:10 pm by FRex »
Back to C++ gamedev with SFML in May 2023

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: didii's simple classes
« Reply #7 on: November 12, 2012, 09:50:56 pm »
Thanks for all the comments and remarks! Will try and keep everything said in mind :)

A more important modification for const correctness would be to make the IsXxx and Draw functions const.
Indeed, I heard some things about it but never understood what it exactly meant. Just googled it and now I know what it does (const functions doesn't allow you to change member variables), but can't find a good reason to implement it. For what reason is it safer to implement const-functions (and actually variables too). Is it because the pc can make accidental changes or anything like that?

Btw, I'm trying to make pong now (with different classes instead of everything in the main, I really don't like having everything in main: it's too complicated and handles very different when using classes in my opninion). But is it a good idea to make the class PlayerBat a child of sf::RectangleShape? Then I wouldn't have to keep a pointer to the main sf::RenderWindow (because I can use sf::RenderWindow.draw(PlayerBat) instead) and will give the class more freedom.

There's example code on the wiki for a tiled map with vertex arrays(since someone some time ago said there is none, not even a basic one, now there is one.. ;D).
Yeah, I didn't look at the wiki too much actually. The tutorials were all about an extra package QCE or something and I had no idea where that came from or what any of those functions meant... (couldn't find anything at first on the wiki about it too, didn't search too much though). But I will take a look then, thanks :)

See attached image. ;D
Woops, missed that for sure ::)

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: didii's simple classes
« Reply #8 on: November 13, 2012, 07:29:31 pm »
You don't need to confuse beginners with that; I personally wouldn't have answered if it was just to say that... unless your intention was to start a troll ;)

I dont want to be a troll. Maybe I dont know much english but I was trying to help him write "better" code.


A more important modification for const correctness would be to make the IsXxx and Draw functions const.

That´s why I inserted the link. My examples were terrible, I know  ;D
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

kaB00M

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Caffeware
    • Email
Re: didii's simple classes
« Reply #9 on: November 13, 2012, 09:41:38 pm »
A VertexArray has to only once call the OpenGL draw function (SFML is built on top of OpenGL), thus it's way more efficient than having to call it as many times as tiles exist. Also since then everything is nicely composed of vertices the GPU will be extremely fast. (GPUs are built to handle millions of vertices per second).

What black magic is this? I guess this won't work in old computers thought, right?



eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: didii's simple classes
« Reply #10 on: November 13, 2012, 11:24:55 pm »
What black magic is this? I guess this won't work in old computers thought, right?
Depends on your definition of old. ;)
GPUs were always built for fast vertices processing, thus if you'd compare draw calls for x different sprites (= quads) and one draw call for x quads at once you'd probably still see a better performance for the latter. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything