What is my error?

Pages: 12
Soooo I am using SFML and I am trying to load sprites. But, when I tried to run it, it crashed. I am actually clueless at why it will not work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
  #include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <fstream>
#include <vector>
#include <limits>
#include <string>
#include <stdio.h>
#include <windows.h>
#include "connector.hpp"
using namespace std;
using namespace sf;

//Declarations

//Global variables
const int scl = 16;         //the images will be placed vector2f(image[][].x * scl, image[][].y * scl)
Sprite Bomberman;
Sprite Bomb;
Texture Entities;
Texture Barrier;
//Classes

//Structures
struct maps{
    int width;
    int height;

//    Texture Other;
    vector<vector<Sprite> > all;
    vector<vector<bool> > canGo;
    maps(int width_, int height_) : all(width_ * height_) {width = width_; height = height_;}
};
//Functions
std::string getexepath()
{
  char result[ MAX_PATH ];
  GetModuleFileName( NULL, result, MAX_PATH );
  return string(result, 1);
}
ifstream& GotoLine(std::ifstream& file, unsigned int num){
    file.seekg(std::ios::beg);
    for(unsigned int i=0; i < num - 1; ++i){
        file.ignore(std::numeric_limits<std::streamsize>::max(),'\n');
    }
    return file;
}
maps getMapData(string fileName){
    const string DRIVE_LETTER = getexepath();
    ////////////////////////////////
    ///Line 1 = comment
    ///Line 2 = comment
    ///Line 3 = int width
    ///Line 4 = comment
    ///Line 5 = int height
    ///Line 6 = Dirt and wall images
    ///Line 7 = Entities images
    ///Line 8 = Dirt array
    ///Line 9 = Entities array
    ///Line 10 = Walls array
    ///////////////////////////////
    string path = (DRIVE_LETTER + ":/C++ Projects/Bomberman/DATA/mapData/" + fileName);
    //////////////////////////////
    ifstream mapFl(path.c_str());
    if (!mapFl){
        cout << "ERR_FILE.NOTFOUND" << endl;
        cout << path << endl;
    }
    string readline = " ";
    GotoLine(mapFl, 3);
    mapFl >> readline;
    //this is 22 for this case
    const int width_ = atoi(readline.c_str());
    GotoLine(mapFl, 5);
    mapFl >> readline;
    //this is 20 for this case
    const int height_ = atoi(readline.c_str());
    maps rtrn(width_, height_);
    cout << "1" << endl << width_ << endl << height_ << endl << endl;
    GotoLine(mapFl, 6);
    mapFl >> readline;
    string idlePath = readline;

    GotoLine(mapFl, 7);
    mapFl >> readline;
    string ePath = readline;

    GotoLine(mapFl, 8);
    mapFl >> readline;
    string dirtAr = readline;

    GotoLine(mapFl, 9);
    mapFl >> readline;
    string entityAr = readline;

    GotoLine(mapFl, 10);
    mapFl >> readline;
    string wallAr = readline;
    ////////////////////////////////
    mapFl.close();
    path = (DRIVE_LETTER + ":/C++ Projects/Bomberman" + idlePath);
    if(!Barrier.loadFromFile(path)){
        cout << "ERROR_CANNOT_OPEN_FILE:" << idlePath;
    }
    path = (DRIVE_LETTER + ":/C++ Projects/Bomberman" + ePath);
    if(!Entities.loadFromFile(path)){
        cout << "ERROR_CANNOT_OPEN_FILE:" << ePath;
    }
    path = (DRIVE_LETTER + ":/C++ Projects/Bomberman" + dirtAr);
    mapFl.open(path.c_str());
    vector<vector<char> > result(width_, vector<char>(height_));
    char arrayResult = 0;
    for(int j = 0; j != height_; j++){
        for(int i = 0; i != width_; i++){
            if(!mapFl.good()){
                cout << "ERROR WITH FILE" << endl;
                break;
            }
            arrayResult = mapFl.get();
            (result[i])[j] = arrayResult;
//            cout << (result[i])[j];
        }
    }
    for (unsigned int i = 0; i < rtrn.canGo.size(); ++i) {
        for (unsigned int j = 0; j < rtrn.canGo[i].size(); ++j) {
            bool temp = false;
            if(result[i][j] == 1){
                temp = true;
            }
            rtrn.canGo[i][j] = temp;
        }
    }
    //cout << "ERR";
//    int i = 0;
//    int j = 0;
//    for (auto&& v : rtrn.all) {
//        for (auto&& sprite : v) {
//            sprite.setTexture(Barrier);
//            sprite.setPosition(Vector2f(i * scl, j * scl));
//            sprite.setTextureRect(getRect(false, true, false, 1, 0));
//            j++;
//        }
//        i++;
//    }
//    for (unsigned int i = 0; i != width_; i++) {
//            for(unsigned int j = 0; j != height_; j++)
//                rtrn.all[i][j].setTexture(Barrier);
//                rtrn.all[i][j].setPosition(Vector2f(i * scl, j * scl));
//                rtrn.all[i][j].setTextureRect(getRect(false, true, false, 1, 0));
//         //       cout << endl << "K" << endl << int(index / width_) * scl << endl << (index - (int((index) / width_) * width_)) * scl << endl;
//    }
    return rtrn;
}
//More Global variables
//Main
int main()
{
    maps LM_MAIN = getMapData("Lvl1.map");
    int i = 0;
    int j = 0;
    for (auto&& v : LM_MAIN.all) {
        for (auto&& sprite : v) {
            sprite.setTexture(Barrier);
            sprite.setPosition(Vector2f(i * scl, j * scl));
            sprite.setTextureRect(getRect(false, true, false, 1, 0));
            j++;
        }
        i++;
    }
   // cout << LM_MAIN.width << endl << LM_MAIN.height << endl;
    RenderWindow window(sf::VideoMode(LM_MAIN.width * scl, (LM_MAIN.height * scl) + 20), "Game!");
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        //cout << "HERE" << endl;
        window.clear();
        i = 0;
        j = 0;
        for (auto&& v : LM_MAIN.all) {
            for (auto&& sprite : v) {
                window.draw(sprite);
                j++;
            }
            i++;
        }
        window.display();
    }
}

Thanks!
If you need the files Connector.hpp or getRect.cpp, please tell me!
Last edited on
http://www.cplusplus.com/articles/jLzyhbRD/

Volume is not precision You need to be precise and informative. This end is not served by simply dumping huge volumes of code or data into a help request.
When asking about code Don't ask others to debug your broken code without giving a hint what sort of problem they should be searching for. Posting a few hundred lines of code, saying "it doesn't work", will get you ignored. Posting a dozen lines of code, saying "after line 7 I was expecting to see <x>, but <y> occurred instead" is much more likely to get you a response.

At least figure out where it's crashing. No one is going to read hundreds of lines of code looking for invalid accesses or whatnot.
Last edited on
Ok, I rounded it down to some logic, after some debugging. Look at this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
struct maps{
    int width;
    int height;

//    Texture Other;
    vector<vector<Sprite> > all;
    vector<vector<bool> > canGo;
    maps(int width_, int height_) : all(width_ * height_) {width = width_; height = height_;}
};

maps getMapData(){
    maps rtrn(width_, height_);
    int i = 0;
    int j = 0;
    for (i; i != width_; i++) {
        vector<Sprite> row(width_);
        for (j; j != height_; j++) {
            Sprite sprite;
            sprite.setTexture(Barrier);
            sprite.setPosition(Vector2f(i * scl, j * scl));
            sprite.setTextureRect(getRect(false, true, false, 1, 0));
            row.push_back(sprite);
            cout << endl << i << endl << j << endl;
        }
        rtrn.all.push_back(row);
    }
I can't really be bothered to wade through all of your code, but using global SFML objects is bound to lead to problems. Global initialization order matters with SFML and the only way to portably ensure the right SFML objects get initialized first is not to have your own global SFML objects.
what do you mean? Please elaborate.
I meant: Don't use global SFML objects.

If you want more details, you might use the search engine of your choice to look up "global initialization order C++" and delve into the resulting links.
Ok, and one more question, how may one go by looping through a 2d vector? is this the proper way to do it?
Snippet:
1
2
3
4
5
6
7
8
9
10
11
12
for (i; i != width_; i++) {
        vector<Sprite> row(width_);
        for (j; j != height_; j++) {
            Sprite sprite;
            sprite.setTexture(Barrier);
            sprite.setPosition(Vector2f(i * scl, j * scl));
            sprite.setTextureRect(getRect(false, true, false, 1, 0));
            row.push_back(sprite);
            cout << endl << i << endl << j << endl;
        }
        rtrn.all.push_back(row);
    }

the rtrn.all is a 2d vector, and the width_ = 22 and the height_ = 20

Thanks!
Why do you use the move semantic in the range based loop?

for (auto&& v : LM_MAIN.all) { // Why move?

What effect does it have?

is this the proper way to do it?
Yes, but since you use push_back(...) on line 8 you shouldn't allocate width_ on line 2.
> for (auto&& v : LM_MAIN.all) {

v is a forwarding reference;
auto&& maintains the const-ness, rvalue-ness, etc. of the result of the iterator dereference.

1
2
3
for(auto&& x: f()) {
  // x is a forwarding reference; this is the safest way to use range for loops
}

http://en.cppreference.com/w/cpp/language/reference
Last edited on
How is it safer than, for example, const auto &?
> How is it safer than, for example, const auto &?

Repeat: auto&& maintains the const-ness, rvalue-ness, etc. of the result of the iterator dereference.

ie. with for( auto&& x: f(t) ),
x is a forwarding reference; we can forward an lvalue as an lvalue and an rvalue as an rvalue.
see: http://en.cppreference.com/w/cpp/utility/forward

This is particularly important in generic code where the callable object may be overloaded or specialised.

The forwarding problem (2002 proposal):
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1385.htm
That doesn't answer how it's safer, though. At best it answers how it's more general.
auto&& is safer because it is less general (it distinguishes between lvalues and rvalues, const and non-const)
const auto& (uniform treatment for all value categories, const and non-const) is more general

From http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1385.htm
Assuming a forwarding function f(a1, a2, ..., an) that calls g(a1, a2, ..., an) ...
...
... valid uses of f that aren't valid uses of g should not invoke undefined behavior.
...
Summary:
...
Const reference ... Fails for f(lvalue) to g(A &)
...
Const reference + const_cast ... Works, but very unsafe
...


This too is worth a careful read: it helps one understand what the perfect forwarding problem that existed prior to C++11 is; and how forwarding references provide a 'perfect' solution to the problem.
http://thbecker.net/articles/rvalue_references/section_07.html
1
2
3
for (i; i != width_; i++) {
   vector<Sprite> row(width_);
   for (j; j != height_; j++)
the inner loop would only execute one time, as `j' value will remain in `height_'
I'm sorry, I don't follow. Are you saying that this
1
2
3
for (auto &&x : xs){
    const_cast<T &>(x).foo();
}
is safer than this
1
2
3
for (const auto &x : xs){
    const_cast<T &>(x).foo();
}
even if referencing the iterator gives a const T &?
If that's not what you're saying, can you give an example where for (auto &&x : xs){ /*A*/ } would not cause UD while for (const auto &x : xs){ /*A*/ } would, where A and the types involved are the same in both cases?
> even if referencing the iterator gives a const T &?

Dereferencing a generic iterator is not guaranteed to always yield some const T&

An example where the types involved may be identical, but forwarding references are essential for the code to be correct, safe and maintainable.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
template < typename FN, typename... T > void good( FN fn, T&&... t )
{
    for( auto&& x : fn( std::forward<T>(t)... ) ) // t and x are forwarding references
        std::forward< decltype( *fn( std::forward<T>(t)... ).begin() ) >(x).mfun( std::forward<T>(t)... ) ; 
        
    // 'perfect' (correct and safe, self-maintaining)
}

template < typename FN, typename... T  > void bad( FN fn, const T&... t  )
{
    for( const auto& x : fn(t...) ) // t is not a forwarding reference:
                                    //     what do you want to do here?
        x.mfun(t...) ; // x is not a forwarding reference:
                       //     what do you want to do here?

    // hint: nothing that you could do here would be scalable, correct and safe
}

http://coliru.stacked-crooked.com/a/fbfa76b51bb6d9bc
http://rextester.com/RCVK12339
@helios

Why have you got a const_cast in the forwarding reference version ?

Did you read the articles JLBorges provided? I think they explain the whole thing rather well.

openstd wrote:
Forwarding method-------------------------------------Problems------Notes

#7: Rvalue reference + modified argument deduction---None known---Perfect
Last edited on
JLBorges: I don't know why in your example you're changing things unrelated to the loop variable.
1
2
3
4
5
6
template < typename FN, typename... T >
void unknown( FN fn, T&&... t )
{
    for( const auto &x : fn( std::forward<T>(t)... ) )
        x.mfun( std::forward<T>(t)... ) ; 
}
What about this? Can you give example values of FN and T that make this code unsafe?

TheIdeasMan: All the article says about safety is that const_cast is unsafe. I'm asking why usage of const auto & necessitates usage of const_cast, since JLBorges is arguing that const auto & is less safe than auto && and hasn't presented any other reason why that might be the case.
Last edited on
1
2
3
4
5
6
> template < typename FN, typename... T >
> void unknown( FN fn, T&&... t )
> {
>    for( const auto &x : fn( std::forward<T>(t)... ) )
>        x.mfun( std::forward<T>(t)... ) ; 
> }

> What about this? Can you give example values of FN and T that make this code unsafe?

The same example as earlier would suffice to demonstrate that this is also bad (incorrect, unsafe).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template < typename FN, typename... T >
void also_bad( FN fn, T&&... t )
{
    for( const auto &x : fn( std::forward<T>(t)... ) )
        x.mfun( std::forward<T>(t)... ) ; 
}

int main()
{
    // ...    
    std::cout << "\n\nalso_bad: for( const auto& x : ... \n--------------------------------\n" ;
    also_bad( non_const_lvalue ) ;
    also_bad( const_lvalue ) ;
    also_bad( rvalue ) ;
}

...

also_bad: for( const auto& x : ...
--------------------------------
return non const lvalue -- call implementation for const lvalue
return const lvalue -- call implementation for const lvalue
return rvalue -- call implementation for const lvalue


Things could be worse: for instance, a more involved example could have one or more of the arguments in the argument pack T&&... t be incorrectly forwarded control variables of a range-for loop written by a programmer who believed that for( const auto& x : ... in generic code is safe.

I do not have anything more to add to this topic.
Your example is completely meaningless. What does any of this return: non_const_lvalue(), const_lvalue(), rvalue()?

For a programmer, you have some serious issues communicating ideas. I have noticed before that you seem to assume that others can read your mind and infer your meaning from half-statements, but this is just ridiculous. You're literally saying nothing at this point.
I'm not trolling you. I'm honestly asking why you say what you say, and you just keep piling up non-information.
Last edited on
Pages: 12