Skip to content

Commit

Permalink
refactor: change renderWindow from pointer to reference in anim and i…
Browse files Browse the repository at this point in the history
…ntro classes

- Updated `Anim` and `intro` classes to use `sf::RenderWindow&` instead of `sf::RenderWindow*`.
- Modified constructor and method implementations to handle references.
- Adjusted usage in `snakeGame` class.
  • Loading branch information
zEuS0390 committed Sep 5, 2024
1 parent faeee21 commit 4fbcbb4
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 44 deletions.
4 changes: 2 additions & 2 deletions include/anim.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ class Anim {
unsigned int total;
float pitch;
bool scoreOnce;
Anim (RenderWindow*, unsigned int);
Anim (RenderWindow&, unsigned int);
void explodeFood (bool&, Color, Vector2f, const float&);
void scoreAnim (SoundManager&, Clock&, Color, unsigned int, Vector2f);
private:
sf::RenderWindow* window;
sf::RenderWindow& window;
sf::Color color;
// explodeFood
sf::RectangleShape shape1;
Expand Down
38 changes: 19 additions & 19 deletions include/intro.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@
class intro
{
public:
float addVal;
float alpha;
bool fadeIn;
bool fadeOut;
bool isSFML;
bool isCreator;
bool isRunning;
explicit intro (sf::RenderWindow*);
void init ();
void updates ();
void events ();
void renders ();
void loop ();
float addVal;
float alpha;
bool fadeIn;
bool fadeOut;
bool isSFML;
bool isCreator;
bool isRunning;
explicit intro (sf::RenderWindow&);
void init ();
void updates ();
void events ();
void renders ();
void loop ();
private:
sf::Clock clock;
sf::RenderWindow* renderWin;
sf::Image sfmlLogo;
sf::Image creatorLogo;
sf::Texture texture;
sf::Sprite sprite;
sf::Clock clock;
sf::RenderWindow& renderWin;
sf::Image sfmlLogo;
sf::Image creatorLogo;
sf::Texture texture;
sf::Sprite sprite;
};

#endif
Expand Down
28 changes: 14 additions & 14 deletions src/anim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ using sf::Vector2f;
using sf::Color;
using sf::Clock;

Anim::Anim (RenderWindow* window, unsigned int scale)
Anim::Anim (RenderWindow& window, unsigned int scale)
:
scale(scale),
total(0),
Expand Down Expand Up @@ -54,32 +54,32 @@ void Anim::explodeFood (bool& isActive, Color color, Vector2f foodPos, const flo
{
if (time >= 0.05f && time < 0.5f)
{
window->draw(shape1);
window->draw(shape2);
window->draw(shape3);
window->draw(shape4);
window.draw(shape1);
window.draw(shape2);
window.draw(shape3);
window.draw(shape4);
}
if (time >= 0.5f && time < 1.0f)
{
shape1.setPosition(Vector2f(shape1.getPosition().x + scale, shape1.getPosition().y));
shape2.setPosition(Vector2f(shape2.getPosition().x - scale, shape2.getPosition().y));
shape3.setPosition(Vector2f(shape3.getPosition().x, shape3.getPosition().y + scale));
shape4.setPosition(Vector2f(shape4.getPosition().x, shape4.getPosition().y - scale));
window->draw(shape1);
window->draw(shape2);
window->draw(shape3);
window->draw(shape4);
window.draw(shape1);
window.draw(shape2);
window.draw(shape3);
window.draw(shape4);
}
if (time >= 1.0f && time < 1.5f)
{
shape1.setPosition(Vector2f(shape1.getPosition().x + 2 * scale, shape1.getPosition().y));
shape2.setPosition(Vector2f(shape2.getPosition().x - 2 * scale, shape2.getPosition().y));
shape3.setPosition(Vector2f(shape3.getPosition().x, shape3.getPosition().y + 2 * scale));
shape4.setPosition(Vector2f(shape4.getPosition().x, shape4.getPosition().y - 2 * scale));
window->draw(shape1);
window->draw(shape2);
window->draw(shape3);
window->draw(shape4);
window.draw(shape1);
window.draw(shape2);
window.draw(shape3);
window.draw(shape4);
}
if(time >= 1.5f)
isActive = false;
Expand Down Expand Up @@ -109,7 +109,7 @@ void Anim::scoreAnim (SoundManager& soundManager, Clock& clock, Color color, uns
text.setFillColor(color);
text.setString(intToStr(total));
text.setPosition(position);
window->draw(text);
window.draw(text);
}
}

14 changes: 7 additions & 7 deletions src/intro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ using sf::Color;
using sf::Event;
using sf::Keyboard;

intro::intro (RenderWindow* renderWin)
intro::intro (RenderWindow& renderWin)
: addVal(0.9f),
alpha(0),
fadeIn(true),
Expand All @@ -17,7 +17,7 @@ intro::intro (RenderWindow* renderWin)
isRunning(true),
renderWin(renderWin)
{
renderWin->setMouseCursorVisible(false);
renderWin.setMouseCursorVisible(false);
}

void intro::init ()
Expand All @@ -28,7 +28,7 @@ void intro::init ()
if (!creatorLogo.loadFromFile("gfx/Creator_Logo.png"))
return;

renderWin->setFramerateLimit(250);
renderWin.setFramerateLimit(250);
texture.loadFromImage(sfmlLogo);
sprite.setTexture(texture);
sprite.setColor(Color(255, 255, 255, alpha));
Expand Down Expand Up @@ -101,7 +101,7 @@ void intro::updates ()
void intro::events ()
{
Event event;
while (renderWin->pollEvent(event))
while (renderWin.pollEvent(event))
{
if (event.type == Event::KeyPressed)
{
Expand All @@ -113,9 +113,9 @@ void intro::events ()

void intro::renders ()
{
renderWin->clear();
renderWin->draw(sprite);
renderWin->display();
renderWin.clear();
renderWin.draw(sprite);
renderWin.display();
}

void intro::loop ()
Expand Down
4 changes: 2 additions & 2 deletions src/snakeGame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ SnakeGame::SnakeGame (const unsigned int winX,
winGrid (winX, winY),
foodObj (Vector2f(winGrid.scale, winGrid.scale), winGrid.scale),
window (VideoMode(winX, winY), windowTitle),
introduction (&window),
anim (&window, winGrid.scale),
introduction (window),
anim (window, winGrid.scale),
interface (&window)
{
font.loadFromFile(FONT_FILENAME);
Expand Down

0 comments on commit 4fbcbb4

Please sign in to comment.