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

Author Topic: Mulptile Questions (new-ish)  (Read 2665 times)

0 Members and 1 Guest are viewing this topic.

destrugter

  • Newbie
  • *
  • Posts: 10
    • View Profile
Mulptile Questions (new-ish)
« on: November 14, 2013, 11:48:53 pm »
Hello everyone. Let me first explain a small history of myself. I've been programming for about 8 years now (as a hobby at first, more seriously more recently). I started off with a language called DarkBASIC Professional. It's a language geared towards making games easily in BASIC. From there I moved on to other stuff, since then I've done fairly extensive stuff in C#, C++, PHP, SQL, and many more. I've recently completed a Data Structures course that was designed around C++. I was looking for a way to make a game outside of DarkBASIC, preferably with C++ (especially with XNA being discontinued), and I found SFML. I did a small Tetris clone at first to see if I was up to the task, and completed one fairly switfly.

I'm stating this small background to establish that I'm not new to programming or programming concepts, and I do try my hardest to think things out and search extensively about my issues before posting. I've been using SFML for a couple months and only just now made an account. I do plan on being active in the community too.

With that done, let's get to the questions. When I was doing DarkBASIC, it was easy (but costly) to create sprites on the fly. It's not an Object Oriented design. I am currently working on a Tower Defense clone as my next project. I just want to know if I at least have the logic right, and if so..Want to ask what suggestions would be to move forward.

Basically the way I'm thinking is that I want to create the sprites and objects dynamically. That is, the user clicks on a turret to purchase, and only when they click on a spot and everything checks out, create the sprite and object for that specific turret.

If I am right with this, I am just a bit confused on how to go about dynamically creating an object and drawing it later. Obviously pointers are 100% necessary for this. I have come to the conclusion that a linked list would work very well in this situation, but thought it might have been a little overkill.

My next question is, I searched around and found out that using too many draw statements is very costly. What would be the best way to do calls to these dynamically created sprites, without using the draw statement too much?

I look forward to any and all answers/questions I receive here. I was (and still am) very excited to have found SFML.

EDIT: And I'm sorry for such a lengthy first post on such a seemingly simple concept.
« Last Edit: November 14, 2013, 11:51:13 pm by destrugter »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Mulptile Questions (new-ish)
« Reply #1 on: November 15, 2013, 12:19:35 am »
If I am right with this
You are, it's possible to create sprites on-the-fly.

I am just a bit confused on how to go about dynamically creating an object and drawing it later. Obviously pointers are 100% necessary for this.
Totally not. Don't use new and delete in C++, it's the wrong approach in the very most cases. Instead, inform yourself about RAII, a fundamental C++ idiom that is extremely important. Basically, it allows automatic resource and memory management, which makes code safer and simplifies it massively (see here for an example). When you use RAII consequently, memory leaks will be history, along with many other problems related to pointers and manual memory management.

In your case, you should use STL containers to store the sprites. For example std::vector<sf::Sprite>. Note that I'm not using any pointers or dynamically allocated memory. If you haven't heard about the STL, look it up, too.

I have come to the conclusion that a linked list would work very well in this situation, but thought it might have been a little overkill.
A dynamic array (std::vector) is fine for the very most cases. Only use different data structures if you have specific needs.

My next question is, I searched around and found out that using too many draw statements is very costly. What would be the best way to do calls to these dynamically created sprites, without using the draw statement too much?
Don't worry about it, you can draw thousands of sprites before you get into trouble. If you should really get to that point, you can use SFML's vertex arrays. But don't do it unless really necessary, it will make your life harder without any gain. Simply use sprites, you can still optimize later.

There may be some C++ and SFML concepts that you have to learn first. Invest the time, it will save you a lot of headaches in the future. I would even suggest to get a good and modern book about C++, such as the C++ Primer. C++ is a language you can't learn only by doing, and a lot of resources you find (including literature) is massively outdated, omits important background knowledge, or is just plain wrong. Therefore it is crucial to make sure you learn modern C++ as it is used these days -- the language has altered significantly throughout the last decade.
« Last Edit: November 15, 2013, 12:25:04 am by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

destrugter

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Mulptile Questions (new-ish)
« Reply #2 on: November 15, 2013, 01:09:10 am »
I didn't want you to get the idea that I'm new to C++. I recently took a Data Structures course using C++. The book we used is www.amazon.com/gp/aw/d/0132129485/ref=ox_sc_act_image_1?ie=UTF8&m=ATVPDKIKX0DER and the book we used for C++ was from the same author. Our teacher was completely teaching from ground zero so we knew exactly how the data structures work. We used pointers for everything, and weren't allowed to use external libraries.

Anyway, thanks for you answers, I will look into everything in greater detail when I get to my PC

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Mulptile Questions (new-ish)
« Reply #3 on: November 15, 2013, 03:55:48 am »
I don't know if it is generally a good idea to use C++ as a tool to teach people about data structures. In universities data structures are normally taught as abstract constructs free of any concrete implementation. They are basically just ideas that have proved themselves time and again. Letting people learn about them by implementing them in a programming language and at the same time breaking good practices of said programming language isn't really a good idea. It might give the students a false impression that it is the standard way of doing things because they don't regard the language as a mere facility for learning but as part of what they might be able to use in the future.

Don't get me wrong, the book you mentioned teaches very well about the data structures it covers, you just mustn't consider it as teaching standard programming practices at the same time. If you ask me, it isn't a good idea to try to teach too many things at once, and it must be made clear what is trying to be taught in a book so that readers don't get the wrong impression they have learnt more than the author intended to teach.

That being said, as Nexus already said, you need to learn from modern books, and more importantly, teachers who are up to date regarding advances in the language. When browsing through C++ books in book stores that claim to have added material about C++11 features, I am not surprised to find them lacking in so many ways. What many people misunderstand is that C++11 is not merely an extension to the existing language, it is a whole new mindset with its own takes on old ideas. As such, merely revising an old book to include modern C++ topics is normally ill-fated. A true book adequately covering modern C++ has to be written from the ground up to take the new ideas into account. This is also the reason why many prominent authors have yet to publish their books, everything has to be rewritten almost from scratch.

The books you might have used while learning C++ from said author are very likely to be written quite a while ago, and only minor revisions have been made since then. They might have been perfectly valid back when they were initially published, but by no means should one consider themselves up to speed regarding the programming language just because the latest edition was published not so long ago.

And regarding the teaching methodology you mentioned: A very very common mistake teachers make is to prohibit their students from using standard language facilities/libraries to get whatever it is they want done. Using C++ to teach about data structures while prohibiting students from using the STL and having to resort to raw pointers is not the way to go. If it has to be that low level, they can at least use C as the programming language instead of C++, that way it would be natural to do what would be done synthetically in C++.

It used to be the case that the first few topics covered in C and C++ lessons were almost identical, C++ at its conception was also referred to as C with classes. As time passed, new ideas were brought into C++ that fundamentally changed the way things are done, and it is important that beginners understand this. Nowadays, with C++11 the differences in ideology are so huge that people who still program using C++98 habits are closer to C than C++. If you asked me to attempt to teach someone C++ 15 years ago and to teach someone C++ today, I would probably have totally different approaches for each situation.

All in all, I find the topic of teaching C++, especially modern C++ a very hard task. I would never trust myself into trying to teach C++ because I know there are still some concepts I don't yet fully grasp. Not only that, but since I would strive to teach things that are not that productive at first glance, it might make for a really boring lesson and I don't think it would be very appealing. Teachers really need to be used as a supplementary to a good book. They can never be as thorough as a book is unless the lessons stretch over a long period of time, and to be honest, these days people don't want to invest that much time into learning how to program any ways. I had experience teaching younger students basic programming concepts at school and the main problem was that they were very result-driven. They were only interested in topics that would yield meaningful results in the short term. I've since then come to consider programming in general more of an art or science rather than a tool to get things done, although that is their main purpose.

Everybody is new to C++ at some point, the question is, how long will it take to no longer be new to C++? And more importantly, when the language changes, does it make everyone a beginner once more? I know I am.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

destrugter

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Mulptile Questions (new-ish)
« Reply #4 on: November 15, 2013, 04:20:20 am »
I sort of understand what you mean. The reason he wouldn't let us use STL is because he thought we'd understand the fundamentals better if we did it ourselves. I truly believe I have a better grasp over it and have since made the same data structures in c# and PHP. I know I didn't have to, but I wanted to. Speaking with friends that are using classes and using them, they use it without understanding. My friend said I couldn't use . And had to use -> for my classes when I let him peak at my code before. I'm glad I have an understanding of it.

I don't know why he wouldn't let us use libraries outside of the ones included with the basic compiler. I plan on taking a look into the things you guys have listed. I appreciate both of your comments.

It is true, the languages are always updating, along with the machine.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10846
    • View Profile
    • development blog
    • Email
Re: Mulptile Questions (new-ish)
« Reply #5 on: November 15, 2013, 08:55:34 am »
Personally it quite freaked me out when going through C++ code of my friends at university. We were using C++ for the Datastructures and Algorithms course and as usual we never received a proper introduction (if that's even possible) to C++. Thus everyone essentially wrote C with a few C++ functions and if you saw a delete somewhere (after endless new calls) you could count yourself lucky. So yeah I kind of have to agree with binary1248, it's not the best way to get into a language and it also keeps up the illusion that being able to implement some common datastructure is going to help one to program anything, that this is the sole purpose of programming. While it's useful to know how a certain datastructure (in theory) works, it's more important to understand how the concept is adapted within the language or the standard library. For instance if I know how to implement a dynamic array but don't know how the memory is being handled with a std::vector then one has essentially missed the important parts on C++.

So it's important to know the theory behind datastructures, but that's just the very beginning, there's a lot more starting from algorithms, going over the whole STL to generic programming (templates etc), making a stop at proper OOP and finally landing on code design. It's one of the most exciting journeys for me so far, but it's not one that can be ever completed nor can one go really fast, it's slow, time consuming and often quite frustrating. ;)

Anyways I hope you'll get the hang of it and start diving into the space of endless possibilities.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

destrugter

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Mulptile Questions (new-ish)
« Reply #6 on: November 15, 2013, 01:22:11 pm »
I wasn't disagreeing with binary. I just think I do have a better understanding of how they work. Or at least, a method they might use. I had a Introduction to C++ class the semester before that, and it was taught assuming no programming experience. It's actually for Engineering majors (my school doesn't have a path for Computer Science, but my advisor told me I have to follow a similar path to the engineers for my time at community college). Maybe that's why he wouldn't let us use other libraries, I don't know.

Either way, I have some stuff to read up on. I can't wait to tackle this and many more projects. This community seems like a great environment with helpful people that know what they'Dr talking about :)

Thank you for your replies and help. I hope I can head in the right direction fairly soon. Unfortunately, I can only do stuff in my free time. With Calc this semester, Calc2 and Physics next, and my job, it's hard to find free time.

EDIT: Nexus, it seems like you misunderstood my 2nd question. I was going on about how, currently in my very premature code, I have:

window.clear();
window.draw(background);
window.draw(statusText);
window.draw(costText);
window.draw(moneyText);
window.draw(healthText);

for(int i = 0; i < 20; i++)
window.draw(turrets[i]);

if(placeTurret)
{
window.draw(cursorCircle);
window.draw(cursor);
}

window.display();
 

Now, before you go about making fun of my code, I've already stated it's very premature. I like coding stuff, and then fixing it later. Making it simple at first helps me collect my thoughts better.

Anyway, my question was, I heard doing draws like that is far from ideal. I was wondering what a better approach to drawing all of this content would be? I guess you sort of answered it, I would store all my sprites inside of a vector, and then I could just call window.draw(vector)?
« Last Edit: November 15, 2013, 04:54:36 pm by destrugter »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Mulptile Questions (new-ish)
« Reply #7 on: November 15, 2013, 05:26:02 pm »
Anyway, my question was, I heard doing draws like that is far from ideal.
Where have you heard this? And has the source also mentioned why? Don't believe such statements without questioning them :)

The drawing as you showed it is not a problem. The only thing one might remark is that, if you have a lot of such objects to draw, it could be worth putting them into an STL container, for easier organization. But that depends on your overall design, your code isn't bad per se.

A minor thing I would change is the loop
for(int i = 0; i < 20; i++)
    window.draw(turrets[i]);
First, avoid magic constants, and second, prefer iterators over indices when you iterate. Like this, it works also for containers where you don't have random access (such as linked lists).
for (std::vector<sf::Sprite> itr = turrets.begin(); itr != turrets.end(); ++itr)
    window.draw(*itr);
With C++11, you can even use the range-based for loop, which simplifies the code a lot:
for (sf::Sprite& sprite : turrets)
    window.draw(sprite);
I hope you consider this as well-intended advice and not making fun of your code ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

destrugter

  • Newbie
  • *
  • Posts: 10
    • View Profile
Re: Mulptile Questions (new-ish)
« Reply #8 on: November 15, 2013, 06:18:41 pm »
I do not consider it making fun of my code and appreciate your reply. I don't know the source, but when I searched for something along the lines of "drawing a lot of sprites" I came across a thread that was titled something like "Draw millions of sprites" and the conversation concluded that using the draw statement too much is very heavy on the program. I don't know which version of SFML they were using. I can find the thread again (maybe).

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10846
    • View Profile
    • development blog
    • Email
Re: Mulptile Questions (new-ish)
« Reply #9 on: November 15, 2013, 06:38:28 pm »
Thousands of draw calls can slowly turn into an issue yes, but you first need to get to thousands of draw calls. ;)
If you get there and run into performance issues, you can check out VertexArray.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/