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

Pages: [1] 2
1
General / Re: vector with multiple classes | Drawing order
« on: November 28, 2014, 09:25:54 pm »
I've now got std::map to work as a reference holder, by looking at the use of map in the sfml book code
resourceholder, with I allready use in my menu and map classes.

And I've placed the instantiation of Animclass map & Drawable vector
in the scene change loop so they destroy by them self.

As has be suggested.

The structure now look like this.

        file:Define_Intro.cpp
       
        // - layer 0
        { AnimCLASS tmp("Background", 0, true, true, 1,0, "textures/Art01", 0,0, 1,1);
        AnimMap.insert(std::make_pair(id, tmp));
        auto found = AnimMap.find(id);
        DrawVec.push_back(&found->second); id++}


void SceneCLASS::SetUp(AudioCLASS &audio, MouseCLASS &mouse, int SN
       , std::vector<DrawableCLASS*> &DrawVec, std::map<int,AnimCLASS> &AnimMap )
{
        int id = 0;
        if (SN==Intro){ #include "Define_Intro.cpp" }
        //
        //
        for (auto& item : DrawVec) {
                item->Init();
                if(item->GetClickPoint().Enable) {
                // push Dynamic clickpoints
                    CPSceneVec.push_back(item->GetClickPoint() );
                }
        }

};



void GameCLASS::Start()
{       bool exit_loop = false; int SceneNumber = 0;
        do{
            if( SCENE.NextScene == SceneNumber)
            {
                std::map<int,AnimCLASS> AnimMap;
                std::vector <DrawableCLASS*> DrawableVec;
                SCENE.SetUp(Audio,Mouse,SceneNumber,DrawableVec, AnimMap);
                Run(DrawableVec, SceneNumber);
            }
        SceneNumber++;
        if(SceneNumber>=NumberofScenes) SceneNumber=0;
        if(SCENE.NextScene == 99) exit_loop=true;
        }while(!exit_loop);
};

int main()
{
    GameCLASS GAME;
    GAME.SetUp();
    GAME.Start();
    return 0;
}

 


2
General / Re: vector with multiple classes | Drawing order
« on: November 26, 2014, 10:24:25 pm »
Anyway, I've now gone away from static instantiation of the Anim class to dynamic storing them in a vector.
and then the DrawAbleVector get a reference too that. To get that to work I had to use vector.reserve()
without it after the 2nd. push the vector resized and chanced memory location and the ref was invalid.


reserve() should only be a performance optimization; relying on it for correct behavior like this means you're misusing std::vector.  In this case, you appear to be assuming your std::vector will never exceed a certain fixed size, so you might as well use a std::array (or raw array) to actually enforce a fixed size if that's what you want.  Or use a node-based container like std::list or std::map if you want variable size and stable references.  Or use only one container to store all your drawable objects so this reference validation issue can't even come up.  Or have two containers that are mutually exclusive (one for animations and one for the other drawables) so one doesn't have to find a way to reference the other.  Any of these would probably be an improvement.

Reading in The C++ Programming Language  page 898 STL containers - size and capacity:
Quote
When changing the size or capacity, the elements may be moved to new storage location.
That implies that iterators(and pointers and references) to elements may become invalid(i.e. points to the old element location.
[..]
It is tempting to assume that reserve() improves performance, but the standard growth strategies for vector are
so effective that performance is rarely a good reason to use reserve().
Instead, see reserve() as a way of increasing the predictability of performance and for
avoiding invalidation of iterators.


In C++ for game programmers page 206:
There is directly instructions to use vector.reserve() to avoid relocation and invalidation of pointer and references.

So I'm considering going back to using a vector since reserve() does not change to value reported by
.size() where a std::array[20] report a size of 20 no matter if I actually use 20 or not.

3
General / Re: vector with multiple classes | Drawing order
« on: November 23, 2014, 04:21:56 am »
Everything works great and as expected,
And since I had an array filled with anim classes anyway there where no reason to use
the block scope and temporary instantiation of the anim class.



const uint32_t MAX_ANIM_ARRAY =20;

class Scene {

 public:
        std::vector<DrawableCLASS*> DrawVec;
        std::array<AnimCLASS, MAX_ANIM_ARRAY> DrawAnimArray;
        void Define(int SN);
        void Setup()  {
               for (auto& item : DrawVec)
                item->Init();
        }
        void Render(sf::RenderWindow &window) {
            for (auto& item : DrawVec)
                item->Render(window);
        }
}

Scene::Define(int sn) {
         uint32_t index=0;
       
   if(sn==Scene_Intro) {

        // - layer 0
        DrawAnimArray[index].Define("Background", 0, true, true, 1,"textures/Art01", 0,0, 1.0f,1.0f,0);
        DrawVec.push_back(&DrawAnimArray[index]);
        index++;

        // - layer 1
        DrawAnimArray[index].Define("Forground", 0, true, true, 1,"textures/Art02", 0,0, 1.0f,1.0f,0);
        DrawVec.push_back(&DrawAnimArray[index]);
        index++;

        // - layer 2
        DrawAnimArray[index].Define("Actor sprite", 0, true, true, 1,"textures/Actor02", 0,0, 1.0f,1.0f,0);
        DrawAnimArray[index].SetClickPoint(true, true, "Dynamic", "ActorName", "ACTOR_YES"  ,0 , "Cursor_Green", 748,380, 48, 72);
        DrawVec.push_back(&DrawAnimArray[index]);
        index++;
   
     if(index> MAX_ANIM_ARRAY) throw std::Kitchen::Sink;
    }
};
 

I played with the refrence wrapper and got it to work by going though an extra function to un-wrap before it could be rendered, so if I really wanted to get rid of  std::vector<DrawableCLASS*> DrawVec; I could use that.

4
General / Re: vertexArray.append()
« on: November 18, 2014, 10:20:37 pm »
What do you mean? You can use the sf::Lines primitive directly with sf::VertexArray...
I mean I draw a some lines in Illustrator export it as svg, eps or what ever,
convert it to sf::lines/VertexArray and render it in sfml.
SFML does not support vector graphics. To load such files, you have to find a different library or parse it yourself. There have been some posts in the forum that might help you.

How do you get 1000 vertices into a VertexArray
Read the documentation ::)

Using Libra office Draw app: a line drawn with snap to grid at 10,10 to 20,20
end up exported as svg looking like this I see
     <g class="com.sun.star.drawing.LineShape">
      <g id="id3">
       <path fill="none" stroke="rgb(0,0,0)" d="M 1000,1000 L 2000,2000"/>
 

And a poly like this:

     <g class="com.sun.star.drawing.PolyPolygonShape">
      <g id="id3">
       <path fill="none" stroke="rgb(52,101,164)" d="M 1000,1000 L 3000,1000 3000,1500 2500,1500 2500,2000 2000,2000 2000,1500 1500,1500 1000,1000 Z"/>
 

Looks straight forward...  ;D

 

5
General / Re: vertexArray.append()
« on: November 18, 2014, 09:18:12 pm »
What do you mean? You can use the sf::Lines primitive directly with sf::VertexArray...

I mean I draw a some lines in Illustrator export it as svg, eps or what ever,
convert it to sf::lines/VertexArray and render it in sfml.

How do you get 1000 vertices into a VertexArray


6
General / Re: vector with multiple classes | Drawing order
« on: November 18, 2014, 12:54:16 pm »
This code cannot work.
When Scene::Define is called, your Anim object is created and pushed into the vector, but when the function ends, your Anim pointer will become invalidated and your program will most likely crash.

Its working alight.
The short lived static instance of AnimCLASS get killed at the end of the block scope,
after the initialized object has been copied into the AnimArray
DrawableVector takes a pointer to a Drawable Class, and can't handle the object by value.

And remember that I got many section with the same name:
Scene::Define {

{// layer 1
        AnimCLASS Anim("Forground 1",true,true,"textures/Art03");
        DrawAnimArray[index]=Anim;
        DrawVec.push_back(&DrawAnimArray[index]);
        index++; }

{// layer 2
        AnimCLASS Anim("Forground 2",true,true,"textures/Art03");
        DrawAnimArray[index]=Anim;
        DrawVec.push_back(&DrawAnimArray[index]);
        index++; }
}
 

So if Anim lasted to the end of Scene::Define scope there would be name conflicting.

I tried this std::vector<DrawableCLASS> DrawVec but could not get it to work with.


7
General / Re: vertexArray.append()
« on: November 17, 2014, 09:24:42 pm »
Has anyone made a converter or utility so vector lines from a drawing program can be displayed with a vertexArray ?

8
General / Re: vector with multiple classes | Drawing order
« on: November 17, 2014, 08:46:44 pm »
Anyway, I've now gone away from static instantiation of the Anim class to dynamic storing them in a vector.
and then the DrawAbleVector get a reference too that. To get that to work I had to use vector.reserve()
without it after the 2nd. push the vector resized and chanced memory location and the ref was invalid.

Quote
This is a major "code smell", i.e. it implies there's a significant design flaw in this code you'll probably regret later (no matter how small this program is supposed to be). Yes, it's bad enough to be worth bringing up despite everything that's been said above.
reserve() should only be a performance optimization; relying on it for correct behavior like this means you're misusing std::vector.  In this case, you appear to be assuming your std::vector will never exceed a certain fixed size, so you might as well use a std::array (or raw array) to actually enforce a fixed size if that's what you want.  Or use a node-based container like std::list or std::map if you want variable size and stable references.  Or use only one container to store all your drawable objects so this reference validation issue can't even come up.  Or have two containers that are mutually exclusive (one for animations and one for the other drawables) so one doesn't have to find a way to reference the other.  Any of these would probably be an improvement.

True, I've now chanced it to std::array size 20, so its workable for now, until I learn some more and change it to something better, safer and cleaner.

I'm expecting to use 1-12 anim classes simultaneously

Otherwise I'm open to better ways of doing it.


const uint32_t MAX_ANIM_ARRAY =20;

class Scene {

 public:
        std::vector<DrawableCLASS*> DrawVec;
        std::array<AnimCLASS, MAX_ANIM_ARRAY> DrawAnimArray;
        void Define(int SN);
}

Scene::Define(int sn) {
         uint32_t index=0;
       
   if(sn==Scene_Intro) {

        {// layer
        AnimCLASS Anim("Forground 1",true,true,"textures/Art03");
        DrawAnimArray[index]=Anim;
        DrawVec.push_back(&DrawAnimArray[index]);
        index++; }
    }
};
 

9
General / Re: vector with multiple classes | Drawing order
« on: November 16, 2014, 07:17:43 pm »
Even if do-while was somehow inferior, it's a very tiny and irrelevant issue compared to all the other things we could say about that code.  Ignoring what's already been said, there's still:

- His main loop renders stuff, then handles keyboard input, then actually displays the rendered stuff.  This is a strange and confusing order in which to do those operations (and it's probably doing something bad like forcing all input to lag one frame).

- He's using real-time input when he probably wants events.  The Escape to close behavior in particular is almost certainly better as an event.

- The "exit" sentinel variable appears to contain no special meaning or information.  He could have just as easily used while(true) {} with break;, or the SFML convention while(window.isOpen()) {} with window.close();, depending on whether or not he wants a "quick exit" from the loop.  Or the do-while equivalents of those.  None of these requires declaring an additional variable at the top of main().

- He's not using any Vector2s or Vector3s in his drawable classes, forcing him to set each x,y,z coordinate individually rather than group them together.

- He's giving his classes Init() methods instead of proper constructors.

- His classes need to be explicitly "enabled" and made "visible".  Is there a reason the classes aren't like this by default after construction?

We should be pointing out stuff like this instead of arguing about do-while.

You could, and I'm sure there would be something to learn from it, but it would be misdirected
and way out of proportion, when considering the purpose of the testbech code.

Anyway, I've now gone away from static instantiation of the Anim class to dynamic storing them in a vector.
and then the DrawAbleVector get a reference too that. To get that to work I had to use vector.reserve()
without it after the 2nd. push the vector resized and chanced memory location and the ref was invalid.

So now I have perfect drawing order control from the scene definition file,
and can move the blocks up and down the layer stack without altering any code.
And I've started making overloaded constructors for the basic stuff, and I have removed
a huge amount of boiler plate code - 'I think it's called', as a benefit from the effort of restructuring the code.

Old class Scene{
 public:
    AnimCLASS Anim_layer1
...
    AnimCLASS Anim_layer8


New class Scene{
 public:
        std::vector<DrawableCLASS*> DrawVec;
        std::vector<AnimCLASS> DrawAnimVec;


Scene.Define(int SN) {

         if(SN==Scene_Intro) {
         uint32_t index=0;

        { // Full screen Background single frame pos 0,0 scale 1,1
        AnimCLASS Anim(true,true,"textures/Art01");
        DrawAnimVec.push_back(Anim);
        DrawVec.push_back(&DrawAnimVec[index]);
        index++; }

           // Fog -old C function/class on the todo list..
           WX.FOG.SetEnable(true);
            WX.FOG.SetVisible(true);
            WX.FOG.SetBandsColor(205,205,205,30);
            WX.FOG.NumOfBands           = 50 ;// 50 optimal
            WX.FOG.BandYmin             = -300;
            WX.FOG.BandYmax             = 600;
            WX.FOG.BandSize                     = 2;
            WX.FOG.BandSpeed            = 2;
            DrawVec.push_back(&WX.FOG);

        {// Actor sidding
        AnimCLASS Anim(true,true,"textures/girl4");
        Anim.SetPos(808,463);
        Anim.SetScale(0.18,0.18);
        Anim.SetClickPoint(true, true, "Dynamic", "Aixy", "ACTOR_NO"  ,0 , "Cursor_Red", 808,463, 50, 88);
        DrawAnimVec.push_back(Anim);
        DrawVec.push_back(&DrawAnimVec[index]);
        index++; }

        {// Fire animation 24 frames
        AnimCLASS Anim(true,true,"textures/Particles/Fireplace");
        Anim.SetFrameCount(23);
        Anim.SetCounterType(UP_Counter);
        Anim.SetPos(484,394);
        Anim.SetScale(0.26,0.26);
        Anim.SetColor(255,255,245,115);
        Anim.SetAnimationSpeed(0.6);
        Anim.SetLayerName("Fire");
        AnimVec.push_back(Anim);
        DrawVec.push_back(&DrawAnimVec[index]);
        index++; }

 

10
General / Re: vector with multiple classes | Drawing order
« on: November 14, 2014, 09:41:39 pm »
do{
// ...
} while(!exit);
`do while` is considered bad style. You should consider using normal `while` instead.

do while is guaranteed to run at least once, even if the condition is always false. A standard while loop will not run at all if the condition is false.

What seams logic to me is using
while(!Exit){
}
if Exit can be activated before the the loop, and
do{
}while(!Exit);
if it is only activated inside





11
General / Re: vector with multiple classes | Drawing order
« on: November 14, 2014, 09:12:29 pm »
Mission accomplished, darn nice to go from this static drawing order and mess

void Render(sf::RenderWindow *window)
{            
            Layer_1_Anim.Render(window);
            WX.Rings.Render(window);
            Layer_2_Anim.Render(window);
            Layer_3_Anim.Render(window);
            WX.Fog.Render(window);
            Layer_4_Anim.Render(window);
            Particle.Render(window);
            Particle2.Render(window);
            WX.Render(window);
            WX.Cloud.Render(window);
            WX.RAIN.Render(window);
            FireAnim.Render(window);
            Text.Render(window);
            Layer_5_Anim.Render(window);
            Layer_6_Anim.Render(window);
            Layer_7_Anim.Render(window);
            Layer_8_Anim.Render(window);
 };
 

To this dynamic beauty..

void Render(sf::RenderWindow *window)
 {
        for (auto& item : DrawVec) {
                item->Render(window);
        }
}    
 

12
General / Re: vector with multiple classes | Drawing order
« on: November 14, 2014, 08:44:18 pm »
Otherwise I agree. Rasmussen, you should also choose a naming convention and apply it consistently. I would especially avoid CAPS_LOCK style except for macros and maybe constants. And you should definitely read again the SFML tutorials about event handling. Don't mix realtime input with events.

And please use C++ code tags, not quotes.

I'm in the creative phase, I'll beautified the code on days when I'm not on fire so to speak.

"Don't mix" this:
 
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) { exit=true; }
with this:
 
WINDOW.pollEvent(event);

?

In the real main loop the mouse uses the event.

Roger on the tags.

13
General / Re: vector with multiple classes | Drawing order
« on: November 14, 2014, 08:14:28 pm »
CloudCLASS Clouds;
Drawable * DrawCloud;
DrawCloud = &Clouds;
Scene.push_back(DrawCloud);
You don't need to create a pointer in this case. Just do this:

CloudCLASS Clouds;
Scene.push_back(&Clouds);

Roger!, thanks.

Quote
do{
// ...
} while(!exit);
`do while` is considered bad style. You should consider using normal `while` instead.

Can't see why at the moment, but I keep it in mind.

Quote
WINDOW.pollEvent(event);
Why are you polling for events when you don't handle any events?

The window froze and got greyed out like style and the default windows 7 mouse cursor
chanced to rotating busy, without it in the testbench code, even while using sf::sleep.

Quote
for(uint32_t i=0; i<Scene.size(); i++)
            Scene[!]->Render(&WINDOW);
You have put an exclamation mark instead of i in the subscript operator.
And in any case you should use range based for-loop when iterating all the elements of a container, like this:
for (auto& item : container) {
    // do something with item
}

That was on purpose , "[ i ]" seam to vanish, I thought it was a forum BB code or something.

Thanks for the suggestions.

14
General / Re: vector with multiple classes | Drawing order
« on: November 14, 2014, 01:52:47 am »
And made into using a vector, so now I have exactly what I was looking for.

Quote
int main()
{
    bool exit=false;
    sf::Event event;
    sf::RenderWindow WINDOW(sf::VideoMode(800, 600), "SFML window");

    std::vector<Drawable*> Scene;

    CloudCLASS Clouds;
    Drawable * DrawCloud;
    DrawCloud = &Clouds;
    Clouds.SetEnable(true);
    Clouds.SetVisible(true);
    Clouds.NumberOfClouds   = 120;
   Clouds.Xmin            = -8000;
   Clouds.Xmax            = 10000;
   Clouds.Ymin            = -6000;
   Clouds.Ymax            = 100;
   Clouds.Zval            = 1000;
   Clouds.Xpos            = 0;
   Clouds.Ypos            = -200;
   Clouds.Speed         = 8;
   Clouds.Size            = 110;
   Clouds.SetColor(255,255,255,255);
    Clouds.Init();
    Scene.push_back(DrawCloud);

    AnimCLASS Anim;
    Drawable * DrawAnim;
    DrawAnim =  &Anim;
    Anim.SetEnable(true);
    Anim.SetVisible(true);
    Anim.SetImageFileName("textures/menu");
    Anim.Init();
    Scene.push_back(DrawAnim);

    do{
        WINDOW.pollEvent(event);
        for(uint32_t i=0; i<Scene.size(); i++)
            Scene[!]->Render(&WINDOW);
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) { Clouds.Speed = 20;}
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) { exit=true; }
        WINDOW.display();
        sf::sleep(sf::milliseconds(10));
    } while(!exit);

    return 0;
}


15
General / Re: vector with multiple classes | Drawing order
« on: November 14, 2014, 12:58:45 am »
I just got this working, and it is testing out great.

I see chapter one in C++ for Game programmers is
Inheritance - polymorph - virtual function ...
and says is very important to learn and for the rest of the book ,
I promise I will look into that soon.  ;D

Quote

class Drawable {
public:
   virtual void Render(sf::RenderWindow*) = 0;
    virtual ~Drawable(){};
};

class AnimCLASS : public Drawable {};
class CloudCLASS: public Drawable {};

int main()
{
    bool exit=false;
    sf::RenderWindow WINDOW(sf::VideoMode(800, 600), "SFML window");

    AnimCLASS Anim;
    Anim.SetEnable(true);
    Anim.SetVisible(true);
    Anim.SetImageFileName("textures/Art01");
    Anim.Init();

    CloudCLASS Clouds;
    Clouds.SetEnable(true);
    Clouds.SetVisible(true);
    Clouds.NumberOfClouds           = 120;
    Clouds.Xmin            = -8000;
    Clouds.Xmax            = 10000;
    Clouds.Ymin            = -6000;
    Clouds.Ymax            = 100;
    Clouds.Zval            = 1000;
    Clouds.Xpos            = 0;
    Clouds.Ypos            = -200;
    Clouds.Speed                 = 8;
    Clouds.Size            = 110;
    Clouds.SetColor(255,255,255,255);
    Clouds.Init();

    Drawable * DrawAnim;
    Drawable * DrawCloud;

    DrawAnim =  &Anim;
    DrawCloud = &Clouds;

    Drawable* SCN[2] = { DrawAnim, DrawCloud };

    do{
    SCN[0]->Render(&WINDOW);
    SCN[1]->Render(&WINDOW);
    WINDOW.display();
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) { Clouds.Speed = 20;}
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) { exit=true; }
    sf::sleep(sf::milliseconds(10));
    } while(!exit);
    return 0;
}

Pages: [1] 2