Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. #pragma once
  2.  
  3. namespace Sprites {
  4.  
  5. using namespace System;
  6. using namespace System::ComponentModel;
  7. using namespace System::Collections;
  8. using namespace System::Windows::Forms;
  9. using namespace System::Data;
  10. using namespace System::Drawing;
  11.  
  12.  
  13. class CImagen
  14. {
  15. protected:
  16. Rectangle areaDibujo;
  17. public:
  18. CImagen(Rectangle area) {
  19. areaDibujo = area;
  20. }
  21. ~CImagen();
  22. virtual void dibujar(Graphics^ g, Bitmap^ img) {
  23. g->DrawImage(img, areaDibujo);
  24. }
  25. };
  26.  
  27. class CSprite :
  28. public CImagen
  29. {
  30. short indice;
  31. short nroSubimagenes;
  32. public:
  33. CSprite(Rectangle area, short nroSubi)
  34. : CImagen(area) {
  35. nroSubimagenes = nroSubi;
  36. indice = 0;
  37. }
  38. void dibujar(Graphics^ g, Bitmap^ img) {
  39. Rectangle areaRecorte = getAreaRecorte(img);
  40. g->DrawImage(img, areaDibujo, areaRecorte, GraphicsUnit::Pixel);
  41. indice++;
  42. if (indice == nroSubimagenes) indice = 0;
  43. }
  44. private:
  45. Rectangle getAreaRecorte(Bitmap^ img) {
  46. short ancho = img->Width / 8;
  47. short alto = img->Height;
  48. short x = indice * ancho;
  49. short y = 0;
  50. return Rectangle(x, y, ancho, alto);
  51. }
  52. };
  53.  
  54. public ref class MyForm : public System::Windows::Forms::Form
  55. {
  56. BufferedGraphics^ buffer;
  57. private: System::Windows::Forms::Timer^ timer;
  58. Bitmap^ imgEscenario;
  59. CImagen* escenario;
  60. Bitmap^ imgHombre;
  61. CSprite* hombre;
  62. public:
  63. MyForm(void)
  64. {
  65. InitializeComponent();
  66. buffer = BufferedGraphicsManager::Current->Allocate(this->CreateGraphics(), this->ClientRectangle);
  67. imgEscenario = gcnew Bitmap("imagenes//paisaje.jpg");
  68. escenario = new CImagen(this->ClientRectangle);
  69. imgHombre = gcnew Bitmap("imagenes//capguy-walk.png");
  70. hombre = new CSprite(Rectangle(100, 300, 100, 200), 8);
  71. }
  72.  
  73. protected:
  74. ~MyForm()
  75. {
  76. if (components)
  77. {
  78. delete components;
  79. }
  80. delete imgEscenario;
  81. delete imgHombre;
  82. delete escenario;
  83. delete hombre;
  84. }
  85. private: System::ComponentModel::IContainer^ components;
  86. protected:
  87.  
  88. private:
  89. /// <summary>
  90. /// Variable del diseñador necesaria.
  91. /// </summary>
  92.  
  93.  
  94. #pragma region Windows Form Designer generated code
  95. /// <summary>
  96. /// Método necesario para admitir el Diseñador. No se puede modificar
  97. /// el contenido de este método con el editor de código.
  98. /// </summary>
  99. void InitializeComponent(void)
  100. {
  101. this->components = (gcnew System::ComponentModel::Container());
  102. this->timer = (gcnew System::Windows::Forms::Timer(this->components));
  103. this->SuspendLayout();
  104. //
  105. // timer
  106. //
  107. this->timer->Enabled = true;
  108. this->timer->Tick += gcnew System::EventHandler(this, &MyForm::animar);
  109. //
  110. // MyForm
  111. //
  112. this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
  113. this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
  114. this->ClientSize = System::Drawing::Size(986, 617);
  115. this->Name = L"MyForm";
  116. this->Text = L"MyForm";
  117. this->ResumeLayout(false);
  118.  
  119. }
  120. #pragma endregion
  121. private: System::Void animar(System::Object^ sender, System::EventArgs^ e) {
  122. escenario->dibujar(buffer->Graphics,imgEscenario);
  123. hombre->dibujar(buffer->Graphics, imgHombre);
  124. buffer->Render();
  125. }
  126. };
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement