Thread: Route in c language

  1. #1
    Registered User LísiasDeCastro's Avatar
    Join Date
    Aug 2013
    Location
    Brasil/São Paulo
    Posts
    27

    Route in c language

    Hey guys. I am buiding an engine upon sdl2. It is going well.
    But there is a function that does not seem as fine as the rest of the library. I call this function object_route. It checks the coordenates from the object and keeps them going until the x_destine and y_destine to be reached. It works into the bloody project.... but with strang behavior. It stops until the traget to be reached. Ah, the method that I am using to get the coordenates is the mouse coordenate. If anyone could help me out, checking for logic mistakes I would be real thankfull. Here it lays the code.

    Code:
    STATUS        object_route(PLACE * to,OBJECT * object,int x,int y){
      if(to==NULL||to->map==NULL||object==NULL)return(Off);
      if(object->x==x&&object->y>y){
        object->where = NORTH;
        object->y    -= object->y_speed;
        if(object->y<=y){
          object->where = NOWHERE;
          object->y     = y;
          return(On);
          }
        }
      else if(object->x==x&&object->y<y){
        object->where = SOUTH;
        object->y    += object->y_speed;
        if(object->y>=y){
          object->where = NOWHERE;
          object->y     = y;
          return(On);
          }
        }
      else if(object->x<x&&object->y==y){
        object->where = EAST;
        object->x    += object->x_speed;
        if(object->x>=x){
          object->where = NOWHERE;
          object->x = x;
          return(On);
          }
        }
      else if(object->x>x&&object->y==y){
        object->where = WEST;
        object->x    -= object->x_speed;
        if(object->x<=x){
          object->where = NOWHERE;
          object->x = x;
          return(On);
          }
        }
      else {
        if(object->x>x&&object->y>y){
          object->where = NORTHWEST;
          object->x    -= object->x_speed;
          object->y    -= object->y_speed;
          if(object->x<=x&&object->y<=y){
            object->where = NOWHERE;
            object->x     = x;
            object->y     = y;
            return(On);
            }
          if(object->x<=x){object->x = x;}
          if(object->y<=y){object->y = y;}
          }
        else if(object->x<x&&object->y>y){
          object->where = NORTHEAST;
          object->x    += object->x_speed;
          object->y    -= object->y_speed;
          if(object->x>=x&&object->y<=y){
            object->where = NOWHERE;
            object->x     = x;
            object->x     = y;
            return(On);
            }
          if(object->x>=x){object->x = x;}
          if(object->y<=y){object->y = y;}
          }
        else if(object->x<x&&object->y<y){
          object->where = SOUTHEAST;
          object->x    += object->x_speed;
          object->y    += object->y_speed;
          if(object->x>=x&&object->y>=y){
            object->where = NOWHERE;
            object->x     = x;
            object->y     = y;
            return(On);
            }
          if(object->x>=x){object->x = x;}
          if(object->y>=y){object->y = y;}
          }
        else if(object->x>x&&object->y<y){
          object->where = SOUTHWEST;
          object->x    -= object->x_speed;
          object->y    += object->y_speed;
          if(object->x<=x&&object->y>=y){
            object->where = NOWHERE;
            object->x     = x;
            object->y     = y;
            return(On);
            }
          if(object->x<=x){object->x = x;}
          if(object->y>=y){object->y = y;}
          }
        }
      if(object->x==x&&object->y==y){
        object->where = NOWHERE;
        return(On);
        }
      return(Off);
      }
    Last edited by LísiasDeCastro; 01-18-2015 at 08:08 PM.

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    I recommend using better indentation first off. Each inner block should have a level of indentation separate than it's enclosing braces.

    Secondly I also recommend using more spacing in your condition statements. As it is now things like object->y>y is really terse and can easily be confused which leads to mistakes and is harder to maintain if you look back on this code later.

    As far as design, you have a clear pattern in your conditions. You should remove the inner pieces of each condition and turn it into a generic function or even a macro you can pass the appropriate options to for the specified direction.

    Here is a trivial example although you should probably write a function that can handle the SOUTHEAST, etc.. directions as well which this macro does not:
    Code:
    #define MOVE_OBJ(obj, axis, dir, op) do{\
        obj->where = dir;                   \
        obj->axis op obj->axis##_speed;     \
        if (obj->axis <= axis) {            \
            obj->where = NOWHERE;           \
            obj->axis = axis;               \
            return On;                      \
        }                                   \
    }while(0)
        if(object->x == x && object->y > y) {
            MOVE_OBJ(object, y, NORTH, -=);
        }   
        else if(object->x == x && object->y < y) {
            MOVE_OBJ(object, y, SOUTH, +=);
        }   
        else if(object->x < x && object->y == y){ 
            MOVE_OBJ(object, x, EAST, +=);
        }   
        else if(object->x > x && object->y == y){ 
            MOVE_OBJ(object, x, WEST, -=);
        }
    The point is to make the logic very clear to read without being polluted with a bunch of repeated code. So you separate the two. Now you can look at the logic (the if statements) and the grunt work code (the MOVE_OBJ macro) separately and debug/maintain them separately.

  3. #3
    Registered User LísiasDeCastro's Avatar
    Join Date
    Aug 2013
    Location
    Brasil/São Paulo
    Posts
    27

    Thanks for reply

    Thanks for the replying nonpuz.
    I've got the idea of the better organization of the code.
    But I use to write the code that way for to long. When I
    put the code in here, I'll try to concatenate it the way you
    can read it better. I have a function named object_x_set,
    which catchs the object and moves it, looking for the place's limit.
    But I don't like using it that much, because to call a funtion inside the the other can slow down the application's progress. I already have a flag named axis. It is inside the object, but it takes care to the rotation of the image. So it won't be that easy to adaptate it in my algorithm. I would like only doing the call of the pointer, without functions inside. I know I am in the right way, but there is something returning out before the right time. :/
    I am postion the struct from the object in order you to see my variables.

    Code:
    typedef struct OBJECT{
      long int         id;
      char          *  name;
      int                width   ,width_offset   ,height  ,height_offset  ,
                          x       ,x_offset       ,y       ,y_offset       ,
                          x_axis  ,x_axis_offset  ,y_axis  ,y_axis_offset  ,
                          flip;
      double           width_zoom ,width_zoom_offset ,
                          height_zoom,height_zoom_offset,
                           x_speed,x_speed_offset ,y_speed ,y_speed_offset,
                           angle,angle_offset;
      COLOR       *   color;
      COMPASS        where;
      SDL_Texture * image;
      SOUND        * sound;
      TEXT           * text;
      TIMER         * timer;
      struct OBJECT * preview,* next;
      }OBJECT;

  4. #4
    Registered User LísiasDeCastro's Avatar
    Join Date
    Aug 2013
    Location
    Brasil/São Paulo
    Posts
    27
    I've got it to work.
    This way the function worked properly.
    Thanks for everyone.... and one more step
    building my nice engine. I've tested more than
    100.000 sprites in a single map and it worked.
    With music, sound effect, text and internet connection.
    There was a logic mistake into my mouse's event in the main.
    And when I fixed it the function worked this way.(I'm not sure if the other way it works, but I guess it worked too)
    Here it is the code:

    Code:
    STATUS        object_route(PLACE * to,OBJECT * object,int x,int y){
      if(to==NULL||to->map==NULL||object==NULL)return(Off);
      if(object->x>x&&object->y>y){
        object->where = NORTHWEST;
        object->x    -= object->x_speed;
        object->y    -= object->y_speed;
        if(object->x<=x&&object->y<=y){
          object->where = NOWHERE;
          object->x     = x;
          object->y     = y;
          return(On);
          }
        if(object->x<=x){object->x = x;}
        if(object->y<=y){object->y = y;}
        return(Off);
        }
      else if(object->x<x&&object->y>y){
        object->where = NORTHEAST;
        object->x    += object->x_speed;
        object->y    -= object->y_speed;
        if(object->x>=x&&object->y<=y){
          object->where = NOWHERE;
          object->x     = x;
          object->x     = y;
          return(On);
          }
        if(object->x>=x){object->x = x;}
        if(object->y<=y){object->y = y;}
        return(Off);
        }
      else if(object->x<x&&object->y<y){
        object->where = SOUTHEAST;
        object->x    += object->x_speed;
        object->y    += object->y_speed;
        if(object->x>=x&&object->y>=y){
          object->where = NOWHERE;
          object->x     = x;
          object->y     = y;
          return(On);
          }
        if(object->x>=x){object->x = x;}
        if(object->y>=y){object->y = y;}
        return(Off);
        }
      else if(object->x>x&&object->y<y){
        object->where = SOUTHWEST;
        object->x    -= object->x_speed;
        object->y    += object->y_speed;
        if(object->x<=x&&object->y>=y){
          object->where = NOWHERE;
          object->x     = x;
          object->y     = y;
          return(On);
          }
        if(object->x<=x){object->x = x;}
        if(object->y>=y){object->y = y;}
        return(Off);
        }
      else {
        if(object->x==x&&object->y>y){
          object->where = NORTH;
          object->y    -= object->y_speed;
          if(object->y<=y){
            object->where = NOWHERE;
            object->y     = y;
            return(On);
            }
          return(Off);
          }
        else if(object->x==x&&object->y<y){
          object->where = SOUTH;
          object->y    += object->y_speed;
          if(object->y>=y){
            object->where = NOWHERE;
            object->y     = y;
            return(On);
            }
          return(Off);
          }
        else if(object->x<x&&object->y==y){
          object->where = EAST;
          object->x    += object->x_speed;
          if(object->x>=x){
            object->where = NOWHERE;
            object->x = x;
            return(On);
            }
          return(Off);
          }
        else if(object->x>x&&object->y==y){
          object->where = WEST;
          object->x    -= object->x_speed;
          if(object->x<=x){
            object->where = NOWHERE;
            object->x = x;
            return(On);
            }
          return(Off);
          }
        }
      if(object->x==x&&object->y==y){
        object->where = NOWHERE;
        return(On);
        }
      return(Off);
      }
    Last edited by LísiasDeCastro; 01-21-2015 at 12:35 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. best route to learning c++?
    By rodrigorules in forum C++ Programming
    Replies: 14
    Last Post: 02-14-2011, 07:26 PM
  2. Best route to the result
    By letmein in forum C Programming
    Replies: 15
    Last Post: 09-23-2010, 02:24 PM
  3. How to store best route using A*
    By pawikan in forum C++ Programming
    Replies: 6
    Last Post: 12-14-2009, 04:10 AM
  4. change route
    By quantt in forum Linux Programming
    Replies: 5
    Last Post: 08-06-2009, 02:53 AM
  5. Replies: 15
    Last Post: 01-14-2003, 11:30 PM

Tags for this Thread