bool parse_bool(const char* json) { CE_ASSERT_NOT_NULL(json); switch (*json) { case 't': { json = next(json, 't'); json = next(json, 'r'); json = next(json, 'u'); json = next(json, 'e'); return true; } case 'f': { json = next(json, 'f'); json = next(json, 'a'); json = next(json, 'l'); json = next(json, 's'); json = next(json, 'e'); return false; } default: { CE_FATAL("Bad boolean"); return false; } } }
void parse_array(const char* json, JsonArray& array) { CE_ASSERT_NOT_NULL(json); if (*json == '[') { json = skip_spaces(++json); if (*json == ']') return; while (*json) { array::push_back(array, json); json = skip_value(json); json = skip_spaces(json); if (*json == ']') return; json = skip_spaces(json); } } CE_FATAL("Bad array"); }
void parse_array(const char* json, Array<const char*>& array) { CE_ASSERT_NOT_NULL(json); if (*json == '[') { json = next(json, '['); json = skip_spaces(json); if (*json == ']') { json = next(json, ']'); return; } while (*json) { array::push_back(array, json); json = skip_value(json); json = skip_spaces(json); if (*json == ']') { json = next(json, ']'); return; } json = next(json, ','); json = skip_spaces(json); } } CE_FATAL("Bad array"); }
bool parse_bool(const char* json) { CE_ENSURE(NULL != json); switch (*json) { case 't': json = next(json, 't'); json = next(json, 'r'); json = next(json, 'u'); json = next(json, 'e'); return true; case 'f': json = next(json, 'f'); json = next(json, 'a'); json = next(json, 'l'); json = next(json, 's'); json = next(json, 'e'); return false; default: CE_FATAL("Bad boolean"); return false; } }
static const char* skip_comments(const char* json) { CE_ASSERT_NOT_NULL(json); if (*json == '/') { ++json; if (*json == '/') { json = next(json, '/'); while (*json && *json != '\n') ++json; } else if (*json == '*') { ++json; while (*json && *json != '*') ++json; json = next(json, '*'); json = next(json, '/'); } else CE_FATAL("Bad comment"); } return json; }
void parse_string(const char* json, DynamicString& string) { CE_ASSERT_NOT_NULL(json); if (*json == '"') { while (*++json) { // Empty string if (*json == '"') { ++json; return; } else if (*json == '\\') { ++json; switch (*json) { case '"': string += '"'; break; case '\\': string += '\\'; break; case '/': string += '/'; break; case 'b': string += '\b'; break; case 'f': string += '\f'; break; case 'n': string += '\n'; break; case 'r': string += '\r'; break; case 't': string += '\t'; break; default: { CE_FATAL("Bad escape character"); break; } } } else { string += *json; } } } CE_FATAL("Bad string"); }
/// Returns the actor with the given @a name const PhysicsConfigActor* actor(const PhysicsConfigResource* pcr, StringId32 name) { const PhysicsConfigActor* begin = (PhysicsConfigActor*)((const char*)pcr + pcr->actors_offset); for (u32 i = 0; i < pcr->num_actors; ++i) { if (begin[i].name == name) return &begin[i]; } CE_FATAL("Actor not found"); return NULL; }
const PhysicsConfigShape* shape(const PhysicsConfigResource* pcr, StringId32 name) { const PhysicsConfigShape* begin = (PhysicsConfigShape*)((const char*)pcr + pcr->shapes_offset); for (u32 i = 0; i < pcr->num_shapes; ++i) { if (begin[i].name == name) return &begin[i]; } CE_FATAL("Shape not found"); return NULL; }
/// Returns the material with the given @a name const PhysicsConfigMaterial* material(const PhysicsConfigResource* pcr, StringId32 name) { const PhysicsConfigMaterial* begin = (PhysicsConfigMaterial*)((const char*)pcr + pcr->materials_offset); for (u32 i = 0; i < pcr->num_materials; ++i) { if (begin[i].name == name) return &begin[i]; } CE_FATAL("Material not found"); return NULL; }
UniformData* get_uniform_data_by_name(const MaterialResource* mr, StringId32 name) { for (u32 i = 0, n = mr->num_uniforms; i < n; ++i) { UniformData* data = get_uniform_data(mr, i); if (data->name == name) return data; } CE_FATAL("Unknown uniform"); return NULL; }
const PhysicsCollisionFilter* filter(const PhysicsConfigResource* pcr, StringId32 name) { const PhysicsCollisionFilter* begin = (PhysicsCollisionFilter*)((const char*)pcr + pcr->filters_offset); for (u32 i = 0; i < pcr->num_filters; ++i) { if (begin[i].name == name) return &begin[i]; } CE_FATAL("Filter not found"); return NULL; }
const GlyphData* get_glyph(const FontResource* fr, CodePoint cp) { CE_ASSERT(cp < fr->num_glyphs, "Index out of bounds"); const CodePoint* pts = (CodePoint*)&fr[1]; const GlyphData* data = (GlyphData*)(pts + fr->num_glyphs); // FIXME: Can do binary search for (u32 i = 0; i < fr->num_glyphs; ++i) { if (pts[i] == cp) return &data[i]; } CE_FATAL("Glyph not found"); return NULL; }
//-------------------------------------------------------------------------- uint32_t JSONElement::size() const { if (m_at == NULL) { return 0; } switch(json::type(m_at)) { case JSONType::NIL: { return 1; } case JSONType::OBJECT: { Map<DynamicString, const char*> object(default_allocator()); json::parse_object(m_at, object); return map::size(object); } case JSONType::ARRAY: { Array<const char*> array(default_allocator()); json::parse_array(m_at, array); return array::size(array); } case JSONType::STRING: { DynamicString string; json::parse_string(m_at, string); return string.length(); } case JSONType::NUMBER: { return 1; } case JSONType::BOOL: { return 1; } default: { CE_FATAL("Oops, unknown value type"); return 0; } } }
void parse_object(const char* json, Map<DynamicString, const char*>& object) { CE_ASSERT_NOT_NULL(json); if (*json == '{') { json = next(json, '{'); json = skip_spaces(json); if (*json == '}') { next(json, '}'); return; } while (*json) { TempAllocator256 ta; DynamicString key(ta); parse_string(json, key); json = skip_string(json); json = skip_spaces(json); json = next(json, ':'); json = skip_spaces(json); map::set(object, key, json); json = skip_value(json); json = skip_spaces(json); if (*json == '}') { next(json, '}'); return; } json = next(json, ','); json = skip_spaces(json); } } CE_FATAL("Bad object"); }
static const char* parse_key(const char* json, DynamicString& key) { CE_ASSERT_NOT_NULL(json); if (*json == '"') { parse_string(json, key); return skip_string(json); } while (true) { if (isspace(*json) || *json == '=' || *json == ':') return json; key += *json++; } CE_FATAL("Bad key"); return NULL; }
void parse_object(const char* json, JsonObject& object) { CE_ASSERT_NOT_NULL(json); if (*json == '{') { json = skip_spaces(++json); if (*json == '}') return; while (*json) { const char* key_begin = *json == '"' ? (json + 1) : json; TempAllocator256 ta; DynamicString key(ta); json = parse_key(json, key); FixedString fs_key(key_begin, key.length()); json = skip_spaces(json); json = next(json, (*json == '=') ? '=' : ':'); json = skip_spaces(json); map::set(object, fs_key, json); json = skip_value(json); json = skip_spaces(json); if (*json == '}') return; json = skip_spaces(json); } } CE_FATAL("Bad object"); }
static void parse_uniforms(const char* json, Array<UniformData>& uniforms, Array<char>& names, Array<char>& dynamic, CompileOptions& opts) { TempAllocator4096 ta; JsonObject object(ta); sjson::parse(json, object); auto begin = json_object::begin(object); auto end = json_object::end(object); for (; begin != end; ++begin) { const FixedString key = begin->pair.first; const char* value = begin->pair.second; UniformHandle uh; uh.uniform_handle = 0; JsonObject uniform(ta); sjson::parse_object(value, uniform); DynamicString type(ta); sjson::parse_string(uniform["type"], type); const UniformType::Enum ut = name_to_uniform_type(type.c_str()); RESOURCE_COMPILER_ASSERT(ut != UniformType::COUNT , opts , "Unknown uniform type: '%s'" , type.c_str() ); const u32 name_offset = array::size(names); array::push(names, key.data(), key.length()); array::push_back(names, '\0'); UniformData ud; ud.type = ut; ud.name = StringId32(key.data(), key.length()); ud.name_offset = name_offset; ud.data_offset = reserve_dynamic_data(uh, dynamic); switch (ud.type) { case UniformType::FLOAT: reserve_dynamic_data(sjson::parse_float(uniform["value"]), dynamic); break; case UniformType::VECTOR2: reserve_dynamic_data(sjson::parse_vector2(uniform["value"]), dynamic); break; case UniformType::VECTOR3: reserve_dynamic_data(sjson::parse_vector3(uniform["value"]), dynamic); break; case UniformType::VECTOR4: reserve_dynamic_data(sjson::parse_vector4(uniform["value"]), dynamic); break; default: CE_FATAL("Unknown uniform type"); break; } array::push_back(uniforms, ud); } }
bool process_events() { OsEvent event; bool exit = false; InputManager* im = device()->input_manager(); while(next_event(event)) { if (event.type == OsEvent::NONE) continue; switch (event.type) { case OsEvent::TOUCH: { const OsTouchEvent& ev = event.touch; switch (ev.type) { case OsTouchEvent::POINTER: im->touch()->set_button_state(ev.pointer_id, ev.pressed); break; case OsTouchEvent::MOVE: im->touch()->set_axis(ev.pointer_id, vector3(ev.x, ev.y, 0.0f)); break; default: CE_FATAL("Oops, unknown touch event type"); break; } break; } case OsEvent::MOUSE: { const OsMouseEvent& ev = event.mouse; switch (ev.type) { case OsMouseEvent::BUTTON: im->mouse()->set_button_state(ev.button, ev.pressed); break; case OsMouseEvent::MOVE: im->mouse()->set_axis(0, vector3(ev.x, ev.y, 0.0f)); break; case OsMouseEvent::WHEEL: im->mouse()->set_axis(1, vector3(ev.wheel, 0.0f, 0.0f)); break; default: CE_FATAL("Oops, unknown mouse event type"); break; } break; } case OsEvent::KEYBOARD: { const OsKeyboardEvent& ev = event.keyboard; im->keyboard()->set_button_state(ev.button, ev.pressed); break; } case OsEvent::METRICS: { const OsMetricsEvent& ev = event.metrics; device()->update_resolution(ev.width, ev.height); bgfx::reset(ev.width, ev.height, BGFX_RESET_VSYNC); break; } case OsEvent::EXIT: { exit = true; break; } case OsEvent::PAUSE: { device()->pause(); break; } case OsEvent::RESUME: { device()->unpause(); break; } default: { CE_FATAL("Unknown Os Event"); break; } } } return exit; }