example.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include "oxygine-framework.h"
  2. #include <functional>
  3. using namespace oxygine;
  4. //it is our resources
  5. //in real project you would have more than one Resources declarations.
  6. //It is important on mobile devices with limited memory and you would load/unload them
  7. Resources gameResources;
  8. class MainActor: public Actor
  9. {
  10. public:
  11. spTextField _text;
  12. spSprite _button;
  13. MainActor()
  14. {
  15. //create button Sprite
  16. spSprite button = new Sprite();
  17. //setup it:
  18. //set button.png image. Resource 'button' defined in 'res.xml'
  19. button->setResAnim(gameResources.getResAnim("button"));
  20. //centered button at screen
  21. Vector2 pos = getStage()->getSize() / 2 - button->getSize() / 2;
  22. button->setPosition(pos);
  23. //register click handler to button
  24. EventCallback cb = CLOSURE(this, &MainActor::buttonClicked);
  25. button->addEventListener(TouchEvent::CLICK, cb);
  26. #ifdef CLOSURE_FUNCTION //if your compiler supports lambda
  27. button->addEventListener(TouchEvent::CLICK, [](Event * e)->void
  28. {
  29. log::messageln("button clicked");
  30. });
  31. #endif
  32. //attach button as child to current actor
  33. addChild(button);
  34. _button = button;
  35. //create TextField Actor
  36. spTextField text = new TextField();
  37. //attach it as child to button
  38. text->attachTo(button);
  39. //centered in button
  40. text->setPosition(button->getSize() / 2);
  41. //initialize text style
  42. TextStyle style;
  43. style.font = gameResources.getResFont("main")->getFont();
  44. style.color = Color::White;
  45. style.vAlign = TextStyle::VALIGN_MIDDLE;
  46. style.hAlign = TextStyle::HALIGN_CENTER;
  47. text->setStyle(style);
  48. text->setText("Click\nMe!");
  49. _text = text;
  50. }
  51. void buttonClicked(Event* event)
  52. {
  53. //user clicked to button
  54. //animate button by chaning color
  55. _button->setColor(Color::White);
  56. _button->addTween(Sprite::TweenColor(Color::Green), 500, 1, true);
  57. //animate text by scaling
  58. _text->setScale(1.0f);
  59. _text->addTween(Actor::TweenScale(1.1f), 500, 1, true);
  60. //and change text
  61. _text->setText("Clicked!");
  62. //lets create and run sprite with simple animation
  63. runSprite();
  64. }
  65. void runSprite()
  66. {
  67. spSprite sprite = new Sprite();
  68. addChild(sprite);
  69. int duration = 500;//500 ms
  70. int loops = -1;//infinity loops
  71. //animation has 7 columns, check 'res.xml'
  72. ResAnim* animation = gameResources.getResAnim("anim");
  73. //add animation tween to sprite
  74. //TweenAnim would change animation frames
  75. sprite->addTween(Sprite::TweenAnim(animation), duration, loops);
  76. Vector2 destPos = getStage()->getSize() - sprite->getSize();
  77. Vector2 srcPos = Vector2(0, destPos.y);
  78. //set sprite initial position
  79. sprite->setPosition(srcPos);
  80. //add another tween: TweenQueue
  81. //TweenQueue is a collection of tweens
  82. spTweenQueue tweenQueue = new TweenQueue();
  83. tweenQueue->setDelay(1500);
  84. //first, move sprite to dest position
  85. tweenQueue->add(Sprite::TweenPosition(destPos), 1500, 1);
  86. //then fade it out smoothly
  87. tweenQueue->add(Sprite::TweenAlpha(0), 500, 1);
  88. sprite->addTween(tweenQueue);
  89. //and remove sprite from tree when tweenQueue is empty
  90. //if you don't hold any references to sprite it would be deleted automatically
  91. tweenQueue->setDetachActor(true);
  92. }
  93. };
  94. //declare spMainActor as intrusive_ptr holder of MainActor
  95. typedef oxygine::intrusive_ptr<MainActor> spMainActor;
  96. //you could use DECLARE_SMART preprocessor definition it does the same:
  97. //DECLARE_SMART(MainActor, spMainActor)
  98. void example_preinit() {}
  99. //called from entry_point.cpp
  100. void example_init()
  101. {
  102. //load xml file with resources definition
  103. gameResources.loadXML("res.xml");
  104. //lets create our client code simple actor
  105. //spMainActor was defined above as smart intrusive pointer (read more: http://www.boost.org/doc/libs/1_60_0/libs/smart_ptr/intrusive_ptr.html)
  106. spMainActor actor = new MainActor;
  107. //and add it to Stage as child
  108. getStage()->addChild(actor);
  109. }
  110. //called each frame from entry_point.cpp
  111. void example_update()
  112. {
  113. }
  114. //called each frame from entry_point.cpp
  115. void example_destroy()
  116. {
  117. //free previously loaded resources
  118. gameResources.free();
  119. }