2017-03-22 17 views
0

JSON 및 PNG 파일에서 그래픽을로드 할 수있는 게임 엔진을 만들고 있습니다. 유일한 문제는 한 번에 여러 스프라이트를로드하는 방법을 알아낼 수 없다는 것입니다. 나는 여러 가지 접근법을 시도해 왔지만 이것은 내가 생각하는 것입니다. 컴파일하는 동안,하지만, 단말기는 나에게 오류를 제공합니다어설 션`fp_! = 0 '실패 (RapidJSON)

Test: /home/thomas/Documents/project-repos/game/rapidjson/filereadstream.h:45: rapidjson::FileReadStream::FileReadStream(FILE*, char*, std::size_t): Assertion `fp_ != 0' failed.

Aborted (core dumped)

나는 이것이 일반적으로 JSON 파일을 찾을 수 없음을 의미 알고,하지만 난 모든 작업 디렉토리에 있는지 확인했습니다.

MAIN.CPP :

#include <iostream> 
#include <unistd.h> 
#include <string> 
#include <SFML/Graphics.hpp> 

// The universal include file. 
#include "include.hpp" 

int main() { 

    // Declaration of the window. 
    sf::RenderWindow window(sf::VideoMode(640, 320), "Test Game", sf::Style::Close); 

    // Getting the background texture. 
    sf::Texture bkgd; 
    if(!bkgd.loadFromFile("../textures/generic.png")) { 
     ErrorLog("1", "../game.log"); 
     window.close(); 
    } 

    // Getting the map textures. 
    sf::Texture map; 
    if(!map.loadFromFile("../textures/textures.png")) { 
     ErrorLog("2", "../game.log"); 
     window.close(); 
    } 

    // Creating the background sprite. 
    sf::Sprite bkgdSp; 
    bkgdSp.setTexture(bkgd); 

    // Local variable to check if fullscreen is activated. 
    int fullScreen = 0; 

    // The window loop. 
    while(window.isOpen()) { 

     // Local variable that keeps the current window size. 
     sf::Vector2f winSize(window.getSize()); 

     // Local variables storing the window ratios. 
     float scaleX = winSize.x/640; 
     float scaleY = winSize.y/320; 

     // Local variable to store the number of map tiles. 
     int mapNum = LoadNumber("../locations.json"); 

     // Local array for the sprites. 
     sf::Sprite mapSp; 

     // Local array for the tiles. 
     tile mapTile; 

     // Load tile information. 
     mapTile = LoadTile("../locations.json", 1); 

     // Texture rectangle for the current sprite. 
     sf::IntRect mapRect(GetFileCoordinates(mapTile.type).x, GetFileCoordinates(mapTile.type).y, GetFileWidth(mapTile.type), GetFileHeight(mapTile.type)); 

     // Setting the texture for the current sprite. 
     mapSp.setTexture(map); 
     mapSp.setTextureRect(mapRect); 

     // Scaling and repositioning the current sprite. 
     mapSp.setScale(scaleX, scaleY); 
     mapSp.setPosition(mapTile.x * scaleX, mapTile.y * scaleY); 


     // The event loop (only used to close the window. 
     sf::Event event; 
     while(window.pollEvent(event)) { 
      if(event.type == sf::Event::Closed) { 
       window.close(); 
      } 
     } 

     // Change window size if F1 is pressed. 
     if(sf::Keyboard::isKeyPressed(sf::Keyboard::F1)) { 
      if(fullScreen == 0) { 
       window.create(sf::VideoMode(1280, 640), "Test Game", sf::Style::Close); 
       fullScreen = 1; 
      }else if(fullScreen == 1) { 
       window.create(sf::VideoMode(640, 320), "Test Game", sf::Style::Fullscreen); 
       fullScreen = 2; 
      }else if(fullScreen == 2) { 
       window.create(sf::VideoMode(640, 320), "Test Game", sf::Style::Close); 
       fullScreen = 0; 
      } 
     } 

     // Resizes the background to fit the window size. 
     bkgdSp.setScale(scaleX, scaleY); 

     // Drawing and displaying the window. 
     window.clear(); 

     window.draw(bkgdSp); 
     window.draw(mapSp); 

     window.display(); 

     usleep(7000); 
    } 

    return 0; 
} 

load.cpp :

#include <iostream> 
#include <SFML/System.hpp> 
#include "rapidjson/document.h" 
#include "rapidjson/filereadstream.h" 

// The universal include file. 
#include "include.hpp" 

using namespace rapidjson; 

// Function that loads tile values from the locations file. 
tile LoadTile(std::string fileName, int number) { 
    tile output; 

    FILE* file = fopen(fileName.c_str(), "r"); 

    char buffer[10000]; 
    FileReadStream stream(file, buffer, 10000); 

    Document doc; 
    doc.ParseStream(stream); 

    std::string input = std::to_string(number); 

    Value& tileNumber = doc[input.c_str()]; 

    output.x = tileNumber[0]["x"].GetInt(); 
    output.y = tileNumber[1]["y"].GetInt(); 
    output.type = tileNumber[2]["type"].GetString(); 

    return output; 
} 

// Function that gets the current tile type's x and y coordinates. 
sf::Vector2f GetFileCoordinates(std::string type) { 
    sf::Vector2f output; 

    FILE* file = fopen("../textures.json", "r"); 

    char buffer[10000]; 
    FileReadStream stream(file, buffer, 10000); 

    Document doc; 
    doc.ParseStream(stream); 

    Value& typeNumber = doc[type.c_str()]; 

    output.x = typeNumber[0]["x"].GetInt(); 
    output.y = typeNumber[1]["y"].GetInt(); 

    return output; 
} 

// Function that gets the number of objects in the current map file. 
int LoadNumber(std::string fileName) { 
    FILE* file = fopen(fileName.c_str(), "r"); 

    char buffer[10000]; 
    FileReadStream stream(file, buffer, 10000); 

    Document doc; 
    doc.ParseStream(stream); 

    int objCount = 1; 
    std::string strCount = std::to_string(objCount); 
    while(doc.HasMember(strCount.c_str())) { 
     objCount++; 
     strCount = std::to_string(objCount); 
    } 

    return objCount - 1; 
} 

// Function that gets the current tile type's width. 
int GetFileWidth(std::string type) { 
    int output; 

    FILE* file = fopen("../textures.json", "r"); 

    char buffer[10000]; 
    FileReadStream stream(file, buffer, 10000); 

    Document doc; 
    doc.ParseStream(stream); 

    Value& typeNumber = doc[type.c_str()]; 

    output = typeNumber[2]["width"].GetInt(); 

    return output; 
} 

// Function that gets the current tile type's height. 
int GetFileHeight(std::string type) { 
    int output; 

    FILE* file = fopen("../textures.json", "r"); 

    char buffer[10000]; 
    FileReadStream stream(file, buffer, 10000); 

    Document doc; 
    doc.ParseStream(stream); 

    Value& typeNumber = doc[type.c_str()]; 

    output = typeNumber[3]["height"].GetInt(); 

    return output; 
} 

include.hpp :

여기

내 코드입니다

locations.json :

{ 
    "1": [{ 
     "x": 32 
    }, { 
     "y": 32 
    }, { 
     "type": "water_c" 
    }], 

    "2": [{ 
     "x": 32 
    }, { 
     "y": 64 
    }, { 
     "type": "dirt_c" 
    }] 
} 

textures.json :

{ 
    "grass_c": [{ 
     "x": 0 
    }, { 
     "y": 0 
    }, { 
     "width": 32 
    }, { 
     "height": 32 
    }], 

    "water_c": [{ 
     "x": 32 
    }, { 
     "y": 0 
    }, { 
     "width": 32 
    }, { 
     "height": 32 
    }], 

    "sand_c": [{ 
     "x": 64 
    }, { 
     "y": 0 
    }, { 
     "width": 32 
    }, { 
     "height": 32 
    }], 

    "dirt_c": [{ 
     "x": 96 
    }, { 
     "y": 0 
    }, { 
     "width": 32 
    }, { 
     "height": 32 
    }], 

    "wood_c": [{ 
     "x": 128 
    }, { 
     "y": 0 
    }, { 
     "width": 32 
    }, { 
     "height": 32 
    }], 

    "brick_c": [{ 
     "x": 160 
    }, { 
     "y": 0 
    }, { 
     "width": 32 
    }, { 
     "height": 32 
    }] 
} 

"오류 로그()"에 대한 언급은 이미 테스트되었습니다 다른 파일에 있습니다. 누구나 도움이된다면 Xubuntu 16.10에서 CMake (gcc)로 컴파일하십시오. 감사.

편집

내 JSON의 모든 기능에 새로운 코드를 추가했습니다 :

FILE* file = fopen("../textures.json", "r"); 
if(file == 0) { 
    std::cout << "GetFileHeight failed to load the file." << std::endl; 
} 

를 ... 그것은 모든 기능에 맞게 변경. "GetFileHeight"이 오류를 일으키는 것 같습니다. 파일을 열어 다시 읽지 전에 파일을 닫지 않는 것 같아요? 나는 잘 모르겠다.

+0

"컴파일 중에 터미널에서 다음과 같은 오류가 발생합니다. ..."컴파일을하면 코어 덤프가 발생합니다. –

+0

그래, 좀 더 구체적이어야 했어. 그것은 나에게 컴파일러 에러를주지는 않는다. 실행 파일이 생성 된 후에 에러가 난다. – 123TQB

답변

1

귀하의 오류로 인해 실패 코드의이 부분 (또는 반복 중 하나)에 일어날 것 같다

FILE* file = fopen(fileName.c_str(), "r"); 

char buffer[10000]; 
FileReadStream stream(file, buffer, 10000); 

당신은 항상 확인해야합니다, fopen() 실제로 파일을 열 수 있는지 여부. 실패한 경우 file0 (또는 정확하게는 NULL)으로 설정되며 FileReadStream의 생성자에서 어설 션을 발생시킵니다. 이는 NULL을 전달하기 때문에 유효한 파일 포인터 (FILE*)가 아닙니다.