Categories

Thursday, 19 March 2015

Brightness and flickering effect

This week's blog post is about the visual effects used on the flare in the game and how it was created using the built in functionality in the SFML. Namely the scaling functionality for SFML's sprite. This method was used because it was convenient and time saving to use the inbuilt functionality instead of programming it by my self. It was also probably better because it has been developed by the SFML community.

the shrinking sprite
To create the shrinking effect of the light the sf::sprite.SetScale() function was used. SetScale() takes two float variables as arguments, x scale and y scale, which are basically multipliers for the width and height of the sprite. This means that if .5 is put in as argument for x the sprite's width will shrink to half it's size and if 2 is put in, the width will increase to the double of the original width. To shrink the sprite over time with this function a variable that represents how much burning time is left on the flare before it extinguishes, can be used. The flare's lifetime is defined by a variable and in the update function a variable increases with the time since the last time the update function was called. Using these two variables we can get the normalized value of how much of the burning process has taken place. This is done by dividing the currently burned time with the total possible amount of burning time. The value given by this division can not yet be used in our set scale because if we used this value the scale would begin at scale 0 and grow towards scale 1. What is wanted is the opposite of that, so to make it shrink instead of grow the inverted value of this is needed, which can be obtained by subtracting the value from 1. This will invert the value and what is left is the normalized value of how much time is left. This value can be put into the setScale() with the effect of shrinking the light sprite according to how much burning time is left.

the randomness
The decrease of light was only a part of the effect that was sought. It was also sought an effect that would make the flare look like it was flickering. To achieve this a random number generator was used. The random number generator was used because it has a potential to do things inconsistently and a flickering light is just that, a light with inconsistent brightness. The random number generator was set to generate a number between 0.9 times the normalized time left and the normalized time left. This gives the light a random brightness every frame between maximum brightness and a bit bellow the maximum brightness which resulted in the wanted flickering effect.

Thursday, 12 March 2015

Collision checking part 2 of 2

This is part two of the collision implementation of our game Ambient Pressure. See last blog post. This blog post is about the lagging issue that was solved by putting multiple Wall blocks into bigger blocks.


It was observed that there was a drop in performance. The reason for the drop was thought to be due to the collision handling code being too expensive (taking too much time for the computer to execute). We thought this was the case because such drop of performance had not been observed before the collision was implemented and the the extensive amount of collision checks we saw was required suggested this was the case.

The lines represents collision checks between the player the wall blocks.


After consulting our tutor he suggested a method to reduce the number of computations needed with the potential of reducing lag. The method was to minimize the amount of collider shapes created in the game by replacing the individual wall blocks with fewer but bigger collider rectangles spanning over the area of multiple adjacent wall blocks.

Following our tutors suggestion an rectangle finding algorithm was created for the map creating text file.

This is how our map look inside our text file:

1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,2,2,1,1,1
1,1,1,1,1,1,1,1,0,0,0,0,0,0,5,0,0,2,2,1,1,1
1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,0,2,1,1,1,1
1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,2,2,2,3,2,8,0
1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,2,2,2,2,2,8,0
1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,1,1,1
1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,6,0,0,1,1,1
1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,0,0,1,1,1
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,1,1
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,5,0,0,7,1,1,1
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,1,1
1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,0,0,1,1,1,1
1,1,1,1,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1
1,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1
1,4,0,0,0,0,0,0,0,5,0,0,1,1,0,0,0,9,0,0,1,1
1,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,1,1
1,1,1,1,1,1,0,0,0,0,0,0,1,1,0,0,0,0,0,0,1,1
1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,2,2,1,1,1,1
1,1,1,1,1,1,1,1,0,0,1,1,1,1,1,1,2,2,1,1,1,1
1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1
1,1,1,1,3,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1

The 1's represents walls, the 0's represents open space the other shown numbers represents other specific in-game objects like pickups, enemies, the player spawning point, etc.

The following algorithm was chosen for this purpose because it was practical to implement and was thought to give us the result aimed for.

The algorithm works by reading the text file as a two-dimensional map of integers. The algorithm Goes through all positions of the map one row at a time until it finds a 1 representing a wall block.

When a wall block is found the algorithm starts to define a collider rectangle. In this stage it is currently known that the collider rectangle at least has the width and height of one wall block.

After this the algorithm tries to find the biggest formable square shape, consisting of only wall blocks, downward and right from the first found wall block by looking at all of the adjacent coordinates both below and to the right of the biggest currently found square block. When ever there is a wall block adjacent to the square one block below or one block to the right of the collider square that is not a 1 the search for bigger square sizes ends.

When the biggest square is found the algorithm goes into a new state where it tries to find the biggest rectangle rightward. It follow the same principles as last state with the difference that it is now only checks rightward and that if anything other than a 1 is found in the first step it goes over to searching for a the biggest rectangle downwards instead.

When the biggest possible rectangle is found by this algorithm the algorithm creates a collider of that size.

Implementing this algorithm gave the result aimed for:


This might not be the most efficient way to optimize tiled based collision blocks. It was just a quick solution that optimized the collision checking significantly. It may also be compatible with other two-dimensional collision checking optimizations such as these quadtrees which could increase collision performance even more.


Thursday, 5 March 2015

Collision checking part 1 of 2

This weeks post will be about my attempts to implement collision in our game.What was aimed for this week was to implement a collision system between the player entity and the walls in the game. To achieve this, a method for checking if shapes are overlapping is needed and a method of adjusting back the position of the overlapped objects in the right direction so that they are no longer intersecting.

example of  drawn collision shapes


Fortunately for us our tutor had uploaded an example for how to handle collision between two types of shapes. A axis aligned bounding box and a bounding circle. Basically the shapes are defined by a center point and a size. The size of the circle is a number defining its radius and the size of the box is defined by numbers representing the width and height from the center.

The shapes chosen for collision was a circle for the diver controlled by the player and boxes for the walls. The circle was chosen for the diver firstly because if it instead would have been a axis aligned box around the player, the corners of that shape could have collided with the walls collision shape without them visually even being close to a collision, and secondly because the diver will be rotated so that the head points towards the direction the diver is swimming in and thus we want the collision to be the same in any given direction. We were thinking about having multiple circle colliders with a specified relative rotation and distance from the player's center placed on the most important parts of the diver's sprite to get a more realistic collision, but we are going with one bigger circle from the center because we think it will be easier to implement and less resource heavy for the computer.

Unfortunately I have been having trouble understanding the code examples and to come up with a way to implement it because it was a hard coded example with collision checking between only two shapes and not 100 blocks or so simultaneously that our maps contain. Until now I have only managed to draw the collision shapes out and not check the collision between them, but only doing that the game started to lag because there are so many new shapes created and drawn out every frame.

So for the next week I will try to implement the actual collision as well as some optimizations for collision checking so that things do no longer lag.

Thursday, 19 February 2015

Graphical layer implementation

This blog post is about the graphical layer implementation I did this week.
The implementation was done to gain better control over the order in which the sprites are drawn on the screen (which sprites that will appear on top of the others) and to simplify changing the view (the portion of the game world that is displayed on the screen) of certain sprites in the game but ignore other sprites like GUIs and HUDs.



In SFML there is a data type called RenderTexture, that works very similar to RenderWindow. The major difference between the two is that RenderWindow creates a graphical window presented on the computer screen whereas RenderTexture can not be viewed, only drawn as sprites on other surfaces.

I use RenderTextures as the data type for my layers. The layers are stored in a list, in the order they are supposed to be drawn out. All sprites are now associated with a layer variable, which is just an integer that the drawing function uses to determine which layer it should be drawn on.

When all sprites are drawn the view is changed on the specified layers by using the inbuilt functionality renderTexture.setView();



A thing to remember when using RenderTexture as a layer is that the RenderTexture.clear() function has by default to fill the screen with a non transparent black color. To make the layer transparent in the clear function you need to specify the clear color like this .clear(sf::color(0x00,0x00,0x00,0x00)) So that the alpha channel (the fourth value) is set to be fully transparent.

Since entities in this game can have more than one sprite I also implemented a sprite list in each entity. The sprite list consists of a std::vector containing std::pair where the first value of that pair is a pointer to a sprite and the second value of the pair is the layer specifying int for that sprite. To reach the sprite, for example when changing the position of it one needs to write like this inside the entity class: m_sprites[index].first->SetPosition(x,y);.

Implementing layers was a high risk move, since I did not know if it was going to work as I wanted and it required rewriting a lot of code in many classes before I could see a result. It took more time than I thought it would because of all the code needed to be rewriten to work with the layer code. I could have reached similar results without the layers by adjusting the position of all sprites according to the game view in the window before every time the sprites are drawn to the window. I choose the layer approach over the moving sprites approach because it made more sense for me to think in terms of layers and I thought it would generate code that was easier to understand and read. In terms of performance I have no idea what approach is more suitable. In the end I got the result I wanted.


Thursday, 12 February 2015

Tile maps

    Most of this week I have been trying to create a good and working parsing code for tile maps created with the tool tiled. I started trying to copy the code from my last project which was based on tiles. That did not work out that well, since that code was not made to work with the files that the program Tiled outputs and since I hadn't fully understood how file parsing works in C++ so my code just returned errors. I got frustrated and searched the web for an easy solution.
I found this project: https://github.com/bjorn/tiled/wiki/TMX-Map-Format

     It seemed like a good solution, and it probably was if only I could understand it, which I couldn't. So I spent a lot of time trying to figure what was needed to make it work, then I gave up to find something simpler. Then I found this: http://en.sfml-dev.org/forums/index.php?topic=3023.0

    It seemed simple at first glance since it was only two classes of code. I started removing parts that I saw as redundant, like the creation of sprites which I manage elsewhere in my code. I realized that this code was dependent on another libary called tinyxml, so I downloaded that and included it. Then I realized this code was made for an older version of SFML than what I am using for my project. So after combating this code and trying to translate things that I did not understand for hours I gave up, because I saw no end to it and I didn't think the program would run after all the changes that was needed to make it work with my code.

    So finally I got back to trying to do my own parser. I worked until I got an error that I did not manage to get around. Searched for a long time on forums for a solution until finally I gave up and decided to ask about it on a forum myself. I got an answer within half an hour that solved all my problems.

    If I had only asked for help on a forum in the first place I would have saved a tremendous amount of time this week that was spent feeling confused and angered about not finding a solution. At the same time I learned much about how to implement and understand code from others so that was nice.

Here is a picture showing the result off the walls (The black squares surronding the diver.) that was generated from a text file loaded by my parser.



Sunday, 14 December 2014

Bomberman Begins

Too be honest, I have really been slacking this week (and a big part of my classmates have too). Maybe it is because it is soon Christmas. Maybe it is tiredness from the game-jam we had, combined with the hobbit movie marathon and a lot of alumni guest lectures that took up our time. We have at least done something this week. We put together functionality for images other than Bitmap files for our games projects, which really just was to include another library called SDL_image that goes well with the rest of the SDL library, it was pretty much included the same way as SDL was. Also we learned how to output text onto the screen in our game windows which we did with, you've guessed it, yet another library not included in the original SDL. This was a bit more interesting though since we had to transform letters from a font into a picture to draw on the screen.

We have at least started on our final work for the course, which will be to create a clone of  a classical game, in which I chose Bomberman. Joakim Ädel joined my project, so hopefully we will get more done than we could individually. I have started working on automatic random map generation and put the pieces of our old projects together into a base to create the Bomberman clone from. I've also looked slightly into git, which really seems like a lovely software that I wished I had learned and used way earlier. Git is basically a program which you use to keep track of different version of a project. The power of git though compared to like Dropbox or any other version controlled storage is that you can have a "branch" for every new feature to implement in the code. This allows you to work on multiple features at the same time. For instance I might have a branch for trying to fix a specific bug and a branch for adding a new feature. Both of those two branches are connected to the latest official version of the project, When I get done with a branch, that is to say when I have succeeded to implement the bug fix or a new feature, I can make the finished branch into the "master", which basically means making the branch the official latest version of the project. When I get done with the other bug or feature branch I can then merge this new branch with the master. And it is here the magical witchcraft is going on, because here the bug fix and the new feature becomes one. The smart thing about git is that it only saves the changes of the content of files so it is possible for a file to be edited in two or more branches simultaneously, the only times there will be a conflict (if I have understood it right) is if the same parts in a file is being changed in both branches differently. Then that part must be manually merged, which in code means making that piece of code work for both branches. This also allows for many people to work on individual branches with individual problems. People can even work on the same problem but different parts of it (a branching in a branch). Or just compete for having the best solution to a problem. It is also easy to go back to an older version, and since every new version is described by the changes that was made to it,  it is easy to know what happened and when.

I am very excited to make my own game in C++ out of the things I know and the ideas I have. It is actually what I came here for, to be able to create games, to be able to create great interactive experiences for other people. So it shouldn't be hard for me to keep motivated. From now on I'll program and learn as much as I possibly can, because in the end, the amount of effort I put in now will determine how good the chances are for me to follow my dreams. And honestly, what I am doing right now is actually a part of that dream, and probably it is also a part of the dream that a lot of people in my class have. So there is really no reason for us to not give it all we can.


Saturday, 6 December 2014

Weak four (on my part)

This week has been purely about how to use the SDL library and about dividing things into appropriate classes as a foundation of games. That means we have one class for starting everything, one class for drawing everything on the screen, one class for handling states, etc... We learned to handle input from mouse and keyboard, we learned how to make a core loop of clearing screen drawing screen and presenting screen and lastly we larned about states, which was pretty confusing. We (a bunch of students) have planned to go through the code we have got on the lectures and try to explain it line by line for each others until we get it.

I like this week because it has been the first to introduce me to mostly, for me, new material.

This week has been extra slow because of some complementary work in other courses and because of the preparations for the game jam on the weekend.

I could probably have done better somehow.

Next week I'll try to analyse my time better so that I know how to improve.

Now GAME JAM!

Friday, 28 November 2014

BlogPosts[2] = new blogPost(3);

Hopefully this works now. Apparently my blog posts wasn't showing up for some reason. I guess I just missed the right instructions or something. Anyways, this has been the result of week three:

It has been way too much and way too little at the same time. As I have programmed before the exercises seemed like a repetition of what I have already learned, but with some small changes in syntax, which was really annoying to find out about because there were not any good error messages when they arose. So one fourth of the week has been trying to figure out minimal syntax errors and programming language differences, C++ is on top of the "most pedantic languages I have tested" list , the second fourth have been figuring out solutions to programming problems and the last half has been spent helping others in the class with understanding object oriented programming and how to use objects and why they should be used. Everyone I helped seemed to understand what classes and objects are good for and what differs them, but somehow they did things wrong. An example was the exercise where we were to create a card and a deck class, and the to store the "cards" in a "deck". People usually stored all the cards in the card class as if the card class was a holder for all the cards and then they didn't see the purpose of the deck class.  Maybe it was the high tempo, maybe the instructions wasn't clear enough in the exercise document, I don't know. I feel bad though for the ones that has not programmed before or have forgotten object oriented programming if it was long ago because this week was probably heavy for them with a lot of new information in a short period of time.

Here is an example of how I did the card class, just to show of or something:
(The const string arrays are declared with static in the header file to not take up the memory for suits and ranks more than once)

I actually skipped the majority of the seminars this week, partly because I have been feeling a sick and exhausted and partly because the only valuable information for me was the syntax differences, I have been working with objects so many times before so just listening to how it all works was kind of painful. (I checked the files uploaded on the portal and asked friends if I missed anything though so I guess it's cool.)

To make better progress I could probably have focused more on my own progress instead of my friends, though as I have said before in this blog, I think there is big value in helping others because you learn in the process of  explaining things as simple as you can.

I have a bunch of exercises from this week left unfinished. I'll try to finish them off before next week. It will probably not be too hard, I just need to clear my mind.

Friday, 21 November 2014

It has been a Pong week..

This week has been fully packed with programming! We have gone through pointers and functions and a lot of fun stuff and by the end of the week we learned how to make pong with the sdl library! I am still a bit lost in the SDL library because I am new to it, but I am pretty used to the structure of the code, because I have done similar things in flash. This was a bit messy thought because we have not gone through classes yet, so we had to make the entire game in one class, which means all code in one big text file. Not a good idea if you want to keep track of things...
After the first Pong lesson where we only made one paddle work, I made both the paddles work and then I started making the rest of the game in the way I thought of, because I got really inspired to make games. It worked fine and I think it was really good practice for what would come. It was problematic though when I got back to class the day after and we were to continue, because I had already written the code in my way, and it was a lot different and also less professional than the code solutions our teacher showed us. So I ended up trying to follow his steps on the screen while at the same time changing my own code so it wouldn't interfere with the new code. In the end the result was really great and it made me really happy. Next time I am going to save different versions so that I don't get carried away and then need delete code instead of just opening an older project version. I feel really inspired to do more games in SDL, but right now I need to focus on finishing the training assignment.

Saturday, 15 November 2014

Programming week one!

Hi there blog!

Long time no see. I have been busy doing stuff so nothing has been happening here lately.
This week the programming course started! I love programming so for me to just be able to focus all my time on stuff that I love a lot is amazing.

Since I have been programming a bunch before there wasn't really anything new to me concept wise. The thing that differs in how programming languages are operated is usually just syntax, which often is not very different from most other programming languages. So I have been taking it easy this week. People from the class have been hanging out at school after the lectures to practice programming. I have been there, helping those who needs helps and exchanging knowledge with those being around the same programming level as me. I have been testing some grid based programming and tried to make a snake game in the console.

We got a list of non mandatory programming exercises which I could finish pretty fast. I think it really helped that I had been practicing programming not so long ago so I had it fresh in my mind. I finished the codeacademy.com python course a while before this course started and I finished a summer course this summer, also in python, called "sommarprogrammering" which translates directly to summer programming. Also this summer I have been looking at some videos, which really is a great way to learn, where a guy talks about how to program in c++ from the basics and up.


Process of unfinished snake game:
So I made a two dimensional int array, which I at best can explain to you non programmers as a list filled with lists that are filled with numbers, where every number represents something. 0 means empty space, 1 means snake part, 2 means apple. I had an infinite loop*, as timer and a sleep function for delay, making the program wait to not make the game go too fast. Then I found this function online that returned true only if a keyboard key with a certain keycode was pressed. I figured out the key codes for the arrow keys then I made the program just check in every clock tick if any of the arrow keys were pressed. If any of them returned true I changed the direction of the snake head in the direction of the arrow. The difference is just if the snake head's x and y coordinates goes +1 or -1 every clock tick. Then I just took the coordinates of the snake head and turned that coordinate into the two dimensional array, by using y as list list index number and x as number list index number, and then changing the value in that location 1 if it was 0 before. Every tick I had another loop that cleared the screen form text and then recreated the two dimensional array into a new text that the program then pushes out on the console screen. I realized that the console wasn't that fast too update the text on the screen, so the game was doomed to lag and be slow. I stopped developing that project then because that killed my motivation.

* An infinite loop is basically a list of instructions that the program goes through one by one until the end of the list where the program goes back to the first instruction and this repeats forever until the program stops or reaches a message that tells it to stop.


What could I have done better this week?
I could have asked for harder programming tasks earlier to make the most of my time. I have also been thinking about creating and posting videos about programming, because I have heard that one of the best way to remember things is to teach it to others. I believe there is this quote by Einstein that "if you can't explain it simply to others you don't know it good enough" or something like that. I really believe that is true, even if I can do these tasks and know how to do them, if I can't do them easily in a simple way then it will take too much energy for me too do things that are harder than these things. Also teaching out to others motivates you to check everything so you don't give out false or vague information.

This weekend I am going to look into SDL, which is a programming library we will be using, and try to do something graphical and maybe record a video of it to help others in my class. I'll also look into a lot programming and c++ terms on Wikipedia to get a deeper view of what the things are and why they exist. Next week I'll continue to go on the lectures so there isn't anything fundamental in c++ programming that I miss and so that I can ask questions if they would arise.