//----------------------------------------------------------------------------- EventDispatcher::EventDispatcher() : m_mouse_listener_list(default_allocator()), m_keyboard_listener_list(default_allocator()), m_touch_listener_list(default_allocator()), m_acc_listener_list(default_allocator()) { }
CollisionFilterCompiler(CompileOptions& opts) : _opts(opts) , _filter_map(default_allocator()) , _filters(default_allocator()) , _filter(1) { }
ResourceManager::ResourceManager(ResourceLoader& rl) : _resource_heap(default_allocator(), "resource") , _loader(&rl) , _type_data(default_allocator()) , _rm(default_allocator()) , _autoload(false) { }
void compile(const char* path, CompileOptions& opts) { Buffer buf = opts.read(path); TempAllocator4096 ta; JsonObject object(ta); sjson::parse(buf, object); Array<TextureData> texdata(default_allocator()); Array<UniformData> unidata(default_allocator()); Array<char> names(default_allocator()); Array<char> dynblob(default_allocator()); DynamicString shader(ta); sjson::parse_string(object["shader"], shader); parse_textures(object["textures"], texdata, names, dynblob, opts); parse_uniforms(object["uniforms"], unidata, names, dynblob, opts); MaterialResource mr; mr.version = RESOURCE_VERSION_MATERIAL; mr.shader = shader.to_string_id(); mr.num_textures = array::size(texdata); mr.texture_data_offset = sizeof(mr); mr.num_uniforms = array::size(unidata); mr.uniform_data_offset = mr.texture_data_offset + sizeof(TextureData)*array::size(texdata); mr.dynamic_data_size = array::size(dynblob); mr.dynamic_data_offset = mr.uniform_data_offset + sizeof(UniformData)*array::size(unidata); // Write opts.write(mr.version); opts.write(mr.shader); opts.write(mr.num_textures); opts.write(mr.texture_data_offset); opts.write(mr.num_uniforms); opts.write(mr.uniform_data_offset); opts.write(mr.dynamic_data_size); opts.write(mr.dynamic_data_offset); for (u32 i = 0; i < array::size(texdata); i++) { opts.write(texdata[i].sampler_name_offset); opts.write(texdata[i]._pad); opts.write(texdata[i].id); opts.write(texdata[i].data_offset); opts.write(texdata[i]._pad1); } for (u32 i = 0; i < array::size(unidata); i++) { opts.write(unidata[i].type); opts.write(unidata[i].name); opts.write(unidata[i].name_offset); opts.write(unidata[i].data_offset); } opts.write(dynblob); opts.write(names); }
void compile(const char* path, CompileOptions& opts) { static const uint32_t VERSION = 1; Buffer buf = opts.read(path); JSONParser json(array::begin(buf)); JSONElement root = json.root(); Array<LevelUnit> units(default_allocator()); Array<LevelSound> sounds(default_allocator()); { JSONElement sounds_arr = root.key("sounds"); const uint32_t size = sounds_arr.size(); for (uint32_t i = 0; i < size; i++) { JSONElement e = sounds_arr[i]; LevelSound ls; ls.name = e.key("name").to_resource_id("sound").name; ls.position = e.key("position").to_vector3(); ls.volume = e.key("volume").to_float(); ls.range = e.key("range").to_float(); ls.loop = e.key("loop").to_bool(); array::push_back(sounds, ls); } } { JSONElement units_arr = root.key("units"); const uint32_t size = units_arr.size(); for (uint32_t i = 0; i < size; i++) { JSONElement e = units_arr[i]; LevelUnit lu; lu.name = e.key("name").to_resource_id("unit").name; lu.position = e.key("position").to_vector3(); lu.rotation = e.key("rotation").to_quaternion(); array::push_back(units, lu); } } LevelResource lr; lr.version = VERSION; lr.num_units = array::size(units); lr.num_sounds = array::size(sounds); uint32_t offt = sizeof(LevelResource); lr.units_offset = offt; offt += sizeof(LevelUnit) * lr.num_units; lr.sounds_offset = offt; opts._bw & lr & units & sounds; }
void BundleCompiler::scan_source_dir(const char* cur_dir) { Vector<DynamicString> my_files(default_allocator()); _source_fs.list_files(cur_dir, my_files); for (u32 i = 0; i < vector::size(my_files); ++i) { TempAllocator512 ta; DynamicString file_i(ta); if (strcmp(cur_dir, "") != 0) { file_i += cur_dir; file_i += '/'; } file_i += my_files[i]; if (_source_fs.is_directory(file_i.c_str())) { BundleCompiler::scan_source_dir(file_i.c_str()); } else // Assume a regular file { vector::push_back(_files, file_i); } } }
void* load(Allocator& allocator, Bundle& bundle, ResourceId id) { File* file = bundle.open(id); BinaryReader br(*file); uint32_t version; br.read(version); uint32_t vs_code_size; br.read(vs_code_size); const bgfx::Memory* vsmem = bgfx::alloc(vs_code_size); br.read(vsmem->data, vs_code_size); uint32_t fs_code_size; br.read(fs_code_size); const bgfx::Memory* fsmem = bgfx::alloc(fs_code_size); br.read(fsmem->data, fs_code_size); bundle.close(file); Shader* shader = (Shader*) default_allocator().allocate(sizeof(Shader)); shader->vs = vsmem; shader->fs = fsmem; shader->program.idx = bgfx::invalidHandle; return shader; }
//-------------------------------------------------------------------------- JSONParser::~JSONParser() { if (m_file) { default_allocator().deallocate((void*) m_document); } }
//-------------------------------------------------------------------------- bool JSONElement::has_key(const char* k) const { Map<DynamicString, const char*> object(default_allocator()); json::parse_object(m_at, object); return map::has(object, DynamicString(k)); }
InputDevice* InputManager::create_input_device(const char* name, uint8_t num_buttons, uint8_t num_axes) { const uint32_t size = 0 + sizeof(InputDevice) + sizeof(uint8_t)*num_buttons*2 + sizeof(Vector3)*num_axes + strlen(name) + 1; InputDevice* id = (InputDevice*)default_allocator().allocate(size); id->_num_buttons = num_buttons; id->_num_axes = num_axes; id->_last_button = 0; id->_last_state = (uint8_t*)&id[1]; id->_current_state = (uint8_t*)(id->_last_state + num_buttons); id->_axis = (Vector3*)(id->_current_state + num_buttons); id->_name = (char*)(id->_axis + num_axes); memset(id->_last_state, 0, sizeof(uint8_t)*num_buttons); memset(id->_current_state, 0, sizeof(uint8_t)*num_buttons); memset(id->_axis, 0, sizeof(Vector3)*num_axes); strcpy(id->_name, name); return id; }
void compile(const char* path, CompileOptions& opts) { Buffer buf = opts.read(path); TempAllocator4096 ta; JsonObject object(ta); sjson::parse(buf, object); JsonArray texture(ta); JsonArray script(ta); JsonArray sound(ta); JsonArray mesh(ta); JsonArray unit(ta); JsonArray sprite(ta); JsonArray material(ta); JsonArray font(ta); JsonArray level(ta); JsonArray phyconf(ta); JsonArray shader(ta); JsonArray sprite_animation(ta); if (json_object::has(object, "texture")) sjson::parse_array(object["texture"], texture); if (json_object::has(object, "lua")) sjson::parse_array(object["lua"], script); if (json_object::has(object, "sound")) sjson::parse_array(object["sound"], sound); if (json_object::has(object, "mesh")) sjson::parse_array(object["mesh"], mesh); if (json_object::has(object, "unit")) sjson::parse_array(object["unit"], unit); if (json_object::has(object, "sprite")) sjson::parse_array(object["sprite"], sprite); if (json_object::has(object, "material")) sjson::parse_array(object["material"], material); if (json_object::has(object, "font")) sjson::parse_array(object["font"], font); if (json_object::has(object, "level")) sjson::parse_array(object["level"], level); if (json_object::has(object, "physics_config")) sjson::parse_array(object["physics_config"], phyconf); if (json_object::has(object, "shader")) sjson::parse_array(object["shader"], shader); if (json_object::has(object, "sprite_animation")) sjson::parse_array(object["sprite_animation"], sprite_animation); Array<PackageResource::Resource> resources(default_allocator()); compile_resources("texture", texture, resources, opts); compile_resources("lua", script, resources, opts); compile_resources("sound", sound, resources, opts); compile_resources("mesh", mesh, resources, opts); compile_resources("unit", unit, resources, opts); compile_resources("sprite", sprite, resources, opts); compile_resources("material", material, resources, opts); compile_resources("font", font, resources, opts); compile_resources("level", level, resources, opts); compile_resources("physics_config", phyconf, resources, opts); compile_resources("shader", shader, resources, opts); compile_resources("sprite_animation", sprite_animation, resources, opts); // Write opts.write(RESOURCE_VERSION_PACKAGE); opts.write(array::size(resources)); for (u32 i = 0; i < array::size(resources); ++i) { opts.write(resources[i].type); opts.write(resources[i].name); } }
//-------------------------------------------------------------------------- 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; } } }
//-------------------------------------------------------------------------- ResourceId JSONElement::to_resource_id(const char* type) const { CE_ASSERT_NOT_NULL(type); // TempAllocator1024 alloc; DynamicString str(default_allocator()); json::parse_string(m_at, str); return ResourceId(type, str.c_str()); }
//----------------------------------------------------------------------------- void VSCompiler::cleanup_impl() { if (m_file_data) { default_allocator().deallocate(m_file_data); m_file_data = NULL; } }
void compile(CompileOptions& opts) { Buffer unit_data(default_allocator()); UnitCompiler uc(opts); uc.compile_unit(opts.source_path()); opts.write(uc.blob()); }
//-------------------------------------------------------------------------- JSONParser::JSONParser(File& f) : m_file(true) , m_document(NULL) { const size_t size = f.size(); char* doc = (char*) default_allocator().allocate(size); f.read(doc, size); m_document = doc; }
void compile(const char* path, CompileOptions& opts) { Buffer unit_data(default_allocator()); UnitCompiler uc(opts); uc.compile_unit(path); opts.write(uc.blob()); }
//-------------------------------------------------------------------------- JSONElement JSONElement::operator[](uint32_t i) { Array<const char*> array(default_allocator()); json::parse_array(m_at, array); CE_ASSERT(i < array::size(array), "Index out of bounds"); return JSONElement(array[i]); }
File* DiskFilesystem::open(const char* path, FileOpenMode mode) { CE_ASSERT_NOT_NULL(path); TempAllocator256 alloc; DynamicString abs_path(alloc); get_absolute_path(path, abs_path); return CE_NEW(default_allocator(), DiskFile)(mode, abs_path.c_str()); }
//-------------------------------------------------------------------------- JSONElement JSONElement::key(const char* k) { Map<DynamicString, const char*> object(default_allocator()); json::parse_object(m_at, object); const char* value = map::get(object, DynamicString(k), (const char*) NULL); CE_ASSERT(value != NULL, "Key not found: '%s'", k); return JSONElement(value); }
void compile(const char* path, CompileOptions& opts) { Buffer buf = opts.read(path); JSONParser json(buf); JSONElement root = json.root(); Array<SpriteAnimationName> anim_names(default_allocator()); Array<SpriteAnimationData> anim_data(default_allocator()); Array<uint32_t> anim_frames(default_allocator()); parse_animations(root, anim_names, anim_data, anim_frames); SpriteAnimationResource sar; sar.version = SPRITE_ANIMATION_VERSION; sar.num_animations = array::size(anim_names); sar.num_frames = array::size(anim_frames); sar.frames_offset = uint32_t(sizeof(SpriteAnimationResource) + sizeof(SpriteAnimationName) * array::size(anim_names) + sizeof(SpriteAnimationData) * array::size(anim_data)); opts.write(sar.version); opts.write(sar.num_animations); opts.write(sar.num_frames); opts.write(sar.frames_offset); for (uint32_t i = 0; i < array::size(anim_names); i++) { opts.write(anim_names[i].id); } for (uint32_t i = 0; i < array::size(anim_data); i++) { opts.write(anim_data[i].num_frames); opts.write(anim_data[i].first_frame); opts.write(anim_data[i].time); } for (uint32_t i = 0; i < array::size(anim_frames); i++) { opts.write(anim_frames[i]); } }
//-------------------------------------------------------------------------- void JSONElement::to_array(Array<float>& array) const { Array<const char*> temp(default_allocator()); json::parse_array(m_at, temp); for (uint32_t i = 0; i < array::size(temp); i++) { array::push_back(array, json::parse_float(temp[i])); } }
//----------------------------------------------------------------------------- size_t VSCompiler::read_resource_impl(DiskFile* in_file) { m_file_size = in_file->size(); m_file_data = (char*)default_allocator().allocate(m_file_size * sizeof(char)); // Copy the entire file into the buffer in_file->read(m_file_data, m_file_size); // Return total resource size return m_file_size + sizeof(uint32_t); }
//-------------------------------------------------------------------------- void JSONElement::to_keys(Vector<DynamicString>& keys) const { Map<DynamicString, const char*> object(default_allocator()); json::parse_object(m_at, object); const Map<DynamicString, const char*>::Node* it = map::begin(object); while (it != map::end(object)) { vector::push_back(keys, (*it).key); it++; } }
//----------------------------------------------------------------------------- bool DiskFile::copy_to(File& file, size_t size) { check_valid(); const size_t chunksize = 1024*1024; char* buff = (char*) default_allocator().allocate(chunksize * sizeof(char)); size_t tot_read_bytes = 0; while (tot_read_bytes < size) { size_t read_bytes; size_t expected_read_bytes = math::min(size - tot_read_bytes, chunksize); read_bytes = m_file.read(buff, expected_read_bytes); if (read_bytes < expected_read_bytes) { if (m_file.eof()) { if (read_bytes != 0) { file.write(buff, read_bytes); } } default_allocator().deallocate(buff); //Either the file gave an error, or ended before size bytes could be copied return false; } file.write(buff, read_bytes); tot_read_bytes += read_bytes; } default_allocator().deallocate(buff); return true; }
Device::Device(const ConfigSettings& cs, Filesystem& fs) : _allocator(default_allocator(), MAX_SUBSYSTEMS_HEAP) , _width(0) , _height(0) , _is_init(false) , _is_running(false) , _is_paused(false) , _frame_count(0) , _last_time(0) , _current_time(0) , _last_delta_time(0.0f) , _time_since_start(0.0) , _cs(cs) , _fs(fs) , _boot_package_id(cs.boot_package) , _boot_script_id(cs.boot_script) , _boot_package(NULL) , _lua_environment(NULL) , _resource_manager(NULL) , _worlds(default_allocator()) { }
//-------------------------------------------------------------------------- void JSONElement::to_array(Vector<DynamicString>& array) const { Array<const char*> temp(default_allocator()); json::parse_array(m_at, temp); for (uint32_t i = 0; i < array::size(temp); i++) { DynamicString str; json::parse_string(temp[i], str); vector::push_back(array, str); } }
//-------------------------------------------------------------------------- JSONElement JSONElement::key_or_nil(const char* k) { if (m_at != NULL) { Map<DynamicString, const char*> object(default_allocator()); json::parse_object(m_at, object); const char* value = map::get(object, DynamicString(k), (const char*) NULL); if (value) return JSONElement(value); } return JSONElement(); }
void Device::destroy_world(World& w) { for (uint32_t i = 0, n = array::size(_worlds); i < n; ++i) { if (&w == _worlds[i]) { CE_DELETE(default_allocator(), &w); _worlds[i] = _worlds[n - 1]; array::pop_back(_worlds); return; } } CE_ASSERT(false, "Bad world"); }
void SpriteAnimationPlayer::destroy_sprite_animation(SpriteAnimation* anim) { const uint32_t num = array::size(m_animations); for (uint32_t i = 0; i < num; i++) { if (anim == m_animations[i]) { CE_DELETE(default_allocator(), anim); m_animations[i] = m_animations[num - 1]; array::pop_back(m_animations); return; } } }