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
- open display, create, and initialize everything, pixmax, colormap and image, etc...
- get file name to load into image to be set, load file into image,which is what this switch is doing,
- 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?