- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一个可以从 JSON 和 PNG 文件加载图形的游戏引擎,并且我取得了一些实际进展。唯一的问题是,我不知道如何一次加载多个 Sprite 。我尝试了很多不同的方法,但这是我认为可行的方法。但是,在编译时,终端给我这个错误:
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:
#include <iostream>
#include <SFML/Graphics.hpp>
#include "rapidjson/document.h"
// Public struct declaring the "tile" data type. Uses the same characteristics as the tile locations file.
struct tile {
int x;
int y;
std::string type;
};
// In load.cpp.
tile LoadTile(std::string fileName, int number);
sf::Vector2f GetFileCoordinates(std::string type);
int LoadNumber(std::string fileName);
int GetFileWidth(std::string type);
int GetFileHeight(std::string type);
// In log.cpp
void ErrorLog(std::string code, std::string fileName);
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
}]
}
“ErrorLog()”的任何提及都在另一个文件中,该文件已经过测试。如果有人可以提供帮助,请在 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”似乎是导致错误的原因。我认为这可能是关于打开文件,而不是在再次阅读之前关闭它?我不确定。
最佳答案
您的错误似乎是由于您的代码的这一部分(或其中一个重复)失败而发生的:
FILE* file = fopen(fileName.c_str(), "r");
char buffer[10000];
FileReadStream stream(file, buffer, 10000);
您应该始终检查 fopen()
是否真的可以打开文件。如果失败,file
将被设置为 0
(或准确地说是 NULL
),这将触发 的构造函数中的断言>FileReadStream
,因为您传递的是 NULL
,它不是有效的文件指针 (FILE*
)。
关于c++ - 断言 `fp_ != 0' 失败 (RapidJSON),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42941783/
我需要一个构造 rapidjson::Document 并返回的函数。但是当我用这个原型(prototype)写一个函数时: rapidjson::Document progressToJson(co
我想从 JSON 字符串创建一个 rapidjson::Value,例如 [1,2,3]。注意:这不是一个完整的 JSON 对象,它只是一个 JSON 数组。在 Java 中,我可以使用 object
根据 the tutorial : Each JSON value is stored in a type called Value. A Document, representing the DOM
如何将 rapidjson 数组迭代器转换为 rapidjson::value? 我不想要关注如何获取快速 json 数组的内容或如何遍历它的答案。 我也很清楚我可以使用 rapidjson 文档中的
似乎文档也可以用作参数 void test(Value value); 而Document和Value都可以具有子值,它们之间有什么区别? 最佳答案 首先,test函数不应该编译,因为Value不支持
当我使用 rapidjson::Value 方法询问 GetType() 的类型时,它只返回以下类型: //! Type of JSON value enum Type { kNullType
我希望能够使用 RapidJSON 创建以下 JSON 输出 { "year": 2013, "league": "national", "teams": [
我想使用 Rapidjson 将嵌套结构序列化为 JSON,并且还希望能够单独序列化每个对象,因此任何实现 ToJson 的类都可以序列化为 JSON 字符串。 在以下代码中,Car 有一个 Whee
我尝试将 rapidjson::Document 对象作为函数参数传递: std::string json_to_string(rapidjson::Document jmsg) { // Con
我正在使用 RapidJSON 为我的游戏解析一些配置文件( Material 定义、组件等)。但是,我很好奇是否可以将 RapidJSON 配置为仅解析 JSON 文档中的第一层。想象一下: {
我有两个由 Rapidjson 库解析的 json 字符串。 JSON 1: { "jKey1":{ "jVal1Key1":{
我正在尝试使用 RapidJSON 解析一个 JSON 文件,其中有数千个像这样的对象 "Amateur Auteur": { "layout": "normal", "name": "Amateur
我有一个包含嵌套对象和对象数组的 Json 记录,这些字段中的键包含空格,我想将所有空格更改为 _,所以我必须迭代 json 对象中的所有键. 我的想法是编写深度优先搜索以使用 ConstMember
我搜索了这个特定错误,但没有找到任何相关问题。 我正在尝试使用 Rapidjson 来解析我的 C++ 项目中的 .json 文件,这就是我的做法(遵循 RapidJSON tutorial 的说明)
想知道是否可以直接从中提取 rapidjson::Value 的名称。 例如,假设我们有以下 JSON 数据: { "name": [ { /*some data*/
我正在尝试使用 RapidJson 在 C++ 中解析 json 数据。我不知道我哪里做错了,但我的断言失败了。当我尝试调试它的显示 sigabrt 时它运行断言行。社区 我感谢您的见解。感谢您回答这
我有以下代码将数据添加到 rapidjson::Document,声明如下: rapidjson::Document rest; rest.SetObject(); 在循环中。 Value v(val
我有一个像这样的 JSON 字符串: {"callCommand":{"command":"car","floor":"2","landing":"front"}} 现在,我想检查是否有名为 comm
我使用 C++ 生成了以下 JSON: { "ProfileID": "DUO1", "ProfileName": "Sample" } 用于生成的代码是: string jsonDa
我正在尝试使用 rapidjson 创建一个 json 文档,但我不知道如何复制以下文档的一部分,特别是以“allocations”开头的嵌套对象,用于我所做的其他元素 Value valObject
我是一名优秀的程序员,十分优秀!