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.


Topics - destrugter

Pages: [1]
1
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)
{
  drawables.emplace_back(enemy.getSprite());
  drawables.emplace_back(enemy.getRedRectnagle());
  drawables.emplace_back(enemy.getHealthRectangle());
}

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)
{
  myWindow.draw(*drawable);
}

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

2
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.

3
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]