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

Pages: [1]
1
Graphics / 'Slow' performance - what am I doing wrong?
« on: May 28, 2014, 07:17:13 pm »
So I am trying to make SFML integrate nicely with Artemis-C++ port(Entity component system).
At the moment it is using about 0.39-0.5sec to draw a frame when drawing 713 "meshes" as a tiled background.

This is obviously something I'm doing wrong so if anyone can point me in the right direction that would be great!

Edit
Meant to press preview, not save.

I know that I am calculating the transform every frame.
/Edit

Here is my current code;

Renderer
Quote
class Renderer
{
public:
   void addToQueue(MeshCollection* mc, Transform* t) {
      m_meshCollections.push_back(mc);
      m_transforms.push_back(t);
      m_count++;
   }

   void render(sf::RenderTarget* target) {
      sf::RenderStates rs = sf::RenderStates::Default;
      sf::Transform original = rs.transform;
      clock.restart();
      for (int i = 0; i < m_count; i++) {
         rs.transform = original;
         rs.transform *= Renderer::getTransform(m_transforms);;
         target->draw(*m_meshCollections, rs);
      }
      std::cout << m_count << " mesh collections drawn in " << clock.getElapsedTime().asSeconds() << "sec" << std::endl;
         
      m_meshCollections.clear();
      m_transforms.clear();

      m_count = 0;
   }

private:
   static inline sf::Transform getTransform(Transform* t) {
      float angle = -t->rotation * 3.141592654f / 180.f;
      float cosine = static_cast<float>(std::cos(angle));
      float sine = static_cast<float>(std::sin(angle));
      float sxc = cosine;
      float syc = cosine;
      float sxs = sine;
      float sys = sine;
      float tx = -0 * sxc - 0 * sys + t->position.x;
      float ty = 0 * sxs - 0 * syc + t->position.y;

      return sf::Transform(sxc, sys, tx, -sxs, syc, ty, 0.f, 0.f, 1.f);
   }

private:
   std::vector<MeshCollection*> m_meshCollections;
   std::vector<Transform*> m_transforms;
   int m_count;
   sf::Clock clock;
};


Mesh
Quote
class Mesh
{
   friend class MeshCollection;
public:
   sf::Vector2f offset;
   sf::Texture texture;
   sf::Vector2f size;
   float rotation;

private:
   sf::Vertex m_vertices[4];
   sf::Vector2f m_origin = sf::Vector2f(0, 0);

   void updateVertices() {
      sf::Vector2u size = texture.getSize();
      m_vertices[0].position = sf::Vector2f(0.0f, 0.0f);
      m_vertices[1].position = sf::Vector2f(0, size.y);
      m_vertices[2].position = sf::Vector2f(size.x, size.y);
      m_vertices[3].position = sf::Vector2f(size.x, 0);
   }

   sf::Transform getTransform() {
      float angle = -rotation * 3.141592654f / 180.f;
      float cosine = static_cast<float>(std::cos(angle));
      float sine = static_cast<float>(std::sin(angle));
      float sxc = size.x * cosine;
      float syc = size.y * cosine;
      float sxs = size.x * sine;
      float sys = size.y * sine;
      float tx = -m_origin.x * sxc - m_origin.y * sys + offset.x;
      float ty = m_origin.x * sxs - m_origin.y * syc + offset.y;

      return sf::Transform(sxc, sys, tx, -sxs, syc, ty, 0.f, 0.f, 1.f);
   }
};

MeshCollection
Quote
class MeshCollection : public sf::Drawable
{
public:
   void add(sf::Vector2f offset, sf::Texture texture, float rotation = 0.0f, sf::Vector2f size = sf::Vector2f(1.0f, 1.0f)) {
      Mesh m;
      m.offset = offset;
      m.texture = texture;
      m.rotation = rotation;
      m.size = size;
      m.updateVertices();
      m_meshes.push_back(m);
   }

   virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const {
      // sf::Transform originalTransform = states.transform;
      for (auto m : m_meshes) {
         // states.transform = states.transform.scale(m.size).rotate(m.rotation).translate(m.offset);
         states.transform *= m.getTransform();
         states.texture = &m.texture;
         target.draw((sf::Vertex*)&m.m_vertices, 4, sf::PrimitiveType::Quads, states);
         // states.transform = originalTransform;
      }
   }

private:
   std::vector<Mesh> m_meshes;
};

Transform
Quote
class Transform : public artemis::Component
   {
   public:
      sf::Vector2f position;
      float rotation;
      // size ?

      Transform() {

      }
   };

And here is my test.
Quote
int main() {

   sf::RenderWindow window(sf::VideoMode(1024, 768), "");
   Renderer r;

   sf::Texture texture;
   texture.loadFromFile("Assets\\32x32_red.png");

   const int debugsize = 50;
   MeshCollection mcArray[debugsize * debugsize];
   Transform tArray[debugsize * debugsize];
   int c = 0;
   for (int x = 0; x < debugsize; x++)
   {
      for (int y = 0; y < debugsize; y++)
      {
         MeshCollection m;
         m.add(sf::Vector2f(0, 0), texture);
         mcArray[c] = m;
         tArray[c].position = sf::Vector2f(x * 32, y * 32);
         tArray[c].rotation = 0.0f;
         c++;
      }
   }


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

      window.clear(sf::Color::Black);
      
      int c = 0;
      for (int x = 0; x < debugsize; x++)
      {
         for (int y = 0; y < debugsize; y++)
         {
            if (tArray[c].position.x > 0 && tArray[c].position.x < 1024 &&
               tArray[c].position.y > 0 && tArray[c].position.y < 768)
               r.addToQueue(&mcArray[c], &tArray[c]);

            c++;
         }
      }
      /*
      for (int i = 0; i < debugsize * debugsize; i++)
      {
         
         r.addToQueue(&mcArray, &tArray);
      }
      */
      r.render(&window);

      window.display();
   }

   return 0;
}

2
General / Character movement & physics
« on: September 20, 2013, 12:27:25 am »
Hi,

I'm in the progress of making a 2d multiplayer platformer. So the question is; how do I get some smooth movement for my actors? Right now I'm using Box2d, but it is very.. "cluncky" and "sloppy". I want more have more control over it. It feels like it takes like hours to just change the maximum movespeed. And that is after I've written nearly 800 lines of "special physics rules" just for the player actor.

So, any tips on what to do? Do I roll my own? Do I change to something like chipmunk?



And yes, I need physics -- if I remove it, it will take too much of the game experience away.

3
General / Deleting a vector of Shape pointers
« on: June 08, 2013, 04:18:18 am »
I have a std::vector<sf::Shape*> that I clear at the end of my main loop.
In the main loop a unknown number of shapes get created and drawn( ConvexShape, CircleShape, etc).

The problem is; how do I clear the vector of shapes that has been drawn and is no longer needed?
The way I'm doing it now;

Code: [Select]
while(!this->m_shapes.empty())
{
delete this->m_shapes.back();
this->m_shapes.pop_back();
}

but this gives me a unhandled exception and brings up dbgheap.c

4
Network / SFML UDP packet fragmentation
« on: February 08, 2013, 04:56:23 pm »
Does the sf::UdpSocket and sf::Packet handle packet fragmentation?

Couldn't find any data on this in the documents.

5
Graphics / Using View instead of translating all Box2d coordinates
« on: January 12, 2012, 05:31:12 pm »
Hi

I'm implenting a client-server platformer I decided to use box2d for physics.
So the server calculates the physics then sends the positions to the client which are interpolated to reduce lag, blah blah,.. But my problem is that I decided that having something like 50pixels or 64pixels = 1m i box2d. The problem then is that the client have to convert all the positions..

Soo then I tought of the sf::View. Can I use this in some way to "magically convert" all the positions getting drawn.

Code: [Select]

sf::View view(MagicGoesHere);
App.SetView(view);
while(true)
{
//rendering by just drawing the position recived from the server which
//is box2d body positions
}

6
Graphics / Can't figure out 'undefined reference'
« on: October 05, 2011, 06:59:34 pm »
Hi

Here is my code
Code: [Select]

sf::Image treeImage;
if(!treeImage.LoadFromFile("D:\\sfml\\textures\\tree_large.png"))
{
    std::cout << "[Image] Could not load 'tree_large.png'" << std::endl;
}


The problem is that CodeBlocks just spits out;

Code: [Select]

obj\Debug\main.o||In function `main':|
D:\sfml\YetAnotherTest\main.cpp|60|undefined reference to `_imp___ZN2sf5ImageD1Ev'|
D:\sfml\YetAnotherTest\main.cpp|60|undefined reference to `_imp___ZN2sf5ImageD1Ev'|
||=== Build finished: 2 errors, 0 warnings ===|


And for the life of me I can't figure out why it ain't linking  :(  :oops:

7
Feature requests / sf::Vector2 Empty value
« on: October 05, 2011, 04:18:46 pm »
Hi

I think SFML should have something like XNA vectors. A empty value. Like this(or similar):

Code: [Select]

sf::Vector2f emptyVector = sf::Vector2f::Empty;


Since it is not possible to return NULL from e, ex., search function that didn't find any result.

8
General / Help with CB and SFML2
« on: October 04, 2011, 02:43:35 pm »
Soo I downloaded the pre-built bins from this blog;
http://sfmlcoder.wordpress.com/2011/05/18/creating-a-first-sfml-project/

But when I try to compile I just get this error;
Code: [Select]
obj\Debug\main.o||In function `main':|
d:\PEngine\TestProject_SFML\main.cpp|5|undefined reference to `_imp___ZN2sf9VideoModeC1Ejjj'|
d:\PEngine\TestProject_SFML\main.cpp|5|undefined reference to `_imp___ZN2sf12RenderWindowC1ENS_9VideoModeERKSsmRKNS_15ContextSettingsE'|
d:\PEngine\TestProject_SFML\main.cpp|5|undefined reference to `_imp___ZN2sf12RenderWindowD1Ev'|
d:\PEngine\TestProject_SFML\main.cpp|15|undefined reference to `_imp___ZN2sf6Window5CloseEv'|
d:\PEngine\TestProject_SFML\main.cpp|10|undefined reference to `_imp___ZN2sf6Window9PollEventERNS_5EventE'|
d:\PEngine\TestProject_SFML\main.cpp|22|undefined reference to `_imp___ZN2sf5ColorC1Ehhhh'|
d:\PEngine\TestProject_SFML\main.cpp|22|undefined reference to `_imp___ZN2sf12RenderTarget5ClearERKNS_5ColorE'|
d:\PEngine\TestProject_SFML\main.cpp|23|undefined reference to `_imp___ZN2sf6Window7DisplayEv'|
d:\PEngine\TestProject_SFML\main.cpp|7|undefined reference to `_imp___ZNK2sf6Window8IsOpenedEv'|
d:\PEngine\TestProject_SFML\main.cpp|26|undefined reference to `_imp___ZN2sf12RenderWindowD1Ev'|
d:\PEngine\TestProject_SFML\main.cpp|26|undefined reference to `_imp___ZN2sf12RenderWindowD1Ev'|
||=== Build finished: 11 errors, 0 warnings ===|


Helpz?

9
Window / [Solved] Cant get SFML2 to work with Qt!
« on: April 05, 2011, 03:27:40 pm »
Hi

Is there a way to attach files?

Anyway, here is my current code:

QSFMLCanvas.h
Code: [Select]
#ifndef QSFMLCANVAS_H
#define QSFMLCANVAS_H

////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include <Qt/qwidget.h>
#include <Qt/qtimer.h>


////////////////////////////////////////////////////////////
/// QSFMLCanvas allows to run SFML in a Qt control
////////////////////////////////////////////////////////////
class QSFMLCanvas : public QWidget, public sf::RenderWindow
{
public :

    ////////////////////////////////////////////////////////////
    /// Construct the QSFMLCanvas
    ///
    /// \param Parent :    Parent of the widget
    /// \param Position :  Position of the widget
    /// \param Size :      Size of the widget
    /// \param FrameTime : Frame duration, in milliseconds (0 by default)
    ///
    ////////////////////////////////////////////////////////////
    QSFMLCanvas(QWidget* Parent, const QPoint& Position, const QSize& Size, unsigned int FrameTime = 0);

    ////////////////////////////////////////////////////////////
    /// Destructor
    ///
    ////////////////////////////////////////////////////////////
    virtual ~QSFMLCanvas();

private :

    ////////////////////////////////////////////////////////////
    /// Notification for the derived class that moment is good
    /// for doing initializations
    ///
    ////////////////////////////////////////////////////////////
    virtual void OnInit();

    ////////////////////////////////////////////////////////////
    /// Notification for the derived class that moment is good
    /// for doing its update and drawing stuff
    ///
    ////////////////////////////////////////////////////////////
    virtual void OnUpdate();

    ////////////////////////////////////////////////////////////
    /// Called when the widget is shown ;
    /// we use it to initialize our SFML window
    ///
    ////////////////////////////////////////////////////////////
    virtual void showEvent(QShowEvent*);

    ////////////////////////////////////////////////////////////
    /// Called when the widget needs to be painted ;
    /// we use it to display a new frame
    ///
    ////////////////////////////////////////////////////////////
    virtual void paintEvent(QPaintEvent*);

    ////////////////////////////////////////////////////////////
    // Member data
    ////////////////////////////////////////////////////////////
    QTimer myTimer;       ///< Timer used to update the view
    bool   myInitialized; ///< Tell whether the SFML window has been initialized or not
};


#endif // QSFMLCANVAS_H


QSFMLCanvas.cpp
Code: [Select]


////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include "QSFMLCanvas.h"

// Platform-specific headers
#ifdef Q_WS_X11
    #include <Qt/qx11info_x11.h>
    #include <X11/Xlib.h>
#endif


////////////////////////////////////////////////////////////
/// Construct the QSFMLCanvas
////////////////////////////////////////////////////////////
QSFMLCanvas::QSFMLCanvas(QWidget* Parent, const QPoint& Position, const QSize& Size, unsigned int FrameTime) :
QWidget       (Parent),
myInitialized (false)
{
    // Setup some states to allow direct rendering into the widget
    setAttribute(Qt::WA_PaintOnScreen);
    setAttribute(Qt::WA_NoSystemBackground);

    // Setup the widget geometry
    move(Position);
    resize(Size);

    // Setup the timer
    myTimer.setInterval(FrameTime);
}


////////////////////////////////////////////////////////////
/// Destructor
////////////////////////////////////////////////////////////
QSFMLCanvas::~QSFMLCanvas()
{
    // Nothing to do...
}


////////////////////////////////////////////////////////////
/// Notification for the derived class that moment is good
/// for doing initializations
////////////////////////////////////////////////////////////
void QSFMLCanvas::OnInit()
{
    // Nothing to do by default...
}


////////////////////////////////////////////////////////////
/// Notification for the derived class that moment is good
/// for doing its update and drawing stuff
////////////////////////////////////////////////////////////
void QSFMLCanvas::OnUpdate()
{
    // Nothing to do by default...
}


////////////////////////////////////////////////////////////
/// Called when the widget is shown ;
/// we use it to initialize our SFML window
////////////////////////////////////////////////////////////
void QSFMLCanvas::showEvent(QShowEvent*)
{
    if (!myInitialized)
    {
        // Create the SFML window with the widget handle
        Create(winId());

        // Let the derived class do its specific stuff
        OnInit();

        // Setup the timer to trigger a refresh at specified framerate
        connect(&myTimer, SIGNAL(timeout()), this, SLOT(repaint()));
        myTimer.start();

        myInitialized = true;
    }
}


////////////////////////////////////////////////////////////
/// Called when the widget needs to be painted ;
/// we use it to display a new frame
////////////////////////////////////////////////////////////
void QSFMLCanvas::paintEvent(QPaintEvent*)
{
    // Let the derived class do its specific stuff
    OnUpdate();

    // Display on screen
    Display();
}


main.cpp
Code: [Select]


////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include "QSFMLCanvas.h"
#include <Qt/qapplication.h>
#include <Qt/qframe.h>


////////////////////////////////////////////////////////////
/// Custom SFML canvas
////////////////////////////////////////////////////////////
class MyCanvas : public QSFMLCanvas
{
public :

    ////////////////////////////////////////////////////////////
    /// Construct the canvas
    ///
    ////////////////////////////////////////////////////////////
    MyCanvas(QWidget* Parent, const QPoint& Position, const QSize& Size) :
    QSFMLCanvas(Parent, Position, Size)
    {

    }

private :

    ////////////////////////////////////////////////////////////
    /// /see QSFMLCanvas::OnInit
    ///
    ////////////////////////////////////////////////////////////
    void OnInit()
    {
        // Change the background color

    }

    ////////////////////////////////////////////////////////////
    /// /see QSFMLCanvas::OnUpdate
    ///
    ////////////////////////////////////////////////////////////
    void OnUpdate()
    {

    }

    ////////////////////////////////////////////////////////////
    /// Member data
    ////////////////////////////////////////////////////////////
};


////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main(int argc, char **argv)
{
    QApplication App(argc, argv);

    // Create the main frame
    QFrame* MainFrame = new QFrame;
    MainFrame->setWindowTitle("Qt SFML");
    MainFrame->resize(400, 400);
    MainFrame->show();

    // Create a SFML view inside the main frame
    MyCanvas* SFMLView = new MyCanvas(MainFrame, QPoint(20, 20), QSize(360, 360));
    SFMLView->show();

    return App.exec();
}


My Qt Creator project file:
Code: [Select]

#-------------------------------------------------
#
# Project created by QtCreator 2011-04-05T12:51:44
#
#-------------------------------------------------

QT       += core gui

TARGET = LekeLand
TEMPLATE = app


SOURCES += main.cpp\
        widget.cpp \
    QSFMLCanvas.cpp

HEADERS  += widget.h \
    QSFMLCanvas.h

FORMS    += widget.ui

INCLUDEPATH += /home/agrualon/SFML-2.0-build/include
LIBS += /home/agrualon/SFML-2.0-build/lib/libsfml-system.so \
    /home/agrualon/SFML-2.0-build/lib/libsfml-window.so \
    /home/agrualon/SFML-2.0-build/lib/libsfml-graphics.so \
    /home/agrualon/SFML-2.0-build/lib/libsfml-audio.so \
    /home/agrualon/SFML-2.0-build/lib/libsfml-network.so


The problem is that when I build it, it returns:
Code: [Select]

collect2: id returned 1 exit status


Nothing more..

I created the project using Qt Creator, File -> New File or Project -> Qt C++ Project. In the wizard I selected QWidget as my base-class..

Any halp?

10
Feature requests / Shaders, vertex shaders?
« on: April 04, 2011, 08:46:40 pm »
Hi!

Any possibility to expand the shader class some more? Why doesn't SFML support vertex shaders? Is there ever going to be support for vertex shader?

Seems like all new games(even 2d indie games) uses some advanced "shading"..

11
Graphics / Making a shape with textures, how?
« on: April 04, 2011, 11:39:26 am »
Hi

I want to make a sf::Shape that can have like 4 different textures.. After a bit of research I realized that couldn't be done straight out of the box.

So I was thinking of doing like I did when I made my DBPro map edtior.
Have 4 textuers and a shader. Then the polygon is painted with four different colors. Each colors represents a texture.

ex:
255, 000, 000 - Grass
000, 255, 000 - Stone
000, 000, 255 - Blood
255, 255, 255 - Gravel

Then the shader would do its magic. But as far is I know this neither is doable with SFML because it only takes whole-fcking-screen shaders, right?

Sooo.. any help?

Pages: [1]
anything