Advertisement
Guest User

mouse position mac osx

a guest
Apr 10th, 2017
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.82 KB | None | 0 0
  1. /** Example 006 2D Graphics
  2.  
  3. This Tutorial shows how to do 2d graphics with the Irrlicht Engine.
  4. It shows how to draw images, keycolor based sprites,
  5. transparent rectangles, and different fonts. You may consider
  6. this useful if you want to make a 2d game with the engine, or if
  7. you want to draw a cool interface or head up display for your 3d game.
  8.  
  9. As always, I include the header files, use the irr namespace,
  10. and tell the linker to link with the .lib file.
  11. */
  12. #include <irrlicht.h>
  13. #include "driverChoice.h"
  14.  
  15. using namespace irr;
  16.  
  17. #ifdef _MSC_VER
  18. #pragma comment(lib, "Irrlicht.lib")
  19. #endif
  20.  
  21. /*
  22. At first, we let the user select the driver type, then start up the engine, set
  23. a caption, and get a pointer to the video driver.
  24. */
  25. int main()
  26. {
  27.     // ask user for driver
  28.     video::E_DRIVER_TYPE driverType=driverChoiceConsole();
  29.     if (driverType==video::EDT_COUNT)
  30.         return 1;
  31.  
  32.     // create device
  33.  
  34.     IrrlichtDevice *device = createDevice(driverType,
  35.         core::dimension2d<u32>(512, 384));
  36.  
  37.     if (device == 0)
  38.         return 1; // could not create selected driver.
  39.  
  40.     device->setWindowCaption(L"Irrlicht Engine - 2D Graphics Demo");
  41.  
  42.     video::IVideoDriver* driver = device->getVideoDriver();
  43.  
  44.     /*
  45.     All 2d graphics in this example are put together into one texture,
  46.     2ddemo.png. Because we want to draw colorkey based sprites, we need to
  47.     load this texture and tell the engine, which part of it should be
  48.     transparent based on a colorkey.
  49.  
  50.     In this example, we don't tell it the color directly, we just say "Hey
  51.     Irrlicht Engine, you'll find the color I want at position (0,0) on the
  52.     texture.". Instead, it would be also possible to call
  53.     driver->makeColorKeyTexture(images, video::SColor(0,0,0,0)), to make
  54.     e.g. all black pixels transparent. Please note that
  55.     makeColorKeyTexture just creates an alpha channel based on the color.
  56.     */
  57.     video::ITexture* images = driver->getTexture("../../media/2ddemo.png");
  58.     driver->makeColorKeyTexture(images, core::position2d<s32>(0,0));
  59.  
  60.     /*
  61.     To be able to draw some text with two different fonts, we first load
  62.     them. Ok, we load just one. As the first font we just use the default
  63.     font which is built into the engine. Also, we define two rectangles
  64.     which specify the position of the images of the red imps (little flying
  65.     creatures) in the texture.
  66.     */
  67.     gui::IGUIFont* font = device->getGUIEnvironment()->getBuiltInFont();
  68.     gui::IGUIFont* font2 =
  69.         device->getGUIEnvironment()->getFont("../../media/fonthaettenschweiler.bmp");
  70.  
  71.     core::rect<s32> imp1(349,15,385,78);
  72.     core::rect<s32> imp2(387,15,423,78);
  73.  
  74.     /*
  75.     Prepare a nicely filtering 2d render mode for special cases.
  76.     */
  77.     driver->getMaterial2D().TextureLayer[0].BilinearFilter=true;
  78.     driver->getMaterial2D().AntiAliasing=video::EAAM_FULL_BASIC;
  79.  
  80.    
  81.     gui::ICursorControl* cursor = device->getCursorControl();
  82.     /*
  83.     Everything is prepared, now we can draw everything in the draw loop,
  84.     between the begin scene and end scene calls. In this example, we are
  85.     just doing 2d graphics, but it would be no problem to mix them with 3d
  86.     graphics. Just try it out, and draw some 3d vertices or set up a scene
  87.     with the scene manager and draw it.
  88.     */
  89.     while(device->run() && driver)
  90.     {
  91.         if (device->isWindowActive())
  92.         {
  93.             u32 time = device->getTimer()->getTime();
  94.  
  95.             driver->beginScene(true, true, video::SColor(255,120,102,136));
  96.  
  97.             /*
  98.             First, we draw 3 sprites, using the alpha channel we
  99.             created with makeColorKeyTexture. The last parameter
  100.             specifies that the drawing method should use this alpha
  101.             channel. The last-but-one parameter specifies a
  102.             color, with which the sprite should be colored.
  103.             (255,255,255,255) is full white, so the sprite will
  104.             look like the original. The third sprite is drawn
  105.             with the red channel modulated based on the time.
  106.             */
  107.  
  108.             // draw fire & dragons background world
  109.             driver->draw2DImage(images, core::position2d<s32>(50,50),
  110.                 core::rect<s32>(0,0,342,224), 0,
  111.                 video::SColor(255,255,255,255), true);
  112.  
  113.             // draw flying imp
  114.             driver->draw2DImage(images, core::position2d<s32>(164,125),
  115.                 (time/500 % 2) ? imp1 : imp2, 0,
  116.                 video::SColor(255,255,255,255), true);
  117.  
  118.             // draw second flying imp with colorcylce
  119.             driver->draw2DImage(images, core::position2d<s32>(270,105),
  120.                 (time/500 % 2) ? imp1 : imp2, 0,
  121.                 video::SColor(255,(time) % 255,255,255), true);
  122.  
  123.             /*
  124.             Drawing text is really simple. The code should be self
  125.             explanatory.
  126.             */
  127.  
  128.             // draw some text
  129.             if (font)
  130.                 font->draw(L"This demo shows that Irrlicht is also capable of drawing 2D graphics.",
  131.                     core::rect<s32>(130,10,300,50),
  132.                     video::SColor(255,255,255,255));
  133.  
  134.             // draw some other text
  135.             if (font2)
  136.                 font2->draw(L"Also mixing with 3d graphics is possible.",
  137.                     core::rect<s32>(130,20,300,60),
  138.                     video::SColor(255,time % 255,time % 255,255));
  139.             wchar_t buffer[80];
  140.            
  141.             swprintf(buffer, 80, L"cursor : %d %d", cursor->getPosition().X, cursor->getPosition().Y);
  142.             if (font)
  143.                 font->draw(buffer, core::rect<s32>(0, 0, 100, 100), video::SColor(255,255, 255, 255));
  144.             //cursor
  145.  
  146.             /*
  147.             Next, we draw the Irrlicht Engine logo (without
  148.             using a color or an alpha channel). Since we slightly scale
  149.             the image we use the prepared filter mode.
  150.             */
  151.             driver->enableMaterial2D();
  152.             driver->draw2DImage(images, core::rect<s32>(10,10,108,48),
  153.                 core::rect<s32>(354,87,442,118));
  154.             driver->enableMaterial2D(false);
  155.  
  156.             /*
  157.             Finally draw a half-transparent rect under the mouse cursor.
  158.             */
  159.             core::position2d<s32> m = device->getCursorControl()->getPosition();
  160.             driver->draw2DRectangle(video::SColor(100,255,255,255),
  161.                 core::rect<s32>(m.X-20, m.Y-20, m.X+20, m.Y+20));
  162.  
  163.             driver->endScene();
  164.         }
  165.     }
  166.  
  167.     device->drop();
  168.  
  169.     return 0;
  170. }
  171.  
  172. /*
  173. That's all. I hope it was not too difficult.
  174. **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement