// Raster Example #include <ivplus/bitmap.h> #include <ivplus/box.h> #include <ivplus/frame.h> #include <ivplus/interactor.h> #include <ivplus/message.h> #include <ivplus/painter.h> #include <ivplus/raster.h> #include <ivplus/shape.h> #include <ivplus/world.h> class RasterRenderer : public Interactor { public: RasterRenderer(Raster* r); protected: virtual void Redraw(IntCoord, IntCoord, IntCoord, IntCoord); virtual void Reconfig(); private: int width, height; Raster* raster; }; // Cache the important values. RasterRenderer::RasterRenderer(Raster* r) { raster = r; width = r->Width(); height = r->Height(); } // A RasterRenderer draws the indicated Raster in the middle // of the canvas. void RasterRenderer::Redraw(IntCoord left, IntCoord bottom, IntCoord right, IntCoord top) { output->ClearRect(canvas, left, bottom, right, top); int rx = (xmax - width)/2; int ry = (ymax - height)/2; output->RasterRect(canvas, rx, ry, raster); } // Make sure there’s enough room to draw the Raster. void RasterRenderer::Reconfig() { shape->width = width; shape->height = height; shape->Rigid(0, hfil, 0, vfil); } int main(int argc, char **argv) { // Create the world to get the connection with the display. World* world = new World("MyApp", argc, argv); // using "/usr/lib/X11/bitmaps/HPlogo.bm" const int HPlogo_width=46; const int HPlogo_height=34; static char HPlogo_bits[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xe0, 0xff, 0x79, 0xfe, 0xff, 0xc1, 0xf8, 0xff, 0x39, 0xfe, 0xff, 0xc7, 0xfc, 0xff, 0x3d, 0xfc, 0xff, 0xcf, 0xfc, 0xff, 0x3c, 0xe0, 0xff, 0xcf, 0xfe, 0x7f, 0x1c, 0x80, 0xff, 0xdf, 0xfe, 0x3f, 0x1c, 0x00, 0xff, 0xdf, 0xfe, 0x1f, 0x1e, 0x00, 0xfe, 0xdf, 0xfe, 0x0f, 0x1e, 0x00, 0xfc, 0xdf, 0xfe, 0x07, 0x0e, 0x00, 0xf8, 0xdf, 0xfe, 0x07, 0x0f, 0x00, 0xf8, 0xdf, 0xfe, 0x03, 0x7f, 0xfc, 0xf1, 0xdf, 0xfe, 0x03, 0xff, 0xfc, 0xf3, 0xdf, 0xfe, 0x03, 0xff, 0xfc, 0xf3, 0xdf, 0xfe, 0x81, 0xf7, 0xde, 0xe3, 0xdf, 0xfe, 0x81, 0x77, 0xde, 0xe1, 0xdf, 0xfe, 0x81, 0x73, 0xce, 0xe1, 0xdf, 0xfe, 0xc1, 0x7b, 0xee, 0xe1, 0xdf, 0xfe, 0xc1, 0x7b, 0xef, 0xe1, 0xdf, 0xfe, 0xc1, 0x3b, 0xe7, 0xe0, 0xdf, 0xfe, 0xc3, 0x39, 0xff, 0xf0, 0xdf, 0xfe, 0xe3, 0xbd, 0xff, 0xf0, 0xdf, 0xfe, 0xe3, 0xbd, 0x7f, 0xf0, 0xdf, 0xfe, 0x07, 0x80, 0x03, 0xf8, 0xdf, 0xfe, 0x07, 0x80, 0x03, 0xf8, 0xdf, 0xfe, 0x0f, 0xc0, 0x03, 0xfc, 0xdf, 0xfe, 0x1f, 0xc0, 0x03, 0xfe, 0xdf, 0xfe, 0x3f, 0xc0, 0x01, 0xff, 0xdf, 0xfe, 0x7f, 0xc0, 0x81, 0xff, 0xdf, 0xfc, 0xff, 0xe1, 0xe1, 0xff, 0xcf, 0xfc, 0xff, 0xe7, 0xfc, 0xff, 0xcf, 0xf8, 0xff, 0xe7, 0xfc, 0xff, 0xc7, 0xe0, 0xff, 0xf3, 0xfc, 0xff, 0xc1, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0 }; // Create a Bitmap from the data above. Bitmap* bm = new Bitmap(HPlogo_bits, HPlogo_width, HPlogo_height); // Create a Raster based on the Bitmap above, twice its size. Raster* rast = new Raster(2 * HPlogo_width, 2 * HPlogo_height); // Copy the bitmap into the raster, scaling and "colorizing" it as we go. for (int x = 0; x < HPlogo_width; x++) { for (int y = 0; y < HPlogo_height; y++) { ColorIntensity r = (float)x/(float)HPlogo_width; ColorIntensity g = (float)y/(float)HPlogo_height; ColorIntensity b = 1.0; static const float alpha = 1.0; if (bm->peek(x,y)) { r = 0.0; g = 0.0; } else { b = 0.0; } // Color the block of 4 adjacent pixels. rast->poke(2 * x, 2 * y, r, g, b, alpha); rast->poke(2 * x + 1, 2 * y, r, g, b, alpha); rast->poke(2 * x, 2 * y + 1, r, g, b, alpha); rast->poke(2 * x + 1, 2 * y + 1, r, g, b, alpha); } } // Create a RasterRenderer to draw it. RasterRenderer* hplogo = new RasterRenderer(rast); // Map the window. world->InsertApplication(new Frame(hplogo, 2, BevelOut)); // Enter the event loop. world->Run(); return 0; }
raster.h