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 - NGM88

Pages: 1 ... 9 10 [11]
151
General / Re: vector of Drawable AND Transformable
« on: June 02, 2017, 06:22:52 am »
That's because you're trying to push_back unique_ptr<Dude> while World expects to have something convertible to sf::Drawable&. Just do this:
World.push_back(*Dudes[0]);

I get an "Exception thrown: read access violation." error.

am I not supposed to have anything else in the base class?

152
General / Re: vector of Drawable AND Transformable
« on: June 01, 2017, 11:47:36 pm »
You don't need to have Dude::Draw function, because RedDude::draw function will be called because it's virtual.

Thanks a lot for the fast replies. I still get the same error at World.push_back though.

153
General / Re: vector of Drawable AND Transformable
« on: June 01, 2017, 11:41:31 pm »
Here's some code showing where I'm stuck.

What I'm trying to do is first put various derived classes of Dude (such as RedDude here) into a vector to sort them by position, and then put everything from vector into a vector of sf::Drawables (along with some other stuff like the background which I don't want mixed with the "Dudes") so that I can draw them.

Problem is I don't really understand what to put into the base class:

#include <SFML/Graphics.hpp>
#include <iostream>
#include <memory>
#include <vector>
#include <algorithm>
#include "AssetManager.h"

class Dude : public sf::Drawable, public sf::Transformable
{
public:

   void Draw(sf::RenderTarget& target, sf::RenderStates states) const
   {
      states.transform *= getTransform();
      target.draw(*THING, states);
   }

private:

   std::unique_ptr<sf::Drawable> THING;
};

class RedDude : public Dude
{
public:
   bool load()
   {
      red_dude = sf::Sprite(AssetManager::GetTexture("Media/red_dude.png"));
      red_dude.setTextureRect(sf::IntRect(0, 0, 96, 140));
      red_dude.setScale(1, 1);
      red_dude.setOrigin(48, 124);

      return true;
   }

   virtual void draw(sf::RenderTarget& target, sf::RenderStates states)const
   {
      states.transform *= getTransform();
      states.texture = &red_dude_tex;
      target.draw(red_dude, states);
   }

   sf::Sprite red_dude;
   sf::Texture red_dude_tex;
};

std::vector<std::unique_ptr<Dude>> Dudes;
std::vector<std::reference_wrapper<const sf::Drawable>> World;

int main()
{
   sf::RenderWindow window(sf::VideoMode(800, 600), "SFML");

   std::unique_ptr<RedDude> d1 = std::make_unique<RedDude>();

   d1->load();
   d1->setPosition(50,50);

   Dudes.push_back(std::move(d1));
   World.push_back(std::move(Dudes[0]));

   while (window.isOpen())
   {
      sf::Event event;
      while (window.pollEvent(event))
      {
         if (event.type == sf::Event::Closed)
            window.close();
      }

      window.clear();
      window.draw(World[0]);
      window.display();
   }

   return 0;
}

-------

trying to compile this code gives me an error at this line:

World.push_back(std::move(Dudes[0]));

... says that argument type doesn't match the object type.

154
General / Re: vector of Drawable AND Transformable
« on: June 01, 2017, 07:26:16 pm »
   void Draw(sf::RenderTarget& target, sf::RenderStates states) const
   {
      states.transform *= getTransform();
      target.draw(SOMETHING??, states);
   }

this is what I have so far. I'm not sure how to get that SOMETHING part.

155
General / Re: vector of Drawable AND Transformable
« on: June 01, 2017, 07:20:23 pm »
Maybe you should create a class DrawableTransformable : public sf::Drawable, sf::Transformable and inherit your classes from it, and then make vector of std::reference_wrapper of DrawableTransformable.

trying that at the moment, but can't quite figure out the definition of the virtual draw function.

156
General / vector of Drawable AND Transformable
« on: June 01, 2017, 07:06:44 pm »
Hi, I'm trying to create a vector for objects from VARIOUS classes that all inherit from Drawable AND Transformable.

if I use

std::vector<std::reference_wrapper<const sf::Drawable>>

then I can't get their position with getPosition()

and if I use

std::vector<std::reference_wrapper<const sf::Transformable>>

then I can't draw them.

I need to be able to do both (getPosition AND draw).


157
General / Isometric Draw Order
« on: May 23, 2017, 07:59:44 pm »
Hey guys,

I'm making a tile-based isometric game (Diablo style) and I'm trying to tackle draw order.

At first I accomplished this by giving all entities a Z value depending on how many tiles there were between them and the player on the Y axis. With the game screen height being 1080p and my tile height being 60p, this resulted my dividing the screen into 18 "rows" or layers to be drawn from top to bottom. I then needed to repeat the following code 18 times with each entity:

if (random_entity.get_Z() == 18)
window.draw(random_entity);

This was crude and unmaintainable, so I looked around and found this: https://github.com/SFML/SFML/wiki/Tutorial:-Drawable-Group. What I did then was instead create 18 of these Group classes from the tutorial, add a "m_drawables.clear();" function to it and I constantly clear and push_back entities into these groups.

I then finally realized that I wouldn't have to use 18 different groups if I could somehow sort the entities according to their Y positions since whatever's higher on the screen should get drawn first anyway. Unfortunately I have no idea how to even begin implementing this.

From what I understand, the Group class uses a reference_wrapper to store the addresses of sf::drawables; and you use the push_back function to add drawables to it. I know that if it were a simple vector of ints I could just do:

std::sort(m_drawables.begin(), m_drawables.end());

... but it's a vector of objects and I have no idea how to sort the Y or Z values of these sf::drawables.

I hate asking others for code, but in this case at least we could add it to the tutorial. I'm a self-taught c++ enthusiast and I'm learning SFML as I make this game, so I really appreciate any help you can give me.

158
General / Re: Problem moving the character to mouse coords
« on: April 06, 2017, 04:39:30 pm »
Solved for now, thank you for all the replies.

Made an if loop to check positions. Previously tried the same thing with a while loop but it kept crashing for some reason.

159
General / Re: Problem moving the character to mouse coords
« on: April 06, 2017, 01:32:53 pm »
My values are correct because when I use a simple "player.setPosition" instead of the code I posted in my original post, the player is relocated to the exact position where I click.

160
General / Re: Problem moving the character to mouse coords
« on: April 06, 2017, 06:56:25 am »
ok so let's say the player starts at position 0,32 ...

I click on 192,128 to move the player

that makes the magnitude 214.663 (it's 4 isometric tiles away)

PlayerSpeed is 100f

it just keeps going!

I'm using 128x64 isometric tiles,
my "test player" is a circle with a radius of 10, centered on a tile
since it's grid based, the minimum distance to move is never less than a whole tile.

161
General / Problem moving the character to mouse coords
« on: April 05, 2017, 11:25:12 pm »
Hi folks

I'm making a grid based diablo-like game and I have a question to ask:

currently I'm using this for moving the player from tile to tile:

sf::Vector2f direction = sf::Vector2f(mouse.x, mouse.y) - player.getPosition();
float magnitude = sqrt((direction.x * direction.x) + (direction.y * direction.y));
sf::Vector2f unitVector(direction.x / magnitude, direction.y / magnitude);
player.move(unitVector * PlayerSpeed * elapsed_time.asSeconds());

the problem is - the player doesn't stop moving! ever!

I tried putting the last line in an if loop like - if player position != mouse coords - but couldn't make it work.

How do I make the player stop once it reaches the clicked tile (mouse.x and y)?


162
General / Can't get SFML to work in codeblocks
« on: January 21, 2017, 09:41:41 pm »
Here's what I have done so far, step by step:

1- downloaded codeblocks-16.01mingw-setup and installed it. codeblocks is working fine.

2- downloaded GCC 4.9.2 TDM (SJLJ) - 32-bit and put it in C:\SFML-2.4.1

3- created an empty project in codeblocks

4- went to Project -> Build Options.

selected the project name from the left menu so that the settings are global (applying to both Debug and Release).

added "C:\SFML-2.4.1\include" under Search Directories -> Compiler

added "C:\SFML-2.4.1\lib" under Search Directories -> Linker

said No to "Keep this as relative path?" on both occasions.

5- selected Debug from the left menu.

went to Linker Settings, added:

sfml-graphics-s-d
sfml-window-s-d
sfml-system-s-d

6- selected Release from the left menu.

went to Linker Settings, added:

sfml-graphics-s
sfml-window-s
sfml-system-s

7- selected the project name from the left menu (global settings)

went to Compiler Settings -> #defines and typed SFML_STATIC in the box.

8- clicked File -> New -> Empty File. Clicked Yes to add the file to the active project.

named it main.cpp and pasted the test code from http://www.sfml-dev.org/tutorials/2.4/start-cb.php into it.

When I run the program I get a ton of "undefined reference to ..." errors. too many to post here.

SFML website says I need to add the opengl32, freetype, jpeg, gdi32 and winmm dependencies under Linker Settings.

I tried this ordering for Debug and the same ordering without "-d"s for Release, but it DIDN'T WORK:

sfml-graphics-s-d
sfml-window-s-d
opengl32
freetype
jpeg
gdi32
sfml-system-s-d
winmm

in what EXACT ORDER do I need to add them?

Also, do I need to add other specific search directories for these to work?

Pages: 1 ... 9 10 [11]
anything