Jump to content

  • Log In with Google      Sign In   
  • Create Account

black_darkness

Member Since 10 Dec 2012
Offline Last Active Feb 28 2013 03:16 AM

Posts I've Made

In Topic: Request feedback on my drawing algorithms.

25 January 2013 - 11:23 AM

Just at a quick glance...

 

Alot of your functions are returning a new copy of the vector passed in. This could be causing a big slow down. Eg,

vector<Entity> init_map_house_entity(vector<Entity> &mve) 
{
    /*do stuff to mve*/
    return mve
}

 

Instead I would work on the vector passed in by reference. This avoids newing memory and copy operations. Eg,

void init_map_house_entity(vector<Entity> &mve) 
{
    /*do stuff to mve*/
}

 

This may gain some speed as well... instead of calling .size() on each iteration, store the size before entering the loop. I say you "may" gain speed here because the compiler could be doing some optimizing on its own anyway, but it's something to try... So instead of the following...

void Game_system::make_map() 
{
    mv.resize(wid_height[0]*wid_height[1]);
    for(int i=0;i<mv.size();i++) 
    {
         /*do stuff*/
    }
    
    /*do more stuff*/
}

 

Try...

void Game_system::make_map() 
{
    int arraySize = wid_height[0]*wid_height[1];
    mv.resize(arraySize);
    for(int i=0;i<arraySize;i++) 
    {
         /*do stuff*/
    }
    
    /*do more stuff*/
}

 

And with the functions such as:

void walk_animation(int,int,int,int,int,int,int,int,int);

 

It's faster to pass a single struct by reference then it is to pass lots of values. But with that said, you'd probably not be able to measure the optimization in this case cause your not passing large objects. Just something to keep in mind I guess. 

 

Thank you I will fix this. Great find. I kept missing those.


In Topic: Request feedback on my drawing algorithms.

25 January 2013 - 11:21 AM

Have you tried profiling of you code?

 

If you do that, especially when you experience the slow down, you will get nice statistics how much time is spent in each function as well as the accumulated sum of all functions called. Do profiling is very important before you start optimizing, or you may spend your time on things that have negligible effects.

 

Otherwise, as Nyssa stated, making copies of vectors looks inefficient.

 

I just got access to Visual Studio premium I think using Microsoft Dreamspark. I will profile my code soon and check which functions and methods are slowing it down.


In Topic: I have two functions that do the same thing. Which one is faster?

03 January 2013 - 01:32 AM

Okay another question. I am implementing your suggestions but I have a problem. I do not know if it would be efficient for me to include the data type Map as a variable in Entity.
The best way is to use a reference (it doesn't copy the data of object, but you use it in same way as an object, unlike a pointer). Making a reference a class member is a bit tricky (requires initializer lists), but this code does it:
//file Entity.h
#pragma once
//forward declaration of a map
//in C++, you can declare a class many times,
//but define it only once!
class Map;
class Entity {
  public:
    //you have to make a constructor with an initialization list
    //because the reference, unlike a pointer, must be defined at its initialization
     Entity(Map & _map) : map(_map) {
      //your constructor, blah blah blah
    }
    //note you can also use Entity(Map & map) : map(map) {...}, it has the same effect
    //(refer to footnote 1)
    void foo();
    protected:
      Map & map;
};

//file Map.h
#pragma once
#include "Entity.h"
class Map {
  void bar();
  //you can pass the current object as a parameter to a function using this
  Entity thisMapEntity() {
    return Entity(this);
  }
};

//file Entity.cpp
#include "Entity.h"
#include "Map.h"
Entity::foo() {
  //note you couldn't define foo in Entity.h,
  //because class Map wasn't defined yet,
  //just declared 

  //calls the entity's map's function "bar"
  map.bar();
}
Footnote 1

Thank you. This is good stuff.


In Topic: I have two functions that do the same thing. Which one is faster?

02 January 2013 - 07:33 AM

Please read a book about C++. You are calling a METHOD (as opposed to function). A method belongs to an object. Therefore, you can access a pointer to object you are calling your method from using keyword 'this'. So you can write

void Entity::turning(char facing1, vector<Tile> mv) {
    if (this->is_swing_hoe == false) {
                            if(this->facing != facing1) {
            this->facing = facing1;            this->wait_time = 0;
        }
        else if (this->can_pass(this->facing,mv,this)&& this->wait_time > 3) {
            this->frame = 1;            this->move_animation = true;
        }
    }
}
and 'this' will correspond to 'hero' if you call it like
hero->turning('d',mv);
Even better, you can remove all the 'this->' from above code. Your compiler knows that in methods, if you write a name of a variable you declared in the class of the object, that is the variable to use (if you didn't write a conflicting declaration in the method, of course).

You also should not pass a std::vector variable by value (that means, not using a pointer or, preferably, a reference). Why? Because your program needs to make a new copy of that vector. That means it must manually copy all the objects (in your case, tiles) your std::vector stores.
In fact, if you think about it, what should the Entity know? It should know on which map it is and its position. If you implement the Map class (which should probably have a vector of tiles), you can use something like
void Entity::turning(char facing1) {        
//not sure what THIS code does, but it is yours, so I won't modify it (except showing the 'OOP way')
    if (is_swing_hoe == false) {
        if(facing != facing1) {
            facing = facing1;
            wait_time = 0;
        }
        //suppose that map can extract all the needed information from the entity using getters
         else if (map->can_pass(this, facing1)&& wait_time > 3) {
            frame = 1;
            move_animation = true;
        }
    }
}
Voila! The function is much more readable. As a rule of thumb, you should always think twice before starting working on a new feature. How will I represent it? Who (which class) will use it? How should I implement it? If you ask and answer all the questions that come to your mind, you will find yourself writing much cleaner (and bug-free) code.

 

 

Okay another question. I am implementing your suggestions but I have a problem. I do not know if it would be efficient for me to include the data type Map as a variable in Entity.


In Topic: I have two functions that do the same thing. Which one is faster?

02 January 2013 - 05:48 AM

Please read a book about C++. You are calling a METHOD (as opposed to function). A method belongs to an object. Therefore, you can access a pointer to object you are calling your method from using keyword 'this'. So you can write

 

I did read a book but I started getting confused so I started making this game so I could get some experience in order to understand the book better. This was a really helpful post thank you.


PARTNERS