Recent Forum Posts
From categories:
page 1123...next »

I added some (smaller) blocks, and the greatest thing happens every once in a while. I just jump into the bottom of it, and about one of every five times, it stops and crashes the game. Another one is that if I jump into one of the big walls (on the sides) and keep going I fall through the bottom.

(EDIT: Code updated semi-significantly)

"Small" Collision Glitches by spacechase0spacechase0, 1258598617|%e %b %Y, %H:%M %Z|agohover

Yep, that works. Thanks again!

Re: Game crashes upon startup by spacechase0spacechase0, 1258595073|%e %b %Y, %H:%M %Z|agohover

Yeah. Basically, it says "give me a reference to walls[i]" instead of saying "give me a copy of walls[i]".

// This gets a reference to walls[0]
wall& current = walls[0]

// This takes a copy of walls[0]
wall current = walls[0]

So, in my first example (using &) current and walls[0] are one and the same. If i change for example the x in current, walls[0] is also changed (because they the same object). The second example, current is just a copy of walls[0]. So if i change something in current, it will not change anything in walls[0].

You found the reason for your crash with the debugger. It is in fact (probably) that you are drawing with the window (App.Draw()) even though you have closed it (in your event loop when the user pressed escape). You could try using an isRunning boolean variable instead, and then call App.Close() after the main loop has exited where you are sure App will not be used agian :)

Re: Game crashes upon startup by u9u9, 1258582767|%e %b %Y, %H:%M %Z|agohover
By the way, what does the & do? I read it is something about pointers, but that is all I know.

It is called a reference operator. In your case it will set the variable w to read from the walls[0] array.
After using this, you just have to use w.x, w.y, etc, instead of using walls[0].x, walls[0].y, etc.

Re: Game crashes upon startup by kake_fiskkake_fisk, 1258523973|%e %b %Y, %H:%M %Z|agohover

This works perfectly, thanks!

I never knew how to use the debugger before, even though I heard of it. Now, the last problem I have isn't that important for now, but it will be if I distribute it.

Whenever I close the application, it gives "The instruction at "0x697374c0" referenced memory at "0x00fa0654". The memory could not be "read".
Click OK to terminate the program"

This has been happening for quite a while. When I run the debugger (now that I know how to use it :) ) it stops at line 203, a blank one right under App.Draw(how_to_scr); (EDIT: After I ran it again and looked, it says there is something wrong with a dll in C:\WINDOWS\system\ called nvoglnt.dll at address 697374C0). Here is the new source:

#include <SFML/Graphics.hpp>
#include <string>
#include <sstream>
#include <vector>
 
using namespace std;
//using namespace sf;
 
//Classes
class player
{
    public:
        sf::Sprite sprite;
        float x;
        float y;
        float xprevious;
        float yprevious;
        float gravity;
        float vspeed;
        bool gravity_on;
};
 
class wall
{
    public:
        sf::Sprite sprite;
        float x;
        float y;
};
 
//Variables
bool cjump = false;
unsigned int i = 0;
long num_of_scr = 0;
string scr_name = "screenshot0.jpg";
 
//Misc functions
bool check_position_wall(float a, float b, std::vector<wall> c)
{
    for (i = 0; i < c.size(); i++)
    {
        if (a > c[i].x and a < c[i].x + 50 and b > c[i].y and b < c[i].y + 50)
        {
            return true;
        }
    }
    return false;
}
 
//Game
int main()
{
    vector<wall> walls;
 
    sf::RenderWindow App(sf::VideoMode(650, 500, 32), "First Game");
    App.SetFramerateLimit(60);
 
    sf::Image char_image;
    if (!char_image.LoadFromFile("char_sprite.png"))
    {
        return 1;
    }
    sf::Image wall_image;
    if (!wall_image.LoadFromFile("wall_sprite.png"))
    {
        return 1;
    }
 
    player player;
    player.x = 325;
    player.y = 250;
    player.gravity = 0.1;
    player.vspeed = 0;
    player.gravity_on = true;
    player.sprite.SetX(player.x);
    player.sprite.SetY(player.y);
    player.sprite.SetImage(char_image);
 
    for (i = 0; i < 650/50; ++i)
    {
        // Add top and bottom rows
        walls.push_back(wall());
        walls.back().sprite.SetImage(wall_image);
        walls.back().x = i * 50;
        walls.back().y = 100;
 
        walls.push_back(wall());
        walls.back().sprite.SetImage(wall_image);
        walls.back().x = i * 50;
        walls.back().y = 450;
    }
    for (i = 100/50; i < 500/50; ++i)
    {
        // Add both columns
        walls.push_back(wall());
        walls.back().sprite.SetImage(wall_image);
        walls.back().x = 0;
        walls.back().y = i * 50;
 
        walls.push_back(wall());
        walls.back().sprite.SetImage(wall_image);
        walls.back().x = 600;
        walls.back().y = i * 50;
    }
 
    sf::String how_to_scr("Press F1 to take a screenshot of the game. \nThey will appear in the game folder.", sf::Font::GetDefaultFont(), 30);
    how_to_scr.SetColor(sf::Color(255,255,255));
    how_to_scr.SetX(8);
    how_to_scr.SetY(8);
 
    while (App.IsOpened())
    {
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
            {
                App.Close();
            }
            else if (Event.Type == sf::Event::KeyPressed)
            {
                if (Event.Key.Code == sf::Key::Escape)
                {
                    App.Close();
                }
                else if (Event.Key.Code == sf::Key::F1)
                {
                    sf::Image Screen = App.Capture();
                    Screen.SaveToFile(scr_name);
                    num_of_scr += 1;
                    std::stringstream textStream;
                    textStream << "screenshot" << num_of_scr << ".jpg";
                    scr_name = textStream.str();
                }
            }
        }
 
        player.xprevious = player.x;
        player.yprevious = player.y;
 
        if (App.GetInput().IsKeyDown(sf::Key::Left))
        {
            player.x = (player.x - 3);
            player.sprite.SetX(player.x);
        }
        if (App.GetInput().IsKeyDown(sf::Key::Right))
        {
            player.x = (player.x + 3);
            player.sprite.SetX(player.x);
        }
 
        for (i = 0; i < walls.size(); i++)
        {
            if (((player.x + 11) > walls[i].x and player.x < walls[i].x + 50) and ((player.y + 19) > walls[i].y and player.y < walls[i].y + 50))
            {
                if (check_position_wall(player.x + 11 + 1, player.y + 9, walls))
                {
                    player.x = player.xprevious;
                }
                else if (check_position_wall(player.x - 1, player.y + 9, walls))
                {
                    player.x = player.xprevious;
                }
                else if (check_position_wall(player.x + 5, player.y + 19 + 1, walls))
                {
                    player.y = player.yprevious - 0.75;
                    player.vspeed = 0;
                    player.gravity_on = false;
                    cjump = true;
                }
                else if (check_position_wall(player.x + 5, player.y + 0.75, walls))
                {
                    player.y = player.yprevious + 0.75;
                    player.gravity_on = true;
                    player.vspeed = 0;
                }
                if (check_position_wall(player.x + 5, player.y + 0.75, walls))
                {
                    player.y = player.yprevious + 0.75;
                    player.gravity_on = true;
                    player.vspeed = 0;
                }
            }
        }
        if (player.gravity_on)
        {
            player.vspeed += player.gravity;
        }
 
        if (App.GetInput().IsKeyDown(sf::Key::Up) and cjump == true)
        {
            cjump = false;
            player.vspeed = -4;
            player.gravity_on = true;
        }
 
        player.y += player.vspeed;
 
        App.Clear();
 
        App.Draw(how_to_scr);
 
        player.sprite.SetX(player.x);
        player.sprite.SetY(player.y);
        App.Draw(player.sprite);
        for(i = 0; i < walls.size(); i++)
        {
            wall& w = walls[i]; // This gets a reference to wall i. The & makes sure of that. I use this as it is easier to type :)
            w.sprite.SetPosition(w.x, w.y);
            App.Draw(w.sprite);
        }
 
        App.Display();
    }
    return 0;
}

Sorry about bugging everyone so much. After this I'll be quiet. :)

By the way, what does the & do? I read it is something about pointers, but that is all I know.

Re: Game crashes upon startup by spacechase0spacechase0, 1258513438|%e %b %Y, %H:%M %Z|agohover

Ok, i have cleaned up the code and done it the same way you, kake. First, remove the resizing of the vector:

//    walls.resize(50);

Then, to create the level do this instead of the previous loops:

for (i = 0; i < 650/50; ++i)
{
    // Add top and bottom rows
    walls.push_back(wall());
    walls.back().sprite.SetImage(wall_image);
    walls.back().x = i * 50;
    walls.back().y = 100;

    walls.push_back(wall());
    walls.back().sprite.SetImage(wall_image);
    walls.back().x = i * 50;
    walls.back().y = 450;
}
for (i = 100/50; i < 500/50; ++i)
{
    // Add both columns
    walls.push_back(wall());
    walls.back().sprite.SetImage(wall_image);
    walls.back().x = 0;
    walls.back().y = i * 50;

    walls.push_back(wall());
    walls.back().sprite.SetImage(wall_image);
    walls.back().x = 600;
    walls.back().y = i * 50;
}

One final bug also. You must set the sprite's position of each wall object before drawing it. (This only needs to be done once if the walls will never move). Correct your drawing loop for the walls like so:

for(i = 0; i < walls.size(); i++)
{
    wall& w = walls[i]; // This gets a reference to wall i. The & makes sure of that. I use this as it is easier to type :)
    w.sprite.SetPosition(w.x, w.y);
    App.Draw(w.sprite);
}

And you are good to go!

Re: Game crashes upon startup by u9u9, 1258494949|%e %b %Y, %H:%M %Z|agohover

@u9
Thanks for posting the debugging guidelines. I don't know how to debug.
This will certainly help.

And I think he wants to do this by the way:

GameObject current(xx*32, yy*32, spr_wall);
obj_walls.push_back(current);

Then he don't need to reserve memory, but it will extend as he pushes back elements.

Re: Game crashes upon startup by kake_fiskkake_fisk, 1258468236|%e %b %Y, %H:%M %Z|agohover

Hi spacechase,

sorry i didn't give a better answer. I wanted to, but i didn't have time to write more. I was looking how to fix it myself. Lets go through it now:

The value of i as you see in the debugger was 51. I know you earlier in the start of main() resized your walls vector to 50. Here lies the problem. You are accessing a part of memory which you have not reserved. It is basically the for loop that is a little mystery for me. It starts:

for (i = walls.size(); xtogo < 650; i ++)

Notice that you initialize i to 50 (the size of your vector). But then you do an i++ every loop, which next iteration brings i to 51, and then it goes wrong1. When you try to access element 51 in the vector which is 50, you get a crash. What exactly are you trying to do exactly?

I will try and be online in the chat in about 8-9 hours, 20-2200 GMT'ish, we can take it there :)

Well, back to work

Re: Game crashes upon startup by u9u9, 1258459498|%e %b %Y, %H:%M %Z|agohover

I see it, although it makes no sense. I see no usage of i before then, except in a function which hasn't even been used yet. I tried changing the declaration to different types. I also tried making i = 0 in that forstatement. The third thing was adding was right after the declaration of the vector, adding a while statement that checked if the vector was empty, and if it was then use pop_back() on it. It ran fine, but the character just fell because here weren't any blocks. I tried declaring i right before the first while, no luck. The last thing I tried was making the array larger. I would love to try more, but my dad is making me watch some G.I.Joe movie. I just got done with school around seven, then had supper, then did a few chores, and now I have to watch this movie and after it is done, I gotta go to bed. Whoopee!

So, I guess I won't be doing anything until tommorow afternoon. I can't think of anything else, please tell me the answer.

Re: Game crashes upon startup by spacechase0spacechase0, 1258422338|%e %b %Y, %H:%M %Z|agohover

Here's a course in debugging. Compile your project in debug target. Then:

  1. Start the debugger by clicking the debug icon (or selecting debug -> start from the menu or pressing F8.) The program will stop with a SIGSEV, which means the program is accessing memory it is not allowed to, and so the operating system terminates it. You will see this error many many times in your c++ career :)
  2. In the Callstack window double click main to see where the program has reached. The cursor will jump to the specific line.
  3. Look which variables on the line might have wrong values. There are three variables, walls (a vector), i (an int) and wall_image (an sf::image). I am pretty sure the walls and wall_image are ok looking at the previous lines of code, so lets have a look at i.
  4. Mark, and right click i and select watch 'i' and it will pop up in your Watches window.
  5. Notice anything wrong with this value? How about when used as the index to your walls vector?
howto_debug.png

P.s. Let me know if you don't see it, or don't manage to fix it.

Re: Game crashes upon startup by u9u9, 1258404254|%e %b %Y, %H:%M %Z|agohover

The code compiles fine, but whenever I run it I get the normal debug console and the application opens up (still showing what is behind it) and a few seconds later the "So-and-so has an error. Please send Microsoft this error report." thing appears and the window turns white. Once I click something and the window closes, the debug console acted like the program ran fine. I had this once in the past, but it somehow fixed itself (AKA I did something I don't remember to fix it). I removed everything I remember adding, and commented out something that gave a little trouble in the past, but nothing helped. Any ideas?

Game crashes upon startup by spacechase0spacechase0, 1258334898|%e %b %Y, %H:%M %Z|agohover

Thanks, that did the trick. :D
I also did a little mistake of putting the push_back outside the while statement.

Re: Vector of surfaces [SDL] by kake_fiskkake_fisk, 1258323619|%e %b %Y, %H:%M %Z|agohover
Re: Vector of surfaces [SDL]
u9u9 1258322264|%e %b %Y, %H:%M %Z|agohover
in discussion Forums / Have Your Question Answered » Vector of surfaces [SDL]

i'm pretty sure what you want is a vector of pointers to surfaces, not a pointer to a vector of surfaces… See the difference?

In code the prior is:

std::vector<SDL_Surface*> msg_filenames;
Re: Vector of surfaces [SDL] by u9u9, 1258322264|%e %b %Y, %H:%M %Z|agohover

I'm trying to make a vector of surfaces in SDL.
This is my code:

//create
vector<SDL_Surface> *msg_filenames;
string tmp = "";
vector<string> files;
 
//onClick
while (!files.empty())
{
    tmp = files.back();
    files.pop_back();
}
msg_filenames->push_back(*TTF_RenderText_Blended(fnt_score, tmp.c_str(), BLACK));
 
//draw
for (int i=0;i<msg_filenames->size();i++)
{
    apply_surface(16,16+i*16,&msg_filenames->at(i),screen);
}

As you see in my code, I basically wants to make a vector of TTF rendered surfaces and draw them in a list.
But when I run it returns 3. And I am pretty sure that the returning value has nothing to say here.
And I would also like to switch out the "->" with a ".". Do any of you see what is wrong here?

Vector of surfaces [SDL] by kake_fiskkake_fisk, 1258298206|%e %b %Y, %H:%M %Z|agohover

To everyone else, the error was that std:: had to be written in front of vector<object>.

Re: Trying to get vectors working by u9u9, 1258203038|%e %b %Y, %H:%M %Z|agohover

Yeh, thanks. :)

Re: Checking for keyboard input [SDL] by kake_fiskkake_fisk, 1258156436|%e %b %Y, %H:%M %Z|agohover

The correct solution is:

if ((event.key.keysym.unicode >= (Uint16)' ' && event.key.keysym.unicode <= (Uint16)'~') &&
!(event.key.keysym.unicode == (Uint16)'"' || event.key.keysym.unicode == (Uint16)'*'))
{
    //Something to happen
}

Right?

Re: Checking for keyboard input [SDL] by u9u9, 1258155911|%e %b %Y, %H:%M %Z|agohover

I am trying to get vectors working with a class, but it doesn't want to compile. There are various other errors, but they seem related to this one. Using SFML.

That works now, thanks to u9u9.

Trying to get vectors working by spacechase0spacechase0, 1258155640|%e %b %Y, %H:%M %Z|agohover

I'm trying to check if a letter on the keyboard is pressed. That works fine. (the upper part)
But since, I am going to use this in a filename, I have to check if there's invalid characters.
When I added that part, it didn't the statement didn't return true anymore.

if ((event.key.keysym.unicode >= (Uint16)' ' && event.key.keysym.unicode <= (Uint16)'~') &&
!(event.key.keysym.unicode = (Uint16)'"' && event.key.keysym.unicode = (Uint16)'*'))
{
    //Something to happen
}

I suspect I have done something wrong with some operators in there, but I'm not sure.
Do you know what the problem is?

Checking for keyboard input [SDL] by kake_fiskkake_fisk, 1258152903|%e %b %Y, %H:%M %Z|agohover

Been kinda lazy lately, but finally got back into C++.

For SFML, how do you use that background loop? I tried editing the FOR loop to set the location of a background sprite and then draw it, but it didn't work.

EDIT: Nevermind, got that done with some help.

Re: A Couple of Questions... by spacechase0spacechase0, 1258066886|%e %b %Y, %H:%M %Z|agohover