Category Archives: Blog

File Server and Client Using Sockets in C++

File transfer

Sending the client to itself, effectively overwriting it while its running.

Find downloads at the bottom of this post!
I wanted to make an application in C++ using sockets and threads to test myself and get familiar with C++. It took me a couple of days and this is what I came up with.
It is an application to easily transmit a file in a LAN.
It works by running “server ” in a CLI, this will start the server which will advertise its presence and transmit its file to anyone who connects to it. By “advertise” I mean it will open a UDP socket to listen for incoming broadcasts from clients.
On another computer you run the client from a CLI which will query for file servers in the LAN by broadcasting a UDP packet to the file server UDP port, present its findings and you can then choose which file to download.

Background

I first started working with sockets in Visual Basic 6 when I was 12 years old. I started out creating a chat application, which at that point took me weeks and never worked properly.
In the past couple of years I’ve been diving back in to it, by creating a lot of unfinished multiplayer games with .NET(C# and VB) and GameMaker. I love LAN games and I really wish there were more of them, but it seems I am not patient enough to create them, at least not alone.
I’m currently taking a course in networking in which we’ve implemented a chat application in Python. It is extremely easy to set up sockets in Python and can even be accomplished by running the Python environment in a terminal, which makes it really cool for debugging.
I’m really eager to try out network programming in Unity3D as it seems completely different from what I am used to; it uses RPCs as far as I can tell.

Code

I use the Boost library to achieve a cross platform implementation of sockets and threads. This did not work out. I spent hours trying to compile the project with MinGW, I eventually gave up and tried with Visual Studio which I got working rather quickly. But then the applications started crashing and acting weird because apparently sockets are only thread safe in Boost if you’re using Linux and some other bugs I don’t have the patience to uncover cropped up, like the client randomly appending characters to filenames and ports when splitting them. I successfully sent and received files from Windows<->Linux and Windows<->Windows, albeit with crashes, but that’s the best I can do without rewriting the entire thing. And it’d probably be easier to just write a Windows specific solution rather than using Boost. But for some reason it still felt really awesome sending a file from a Linux laptop to a Windows desktop.
I haven’t put much weight on the structure of the code and spent most of the time digging around in Boost documentation. Right now it works, but it is really cluttered and a lot of stuff could probably be removed, since it is only there because I thought it would work at some point in time, but it didn’t and I am too lazy to refactor the code, since this was just a test.
I only wrote one file per executable, no header, no classes, no nothing(so a lot of something, I guess). I commented every section of the code to make it easier to understand for people wanting to learn.
I also learned about static linking from making this project. It’s something I guess you don’t know about when you’ve grown up with IDEs and very high level languages which handle most of the linking hassle for you. But apparently you can just put whatever libraries you’re using in the compiled executable, which will make it larger but also self contained, i.e. no darn dependencies!
Dependencies have always been a bitch for me; I couldn’t show off my first games when I was a kid since they would only run on my own PC(GameMaker was a dream come true). Sometimes I find it hard to find dependencies and package them properly; to this day I still don’t know how to deploy MonoDevelop/GTK projects short of just including the entirety of MonoDevelop in the package, which really sucks since I wanted to use it for a Linux/Windows game. I wonder how Unity3D does it?

Ideas for expansion

  • Multiple file servers on one host: You could alter the server so that if the UDP port is taken it will send its filename and endpoint to the UDP port so the currently running fileserver can advertise its presence for it. You could also just make a single fileserver handle more files, but that would make it more complicated and defeat the purpose of the project, i.e. easy file transfer.
  • More customizability: Most of the options are hardcoded; port numbers, buffer size, etc., simply because I was too lazy to handle arguments.

Download

Without further ado:

Linux binaries

Windows binaries(UNSTABLE!)

Commented source files

Creating 3D models(And some life story)

FishDesktop2(Click picture for wallpaper!)
I got really inspired watching time-lapse videos of people creating 3D assets so I decided to model, rig and texture a 3D model for real, i.e. extruding, cutting, welding, UV mapping by hand and carefully skinning the model. This is my first real attempt at creating a 3D model.

Continue reading

Save Us, Unity3D vs GameMaker comparison

I’m still early in the process of porting an old game from GameMaker to Unity3D. I thought I’d compare them graphically, since I’ve reached a point where most of the 3D modeling is done.

Overall I think going full 3D adds a little immersiveness to the game and makes everything look a little nicer, while I loose a bit of aesthetic, but that’s purely because of my lack of skill with 3D modeling. In any case, porting to Unity3D will make me able to make Save Us into a browser game, thereby reaching a larger audience.

Compare2
I think this scene made it out the best. Because of the bad graphics and stupid looking characters, the atmosphere was completely broken in the old version. The new version has a much creepier feel to it. You can’t see it in the picture, but imagine the cloaks of the characters flailing gently in the air. I wish I could’ve done a better job of modeling and texturing them though.

Compare3
The old version was more stylized here. I had more control since everything was just 2D sprites. The sheep don’t look nearly as good in the new version and I’m not sure if the intensity of the grass in the new version is too high. In any case, the aesthetic could probably be saved by a fullscreen shader, which I can’t utilize unless I pay 1500$ for Unity Pro. On the plus side the claw is way more awesome now, it’s really come alive because of its animations. I really need to add a hatch to the robot.

Compare1
I completely re-did the bad guys. In the old version they had a genie like shape; floating in the air with a tail thing. I did that so I wouldn’t have to render an animated walk cycle from 8 different angles. This is not a problem in the new version, so I made them four legged with inverted knees on the back legs for added creepiness.

Compare4
I really like this screenshot. The yellowish dots are eyes; I made a shader with an emissive texture so that the eyes of the bad guys glow in the dark.

Fresnel shader in Unity(With source)

FresnelShader

I was just messing around with shaders in Unity while working on a game. I’ve never really tried making a shader before because I feel the math is intimidating, but it is actually pretty simple once you get into it. I’ve always liked the fresnel effect; it looks cool for microbiological illustrations and it’s nice for highlighting stuff.

The way it works is that you take the dot product of the direction vector of the camera and the normal of the current pixel being rendered on the model(See picture below). The dot product will go from 1 to 0(assuming that we’re dealing with unit vectors(we are)) the closer these vectors are to being perpendicular and the closer these vectors are to being perpendicular the closer the pixel is to the edge of the model being rendered in relation to the camera. You then take the inverse of the dot product, i.e. 1 – (cameraDirection · normal), and you multiply it with the color you want. The color then gets more intense the closer you get to the edge of the model, as you can see in the picture above. If you add a bump map to this, the surface normals get distorted and the fresnel effect distorts with it, which looks really cool as you can see in the bottom two spheres on the right, in the picture above.

Normals

The white lines are the normals of the sphere; the outward direction of the face.

Anyway, feel free to use it in your own projects as much as you want, link in the comments if you wanna show off something you’ve made with it. Here’s the source:

Shader "Custom/FresnelShader" {
	Properties {
	 	_Shininess ("Shininess", Range (0.01, 3)) = 1

	 	_MyColor ("Shine Color", Color) = (1,1,1,1) 

		_MainTex ("Base (RGB)", 2D) = "white" {}

		_Bump ("Bump", 2D) = "bump" {}

	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200

		CGPROGRAM
		#pragma surface surf Lambert

		sampler2D _MainTex;
		sampler2D _Bump;
		float _Shininess;
		fixed4 _MyColor; 

		struct Input {
			float2 uv_MainTex;
			float2 uv_Bump;
			float3 viewDir;
		};

		void surf (Input IN, inout SurfaceOutput o) {
			half4 c = tex2D (_MainTex, IN.uv_MainTex);
			o.Normal = UnpackNormal(tex2D(_Bump, IN.uv_Bump));
			half factor = dot(normalize(IN.viewDir),o.Normal);
			o.Albedo = c.rgb+_MyColor*(_Shininess-factor*_Shininess);
			o.Emission.rgb = _MyColor*(_Shininess-factor*_Shininess);
			o.Alpha = c.a;
		}
		ENDCG
	} 
	FallBack "Diffuse"
}

Save Us, Unity3D remake

I’m currently in the process of converting my old competition game to a full 3D browser game using Unity3D.

SaveUs1
Checking if importing in unity is easy(It is) and trying out some basic behavior for the sheep things(Walk to a random point, when you get there: Choose a new point. Obstacle? Choose a new a point). And yes, I’m well aware of the fact that they are extremely creepy.

SaveUs2
Adding the player(Robot thing) and setting up the top-down camera with look. I read that you can use curves, specifically AnimationCurve, as a public variable in Unity and Unity will provide a curve editor! I used this to make a hover effect for the robot by changing the y-position with a smooth curve thereby making it bob. I read it from this post from SauropodStudio(Looking forward to Castle Story).

SaveUs3
Attached two lights to the robot(Second one at the feet for hover effect) and updated floor texture. Also, notice how much the shadows add to making it look like the sheep are more grounded, and not just added to the background. It’s just a 2 polygon square, 0.01 units above the ground, with a black gradient circle texture:
Shadow

SaveUs4
Re-did the sheep things, or rather, I spent a huge amount of time merging and welding polygons from the old sheep(From the first Save Us game) to make a new sheep with a single textured and rigged surface. Since they were only gonna be rendered to a sprite, the old sheep consisted of hundreds of high polygon spheres, I had to degrade the quality a lot for performance. They are less frightening but jaggy, but I fixed that in the next picture using some phong shading(Bascially checking a box in Unity).

SaveUs5
Added trees, sadly not my own generated ones, since it was quicker to just create a new one with splines rather than writing an export function for my tree generator(Well, probably not). I should just port the tree generator to Unity since they’re both C#! Great idea me 😀

Home Theater PC for free

HTPC

What you need, if you want the HTPC(Home Theater PC) for free:

  • A TV with VGA or HDMI input(Possibly audio input jack or speaker system).
  • A wireless router.
  • An old PC(Possibly laptop, if you’re wiling to plug it in every time you want to watch stuff).
  • A smartphone(Only necessary for easily portable remote control).
  • A USB with about 1 GB free space or a writable DVD.
  • Willingness to “just fucking Google it”.

Continue reading

Church of Denmark

I am no longer a member of the Church of Denmark. I’ve been a member all my life, but I’ve never believed in anything. (If you’re not interested in reading a somewhat aimless rant on religion, don’t continue reading). Continue reading

Procedurally generated tree(With source)

I was bored one day so I sat down and tried making a random tree generator. Essentially it’s extremely simple, you basically have this:

struct Tree
    {
    int x, y, z;
    float radius;
    Tree[] subTrees;
    }

It’s a root which defines a point in R3, a thickness and branches connected to it. From this structure I can generate a tree by recursively traversing the structure, building cylinders as I go:

AddCylinder(int x1, int  y1, int  z1,
            int  x2, int  y2, int  z2,
            float w1, float w2, 3DModel model)
    {
    //Construct cylinder from (x1,y1,z1) with raidus w1 to (x2,y2,z2) with radius w2
    //Add cylinder to model
    }

BuildTree(Tree node, 3DModel model)
    {
    foreach(Tree subTree in node.subTrees)
        {
        AddCylinder(node.x, node.y, node.z,
                    subTree.x, subTree.y, subTree.z,
                    node.radius, subTree.radius, model);
        BuildTree(subTree, model)
        }
    }

The interesting part of all this is finding an algorithm which generates cool trees. My algorithm was kind of half-assed, I have a much better idea now than I did when I was motivated to implement it. I was thinking of having three cases and then choosing between them: “Stop here”, “Continue growing” or “Branch out”. Then it would be a simple problem of choosing what would be best at any given time. My current algorithm is implemented like “I just want to see if it works”.

treegen

(Note that the “leaves” are icosahedrons stolen from my first post)

As you can see the approach I have to constructing the cylinders is not as great as it could have been. The ends are always on an XY plane, or aligned with the floor. I could’ve rotated them so they were facing the direction they’re growing in and then connected them with an extra cylinder at each node:

TreeGen2

The generator has the potential of building really realistic trees by adding a lot more small branches and adding a lot of small leave sprites instead of big spheres added to the end of each branch.

It’s written in C# using .NET 3.5 and TrueVision3D(DLL included). Here’s the source:

Download Here

File: TreeGen.rar

Contents: C# Solution and TrueVision3D assembly.

Size: ~4.5 MB

 

Clever title here(Site facelift)

So, after about a year(Just paid my yearly bills for the domain name and web hosting) I decided to replace the guts of my site(New design by my beautiful girlfriend).

The site had mainly become my weird obsession with observing spam comments. I coded this anti spam script, which would compare the content of a comment with a list of banned words; if a word in your comment is banned, the comment will not be submitted. I would check in regularly to see if any words had gained traction or if there were new words to ban, but not really much else.

This was what I retrieved from the database before I pulled down my old code, sorted by # times caught(I accidentally the whole table at some point, so the words have been caught more times than stated, especially viagra):

Word # times caught Last catch
chanel 25 14-01-2013 01:33
payday 13 15-01-2013 09:26
viagra 13 17-01-2013 00:36
cialis 7 12-12-2012 15:24
louis vuitton 5 12-01-2013 23:08
levitra 4 12-12-2012 09:32
ex back 3 07-12-2012 12:36
ceftin 3 22-11-2012 22:42
propecia 3 07-11-2012 05:55
northface 2 11-11-2012 08:04
escort 2 06-11-2012 00:13
girlgetsring 1 17-11-2012 10:47
anti wrinkle 1 07-11-2012 15:30
http://ggrsite.com 1 11-11-2012 10:25
amoxil 1 07-01-2013 14:21
nolvadex 1 11-01-2013 02:55
gucci 1 15-01-2013 20:43
zoloft 1 15-01-2013 20:43
http://www.rmtest.com 1 15-01-2013 20:44
http://cufikiro.hotbox.ru 1 02-11-2012 17:36
zithromax 1 01-11-2012 09:36
arthritis 1 29-10-2012 12:17

I wasn’t using the site for anything productive. I wrote the site from scratch in about one week after just one week of having first tried PHP. The admin interface I implemented was rubbish, I didn’t put any effort in to it; it was just a means to an end. Adding new content was a pain in the ass, so I couldn’t force myself to do it.

I like doing things myself, reinventing the wheel, because I believe it’s the best way to learn. But having a site I never update because it’s difficult is just stupid. So I gave up and installed WordPress. I had everything up and running in about 2 hours! Hopefully now I can start writing more.

Here’s some highlights of the old site:

coredumping1

My beautiful, somewhat large, header.

coredumping3

I had a custom game overview site. WordPress is all about blogging, so there’s not really any easy way to add a game portfolio with download links, status, etc. But I can have categories and just pretend that a regular blog post is a game showcase.

coredumping2

This is my own image scroller thing, written in javascript. I really fucking hate writing javascript, but I love the opportunities it opens up for making dynamic content on websites. Here’s a link to the code.

Well that’s about it. I really hope to be posting more about what I do and think in the future. In fact, even though it’s late and fucking dumb, it will be my new year’s resolution!