Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
back2root:archives:denthor:part-06 [2021/09/02 10:27] – créée frater | back2root:archives:denthor:part-06 [2024/08/27 08:55] (Version actuelle) – [How do I generate a lookup table?] frater | ||
---|---|---|---|
Ligne 31: | Ligne 31: | ||
One way of looking at a lookup table is as follows : Let us say that for some obscure reason you need to calculate a lot of multiplications (eg. 5*5 , 7*4 , 9*2 etc.). Instead of actually doing a slow multiply each time, you can generate a kind of bonds table, as seen below : | One way of looking at a lookup table is as follows : Let us say that for some obscure reason you need to calculate a lot of multiplications (eg. 5*5 , 7*4 , 9*2 etc.). Instead of actually doing a slow multiply each time, you can generate a kind of bonds table, as seen below : | ||
+ | {{tablelayout? | ||
+ | ^ X ^ 1 ^ 2 | ||
+ | ^ 1 | 1 | 2 | ||
+ | ^ 2 | 2 | 4 | ||
+ | ^ 3 | 3 | 6 | ||
+ | ^ 4 | 4 | 8 | ||
+ | ^ 5 | 5 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | | ||
+ | ^ 6 | 6 | 12 | 18 | 24 | 30 | 36 | 42 | 48 | 54 | | ||
+ | ^ 7 | 7 | 14 | 21 | 28 | 35 | 42 | 49 | 56 | 63 | | ||
+ | ^ 8 | 8 | 16 | 24 | 32 | 40 | 48 | 56 | 64 | 72 | | ||
+ | ^ 9 | 9 | 18 | 27 | 36 | 45 | 54 | 63 | 72 | 81 | | ||
+ | |||
- | < | ||
- | ╔═Đ═╦═══════Đ══════Đ══════Đ══════Đ══════Đ══════Đ══════Đ══════Đ══════╗ | ||
- | ă─┼─Â | ||
- | ă─┴─Î═══════ě══════ě══════ě══════ě══════ě══════ě══════ě══════ě══════Á | ||
- | ║ 1 ║ | ||
- | ă───Î───────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┤ | ||
- | ║ 2 ║ | ||
- | ă───Î───────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┤ | ||
- | ║ 3 ║ | ||
- | ă───Î───────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┤ | ||
- | ║ 4 ║ | ||
- | ă───Î───────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┤ | ||
- | ║ 5 ║ | ||
- | ă───Î───────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┤ | ||
- | ║ 6 ║ | ||
- | ă───Î───────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┤ | ||
- | ║ 7 ║ | ||
- | ă───Î───────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┤ | ||
- | ║ 8 ║ | ||
- | ă───Î───────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼──────┤ | ||
- | ║ 9 ║ | ||
- | ╚═══╩───────┴──────┴──────┴──────┴──────┴──────┴──────┴──────┴──────┘ | ||
- | </ | ||
This means that instead of calculating 9*4, you just find the 9 on the top and the 4 on the side, and the resulting number is the answer. This type of table is very useful when the equations are very long to do. | This means that instead of calculating 9*4, you just find the 9 on the top and the 4 on the side, and the resulting number is the answer. This type of table is very useful when the equations are very long to do. | ||
- | The example I am going to use for this part is that of circles. Cast your minds back to Part 3 on lines and circles. The circle section took quite a while to finish drawing, mainly because I had to calculate the SIN and COS for EVERY SINGLE POINT. Calculating SIN and COS is obviously very slow, and that was reflected in the speed of the section. | + | The example I am going to use for this part is that of circles. Cast your minds back to [[back2root: |
==== How do I generate a lookup table? ==== | ==== How do I generate a lookup table? ==== | ||
Ligne 72: | Ligne 61: | ||
Then in the program we get the memory for these two pointers. Asphyxia was originally thinking of calling itself Creative Reboot Inc., mainly because we always forgot to get the necessary memory for our pointers. | Then in the program we get the memory for these two pointers. Asphyxia was originally thinking of calling itself Creative Reboot Inc., mainly because we always forgot to get the necessary memory for our pointers. | ||
+ | |||
(Though a bit of creative assembly coding also contributed to this. We wound up rating our reboots on a scale of 1 to 10 ;-)). | (Though a bit of creative assembly coding also contributed to this. We wound up rating our reboots on a scale of 1 to 10 ;-)). | ||
- | The next obvious step is to place our necessary answers into our lookup tables. This can | + | The next obvious step is to place our necessary answers into our lookup tables. This can take a bit of time, so in a demo, you would do it in the very beginning (people just think it's slow disk access or something), or after you have shown a picture (while the viewer is admiring it, you are calculating pi to its 37th degree in the background ;-)) |
- | take a bit of time, so in a demo, you would do it in the very beginning (people just think it's slow disk access or something), or after you have shown a picture (while the viewer is admiring it, you are calculating pi to its 37th degree in the background ;-)) | + | |
Another way of doing it is, after calculating it once, you save it to a file which you then load into the variable at the beginning of the program. Anyway, this is how we will calculate the table for our circle : | Another way of doing it is, after calculating it once, you save it to a file which you then load into the variable at the beginning of the program. Anyway, this is how we will calculate the table for our circle : | ||
Ligne 125: | Ligne 114: | ||
Denthor | Denthor | ||
+ | |||
+ | ==== Code Source ==== | ||
+ | |||
+ | === PASCAL === | ||
+ | |||
+ | <code pascal> | ||
+ | (*****************************************************************************) | ||
+ | (* *) | ||
+ | (* TUT6.PAS - VGA Trainer Program 6 (in Pascal) | ||
+ | (* *) | ||
+ | (* "The VGA Trainer Program" | ||
+ | (* was limited to Pascal only in its first run. All I have done is taken *) | ||
+ | (* his original release, translated it to C++, and touched up a few things. | ||
+ | (* I take absolutely no credit for the concepts presented in this code, and *) | ||
+ | (* am NOT the person to ask for help if you are having trouble. | ||
+ | (* *) | ||
+ | (* Program Notes : This program presents pregenerated arrays. | ||
+ | (* *) | ||
+ | (* Author | ||
+ | (* *) | ||
+ | (*****************************************************************************) | ||
+ | |||
+ | {$X+} | ||
+ | USES crt; | ||
+ | |||
+ | CONST VGA = $a000; | ||
+ | |||
+ | TYPE tbl = Array [1..8000] of real; | ||
+ | { This will be the shape of the ' | ||
+ | | ||
+ | |||
+ | VAR loop1: | ||
+ | Pall : Array [1..20, | ||
+ | { This is our temporary pallette. We ony use colors 1 to 20, so we | ||
+ | only have variables for those ones. } | ||
+ | |||
+ | {ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ} | ||
+ | Procedure SetMCGA; | ||
+ | BEGIN | ||
+ | asm | ||
+ | | ||
+ | | ||
+ | end; | ||
+ | END; | ||
+ | |||
+ | |||
+ | {ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ} | ||
+ | Procedure SetText; | ||
+ | BEGIN | ||
+ | asm | ||
+ | | ||
+ | | ||
+ | end; | ||
+ | END; | ||
+ | |||
+ | {ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ} | ||
+ | Procedure Cls (Col : Byte); | ||
+ | { This clears the screen to the specified color } | ||
+ | BEGIN | ||
+ | Fillchar (Mem [VGA: | ||
+ | END; | ||
+ | |||
+ | |||
+ | {ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ} | ||
+ | Procedure Putpixel (X,Y : Integer; Col : Byte); | ||
+ | { This puts a pixel on the screen by writing directly to memory. } | ||
+ | BEGIN | ||
+ | Mem [VGA: | ||
+ | END; | ||
+ | |||
+ | |||
+ | {ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ} | ||
+ | procedure WaitRetrace; | ||
+ | { This waits for a vertical retrace to reduce snow on the screen } | ||
+ | label | ||
+ | l1, l2; | ||
+ | asm | ||
+ | mov dx,3DAh | ||
+ | l1: | ||
+ | in al,dx | ||
+ | and al,08h | ||
+ | jnz l1 | ||
+ | l2: | ||
+ | in al,dx | ||
+ | and al,08h | ||
+ | jz l2 | ||
+ | end; | ||
+ | |||
+ | |||
+ | {ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ} | ||
+ | Procedure Pal(ColorNo : Byte; R,G,B : Byte); | ||
+ | { This sets the Red, Green and Blue values of a certain color } | ||
+ | Begin | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | End; | ||
+ | |||
+ | |||
+ | {ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ} | ||
+ | Function rad (theta : real) : real; | ||
+ | { This calculates the degrees of an angle } | ||
+ | BEGIN | ||
+ | rad := theta * pi / 180 | ||
+ | END; | ||
+ | |||
+ | |||
+ | |||
+ | {ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ} | ||
+ | Procedure NormCirc; | ||
+ | { This generates a spireal without using a lookup table } | ||
+ | VAR deg, | ||
+ | x, | ||
+ | |||
+ | BEGIN | ||
+ | gotoxy (1,1); | ||
+ | Writeln (' | ||
+ | for loop1:=60 downto 43 do BEGIN | ||
+ | deg:=0; | ||
+ | radius: | ||
+ | repeat | ||
+ | X: | ||
+ | Y: | ||
+ | putpixel (x+160, | ||
+ | deg: | ||
+ | radius: | ||
+ | until radius< | ||
+ | END; | ||
+ | END; | ||
+ | |||
+ | |||
+ | {ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ} | ||
+ | Procedure LookupCirc; | ||
+ | { This draws a spiral using a lookup table } | ||
+ | VAR radius: | ||
+ | x, | ||
+ | costbl : ^tbl; | ||
+ | sintbl : ^tbl; | ||
+ | |||
+ | Procedure Setupvars; | ||
+ | { This is a nested procedure (a procedure in a procedure), and may | ||
+ | | ||
+ | This section gets the memory for the table, then generates the | ||
+ | | ||
+ | VAR deg:real; | ||
+ | BEGIN | ||
+ | getmem (costbl, | ||
+ | getmem (sintbl, | ||
+ | deg:=0; | ||
+ | for loop1:=1 to 8000 do BEGIN { There are 360 degrees in a } | ||
+ | deg: | ||
+ | costbl^[loop1]: | ||
+ | sintbl^[loop1]: | ||
+ | END; { 360/ | ||
+ | END; | ||
+ | { NB : For greater accuracy I increase the degrees by 0.4, because if I | ||
+ | | ||
+ | | ||
+ | is bigger, takes up more memory and is slower to calculate, but | ||
+ | the finished product looks better.} | ||
+ | |||
+ | BEGIN | ||
+ | cls (0); | ||
+ | gotoxy (1,1); | ||
+ | Writeln (' | ||
+ | setupvars; | ||
+ | gotoxy (1,1); | ||
+ | Writeln ('With pregenerated arrays.' | ||
+ | for loop1:=60 downto 43 do BEGIN | ||
+ | pos:=1; | ||
+ | radius: | ||
+ | repeat | ||
+ | X:=round (radius*costbl^[pos]); | ||
+ | Y:=round (radius*sintbl^[pos]); | ||
+ | putpixel (x+160, | ||
+ | radius: | ||
+ | inc (pos); | ||
+ | if pos>8000 then pos: | ||
+ | { must never exceed that, or the program | ||
+ | { will probably crash. | ||
+ | until radius< | ||
+ | END; | ||
+ | freemem (costbl, | ||
+ | freemem (sintbl, | ||
+ | END; | ||
+ | |||
+ | |||
+ | {ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ} | ||
+ | Procedure PalPlay; | ||
+ | { This procedure mucks about with our " | ||
+ | to screen. } | ||
+ | Var Tmp : Array[1..3] of Byte; | ||
+ | { This is used as a " | ||
+ | loop1 : Integer; | ||
+ | BEGIN | ||
+ | | ||
+ | { This copies color 1 from our virtual pallette to the Tmp variable } | ||
+ | | ||
+ | { This moves the entire virtual pallette down one color } | ||
+ | | ||
+ | { This copies the Tmp variable to no. 18 of the virtual pallette } | ||
+ | | ||
+ | For loop1:=1 to 18 do | ||
+ | pal (loop1, | ||
+ | END; | ||
+ | |||
+ | |||
+ | BEGIN | ||
+ | ClrScr; | ||
+ | writeln ('Hi there! This program will demonstrate the usefullness of '); | ||
+ | writeln (' | ||
+ | writeln ('will first draw a spiral without using a lookup table, rotate' | ||
+ | writeln ('the pallette until a key is pressed, the calculate the lookup' | ||
+ | writeln (' | ||
+ | writeln; | ||
+ | writeln ('This is merely one example for the wide range of uses of a '); | ||
+ | writeln (' | ||
+ | writeln; | ||
+ | writeln; | ||
+ | Write (' | ||
+ | Readkey; | ||
+ | setmcga; | ||
+ | directvideo: | ||
+ | { Writeln in GFX mode. Hit CTRL-F1 on it for more } | ||
+ | { info/help } | ||
+ | For Loop1 := 1 to 18 do BEGIN | ||
+ | Pall[Loop1, | ||
+ | Pall[Loop1, | ||
+ | Pall[Loop1, | ||
+ | END; | ||
+ | { This sets colors 1 to 18 to values between 12 to 63. } | ||
+ | |||
+ | | ||
+ | For loop1:=1 to 18 do | ||
+ | pal (loop1, | ||
+ | { This sets the true pallette to variable Pall } | ||
+ | |||
+ | normcirc; | ||
+ | Repeat | ||
+ | PalPlay; | ||
+ | Until keypressed; | ||
+ | readkey; | ||
+ | lookupcirc; | ||
+ | Repeat | ||
+ | PalPlay; | ||
+ | Until keypressed; | ||
+ | Readkey; | ||
+ | |||
+ | SetText; | ||
+ | Writeln ('All done. This concludes the sixth sample program in the ASPHYXIA' | ||
+ | Writeln (' | ||
+ | Writeln (' | ||
+ | Writeln (' | ||
+ | Writeln ('Get the numbers from Roblist, or write to :'); | ||
+ | Writeln (' | ||
+ | Writeln (' | ||
+ | Writeln (' | ||
+ | Writeln (' | ||
+ | Writeln ('I hope to hear from you soon!' | ||
+ | Writeln; Writeln; | ||
+ | Write | ||
+ | Readkey; | ||
+ | END. | ||
+ | </ | ||
+ | |||
+ | === C === | ||
+ | |||
+ | <code c> | ||
+ | ///////////////////////////////////////////////////////////////////////////// | ||
+ | // // | ||
+ | // TUTPROG6.CPP - VGA Trainer Program 6 (in Turbo C++ 3.0) // | ||
+ | // // | ||
+ | // "The VGA Trainer Program" | ||
+ | // was limited to Pascal only in its first run. All I have done is taken // | ||
+ | // his original release, translated it to C++ and touched up a few things. // | ||
+ | // I take absolutely no credit for the concepts presented in this code and // | ||
+ | // am NOT the person to ask for help if you are having trouble. | ||
+ | // // | ||
+ | // Program Notes : This program demonstrates the advantages of // | ||
+ | // | ||
+ | // // | ||
+ | // The Compact memory model (-mc) seems to provide the // | ||
+ | // best results for this tutorial. | ||
+ | // | ||
+ | // | ||
+ | // // | ||
+ | // Author | ||
+ | // Translator | ||
+ | // // | ||
+ | // Last Modified : January 4, 1995 // | ||
+ | // // | ||
+ | ///////////////////////////////////////////////////////////////////////////// | ||
+ | |||
+ | // // | ||
+ | // INCLUDE FILES // | ||
+ | // // | ||
+ | |||
+ | #include < | ||
+ | // clrscr(), getch(), kbhit(), gotoxy(), | ||
+ | // directvideo | ||
+ | #include < | ||
+ | // MK_FP, geninterrupt() | ||
+ | #include < | ||
+ | // cout, memset(), memmove() | ||
+ | #include < | ||
+ | // sin(), cos() | ||
+ | #include < | ||
+ | // calloc(), free(), exit() | ||
+ | |||
+ | // // | ||
+ | // CONSTANTS // | ||
+ | // // | ||
+ | |||
+ | const PI = 3.1415927; | ||
+ | const TABLESIZE = 900; | ||
+ | |||
+ | // // | ||
+ | // FUNCTION PROTOTYPES // | ||
+ | // // | ||
+ | |||
+ | // UTILITY FUNCTIONS | ||
+ | void SetMCGA(); | ||
+ | void SetText(); | ||
+ | void Cls(unsigned char Col); | ||
+ | void Pal(unsigned char ColorNo, unsigned char R, | ||
+ | unsigned char G, | ||
+ | void Putpixel(int x, int y, unsigned char Col); | ||
+ | void WaitRetrace(); | ||
+ | float rad(float theta); | ||
+ | |||
+ | // MID-LEVEL FUNCTIONS | ||
+ | void NormCirc(); | ||
+ | void LookupCirc(); | ||
+ | void PalPlay(); | ||
+ | |||
+ | // // | ||
+ | // GLOBAL VARIABLE DECLARATIONS // | ||
+ | // // | ||
+ | |||
+ | // pointer to the offset of the VGA memory | ||
+ | unsigned char *vga = (unsigned char *) MK_FP(0xA000, | ||
+ | |||
+ | // This is our temporary pallette. | ||
+ | // only have variables for those ones. | ||
+ | unsigned char Pall[18][3]; | ||
+ | |||
+ | |||
+ | /////////////////////////////////////////////////////////////////////////////// | ||
+ | // // | ||
+ | // MAIN FUNCTION | ||
+ | // // | ||
+ | /////////////////////////////////////////////////////////////////////////////// | ||
+ | |||
+ | void main() { | ||
+ | |||
+ | int loop1; | ||
+ | |||
+ | clrscr(); | ||
+ | cout | ||
+ | << "Hi there! This program will demonstrate the usefullness of\n" | ||
+ | << " | ||
+ | << "will first draw a spiral without using a lookup table, rotate\n" | ||
+ | << "the pallette until a key is pressed, the calculate the lookup\n" | ||
+ | << " | ||
+ | cout | ||
+ | << "This is merely one example for the wide range of uses of a\n" | ||
+ | << " | ||
+ | cout | ||
+ | << "Hit any key to contine ..."; | ||
+ | getch(); | ||
+ | SetMCGA(); | ||
+ | |||
+ | // This handy trick allows you to use gotoxy() and cout in graphics mode. | ||
+ | directvideo = 0; | ||
+ | |||
+ | // This sets our pallette colors 0 to 17 with red values between 12 to 63. | ||
+ | for (loop1=0; | ||
+ | Pall[loop1][0] = ((loop1+1)*3)+9; | ||
+ | Pall[loop1][1] = 0; | ||
+ | Pall[loop1][2] = 0; | ||
+ | } | ||
+ | |||
+ | WaitRetrace(); | ||
+ | |||
+ | // This sets the true pallette to variable Pall | ||
+ | for (loop1=0; | ||
+ | // loop+1 (make sure to leave color 0 for black) | ||
+ | Pal(loop1+1, | ||
+ | |||
+ | // This draws a spiral without lookups | ||
+ | NormCirc(); | ||
+ | while (!kbhit()) PalPlay(); | ||
+ | getch(); // clear the keyboard buffer after kbhit() | ||
+ | |||
+ | // This draws a spiral with lookups | ||
+ | LookupCirc(); | ||
+ | while (!kbhit()) PalPlay(); | ||
+ | getch(); | ||
+ | |||
+ | getch(); | ||
+ | SetText(); | ||
+ | cout | ||
+ | << "All done. This concludes the sixth sample program in the ASPHYXIA\n" | ||
+ | << " | ||
+ | << "SMITH on the MailBox BBS, or leave a message to ASPHYXIA on the\n" | ||
+ | << " | ||
+ | << "Get the numbers from Roblist, or write to :\n" | ||
+ | << " | ||
+ | << " | ||
+ | << " | ||
+ | << " | ||
+ | << "I hope to hear from you soon!\n\n"; | ||
+ | cout | ||
+ | << "Hit any key to exit ..."; | ||
+ | getch(); | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | ///////////////////////////////////////////////////////////////////////////// | ||
+ | // // | ||
+ | // SetMCGA() - This function gets you into 320x200x256 mode. // | ||
+ | // // | ||
+ | ///////////////////////////////////////////////////////////////////////////// | ||
+ | |||
+ | void SetMCGA() { | ||
+ | _AX = 0x0013; | ||
+ | geninterrupt (0x10); | ||
+ | } | ||
+ | |||
+ | |||
+ | ///////////////////////////////////////////////////////////////////////////// | ||
+ | // // | ||
+ | // SetText() - This function gets you into text mode. // | ||
+ | // // | ||
+ | ///////////////////////////////////////////////////////////////////////////// | ||
+ | |||
+ | void SetText() { | ||
+ | _AX = 0x0003; | ||
+ | geninterrupt (0x10); | ||
+ | } | ||
+ | |||
+ | ///////////////////////////////////////////////////////////////////////////// | ||
+ | // // | ||
+ | // Cls() - This clears the screen to the specified color. | ||
+ | // // | ||
+ | ///////////////////////////////////////////////////////////////////////////// | ||
+ | |||
+ | void Cls(unsigned char Col) { | ||
+ | memset(vga, Col, 0xffff); | ||
+ | } | ||
+ | |||
+ | |||
+ | ///////////////////////////////////////////////////////////////////////////// | ||
+ | // // | ||
+ | // Pal() - This sets the Red, Green, and Blue values of a certain color. | ||
+ | // // | ||
+ | ///////////////////////////////////////////////////////////////////////////// | ||
+ | |||
+ | void Pal(unsigned char ColorNo, unsigned char R, | ||
+ | unsigned char G, | ||
+ | |||
+ | outp (0x03C8, | ||
+ | outp (0x03C9,R); | ||
+ | outp (0x03C9,G); | ||
+ | outp (0x03C9,B); | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | ///////////////////////////////////////////////////////////////////////////// | ||
+ | // // | ||
+ | // Putpixel() - This puts a pixel at X,Y using color Col, on VGA or the // | ||
+ | // Virtual Screen; | ||
+ | // // | ||
+ | ///////////////////////////////////////////////////////////////////////////// | ||
+ | |||
+ | void Putpixel (int x, int y, unsigned char Col) { | ||
+ | memset(vga+x+(y*320), | ||
+ | } | ||
+ | |||
+ | |||
+ | ///////////////////////////////////////////////////////////////////////////// | ||
+ | // // | ||
+ | // WaitRetrace() - This waits until you are in a Verticle Retrace. | ||
+ | // // | ||
+ | ///////////////////////////////////////////////////////////////////////////// | ||
+ | |||
+ | void WaitRetrace() { | ||
+ | |||
+ | _DX = 0x03DA; | ||
+ | |||
+ | l1: asm { | ||
+ | in al,dx; | ||
+ | and al,0x08; | ||
+ | jnz l1; | ||
+ | } | ||
+ | |||
+ | l2: asm { | ||
+ | in al,dx; | ||
+ | and al,0x08; | ||
+ | jz l2; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | ///////////////////////////////////////////////////////////////////////////// | ||
+ | // // | ||
+ | // rad() - This calculates the degrees of an angle. | ||
+ | // // | ||
+ | ///////////////////////////////////////////////////////////////////////////// | ||
+ | |||
+ | float rad(float theta) { | ||
+ | return ((theta * PI)/180); | ||
+ | } | ||
+ | |||
+ | |||
+ | ///////////////////////////////////////////////////////////////////////////// | ||
+ | // // | ||
+ | // NormCirc() - This generates a spiral without using a lookup table. | ||
+ | // // | ||
+ | ///////////////////////////////////////////////////////////////////////////// | ||
+ | |||
+ | void NormCirc() { | ||
+ | |||
+ | int loop1,x,y; | ||
+ | float deg,radius; | ||
+ | |||
+ | gotoxy(1, | ||
+ | cout << " | ||
+ | |||
+ | for (loop1=60; | ||
+ | deg = 0.0; | ||
+ | radius = (float) loop1; | ||
+ | |||
+ | do { | ||
+ | x = radius*cos(rad(deg)); | ||
+ | y = radius*sin(rad(deg)); | ||
+ | Putpixel(x+160, | ||
+ | deg += 0.4; // Increase the degree so the circle is round | ||
+ | radius -= 0.019; | ||
+ | |||
+ | // NOTE: If you change this last statement to " | ||
+ | // the Pascal code has it, you will get holes in the spiral. | ||
+ | // get bonus credit if you can tell me why this is. | ||
+ | |||
+ | } | ||
+ | while (radius >= 0.0); // continue until the radius is zero (at center) | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | ///////////////////////////////////////////////////////////////////////////// | ||
+ | // // | ||
+ | // LookupCirc() - This draws a spiral using a lookup table. | ||
+ | // // | ||
+ | ///////////////////////////////////////////////////////////////////////////// | ||
+ | |||
+ | void LookupCirc() { | ||
+ | |||
+ | int x, y, pos, loop1; | ||
+ | float radius, deg; | ||
+ | float *costbl=NULL, | ||
+ | |||
+ | // allocate memory for the two tables | ||
+ | costbl = (float *) calloc(TABLESIZE, | ||
+ | sintbl = (float *) calloc(TABLESIZE, | ||
+ | |||
+ | // always check to see if enough memory was allocated | ||
+ | if ((costbl == NULL) || (sintbl == NULL)) { | ||
+ | SetText(); | ||
+ | cout << " | ||
+ | exit(1); | ||
+ | } | ||
+ | |||
+ | Cls(0); | ||
+ | gotoxy(1, | ||
+ | cout << " | ||
+ | |||
+ | // There are 360 degrees in a circle. | ||
+ | // the number of needed parts of the table is 360/ | ||
+ | // | ||
+ | // For greater accuracy I increase the degrees by 0.4, because if I | ||
+ | // increase them by one, holes are left in the final product as a | ||
+ | // result of the rounding error margin. This means the pregenerated array | ||
+ | // is bigger, takes up more memory and is slower to calculate, but | ||
+ | // the finished product looks better. | ||
+ | |||
+ | deg = 0.0; | ||
+ | for (loop1=0; | ||
+ | costbl[loop1] = cos(rad(deg)); | ||
+ | sintbl[loop1] = sin(rad(deg)); | ||
+ | deg += 0.4; | ||
+ | } | ||
+ | |||
+ | gotoxy(1, | ||
+ | cout << "With pregenerated arrays."; | ||
+ | |||
+ | // NOTE: The spiral drawn with this function leaves a small piece missing | ||
+ | // on the right side. It looks like a pie with a piece cut out. You get | ||
+ | // extra credit if you can tell me why this is. | ||
+ | |||
+ | for (loop1=60; | ||
+ | pos = 0; | ||
+ | radius = loop1; | ||
+ | do { | ||
+ | // Note how I am not recalculating sin and cos for each point. | ||
+ | x = radius*costbl[pos]; | ||
+ | y = radius*sintbl[pos]; | ||
+ | Putpixel(x+160, | ||
+ | // Decrease the radius for a spiral effect | ||
+ | radius -= 0.020; | ||
+ | // I only made a table from 1 to 900, so it must never exceed that, | ||
+ | // or the program will probably crash. | ||
+ | // pos while you are at it (more compact but less readable). :) | ||
+ | if (++pos > TABLESIZE-1) pos = 0; | ||
+ | } | ||
+ | while (radius >= 0.0); | ||
+ | } | ||
+ | |||
+ | // Freeing the memory taken up by the tables. This is very important. | ||
+ | free(costbl); | ||
+ | free(sintbl); | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | ///////////////////////////////////////////////////////////////////////////// | ||
+ | // // | ||
+ | // PalPlay() - This function mucks about with our " | ||
+ | // | ||
+ | // // | ||
+ | ///////////////////////////////////////////////////////////////////////////// | ||
+ | |||
+ | void PalPlay() { | ||
+ | |||
+ | // This is used as a " | ||
+ | unsigned char Tmp[3]; | ||
+ | int loop1; | ||
+ | |||
+ | // This copies color 0 from our virtual pallette to the Tmp variable. | ||
+ | memmove(Tmp, | ||
+ | |||
+ | // This moves the entire virtual pallette down one color. | ||
+ | memmove(Pall[0], | ||
+ | |||
+ | // This copies the Tmp variable to the bottom of the virtual pallette. | ||
+ | // Don't change 0: leave this always black to not change overscan color. | ||
+ | memmove(Pall[17], | ||
+ | |||
+ | WaitRetrace(); | ||
+ | for (loop1=0; | ||
+ | | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | <nspages back2root/ |