Sunday, December 21, 2014

Networking (Week 50 + Week 51)

Where do I start?
Well I started with assignment 3 last week, which consists of making a 3D game using DirectX. I became so overwhelmed (and still am) by the task that I basically did nothing for two weeks! Sure I read about programming patterns and networking and I did dibble in some network programming.

So that’s why I have no blog post from last week. I didn’t do anything worth blogging about.  So a few days ago I finally started working on the assignment.
I have made some basic server network code and a packet managing system and I have also merged my code with the code from our teacher Tommi.

I started out with networking because that’s hard to add when the game is already done. So my basic plan is this layout:



So the client or player does something. That is made into a packet of data that is stored in a queue that the packet handler / manager take care of.  
Next step is that this is sent down to the lower network level that actual take care of the sending and receiving data using UDP. So the packet is sent to the server and put into a queue by the packet handler on the server side and then the server reads and handles the data and updates the game logic and creates a new packet and sends it back to the client or to all clients depending on the logic inside.

As you can see in the picture above the client has its on local game logic. This is so the player can still collide and move around in the world without feedback from the server. This so the player don’t have to wait for half a second if a packet is lost along the way.

When the server has handled everything and the packet is back to the client there will be a check if the client position/ state is different from the server’s calculations. If it is then the client will be corrected to the server data.

Each packet is made up of a packet header and the actual data from the client or the server depending on where it’s created. I haven’t created the client side of the network yet so the code is not tested but this is how a header looks like:


The reason I made a header is so I can keep track of who created the packet. What type of packet it is. When it was created and how big it is and if it’s an outgoing or incoming packet. I don’t know if all this is needed but it’s a start and I’m no experienced network programmer.


If you can't read it then just ask me here and I will allow you access.

Monday, December 8, 2014

Planning the next step. (Week 49)



So this week I haven't done much actual programming. I have just been reading up about different programming patterns and I have also been planning about how to create a server solution for the game we need to make in assignment 3.

Making a game with a multiplayer solution is only needed if you aim for a higher grade, which I do.

So I'm reading up about different things to think about when it comes to game networking. I first found this site with an introduction to multiplayer game programming it only explains the basics of how it works on the surface but then I found this new site and it's quite good at explaining the different problems http://gafferongames.com/networking-for-game-programmers/

So after reading that I have a rough plan on how I want to go around this. I will create a Server/client solution with UDP networking because as far as I have understood its far more effective for games with UDP than using TCP protocol, because TCP always need to make sure every packet gets delivered and that can clog up the traffic and in worst case scenarios create lag in the game if the game always have to wait for each packet being delivered.

I have also understood that since UDP just shoots packets like a machine gun and that I need to somehow check what packets I get and when they were sent and check that against the packets I already received because with UDP there is no guarantee that the first packet sent is the first you receive. I need to do this so check so the players will have a smoother gaming experience.

Other stuff I have been reading up on this week is on this site:
http://gameprogrammingpatterns.com/contents.html it’s a must read for anyone who wants to be a serious programmer. I have read through about half of it and it’s really amazing and it’s easy to read and follow the author. I especially like the part about the command pattern and I’m thinking of incorporate that into my programming.

Why you might ask? Well I have always wondered how teams can program in the same programs without getting all tangled up in each other’s code and I have learned from this site about something called decoupling. It’s when you program in a way that all the modules you make can just be put together without really needing to know much about the other components in the program.
 I want to push myself into learning more advanced programming techniques like this and I feel the command pattern is a good place to start.  Found here 

Monday, December 1, 2014

Done with the Web Server! (Week 48)

So I managed to finish my web server. 

When I started coding I managed to get something up and working in a few hours and I blogged about it last week, but then I realized that there was much more to assignment 2 so I got down to business.

First of all I had to figure out how a web browser operates. Something I knew nothing about. My classmate Jonas explained how the browser sends a “GET” request (e.g. GET / HTTP1.1) to the server and how search paths works when you only use your IP-address in the search field of the browser.

Understanding that I managed to get the information sent by the web browser and saved that info into a buffer. From the buffer I could read what kind of things the browser requested and supported.

I started with managing a simple get request.  When someone enters my web server IP-address I answer by sending header information about the server.

http://en.wikipedia.org/wiki/List_of_HTTP_header_fields

Only a few of these header fields has to be included in the header info the server sends. Content type and the content length is something that needs to be included.

The info that is sent has to be EXACTLY correct or the page never finish loading or don’t load at all. I thought it didn’t matter because I only sent strings with text in them.  It took about a week figuring out that this was what caused most of my troubles. This is what I finally included as my header info:


Another big problem I had was to figure out how to handle timeouts or disconnects from the client side. First I went around this problem by just sending information to the client to see if it arrives, like a “ping” but that felt strange to do every loop so I kept looking for other ways and I found this website


explaining that there is no real sure way to know that the client has lost connection unless the server and client are both programmed to handle this. But since I only make the server side I can’t do that.


So I went with just keeping a timer that updates only when a user sends data to the server. If no data is sent for five minutes the server cuts the connection to the client

























..…but in our assignment it says that we have to respect the browsers connection header so I extracted the information of the connection status from the web browser that is stored in my receiving data buffer and I set the client to keep alive if it’s a keep alive connection and to closed if it’s a closed connection.


I chose to include both capital and small letters in my control because I noticed that the browsers sent different information.

That’s it for this week. I will start with assignment 3 next week. Making a 3D engine and a game….Alone! so that’s a challenge.


Friday, November 21, 2014

My first Webserver!,*cough*...kind of.

So this week I have been working on assignment 2, making a web server using C++ and Winsock2 API. It went quite well at the start and in basically one day I got a simple server (if you can call it that) that could handle one connection. I received data from the user and I displayed some text in the web browser to the user.

 


As you can see the browser in the back and in the front is the C++ console that shows the IP address of the user. I also show how many bytes I received and how many I sent. So far so good, but as you can see in the picture there are two new visitors from same IP address, that’s something I need to fix because right now I just grab anything I got, delete it and grab it again for some reason I have yet to figure out. I also I also don't handle time outs correctly and a bunch of other things but I don't care. It's a great feeling just getting something on screen. It made my day!

 Below is part of my main loop. First I had the myServer.listenOnSocket() function inside the loop but after asking our teacher Tommi how the listen() function for Winsock2 works I moved it out from the loop. Apparently the function opens up a listener for sockets that keep listening until closed. So it doesn't need to be in the loop since it keeps listening.




I then call my accept client function and accept the client and creates a client struct that holds all the data of the client, like the IP address and such. After that I call my update client connections which I want to use for updating retrieving and sending data as well as checking for time outs from the clients but I won't show that in detail because it's not working properly yet.

When I built this I thought about going around it like a game loop because a connection to a website/ server is in my mind is always open like a game that is always running.  Today I almost managed to handle multiple connections. The only trouble I have is when one new user connects the other one gets timed out. I'm not sure how to solve that but I will have to do some more code sniffing. So this post isn’t so analytic but I can’t help it because right now I’m just playing around with all the functions in this API trying to figure out how it all works.  Maybe I will dive deeper into this in my next post.

By the way, Last week I mentioned I had some problems with templates. This one:
template<typename T>               //this is the way I tried with no success
class LinkedList<T>;

and:
bool VerifyListErase(LinkedList<T> _List, const std::string& _Message);
Well apparently my error was not how I wrote these. They are all correct. The error was in a completely other place in the code. That’s the difficult thing with templates. I will use them more carefully in the future because the errors you get don’t make sense.



Friday, November 14, 2014

Back to programming!

Finally we started programming again and moved on from all that nonsense of playing board games and whatnot. Part of this course we need to write weekly blogs so I will be more active again.

So in this course we have 3 assignments.

Assignment 1 - Create a linked list and a Binary search tree. (Almost done with this)
Assignment 2 - Consists of writing a webserver
Assignment 3 - Create our own 3D game and engine using DirectX.

So this week I will share my thoughts on Assignment 1. So I actually did create a linked list and a binary search tree a few months ago with the help from youtube, especially from Paul Programming .
It's not very hard. The hard part is keeping track of your pointers.

This week we also learned how to use templates in programming to make our programming more generic. I still have problems using templates in a effective way but I gave it a try and made a linked list and BST (Binary search tree) with templates in mind. at first I got a bunch of errors that made no sense but I fast started to get a hand of how the syntax was supposed to look. Though I'm still struggling with some things, For example when working with normal classes you can forward declare instead of using #include "headername.h" you just write class classname; before the class where you want to use it, so I wrote this:


( class LinkedList;                         //normal way to write a forward declare )

template<typename T>               //this is the way I tried with no success
class LinkedList<T>;


template <typnename T>
class TestModule
{
public:
                  LinkedList();
                  ~LinkedList();

private:
........
........
}

So I still have to figure out how to do that and also another thing I had problem with was how to use another template as a function parameter in a function that is part of class template. Like so:

template <typnename T>
class TestModule
{
public:
bool VerifyListErase(LinkedList<T> _List, const std::string& _Message);

.........
.........
}
If you read this and know how to solve this please feel free to comment below. I will write here again when I find a good solution. It's probably not that hard.
Recursion
So on to what I liked about this week. Recursion! Using recursive functions can be really useful but sometimes they might be tricky to understand. It is basically a function that calls itself within the function creating a loop. 

Below is a search function I wrote that starts the search in the binary search tree from the node that is entered in and search through the whole tree/sub tree for a specific key/data. If a match is found the boolean variable _Match is set to true.



If no match is found the function calls itself and goes to the left and does the whole thing over again until it’s not possible to go left anymore. Then the function moves upwards one step and then tries to go right. It searches through every node until something is found or not found. 

It technically moves through the tree like the red numbers below show. If new to the concept it’s quite hard to grasp so I will try to explain it again.  What happens when a function calls itself is that it goes deeper and deeper until the end and then it returns up one step and does the next instruction. if there is no instruction it goes up another step and checks and so on.

So for example looking at the picture below and starting at 1 the function goes left every iteration until it can't go left more and when number 5 is reached and no match is found the function moves up one step to number 6 and then calls the function again but now with parameter to go right. So it goes to number 7, when nothing is found it moves up to number 8 and then 9 and then goes right again down to 10 and then back up and so on until all nodes have been searched..



If you want to see my code here is a link to Bitbucket. You can use my code freely but if something breaks it’s not my fault. J

Friday, September 26, 2014

Pandemic - Board Game Analysis 2

This week me and my group played another board game and here is the analysis of that game.

The game we chose was Pandemic. It’s played by 2-4 players. The goal of the game is to fight diseases and find cures for them. When all cures are found you win the game. This is done together with the other players where you help each other fighting outbreaks around the world. You play on earth and can move around to different cities in the world. The cities in the world are colored in four different colors. Each color also has a disease with that same color. Before the game start some disease cubes are randomly placed on the board on locations on the infection cards that are drawn before start.

How it’s played

The characters

Each player randomly gets one type of character card describing your special abilities at the start of the game. Those are the scientist, medic, researcher, dispatcher and operational expert. You can see their special abilities in the pictures. So a player can’t choose the role he or she is most comfortable with. This makes the game fair in the way that all get the same chance to play a certain role.


One turn


It’s a turn based game and you have four actions per turn. You can choose to use your actions how you wish. You can move in different ways, fight disease in an area, build a research station, discover a cure or share your knowledge (share cards with other players). How you can play these actions depend on the cards you hold and also your special abilities.

After you have done your actions you draw two player cards which can contain three types of cards, a city card with a color connected to that city, a special event card that basically is a special power card that can be used one time and the epidemic card.

If there are no player cards left in the pile the game is lost.

Then you draw a number of infection cards (depending on the infection rate) The cards show a city and a color and you place one new disease of that color in that city for each card.
If there not enough cubes left to place on the board the players lose the game.
 
City card

The city card has a color and a city name of it. By using this card a player can fly to that location using an action point or the card can be saved for creating a cure for the disease of cards color later on. You can also give these cards to other players. 

Special event card

The special event card can be used any time even if it isn’t your turn and they are all different. With one you can create a research station anywhere in the world, another let you fly a player to any city in the world and so on. 

Epidemic card

If you get the epidemic card some special things happen. First the infection rate is moved one step which might result in the players having to draw more infection cards every round. You then draw a card from the bottom of the infection card pile and place three disease cubes in that city and you then reshuffle all the played infection cards and place them on top of the infection card pile. This will make it so the played infection cards will soon be played again resulting in disease cubes popping up in same cities once more.  This together will easily lead to outbreaks. 

Outbreaks

Outbreaks occur when a disease cube needs to be added to a city and the total amount of cubes in that city exceeds three. Then the disease spread to all adjacent cities with one cube in each of those cities. If one of those cities also exceeds three you get a chain reaction of outbreaks. That’s bad and can make you lose the game quickly because if you get more than seven outbreaks totally in a play through everyone loses the game.



Cures

You can create a cure if you got five cards of the same color and you are in a city with a research station. (The scientist only needs four cards) That disease can after it's been cured be removed by the medic without using action points. When all cubes of that color is removed from the map that disease will never be able to come back and is cured forever.

To win the game you have to create the cure for all four diseases. All disease cubes don't have to be removed though.




The Core system

The core system of this game is moving around the board and removing disease cubes. New disease cubes are placed on the board depending on the epidemic cards and the infection cards. Your job is to treat the infected areas as soon as possible to prevent outbreaks at the same time you need to find a way to create a cure for that disease.

To find a cure you need to constantly have a dialog and work together with the other players. You need to share cards of the right color by being in the same location or maybe build a research station close to a scientist so he can create a cure there. And as a dispatcher you can move around other players so they can meet up in the right place and so on. All the actions a player can do in a turn is shown on the pictures below.




The Good

The balance of the game is amazing. It’s really nerve wrecking. We played a bunch of times and we only won one time but we almost won all other times. Most of the times we were basically only one turn from winning the game. We all found the game entertaining and well balanced. If the game would have been too easy I believe we would only play once or twice and then get tired of it but because it’s so difficult it makes you want to play again and again. You want to beat the game.
When we all learned how to combine our strengths the game got easier and alot more options and strategies were available to us. 

The Bad

This is a tough one. I like the game as it is. I think that the game is very well balanced and I like the core mechanics and how the players need to interact with each other. If I need to think of something that is bad I would say that the player cards are a bit boring. There is not much that happens when you take two player cards except for the epidemic card. I think as an improvement to the game they could add some spice by adding some more random events and story to the player cards or maybe just some more special event cards.  

Interesting system

For me the most interesting system is the randomness of the player characters and the ability to find a cure together. The different attributes of the characters and the way the players constantly need to keep talking strategies to each other to be able to find a cure in time makes this game really interesting. You also need to constantly adapt to the situation. Do I choose to remove some disease in the city where I am or do I help my fellow player the scientist by meeting up with him and sharing knowledge so he can find a cure so the medic in his turn can cure an area from disease. The game is a nice social gaming experience. The cooperative game play is really a breath of fresh air compared to most other games out there where you compete to win against the other players.

To be able to beat this game you need to really learn how to use the strengths of your characters and work together and I believe this is what makes this game so interesting. After playing a few times we saw solutions to certain problems in other ways then just individually moving to a place and removing the disease in that city. The dispatcher for example is really powerful when you learn how to use him in the correct way. He can move other players around the world so they can meet and share cards and so they come up with a cure just in time before the game ends.

Target Group

On the box it says 10 years and up and I believe that’s quite fitting. For young players this can be an educational experience and also at this age I believe the children can start to understand the cooperative parts of the game. I believe that this game aim for a broad group of players at all ages. This is not the game for the competitive person but more so for people who are more of a socializer that likes helping others instead of always winning.


Summary

The game is quite easy to learn and setup. You learn to play it instantly but it takes time to master. It’s when you realize that the more you work together the better the results will be and the higher the chances are for you all to win. Adding so the characters can’t be chosen but are drawn to give some randomness makes you rethink your role in the team. Some players who might have played one role know how to play that good. When they are given another role they then need to rethink when playing. This adds replayability to the game, this and the difficulty level. When you win the game you can increase the difficulty by adding more epidemic cards into the player card pile. This game is nerve wrecking and you feel you are about to win when you suddenly get a chain reaction of outbreaks just messing up your plan and making you all lose the game just before you were about to win. A game takes about 45 minutes to play and I would recomend this game to people who want to try something different.

Friday, September 12, 2014

Small World - Analysis

Last week we were assigned to play and analyze a board game of our choice in the course, Advanced game design. My group chose Small world and here is my personal analysis of the game.



The game

Small world is a turn based strategy game where your goal is to collect coins by conquering and holding areas of a map. You get coins depending on a number of different things like how many provinces you hold and you can also get bonus coins depending on your racial and special power abilities. The player with the most amounts of coins after a set number of turns win the game. The game can be played by 2-5 players and depending on the number of players the board that the game is played on is different and so is the number of turns.

Choosing a race combo

You start out with five coins. At the start of the first turn you can choose or buy a race you want to play. With the race there is also a special power card connected. The races you can chose from are random and so are the special power cards. From the deck with the races and power cards only the top five are placed on the table. (see picture below)

The first is free to pick. The second race cost one coin and the third two coins and so on. All the placed coins by previous players will become yours if you choose a race card with money on it. This is probably developed in such a way so if you are not allowed to go first you can choose a race with all the coins from previous players giving you some bonus for your start. And it’s not a bad bonus since the game is about getting the highest amount of coins before the end of the game.


Conquering

Now you have your race and are ready to go. Depending on the race and power card combo you’ve chosen you get a number of tokens that you can place on the map to conquer new areas. If you look at the picture below you see these big numbers in the lower corners of each card. this represent the number of tokens you are allowed to start with. So if I would choose Fortified Amazons I would get 6 tokens for being Amazon and then 3 more tokens for the special power card Fortified. And being Amazon also grants me 4 extra tokens that I can use when I'm conquering but that have to be removed from the board when my turn is up.


An empty province costs two tokens to conquer. An occupied province cost the original two tokens + one extra token for each unit occupying that space. When you have used all your tokens and conquered as much as you can, you can redeploy your tokens on the conquered provinces to increase the defence in certain positions. You then count your score and receive your coins depending on the number of provinces and special abilities you have. In this case my race won't give me extra coins but I have the ability to build a fortress each turn and that increase the defence in that area by one but also gives that province one extra coin per turn. So I would naturally do that. When I'm done the turn moves on to the next player. When the last player is done the game moves on to the next round.


Here on the picture to the left you see tokens of skeletons on the board. conquering this province would need two for the basic cost and then four extra tokens, one for each skeleton. At the end of the turn if you not have enough tokens to conquer a province you can chose to gamble with the special dice that comes with this game. The dice have three empty sides and when rolled you can get 1-3 extra tokens for that attack only.

Using your race to your advantage

If you want to win this game it’s quite important to play your race correctly. For example if you have Fortified Humans you get a bonus to holding farmlands so you should conquer as many farmland provinces as you can and try to fortify yourself there. Building forts and holding farmlands gives you extra coins.

On the other hand, if you have the Pillaging Ratmen your tactics should be to conquer as much as you can as fast as possible because you don’t have any other special abilities with your race other than the big numbers of tokens and the power card bonus for pillaging which gives a bonus to conquering .The next step would be turning your race into decline.

Turning your race into decline

In the start of each round you can choose to keep conquering and collecting coins or you can turn your race into decline. When you turn your race into decline you flip over your race card, special power card and all placed tokens on the board.

You can no longer use this race to conquer new provinces but you still get one coin for each province your declined race holds at the end of your turn. Then the next round you get to choose or buy a new race to play and you will get coins from both the declined race and the active race. The ability to turn your race into decline is really one of the things that make this game special. Comparing this to a normal game of Risk (http://en.wikipedia.org/wiki/Risk_(game) ) where if you lose the last troops you control you will be out of the game. Well here you get a fresh new race and can begin conquering all over again.

Here is where the strategy of this game comes in. Timing the turn you go into decline can be crucial for your success in winning this game. If you overextend and ignore going into decline you will soon have your forces spread thin and you will be an easy target by other players and will quickly lose income. Also turning into decline at the right moment to be able to choose a new race with a powerful power card combo before your opponents is able to do the same is a game changer. You can go from losing the game to almost winning the game in a few turns.

The Good - Randomness

As I’ve just mentioned the ability to go into decline is really a great thing about this game. Another good thing about this game is the randomness of the races and how you get to pick a race. Each game is different because you and your opponents will have different race and power card combinations to choose from and you will have to apply different strategies each time you play this game all because of the different race combos available. Combining this with the decline system is what gives this game a unique feel with great replay value.



The Bad - Overpowered combinations

The only downside of this game that I can think of is that it sometimes lead to some combinations of race and power cards to come up that are really overpowered and with some luck one player can manage to get one powerful combo after another leading him to victory leaving a feeling of unfairness.

For example one round we played I had just finished conquering around seven provinces when another player picked a new race and steamrolled all over my province and conquered it all in one turn.

Also sometimes the rules weren't exactly clear on how some abilities was supposed to work so we had to fill in with what we felt worked best. It was only minor things and nothing game breaking.

Who this game is for

It says on the box that the game is for ages eight and up which is very broad. I believe this game is aimed towards people who enjoy strategy games but don’t have a lot of time to play too deep and complex games. This game is easy to learn and you can just pick it up and play but when you play it a few times you soon realize that there are some strategy involved here that can tip the scale in your favor. 

I myself am a strategy fan and it was a long time since I played a board game and I really liked this game. The game itself took around one and a half hour to play and it felt like an okay length for a game. This game could easily be played by a family with the father of the family having the interests in these kinds of games or by a group of friends meeting an evening to hang out.
So I believe the biggest target group is male in his 20- 40s with interests in strategy type of games. The secondary target will be friends and family of that individual.


Summary


At first glance this game looks complex but it’s quite easy to learn. It’s very easy to play with the hardest thing being to remember to count your score after each turn. It has some unique features like the system of going into decline and picking a new race to play which makes it a game where all players can play to the finish without being eliminated like in a Risk game. 

The way that this game also combines the randomness of the races together with the decline system really makes this game shine by forcing the player to always adapt to the game and planning your strategy well and then if a new racial combo appear, you have to rethink your whole strategy for the next round because everything changed. Overall I think this is a good game with light strategy elements to it. 

Thursday, March 20, 2014

Project Aurora, post 10, (Blog Assignment 6) Lighting up the dark waters

One of the things I have done this week is adding plants that the player can light up using his fishingrod with the light bulb, like so:



First I thought about adding a special class for these types of plants called interactive objects but since they will differ so little from normal game objects I decided to keep them in the basic game object class for now, the biggest reason being lack of time.

I have already added earlier in code that game objects can hold a light so all I had to do was to enable this light and then use it. It was fairly easy to add and makes a huge different in the feeling of how the world looks. The plan is also to add AI to the other fish that make them attracted to these lights but I'm not sure we will have time to implement that.













So the circle for the plant light is cut out of the dark surroundings the same way as the player light circle mentioned in earlier posts but then I also added a sprite for the yellow light you see in the screenshot for both the player and the plants to make the illusion that the light bulbs actually emits light. It's hard coded?? (It's coded in a non-dynamic way) So the position for these sprites is fixed with numeric values, which is not that good but at this point it's all about getting our features into the game before time runs out.

Then I started adding collider boxes for the plant light bulbs so the program would know when to turn them on or off. For example when the player light bulb is close in range it would light up the plant.
It caused a few minor problems with the player vibrating when near the lights because our collision detection gives off an offset on both objects colliding and it caused the player to bump and move in an illogical way.

Instead of changing how our collision is handled. Changes that can cause our other objects to behave differently I decided to look for another solution. I found one in SFML. SFML have a built in function that checks if a sprite box intersects with another sprite box. It's called Intersects() and since I now use a sprite for the yellow light on both the player and the plants I could simply just check if these intersects and if they do light up the plants light bulb for a set amount of seconds. Here is some code of it:







First I check if the game object has a light. If it has I check the status of the players light bulb (on or off). I then check if the player light sprite intersects with the game object light sprite and if they do the game object light is toggled on.

Right now the timer is on 10 seconds. After 10 seconds the light on the game object goes out. This feature isn't a "game changer" but it adds a lot to the atmosphere and feeling of the game world making it feel more alive.

The feedback I got from testing this feature was really good by the people who tried it so I'm quite happy with it.

Wednesday, March 12, 2014

Project Aurora, post 9, (Blog Assignment 5) Darkness in the deep

In our game it gets darker the deeper you go in the ocean and I was tasked to implement this feature because I had already worked with the camera and the black filter that covers the view (see earlier posts).

My first thought was to implement this so the changes of opacity are only happening where the camera is but I started with asking my fellow group mates and class mates how they would go about doing this. All of them basiclly said why not cover the whole world with a black filter with a gradient that starts at a certain position / depth on the Y-axis and that covers the whole X-axis. (see picture)

I thought, sure that might work. I just have to do some changes to how the sf::RenderTexure works with the camera (again earlier posts) I then went on to try to make the RenderTexure as big as half our game world and it ended with a size of 22.000 pixels in X-axis and around 8000 pixels in Y-axis. considering our game world will probably grow to the double of this size it's going to be HUGE! 

Since RenderTexture don't load a texture from a file I thought it would be fine but it seems like there is a limit of some sort. When I made it too big, the RenderTexture / Black filter wasn't drawn at all. If i made it too big in Y-axis it crashed our animations in some way I don't yet understand.

So I went back to my earlier thought to only make the changes where the camera is. I found the solution to be fairly simple actually. Since the sf::RenderTexture work similar like a sf::RenderWindow with a clear(), draw() and a display() funtion I came up with an idea to try.

The clear()- function works like this. It clears the window or in this case RenderTexture with a RGB color and a Alpha value.


So I though why not just manipulate this alpha value the deeper I go? So thats what I did.
I started thinking I need to know how deep the player is so I took the players Y-position for that. Then I also need a position where the darkness start to kick in. because we don't want our game to start getting darker until the player reach a certain depth. and Then I need to determine how fast it gets darker.



So first I use the clear() function with zero alpha so everything is seen. Then when you reach a certain depth it starts getting darker. I added a constant called OPACITYSTART to keep track of that position.

Since the alpha value can only be 0-255 I thought if every pixel movement in Y-axis the player makes increase the alpha by 1 then it gets dark very fast. So I increased this value and called the constant OPACITYDEPTH and set a fixed value of 1000 pixels. so this gives 1000 / 256 so basicly the alpha value/opacity is increased by 1 every 4 pixels. (still very fast but all these numbers are only for testing and the final game will have different values.)

Then in our game we don't want it pitch black except maybe for the deepest levels of the ocean so I put in another constant called OPACITYMAX. This is the darkest it can get.

So if the Player position is higher than OPACITYSTART it starts getting darker.
I added a while loop that checks if the player is within the limit OPACITYSTART and a check that the OpacityCounter is within the maximum limit OPACITYMAX.

The variable called OpacityLevel gets the value of 1000/ 256 that I mentioned. This is added for every iteration creating a leveled layer, like 4, 8, 12 ,16 and so on.(not exakt numbers) An OpacityCounter counts what level you are in and it's this counters value that then determine how high the alpha value/ opacity will be seen in the last line of code.

Then I call the draw() function and display() function to draw /cut the light circle out from the black filter like I mentioned in earlier posts, and now with a gradient darkness depending on position instead of completely black as it was before.

The result is shown below, the deeper the player is the darker it gets. The last picture look pitch black but it isnt.