Penjin::Colour RendererSDL_2d::getPixel(SDL_Surface* s, Vector2d<int> pos) { if(s == NULL || pos.x >= s->w || pos.y >= s->h) return Colour(MAGENTA); int bpp = s->format->BytesPerPixel; /* Here p is the address to the pixel we want to retrieve */ Uint8 *p = (Uint8 *)s->pixels + pos.y * s->pitch + pos.x * bpp; Colour c; switch(bpp) { case 1: c.convertColour(*p,s->format);break; case 2: c.convertColour(*(Uint16 *)p,s->format);break; case 3: if(SDL_BYTEORDER == SDL_BIG_ENDIAN) c.convertColour(p[0] << 16 | p[1] << 8 | p[2],s->format); else c.convertColour(p[0] | p[1] << 8 | p[2] << 16,s->format); break; case 4: c.convertColour( *(Uint32 *)p,s->format);break; default: c.setColour(MAGENTA); /* shouldn't happen, but avoids warnings */ } return c; }
// The main test program int main() { cout << "TEST: Colour.C\n\n"; cout << "Can we create an Colour with the right default values?\n"; Colour *red = new Colour; printRC(red); cout << "Can we set values; green here is invalid - it should be set to 0 instead\n"; red->setRed(210); red->setGreen(276); red->setBlue(100); cout << "Testing the copy constructor\n"; Colour *blue = new Colour(*red); printRC(blue); cout << "Check operator= works\n"; Colour green; green = *red; printRC(&green); cout << "Check the setColour routine\n"; green.setColour(1,2,3); printRC(&green); cout << "Check the getColour routine\n"; unsigned int r, g, b; green.getColour(r, g, b); printRC(&green); cout << "\nTEST: ColourMap.C\n\n"; cout << "Can we create a ColourMap with the right default Colour + String\n"; ColourMap *map = new ColourMap(); cout << "Can we get the default colour back out of it?\n"; string s1 = map->getNameByIndex(0); green = map->getColourByIndex(0); printSRC(&s1, &green); cout << "Can we create a ColourMap with a specified default Colour?\n"; ColourMap *map2 = new ColourMap(*red); cout << "Can we get the information back out of it?\n"; s1 = map2->getNameByIndex(0); green = map2->getColourByIndex(0); printSRC(&s1, &green); cout << "Can we add a Colour\n"; s1 = "TEST1"; green.setColour(100, 101, 102); map2->addItem(green, s1); cout << "Can we get the info back out?\n"; s1 = ""; s1 = map2->getNameByIndex(1); green = map2->getColourByIndex(1); printSRC(&s1, &green); cout << "Add a couple more colours\n"; s1 = "TEST2"; green.setColour(101, 102, 103); map2->addItem(green, s1); s1 = "TEST3"; green.setColour(102, 103, 104); map2->addItem(green, s1); s1 = "TEST4"; green.setColour(103, 104, 105); map2->addItem(green, s1); // From an iterator: // iterator->first ==> Index // iterator->second.first ==> Colour // iterator->second.second ==> string // This rather unwieldy notation is because we store a pair in the map which is made up of a pair // to start with printIteratorContents(map2); cout << "Now try deleting the third item\n"; map2->deleteItemByIndex(3); // Print the map again printIteratorContents(map2); cout << "Make sure we get false when we try and modify item number 3\n"; s1 = "NO"; green.setColour(199,199,199); bool check = map2->modifyColourByIndex(3, green); if (check) cout << "WARNING: Managed to modify colour which doesn't exist\n"; check = map2->modifyNameByIndex(3, s1); if (check) cout << "WARNING: Managed to modify name which doesn't exist\n"; cout << "Check we can modify a colour which *is* there\n"; s1 = "YES"; green.setColour(233,233,233); check = map2->modifyColourByIndex(4, green); if (!check) cout << "WARNING: Couldn't modify colour which does exist\n"; check = map2->modifyNameByIndex(4, s1); if (!check) cout << "WARNING: Couldn't modify name which does exist\n"; // Print the map again printIteratorContents(map2); cout << "Now try adding another item - it should take the place of the one we removed.\n"; s1 = "NEW"; green.setColour(211, 212, 213); map2->addItem(green, s1); // Print the map again printIteratorContents(map2); cout << "Try swapping two items:\n"; check = map2->swapItems(3, 4); if (!check) cout << "WARNING: Couldn't swap two items which both exist\n"; // Print the map again printIteratorContents(map2); cout << "\nTEST: Generic Colour routines\n\n"; cout << "Try getting a combination colour:\n"; Colour blah = map2->getColourByIndex(0); Colour blah2 = map2->getColourByIndex(1); cout << "Original colours:\n"; printRC(&blah); printRC(&blah2); cout << "Combination colour:\n"; blah = getCombinationColour(blah, blah2); printRC(&blah); // Test the XML output cout << "\nTEST: XML Output\n\n"; cout << "For a single colour:\n"; cout << blah.toXmlString(); cout << "For a colourmap:\n"; cout << map2->toXmlString(std::string("segmentmap")); delete map; delete map2; delete red; delete blue; return 0; }