Image Loading... Image Loading...
Click Here to Visit Our Sponsor

Home
News
Cartoon

Books
Reviews

SimTel
Compress
Database
Delphi
Desktop
Diskutl
Fileutl
Graphics
Inet
Mmedia
Prog
Scrsave
Util

C/C++
CodeBase!
Libraries
Tutorials
WebLinks

Pascal
Libraries
WebLinks



Home / C Tutorials
SciTech MGL 4.05
Hw to move a bitmap around the screen
Written by Ryan Wahle
Friday, June 4, 1999

Well, finally the second tutorial for mgl! WHEW!YEAH!! hmmm.. anyways, this tutorial is gonnashow us how to move a bitmap around the screenwith the cursor keys!

Step 1
I'm not gonna show you go to setup all yourmgl stuff. If you want to know, just read thefirst tutorial. Now, make sure you have the following code somewhere in your program. The best place is probably at the top somewhere.

    #define replace MGL_REPLACE_MODE

    event_t evt;
    bitmap_t *bitfile = NULL;

Now, the event_t is a struct for all the events that get passed back to us, like when they press up or something. If we didn't have this, our picture would just sit there. The next thing is the bitmap_t struct. This is the variable (bitfile) which we will store thedata to where our bitmap file is located andthe colors it has, etc. Look in the docs for more info on it.

Step 2
Now lets setup a few variables. Put the seat the top of your program somewhere also.

    int x = 60, y = 60;
    int done = 0;

The x and y integers are the pixel postion at which the bitmap will be placed when we first start the program.

Step 3
Now it's time to load the bitmap file we have. Just take your favorite small bitmap file and rename it to guy.bmp and put it in the same directory as this program runs from. Or you can hard code the path in if you want. Now put this code in after you have setup yourdisplay and everything explained in the "Step by step".

  MGL_checkIdentityPalette(true);
  if ( (bitfile =
          MGL_loadBitmap ("a.bmp", true)) == 0)
      printf("error loading bmp\n");

I'm acually not sure what the MGL_checkIdentityPalette does. I wrote this a while ago, and just can't remember. I think it won't work without it though. Look in the docs and find out if you want to. You should do it even if you don't. :) What this does, is it loads the bitmap guy.bmp into the struct bitfile, and if there is a error, like the file doesn't exist, then it prints error loading bmp.

Step 4
Since we have loaded the bmp file, that doesn't mean it gets displayed in to our screen. All it is doing rightnow is taking up space in memory. To put it on the screen, just type this.

    MGL_putBitmap (dc, x, y, bitfile, replace);

This will put it at the cords we choose above. Again, look in the docs to see what these params actually do.

Step 5
Ok! Now for the good part!!! Lets make our man move!!! YEAH!! Now, if you remember above, we did the event_t. This is where we are gonna use it. I am gonna explaineverything here, and then print the whole loop below.

First off, we have our while loop. It will continue to run until done = 1 . Everytime we start the loop again we check to see if there is an event waiting for us. An event can be anything, from a joystick button was pressed to keyboard keys were pressed. I think there are other events also, but we don't care about those right now. So to check to see if there are waiting events, then just call this:

    EVT_getNext(&evt,EVT_EVERYEVT)

Now we will want to put that in an if statement so we can tell if it's true or not. Now if it is, then we do something like a switch on:

    evt.what

This will tell us what kind of event we have. The one we are looking for is:

    EVT_KEYDOWN

That means if a key was pressed on the keyboard, let us know so that we can do another switch on:

    evt.message

This tells us what key was pressed. We can't read it, so we do something like:

    EVT_asciiCode(evt.message)

This convets it into an ascii code were we can do a switch on it and find out what to do. If it's escape, meaning they want to leave the program, then we are looking for 27, and set done to true, so that we exit the while loop and quit the program.

If it's up, down, left, or right then we want to move the y and x positions as needed. Here are the key codes:

    Up: 56
    Down: 50
    Left: 52
    Right: 54

Now then we call MGL_putBitmap with the new cords. You will notice that I call:

    MGL_clearDevice();

I do this so that it will clear the old bitmap off the screen so it looks like we are moving. Try commenting them out, and see for yourself. The only problem is that everything on the screen gets wiped out, so you will have to redraw everything. But we don't care about that right now since we don't have anything else to redraw.

Conclusion
That's pretty much it. Here's the full loop so you can see exactly what I was talking about above. The only thing is that you have to use the arrow keys on your number pad; you can't use the real arrow keys because I don't know the code for them. If you have the ascii code for them or know how to write a program that will give you the ascii code of any key; send it my way, and preferably written in MGL if possible.

  #define replace MGL_REPLACE_MODE

  while (!done) {
   if (EVT_getNext(&evt,EVT_EVERYEVT)) {
    switch (evt.what) {
     case EVT_KEYDOWN:
      switch (EVT_asciiCode(evt.message)) {
       case 27:
        done = true;
        break;
       case 56: // Up
        MGL_clearDevice();
        y = y - 10;
        MGL_putBitmap (dc, x, y, bitfile, replace);
        break;
       case 50: // Down
        MGL_clearDevice();
        y = y + 10;
        MGL_putBitmap (dc, x, y, bitfile, replace);
        break;
       case 52: // Left
        MGL_clearDevice();
        x = x - 10;
        MGL_putBitmap (dc, x, y, bitfile, replace);
        break;
       case 54: // Right
        MGL_clearDevice();
        x = x + 10;
        MGL_putBitmap (dc, x, y, bitfile, replace);
        break;
       }
      }
     }
    }
    MGL_exit();


This Article
Installation
Configuration
Step by step
Move bitmaps
Troubleshooting

Downloads
MGL Documentation Programming Guide Reference Guide

See Also
MGL Libraries

NetLinks
SciTech Homepage
MGL Repository
MGL Newsgroup

Next:  Troubleshooting

   
Image Loading...