I got a code of snake in c++ sfml.. I got 2 objects here Snake and Food, also I have a main()
that contains sfml setup for windows. I would simply make 3 objects that are called from main but it results in circular references and can't compile.
What I want to have is (or something close to the idea below).
int main
{
Food food(window);
Snake snake(window, food);
Window window(snake, food);
window.start();
}
snake gas method draw that needs reference to its window (for drawing as drawing needs window handle), and update whuch also needs window (for window size)
food also needs those for close reason (need draw and window suze)
window in turn has its window loop from where it need to calls those updates and draws of snake and food
//edit
someone asked for definitions but definitions are strightforward
class Snake;
class Food;
class Window
{
public:
Window::Window(Snake& s, Food& f): snake(s), food(f)
{
}
private:
Snake& snake;
Food& food;
}
class Snake
{
public:
Snake::Snake(Window& w, Food& f): window(w), food(f)
{
}
private:
Window& window;
Food& food;
}
class Food
{
public:
Snake::Snake(Window& w): window(w)
{
}
private:
Window& window;
}