Thread: "good programming" what is it? adding classes and struct and functions and switches

  1. #1
    Banned
    Join Date
    Aug 2017
    Posts
    861

    "good programming" what is it? adding classes and struct and functions and switches

    I've never used a class. Just wondering if it'd be a good programming idea to stick a class within my program, or not. I cannot think of a reason to use a class or classes throughout this entire program. it was only when I took it to doing this, what I am doing now that the question came to mind.

    this is a personal project. I am just fiddling with adding new and different things it can do. Right now It displays (sets) the image on the background (desktop) in which ever way available to the user I have coded.

    This one particular function I am doing. It can change the image at any given interval while moving it across he screen. Be it the same image or a different image.

    As of now ( in testing / modifying this function to move image wherever) as much as I have written. It is only moving the image down and to the right off the screen, then back again in the opposite direction.

    I want to make it more random. Then I thought of how that ball game works, I'd be the same algorithm to get the image to "bounce" across the screen with each timed update. So instead of figuring it out. I you tubed it. found one that is the pong game using a class. I've never done a class per se' . so why not seems like a good idea for now.

    While following along in how to write the class. The question came to mind how to attach it to the Imlib2 Image which would be a struct if anything. Then move that, as I have not even gotten that far yet. Thought I'd just do a preemptive strike on this and ask before getting there.

    My code I am using now is this. it is Using a struct to keep the values of x,y in a safe place so they do not get reset to zero or whatever each time that function gets called. it is called within a switch and uses It works off an enum to know which case ( mode) to go to. It is in a loop that is within a timer , that is where the loop is at. So each time the time is up it calls this case's main function, this
    case gets picked again then it runs the switch again to add to or subtract from the values which causes the image to move then be set to the the background
    in a different area.

    using the same basic set up this class is using. where here wee see an enum and struct, with functions outside of the the struct to use what is in the struct.

    as most should see the (basic) change between struct and class is. The class is a "gloried struct". where the class uses its own functions. ( and added whatever, mute point)

    this loop is a generalization.
    I do not think the enum needs to be posted,
    Code:
    loop 
    while ( time is ticking)
    {
    switch(mode)
    {
    
     case MOVEIMAGE:
            
            switch (mi.flag)
            {    // starts at 0,0 upper left hand corner 
                // moves it down and to the right
                case 0:
                    mi.x += 130;
                    mi.y += 100;
    
                    if(mi.x == img.screenW || mi.y == img.screenH)
                    { // resets the tip of image to exact upper left corner
                        // to screen lower right hand corner
                        // just off screen
                        mi.x = img.screenW;
                        mi.y = img.screenH;
                        mi.flag = 1; // sets flag to go cause the image 
                                 // to go back up, using the other case
                    }
                    break;
                case 1: // starts image moving back up diagonally 
                    mi.x -= 150;
                    mi.y -= 100;
                    if (mi.x == 0 || mi.y == 0)
                    { //resets it to exact upper left corner
                        mi.x = 0;
                        mi.y = 0;
                        mi.flag = 0; // sets flag to move image back down again
                    }
                        break;
                    default:
                        break;
                    }
    
            // source_x,source_y,source_width,source_height, 
            // destination_x, destination_y, destination_width, destination_height
             imlib_blend_image_onto_image (imgdata.image, 0, 0, 0, w, h, mi.x, mi.y, w, h);
            break;
    }
    wait_time_up
    }
    In order to get a more random effect from this, that is where I got the idea to use the Ole' that ball that bounces off the walls of a square program that so many of use have seen idea.

    xx being the right side of an object whereas x is the left side cord. of the same object.

    When xx hits the right side of the wall being the max length of window, it changes directions. randomly. if ran fast enough one sees it bounces all over the place.

    Basically, that is my general idea to get the image to just be displayed "wherever" on the screen. slowing moving around the screen each time it updates.

    when I seen this pong gaming being done in C++, I said why not try using a class for this instead of a struct? then the question came to mind how do I interact with the class and the image (struct) object?

    this is only for one part of a function it would be used in as well. what problems might that cause?

    the steps to get the desktop and image setup to have an image set to it is a three step process but a lot of function calls.
    X11
    1. open display, create, and initialize everything, pixmax, colormap and image, etc...
    2. get file name to load into image to be set, load file into image,which is what this switch is doing,
    3. set image, free whatever needs freeing.


    the class I copied off youtube, before getting into modifying it in any way is this.

    Code:
    
    enum eDir {STOP = 0, LEFT = 1, UPLEFT = 2, DOWNLEFT = 3, RIGHT = 4, UPRIGHT = 5, DOWNRIGHT = 6};
    
    class mvImage
    {
        private:
            int x,y;
            int orginalX, orginalY;
            eDir direction;
        public:
            mvImage(int Xpos, int Ypos)
            {
                orginalX = Xpos;
                orginalY = Ypos;
                x = Xpos;
                y = Ypos;
                direction = STOP;
            }
            void reset()
            {
                x = orginalX;
                y = orginalY;
                direction = STOP;
            }
            void changeDirecton(eDir d)
            {
                direction = d;
            }
            void randomDirection()
            {
                direction = (eDir)((rand() % 6 ) + 1);
            }
            inline int getX() { return x; }
            inline int getY() { return y; }
            inline eDir getDirection() { return direction; }
            void move()
            {
                switch(direction)
                {
                    case STOP:
                        break;
                    case LEFT:
                        x--;
                        break;
                    case RIGHT:
                        x++;
                        break;
                    case UPLEFT:
                        x--; y--;
                        break;
                    case DOWNLEFT:
                        x--; y++;
                        break;
                    case UPRIGHT:
                        x++; y++;
                        break;
                    case DOWNRIGHT:
                        x++; y++;
                        break;
                    default:
                        break;
                }
            }
    };
    The getX, getY to set the image, modify the switch within the class to make it give a greater values changes in the x,y, then do if statements, maybe even a switch, to check where it is at then use the direction functions to change directions. pretty basic.

    Basically the question is, Is that cray coding to just slap in a class just for this one use, within the entire program or what? I know it (has to be) is feasible but is it "good programming"?

    where I probably could figure out how to do all of this with a struct to keep the values from changing back (being reset) to the original start 0,0 wherever the function is called, and a switch.

    if done like this.

    where one should know each time that function gets called the x,y it gets reset
    each time it is called, hence the need for a struct to separate the data values from the function, keeping them protected from being changed back each
    time a function set up like this would do each time it is called.

    (the same basic function I am using without all of the different image
    settings in it using that switch to determine which one to send it to. )

    Code:
    while ( it keeps calling it again and again)
    
    void function(char *filename, int screenW, int screenH)
    {
    
     int w,h;
    int x = 0,y = 0;
    image = load_image(filename);
    w = image_size;
    h = image_size:
    
    /*
     the switch would be here, all of that information gotten
     for use for all of the other function calls to imlib2 function
     to prepare the image for setting....
    */
    
    
    // it could never increment down the screen.
    x = x + 150; 
    y = y  + 100;
    // it would just keep being set to x = 150, y = 100; 
    
      imlib_blend_image_onto_image (image, 0, 0, 0, w, h, x, y, w, h);
    }/ / end function
    
    } //end loop
    so I am wondering if using a class to do this instead of a struct, all for the sake of using a class without any real known benefits of a class over a struct , especially in this case. would it be good programming, and worth my time to further work on the code using a class to get more random movement out of the image than coding it with a struct?

  2. #2
    Citizen of Awesometown the_jackass's Avatar
    Join Date
    Oct 2014
    Location
    Awesometown
    Posts
    269
    Use an dx and dy (ie change in x and y for a given time interval) value, much more general. Randomly set initial position and velocity values and update ball position every given time interval. When a wall is hit, reverse the sign of the normal component. (Example, if vertical wall is hit then dx=-dx)
    Last edited by the_jackass; 10-25-2017 at 10:15 AM.
    "Highbrow philosophical truth: Everybody is an ape in monkeytown" --Oscar Wilde

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by the_jackass View Post
    Use an dx and dy (ie change in x and y for a given time interval) value, much more general. Randomly set initial position and velocity values and update ball position every given time interval. When a wall is hit, reverse the sign of the normal component. (Example, if vertical wall is hit then dx=-dx)
    then just apply all of that to an rectangle image. sweet!

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by userxbw View Post
    I've never used a class. Just wondering if it'd be a good programming idea to stick a class within my program, or not.
    ...
    The class is a "gloried struct". where the class uses its own functions.
    ...
    the class I copied off youtube, before getting into modifying it in any way is this.
    Don't copy something that doesn't make sense for your problem, and don't use classes just because someone said they're a good idea. Start with simple classes that help simplify notation or help you describe operations on multiple pieces of data together. For example, when you manipulate foo_x and foo_y together, that is a class in disguise. Give it a name and use it as one thing:

    Code:
    struct Point {
        int x;
        int y;
        Point(int x_, int y_) : x(x_), y(y_) {}
        void move(eDir);
    };
    In C++ classes the functionality need not be member functions. For example you could define move as a free function if you like.

    Code:
    // Free function
    void move(Object*, eDir);
    
    // Member function
    struct Object {
       /* ... */
       void move(eDir);
    };
    In one style, you would write move(anObject, aDirection) and in the other you would write anObject.move(aDirection).

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by c99tutorial View Post
    Don't copy something that doesn't make sense for your problem, and don't use classes just because someone said they're a good idea. Start with simple classes that help simplify notation or help you describe operations on multiple pieces of data together. For example, when you manipulate foo_x and foo_y together, that is a class in disguise. Give it a name and use it as one thing:

    Code:
    struct Point {
        int x;
        int y;
        Point(int x_, int y_) : x(x_), y(y_) {}
        void move(eDir);
    };
    In C++ classes the functionality need not be member functions. For example you could define move as a free function if you like.

    Code:
    // Free function
    void move(Object*, eDir);
    
    // Member function
    struct Object {
       /* ... */
       void move(eDir);
    };
    In one style, you would write move(anObject, aDirection) and in the other you would write anObject.move(aDirection).
    the class was copied with the knowledge and understanding that it can be changed to suit my needs if used. I would/will have been using it as a template.

    the separation of functions: calling an object into a function. then is / would it not then be written within that function in the same manner to manipulate the members within it as if it was an objects function using the dot to connect the object to its member?
    Code:
    void function(obj *a, eDir dr)
    {
      if ( (int)dr == 3)
      a.Move(3,5);
     }
    the classes are glorified struct was more of a loosely lack of a better way of putting it, extension or expansion of a struct, because if you look at programming as a whole the upper level ones, you'll see, At least I see how at the start using a data type. Which is just a name to describe something , its properties using a word.

    The word char is short for character. it is used for a letter. an int is short for integer, a whole number not a fraction. the point being that it holds information. The basic idea just keeps getting built upon.

    A place that givens means to store information. The bigger the place the more information can be stored within it. one char. one letter, make a char array, many chars, many letters, a word is born.

    someone came up with the idea of making an char array to hold many array's now a sentence can be written by printing out the arrays within the main array, then someone else came alone and suggest "why don't you just make that one array longer?

    That way it can hold all of them words in just one array. A fight broke out, and for many years a battle was fraught over which way was best until a man called Gulliver showed up and said, why not just use both? Then just leave it up to the programmer on what to do?

    They both seen the errors of the rigid thought process. Then the battle over which way is better was settled. Not only was storing a string inside one array born out of that settlement, but a means for more flexibility. (of course that is only a fable.)

    But it shows how programming uses what it started with and just builds upon it, 8 bit to 16 bit to 32 bit to 64 bit to 128 bit ....

    Anyways, point being, ( I may have even lost the point(er) ) is that Programming as an hierarchy from one char now all the way up to a class, or would the top of the hierarchy in programming be an Object? Keeping the flexibility that was born out of fable of the battle over an array and how to use it. It keeps its flexibility of having functions for manipulating it being able to be written within the class declaration as well as outside of it. As you have demonstrated. but,
    Member functions can (and should) be used to interact with data
    contained within user defined types. User defined types provide
    flexibility
    in the "divide and conquer" scheme in program writing
    C++ Programming/Classes/Member Functions - Wikibooks, open books for an open world


    anyways, that Point function makes sense and it is in a struct example, I'll be needing to look into that functionality of a struct more.

    Thanks for your input. an Idea has been born..
    Last edited by userxbw; 10-26-2017 at 07:52 AM.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    the separation of functions: calling an object into a function. then is / would it not then be written within that function in the same manner to manipulate the members within it as if it was an objects function using the dot to connect the object to its member?
    There is a point though if you want to add something to a class, and cannot change its interface, for whatever reason. For example: you did not compile the class yourself.

    It can also be nice to do if you have an operation in a program made up of smaller operations.
    Code:
    void nap(Wombat& w) {
       w.move();
       w.sleep(1200);
    }
    There is also a point to be said about encapsulation; that if you keep the interface tight it will be easier to debug. The string class is an excellent example of a class with a potential encapsulation problem. It has a lot of dot methods that could have been written as free functions (like find()).

    It's always up to the programmer though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-06-2015, 10:23 PM
  2. Marking functions as "Local" or "Global"
    By zyxwvuts in forum C Programming
    Replies: 10
    Last Post: 02-22-2014, 07:58 AM
  3. Good books on getting a "Programming mindset"?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 04-08-2008, 09:04 AM
  4. An interesting code about "struct" in "union"
    By meili100 in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2008, 04:37 AM
  5. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM

Tags for this Thread