Spellcaster presents: TTTTTTTTTT HH HH EEEEEEEEEE MM MM AAAA GGGGGGGGG TT HH HH EE MMM MMM AA AA GG TT HH HH EE MM M M MM AA AA GG TT HHHHHHHHHH EEEEEE MM MM MM AAAAAAAA GG TT HH HH EE MM MM AA AA GG GGGG TT HH HH EE MM MM AA AA GG GG TT HH HH EEEEEEEEEE MM MM AA AA GGGGGGGG Issue 4 25-10-95 þ Index 1. Introduction 1.1. About the magazine 1.2. About the author 1.3. Distribution 1.4. Contribuitions 1.5. Hellos and greets 2. Bikes and programs - What to they have in common ? 2.1. Introduction 2.2. The For cicle 2.3. The Repeat...Until... 2.4. While loop 3. Graphics, part 3 : Lines and circles 3.1. Lines 3.2. Circles 4. Hints and tips 5. Points of view 6. The adventures of Spellcaster, part 4 þ Introduction þ About the magazine Hello, everybody out there... Here it is: number four of 'The Mag', brought to you, as usual, by Spellcaster, alias Diogo de Andrade. As you may have noticed, this issue is very late, due to the beginning of scholl... But, never the less, here it is. This issue includes part III of my mode 13h tutorial, more begginers teaching and a new regular, called 'Hints and tips', something like a bunch of tricks I found out during my coding days. I want to put there tips for everybody, from the begginner to the advanced programmer. This magazine is dedicated to all the programmers and would-be programmers out there, especially to those that (like me) can't access the Net easily to get valuable information, and to those who wish to learn how to program anything, from demos to games, passing through utilities and all sort of thing your mind can think of. Somobody told me that the magazine had things too complicated, like the article on pointers and the ones about graphics. As I said before, these aren't meant for the begginer, but for the more serious programmers. I must catter for everybody. So, if any begginers are worried because they don't understand shit, don't worry. Just continue to follow the begginers articles, and I will tell you when you can go back to previous issues. When you read this magazine, I'll assume some things. First, I assume you have Borland's Turbo Pascal, version 6 and upwards. I'll also think you have a 80386 (or 386 for short; a 486 would be even better), a load of patience and a sense of humor. This last is almost essencial, because I don't receive any money for doing this, so I must have fun doing it. I will also take for certain you have the 9th grade (or equivelent). Finally, I will assume that you have the last issues of 'The Mag', and that you have grasped the concepts I tried to transmit. If you don't have the issues, you can get them by mail, writing to the adress shown below. As I stated above, this magazine will be made especially for those who don't know where to get information, or want it all in the same place, and to those who want to learn how to program, so I'll try to build knowledge, building up your skills issue by issue. If you sometimes fail to grasp some concept, don't despair; try to work it out. That's what I did... Almost everything I know was learnt from painfull experience. If you re-re-re-read the article, and still can't understand it, just drop a line, by mail, or just plain forget it. Most of the things I try to teach here aren't linked to each other (unless I say so), so if you don't understand something, skip it and go back to it some weeks later. It should be clearer for you then. Likewise, if you see any terms or words you don't understand, follow the same measures as before. Ok, as I'm earing the Net gurus and other god-like creatures talking already, I'm just going to explain why I use Pascal. For starters, Pascal is a very good language, ideal for the beginner, like BASIC (yech!), but it's powerfull enough to make top-notch programms. Also, I'll will be using assembly language in later issues, and Pascal makes it so EASY to use. Finally, if you don't like my choice of language, you can stop whining. The teory behind each article is very simple, and common with any of the main languages (C, C++, Assembly - Yes, that's true... BASIC isn't a decent language). Just one last thing... The final part of the magazine is a little story made up by my distorted mind. It's just a little humor I like to write, and it hasn't got nothing to do with programming (well, it has a little), but, as I said before, I just like to write it. þ About the author Ok, so I'm a little egocentric, but tell me... If you had the trouble of writing hundreds of lines, wouldn't you like someone to know you, even by name ? My name is Diogo de Andrade, alias Spellcaster, and I'm the creator, editor and writer of this magazine. I live in a small town called Set£bal, just near Lisbon, the capital of Portugal... If you don't know where it is, get an encyclopedia, and look for Europe. Then, look for Spain. Next to it, there's Portugal, and Set£bal is in the middle. I'm 18 years old, and I just made it in to the university (if you do want to know, I'm in the Technical Institute of Lisbon, Portugal), so I'm not a God-Like creature, with dozens of years of practice (I only program by eight years now, and I started in a Spectrum, progressing later to an Amiga. I only program in the PC for a year or so), with a mega-computer (I own a 386SX, 16 Mhz), that wear glasses with lens that look like the bottom of a bottle (I use glasses, but only sometimes), that has his head bigger than a pumpkin (I have a normal sized head) and with an IQ of over 220 (mine is actually something like 180). I can program in C, C++, Pascal, Assembly and even BASIC (yech!). So, if I am a normal person, why do I spend time writing this ? Well, because I have the insane urge to write thousands of words every now and then, and while I'm at it, I may do something productive, like teaching someone. I may be young, but I know a lot about computers (how humble I am; I know, modesty isn't one of my qualities). Just one more thing, if you ever program anything, please send to me... I would love to see some work you got, maybe I even could learn something with it. Also, give me a greet in your program/game/demo... I love seeing my name. þ Contributions I as I stated before, I'm not a God... I do make mistakes, and I don't have (always) the best way of doing things. So, if you think you've spotted an error, or you have thought of a better way of doing things, let me know. I'll be happy to receive anything, even if it is just mail saying 'Keep it up'. As all human beings, I need incentive. Also, if you do like to write, please do... Send in articles, they will be welcome, and you will have the chance to see your names up in lights. They can be about anything, for a review of a book or program that can help a programmer, to a point of view or a moan. If anyone out there has a question or wants to see an article about something in particular, feel free to write... All letters will be answered, provided you give me your address. I'm also trying to start a new demo/game/utility group, and I need all sort of people, from coders (sometimes, one isn't enough), musicians (I can compose, but I'm a bit limited), graphics artists (I can't draw nothing) and spreaders... I mean, by a spreader, someone who spreads things, like this mag. If you have a BBS and you want it to include this magazine, feel free to write me... You can also contact me personally, if study on the IST (if you don't know what the IST is, you don't study there). I'm the freshman with the black hair and dark-brown eyes... Yes, the one that is occupying one of the X-terminals... I recommend you to contact me personally, if you can, especially if you are a member of the opposite sex (I'm a man, for those of you who are wondering). My adress is: Praceta Carlos Manito Torres, n§4/6§C 2900 Set£bal Portugal Email: dgan@rnl.ist.utl þ Hellos and greets I'll say hellos and thanks to all my friend, especially for those who put up with my constant whining (you know who you are). Special greets go to Denthor from Asphyxia (for the excelent VGA trainers), Draeden from VLA (for assembly tutorials), Joaquim Elder Guerreiro, alias Dr.Shadow (Delta Team is still up), Alex "Darkfox" (thanks for letting me use your BBS), Jo„o Neves and Henrique Craveiro for sugestions, and all the demo groups out there. I also want to say hi to my idols (I know they don't read this, but...), Chris Roberts, Fran‡ois Lionet, Archer MacLean, everybody at ID Software and Apogee, Sierra On-Line, Lucas Arts and Team 17, for showing me what programming is all about. þ Bikes and programs - What to they have in common ? þ Introduction They cicle, that's what !!! If you don't know what I'm talking about, this article is just for you... Let's say that you wanted to make a program to write ten times 'Spellcaster' on the screen. One aproach would be like this: Program Test_10; Begin Writeln('Spellcaster'); Writeln('Spellcaster'); Writeln('Spellcaster'); Writeln('Spellcaster'); Writeln('Spellcaster'); Writeln('Spellcaster'); Writeln('Spellcaster'); Writeln('Spellcaster'); Writeln('Spellcaster'); Writeln('Spellcaster'); Readln; End. But, this aproach has several errors, computationaly speaking. It isn't logic and efficent. Imagine I wanted to write it 100 times?! So, someone really clever tought up a thing called a cicle. A cicle is something that repeats itself for a number of times, or until a condition is met. See the last example re-writen: Program Test_11; Var A:Word; Begin For A:=1 To 10 Do Writeln('Spellcaster'); Readln; End. See, how economical it is? It saves time and valuable memory. There are three kinds of cicles, each one of them with their advantages and disadvantages. þ The For cicle The For cicle is for me the simplest one to use, and it is very usefull, in all circumstances. The sintax is: For variable:=firstvalue To lastvalue Do instruction What this command does is this: 1. Assigns firstvalue to the variable 2. Executes instruction 3. Adds one to the variable 4. Returns to 2 if the value in the variable is different from the lastvalue. You can use the For keyword with the Begin and End keywords, like this: For variable:=firstvalue To lastvalue Do Begin instructions End; In this case, Pascal executes all instruction between the begin and end clauses, before incrementing the variable. Let's see an example: Imagine you wanted to show on the screen the numbers from 1 to 20: Program Test_12; Var A:Word; Begin For A:=1 To 20 Do Writeln(A); Readln; End. See how simple this is? Now, let's try something similar... Show the numbers from -20 to 5... Program Test_13; Var A:Word; Begin For A:=-20 To 5 Do Writeln(A); Readln; End. This also works... Note that the first value must be smaller than the last value, or else the cicle won't have effect. If you want to do reverse order, change the To keyword to DownTo, like this: Program Test_14; Var A:Word; Begin For A:=150 DownTo 130 Do Writeln(A); Readln; End. As you may already have guessed, this writes the numbers from 150 to 130. Remember that you can use a varible instead of a number, when you specify the cicle range: Program Test_15; Var A:Word; B:Byte; Begin B:=150; For A:=B DownTo 130 Do Writeln(A); Readln; End. Well, that's it for the For loop... þ The Repeat...Until... Sometimes, you must cicle something until a certain condition is met. There are two ways of doing this (there are actually more, but the others are just plain stupid). The first one is the Repeat...Until keywords. The sintax is as follows: Repeat instructions Until (condition is true) This executes the instructions UNTIL the condition is met... For example, imagine you wanted the computer to give you the powers of two, until the number is larger than 10000... Program Test_16; Var A:Word; Begin A:=1; Repeat A:=A*2; Writeln(A); Until A>10000; Readln; End. Note that you don't have to use the Begin and End keywords in the Repeat loop (this is why I like the Repeat very much and use almost everytime). Also note that if the initial number was 10001, the computer would print the number 20002 on the screen, a number that is larger than 10000. Why is that ?! Before you start throwing away your copy of Pascal out of anger, notice that the checking is done AFTER the first execution. If you want to test before, you must use the... þ While loop The only differences between the Repeat...Until... loop and the While loop resides just in two things: First: The checking is performed in the start of the loop, in case of the While loop. Second: To execute multiple instructions in the While loop, you must use the Begin and End clauses. Check the above program re-writen to use the While loop: Program Test_17; Var A:Word; Begin A:=1; While A<10000 Do Begin A:=A*2; Writeln(A); End; Readln; End. Notice the 'While A<10000 Do' line... You probably noted that it very different from the Repeat. The reason is that they have a different "meaning". The sintax for the While loop is: While (condition is true) Do instruction or While (condition is true) Do Begin instructions End; This executes the instruction(s) WHILE the condition is met... Compare it with the Repeat...Until... loop... There isn't any rules of usage for the For, While and Repeat loops... As in everything in coding (=programming), you must try to learn. Each one of them has it's advantages and disadvantages. þ Graphics, part III : Lines and Circles This issues's graphics tutorial is about lines and circles, a very important part of many programms. These are one of the tougher subjects to teach (for me, at least), and it may be confusing for starters; but don't despair... Try to work it out. þ Lines This is a tricky subject to talk about... There are so many ways to do a line, that I don't know were to start. I will start to talk about a general algorythm. Let's say you want to draw a line from (x1,y1) to (x2,y2), like this: (x1,y1) oooooooooo oooooooooo ooooooooooo (x2,y2) Now, you must find out the length of the line: deltaX := Abs (x2-x1); deltaY := Abs (y2-y1); If one of these variables (deltax or deltay) is 0, the line is horizontal or vertical. In that case, the program should branch to a specific part, because the division by zero error. Know, you must find out the slope of the line... You do that like this: Xslope := deltaX / deltaY; Yslope := deltaY / deltaX; Looks familiar ?... This is standart trigonometry (trig for short). But, if you remember trig, you will now now that there are two line equations: X := Xslope * Y; Y := Yslope * X; So, which one to use ?... If you use the wrong one, you'll end up with something like this: o o o instead of this: oooooo oooooo oooooo Well, you must find out which one to use. Picture this: o\xx|xx/o oo\x|x/oo ooo\|/ooo ----+---- ooo/|\ooo oo/x|x\oo o/xx|xx\o If the slope angle is in the 'o's area, then you must use the first equation. If it is in the 'x's area, use the second. In the middle position, use either. When you find out what line to use, you just have to loop the right hand variable, and calculate the left side one from it. Well, we almost everything we need, except for a function that returns the signal of a number. The function (Sgn) returns 1 if the number is positive, -1 if the number is negative or 0 if the number is zero. Function Sgn(A:Real):Integer; Begin If A<0 then Sgn:=-1; If A=0 then Sgn:=0; If A>0 then Sgn:=+1; End; Now, here's the line procedure... Procedure Line(X1,Y1,X2,Y2,Col:Integer); Var Deltax,S,Deltay,Dx1,Dy1,Dx2,Dy2,S1,S2:Real; I:Integer; Begin Deltax:=X2-X1; Deltay:=Y2-Y1; Dx1:=Sgn(Deltax); Dy1:=Sgn(Deltay); Dx2:=Sgn(Deltax); Dy2:= 0; S1:=Abs(Deltax); S2:=Abs(Deltay); If Not (S1>S2) Then Begin Dx2:=0; Dy2:=Sgn(Deltay); S1:=Abs(Deltay); S2:=Abs(Deltax); End; S:=Int(S1/2); For I:=0 To Round(S1) Do Begin PutPixel(X1,Y1,Col); S:=S+S2; If Not (S2*PI; End; I think this is a simple procedure. As for the theory, this is 9th grade math. If you haven't reach it yet, or you don't understand shit about maths, write to me and I'll teach you... If enough of people write to me, I may even include a complete explanation on trigonometry in a near issue. There is room for expansion in this routines. For example, try to use diferent radius values for the calculation of the Px and Py variables, or try adding a different value to Deg, for example, 0.01. Experiment. With this issue of 'The Mag', you probably noticed a Pascal source code. It is the source code of a couple of effects. One you might recognize from the Color Blind demo I gave away in last issue. It is probably slower than the one in Color Blind, because the line routine I use in the demo is almost 100% assembler. The other effect is very SLOWWWW... I will teach how to speed it up in next issue. It is due to sines and cosines calculation. If you like to experiment, try finding a book or a doc on the Bressenhams algorythms. These algorythms are a lot faster than the routines I'm giving you, and are based on the simmetry of the objects. I don't have any docs on it, but I can do an explanation of them in a future issue, if you ask me to. þ Hints and Tips Welcome to the new regular... It is a tricks part, where I teach some short tricks about programming. Every tip has a number of stars on the title. This is the level of it: * - This is for begginners ** - Medium *** - Advanced (there will not be a lot of these, because I'm not that good...) - Division by zero (*) If you executed the Maths program I gave you in issue 3, and you selected 0 as the second number in the division, you would get an error message (Division by Zero). This error is caused by (as you don't know) by diving something by zero, because in Pascal (as in real life), division by zero is impossible. So, you should always check if the division's second term is non-zero. - Super fast multiplication (**) If I said that there was a way to multiply by a two (or any power of two) that was more than twenty times faster, you'll probably put me in a sanitarium. Well, as the matter of fact, there is. If you translate a decimal number to binary and you shift the bits to the left, and you convert again to decimal, you'll have a multiplication by two, but a lot faster than standart multiplication. Schematically: 50 = 00110010 ; Then, you shift left... 01100100 = 100 ; If you shift again to the left... 11001000 = 200 ; So, by shifting two bits to the left, you multiplied 50 by 4 (2ý) As you may have figured out already, you can do the opposite (shift right) to divide by two (again ultra-fast). The ideia is this: B:=A*2 equals B:=A shl 1 B:=A*4 equals B:=A shl 2 B:=A*8 equals B:=A shl 3 B:=A*16 equals B:=A shl 4 And: B:=A div 2 equals B:=A shr 1 B:=A div 4 equals B:=A shr 2 B:=A div 8 equals B:=A shr 3 B:=A div 16 equals B:=A shr 4 So, you have an ultra-fast way of doing mults (short for multiplication) and divs (short for divisions) by powers of two. þ Points of View This week's point of view is... I don't know what. I don't really don't know what to talk about, so I'll talk about censorship. A week ago, I tried to put this mag in a BBS, that shall remain nameless. After I've downloaded it, I received a message from the Sysop, saying that he didn't wanted crap like this in his BBS. Just because I said shit... And because I made fun of Atari and Bill Gates in the Adventures of Spellcaster. If this isn't censorship, I don't know what is... People should be free, not manipulated by a bunch of burocrats and new-age folks that think that shit like this isn't intended for this pure world of ours. Our world is a fun place... It's the only place in the Universe were people say shit and other things like that, and make fun of others when talking, but when they write it, they say it is filthy and degradating for the human race. I can understand their concern, but people must realize some stuff: - When somebody does something for fun, that person MUST have some fun doing it, and depriving someone (namelly me) of writing things that he likes, is wrong. - A person that can handle a modem to download my mag, or that can operate computers has enough age to read something like it - Censorship is a crime (we have freedom of speech... At least here in Portugal). Fortunatly this is a free country, and I can say what I want to... So I say, quoting one of my favorite bands (Iron Maiden): I'm not a number... I'm a free man Next issue, I'm gonna write the fourth part of the Graphics tut, about pre-generated arrays and virtual screens. I'm thinking of also including another tutorial on assembler, but I'm not sure I will... þ The adventures of Spellcaster, the rebel programmer of the year 2018. Episode 4 - Brotherhood of the Rebel Programmers My scream still echoed in the old powerplant, a relic from the year 1998. Me and my new friend were lying on the ground, grasping to get air after our long run. The sirens of the Atari Penetenciary were silent now, and the only sound heard was the sound of a jetcopter flying overhead. I sat down and looked at the blond man. - Thanks... - I said, smilling at my saviour. - You're welcome... You've also saved my life, by enabling me to get out of that madhouse. A long silent period followed, as we studied each other, trying to figure out if we could trust one another. - My name's Diogo, also known by SpellCaster... - I said, streching out my hand towards the man. - I'm Gundsen, Karl Gundsen. In the Universenet I was known by the codename DeathRipper. The ambient of the room lightened up, and the conversation continued in a friendly manner: - So, what were in for ? - asked Gundsen - I was sentenced for 30 years, for not having Windows 2018. - You're a programmer ?! - Gundsen opened up his eyes, apparently surprised. - Yes... For my sins... - I said sadly, thinking of my old computer. - So, what's your story ? - I was going to be executed tomorrow... - WHAT ?! - I yelled, suddently frightned. - Relax... I didn't killed anyone. - So, what did you do ? - I asked, still scared. - I tried to penetrate the Gate... - The Gate ? - I asked, because I didn't knew what the Gate was. - Yes... The software defense sistem of Comptel. - You're a hacker. - I said, honnored to meet one of them. the hackers were a type of programmers that were tought extinct in the end of the 20th century. - Ye... Probably the last one. - he said proud, sitting up straight. - But why did you tried to break into Comptel's system. - I want to find out a way to know and destroy the NeuroFrame, Comptel's main system. If I succeed, Comptel's reign of terror will reach an abrupt end. I looked at him, trying to figure out the mixed emotions in Gundsen eyes. - You said 'I want'... After the first failure, you still want to get into Comptel's system ? - Definatly... I made one mistake in the final defense, and it cost me my freedom, and almost my life. I thought about it... It made sense. Comptel needed to be stopped, from preventing it to arrest and kill innocent people like me and DeathRipper. - Do you need help ?... - I asked, standing up and looking directly into him. He smilled and stood up. He streched out his hand and we shook hands. - Sure, SpellCaster... But are you sure you want to try... If we are caught, we can die... And it will not be easy. - Hey, all my life I wanted to be a hacker... And besides, it is better to die with honor, than to be a fugitive the rest of my life. - Ok... Thanks... - he said, smilling. - All we need now is a name for our movement... We thought for a minute or so. - How about "The Bill Gates Hatters Club"... - I said. - I think it sucks... - Me too... We thought a bit more. - And "Comptel's Destruction Group"... - he said. - Please... That almost made me puke. - Ok, ok... After two hours of thinking, we came to a conclusion: The Brotherhood of the Rebel Programmer was born !... See you in the next issue Diogo "SpellCaster" Andrade