BlogWeek 3, The Menu
|
This was an easy one, although somewhat hard compared to our programming 1 course where we got most of our structure beforehand. Up until now we had hardcoded everything in main.cpp because of the amount of prototyping and because taht we needed to be done in time for the alpha presentation. This we regretted. The amount of time it took me and my coding-coworker to seperate the “gamestate” in a separate h and cpp-folder and how efficient I feel I became afterwards, not seperating the code in the beginning was in hindsight poor planning. After sorting the code i preceded to create a menu.h file to house all visual information. Sprites and pointers to said sprites was created for all the parts of the menu, for the individual texts aswell background motive. I created pointers to sf::View, sf::vectorf2, renderwindow and to the mouse. The view of the game needed to scale to match the size of the window/screen in order for the project to work properly in any dimensions. Vectior2f was created in order to get the position of the mouse coordinates and the mouse was created in the h file for it to work and to keep a sense of clean code. In order to scale the window two floats was created for the x and y axel in order to house the correct coordinates for the mouse to work properly in the scaled window. This is achieved by dividing the window’s x and y size with two constant ints with the X=1920 times Y=1280 preset. The two constant ints was created to house the prefered resolution of X=1920 times Y=1280 and was put in the view funktion from the sfml library. This way it would later scale as if it was in 1920X1280 but in the windowsize generated by either the display-size or the preset window size. To make sure that everything was centerd (allways) the constant ints was put in the view funktion called setCenter. Within the brackets the preset resolution of X and Y was halfed creating a fixed center of the screen. This would make sure that all the sprites would be drawn relative to the center of the screen. I created The difficult part of all this was how to scale the sprites to match the window. This our head coder decided would be a good implementation since that we wouldent know the screensize of the computer in which our game would be played. This made it more difficult for me but with the guidence of our head coder I/we acheaved our goal. The sfml library made it easy aswell. |
all the sprites and drew them in the right order so that everone would be visible. I gave them a position in the window in a menu like fashion and used the same reticle sprite for navigation in the menu as the one we use in our gamestate. Inside the Update funktion I made sure that the reticle was updated to be where the mouse were
inside the window. In the reticle update the scale method is used in order to set the sprite at the mouse exact position. This is achieved by giving the reticle the x coordinate of the mouse times the scale funktion for the x point and vice versa. Then all I had to do was to check if the different sprites, leading to different locations
in the game, contained the reticle sprite. If any of the sprites contained the courser it would change its color to red. Inside the if-statement I also check’d if the left mouse button was pressed. If all the recuirements was met it would return false which would lead in to either in to the gamestate or to a comand that closes down the window.