1 ÕÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ͸
\r
3 ³ To the VGA Trainer Program ³ ³
\r
5 ³ DENTHOR of ASPHYXIA ³ ³ ³
\r
6 ÔÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ; ³ ³
\r
7 ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ ³
\r
8 ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
\r
18 Greetings! This is the third part of the VGA Trainer series! Sorry it
\r
19 took so long to get out, but I had a running battle with the traffic
\r
20 department for three days to get my car registered, and then the MailBox
\r
21 went down. Ahh, well, life stinks. Anyway, today will do some things
\r
22 vital to most programs : Lines and circles.
\r
24 Watch out for next week's part : Virtual screens. The easy way to
\r
25 eliminate flicker, "doubled sprites", and subjecting the user to watch
\r
26 you building your screen. Almost every ASPHYXIA demo has used a virtual
\r
27 screen (with the exception of the SilkyDemo), so this is one to watch out
\r
28 for. I will also show you how to put all of these loose procedures into
\r
31 If you would like to contact me, or the team, there are many ways you
\r
32 can do it : 1) Write a message to Grant Smith in private mail here on
\r
34 2) Write a message here in the Programming conference here
\r
35 on the Mailbox (Preferred if you have a general
\r
36 programming query or problem others would benefit from)
\r
37 3) Write to ASPHYXIA on the ASPHYXIA BBS.
\r
38 4) Write to Denthor, Eze or Livewire on Connectix.
\r
39 5) Write to : Grant Smith
\r
42 6) Call me (Grant Smith) at 73 2129 (leave a message if you
\r
43 call during varsity)
\r
45 NB : If you are a representative of a company or BBS, and want ASPHYXIA
\r
46 to do you a demo, leave mail to me; we can discuss it.
\r
47 NNB : If you have done/attempted a demo, SEND IT TO ME! We are feeling
\r
48 quite lonely and want to meet/help out/exchange code with other demo
\r
49 groups. What do you have to lose? Leave a message here and we can work
\r
50 out how to transfer it. We really want to hear from you!
\r
53 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\r
56 You all know what a circle looks like. But how do you draw one on the
\r
59 You probably know circles drawn with the degrees at these points :
\r
69 Sorry about my ASCI ;-) ... anyway, Pascal doesn't work that way ... it
\r
70 works with radians instead of degrees. (You can convert radians to degrees,
\r
71 but I'm not going to go into that now. Note though that in pascal, the
\r
72 circle goes like this :
\r
83 Even so, we can still use the famous equations to draw our circle ...
\r
84 (You derive the following by using the theorem of our good friend
\r
88 (This is standard 8(?) maths ... if you haven't reached that level yet,
\r
89 take this to your dad, or if you get stuck leave me a message and I'll
\r
90 do a bit of basic Trig with you. I aim to please ;-))
\r
92 Where Y = your Y-coord
\r
94 R = your radius (the size of your circle)
\r
97 To simplify matters, we rewrite the equation to get our X and Y values :
\r
102 This obviousy is perfect for us, because it gives us our X and Y co-ords
\r
103 to put into our putpixel routine (see Part 1). Because the Sin and Cos
\r
104 functions return a Real value, we use a round function to transform it
\r
107 Procedure Circle (oX,oY,rad:integer;Col:Byte);
\r
113 X:=round(rad*COS (deg));
\r
114 Y:=round(rad*sin (deg));
\r
115 putpixel (x+ox,y+oy,Col);
\r
120 In the above example, the smaller the amount that deg is increased by,
\r
121 the closer the pixels in the circle will be, but the slower the procedure.
\r
122 0.005 seem to be best for the 320x200 screen. NOTE : ASPHYXIA does not use
\r
123 this particular circle algorithm, ours is in assembly language, but this
\r
124 one should be fast enough for most. If it isn't, give us the stuff you are
\r
125 using it for and we'll give you ours.
\r
128 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\r
131 There are many ways to draw a line on the computer. I will describe one
\r
132 and give you two. (The second one you can figure out for yourselves; it
\r
133 is based on the first one but is faster)
\r
135 The first thing you need to do is pass what you want the line to look
\r
136 like to your line procedure. What I have done is said that x1,y1 is the
\r
137 first point on the screen, and x2,y2 is the second point. We also pass the
\r
138 color to the procedure. (Remember the screens top left hand corner is (0,0);
\r
146 Again, sorry about my drawings ;-)
\r
148 To find the length of the line, we say the following :
\r
150 XLength = ABS (x1-x2)
\r
151 YLength = ABS (y1-y2)
\r
153 The ABS function means that whatever the result, it will give you an
\r
154 absolute, or posotive, answer. At this stage I set a variable stating
\r
155 wheter the difference between the two x's are negative, zero or posotive.
\r
156 (I do the same for the y's) If the difference is zero, I just use a loop
\r
157 keeping the two with the zero difference posotive, then exit.
\r
159 If neither the x's or y's have a zero difference, I calculate the X and Y
\r
160 slopes, using the following two equations :
\r
162 Xslope = Xlength / Ylength
\r
163 Yslope = Ylength / Xlength
\r
165 As you can see, the slopes are real numbers.
\r
166 NOTE : XSlope = 1 / YSlope
\r
168 Now, there are two ways of drawing the lines :
\r
173 The question is, which one to use? if you use the wrong one, your line
\r
174 will look like this :
\r
186 Well, the solution is as follows :
\r
194 If the slope angle is in the area of the stars (*) then use the first
\r
195 equation, if it is in the other section (`) then use the second one.
\r
196 What you do is you calculate the variable on the left hand side by
\r
197 putting the variable on the right hand side in a loop and solving. Below
\r
198 is our finished line routine :
\r
200 Procedure Line (x1,y1,x2,y2:integer;col:byte);
\r
201 VAR x,y,xlength,ylength,dx,dy:integer;
\r
202 xslope,yslope:real;
\r
204 xlength:=abs (x1-x2);
\r
205 if (x1-x2)<0 then dx:=-1;
\r
206 if (x1-x2)=0 then dx:=0;
\r
207 if (x1-x2)>0 then dx:=+1;
\r
208 ylength:=abs (y1-y2);
\r
209 if (y1-y2)<0 then dy:=-1;
\r
210 if (y1-y2)=0 then dy:=0;
\r
211 if (y1-y2)>0 then dy:=+1;
\r
212 if (dy=0) then BEGIN
\r
213 if dx<0 then for x:=x1 to x2 do
\r
214 putpixel (x,y1,col);
\r
215 if dx>0 then for x:=x2 to x1 do
\r
216 putpixel (x,y1,col);
\r
219 if (dx=0) then BEGIN
\r
220 if dy<0 then for y:=y1 to y2 do
\r
221 putpixel (x1,y,col);
\r
222 if dy>0 then for y:=y2 to y1 do
\r
223 putpixel (x1,y,col);
\r
226 xslope:=xlength/ylength;
\r
227 yslope:=ylength/xlength;
\r
228 if (yslope/xslope<1) and (yslope/xslope>-1) then BEGIN
\r
229 if dx<0 then for x:=x1 to x2 do BEGIN
\r
230 y:= round (yslope*x);
\r
231 putpixel (x,y,col);
\r
233 if dx>0 then for x:=x2 to x1 do BEGIN
\r
234 y:= round (yslope*x);
\r
235 putpixel (x,y,col);
\r
240 if dy<0 then for y:=y1 to y2 do BEGIN
\r
241 x:= round (xslope*y);
\r
242 putpixel (x,y,col);
\r
244 if dy>0 then for y:=y2 to y1 do BEGIN
\r
245 x:= round (xslope*y);
\r
246 putpixel (x,y,col);
\r
251 Quite big, isn't it? Here is a much shorter way of doing much the same
\r
254 function sgn(a:real):integer;
\r
256 if a>0 then sgn:=+1;
\r
257 if a<0 then sgn:=-1;
\r
258 if a=0 then sgn:=0;
\r
261 procedure line(a,b,c,d,col:integer);
\r
262 var u,s,v,d1x,d1y,d2x,d2y,m,n:real;
\r
281 FOR i := 0 TO round(m) DO
\r
289 b := b + round(d1y);
\r
293 a := a + round(d2x);
\r
294 b := b + round(d2y);
\r
299 This routine is very fast, and should meet almost all of your requirements
\r
300 (ASPHYXIA used it for quite a while before we made our new one.)
\r
301 In the end program, both the new line routine and the circle routine are
\r
302 tested. A few of the procedures of the first parts are also used.
\r
304 Line and circle routines may seem like fairly trivial things, but they are
\r
305 a vital component of many programs, and you may like to look up other
\r
306 methods of drawing them in books in the library (I know that here at the
\r
307 varsity they have books for doing this kind of stuff all over the place)
\r
308 A good line routine to look out for is the Bressenhams line routine ...
\r
309 there is a Bressenhams circle routine too ... I have documentaiton for them
\r
310 if anybody is interested, they are by far some of the fastest routines
\r
313 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
\r
316 Varsity has started again, so I am (shock) going to bed before three in
\r
317 the morning, so my quote this week wasn't written in the same wasted way
\r
318 my last weeks one was (For last week's one, I had gotten 8 hours sleep in
\r
319 3 days, and thought up and wrote the quote at 2:23 am before I fell asleep.)
\r
321 [ "What does it do?" she asks.
\r
322 "It's a computer," he replies.
\r
323 "Yes, dear, but what does it do?"
\r
324 "It ..er.. computes! It's a computer."
\r
325 "What does it compute?"
\r
326 "What? Er? Um. Numbers! Yes, numbers!" He smiles
\r
329 "Why? Well ..um.. why?" He starts to sweat.
\r
330 "I mean, is it just something to dust around, or does
\r
331 it actually do something useful?"
\r
332 "Um...you can call other computers with it!" Hope lights
\r
333 up his eyes. "So you can get programs from other computers!"
\r
334 "I see. Tell me, what do these programs do?"
\r
335 "Do? I don't think I fol..."
\r
336 "I see. They compute. Numbers. For no particular reason." He
\r
337 withers under her gaze.
\r
339 She smiles, and he trails off, defeated. She takes another look
\r
340 at the thing. "Although," she says, with a strange look in
\r
341 her eyes. He looks up, an insane look of hope on his
\r
342 face. "Does it come in pink?" she asks.
\r
352 ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÂÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
\r
362 Pall : Array [1..199,1..3] of byte;
\r
363 { This is our temporary pallette. We ony use colors 1 to 199, so we
\r
364 only have variables for those ones. }
\r
366 {ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ}
\r
367 Procedure SetMCGA; { This procedure gets you into 320x200x256 mode. }
\r
376 {ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ}
\r
377 Procedure SetText; { This procedure returns you to text mode. }
\r
386 {ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ}
\r
387 Procedure Putpixel (X,Y : Integer; Col : Byte);
\r
388 { This puts a pixel on the screen by writing directly to memory. }
\r
390 Mem [VGA:X+(Y*320)]:=Col;
\r
394 {ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ}
\r
395 procedure WaitRetrace; assembler;
\r
411 {ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ}
\r
412 Procedure Pal(ColorNo : Byte; R,G,B : Byte);
\r
413 { This sets the Red, Green and Blue values of a certain color }
\r
415 Port[$3c8] := ColorNo;
\r
422 {ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ}
\r
423 Procedure Circle (X,Y,rad:integer;Col:Byte);
\r
424 { This draws a circle with centre X,Y, with Rad as it's radius }
\r
429 X:=round(rad*COS (deg));
\r
430 Y:=round(rad*sin (deg));
\r
431 putpixel (x+160,y+100,col);
\r
437 {ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ}
\r
438 Procedure Line2 (x1,y1,x2,y2:integer;col:byte);
\r
439 { This draws a line from x1,y1 to x2,y2 using the first method }
\r
440 VAR x,y,xlength,ylength,dx,dy:integer;
\r
441 xslope,yslope:real;
\r
443 xlength:=abs (x1-x2);
\r
444 if (x1-x2)<0 then dx:=-1;
\r
445 if (x1-x2)=0 then dx:=0;
\r
446 if (x1-x2)>0 then dx:=+1;
\r
447 ylength:=abs (y1-y2);
\r
448 if (y1-y2)<0 then dy:=-1;
\r
449 if (y1-y2)=0 then dy:=0;
\r
450 if (y1-y2)>0 then dy:=+1;
\r
451 if (dy=0) then BEGIN
\r
452 if dx<0 then for x:=x1 to x2 do
\r
453 putpixel (x,y1,col);
\r
454 if dx>0 then for x:=x2 to x1 do
\r
455 putpixel (x,y1,col);
\r
458 if (dx=0) then BEGIN
\r
459 if dy<0 then for y:=y1 to y2 do
\r
460 putpixel (x1,y,col);
\r
461 if dy>0 then for y:=y2 to y1 do
\r
462 putpixel (x1,y,col);
\r
465 xslope:=xlength/ylength;
\r
466 yslope:=ylength/xlength;
\r
467 if (yslope/xslope<1) and (yslope/xslope>-1) then BEGIN
\r
468 if dx<0 then for x:=x1 to x2 do BEGIN
\r
469 y:= round (yslope*x);
\r
470 putpixel (x,y,col);
\r
472 if dx>0 then for x:=x2 to x1 do BEGIN
\r
473 y:= round (yslope*x);
\r
474 putpixel (x,y,col);
\r
479 if dy<0 then for y:=y1 to y2 do BEGIN
\r
480 x:= round (xslope*y);
\r
481 putpixel (x,y,col);
\r
483 if dy>0 then for y:=y2 to y1 do BEGIN
\r
484 x:= round (xslope*y);
\r
485 putpixel (x,y,col);
\r
491 {ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ}
\r
492 procedure line(a,b,c,d,col:integer);
\r
493 { This draws a line from x1,y1 to x2,y2 using the first method }
\r
495 function sgn(a:real):integer;
\r
497 if a>0 then sgn:=+1;
\r
498 if a<0 then sgn:=-1;
\r
499 if a=0 then sgn:=0;
\r
502 var u,s,v,d1x,d1y,d2x,d2y,m,n:real;
\r
521 FOR i := 0 TO round(m) DO
\r
529 b := b + round(d1y);
\r
533 a := a + round(d2x);
\r
534 b := b + round(d2y);
\r
540 {ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ}
\r
542 { This procedure mucks about with our "virtual pallette", then shoves it
\r
544 Var Tmp : Array[1..3] of Byte;
\r
545 { This is used as a "temporary color" in our pallette }
\r
548 Move(Pall[199],Tmp,3);
\r
549 { This copies color 199 from our virtual pallette to the Tmp variable }
\r
550 Move(Pall[1],Pall[2],198*3);
\r
551 { This moves the entire virtual pallette up one color }
\r
552 Move(Tmp,Pall[1],3);
\r
553 { This copies the Tmp variable to the bottom of the virtual pallette }
\r
555 For loop1:=1 to 199 do
\r
556 pal (loop1,pall[loop1,1],pall[loop1,2],pall[loop1,3]);
\r
562 Writeln ('This sample program will test out our line and circle algorithms.');
\r
563 Writeln ('In the first part, many circles will be draw creating (hopefully)');
\r
564 Writeln ('a "tunnel" effect. I will the rotate the pallete to make it look');
\r
565 Writeln ('nice. I will then draw some lines and rotate the pallette on them');
\r
566 Writeln ('too. Note : I am using the slower (first) line algorithm (in');
\r
567 Writeln ('procedure line2). Change it to Procedure Line and it will be using');
\r
568 Writeln ('the second line routine. NB : For descriptions on how pallette works');
\r
569 Writeln ('have a look at part two of this series; I won''t re-explain it here.');
\r
571 Writeln ('Remember to send me any work you have done, I am most eager to help.');
\r
573 Writeln ('Hit any key to continue ...');
\r
577 For Loop1 := 1 to 199 do BEGIN
\r
578 Pall[Loop1,1] := Loop1 mod 30+33;
\r
579 Pall[Loop1,2] := 0;
\r
580 Pall[Loop1,3] := 0;
\r
582 { This sets colors 1 to 199 to values between 33 to 63. The MOD
\r
583 function gives you the remainder of a division, ie. 105 mod 10 = 5 }
\r
586 For loop1:=1 to 199 do
\r
587 pal (loop1,pall[loop1,1],pall[loop1,2],pall[loop1,3]);
\r
588 { This sets the true pallette to variable Pall }
\r
590 for loop1:=1 to 90 do
\r
591 circle (160,100,loop1,loop1);
\r
592 { This draws 90 circles all with centres at 160,100; with increasing
\r
593 radii and colors. }
\r
600 for loop1:=1 to 199 do
\r
601 line2 (0,1,319,loop1,loop1); { *** Replace Line2 with Line to use the
\r
602 second line algorithm *** }
\r
603 { This draws 199 lines, all starting at 0,1 }
\r
611 Writeln ('All done. Okay, so maybe it wasn''t a tunnel effect, but you get the');
\r
612 Writeln ('general idea ;-) This concludes the third sample program in the ASPHYXIA');
\r
613 Writeln ('Training series. You may reach DENTHOR under the name of GRANT SMITH');
\r
614 Writeln ('on the MailBox BBS, or leave a message to ASPHYXIA on the ASPHYXIA BBS.');
\r
615 Writeln ('Get the numbers from Roblist, or write to :');
\r
616 Writeln (' Grant Smith');
\r
617 Writeln (' P.O. Box 270');
\r
618 Writeln (' Kloof');
\r
620 Writeln ('I hope to hear from you soon!');
\r
622 Write ('Hit any key to exit ...');
\r