Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- namespace Sprites {
- using namespace System;
- using namespace System::ComponentModel;
- using namespace System::Collections;
- using namespace System::Windows::Forms;
- using namespace System::Data;
- using namespace System::Drawing;
- class CImagen
- {
- protected:
- Rectangle areaDibujo;
- public:
- CImagen(Rectangle area) {
- areaDibujo = area;
- }
- ~CImagen();
- virtual void dibujar(Graphics^ g, Bitmap^ img) {
- g->DrawImage(img, areaDibujo);
- }
- };
- class CSprite :
- public CImagen
- {
- short indice;
- short nroSubimagenes;
- public:
- CSprite(Rectangle area, short nroSubi)
- : CImagen(area) {
- nroSubimagenes = nroSubi;
- indice = 0;
- }
- void dibujar(Graphics^ g, Bitmap^ img) {
- Rectangle areaRecorte = getAreaRecorte(img);
- g->DrawImage(img, areaDibujo, areaRecorte, GraphicsUnit::Pixel);
- indice++;
- if (indice == nroSubimagenes) indice = 0;
- }
- private:
- Rectangle getAreaRecorte(Bitmap^ img) {
- short ancho = img->Width / 8;
- short alto = img->Height;
- short x = indice * ancho;
- short y = 0;
- return Rectangle(x, y, ancho, alto);
- }
- };
- public ref class MyForm : public System::Windows::Forms::Form
- {
- BufferedGraphics^ buffer;
- private: System::Windows::Forms::Timer^ timer;
- Bitmap^ imgEscenario;
- CImagen* escenario;
- Bitmap^ imgHombre;
- CSprite* hombre;
- public:
- MyForm(void)
- {
- InitializeComponent();
- buffer = BufferedGraphicsManager::Current->Allocate(this->CreateGraphics(), this->ClientRectangle);
- imgEscenario = gcnew Bitmap("imagenes//paisaje.jpg");
- escenario = new CImagen(this->ClientRectangle);
- imgHombre = gcnew Bitmap("imagenes//capguy-walk.png");
- hombre = new CSprite(Rectangle(100, 300, 100, 200), 8);
- }
- protected:
- ~MyForm()
- {
- if (components)
- {
- delete components;
- }
- delete imgEscenario;
- delete imgHombre;
- delete escenario;
- delete hombre;
- }
- private: System::ComponentModel::IContainer^ components;
- protected:
- private:
- /// <summary>
- /// Variable del diseñador necesaria.
- /// </summary>
- #pragma region Windows Form Designer generated code
- /// <summary>
- /// Método necesario para admitir el Diseñador. No se puede modificar
- /// el contenido de este método con el editor de código.
- /// </summary>
- void InitializeComponent(void)
- {
- this->components = (gcnew System::ComponentModel::Container());
- this->timer = (gcnew System::Windows::Forms::Timer(this->components));
- this->SuspendLayout();
- //
- // timer
- //
- this->timer->Enabled = true;
- this->timer->Tick += gcnew System::EventHandler(this, &MyForm::animar);
- //
- // MyForm
- //
- this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
- this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
- this->ClientSize = System::Drawing::Size(986, 617);
- this->Name = L"MyForm";
- this->Text = L"MyForm";
- this->ResumeLayout(false);
- }
- #pragma endregion
- private: System::Void animar(System::Object^ sender, System::EventArgs^ e) {
- escenario->dibujar(buffer->Graphics,imgEscenario);
- hombre->dibujar(buffer->Graphics, imgHombre);
- buffer->Render();
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement