Important alert: (current site time 7/16/2013 4:39:14 AM EDT)
 

article

DelphiX for Delphi7 - Part 3 - Backgrounds and flickering

Email
Submitted on: 5/2/2005 4:26:36 PM
By: Lrd.Sandman 
Level: Beginner
User Rating: By 7 Users
Compatibility: Delphi 7
Views: 18844
 
     Part 3 of a delphix tutorial under delphi 7.

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				
-DelphiX for Delphi7

Part 3 - Backgrounds and flickering
Taking off from the end of part 2, go to your dximagelist and add another item. We're going to add a background, a black one in this case. Create a picture (with paint or something) that's 640x480 in size and make it black.
Head back to the imagelist and add the black image to the 2nd item (note that in code this is item[1], since it starts at 0). Remember to make sure that the transparent color is not the same as your background color!

If you've played around a bit with the result of part 2 you probably noticed that under certain circumstances the image will not be removed. To make sure that the old images are removed we'll simply overwrite them, hence the background. This is done by adding the following line above the item[0].draw line in the OnTimer event of the timer:

dximagelist1.Items[1].Draw(dxdraw1.Surface, 100, 100, 0);

Compile your project and move the smilie around a bit, you'll probably won't see alot of difference if you didn't played around with the result of part 2. If that's the case then temporarely replace the smilie with a yellow block of 300x300 (there's alot more sizes, but it doesn't always happen). Test it with and without the line and you'll see that without the image won't be removed. There's still one problem though, go to the end of the screen and you'll see that the smilie goes out of the screen. This might be handy from time to time (ie. end of a level), but alot of games never even use this. So let's limit where the smilie can go.

Development side-note: Develop your games using a grid with the screen sizes, this reduces the chance of errors.

Go to the FormKeyDown event of Form1 (OI) and enter the following code above and below what's in the function:

if x_pos > 5 then
begin
if y_pos > 5 then
begin

[..existing code..]

end
else
begin
y_pos := y_pos + 1;
end
end
else
x_pos := x_pos + 1;

The first part set the boundaries to ([x,y]) [5,5]. Now you can't cross the left or the top of the screen.
The right side works the same, try to get this working yourself. Remember, if dxdraw is not running in full screen, the actual screen-size may differ, just test it out ;).
The part that goes below the existing code makes sure that if the image is not within those borders that it get's a + 1 on the x or y value. You have to do this, because if you don't the image will go beyond the border (if it's on x = 6 it will set x to 5 rendering the image beyond the defined boundary), wich results in your image getting stuck.

Now for the flickering, flickering will occur when you're drawing faster then Windows. This usually happens when you have animations or when moving larger pictures under runtime. We won't have alot of problems with this yet, but this is something that can give you alot of problems later. It's quite simple to fix this, under

type
TForm1 = class(TForm)

add the following line (just add it above the other procedures):

procedure StopFlickering(var Msg: TWMEraseBkgnd); message WM_ERASEBKGND;

This means that when Windows sends the message to repaint our GUI together with the rest of the screen this function gets started. Now for the function itself:

Procedure TForm1.StopFlickering(var Msg: TWMEraseBkgnd);
begin
Msg.Result := 1;
end;

We're handling the repainting ourself, but since we don't want this to happen (we got the timer for that) the function simply says that it has finished (msg.result := 1;). I'll show you some samples of flickering in later parts, but for now that's not important yet. Well that's it for this part, check the next one for animating and sprites.

Download the source-code at http://devcenter.darkangeldev.com (normally outline version as well).


Other 7 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
5/2/2005 4:46:13 PMLrd.Sandman

Part 1 and 2 are on the site as well.. If u like it then vote, if not well then don't.
(If this comment was disrespectful, please report it.)

 
5/3/2005 10:33:14 AM

Nice, good way to get started on a great topic.
(If this comment was disrespectful, please report it.)

 
5/16/2005 10:58:47 PM

It's Very Exelent
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.