|
|||||||
Home
Books
SimTel
C/C++
|
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
#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 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 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 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 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:
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 #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
See Also
NetLinks
|
|||||
Next:
Troubleshooting
|