Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - RetroRain

Pages: [1]
1
The point I was trying to make is that whenever any render target is changed, it should be cleared, drawn and displayed. This is the same for both your window and your render texture. When I said "display" the render texture, I mean using the display() method, not showing it on the window.
Remember, if you clear a render target (render texture or window, for example), it should also be displayed after you've finished drawing to it. And, you should always clear it first after displaying it is the last thing that happened. clear/draw/display

You can draw the "render sprite" (the sprite used to draw the render texture to the window) window every frame but the texture itself needs to treated with the clear/draw/display cycle every time (you change it).

Yes, I know.  I always clear, draw, and display.  I have a whole function for drawing my levels that takes care of it.  I clear the texture, draw to it, display it, and then assign it to a sprite for drawing to the window on every frame.  I only draw the level when I need to, so I have a flag that is set when the texture is drawn.

If you've already used them (for points, I presume), moving on to using larger primitives should be comfortable enough for you. Instead of one vertex per output (as in point primitive), it would be multiple vertices per output (3 for triangle primitive and 4 for quads). The strips/fans can be a little more complicated at first but can reduce the number of required once you understand them.

Yes, I'll look into them when I need them.  What is interesting, is that after my recent modifications to my game (having it the way that it was in the first post), there is no longer any lagging going on.  But that could also be due to my code doing more since then.  I'm not sure, but my game works fine now with the clock timing.

Thank you for your help Hapax.

2
Hapax, sorry for the late reply.  I have been away from programming the past several days, and I didn't expect anymore replies to this topic.

If I understand what you are trying to tell me, is that the window clearing, drawing, and displaying should be happening every frame (every loop cycle of the while loop), but the actual game logic can be handled however I need it to be handled?

I'm going to try some modifcations to my code, as I want optimal performance.

Yes, I know about the RenderTexture class.  I've been using it to draw my levels.  I have found out in the past that having multiple calls to the window drawing can slow down the speed of the program.  But drawing to a texture first, and then simply drawing that texture to the window makes things faster.

I will definitely look into the vertex arrays (I've used SFML's vertex arrays to draw stars in a space fight game that I attempted).

Thanks for the info.

3
UPDATE - 6/9/20: SOLVED!

The lagging is a result of the timing mechanism.

It has nothing to do with the nested loops.  The nested loops happened to work, because the snake was being drawn right after the nested loop, so the timing was in-sync.

This is the original code:
   if (timer>delay) {timer=0; Tick();}

   ////// draw  ///////
    window.clear();

      for (int i=0; i<N; i++)
         for (int j=0; j<M; j++)
           { sprite1.setPosition(i*size, j*size);  window.draw(sprite1); }

    for (int i=0;i<num;i++)
        { sprite2.setPosition(s[i].x*size, s[i].y*size);  window.draw(sprite2); }

    window.display();

The drawing code is OUTSIDE of the timer.  The reason for the lag, is because the drawing is happening every frame, but the logic for the snake movement is not happening every frame.  It is happening when the timer is greater than the delay, as shown above.

However, if I go like this:

   if (timer>delay)
   {
     timer=0;
     Tick();

   ////// draw  ///////
    window.clear();

      for (int i=0; i<N; i++)
         for (int j=0; j<M; j++)
           { sprite1.setPosition(i*size, j*size);  window.draw(sprite1); }

    for (int i=0;i<num;i++)
        { sprite2.setPosition(s[i].x*size, s[i].y*size);  window.draw(sprite2); }

    window.display();
    }

Now the drawing code is in-sync with the timer mechanism, hence no more lagging.

I can't believe it didn't see this initially, but I just figured it out now, as I am currently reprogramming my snake game from scratch.

So the answer to my question has now been solved.

The nested loop that draws the background gives the program the time it needs to draw the snake, so there is no lag.

Otherwise, once the timing mechanism is introduced, everything should be in-sync with the timer.

Hapax, thank you again for your help.

You gave me that detail of the nested loop, which in turn led me in the right direction to figuring out why this annoying lag was happening.

You guys here are awesome.  You've always been helpful.

I only hope that if anyone out there is having the same problem that I had, and they don't understand WHY it is happening, they will be able to stumble upon this thread, to understand why it is happening.

Thank you. :)

4
I think I get what you are trying to tell me.

There definitely is a flaw in the code logic here.  One that I was just blind to.

This line, by itself, is flawed logic:

for (int i=0;i<num;i++)
{ sprite2.setPosition(s[i].x*size, s[i].y*size);  window.draw(sprite2); }
 

But when I used this nested loop...

for (int i=0; i<N; i++)
    for (int j=0; j<M; j++)
        for (int i=0;i<num;i++)
            { sprite2.setPosition(s[i].x*size, s[i].y*size);  window.draw(sprite2); }

there was no lag.

I tried all sorts of various combinations of commenting out single lines, the whole blocks, and removing/adding braces, etc.

I'm still a little bit confused by all of this, but I feel like I have a better idea of why this is happening.

It definitely has to do with the nested loop code logic.

Thank you Hapax for seeing something that I was just blind to.

That is why I posted it on here to begin with.  Something little like this was driving me nuts.

I appreciate it.

5
I've been trying to recreate the game, called Snake, in C++, using the SFML library.

I was programming it my way, with certain code aspects taken from this video:



I was having a lagging problem.  The snake wouldn't move downwards smoothly without lagging.  And I couldn't figure out why.

So I was forced to strip my code down similar to the code in the video.

Even after modifying every line, and testing, I kept getting the lagging.

Even when the code was exactly the same as the one in the video, I kept getting the lagging (I omitted the fruit code and my aesthetics were different, but the code was the same).

After a few hours of testing, I finally traced where the lagging is coming from.

This is what is bizarre.

Why would NOT drawing something, be causing a lag?

If you omit (or comment out) this block of code, or any of the invididual lines here, lagging occurs.  This code is meant to draw the background tiles.

    for (int i=0; i<N; i++)
      for (int j=0; j<M; j++)
        { sprite1.setPosition(i*size, j*size);  window.draw(sprite1); }
 

The source code and folder with images is downloadable at the YouTube video link I posted above.  But you can simply use any 16x16 pixel square to represent the snake (sprite2) and background (sprite1).

Here is the code.  It's actually not that long at all, and the code block above appears right after window.clear().

#include <SFML/Graphics.hpp>
#include <time.h>
using namespace sf;

int N=30,M=20;
int size=16;
int w = size*N;
int h = size*M;

int dir,num=4;

struct Snake
{ int x,y;}  s[100];


void Tick()
 {
    for (int i=num;i>0;--i)
     {s[i].x=s[i-1].x; s[i].y=s[i-1].y;}

    if (dir==0) s[0].y+=1;
    if (dir==1) s[0].x-=1;
    if (dir==2) s[0].x+=1;
    if (dir==3) s[0].y-=1;
 }

int main()
{
    srand(time(0));

    RenderWindow window(VideoMode(w, h), "Snake Game!");

    Texture t1,t2;
    t1.loadFromFile("images/white.png");
    t2.loadFromFile("images/red.png");

    Sprite sprite1(t1);
    Sprite sprite2(t2);

    Clock clock;
    float time = 0;
    float timer=0, delay=0.1;

    while (window.isOpen())
    {
        time = clock.getElapsedTime().asSeconds();
        clock.restart();
        timer+=time;

        Event e;
        while (window.pollEvent(e))
        {
            if (e.type == Event::Closed)
                window.close();
        }

        if (Keyboard::isKeyPressed(Keyboard::Left)) dir=1;
        if (Keyboard::isKeyPressed(Keyboard::Right)) dir=2;
        if (Keyboard::isKeyPressed(Keyboard::Up)) dir=3;
        if (Keyboard::isKeyPressed(Keyboard::Down)) dir=0;

        if (timer>delay) {timer=0; Tick();}

   ////// draw  ///////
    window.clear();

      for (int i=0; i<N; i++)                       //IF YOU COMMENT OUT THIS LINE
         for (int j=0; j<M; j++)                   //OR THIS LINE
           { sprite1.setPosition(i*size, j*size);  window.draw(sprite1); }     //OR THIS LINE, LAGS OCCUR.

    //BUT IF YOU KEEP THIS BLOCK IN, THE LAGS DON'T OCCUR.  THAT DOESN'T MAKE ANY SENSE TO ME.

    for (int i=0;i<num;i++)
        { sprite2.setPosition(s[i].x*size, s[i].y*size);  window.draw(sprite2); }

    window.display();
    }

    return 0;
}
 

I'm simply trying to understand why NOT drawing something, is causing a problem?  But if the code is left in there, there is no problem at all.

In other words, if you decide to not draw the background tiles, a lagging problem will occur with the snake.  The snake will freeze and move irregulary.  But if the background tiles are drawn, the snake moves smoothly and perfectly along.

The only thing that comes to mind, that would even cause a potential problem, is the code that deals with the clock and timing.  But it still doesn't make any sense to me.

By omitting the background tile drawing, this should not impact the movement of the snake in the slightest.

Does anyone know what exactly is going on here?

The only reason I am posting this topic, is because I do find it very bizarre that leaving out a code that is meant to draw something is very weird.  What if I didn't want a background?  NOT drawing something shouldn't be causing a problem.

Thank you for taking the time to look at this.

6
I tried it this way, having a flag be checked if colliding with a block, and then moving based on that flag.

For whatever reason, and I don't know why, the for loop only works for one block.  One you can't collide with, and the other you can go right through.

If I change the subscript of the block to 0, it works for 0.  If I change it to 1, it works for 1.  But not both.  I just don't get it.

#include <SFML/Graphics.hpp>
using namespace sf;

int main()
{
    RectangleShape playerRect;
    RectangleShape blockRect[2];

    bool canMoveUp = true;
    bool canMoveDown = true;
    bool canMoveLeft = true;
    bool canMoveRight = true;

    float playerTop, playerBottom, playerLeft, playerRight;
    float blockTop[2], blockBottom[2], blockLeft[2], blockRight[2];

    float playerX = 128;
    float playerY = 200;

    float blockX[2] = {200, 128};
    float blockY[2] = {120, 60};

    playerRect.setSize(Vector2f(16,16));
    playerRect.setFillColor(Color::Yellow);
    playerRect.setPosition(playerX,playerY);

    blockRect[0].setSize(Vector2f(16,16));
    blockRect[0].setFillColor(Color::Blue);
    blockRect[0].setPosition(blockX[0],blockY[0]);

    blockRect[1].setSize(Vector2f(16,16));
    blockRect[1].setFillColor(Color::Blue);
    blockRect[1].setPosition(blockX[1],blockY[1]);

    RenderWindow window(VideoMode(256,240),"");
    window.setSize(Vector2u(512,480));

    while (window.isOpen())
    {
        // Process events
        Event event;
        while (window.pollEvent(event))
        {
            // Close window: exit
            if (event.type == Event::Closed)
                window.close();
        }

        playerTop = playerY;
        playerBottom = playerY + 16;
        playerLeft = playerX;
        playerRight = playerX + 16;

        for (int i = 0; i < 2; i++)
        {

            blockTop[i] = blockY[i];
            blockBottom[i] = blockY[i] + 16;
            blockLeft[i] = blockX[i];
            blockRight[i] = blockX[i] + 16;

            //player collides with bottom of block
            if (playerTop - 0.01 <= blockBottom[i]
             && playerBottom > blockTop[i]
             && playerLeft < blockRight[i]
             && playerRight > blockLeft[i]) canMoveUp = false; else canMoveUp = true;

            //player collides with top of block
            if (playerBottom + 0.01 >= blockTop[i]
             && playerTop < blockBottom[i]
             && playerLeft < blockRight[i]
             && playerRight > blockLeft[i]) canMoveDown = false; else canMoveDown = true;

            //player collides with right side of block
            if (playerLeft - 0.01 <= blockRight[i]
             && playerRight > blockLeft[i]
             && playerTop < blockBottom[i]
             && playerBottom > blockTop[i]) canMoveLeft = false; else canMoveLeft = true;

            //player collides with left side of block
            if (playerRight + 0.01 >= blockLeft[i]
             && playerLeft < blockRight[i]
             && playerTop < blockBottom[i]
             && playerBottom > blockTop[i]) canMoveRight = false; else canMoveRight = true;
        }

        if (Keyboard::isKeyPressed(Keyboard::Up))
        {
            if (canMoveUp) playerY -= 0.01;
        }
        else if (Keyboard::isKeyPressed(Keyboard::Down))
        {
            if (canMoveDown) playerY += 0.01;
        }
        else if (Keyboard::isKeyPressed(Keyboard::Left))
        {
            if (canMoveLeft) playerX -= 0.01;
        }
        else if (Keyboard::isKeyPressed(Keyboard::Right))
        {
            if (canMoveRight) playerX += 0.01;
        }

        if (Keyboard::isKeyPressed(Keyboard::Escape)) window.close();

        playerRect.setPosition(playerX,playerY);

        // Clear screen
        window.clear();

        // Draw the sprite
        window.draw(playerRect);
        window.draw(blockRect[0]);
        window.draw(blockRect[1]);

        // Update the window
        window.display();
    }

    return 0;
}

7
Thank you.  But before I take the time to read that article and understand everything in it, I managed to get my collision detection to work, without using the built-in SFML collision functions.

I simply did the math for the bounding box sides, and the way I'm doing the collision, is simply checking BEFORE the player comes into contact with the box.  If the player is GOING TO COLLIDE, then don't move.

And it works.

Except there is one small problem...

As you can see in the code below (in the keyboard section), I'm simply checking for a collision with one block.

If I use a loop to cycle through all of the block rectangles, the player will move too fast, and then be able to go through the blocks (a little bit slower, but it goes through them).

You can see it where I commented the loop code out in the keyboard section.

The blockTop[0], blockBottom[0], blockLeft[0], blockRight[0] were blockTop[ i ], blockBottom[ i ], blockLeft[ i ], and blockRight[ i ].


So my question is, is there a way to check the array of block rectangles in the keyboard section, without the loop making everything so fast?



#include <SFML/Graphics.hpp>
using namespace sf;

int main()
{
    RectangleShape playerRect;
    RectangleShape blockRect[2];

    float playerTop, playerBottom, playerLeft, playerRight;
    float blockTop[2], blockBottom[2], blockLeft[2], blockRight[2];

    float playerX = 128;
    float playerY = 200;

    float blockX[2] = {200, 128};
    float blockY[2] = {120, 60};

    playerRect.setSize(Vector2f(16,16));
    playerRect.setFillColor(Color::Yellow);
    playerRect.setPosition(playerX,playerY);

    blockRect[0].setSize(Vector2f(16,16));
    blockRect[0].setFillColor(Color::Blue);
    blockRect[0].setPosition(blockX[0],blockY[0]);

    blockRect[1].setSize(Vector2f(16,16));
    blockRect[1].setFillColor(Color::Blue);
    blockRect[1].setPosition(blockX[1],blockY[1]);

    RenderWindow window(VideoMode(256,240),"");
    window.setSize(Vector2u(512,480));

    while (window.isOpen())
    {
        // Process events
        Event event;
        while (window.pollEvent(event))
        {
            // Close window: exit
            if (event.type == Event::Closed)
                window.close();
        }

        playerTop = playerY;
        playerBottom = playerY + 16;
        playerLeft = playerX;
        playerRight = playerX + 16;

        for (int i = 0; i < 2; i++)
        {
            blockTop[i] = blockY[i];
            blockBottom[i] = blockY[i] + 16;
            blockLeft[i] = blockX[i];
            blockRight[i] = blockX[i] + 16;
        }

        if (Keyboard::isKeyPressed(Keyboard::Up))
        {
            //for (int i = 0; i < 2; i++) {
            if (playerTop - 0.01 <= blockBottom[0]
             && playerBottom > blockTop[0]
             && playerLeft < blockRight[0]
             && playerRight > blockLeft[0]) {} else playerY -= 0.01; //}
        }
        else if (Keyboard::isKeyPressed(Keyboard::Down))
        {
            //for (int i = 0; i < 2; i++) {
            if (playerBottom + 0.01 >= blockTop[0]
             && playerTop < blockBottom[0]
             && playerLeft < blockRight[0]
             && playerRight > blockLeft[0]) {} else playerY += 0.01; //}
        }
        else if (Keyboard::isKeyPressed(Keyboard::Left))
        {
            //for (int i = 0; i < 2; i++) {
            if (playerLeft - 0.01 <= blockRight[0]
             && playerRight > blockLeft[0]
             && playerTop < blockBottom[0]
             && playerBottom > blockTop[0]) {} else playerX -= 0.01; //}
        }
        else if (Keyboard::isKeyPressed(Keyboard::Right))
        {
            //for (int i = 0; i < 2; i++) {
            if (playerRight + 0.01 >= blockLeft[0]
             && playerLeft < blockRight[0]
             && playerTop < blockBottom[0]
             && playerBottom > blockTop[0]) {} else playerX += 0.01; //}
        }

        if (Keyboard::isKeyPressed(Keyboard::Escape)) window.close();

        playerRect.setPosition(playerX,playerY);

        // Clear screen
        window.clear();

        // Draw the sprite
        window.draw(playerRect);
        window.draw(blockRect[0]);
        window.draw(blockRect[1]);

        // Update the window
        window.display();
    }

    return 0;
}

Thank you for your help.

8
Below is the whole program.  It is a very small and simple program made to test collisions.

You have a player rectangle, and two block rectangles.  If the player collides with either of the blocks, based on the direction he is coming from, his x or y coordinate gets pushed back.

So, the collision works fine in that sense...

However, for instance, if you are moving up and hit a block from the bottom (holding the up key), then press and hold the left key (while still holding the up key), and then let go of the up key, the player gets pushed all the way to the right, past the block.

I know why this is happening, it's obvious.  It is simply executing the collision routine for when you press left.  But I put flags in, such as movingUp, movingDown, etc., to combat this, and yet it still happens.

Also, in the code below, where it checks the key and the direction you're moving in, I even tried checking false for the other three directions, and it still doesn't solve anything.

For instance:
(if (playerRect.getGlobalBounds().intersects(blockRect[i].getGlobalBounds()) && movingUp && !movingDown && !movingLeft && !movingRight)

Feel free to copy and paste this code, and compile it yourself to see what I am talking about.

The collision works, but I don't know what logic I need to stop this collision bug from happening.  You'd think the flags that I have would work, but they're not.


Does anyone know WHY this is happening?

Any help would be greatly appreciated.  Thank you for your time.



#include <SFML/Graphics.hpp>
using namespace sf;

int main()
{
    RectangleShape playerRect;
    RectangleShape blockRect[2];

    float x = 128;
    float y = 120;

    bool movingUp = false;
    bool movingDown = false;
    bool movingLeft = false;
    bool movingRight = false;

    playerRect.setSize(Vector2f(16,16));
    playerRect.setFillColor(Color::Yellow);
    playerRect.setPosition(x,y);

    blockRect[0].setSize(Vector2f(16,16));
    blockRect[0].setFillColor(Color::Blue);
    blockRect[0].setPosition(200,120);

    blockRect[1].setSize(Vector2f(16,16));
    blockRect[1].setFillColor(Color::Blue);
    blockRect[1].setPosition(128,60);

    RenderWindow window(VideoMode(256,240),"");

    while (window.isOpen())
    {
        // Process events
        Event event;
        while (window.pollEvent(event))
        {
            // Close window: exit
            if (event.type == Event::Closed)
                window.close();
        }

        if (Keyboard::isKeyPressed(Keyboard::Up))
        {
            y -= 0.01;
            movingUp = true;
            movingDown = false;
            movingLeft = false;
            movingRight = false;
        }
        else if (Keyboard::isKeyPressed(Keyboard::Down))
        {
            y += 0.01;
            movingUp = false;
            movingDown = true;
            movingLeft = false;
            movingRight = false;
        }
        else if (Keyboard::isKeyPressed(Keyboard::Left))
        {
            x -= 0.01;
            movingUp = false;
            movingDown = false;
            movingLeft = true;
            movingRight = false;
        }
        else if (Keyboard::isKeyPressed(Keyboard::Right))
        {
            x += 0.01;
            movingUp = false;
            movingDown = false;
            movingLeft = false;
            movingRight = true;
        }

        for (int i = 0; i < 2; i++)
        {
            if (playerRect.getGlobalBounds().intersects(blockRect[i].getGlobalBounds()) && movingUp) y = blockRect[i].getPosition().y + 16;
            if (playerRect.getGlobalBounds().intersects(blockRect[i].getGlobalBounds()) && movingDown) y = blockRect[i].getPosition().y - 16;
            if (playerRect.getGlobalBounds().intersects(blockRect[i].getGlobalBounds()) && movingLeft) x = blockRect[i].getPosition().x + 16;
            if (playerRect.getGlobalBounds().intersects(blockRect[i].getGlobalBounds()) && movingRight) x = blockRect[i].getPosition().x - 16;
        }

        playerRect.setPosition(x,y);

        // Clear screen
        window.clear();

        // Draw the sprite
        window.draw(playerRect);
        window.draw(blockRect[0]);
        window.draw(blockRect[1]);

        // Update the window
        window.display();
    }

    return 0;
}

9
G. and fallahn, thank you so much guys.  I really do appreciate it.  You guys are both life savers.  I understand both things you guys pointed out.

The first one, with the playerSprite(), I can't believe I didn't notice that.  I guess I got confused when I was using the getLocalBounds(), so I put the parentheses after the playerSprite as well.

And the other one, when creating the player object--this is the first time that I'm using parameters in the constructor, so I didn't know that I had to include those parameters when I create the player object.  But it makes sense to me.

I couldn't see my errors, even though they were clearly right in front of me.  I don't like posting a question on a forum unless it's a last resort, because I like to figure out what the problem is on my own.  But I just couldn't understand why I was getting those errors.

Thanks again guys. :)

10
The errors I get are:

In constructor 'Player::Player(sf::Sprite&)':
error: no match for call to '(sf::Sprite) ()'



Brief code with certain things that don't matter omitted (like the RenderWindow and other event stuff).


#include <SFML/Graphics.hpp>

using namespace sf;

class Player
{
  private:
        int bbox_top, bbox_bottom, bbox_left, bbox_right;    //collision for all sides of the player sprite
  public:
        Player(Sprite& playerSprite);   // or (const Sprite& playerSprite) not sure if the const is needed
        ~Player();
};

Player::Player(Sprite& playerSprite)
{
    bbox_top = playerSprite().getLocalBounds().y;
    bbox_bottom = playerSprite().getLocalBounds().height;
    bbox_left = playerSprite().getLocalBounds().x;
    bbox_right = playerSprite().getLocalBounds().width;
}

int main()
{
    Texture playerTexture;
    playerTexture.loadFromFile("player.png");
    Sprite playerSprite(playerTexture);

    Player player;
}
 


I'm referencing the sprite, so I don't get why I'm getting these errors.  It doesn't matter if I use const in the Player constructor parameter or not, I still get the error.  I even tried different names in the parameter list, like Sprite& ps.

I know this is more of a C++ question than SFML, but it is related to the sprites.  I don't get why it's not working.

If you know the answer, you're a life saver.  Thank you.

11
Thank you for your response.  I'm sorry for asking a C++ related question.  I'm using SFML at the moment, but I couldn't get this enumeration state code to work, so I typed up this code without the library elements.

And sorry for the late reply.  Been very busy.

Thanks again Laurent for your help. :)

12
The language is C++.

I Googled tons of different results and looked at them, but I can't seem to find a specific answer to what I'm looking for.

I have a Player class, and I want to set his "state", but I want to do it from its public member function, because the state is an enumeration in his private section.  I've tried various different ways, but the compiler keeps giving me errors.  I know how to do it with other values, but when it comes to enumerations, I can't get it to work.  I even tried references, and still no luck.

Here is sample code.  Do you know what I'd have to change to get it work?  Thanks.

#include <iostream>

using namespace std;


class Player
{
   private:
        enum State {IDLE, MOVING};
        State playerState;
   public:
        Player();
        ~Player();
        enum setPlayerState(enum &playerState ps);
        enum getPlayerState();
};

Player::Player()
{
}

Player::~Player()
{
}


enum Player::setPlayerState(enum &playerState ps)
{
   playerState = ps;
}


enum Player::getPlayerState()
{
   return playerState;
}




int main()
{
   Player player;
   player.setPlayerState(MOVING);
   cout << "The player's state is" player.getPlayerState() << "." << endl;
   return 0;
}

I know how to set and get private integer values, but enumerations, I'm having no luck.  I want to be able to type in the parameters player.setPlayerState(IDLE) OR player.setPlayerState(MOVING), but I don't know how to do it from the public member function.

If you know the answer, I just want to say Thank You, and I appreciate your time.

13
I understand now, at least I think I do.  When I'm creating an instance of RenderWindow, I'm using the name 'window'.  So, it is that 'window' that has to be passed to my newly created function.

I tried using a different variable name to pass to the function, such as 'a', or 'i', but it gives me errors.  However, when I use 'window', such as windowHandler(window);, it works like a charm.  And the only thing I can think of, is that the only instance of window is 'window'.  So it works.

In any case, I got it to work, so thank you very much. :)

14
I'm trying to use SFML's RenderWindow class from another function, but I keep getting an error, and honestly, I have no clue why I am getting the error.  I'm referencing the "myWindow" variable in the parameter list, and everything has been declared.  I'm getting frustrated.  I've Googled tons of tutorials and articles, and I can't pinpoint why this error is happening.  Here's the code:

#include <SFML/Graphics.hpp>
#include "declarations.h"

int main()
{
    //create the main window
    sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT), "Title");

    //start the game loop
    while (window.isOpen())
    {
        //process events
        sf::Event event;
        while (window.pollEvent(event))
        {
            //close window: exit
            if (event.type == sf::Event::Closed) window.close();
        }
        windowHandler(myWindow);  //Right here is where I am getting the error
    }
    return EXIT_SUCCESS;
}

-----------------------------------


#ifndef DECLARATIONS_H_
#define DECLARATIONS_H_

const short SCREEN_WIDTH = 256;
const short SCREEN_HEIGHT = 240;

void windowHandler(sf::RenderWindow& myWindow);

#endif

-----------------------------------


#include <SFML/Graphics.hpp>
#include "declarations.h"

void windowHandler(sf::RenderWindow& myWindow)
{
    //clear screen
    myWindow.clear();

    //draw to screen, will update this later, right now it doesn't draw anything
    //window.draw();

    //update the window
    myWindow.display();
}

The error I get:

In function 'int main()':|
error: 'myWindow' was not declared in this scope|

I don't understand why I am getting this error.  Everything is declared, and myWindow is referenced with an &.  The function prototype is included in the declarations header file, and the header file is included before it even touches main(), which means it should be able to be used anywhere in the program.  So why is it telling me that 'myWindow' is not declared in this scope.  It is!

If you know the answer to the problem, it would greatly be appreciated.  Thank you so much.

Pages: [1]
anything