2017-08-13 11 views
-6

私はちょうど六角形を使って簡単なゲームを作ろうとしています。しかし、std :: vectorは何らかの理由で動作していません。それはリストを作成するはずですが、それはちょうどNULLですか?そのorigionalyがするべきことはトラックを作成するためにトラパゾイドを追加することです。その点から、そのほとんどがカメラを動かす。私は現在、SFMLという名前のアドオンを使用しており、主にOpenGLのようなイメージを作成するために使用されています。このリストを作成するにはどうすればいいですか

/////////////////////////////////////////////////////////////////////////////////////////////////////////// 
#include <SFML\Graphics.hpp>   
#include <iostream> 
/////////////////////////////////////////////////////////////////////////////////////////////////////////// 
using namespace sf; 

int width = 1024; 
int height = 768; 
char title[] = "Racing"; 

int roadW = 2000; 
int segL = 200;    //segment length 
float camD = 0.84;   //camera debth 

int update(RenderWindow &w); 
void drawQuad(RenderWindow &w, Color c, int x1, int y1, int w1, int x2, int y2, int w2); 

/////////////////////////////////////////////////////////////////////////////////////////////////////////// 
struct Line 
{ 
    float x, y, z;   //segment length 
    float X, Y, W;   //screen cordinates 
    float scale; 

    Line() { x = y = z = 0; } 

    //from world to screen coordinates 
    void project(int camx, int camy, int camz) 
    { 
     scale = camD/(z - camz); 
     X = (1 + scale*(x - camx))*width/2; 
     Y = (1 - scale*(y - camx))*height/2; 
     W = scale * roadW * width/2; 

    } 
}; 


std::vector<Line> lines; //main track 
int N = lines.size(); 

/////////////////////////////////////////////////////////////////////////////////////////////////////////// 
int main() 
{ 
    std::cout << "Starting up... \n"; 
    std::cout << "Creating RenderWindow...(VideoMode(" << width << "," << height << "), " << title << "\n"; 
    RenderWindow window(VideoMode(width, height), title); 
    window.setFramerateLimit(60); 

    std::cout << "Creating std::vector<Lines> lines... \n"; 

    for (int i = 0; i < 1600; i++) 
    { 
     Line line; 
     line.z = i*segL; 
     lines.push_back(line); 
    } 

    std::cout << "Total Length[" << N << "]\n"; 

    std::cout << "\nRunning...\n"; 
    while (window.isOpen()) 
    { 
     if (update(window) != 0) return 0; 
    } 

    return 0; 
} 

int update(RenderWindow &w) 
{ 
    ///│LOGIC//// 
    ///└EVENTS/// 
    Event e; 
    while (w.pollEvent(e)) 
    { 
     if (e.type == Event::Closed) return 1; 
    } 

    ///│RENDER/// 
    ///├CLEAR//// 
    ///├ROAD///// 
    ///└CLEAR//// 
    w.clear(); 

    for (int n = 0; n < 300; n++) 
    { 
     Line &l = lines[n%N]; 
     l.project(0, 1500, 0); 

     Color grass = (n/3) % 2 ? Color(16, 200, 16) : Color(0, 154, 0); 
     Color rumble = (n/3) % 2 ? Color(255, 255, 2555) : Color(0, 0, 0); 
     Color road = (n/3) % 2 ? Color(107, 107, 107) : Color(105, 105, 105); 

     Line p = lines[(n - 1) % N]; 

     drawQuad(w, grass, 0, p.Y, width, 0, l.Y, width); 
     drawQuad(w, rumble, p.X, p.Y, p.W*1.2, l.X, l.Y, l.W*1.2); 
     drawQuad(w, road, p.X, p.Y, p.W, l.X, l.Y, l.W); 
    } 

    w.display(); 

    return 0; 
} 

void drawQuad(RenderWindow &w, Color c, int x1, int y1, int w1, int x2, int y2, int w2) 
{ 
    ConvexShape shape(4); 
    shape.setFillColor(c); 
    shape.setPoint(0, Vector2f(x1 - w1, y1)); 
    shape.setPoint(1, Vector2f(x2 - w2, y2)); 
    shape.setPoint(2, Vector2f(x2 + w2, y2)); 
    shape.setPoint(3, Vector2f(x1 + w1, y1)); 

    w.draw(shape); 
} 

おかげ -Logan

+2

あなたの 'lines'はポインタではないので' NULL'にすることはできません。この 'std :: cout <<"の長さ["<< N <<"] \ n ";"を参照している場合、 'N'を初期化する場所を見てください。 –

+0

OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO –

答えて

0

あなたはそれが更新さを呼び出したときにコードの先頭に、それは行数を取得しますないのでどうやら、Nはゼロの略!

関連する問題