Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » C++ is changing the value of ALLEGRO_COLOR when adding to a vector.

This thread is locked; no one can reply to it. rss feed Print
C++ is changing the value of ALLEGRO_COLOR when adding to a vector.
Darren Hoehna
Member #15,297
September 2013

Hi all,

I made a Ship class that stores the bitmap and the ALLEGRO_COLOR is stored inside the Ship object.

I have a collection of Ships in a vector and the problem is that, once I push the newly created ship onto the vector, the ALLEGRO_COLOR changes to really low negative values.

I bet this is a C++ thing, but I can't figure out why this is happening.

Here is the lines of code where I am testing this out:

#SelectExpand
1Ship ship(SHIP_WIDTH, SHIP_HEIGTH, ship_X_Position, ship_Y_Position); 2ship.setColor(255, 0, 255); 3shipCollection.push_back(ship);

The color of the ship is correct. But when I access the color of this ship from shipCollection[0] the color value are messed up.

What does that?

Ship.h

#SelectExpand
1#pragma once 2#ifndef SHIP_H 3#define SHIP_H 4#include<iostream> 5#include<allegro5\allegro.h> 6#include<allegro5\allegro_native_dialog.h> 7#include <string> 8class Ship 9{ 10public: 11 Ship(int width, int heigth, float upperLeft_X_Position, float upperLeft_Y_Position); 12 ~Ship(void); 13 Ship(const Ship &thisShip); 14 ALLEGRO_BITMAP* getImage(); 15 void printShip(); 16 void setColor(int red, int green, int blue); 17 ALLEGRO_COLOR getColor(); 18 int width; 19 int heigth; 20 float upperLeft_X_Position; 21 float upperLeft_Y_Position; 22 23private: 24 ALLEGRO_BITMAP* bitmap; 25 ALLEGRO_COLOR shipColor; 26 void displayErorMessage(const char* message) const; 27}; 28 29#endif

Ship.cpp

#SelectExpand
1#include "Ship.h" 2 3 4Ship::Ship(int width, int heigth, float upperLeft_X_Position, float upperLeft_Y_Position) : width(width), heigth(heigth), 5 upperLeft_X_Position(upperLeft_X_Position), upperLeft_Y_Position(upperLeft_Y_Position){ 6 bitmap = al_create_bitmap(this->width, this->heigth); 7 if(bitmap == NULL) 8 displayErorMessage("Can't make bitmap."); 9 } 10 11 Ship::~Ship(void) { 12 al_destroy_bitmap(bitmap); 13 } 14 15 Ship::Ship(const Ship &thisShip) { 16 this->upperLeft_X_Position = thisShip.upperLeft_X_Position; 17 this->upperLeft_Y_Position = thisShip.upperLeft_Y_Position; 18 this->width = thisShip.width; 19 this->heigth = thisShip.heigth; 20 bitmap = al_create_bitmap(thisShip.width, thisShip.heigth); 21 if(bitmap == NULL) 22 displayErorMessage("Can't copy bitmap."); 23 } 24 25 void Ship::setColor(int red, int green, int blue) { 26 if(blue <= -1 || green <= -1 || red <= -1 || blue >= 256 || green >= 256 || red >= 256) { // invalid numbers 27 shipColor = al_map_rgb(0, 0, 0); 28 } 29 else { 30 shipColor = al_map_rgb(red, green, blue); 31 } 32 } 33 34 ALLEGRO_COLOR Ship::getColor() { 35 return shipColor; 36 } 37 38 ALLEGRO_BITMAP* Ship::getImage() 39 { 40 return bitmap; 41 } 42 43 void Ship::printShip() { 44 std::cout << "upperLeft_X_Position: " << upperLeft_X_Position << " "; 45 std::cout << "upperLeft_Y_Position: " << upperLeft_Y_Position << std::endl; 46 } 47 48 void Ship::displayErorMessage(const char* message) const { 49 ALLEGRO_DISPLAY* errorMessage = al_create_display(300, 300); 50 al_show_native_message_box(errorMessage, "Ship Error", "Ship Error", message, NULL, ALLEGRO_MESSAGEBOX_ERROR); 51 al_destroy_display(errorMessage); 52 }

Source.cpp

#SelectExpand
1#include<allegro5\allegro.h> 2#include<allegro5\allegro_native_dialog.h> 3#include <stdio.h> 4#include <vector> 5#include"Ship.h" 6 7 8 9enum KeyDirections { Up, Down, Left, Right }; 10int SCREEN_WIDTH = 200; 11int SCREEN_HEGTH = 200; 12int SHIP_WIDTH = 10; 13int SHIP_HEIGTH = 10; 14double FPS = 1.0 / 60.0; 15float SHIPS_MOVEMENT = 4.0; 16int main(int argc, char **argv) 17{ 18 19 20 std::vector<Ship> shipCollection; 21 float ship_X_Position = SCREEN_WIDTH / 2; 22 float ship_Y_Position = SCREEN_HEGTH / 2; 23 ALLEGRO_DISPLAY* mainDisplay = NULL; 24 ALLEGRO_TIMER* screenRefreshTimer = NULL; 25 ALLEGRO_EVENT_QUEUE* eventQueue = NULL; 26 bool KeysPressed[4] = { false, false, false, false }; 27 bool redraw = true; 28 bool doExit = false; 29 30 if (!al_init()) { 31 al_show_native_message_box(mainDisplay, "Title", "Heading", "Text", NULL, ALLEGRO_MESSAGEBOX_ERROR); 32 return 0; 33 } 34 35 Ship ship(SHIP_WIDTH, SHIP_HEIGTH, ship_X_Position, ship_Y_Position); 36 ship.setColor(255, 0, 255); 37 shipCollection.push_back(ship); 38 std::cout << ship.getColor().r << std::endl; 39 std::cout << shipCollection[0].getColor().r << std::endl; 40 mainDisplay = al_create_display(SCREEN_WIDTH, SCREEN_HEGTH); 41 42 if (mainDisplay == NULL) { 43 al_show_native_message_box(mainDisplay, "Title", "Heading", "Text", NULL, ALLEGRO_MESSAGEBOX_ERROR); 44 return 0; 45 } 46 47 if (!al_install_keyboard()) { 48 al_show_native_message_box(mainDisplay, "Title", "Heading", "Text", NULL, ALLEGRO_MESSAGEBOX_ERROR); 49 al_destroy_display(mainDisplay); 50 } 51 52 screenRefreshTimer = al_create_timer(FPS); 53 if (screenRefreshTimer == NULL) { 54 al_show_native_message_box(mainDisplay, "Title", "Heading", "Text", NULL, ALLEGRO_MESSAGEBOX_ERROR); 55 al_destroy_display(mainDisplay); 56 return 0; 57 } 58 59 60 eventQueue = al_create_event_queue(); 61 if (eventQueue == NULL) { 62 al_show_native_message_box(mainDisplay, "Title", "Heading", "Text", NULL, ALLEGRO_MESSAGEBOX_ERROR); 63 al_destroy_display(mainDisplay); 64 al_destroy_timer(screenRefreshTimer); 65 return 0; 66 } 67 al_set_target_bitmap(al_get_backbuffer(mainDisplay)); 68 69 al_register_event_source(eventQueue, al_get_timer_event_source(screenRefreshTimer)); 70 al_register_event_source(eventQueue, al_get_display_event_source(mainDisplay)); 71 al_register_event_source(eventQueue, al_get_keyboard_event_source()); 72 al_clear_to_color(al_map_rgb(0, 0, 0)); 73 al_flip_display(); 74 al_start_timer(screenRefreshTimer); 75 76 while (!doExit) { 77 ALLEGRO_EVENT thisEvent; 78 al_wait_for_event(eventQueue, &thisEvent); 79 if (thisEvent.type == ALLEGRO_EVENT_TIMER) { 80 for(int index = 0; index < shipCollection.size(); index++) 81 { 82 if (KeysPressed[Up] /*&& ship.upperLeft_Y_Position >= 4.0*/) { 83 shipCollection[index].upperLeft_Y_Position -= SHIPS_MOVEMENT; 84 } 85 86 if (KeysPressed[Down] /*&& ship.upperLeft_Y_Position <= 200 - 10 - 4.0*/) { 87 shipCollection[index].upperLeft_Y_Position += SHIPS_MOVEMENT; 88 } 89 90 if (KeysPressed[Left] /*&& ship.upperLeft_X_Position >= 4.0*/) { 91 shipCollection[index].upperLeft_X_Position -= SHIPS_MOVEMENT; 92 } 93 94 if (KeysPressed[Right] /*&& ship.upperLeft_X_Position <= 200 - 10 - 4.0*/) { 95 shipCollection[index].upperLeft_X_Position += SHIPS_MOVEMENT; 96 } 97 } 98 99 redraw = true; 100 } 101 else if (thisEvent.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { 102 break; 103 } 104 else if (thisEvent.type == ALLEGRO_EVENT_KEY_DOWN) { 105 switch (thisEvent.keyboard.keycode) { 106 case ALLEGRO_KEY_UP: 107 KeysPressed[Up] = true; 108 break; 109 case ALLEGRO_KEY_DOWN: 110 KeysPressed[Down] = true; 111 break; 112 case ALLEGRO_KEY_LEFT: 113 KeysPressed[Left] = true; 114 break; 115 case ALLEGRO_KEY_RIGHT: 116 KeysPressed[Right] = true; 117 break; 118 } 119 } 120 else if (thisEvent.type == ALLEGRO_EVENT_KEY_UP) { 121 switch (thisEvent.keyboard.keycode) { 122 case ALLEGRO_KEY_UP: 123 KeysPressed[Up] = false; 124 break; 125 case ALLEGRO_KEY_DOWN: 126 KeysPressed[Down] = false; 127 break; 128 case ALLEGRO_KEY_LEFT: 129 KeysPressed[Left] = false; 130 break; 131 case ALLEGRO_KEY_RIGHT: 132 KeysPressed[Right] = false; 133 break; 134 } 135 } 136 137 if (redraw && al_is_event_queue_empty(eventQueue)) { 138 redraw = false; 139 al_clear_to_color(al_map_rgb(0, 0, 0)); 140 //for(int shipIndex = 0; shipIndex < ShipCollection.size(); shipIndex++) 141 //{ 142 // al_draw_bitmap(ShipCollection[shipIndex].getImage(), ShipCollection[shipIndex].upperLeft_X_Position, 143 // ShipCollection[shipIndex].upperLeft_Y_Position, 0); 144 //} 145 146 al_draw_bitmap(shipCollection[0].getImage(), shipCollection[0].upperLeft_X_Position, 147 shipCollection[0].upperLeft_Y_Position, 0); 148 /* 149 foreach Ship in the collections 150 draw it. 151 */ 152 al_flip_display(); 153 } 154 } 155 156 al_destroy_timer(screenRefreshTimer); 157 al_destroy_event_queue(eventQueue); 158 al_destroy_display(mainDisplay); 159}

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Ship ship(SHIP_WIDTH, SHIP_HEIGTH, ship_X_Position, ship_Y_Position);
ship.setColor(255, 0, 255);
shipCollection.push_back(ship);

You're adding a copy of ship to your vector, not ship itself. To do that you need to make a vector of ship pointers so you don't get ships copying themselves. Your copy constructor for ship doesn't copy the color so it is still uninitialized since you haven't used a constructor initialization list :

class Example {
   int i;
   float f;
   double d;
public :
   Example() :
      i(0),
      f(0.0f)
   {
      // d is unitialized here
      printf("%lf\n" , d);
   }
};

Darren Hoehna
Member #15,297
September 2013

Right, when I added the ALLEGRO_COLOR, I forgot to add that to my copy constructor. :P

I understand. Thank you again.

Go to: