SFML Error with vertexArray

I was wanting to make a 3d Terrain generator, but when I compiled the code below, it just crashed.
I'm using CodeBlocks(MINGW) and SFML 2.1;

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
#include <iostream>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <cmath>
using namespace std;
using namespace sf;
Vector2f landPos[20][20];
int main()
{
    for(int i; i != 20; i++){
        for(int j; j!= 20; j++){
            landPos[i][j].y = i * 5;
            landPos[i][j].x = j * 5;
        }
    }
    RenderWindow Mainwindow(VideoMode(400, 400), "Terrain");
    VertexArray land(TrianglesStrip, 40);
    for(int i; i != 20; i++){
        for(int j; j!= 20; j++){
            land[i*j].position = landPos[i][j];
        }
    }
    while(Mainwindow.isOpen()){
        Event e;
        while(Mainwindow.pollEvent(e)){
            if(e.type == Event::Closed){
                Mainwindow.close();
            }
        }
        Mainwindow.clear(Color(0, 0, 0));
        Mainwindow.draw(land);
        Mainwindow.display();

    }
}

Thanks for your help
C++ does not implicitly set declared raw types to 0.

If you just say,
 
int i;

i will have a junk value in it, and you shouldn't use it. (It's technically "undefined behavior")

You must set a value to i and j in lines 11-12 and 19-20

1
2
3
4
5
6
7
    for(int i = 0 ; i < 20; i++){
        for(int j = 0; j < 20; j++){
            ...

    for(int i = 0; i < 20; i++){
        for(int j = 0; j < 20; j++){
            ...


Edit: I think you have a 2nd problem
http://www.sfml-dev.org/documentation/2.0/classsf_1_1VertexArray.php
Line 18, you set 40 to be the vertex count, but 20 * 20 is 400, which is much greater than 40.
You probably meant say 400 instead of 40.

I would not use magic numbers. Make a named constant and set it equal to 20.
Last edited on
Topic archived. No new replies allowed.