Repos

MELHARFI-2D-Game-Engine.

Got to repo.

04. Bmp (Bitmap)

Bmp class

To draw a picture "Bitmap" you should create an instance of Bmp class. This class has 20 overloads depending on what the parameters you want to initialize. The easy one is

                        
Bmp cloudPicture = new Bmp("cloud.png", new Point(0,0), manager);
                        
                      

And here is a complete code

                        
using System.Drawing;
using System.Windows.Forms;
using MELHARFI.Manager;
using MELHARFI.Manager.Gfx;

namespace _2dProject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            Manager manager = new Manager(this, "CORE_ENGINE");
            manager.Background = Color.Blue;

            Bmp cloudPicture = new Bmp("cloud.png", new Point(0,0), manager);
            
            manager.BackgroundLayer.Add(cloudPicture);
        }
    }
}                          
                        
                      

Obiously you can chage the Position as you want

ObjectLayer Is the common asset like players, monsters... that come front of background (layer 2)

                        
Bmp superMario = new Bmp("supermario.png", new Point(100, 100), manager);
manager.ObjectLayer.Add(superMario);
                        
                      

If you experiance some king of flickering of your form, you can go to Flickering chapter to fix this or add that code right in the top of your main function

                        
this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.DoubleBuffer, true);
this.Invalidate();

Manager manager = new Manager(this, "CORE_ENGINE");
manager.Background = Color.Blue;

Bmp cloudPicture = new Bmp("cloud.png", new Point(0,0), manager);            
manager.BackgroundLayer.Add(cloudPicture);

Bmp superMario = new Bmp("supermario.png", new Point(100, 100), manager);
manager.ObjectLayer.Add(superMario);
                        
                      

Bmp parameters.

Name : You can associate a name to an image object so that you can fetch for it if you want to, like so :

                        
cloudPicture.name = "cloud1";
                        
                      

Opacity : cloudPicture.Opacity = 0.5F; meaning that the picture is going to be transparent by 50%, min value is 0.0F (completely transparent) and 1F is 100% opaque.

Rectanle : let you draw only a peace of your picture depending on a start point and the size to take.

                        
cloudPicture.rectangle = new Rectangle(new Point(50, 50), new Size(60, 70));
                        
                      

The picture rendered will croped, from the position 50 X, 50 Y of the original picture, and only a rectangle of 60 pixel width and 70 pixel height will be taken, the other pieces is ignored and not displayed.

typeGfx : to identify object as it's type if is it a background or Object or Top

                        
cloudPicture.TypeGfx = Manager.TypeGfx.Bgr;
//or
cloudPicture.TypeGfx = Manager.TypeGfx.Obj;
                        
                      

Useful if somehow you don't know where an object is added, if is it in background or object, so by that value you know where it’s stored by fetching like so :

                        
if(cloudPicture.TypeGfx == Manager.TypeGfx.Bgr)
{
  /*...*/
}
else if(cloudPicture.TypeGfx == Manager.TypeGfx.Obj)
{
  /*...*/
}
else
{
  /*...*/
}
                        
                      

Visible : boolean property to make picture visible or not.

Z-Index : depth of an object inside a layer to superpose a picture over another one as integer.

Crypted : is a bool flag to determine if a picture is encrypted or not, if it's so, you need my CryptDecrypt tool share in the link:.

https://github/MELHARFI/CryptDecrypt

This flag is self initialized 'readonly' when you create your Bmp instance as you'll specify whether the picture is encrypted or not, so you don't need to initialize it as it's done in the constructor like :

                        
Manager manager = new Manager(this, "manager");
manager.Background = Color.Blue;
                          
/*not encrypted Bmp*/
Bmp NotEncryptedBMP = new Bmp("1.jpg", Point.Empty, manager);
manager.BackgroundLayer.Add(NotEncryptedBMP);

/*encrypted Bmp*/
string key = "12345678";
string iv = "87654321";
Bmp encryptedBMP = new Bmp("1.jpg", Point.Empty, key, iv, manager);
manager.BackgroundLayer.Add(encryptedBMP);
                        
                      

To encrypt a picture you must use the CryptDecrypt tool to encrypt it first using 2 Keys you must regenerate "Key & IV"

  • >Key
  • is the first key must be exactly 8 characters as string, (any character, number, letters, special char ..)

  • IV
  • is the second key called "Initializing Vector" must be exactly 8 characters too as string, (any character, number, letters, special char ..)

    you can use the same string for both of them.

    After that you must replace your original picture by the one being encrypted and saved and then use the apropriate constructor :

                            
    Bmp encryptedBMP = new Bmp("1.jpg", Point.Empty, key, iv, manager);
                            
                          

    isSpriteSheet boolean value indicating wheter the object "Bmp" must show only a piece of the original picture or the full image, this value is read only, and it's initialized when a constructor of Bmp is called with the rectangle parameter.

    quite useful when you want to check if a Bmp object is a sprite sheet or not.

    newColorMap is a way to convert a color to another one, meaning that each pixel with the given color is found will be changed.

    More details is in the 19 Converting-color chapter

    Services

    App Developpement

    Want me to dev an app for you ? dont hesitate to contact me.

    Programming

    Are you looking for a coder/teammate for your project ? Let's give it a try.

    Job Invitation

    Have a proposal for me ? we can discuss about it.

    Donation maybe ?!

    You want to buy me a coffe ? m.elharfi@gmail.com