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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Messages - destrugter

Pages: [1]
Graphics / Re: Help with vector of Drawables
« on: November 04, 2018, 05:25:26 pm »
What about making Enemy a Drawable itself, and write window.draw(enemy), rather than adding all those accessors? That would make it much more encapsulated, readable and maintainable.

Excellent suggestion. Now I can:

Add an enemy directly to the drawables vector:
for (enemy_main::Enemy& enemy : enemies)

Draw all of my drawable objects:
for (sf::Drawable* const drawable : drawables)

Graphics / Re: Help with vector of Drawables
« on: November 04, 2018, 05:29:31 am »
You need to read up about (basic) differences between references and pointers and how to convert between them.

You could also make your Drawable references/pointers const if it's just for drawing and not for any modification (this is just nice and clean practice, it's unrelated to your problem here).

You are absolutely right. Thank you for pointing it out to me and consequently solving my issue.

Graphics / Help with vector of Drawables
« on: November 04, 2018, 04:07:17 am »
I'm trying to make a vector of all drawables so that I can make a simple render routine that just loops through all drawable objects and draws them. Here are some of the relevant bits of my code:

Declaring the vector:
std::vector<sf::Drawable*> drawables;

Looping through the enemies and adding them to the vector:
for (enemy_main::Enemy& enemy : enemies)

In my Enemy class, here are the accessors you see in the code above:
sf::Sprite& getSprite() { return sprite; }
sf::RectangleShape& getHealthRectangle() { return healthRectangle; }
sf::RectangleShape& getRedRectnagle() { return redRectangle; }

And finally the drawing:
for (auto const& drawable : drawables)

What I'm getting when I try to build are the following errors:
Error   C2440   'initializing': cannot convert from 'sf::Sprite' to 'sf::Drawable *'    TestSFML        c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\xmemory0        918

Error   C2440   'initializing': cannot convert from 'sf::Sprite' to 'sf::Drawable *'    TestSFML        c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\vector  1035   

Error   C2440   'initializing': cannot convert from 'sf::RectangleShape' to 'sf::Drawable *'    TestSFML        c:\program files (x86)\microsoft visual studio\2017\community\vc\tools\msvc\14.14.26428\include\xmemory0        918

The errors are pointing to libraries that I didn't obviously write, but the error isn't in those libraries. That much is obvious to me. What's not obvious to me is what's wrong with my code where

Window / Re: Window, possibly mouse. Possible bug when resizing
« on: June 12, 2014, 03:14:21 pm »
Thanks eXpl0it3r. Not sure how or why I missed those functions. I must have just misread or was reading too fast. This did solve the problem for anyone wondering =)

Although I do wonder if this should be done automatically by SFML when the window is resized. I could be wrong about this though.

Window / [Solved]Window, possibly mouse. Possible bug when resizing
« on: June 12, 2014, 01:57:46 pm »
I can not find anything else about this strange bug, leaving me to believe I've stumbled upon something weird. I use a simple function comparing a sprite's position to the mouse to see if the mouse is hovering over the sprite. I've found that when you resize the window, the images stretch. I do like this feature, but what I do not like about it is, the sprite's original x,y are kept and it's not updated when the window is stretched. I am getting the mouses coords relative to the window too. Here's my code for detecting if the mouse is over the sprite. Please correct any errors you see, or give me tips.

I create a RenderWindow object:
sf::RenderWindow myWindow;

I create a simple function that returns true if the mouse is within the sprite's x and y boundries and its width + height.
bool isMouseOverSprite(const sf::RenderWindow& myWindow, const sf::Sprite& Sprite)
        return  (sf::Mouse::getPosition(myWindow).x < (Sprite.getPosition().x + Sprite.getGlobalBounds().width)) && (Sprite.getPosition().x < sf::Mouse::getPosition(myWindow).x) &&
                        (sf::Mouse::getPosition(myWindow).y < (Sprite.getPosition().y + Sprite.getGlobalBounds().height)) && (Sprite.getPosition().y < sf::Mouse::getPosition(myWindow).y);

Maybe I missed something in the forums or documentation or tutorials, but this code works when my window is set to a small 900x600 window. I realize this is an odd resolution and I don't plan on keeping it. I'm in very early testing stages of a lot of things at this point. The point is, when I resize the window, the sprites report their original x,y coordinates while the mouse reports its NEW x,y coordinates.

What I'm trying to say is, the sprite class accounts for stretching, the mouse class does not. Maybe this is intended, I do not know. I may have missed a detail reading up for this issue.

General / Re: Mulptile Questions (new-ish)
« 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).

General / Re: Mulptile Questions (new-ish)
« 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:


for(int i = 0; i < 20; i++)



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)?

General / Re: Mulptile Questions (new-ish)
« 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.

General / Re: Mulptile Questions (new-ish)
« 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

General / 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.

Pages: [1]