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

Author Topic: How does draw work?  (Read 1713 times)

0 Members and 1 Guest are viewing this topic.

KoeKhaos

  • Newbie
  • *
  • Posts: 10
    • View Profile
How does draw work?
« on: July 08, 2015, 05:55:06 pm »
I'm new to programming in general and so I don't really understand a lot of things but I've been working on a general star background generator and came up with a method that works nicely. The problem I have is that a lot of stars seems to slow it down greatly. A lot of stars being around 30,000 or more. Seeing as there are much more complex and numbered textures in video games I feel like this should be easy for a modern computer. I tried commenting out various parts of the code and narrowed it down specifically to the draw call. At first I thought maybe my stars vector was copying instead of passing by references, but I tried to change it specifically to references and got it working but that didn't make it any faster. I'm only using a small png with a few star shapes for a template and only load it once and just apply it to multiple sprites so I didn't think the resource itself should be slowing it down since it's just one reference used multiple times. Am I missing something important in how draw works? Thanks for any help, tips, etc!

The short: How does draw work and why is it slow when redrawing from a single resource reference of a small png? Is it just a limitation for the number of draws it can do efficiently or am I just probably doing something wrong?


void Stars::render(sf::RenderWindow *temp)
{
    for(size_t i=0;i<starsVec.size();i++)
    {
        temp->draw(starsVec);
    }
}
« Last Edit: July 08, 2015, 06:11:08 pm by KoeKhaos »

kitteh-warrior

  • Guest
Re: How does draw work?
« Reply #1 on: July 08, 2015, 07:44:24 pm »
This is a good reference. Reduce the amount of draw calls. A search of this forum would definitely give you the information you need.

Also,
I'm new to programming in general and so I don't really understand a lot of things

This is a bad idea. SFML is not meant to teach you C++, or programming idioms in general. Learn those first.

Quote from: KoeKhaos
How does draw work and why is it slow when redrawing

SFML is open source. If you want to know how/why, look at the source code and figure it out.

KoeKhaos

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: How does draw work?
« Reply #2 on: July 08, 2015, 07:56:04 pm »
I took a quick glance at that and will read more deeply, thanks. I was mainly curious about why it was that caused it to be slow and I see that article vaguely mentions it though not in much detail.

Your second part is a point I keep seeing being made and I can't really agree with it. If I don't do something interesting I won't learn anything because I won't do anything. I've been trying to learn C++ for about 4 years now and still don't understand much. I've read over 10 books on C++ and watched three entire c++ tutorials on SFML and Allegro as well as other small videos. I fear I'm just a very slow learner with this type of thing but I have 0 artistic abilities in other areas while with programming I think with perseverance I can actually create something eventually which is a dream of mine. This isn't something that just clicks for me so the only way I learn is to actually do something I want to and figure it out as I go. I've learned a great deal about C++ simply from applying it and fiddling with it until it works with SFML. I'm a very hands on learner and just reading things makes almost no sense to me. So, basically, I'd argue that learning C++ from C++ books AND SFML at the same time is a very good idea as I can actually put into application what I learn in the books so it makes sense in a real sense for me.

I did try looking at the source, but to be honest it's still beyond me at this point to really understand. Was just hoping for a simple explanation to start on.

I hope you don't take me as ungrateful, however. That article you gave me really does look interesting though and I am thankful for your response and link. :)

kitteh-warrior

  • Guest
Re: How does draw work?
« Reply #3 on: July 08, 2015, 08:17:32 pm »
Learning C++ can be hands on, without needing any libraries. You can use the STL. Making simple little console-based utilities should generally be a first step.

Quote from: KoeKhaos
watched three entire c++ tutorials

Video tutorials become much more confusing. This is because when they are outdated, they usually don't get updated. People tend to follow these video tutorials and the often bad habits that the creators of them do.

For example, if you don't know how class abstraction works at all: is it better to just use the STL, or jump into the middle of SFML? With using SFML, you would have to know how to STL works (unless you are not making anything that works very well). This process is called encapsulation. You learn the things required for the next step before you take it.

Quote from: KoeKhaos
Was just hoping for a simple explanation to start on

To be put as simply as I can, it is completely magic. ;)

Quote from: KoeKhaos
That article you gave me really does look interesting though
??? The official SFML tutorials should be something you already know about.

KoeKhaos

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: How does draw work?
« Reply #4 on: July 08, 2015, 08:24:28 pm »
Thanks again, I did read through them all but there is a lot to read and I forget things... Just ask my wife! And yes, I feel a bit silly that I didn't find these when looking through just now. Completely forgot about the FAQ when searching. Definitely gave me some stuff to focus on now. As for the other stuff, I'd really not get anything done if I was working on console stuff still because I'd be too bored. For me, right now, as long as I get something working that I can see it is a good movement forward in learning so that's my goal and SFML is proving to be quite nice for it. :) Still some things that completely boggle my brain like Lambda expressions, but I'll get there slowly! Thanks again!
« Last Edit: July 08, 2015, 08:27:09 pm by KoeKhaos »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How does draw work?
« Reply #5 on: July 08, 2015, 09:21:48 pm »
I strongly disagree with what kitteh was saying, or - at least - the way that it was said. It's true that to fully grasp using SFML, having a solid (preferably superhuman) understanding of C++ is required. However, it's completely up to the individual if they wish to persue its use without understanding some basic C++. Some things in SFML are easy; some are hard. Generally, the harder stuff requires deeper knowledge of C++, programming techniques and the like. However, the simpler stuff does not and SFML can be used quite easily to create applications that use graphics and sound in a simple way.

That said, learning/attempting SFML while still learning the more basic C++ can be slower. Much slower. But, as was pointed out, slow progression (due to being able to do some things that interest you) is faster than no progression (due to having no inspiration).

Although some people on these forums can be quite aggressive about learning C++ completely before touching SFML, take it with a grain of salt. Take heed of their warnings and listen to their advice but don't give up just because your C++ might not be up-to-scratch. One of the (main) reasons that people advise learning C++ first is because a lot of your errors/debugging etc. could be from simple C++ errors or actual library problems. Unfortunately, a lot of beginners with SFML assume that it's the latter and report bugs that don't exist because they made a mistake in the C++.

On the other hand, some people assume that because they're learning both together that any problem they face should be asked on an SFML forum. This leads to people here becoming irritated that they're asking C++ questions which should not need to be asked.

If people begin SFML with a less-than-perfect knowledge and understanding of C++ (or other parts that will be required with or without SFML like: compiling, building, debugging etc.) then they must understand that they will have to find most of the answers themselves and be certain that it's SFML-related before bringing the question to these forums or expect to be hit with replies like "learn c++ first".

The answers to a lot of SFML-related question can, in fact, be found on the SFML site by clicking on Learn on the home page. This gives you links to the tutorials, (which will answer the majority of basic questions, and an occasional advanced one), the documentation, (which will answer the majority of questions not explained in the tutorials), and the FAQ (which includes some information on some of the common pitfalls).

In summary, if you have no inspiration to program using the console only, allow something like SFML to give you a doorway to the inspiration that may allow you to progress but be aware that it's a side-door and the journey is not a clean one.

p.s. I took the side door. Say hello if you see me on your journeys!
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*