Passing objects via functions
agonvs

Does anybody know if there is any way to pass instantiated objects of classes to functions either via pointer or reference, or some other way? I'm really scratching my head on this one.

jmasterx

#SelectExpand
1class Object { 2int m_var; 3public: 4void func(); 5}; 6 7//by pointer 8void someFuncP(Object* obj) { 9obj->func(); 10} 11 12//by reference 13void someFuncR(Object& obj) { 14obj.func(); 15} 16 17void someFuncV(Object obj) { 18obj.func(); 19} 20 21int main() 22{ 23 Object o; 24 someFuncP(&o); 25 someFuncR(o); 26 someFuncV(o); 27 28}

Here you have the 3 basic ways to pass an object to a function. The first way passes the pointer by value and then we dereference the pointer. In otherwords, it is like just passing the address of our object instance so we do not make a copy of it.

The second way actually passes it by reference, so we do not have to use -> we can use it like any normal object, except the object you are referring to is o. No copy is made.

The last one is passed by value. This means an entire copy of o is made and then passed to the function. This is very inefficient and not recommended unless you are passing primitive types.

After that there is also const correctness. Because you may want to pass your object in such a way that the function can only read your object's data, not modify it. But that can be for another topic, it can be a bit tricky to get right.

agonvs

I've tried all 3 methods but no matter what I do I can't get the class to recognize the object. The problem lies in line 28 in the following:

#SelectExpand
1#include"ball.h" 2#include"brick.h" 3 4void Ball::initialize(int x, int y, int velX, int velY, int width, int height, int paddleX, int paddleY, int paddleWidth, int paddleHeight, ALLEGRO_BITMAP* image, Brick& brick) 5{ 6 mX = x; 7 mY = y; 8 mVelX = velX; 9 mVelY = velY; 10 mWidth = width; 11 mHeight = height; 12 mPaddleX = paddleX; 13 mPaddleY = paddleY; 14 mPaddleWidth = paddleWidth; 15 mPaddleHeight = paddleHeight; 16 mImage = image; 17 lives = 3; 18} 19 20void Ball::update(int paddleX, int paddleY) 21{ 22 mPaddleX = paddleX; 23 mPaddleY = paddleY; 24 if(mY + 16 >= mPaddleY) 25 { 26 if (mY > 719) 27 { 28 brick.breakBrick(true); 29 mX = mPaddleX +56; 30 mY = 671; 31 mVelX = 0; 32 mVelY = -2; 33 lives--; 34 } 35 if (((mX >= mPaddleX) && (mY >= mPaddleY + 8) && (mX <= mPaddleX + 8) && (mY <= mPaddleY + 32)) || ((mX + 16 >= mPaddleX) && (mY + 16 >= mPaddleY + 8) && (mX + 16 <= mPaddleX + 8) && (mY + 16 <= mPaddleY + 32))) 36 { 37 mX = mPaddleX - 16; 38 mY = mPaddleY + 8; 39 mVelX = -8; 40 mVelY = 4; 41 } 42 else if (((mX >= mPaddleX + 120) && (mY >= mPaddleY + 8) && (mX <= mPaddleX + 128) && (mY <= mPaddleY + 32)) || ((mX + 16 >= mPaddleX + 120) && (mY + 16 >= mPaddleY + 8) && (mX + 16 <= mPaddleX + 128) && (mY + 16 <= mPaddleY + 32))) 43 { 44 mX = mPaddleX + 128; 45 mY = mPaddleY + 8; 46 mVelX = 8; 47 mVelY = 4; 48 } 49 else if (((mX >= mPaddleX) && (mY >= mPaddleY) && (mX <= mPaddleX + 128) && (mY <= mPaddleY + 8)) || ((mX + 16 >= mPaddleX) && (mY + 16 >= mPaddleY) && (mX + 16 <= mPaddleX + 128) && (mY + 8 <= mPaddleY + 32))) 50 { 51 int position = (mX + 8) - mPaddleX; 52 if (position < 16) 53 { 54 mY = 671; 55 mVelX = -12; 56 mVelY = -6; 57 } 58 else if (position < 32) 59 { 60 mY = 671; 61 mVelX = -8; 62 mVelY = -8; 63 } 64 else if (position < 48) 65 { 66 mY = 671; 67 mVelX = -4; 68 mVelY = -6; 69 } 70 else if (position < 64) 71 { 72 mY = 671; 73 mVelX = -2; 74 mVelY = -4; 75 } 76 else if (position == 64) 77 { 78 mY=671; 79 mVelX = 0; 80 mVelY = -4; 81 } 82 else if (position < 80) 83 { 84 mY = 671; 85 mVelX = 2; 86 mVelY = -4; 87 } 88 else if (position < 96) 89 { 90 mY = 671; 91 mVelX = 4; 92 mVelY = -6; 93 } 94 else if (position < 112) 95 { 96 mY = 671; 97 mVelX = 8; 98 mVelY = -8; 99 } 100 else 101 { 102 mY = 671; 103 mVelX = 12; 104 mVelY = -6; 105 } 106 107 } 108 } 109 else if (mX < 0) 110 { 111 mX = 0; 112 mVelX = -mVelX; 113 } 114 else if (mY < 0) 115 { 116 mY = 0; 117 mVelY = -mVelY; 118 } 119 else if (mX + 16 > 1279) 120 { 121 mX = 1263; 122 mVelX = -mVelX; 123 } 124} 125 126void Ball::render() 127{ 128 al_draw_bitmap(mImage, mX, mY, 0); 129 mX += mVelX; 130 mY += mVelY; 131}

Here is the main.cpp code:

#SelectExpand
1 #include<allegro5/allegro.h> 2 #include<allegro5/allegro_primitives.h> 3 #include<allegro5/allegro_image.h> 4 #include<allegro5/allegro_audio.h> 5 #include<allegro5/allegro_acodec.h> 6 #include<iostream> 7 #include"globals.h" 8 #include"paddle.h" 9 #include"ball.h" 10#include"brick.h" 11#include"entity.h" 12 using namespace std; 13 14 int main() 15 { 16 float gameTime = 0; 17 int frames = 0; 18 int gameFPS = 0; 19 if(!(al_init())) 20 cout << "Couldn't initialize Allegro." << cout; 21 if(!(al_install_joystick())) 22 cout << "Couldn't install joystick." << endl; 23 al_init_image_addon(); 24 al_init_primitives_addon(); 25 ALLEGRO_DISPLAY *display = NULL; 26 display = al_create_display(1280,720); 27 al_install_audio(); 28 al_init_acodec_addon(); 29 al_reserve_samples(1); 30 ALLEGRO_SAMPLE* sample = NULL; 31 ALLEGRO_SAMPLE_INSTANCE* instance = NULL; 32 sample=al_load_sample("The Heat Is On.ogg"); 33 instance = al_create_sample_instance(sample); 34 al_attach_sample_instance_to_mixer(instance, al_get_default_mixer()); 35 ALLEGRO_EVENT_QUEUE *event_queue = NULL; 36 event_queue = al_create_event_queue(); 37 if(!event_queue) 38 cout << "al_create_event_queue failed." << endl; 39 ALLEGRO_TIMER *timer; 40 timer = al_create_timer(1.0/60); 41 al_register_event_source(event_queue, al_get_timer_event_source(timer)); 42 gameTime = al_current_time(); 43 al_register_event_source(event_queue, al_get_joystick_event_source()); 44 ALLEGRO_JOYSTICK* joy = al_get_joystick(0); 45 ALLEGRO_JOYSTICK_STATE jst; 46 //~ buttons button; 47 direction dir = NONE; 48 bool render = false; 49 ALLEGRO_BITMAP *image = NULL; 50 ALLEGRO_BITMAP *image2 = NULL; 51 ALLEGRO_BITMAP *image3 = NULL; 52 image = al_load_bitmap("batonspritesheet.png"); 53 image2 = al_load_bitmap("chromeball.png"); 54 image3 = al_load_bitmap("brick.png"); 55 const int arraySize = 3; 56 bool alive = true; 57 Paddle paddle; 58 Brick brickPtr; 59 brickPtr.Initialize(0,0,image3); 60 Ball ball; 61 62 paddle.initialize(576, 687, 0, 0, 128, 32, image); 63 ball.initialize(632,671,0,-1,16,16,paddle.getX(),paddle.getY(),128,32,image2,&brickPtr); 64 int mX, mY; 65 al_set_sample_instance_playmode(instance, ALLEGRO_PLAYMODE_LOOP); 66 al_play_sample_instance(instance); 67 al_start_timer(timer); 68 69 while(alive) 70 { 71 ALLEGRO_EVENT ev; 72 al_wait_for_event(event_queue, &ev); 73 al_get_joystick_state(joy, &jst); 74 if (ev.type == ALLEGRO_EVENT_JOYSTICK_AXIS) 75 { 76 if ((jst.stick[2].axis[0] == 0) && (jst.stick[2].axis[1] == 0)) 77 dir = NONE; 78 if ((jst.stick[2].axis[0] == 1) && (jst.stick[2].axis[1] == 0)) 79 dir = RIGHT; 80 if ((jst.stick[2].axis[0] == -1) && (jst.stick[2].axis[1] == 0)) 81 dir = LEFT; 82 } 83 //~ if (ev.type == ALLEGRO_EVENT_JOYSTICK_BUTTON_DOWN) 84 //~ if (jst.button[9]) 85 //~ button = NINE; 86 //update 87 else if (ev.type == ALLEGRO_EVENT_TIMER) 88 { 89 render = true; 90 paddle.update(dir); 91 ball.update(paddle.getX(),paddle.getY()); 92 brickPtr.update(); 93 mX=ball.getX(); 94 mY=ball.getY(); 95 if (((mX >= 0) && (mY >= 0) && (mX <= 128) && (mY <= 32)) || ((mX + 16 >= 0) && (mY + 16 >= 0) && (mX + 16 <= 128) && (mY + 8 <= 32))) 96 { 97 brickPtr.breakBrick(true); 98 } 99 frames++; 100 if (al_current_time() - gameTime >= 1) 101 { 102 gameTime = al_current_time(); 103 gameFPS = frames; 104 frames = 0; 105 } 106 } 107 //render 108 if (render && al_is_event_queue_empty(event_queue)) 109 { 110 //~ al_set_target_bitmap(al_get_backbuffer(display)); 111 al_clear_to_color(al_map_rgb_f(0,0,0)); 112 paddle.render(); 113 brickPtr.render(); 114 ball.render(); 115 al_flip_display(); 116 } 117 if (!ball.getLives()) 118 alive = false; 119 if (jst.button[9]) 120 alive = false; 121 } 122 //~ al_destroy_bitmap(image); 123 al_destroy_timer(timer); 124 al_destroy_event_queue(event_queue); 125 return 0; 126 }

Thoughts?

jmasterx

Does Ball have a Brick ?

What does Ball.h look like?

bamccaig

Danger! Using reference member variables is a somewhat advanced usage. Typically you would do so using a constructor, not a public "initialize" method. A smart pointer might be a better option. They could come in handy for C allocations as well, such as your Allegro 5 objects. Of course, you need to crawl before you walk. I recommend that you put this project on hold and get a grip on the C++ language. Start a new code file to experiment with. Simple, short programs to just figure out the syntax. C++ is one of the most difficult languages to learn and write for. It seriously takes years just to figure out how little you know. If you try to take on too much at once you'll run into wall after wall and burn out. Take it slow. It's exhausting to just think of how to help you at this point...

Thread #615229. Printed from Allegro.cc