While programing we find ourselves with things we would need to write over and over again. Even tho copy paste is very friendly to use, it tends to make the code larger, and since we are bound to make bugs every now and then. Its not easy fixing hundreds of times in the same error, so most of us use functions. Functions are an essential part of programing when it comes to extremely complex algorithms, but sometimes functions aren't enough and we still end up writing the same processes over and over again. For example we might have a common process, lets say we have a simple loop (of course a simple loop is simple but for the sake of explaining lets use a loop) so lets say we have something like this
void primary_process(int beginning, int times)
{
int x;
for (x=0;x<times;x++)
cout << beginning + x << " ";
}
yet as we keep on programing we notice that we use that loop a lot of times, the only thing that changes is the formula that we use on the number. So we could write yet another function with the same loop.
void primary_process_multiply(int beginning, int times)
{
int x;
for (x=0;x<times;x++)
cout << beginning * x << " ";
}
But that would be writing the same loop function once again. What we can do is use a function
int sum(int a, int b)
{
return a+b;
}
int multiply(int a, int b)
{
return a*b;
}
once again this functions are extremely simple but in a real programing environment functions could take hundreds of lines. Now that we have this two functions lets use function pointers.
a function pointer can be declared like this
class (*function_name)(classes)
so if we whant to rewrite our loop function but using a function pointer we do this
void primary_process(int beginning, int times,int (*function)(int,int))
{
int x;
for (x=0;x<times;x++)
cout << function(beginning,x) << " ";
}
Now you must notice how I added a function pointer in the parameters the function will take, and then I used the function pointer like if it were a normal function. How we use this?
well here is an example
primary_process(1,15,sum);
cout << endl;
primary_process(3,14,multiply);
by writing the name of the function in the third parameter, I pass the function I want to use to the primary_process function
now its important to note that the function pointer must contain the same amount of parameters as the function it will point at otherwise it will fail to work.
Now we have made our code smaller and without duplicates. But what if another problem arises. Lets say we want to play those same processes but to other types, in our example we want to use characters too and also output characters. Well we could copy all 3 functions and make functions that get characters in them OR. Use templates.
look at this code.
template <class T>
T sum(T a, T b)
{
return a+b;
}
template <class T>
T multiply(T a, T b)
{
return a*b;
}
template <class T>
void primary_process(T beginning, int times, T (*process)(T,T))
{
int x;
for (x=0;x<times;x++)
cout << process(begining,(T)x) << " ";
}
by using templates in functions we allow it to take any type of class, it could be an integer, a character, even a custom class. of course there could be exceptions but I won't discuss them today. Yet this way allows us to use different types in similar processes now for using this functions I ran this example code.
primary_process('a',5,sum);
cout << endl;
primary_process('b',10,multiply);
cout << endl;
primary_process(1,15,sum);
cout << endl;
primary_process(3,14,multiply);
this returned.
a b c d e
b ─ & ê Ω L « ► r
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 3 6 9 12 15 18 21 24 27 30 33 36 39
In a larger programing environment we might use classes that need to go trough the same processes, and this processes might take hundreds even thousands of lines, and sometimes having duplicates of those processes will cause enormous headaches when debugging.
By using this methods we can have cleaner codes, and be able to change them fast in the future instead of going trough a all the copies of itself.
back to the code.
Wednesday, April 9, 2008
Make your code Smaller with Function Pointers and Templates
A step further in encryption
In an early post, Simple Encryption Methods, I mentioned 4 kinds of bitwise level operations that could be used to encrypt data. Between them was, mask, inverse, reverse and rotate. If you want speed its good to remember that the more complex the more processing time it has to be done yet it normally also means that it is more secure. Mixing several kinds of operations can be a good idea. if only you know the order it would be like the formula itself is a giant password. But also, if you want security its a good thing not to use the same process with each word, that would just generate an alternate alphabet that can be easily decrypted by using word associations. So you need to vary your methods in a way a pattern its difficult to recognize. So we start by mixing the methods. We need a formula that tells what methods to use, so the lowest form would be, if the method will be used or not used. Also we can alter the order of the methods this can create a large amount of possibilities. But first we have to know, that when using this 4 kinds of methods the inverse one can be anywhere. And therefore should be used once.
Its easy to identify why rotate and reverse don't matter when you did the inverse (if before or after.
1100 rotated once 1001 and then inversed 0110
1100 inversed 0011 rotated once 0110
1100 reversed 0011 inversed 1100
1100 inversed 0011 reversed 1100
note that in combinations inverse and reverse will return the same, but this is not true for all values.
mask also have this property lets see at a simple test
1100 masked with 1010 is 0110 inversed is 1001
1100 inversed is 0011 masked with 1010 is 1001
this has been tested with all possible combinations and is true for all.
therefore we don't need to care about the position of the inverse only if it exist or not
inverse rotate reverse masked is the same as rotate inverse reverse masked and therefore is the same as rotate reverse inverse masked and therefore is the same as rotate reverse masked inverse.
Yet on further testing the position between rotate reverse and masked, does matter, So its a good thing to play with their order when building an encryption method. Also since the position of inverse doesn't mater don't go around making more than one inverse because two inverse is the same as no inverse.
Remember you can get creative with the formula of encryption a nice idea is to force an order of encryption, by using a formula that generates values for each letter that affect the way they are encrypted and by using the letter before as a value to alter the encryption. This way ensures that not only you need to know the method and the formula, but you need to encrypt in an order, and the encryption depends on the message itself making it hard to identify a pattern between two messages.
Tuesday, April 8, 2008
Lunatic Night Walk. The sea of darkness
So two weeks or so have passed since my last long walk, to keep my sanity I decided to take this next trip, this time, I knew where I was going. To my surprise things might be a bit misleading in the night, specially when you can't hear your destination or see it in the night. Once again I was talking in the cellphone with my good friend Gerson, who once again asked me to please don't go walking in the night. But since I don't want to reach the old age I didn't listen and went out of my house at 12:00 am
To my happiness the bar "El Colegial" was open at that hour so I used the only two dollars and 25 cents I had in my pocket to buy myself a nice Smirnof Green apple, before the long journey I was about to go. Not knowing how lost I was gonna get.
Because I am shush a good person and like to keep my island clean I decided to turn back and walk around the houses for a while so once I finished the drink I had a trashcan nearby to dispose it.
That done I decided to go out of the known area and take the unknown path. Near the end was when I began getting confused, what in Google map appeared as a street was actually a large metal bridge for people only. The metal planks made loud noises and bent down as I walked in them. But the river under it looked nice and calm. After that I made my first mistake, I tough I had to keep walking forward but I saw a guy standing in the middle of the empty street so I took a turn. In the way near the funerary I saw a strange light orb come out and hover into some bushes. Also found another strange guy standing in the middle of a street who I think asked me for money but I had none.
I found myself in the viaduct what just told me, I have made a huge mistake and I was walking away from my destination. So I took a turn and went into the next street. As I walked I notice a place full of people so I took a turn to a more solitary street were I saw a rather peculiar building that had the name Stealth in it and the drawing of a black angel in one side. finally I found a dead end and no sign of the sea. Even tho in the map it looks like I was so close to the sea I took a turn passed behind the bat and kept on walking some rather unused streets that belonged to some factories. later I found a bridge that was only for cars so I decided to walk back away from what I tougth was the good way. ending up in a rather strange neighborhood with old houses.
Creepy enough I got in the heart of those old and small looking houses where I saw a guy entering a house with a bicycle and his face was masked. Y took a turn away from him and ended up in a dead end, so I walked back and took another dead end so I just traced my steps back and went walking in front of another bar. back to were I came from but trough another street.
and when I finished walking I found myself in the street were I saw the first guy standing. So I began rethinking where I was and decided to take a turn toward the correct path. I ended up in a large bridge and I tough I was in the bridge I wanted to take in the first place so I walked back and ended back in the street with the black angel so trying to walk other streets just in case someone saw me didn't noticed I was lost I took a few unnecessary turns back to the bridge. The buildings there looked extremely old and the gates looked almost castle like. but I kept on walking.
Then I started looking in the direction the river went and I noticed that all that was over there was some rather heavy dark looking wilderness and the most nearer streets that went in the direction I tough was the sea was in another side of a highway, and there was no sidewalk there. But considering it was already 1 am in the morning in a rather solitary place I ran in the middle of the highway and not even one car passed. I was getting scared, I was passing near construction sights and no sign of the sea. No sound no wind just the distant bark of dogs. I was about to call Gerson who I told I was gonna call him so he could hear the sea, and tell him I had failed miserably and was now lost. But then I heard it, the sound of the sea, the beautiful sound of crashing of waves. I followed the street and saw some dogs barking at me, yet no sign of path to go there, I was thinking again to turn back. but then I notice an open green area that wasn't full of tall grass, and immediately called my friend. Happy with joy I finally saw the waves, the almost infinite plain of darkness, I was so overjoyed.
Under a nice tree I found a fallen tree that served perfectly as a seat and there I sat, for about an hour. Looking at the sea and meditating. While in there I saw some nocturnal birds running in the shoreline. Probably eating. Until I decided to leave.
I took the same path back, now looking at the map I notice I took a rather long path when I could have took a short cut, but the reason of this walks is not to get to places fast, is just to enjoy the beauty of the night. To go to places at hours no one goes, and experience the world without the pain of the sun. In the way I saw this guy coming from the University, for a reason I found him familiar, he looked almost like my friend, so after we passed each other I decided to look back, just to find out he was also looking back. But I kept on walking.
Passed in front of "El Colegial" wish was already closed, only the bartender was cleaning around.
and I got back home with a smile in my face and a large feeling of accomplishment, the waves still fresh in my mind, the beautiful sight of the moonless night, the blurry of the horizon like a lived dream.
4 sleeping pills
2 pain relievers
no casualties.
Sunday, April 6, 2008
Drawing with Light
SO yeah it has been a weekend or so I haven't written stuff. I feel I have failed you people. But no fear I come with lunacy. Not programing because I am a bit null after the exams. So a few days ago I was talking to my good friend Gerson over the internet and he asked a bunch of things about my digital camera, and I, sounding like a total newbie didn't knew anything about my camera, so I decided to look at the manual. After noticing I could change the time the shutter is open and the wideness of the shutter and the amount of sensors, I said. HEY I know how to test this, lets draw with light. And here is the result.
side note: its just a star get over it.
who can resist not making a star with this new power.
after a star who can resist not making an inverted one.
my symbol or an attempt to do my symbol
Fire chaos weee. Because a flash ligth is not the only source of light.
Look at the pretty fire spirits... and hands?
I'm not human....
basically by letting the shutter be open for a long time it detects a lot of what happens and swallows a lot of light. so by letting it be on for 8 seconds I can draw with light. The hard part is to stay still so you don't look so blurry.
Thursday, April 3, 2008
Simple Encryption methods
We all like to be sneaky every now and then, there is information we don't want anyone to read, or just someone who we trust. But as we all know, just talking around is insecure, using a public medium to transfer information will be viewable to anyone that has the knowledge to get it. So we employ encryption. For encryption we need to have something in mind all the time. The data we encrypt we need to decrypt later, so we need to use methods that won't end up in data being lost and we need to use methods we can remember later. Thanks to computers we can use more complex methods to encrypt our data, and let them remember how to decrypt it.
Most of the time all we have to do is reverse the process, so let me explain some computer level encryptions. And since everything is binary in computers most encryption can be done in the bitwise level.
I believe one of the most common processes used on basic encryptions is masking. Masking consists of using the bitwise operator XOR or ^ in order to "mask" a sequence of bits. XOR is the same as the logic exclusive or in logic, its truth table is
A|B|A^B
1|1|0
1|0|1
0|1|1
0|0|0
this sounds weird to be used as encryption but lets see what happens with larger numbers. Lets say we have this 10110101 and we want to encrypt it with the "mask" 10101010 (notice that both the code and the mask have the same amount of bits"
10110101
10101010 ^
00011111
the result is 00011111 now thats nothing like the mask or the original thing. The good thing about masking is that if we take the new object and mask it with the same mask...
00011111
10101010^
10110101 we get the original number. This way you can mask with different numbers each letters and only if someone knows the mask then they can decrypt it.
Another good operation is inverse, its not a very strong encryption since it only does one thing, but in combination with other encryptions it can become slightly powerful. Lets say we use what we got in the last encryption 00011111 and we decide to inverse it
~00011111 = 11100000
now if someone whanted to decrypt it, they had to inverse it first and mask it later.
since when you ~11100000 you get 11100000.
now getting deeper in the code lets use another kind of encryption. Rotation.
#define ROR(x, n) ((x>>n)|(x<<8*sizeof(x)-n))
#define ROL(x, n) ((x<<n)|(x>>8*sizeof(x)-n))
this two functions might be useful in encryption and are used in several encryptions. they rotate the bits to one side or the other.
lets go in depth with it lets say we have 11100000 and we rotate it twice to the left (the ROL function)
so first er have 11100000 << 2 and we get 10000000 then the other side of the function gets that the sizeof this is 1 byte (8 bits) so
11100000 >> 6 so we get 00000011
then we or this two values and we get 10000011
if we use ROR and rotate it to the right twice we get 11100000
so now we have 10000011 a number very different from 10110101 but if we trace back all our steps we will get that number. only you know what steps you took and what masks you used.
I guess I have time to explain one last function. Reverse. The reverse function is used to reverse the numbers for example 10000011 would turn 11000001 this function isn't very strong since it can only return one value of each letter but just like inverse it can be mixed. A good way to use this is to use a character table for speed.
static char REVtable[256] ={ 0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0, 0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0, 0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8, 0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8, 0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4, 0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4, 0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec, 0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc, 0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2, 0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2, 0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea, 0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa, 0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6, 0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6, 0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee, 0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe, 0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1, 0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1, 0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9, 0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9, 0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5, 0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5, 0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed, 0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd, 0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3, 0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3, 0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb, 0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb, 0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7, 0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7, 0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef, 0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff };
using this table that is written in hex numbers you can quickly return the reverce of any character.
NOW well enough of binary lets look at some code. and mask one letter
unsigned char I = 'A';
printf("Original: %c\n",I);
I = REV(I);
printf("Reversed: %c\n",I);
I = ~I;
printf("Inverted: %c\n",I);
I = I ^ 'B';
printf("Masked: %c\n",I);
I = ROR(I,2);
printf("Rotated: %c\n Start Decoding:\n",I);
I = ROL(I,2);
printf("Rotated back: %c\n",I);
I = I ^ 'B';
printf("Masked: %c\n",I);
I = ~I;
printf("Inversed: %c\n",I);
I = REV(I);
printf("Reversed: %c\n",I);
this returns the following.
Original: A
Reversed: é
Inverted: }
Masked: ?
Rotated: ╧
Start Decoding:
Rotated back: ?
Masked: }
Inversed: é
Reversed: A
you could run this kind of processes in each letter of a sentence, make sure to remember the masks and the amount of rotation used in each letter. With a good ammount of combinations your encryption will be really hard to decode.
Wednesday, April 2, 2008
A quick formula for extracting numbers.
We know how to extract booleans out of larger integers, but what if our base is larger than one. What if we want to store numbers like example a 3. We know that if we whant to store a number of any base we can just go and take for example, if we have 3,2,1 in base 4(numbers from 0 to 3) and we want to store them in a single decimal integer we can do (3)(4^0)+(2)(4^1)+(1)(4^2),(in other place number*(base^position)) but what if after we get a number 27 we want to extract them individually. well the formula is rather easy it is floor(number/pow(base,position))%base so if we take 27 and we want the numper in position 1 (that we know its 2) we do this:
floor(27/pow(4,1)%4
floor(27/4)%4
floor(6.75)%4
6%4
2.
This way we can store things that hold more than just yes or no questions. Using the other formulas to store items we can create highly portable data by compressing all possibilities in numbers, Converting to larger bases for example hexadecimal or one that uses all the letters of the alphabet makes it more portable.
Tuesday, April 1, 2008
Lets Play Rock Paper Scissors
Rock paper scissors one of the most balanced games that exists, every option has equal possibilities to win, loose or draw. The rps system is implemented in many games and hidden with many fancy names like elements, weak points and strong point and others. The good thing about this is that it can be used in strategies once you know your enemies, while giving you the option to grow in one without being the weakest nor the strongest. But now I would like to propose a more complex system. Lets include the rps in a circle, the angle between each point is 120. So lets take any position in a circle and say its stronger point its that position + 120 and its weakest point its that position + 128. Now lets make a bonus system out of that and the closer you are to the stronger point you get a 100% bonus while the closer you are to the weakest point you get -100%. This leave us with two arks. one arc has a length of 120 and goes from 120 to 240 from 100% in 120 to 0 in 180 to -100% in 240. And we also have an arc of 240, this arc goes from -180 in 240, to 0 in 0 as it starts the circle again and finally 100% in 120. Now to put it in code let me explain my float modulus formula.
float fmodulus(float a,int b)
{
return ((a/b)-floor(a/b))*b;
}
this formula behaves like modulus. In other words it returns the residual of a division but with the difference that the first number can be a float and not an integer.
now for the formula to calculate the bonus we have this. It can be made better but lets use this for explanation.
float huebonus(float a,float p)
{
a = fmodulus(a,360); //first we clean the atacking point so its between 0 and 360;
float b = fmodulus(a+120,360); //then we get the strong point and we also put it between 0 and 360
float c = b + 120; // then we need the weak point
float d = fmodulus(c,360); //then we get the weak point and make sure its in the circle
float e = d + 240; //then we get the strong point again this time from the weakpoint
p = fmodulus(p,360); //we put the defending point in the circle
if((b<=p)&&(p<c)) //first we check if its between a+120 and a+240
return (60 - p + b)/60;
else if ((c>360)&&(0<=p)&&(p<d)) then if a+240 is larger than 0 then we check from 0 to the correct point
return (b - p - 300)/60;
else if((d<=p)&&(p<e))//then we check from a+240 and from that point +240 (the weak point)
return (p - 120 - d)/120;
else //once again if its larger than 360 we check from 0 to that point
return (240 + p - d)/120;
}
great now how can we make this pretty. just use color hues!.
thats for today back to the code.
Sunday, March 30, 2008
easy 2d movement.
Now for something rather simple a few days ago I explained a bit of moving in an array. But now lets say we want to move in a 2d plane and have any angle of direction. Well lets say the top left corner is position 0,0 and that looking directly right is rotation 0. So now that we have all our references lets make a formula that will let us move a unit in any direction. According to our references 0 is looking right, therefore if we are looking directly right we are going to move only in the x field therefore we need to use cosine in order to move since cosine of 0 is 1, also sine of 0 is 0. Now if we consider that the rotation is made clockwise, and that 90 is looking downward, then using cosine for x and cosine for y works well. Therefore we need to store a position variable and a rotation variable and each time we move forward the new position will be position + (cosine(angle),sine(angle))*unit and moving backwards would be position - (cosine(angle),sine(angle))*unit. Very easy. Now for collisions there are a bunch of different ways. the easiest is the collision between two dots. Basically a collision happens only if the two dots are in the same position. We could also have a collision between a dot and a circle. First you get the distance between the dot and the circle, then get the radius of the circle. If the distance is smaller than the radius then there is a collision. We can have collision between two circles, this time we also need the distance between the center of the two circles, then if the distance is smaller than the sum of both radius we have a collision.
There are many different collision tests each of them get more complex depending on geometry of the objects, but thats all for today.
back to the code
argc and argv
So today's brief blog is about the argc and argv in that appear besides the main function when dealing with c++ console applications. Basically argc is the number of arguments that the program gets when its open, and *argv[] is an array that contains the arguments. The first argument tends to be the direction in your hard drive of the program, and the next arguments may vary. For example you could open your program using the command prompt and write something after the dir of the program, and that will become the argument. Separate it by space and you will have more arguments. Now, what things can we do with this. I mean, we might like to use the command prompt, but the users usually don't so whats use for them?
One nice thing about it, its opening files. Try making a file and end it with anything you want after a dot like .test and create a program that uses the second value of argv to open and read from the file. Then open your file with the program. Guess what, the second value will be the direction to the file. So you will have your own custom file.
back to the code.
Friday, March 28, 2008
NODES! and dynamic stuff
yay for nodes I say, they make adding dynamic content easy when making a class of nodes, each class will contain something, it can be numbers and stuff variables and functions that will affect and work and stuff. Virtually anything you want to store there but not only that, each node class will be pointing to the next node class, so they will contain a pointer. This is very useful for having dynamic data.
Picture this, you have a ball, the ball bounces around. You have two balls doing this, no prob. You want to add a bunch of balls that do different things by themselves, we might have a problem. Specially since computer works rather linearly, each line of code is executed one at a time so how can you even have several objects doing things at the same time. Well you can have each ball pointing to the next one and then all you need is to go from one ball to another calculating the behavior and when you reach the end of the list of nodes you begin again. and suddenly, we have the impression that we have object oriented programing! since each object will require some time of the machine to calculate its behavior, since the machine tends to be fast (unless you have enough calculations to slow down your pc in each object)
now here is an example of stuff in c++
class ball
{
//some variables
public:
ball();
~ball();
void behavior();
ball *next;
};
so then we can create a pointer that will always point to the first element and a pointer that will move in a way that it will stop when
pointer->next == NULL
=D FUN
but I say this is not the only approach to have some dynamic objects you can add dynamically and access them one by one.
we could have an array of pointers. actually a dynamic array of pointers.
here we have someting like that
class pointaray
{
int size, **data;
public:
pointaray();
pointaray(int);
~pointaray();
int value(int);
void set(int,int);
void add();
void del(int);
};
yeah doesn't explain much that but with several functions I can create some objects, destroy some objects, and dynamically modify the array and add elements by going
data[x] = new int;//and or any other class I want to handle there.
this method loosely explained here, well has some advantages and some bad stuff. the good thing is you can quickly access a point in the array if you know the number, the bad stuff is you have to have more functions.
While the other method just go to the next, but you have to crawl one by one. Well some ideas and tools you might need to use in the future.
Thursday, March 27, 2008
Thinking in Triangles and vertex
Well lately I been looking into the wonderful world of openGL. Making 3d programs seem easy until you start digging in the books and making the code. So as part of a quick warning, coming from a noob in the library, you have to start thinking in triangles and vertex. You could also have quads but basically the graphic card (the wonderful thingy in your pc that does most of the magic of the grafics) works with triangles. A relief is that you don't have to make all the libraries for drawing objects in a 3d space, so some rotations and perspective are already programed, yay. Also each vertex can be colored and each triangle textured and the library along with the graphic card does the work of making it look 3d. YET this doesn't mean your just going tell the program to load an object just like that and have a world done without making first an engine. The basic is, all triangles have to be declared, a triangle consists of 3 vertex and each vertex consists of 3 opengl floats, so its a lot of information per triangle, but thats where the magic of opengl pipeline works with, it connects directly to the grafic card so that large amount of information can pass quickly to the grafic card prossesed and from there to your monitor. A prosses that is done quite fast considering most of todays games have thousands to sometimes millions of vertex in each frame that are constantly changing.
here is a bit of simple code
glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glEnd();
Of course the complete code for displaying an opengl window is more complex. and varies depending on OS, but I will dedicate this blog just to give a heads up idea of what your gonna get into the future, so your not surprised later. So to start making your work easy you will need arrays of vertex and loops to display thousands of triangles efficiently and without loosing your head hard coding shapes.
that for now and back to the code.
Wednesday, March 26, 2008
Yay for rss
I have now added rss (Really Simple Syndication) to my blog. Now people can subscribe to my rss and they receive news feeds of new interesting (hardly interesting) blogs about things. That way you can keep up to date to my ramblings about computers and mental problems. I also added a bunch of buttons that are better to use by moving your mouse over the Subscribe section and using the scroll down because there a bunch haha.
By the way to add rss to your blog is rather simple using feedburner, a site where you set a feed site just follow the simple instructions, and adding the feed link to the blog on the customize section, settings, side feed section. also this site http://www.toprankblog.com/tools/rss-buttons/ is good for making rss buttons quickly. >.> maybe I overdid it.
yay.
Now lets see how this Post appears on the rss.
back to the code.
Tuesday, March 25, 2008
Basic formulas for array movement and data storage
So we want to play with numbers we want useful formulas now and then so lets start this.
we have one dimension
0
is a zero one number only one in size, lets call this dimension x
0,1,2,3
x is now of length 4 so x < 4 =" 9." 4 =" 2" x =" number" 1="4" 1 =" 6" 4=" 1" 4 =" 9" x =" Floor(number/(4*3))%4" y =" Floor(Floor(number/(4*3))/4)" z =" Floor(number/(4*3))" position =" position" position =" position" p =" 0" 0 =" 1" 1 =" 1">>0 = 1
1 is the bit in position 0
p = 1
1<<1 2 =" 0">>1 = 0
0 is the bit in position 1
P = 2
1 <<2 4 =" 4">>2 = 1
1 is the bit in position 2
just so you know the bitwise operators are close together with the tables of truth but they operate at a bit level, so think of every number as a bunch of truths and false
Ideas...
Well here is an idea for you to steal so I can whine later. I have already started developing it so its not just a dream. Now see, there are many kinds of rpgs out there, but we can divide the map systems in two categories. We have the most common, the preset maps, those wonderful maps fully made by the hand of artists are great, people can travel in them, do stuff in them, and get familiar with them, so they can always come back and say, I know this place. But these kind of maps are limited, require time to make and in the end, they are explored several times and they get old. Then comes the random maps, here we can have unlimited fun, several variables can cause a virtually endless (A.K.A HUGE calculable number that we should not worry calculating). What happened with these kind of map is that you never know what you will get, you can never return to a place you like, and in the end, it starts becoming just random, the random feel that just tells you this is not virtually endless is just the same but moved around.
So my proposal is, lets break the barrier and dig deep into development, (lets forget how much ram it will eat each time you enter a new map), lets work with pseudo random (basically the current random is also pseudo random but I'm talking about a more controlled random). In this process we will ask the player were to go, the player can say anything that comes out of their head, and then our system starts to work.
Basically we need key words, your common regular chat AI has them, to affect the variables. Lets not say you want to go to a castle and end up in a cave. Below the keywords comes other types of processes (some of them already programed by the way) in which we will turn the entire string into numbers. We need to ensure that no matter how long the string or short, the same amount of numbers are extracted from the string. And once we have enough numbers that are pseudo random but come from that string, we can go and work with the carefully developing of the map, and several other objects, colors virtually everything we can add a variable to and fuck up the ram. no not really the process is simple we are creating the objects on the go after that it works as your regular game just with custom made objects.
Now picture this, by working on the lowest levels of programing and slowly building up is so we can do this, having total control of the output and what we will generate is a good idea, since that we can create custom walls, custom floors, with custom colors. But always following intelligence, not going and adding numbers everywhere. Processes of symmetry, of keeping patterns. Basically we build the patterns, and rules that will apply to create a unity, then we add styled effects and generate more artistic looking environments. Preset patterns are good guidelines to keep things in place, we don't want the extreme to look too extreme. So basically become an artist, and study yourself, then put those rules on code and let them freely interact and develop, the more deep you go the more different environments can exist(no more random generators that use the same walls for everything but a generator that generates the walls). If we want we can create textures and pretty much everything an artist does and fine tune it until allmost all maps are unique and beautiful.
now stick some sockets, some server and some other algorithms and we have something.
well that is for today I'm back to the code.
Lunatic Night walk. The nocturnal Pilgrim
Lunatic night walk.
So on march 25, 2008 I lost my mind I tough it was time to do something different so I decided lets open those gates and go outside. It was 12:45 am and I was talking in the internet with my best friend, as he kept telling me not to do it that it was too late in the night to go for a go for a walk, but I went out anyway it was nice outside.
After reaching the college's lateral gate I decided I was taking a regular path, and it was boring so I decided to just change the path a little, so I walked in the empty area by the college. in there I smelt the sent of cow manure and heard the beautiful sound of water. Also I saw a hobo sleeping. Also I went and took the bridge on the north since the gates to the highway on the back of the college were closed. The bridge looked awesome, there was moss growing all over the metal fence around it and a tree had its branches growing inside the bridge wish gave a feeling of some kind of jurasik park movie.
So I went to pay a visit to my old art building. Before getting there I got the feeling I was just in a nightmare, as the street entered the mountain light went to a sudden stop revealing a rather pitch black area. I felt I was just dreaming, that I was just in another nightmare where the expected happens. But while entering the darkness I could see a bit, so I felt secure, specially since it was still a known place. it was later when I pushed on past the art building that the tension got worst. Story short I was fearing the sidewalk would end, in the way someone yelled at me junky and then some Doberman or pit bulls, it was to dark to see but rather large and black started barking and pushing the fence. I finally found the end of the sidewalk and had to move back. As I walked back I herd howling from the dogs and winning also some of the howls sounded rather strange and terrifying. accompanied by other noises I doubt came from dogs.
so after retracing back I went back to college, were I saw two nocturnal birds that are endangered specie, one before entering the bridge again and another in that area with the yellow arrows that means I walked in and then walked back. in the rest of this way, My good friend Gerson called me, so I told him I was just insane.
So out of college and back to the main highway, I went to walk in front of "La cerveseria India" and to my amazement there were people working in it, and thru one door I saw how many of the distinctive golden cans passed by. But I kept walking until I reached the city. And then I just walked a long road. And walked.
on and on.
and on
I was starting to get creeped out houses were getting older and uglier, places started to look more and more abandoned, the only thing that kept me going was, the smell of laundry detergent and candles... Candles? I smelt some strong sent of sencted candles. What made me feel rather odd, but all I could hear was some music comming from a cars mechanic place. I was starting to worie, because there was no other streets I could take until I saw a black cat. I said, time to move back, but then I saw a white cat and ended up walking around some huge abandoned building in where I heard some low whinnying or maybe a tv. but I don't think a tv was possible in that weird place where stairs seem to come and go from darkness to nowhere. I turned back the place looked rather spooky, I ended up behind a gas station so I went back the same street I came in
then I decided to turn and see the plaza, wish was rather cute. the church was close at that hour. so I couldn't see it
as I left the plaza I went tru a street that had a nice smell of bakery but nothing was open, so I kept on walking until in one street I saw a hobo walking toward me so I walked faster and turned the next street. then reached the bus terminal where I saw someone coming out of one of the offices.
finally I walked back and in here I had the odd experience that while I was walking a car flashed my eyes from the distance and I scratched my eyes for a second finding out the street was completely empty the car had dissapired from the entire highway. Then I went to wallgreens the only place open in the entire city where there was two people buying things, the cashier had to move to another cash register since the one he was in didn't scanned wet things. I was buying some passion fruit juice.
AND I got home where I drank 4 sleeping pills and 2 pain reliving pills and slept till next class.
casualties: My converse shoes have holes in their soles.
4 am

























