ladbon

There was one and there was no one

Category: 5SD048

This weeks production on the helium engine

Working with the states gave me a better understanding on something I’ve worked with for some time. I’ve been looking at each feature we’re putting in such as 3rd person camera, a cube, a goal, a button(GUI), a HUD and been thinking how each class should be structured.

Its pretty simple if you think about it. You have a function that constructs the feature, another that update it if it needs to and one that draws it.

The problem we are looking at on every corner is how to construct the damn thing. Most issues have been very generic like sending 16 bits instead of 32 bits integers around the engine.

I was working this week mainly on implementing the HUD and co-operating with my partner implementing his heightmap. We actually had the same problems but I couldn’t understand how we didn’t see it before. We need to watch ourselves for more legacy code that’s obsolete.

Next week will be a hard one, I have to implement a working button on another state, put in working numbers(font) on the HUD, establish a goal class that knows when our racing car is on that specific location so it can quit the game.

All of this on one week while this week was hard just to implement a non-working HUD. We’ll see how it goes on the next turn.

Implementation in Helium!

I’ll give you guys a rerun on whats happened so far.
The class was given a 3D engine from the teacher to create a 3D game.

Most of the engine is already written such as the camera, scene management and all of their functions capable of creating a passing grade result.

The lectures we’ve been having before this was working on a shell of the engine just implementing the camera, scene, background, triangles, squares, 3D objects, height maps and HUD.

Thing is, with this new version nothing looks the same and now we have to connect it all and make sure it looks professional.

And if somebody haven’t been reading I am the average Joe. It feels like a chimpanzee understanding how to work a Swiss knife.

So my first challenge was to concentrate on getting a state up and running from where I can place the objects in. From here I can declare,  initialize, draw and update my objects.

My first problem was to understand that all the states inherit from the abstract_game class which basically is the engine. The engine class is just like any class, it initialize everything and connect most of the engine classes to the game specific classes.

So problem one solved.

After that I had to make sure the state manager has my state, attaches it and then use it as the first one.

We now have everything up and running!

Now we need to initialize all the objects and the engine systems in order to construct, draw and update the stuff we create !

I’ll leave you guys here I am afraid. Most of the time have been spent looking at the mountain of information, observing and planning my approach. I am not going to even talk about the amount of help I got(shout out for my class mates <3).

Good night all and I’ll see you guys after Christmas. Cheers.

 

Web Servers!

This blog post and probably the next one will be about winsock servers.

Winsock like many other ways to code servers is a hassle and are not worth the trouble unless you are seriously considering creating one.

This post will only outline my idea of how the server is going to be designed in a simple way to interact with a user trying to connect to the server.

I will show some code but as I am only at the accept phase I wont be going into detail from there on.

So, creating a web server!

Networking is done very close to how we communicate outside the digital era.
It needs to be initialized first with

#include

int main(int argc, char* argv[])
{
WSAData init;
WSAStartup(MAKEWORD(2, 2), &init);

you then need to create a mailbox with an address

struct sockaddr_in server_addr;

here you create a mailbox with the name of the address as “server_addr(ess).

server_addr.sin_family = AF_INET;
server_addr.sin_addr.S_un.S_addr = INADDR_ANY;
server_addr.sin_port = htons(8080);
memset(server_addr.sin_zero, 0, sizeof(server_addr.sin_zero));

here you give the title of the mailbox as an internet based one, you give the addresscode to be your IP address you pick your zipcode(port) and you

So now you have a mailbox of the internet type with your address(localIP) and your zip code(port).

Now its time to create a route for your mailman. We can this a socket, this socket will be listening on the zip code you have to see whether or not you got mail(hihi).

SOCKET listener = INVALID_SOCKET;
p_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

here we declare the socket and define it as a TCP one. This means we use the TCP protocol as the route for our mailman.

After this we give our mailman a badge, a title, a bag and a paycheck to keep working until we close down shop.

In networking terms we call this bind.

bind(p_sock, (const struct sockaddr*)&addr, sizeof(struct sockaddr_in));

here we bind the socket our address. Its like hiring a mailman for our mailbox.

listen(p_sock, SOMAXCONN);

these are his instructions, he is to see if there are any requests to send mail to us and signals us whenever that happens.

From here on out there wont be any code so I’ll just follow the metaphor for people to understand the inner workings.

So we got our mailbox with an address and a zip code, we have a mailman that goes our zip code route to see if we get any requests to send mail as well.

Now its time to wait…

we create a while loop from here on out to look for visitors. In this loop we check if our mailman ever comes back with some mail and if he does we look at the envelope to see its return address and accept it before we open the mail.

This mean we have to create a socket and an sockaddr_in and when we do get a request we use the accept method and bind the socket and sockaddr_in to the user.

while (true)
{
SOCKET ns = INVALID_SOCKET;
struct sockaddr_in remote;
int size = sizeof(struct sockaddr_in);
if (WSAWaitForMultipleEvents(1, &hacc, TRUE, 10, FALSE) == WSA_WAIT_EVENT_0)
{
std::cout << “accepterar” << std::endl;
ns = accept(listener, (sockaddr*)&remote, &size);
}
}

This is what it could look like.
When you’ve done this part you have accepted the mail and you know the return address.

You know accept connections to your server.

When this is done you now allow requests form that user so you receive anything they send in a manner you find welcoming, send the requested information and wait for them to send an acknowledgment.

This is what TCP call a three way handshake.

Now to have a good server you might want to save each user after you accept them in a list, keep their connection alive(see if they have data) and delete users whenever you haven’t gotten anything from then in a while.

When you finally close down shop you should end with this:

WSACleanup();

but you need to close down any sockets you’ve open like so:

closesocket();

Linked List

Warning: This post will contain video game blood.

OK so this time I’ll be talking about how to make an linked list from scratch.

I’ll be using VS 2014 with c++ syntax(might be some C involved here).

Linked list in programming terms are(but can be something else!) a series of nodes which carry a data and a pointer.
These nodes make up a list where each node looks in front of them so you can traverse in a line to find what you need to add, delete or look for.

Linked list for me can be explained like this:

“There is a line of people. Each person knows their own age and can only see who the person in front of them are. They are allowed to ask the person in front of them their age and they always answer with the truth.”

-To remove a specific person in the line:

To remove, lets say #14 in this line of people. You simply ask the first one in line to tell where the second one is and repeat this until you reach the 13th in line. When that person points to the 14th you kick that person to the face and remove them from the line.

1

You then tell #13 in the line to start looking at #15.

-To remove the first person in the line

You simply ask the cashier (which always looks at the first person in the line and can ask about the people their age) who is in front. You then proceed to:

1

You then tell the cashier that the second person in line is the first one now.

-To remove the last person is just as easy.

You tell the cashier to ask the first person to see who’s next in line until you get the question “nobody” then:

1

You then tell the second last person that nobody is after them.

-To add people to the front of the line.

You simply put someone in front of the first person in line like so:

waiting-in-line

So now you got tennis dude in line. You tell the cashier that the first person in line is the tennis dude and the tennis lady that the angry lady in behind you, Voilá.

-To add someone at the back!

You tell the cashier to ask the first person to see who’s next in line until you get the question “nobody” then:

waiting-in-line

Place grumpy guy to the back of the line, tell overweight Mario that grumpy guy is the last guy in line now.

-Now to find a specific person you simply tell the cashier to ask the first person in line his age, if its not the age you were looking for that person will ask the second person until you’ve found you guy. You then do whatever you want with him like so:

754-1

Now that’s about it really. You can do so much more with a linked list and its possibilities are actually good. It does have some bad sides I’ll give it that but in the end if you are in need for a straight up list of values this is the way to go.
I would recommend using binary search trees instead =).

So to summarize:

Linked list is a series of nodes which contain a value and a pointer to the next value. You have a head node which looks at the first node in the line. This head node will traverse the list with the help of all the nodes because they all are looking to the guy after them. You can delete the node in front, the last in line and any inside the list.

*The one in front by deleting the one the head is currently looking at, then tell the head to look at the second node.

*The one in the back by traversing through the list until the node that says “the guy after me is a NULL/nullptr” then delete that node and tell the node before that there are no nodes after him.

*to take away a node in the middle you have a temporary node that looks at the node before the one you want to delete. The head node find the one you want to delete. You proceed to tell the temporary node that the node AFTER the one you WANT to delete is the one after him now and then proceed to delete the node you wanted to delete.

These are displayed as the examples I just wanted to show these in a simple matter for people to understand how the three ways work. This can also be the same way you add a node with simple alterations(the first examples already explains it).

A few good links to further understand linked list:

http://www.cplusplus.com/articles/LACRko23/
http://www.cprogramming.com/tutorial/lesson15.html
http://www.bogotobogo.com/cplusplus/quiz_linkedlist.php
http://www.bogotobogo.com/cplusplus/linkedlist.php#linkedlistexample10
http://www.codeproject.com/Articles/24684/How-to-create-Linked-list-using-C-C
http://en.wikipedia.org/wiki/Linked_list

Remember this is how I interpreted linked list as. This might be in no way a definite way to look at it. There is no code here simply an explanation on how to think linked list can work.

Stay sharp.

Binary Search Tree

So its been a while !
I am currently studying for a course that will introduce me to 3D engines, networking and multi threading.

What I’ve been taught this week has blown me away so far so I’ll go through one subject which interested me.

Binary search trees.

So currently I haven’t really started on doing the assignment but I would love to tell you everything I know about BST’s.

Binary search trees or BST is a system for listing certain variables be it string, integers or floats in a way you can search through them in an effective manner.

Each object in this “tree” is called a node. A node carries with it a value(lets say an integer value) with a pointer that points to another node that either has a smaller value or a higher, these are called child or “leafs”; nodes without any children.

So nodes with children are called branches and nodes without are called leafs. Every node has a value an a pointer that points to a child with either a higher value or a smaller. These are then divided as well just the same, branches and children until you finally get to the leafs.

A setup can look like this.

Node(1)
/
Node(2)
/ \
/ Node(3)
RootNode(4)
\ Node(5)
\ /
Node(6)
\
Node(7)

The root node is the first node to be created, its from there all the others are designed.

Now!
The assignment need to have a class created in a way that you can easily search between the tree, remove unwanted nodes and print them out in different fashions.

We were taught today on how to do these so I’ll try my best to actually explain these, please do not use this as a reference as its only my interpretation of the lecture not religious text.

To search the tree for lets say value[5] it will first go to the root node and ask

“does this node have value 5?”
-no

is it bigger or smaller than 5?
-smaller

does the node point to a higher valued node? (just to send an error “cannot find value 5”)
-yes

The program then goes to node with the value of 6 and asks the same thing until it finally find the node and return the nodes value.

Unto deleting!

same thing then, you find the value by asking the questions and navigating to the node and delete the value.

But what if your destroying a value that has some children that needs to be re binded so they are connected.
You need to first look at its children and find the lowest value and point it just like any branch node to the rest of them, taking the place of the node you removed.

NOW UNTO PRINTING IN DIFFERENT FASHION!

So there are a few ways of printing your values in the BST.

Node(1)
/
Node(2)
/ \
/ Node(3)
RootNode(4)
\ Node(5)
\ /
Node(6)
\
Node(7)

One way would be to look for the lowest leaf then work yourself up from there.
So when you find [1] you go up to its parent [2] then see if it points to a higher value and take that and so on until you’ve seen those then start moving upwards to the root node again. The program starts to do the same with the higher valued node(higher than the node)

Now this is basically what I’ve learned this week in programming. Do not ask me why we always start with the lowest, its just a way to do it. There are tons of different ways I am sure.
Another thing is why I only pointed out one way to print them as well, well its because I cannot explain the others atm.

One thing I would like to add is templates but I’ll probably do that next week when I’ve actually coded this assignment.

Stay true and much love,
Ladbon