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 - janszy

Pages: [1]
1
Graphics / Re: Problem with stars
« on: August 05, 2014, 05:42:29 pm »
Quote
Something is wrong in your code: you are using the fill constructor of std::vector and also push_back sprites.

You're right. I indeed mixed those. Thanks for the correction  ;)

2
Graphics / Re: Problem with stars
« on: August 05, 2014, 02:06:48 pm »
You still need to declare your variables, load the texture, set the sprites etc. like in your first post. You don't have to create new variables for every star instance though. You can do it in a loop as well:
sf::Texture gwiazda;
gwiazda.loadFromFile("gwiazda.png"); // load the texture

int numStars = 5; //how many stars you want
std::vector<sf::Sprite> stars(numStars);

for(int i=0; i < numStars; ++i)
{
    sf::Sprite spritegwiazda;
    spritegwiazda.setTexture(gwiazda); // use the loaded texture
    spritegwiazda.setPosition(150,i*100); // you can set the position based on the value of i or generate a random position etc.
    stars.push_back(spritegwiazda);
}

3
Graphics / Re: Problem with stars
« on: August 05, 2014, 10:50:13 am »
It doesn't matter which side the player collects the star, you still can use vectors.
std::vector<sf::Sprite> stars;
//create the stars
// ...

//put this code in your game loop
for(auto &thisStar: stars) //assuming you use C++11
{
    if(intersect(player, thisStar)) //check that player collected a star or not. Use your check function here.
    {
        stars.erase(thisStar); //If it did then remove the star's sprite from the vector and don't render it next time.
    }
}

 

4
Graphics / Re: Large arrays of sprites crashing
« on: July 29, 2014, 04:01:36 pm »
You can also use vector as multidimensional:
/*
This code creates a 10x20 vector of sprites. You can index it as you would index
an array: window.draw(twoDimVec[3][6]);
Note that you have to create the inner vectors (second dimension) with a for loop.
(I'm not aware of easier solutions for that).
*/


//You must put a space between the two >> if you don't use C++11
std::vecotr<std::vector<sf::Sprite>> twoDimVec(10);
for(int i=0; i<10; ++i)
{
    twoDimVec[i] = std::vector<sf::Sprite>(20);
}

5
Graphics / Re: Large arrays of sprites crashing
« on: July 29, 2014, 03:45:03 pm »
Use std::vector and forget about raw arrays.

Strelok is right. Use vector and extend dynamically when it's needed. Consider also reserve rather then resize (members of vector). The former only allocate memory while the latter also call the default constructor of your stored objects (which is sf::Sprite now), this cause unnecessary overhead in most cases.

Where do you load your textures btw? Doesn't setTexture throw exception when texture size is 0x0?

6
Graphics / Re: Problems with passing sf::Texture* as a parameter
« on: July 29, 2014, 09:54:23 am »
Hi there!

I suspect that your sf::Texture* is uninitialized. Do you create your texture before you try to create your RoomObject? I got similar error when I used
sf::Texture *texture;
instead of
sf::Texture *texture = new sf::Texture();

After this you need to load a texture, because the line
thisShape.setTexture(spriteLoc);
will cause an error too, if spriteLoc is an initialized but empty texture.

I also have a tip for you. Use initializer lists in constructors, when you can:
RoomObject::RoomObject(sf::Texture* sprLoc, std::string name, int xLocation, int yLocation):
spriteLoc(sprLoc),objName(name),xLoc(xLocation),yLoc(yLocation)
{
    thisShape.setFillColor(sf::Color::White);
    thisShape.setPosition(xLoc * 32, yLoc * 32);
    thisShape.setSize(sf::Vector2f(32, 32));
    thisShape.setTexture(spriteLoc);
}
 
It works like this: you type a colon after the closing ) of your constructor, then you list your member variables as my_member(initialize_from_this) separated by comas. When you're done the open { follows, and you can continue with other statements (like setting the properties of thisShape).
This is better, because here the member variables are initialized directly from the variables. If you assign them in the body of the constructor, then they are created from some default values (this is unnecessary) and then assigned to your variables.

7
Graphics / Re: SFML Game Development, Movement
« on: July 25, 2014, 01:36:44 pm »
It would be more straightforward if you put your keyboard handling directly into update function instead passing bools around. Something like this:

void Game::update(){
    sf::Vector2f movement(0.f, 0.f);

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
        movement.y -= 1.f;
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
        movement.y += 1.f;
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
        movement.x -= 1.f;
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
        movement.x += 1.f;

    mPlayer.move(movement);
}
 

Is this working for you? (I didn't test it)
This solution should work as long you want to move your objects while a key is pressed.

8
Graphics / Re: Drag & drop self created sprites
« on: July 25, 2014, 10:08:06 am »
Hi, there are some things I don't understand:
1., What's the point of calling EnemyList.rbegin() and EnemyList.rend() alone (without an assignment)?
2., In the for loop after if(dragging == true) you set every enemy's position to the same (not just the one's that you dragging).
3., If you create a new enemy then you add it to the EnemyList so it will be drawn in the last for loop as well, so you don't have to draw newSprite specifically.

Sorry, I know that this doesn't answer your question, but I couldn't figure the main cause of your problem yet.

9
Graphics / Re: Displaying text in center of screen
« on: July 24, 2014, 01:50:21 pm »
In order to put your text right in the middle you need to switch the lines of setPosition and setOrigin. Now your text is positioned based on the top left corner of the bounding rectangle, and then you set the origin to the middle of it.

10
Graphics / Re: OpenGL - clipping with not rectangular shape
« on: October 06, 2013, 06:11:38 pm »
Thank you for the answers. Sorry for not being clear in the first place. So my original idea was, that I create a png image with the puzzle shape (I draw it with a graphical program), set the inside part to transparent and the outside part lets say to green. Then I mask this image with the original picture and set the green pixels to transparent.

But I'd like to add an extra functionality: The user can define a deformation rate, and so every piece would have a unique deformed shape. I don't know how to do this with the above method. So after some search I found that the common way to draw puzzle pieces is with Bezier curves. So I would give only the control points of the curve (and I slightly modify the control points to get a deformed shape), but this way I need to check every point if it's inside or outside the shape that the control points define. And therefore I need an algorithm to test for a point if it's outside or inside. (It would be enough, if I knew which side of a given curve is the point and test it for all of the four sides.) And I didn't know that I need to create such a test function myself (and if I need, how to do this) or OpenGL could handle this automatically for me.

By the way I need to create the puzzle pieces only once, then I'd store them (with their transparent bounding rectangle) on a bigger texture, and use that texture for drawing sprites.

11
Graphics / Re: OpenGL - clipping with not rectangular shape
« on: October 05, 2013, 11:41:15 am »
Yes, you understood what I want, and I know thats the method for doing this (although I didn't know I can do this inside SFML). I just wondered OpenGL could do this for me automatically with a closed shape defined by its bounding curves, or do I have to find and implement an algorithm for that myself.

12
Graphics / OpenGL - clipping with not rectangular shape
« on: October 05, 2013, 09:39:57 am »
Hello Everyone!

I've got a problem, and haven't found a solution yet. Assume that I have a puzzle shape bounded by some bezier curves, and I want to clip that shape from a texture. Can I do this with OpenGL? I've never used it before and don't know if it is capable of such thing. Or do I have to define my own test function, and check every pixel if it's inside/outside the shape, and copy it from the original texture depending on that? If yes, what algorithm do I use? Can you suggest me a solution or some tutorial that can help me? Thanks!

13
General / Re: Structure of Game loop
« on: August 09, 2012, 06:50:37 pm »
Yes, I accidently posted it while I was writing it. I edited my post, and added the missing section.
Thank you for your answer! I will check the gitHub  ;)

14
General / Structure of Game loop
« on: August 09, 2012, 06:21:56 pm »
Hello Everybody!

I'm new in game programming, and I need a little help to set up my game loop. My game has several states, like 'Initialize', 'MainMenu', 'GamePlay', etc. I'm not sure what is the correct order of event handling and checking gamestate. My current solution is that I check the state first, then I handle events, if I need it. My code structure is like this:
Game::GameLoop(){
    while( GameState != Closed ){
        sf::Event evt; //I can't declare variable in the switch statement, because then I got compile error

        switch( GameState ){
        case Initialize:
            InitializeGame(); //I don't need to handle events here
            break;
        case MainMenu:
            while( window.pollEvent( evt ){
                if( evt.type == sf::Event::Closed ){
                    GameState=Exit;
                }
                if( evt.type == sf::Event::KeyPressed ){  //and other input events
                    HandleInput( evt );
                }
            }

            //Other game specific stuff goes here, like update and render menus, etc.

            break;
        case GamePlay:
            //It's almost the same, as above
            break;
        //Check all the states...

        } //end of switch
    } //end of while
} //end of the function
 

I do it like this way because not every state needs event handling (for example Initialize, Exit). Would be the other way better, when I always handle events, and I check the states in the EventHandler function?

Such like this:
Game::GameLoop(){
    while( GameState != Closed ){
        sf::Event evt;
        while( window.pollEvent( evt ) ){
            EventHandler( evt );
        }
        //Do game specific stuff here
    }
}
 

I'm a little bit confused, which is the better solution. What do you suggest? Does this change game to game?

I would like to ask one more question. I read an article (http://www.koonsolo.com/news/dewitters-gameloop/ ) about implementations of the game loop. I'm using the second implementation (Game Speed dependent on Variable FPS) at the moment, but the article says it is the worst. I read this implementation in two different game development books, that's why I use it. Should I change this approach with something else? Or is this ok for PC games?

Thank you for your answers!

Pages: [1]
anything