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

Author Topic: Crash Error, windows sfml game  (Read 9142 times)

0 Members and 1 Guest are viewing this topic.

lordseanington

  • Jr. Member
  • **
  • Posts: 86
  • Coffee is the blood of a coder.
    • View Profile
Crash Error, windows sfml game
« on: July 30, 2014, 10:44:56 pm »
 This game i am working on is a tilemap game. It compiles perfectly but crashes when run.
 I have created a game in c++ using sfml. It is still in early stages. I have an error I can not figure out. It compiles perfectly but when it is run, it instantly crashes. It happened after I added the area I commented as inventory
and the area commented as item_use. Please note I had to cut out some functions.

The Code

(click to show/hide)

This used to be much worse. Before i ran into this error i had the graphics driver fail on me. It is fixed but now i ran into this.
« Last Edit: August 01, 2014, 10:59:09 pm by lordseanington »

Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
Re: Crash Error, windows sfml game
« Reply #1 on: July 30, 2014, 11:15:32 pm »
This makes me feel dizzy :o

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Crash Error, windows sfml game
« Reply #2 on: July 30, 2014, 11:17:05 pm »
 :o
Code should be in [code=cpp][/code] tags.
Entire code should not be necessary to ask about a single problem.
If you're going to post entire code, it should be at least compilable as it is (which this isn't), and preferably in [spoiler][/spoiler] tags.

You definitely should read this. If I didn't post this link, someone else would have.

EDIT: spelling fix
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Crash Error, windows sfml game
« Reply #3 on: July 31, 2014, 12:21:46 am »
In addition to the link Hapax already posted, take a look at sscce.org also.

I'm not even going to try to spot the cause for your specific problem in the wall of code you posted, but I will point out a few things that caught my eye while just quickly scanning it.

#define MAPWIDTH 10
#define MAPHEIGHT 10
#define BLOCK_NUMBER 2
...
Preprocessor macros are evil, avoid when possible - use "const int foo = ...;" or "constexpr" or lots of other options (here are some more arguments).

...
sf::Sprite sprite;
sf::Texture texture;
sf::Sprite player_image;
sf::Vector2i mouse_information;
bool show_inventory;
bool show_menu;
sf::RenderWindow window(sf::VideoMode(64+(MAPWIDTH*32), 64+(MAPHEIGHT*32)), "Adventure Game");
signed int current_block_temp_x;signed int current_block_temp_y;
...
Global variables. Just say no.
There are loads of good reasons why you should avoid global variables in general and SFML types especially.

void wait(int miliseconds)
{
sf::Time elapsed;
int exit1=1;
...
"exit1" is begging to be a bool rather than int. Why is it named "exit1" by the way?

/////////////////////////////////Graphics Functions///////////////////////////

sf::Font font;
sf::Text text;
Again with the globals :-(

void load_font(std::string filename)
{
font.loadFromFile(filename);
}
This seems rather pointless. So you replace one line of code "font.loadFromFile(..." with another single line of code "load_font(..." - what do you gain by that?  maybe if there was some error handling or other stuff in there I could see the point, but as it it, just wrapping one line of code in a function like this seems pretty pointless.

struct map
{
int block[MAPWIDTH+1][MAPHEIGHT+1];
int entity[(MAPWIDTH+1)*16][(MAPHEIGHT+1)*16];
bool colition[MAPWIDTH*16][MAPHEIGHT*16];
}map;
I'm guessing you have a C background.
Also again - why global?
And personally I'd prefer std::array or std::vector over old style arrays.

You output a lot of stuff to std::cout - like
std::cout << "\nWindow Created";
but you never seem to output std::flush or std::endl, so since std::cout is buffered there's no telling when that output is actually going to show up. Either flush the stream once in a while or use std::cerr if you want to see the output at predictable times.

x3=1;y3=1;
My advice would be to stick to one statement pr line as a general rule. Once it comes time to debug something and you try to set a breakpoint on a specific statement and find that you can only set it on a line you'øll be sorry. Also when something crashes and the backtrace you get from the coredump tells you that the problem is on line 1234 and then there are 5 statements on that line you'll also be sorry.

Btw, I seriously hope that the lack of indentation and general unreadabillity of the code is mostly due to the lack of "code=cpp" tags, but if not then I'd advice looking into tools like clang-format

There's loads more to comment on, but I'll leave it at this for now. I hope you see what I'm trying to say...

lordseanington

  • Jr. Member
  • **
  • Posts: 86
  • Coffee is the blood of a coder.
    • View Profile
Re: Crash Error, windows sfml game
« Reply #4 on: August 01, 2014, 08:55:11 pm »
I have started using vectors and do have some c background. As for the wall of code, sorry. I am brand new to this forum, have no idea how to do spoiler tabs. I will move to constants. I just have been trying to optimize my code and fix this error. Oh, exit is already used in another function somewhere else and i prefer to keep names simple. I have made things much better. I can not figure out how to avoid global variables for what I am doing.
« Last Edit: August 01, 2014, 09:11:51 pm by lordseanington »

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Crash Error, windows sfml game
« Reply #5 on: August 01, 2014, 09:50:19 pm »
As for the wall of code, sorry. I am brand new to this forum, have no idea how to do spoiler tabs.
Feel free to modify your original post to add [spoiler] before the code and [/spoiler] after the code so we don't have to scroll past the entire thing to see what the latest comment was  ;)

I can not figure out how to avoid global variables for what I am doing.
At the very least you could define them within the main() block and pass them to the functions as parameters. You only need pass the ones that that particular function needs.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
Re: Re: Crash Error, windows sfml game
« Reply #6 on: August 01, 2014, 10:02:18 pm »
I have started using vectors and do have some c background. As for the wall of code, sorry. I am brand new to this forum, have no idea how to do spoiler tabs. I will move to constants. I just have been trying to optimize my code and fix this error. Oh, exit is already used in another function somewhere else and i prefer to keep names simple. I have made things much better. I can not figure out how to avoid global variables for what I am doing.
It's not a matter of optimizing the code, you need to code proper c++ instead of highschool level c/c++ hybrid. It's like an author writing a 200 pages book in a single chapter without using half of the language rules and using Latin and Latin derived language altogether :)

lordseanington

  • Jr. Member
  • **
  • Posts: 86
  • Coffee is the blood of a coder.
    • View Profile
Re: Crash Error, windows sfml game
« Reply #7 on: August 01, 2014, 11:01:03 pm »
Thank you for the help / encouragement.
It's like an author writing a 200 pages book in a single chapter without using half of the language rules and using Latin and Latin derived language altogether
:'(
 I have gotten a better peice of code that should compile. The one i posted before compiled on MinGW. I am putting it in a cpp file this time  :)
Note the reason why I am posting the entire code is I cant figure out where the error is
« Last Edit: August 01, 2014, 11:23:20 pm by lordseanington »

Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
Re: Crash Error, windows sfml game
« Reply #8 on: August 01, 2014, 11:39:40 pm »
Thank you for the help / encouragement.
It's like an author writing a 200 pages book in a single chapter without using half of the language rules and using Latin and Latin derived language altogether
:'(
 I have gotten a better peice of code that should compile. The one i posted before compiled on MinGW. I am putting it in a cpp file this time  :)
Note the reason why I am posting the entire code is I cant figure out where the error is
It's kinda harsh to say but that's the truth. I've just tried compiling your code:
  • declaring a inventory struct with a matrix named inventory inside of it doesn't make any sense.
  • there was a typo if(stuff & stuff) instead of (stuff && stuff)
  • a lot of warnings because there are no explicit int<->float conversions
  • bad code in general

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Crash Error, windows sfml game
« Reply #9 on: August 01, 2014, 11:42:31 pm »
My IDE still complains about almost everything in your code  :P
To get it to compile, I had to rename the struct Inventory instead of inventory. It didn't like the overuse of the identical variable name (and nor do I). Just saying this so that you could change it so that others can compile it without having to change that. There are many other things that will get flagged and marked but you can fix those (now! ;D )

You resize vectors and then directly access them with []. I think in this case, pushing would be safer. I say this because that's where the program is crashing - because of your misuse of that.
Vector indices begin at zero. You resized it the block vector to two and tried to access element [1] and element [2] where it should be element
  • and element [1]. This seems to be the thing that keeps happening; you're accessing elements that don't exist.


@Strelok, I agree. Does MinGW not give any of this information?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
Re: Crash Error, windows sfml game
« Reply #10 on: August 01, 2014, 11:52:49 pm »
Quote
@Strelok, I agree. Does MinGW not give any of this information?
I don't know honestly, but @lordseanington you should always compile with -Wall and -Wextra arguments
And please, don't write
 bool variable = 0;
ever again :'(
« Last Edit: August 01, 2014, 11:58:48 pm by Strelok »

lordseanington

  • Jr. Member
  • **
  • Posts: 86
  • Coffee is the blood of a coder.
    • View Profile
Re: Crash Error, windows sfml game
« Reply #11 on: August 02, 2014, 07:10:57 am »
Your right. I am a generally bad coder.

 :'(

that's why i m at this forum. To get help. By the way, i know the vectors are an isue. It crashed before i added them to the code. I alsoam not using an ide. I am using MinGW with notepad.

@Strelok, I agree. Does MinGW not give any of this information?

No
« Last Edit: August 02, 2014, 07:14:48 am by lordseanington »

Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
Re: Crash Error, windows sfml game
« Reply #12 on: August 02, 2014, 07:31:49 am »
You should try notepad++, it's a really solid source editor, with lots of useful plugins, syntax highlighting and all that cool stuff :)

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Crash Error, windows sfml game
« Reply #13 on: August 02, 2014, 10:20:40 am »
By the way, i know the vectors are an isue. It crashed before i added them to the code.
If you already knew, why didn't you fix that problem already?

lordseanington

  • Jr. Member
  • **
  • Posts: 86
  • Coffee is the blood of a coder.
    • View Profile
Re: Crash Error, windows sfml game
« Reply #14 on: August 03, 2014, 12:48:21 am »
I havnt had time i just added them recently.

 

anything