Skip to content

Commit faf700d

Browse files
committed
Added documentation
1 parent 255529f commit faf700d

File tree

9 files changed

+144
-4
lines changed

9 files changed

+144
-4
lines changed

Collideable.hpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* Snake game program using the SDL library
3+
*
4+
* @author J. Alvarez
5+
*/
16
#include "SDL2/SDL.h"
27
#include "Screen.hpp"
38
#include "Drawable.hpp"
@@ -13,11 +18,21 @@ namespace SnakeGame {
1318
* are in the same position a collision is assumed
1419
*/
1520
struct Collideable: Drawable {
21+
/**
22+
* Creates an Collideable instance with a position
23+
* @param x X position of this object
24+
* @param y Y position of this object
25+
*/
1626
Collideable(int x, int y);
1727

28+
/**
29+
* Validates whether there is a collision between this object and another
30+
* @param other The other object to check whether collision occurs
31+
* @return boolean indicating whether collision occurs
32+
*/
1833
bool collidesWith(Collideable & other);
1934
};
2035

2136
} // namespace SnakeGame
2237

23-
#endif // COLLIDEABLE_HPP
38+
#endif // COLLIDEABLE_HPP

Drawable.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* Snake game program using the SDL library
3+
*
4+
* @author J. Alvarez
5+
*/
16
#include "SDL2/SDL.h"
27
#include "Screen.hpp"
38

@@ -13,8 +18,17 @@ struct Drawable {
1318
int m_x;
1419
int m_y;
1520

21+
/**
22+
* Initialices Drawable object with a position
23+
* @param x X position of this object
24+
* @param y Y position of this object
25+
*/
1626
Drawable(int x, int y): m_x(x), m_y(y) {}
1727

28+
/**
29+
* Draws on screen this object
30+
* @screen Reference to screen to draw on
31+
*/
1832
virtual void draw(Screen & screen) = 0;
1933
};
2034

Food.hpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* Snake game program using the SDL library
3+
*
4+
* @author J. Alvarez
5+
*/
16
#include "Collideable.hpp"
27
#include "Screen.hpp"
38
#include "SDL2/SDL.h"
@@ -19,11 +24,17 @@ struct Food: Collideable {
1924
static const unsigned int S_FOOD_WIDTH;
2025
static const int S_VALUE;
2126

27+
/**
28+
* Default constructor for Food instances
29+
*/
2230
Food();
2331

32+
/**
33+
* @see Drawable#draw
34+
*/
2435
void draw(Screen & screen);
2536
};
2637

2738
} // namespace SnakeGame
2839

29-
#endif // FOOD_HPP
40+
#endif // FOOD_HPP

Screen.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* Snake game program using the SDL library
3+
*
4+
* @author J. Alvarez
5+
*/
16
#include "Screen.hpp"
27
#include "SDL2/SDL.h"
38
#include <iostream>

Screen.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* Snake game program using the SDL library
3+
*
4+
* @author J. Alvarez
5+
*/
16
#include "SDL2/SDL.h"
27
#include "SDL2/SDL_ttf.h"
38
#include <string>

Section.hpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*
2+
* Snake game program using the SDL library
3+
*
4+
* @author J. Alvarez
5+
*/
16
#include "Collideable.hpp"
27
#include "Screen.hpp"
38
#include "SDL2/SDL.h"
@@ -14,11 +19,37 @@ namespace SnakeGame {
1419
struct Section: Collideable {
1520
static const unsigned int S_SECTION_WIDTH;
1621

22+
/**
23+
* Default constructor for creating Section instances
24+
*/
1725
Section();
26+
27+
/**
28+
* @see Collideable#Collideable(int, int)
29+
*/
1830
Section(int x, int y);
31+
32+
/**
33+
* @see Drawable#draw
34+
*/
1935
void draw(Screen & screen);
36+
37+
/**
38+
* Moves the section to another position according to a direction
39+
* @param direction The direction to move this section to
40+
*/
2041
void move(int direction);
42+
43+
/**
44+
* Returns the direction value from this Section to another
45+
* @other The other section to calculate direction
46+
* @return integer indicating direction from Snake::Direction
47+
*/
2148
int calculateDirection(Section & other);
49+
50+
/**
51+
* Show information about this instance on screen
52+
*/
2253
void toString(); // TODO Remove
2354
};
2455

Snake.hpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,73 @@ struct Snake {
3030

3131
std::vector<Section *> m_sections;
3232

33+
/**
34+
* Default constructor of Snake to create instances
35+
*/
3336
Snake();
37+
38+
/**
39+
* Destructor for Snake that cleans up sections
40+
*/
3441
~Snake();
3542

43+
/**
44+
* @see Drawable#draw
45+
*/
3646
void draw(Screen & screen);
47+
48+
/**
49+
* Updates the direction of the snake
50+
* @direction The direction of the snake according to Snake::Direction
51+
*/
3752
void updateDirection(int direction);
53+
54+
/**
55+
* Moves the snake, which implies moving all its sections
56+
* @return true if movement is within Screen's area, false otherwise
57+
*/
3858
bool move();
59+
60+
/**
61+
* Removes one life from the snake
62+
*/
3963
void die();
64+
65+
/**
66+
* Resets snake to its initial state (except for lives)
67+
*/
4068
void reset();
69+
70+
/**
71+
* Checks whether the snake (it's head section) collides with another object
72+
*/
4173
bool collidesWith(Collideable & object);
74+
75+
/**
76+
* Adds a new section to the end of the snake
77+
*/
4278
void addSection();
79+
80+
/**
81+
* Show information about this instance on screen
82+
*/
4383
void toString(); // TODO Remove . For debugging purposes
4484

4585
private:
86+
/**
87+
* Frees memory from snake's sections
88+
*/
4689
void freeSections();
90+
91+
/**
92+
* Resets the snake's sections to the initial state (initial number and
93+
* position)
94+
*/
4795
void resetSections();
96+
97+
/**
98+
* Resets the snake's direction to the initial direction
99+
*/
48100
void resetDirection();
49101
};
50102

Wall.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@ struct Wall: Collideable {
1818

1919
static const unsigned int S_WALL_WIDTH;
2020

21+
/**
22+
* @see Collideable#Collideable(int, int)
23+
*/
2124
Wall(int x, int y);
2225

26+
/**
27+
* @see Drawable#draw
28+
*/
2329
void draw(Screen & screen);
2430
};
2531

2632
} // namespace SnakeGame
2733

28-
#endif // WALL_HPP
34+
#endif // WALL_HPP

main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
2-
*
2+
* Snake game program using the SDL library
3+
*
34
* @author J. Alvarez
45
*/
56
#include <iostream>

0 commit comments

Comments
 (0)