From b17ff040d7c7f465db15497ea2af1c7da51cc451 Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Fri, 19 Sep 2014 15:19:02 +0300 Subject: [PATCH] client, ext/graphics3d: Loading of textures (not tested) --- extensions/graphics3d/init.lua | 4 +++- src/client/app.cpp | 24 ++++++++++++++++++++++ src/client/state.cpp | 11 +++++++++- src/client/state.h | 1 + test/testmodules/test1/client_lua/init.lua | 4 ++-- 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/extensions/graphics3d/init.lua b/extensions/graphics3d/init.lua index 2f2d5a9..ae78d28 100644 --- a/extensions/graphics3d/init.lua +++ b/extensions/graphics3d/init.lua @@ -29,6 +29,7 @@ M.safe.Scene = polybox.wrap_class("Scene", { M.safe.ScenePrimitive = polybox.wrap_class("ScenePrimitive", { constructor = function(type, v1, v2, v3, v4, v5) + polybox.check_enum(type, {ScenePrimitive.TYPE_BOX, ScenePrimitive.TYPE_PLANE}) polybox.check_type(v1, {"number", "nil"}) polybox.check_type(v2, {"number", "nil"}) polybox.check_type(v3, {"number", "nil"}) @@ -44,7 +45,8 @@ M.safe.ScenePrimitive = polybox.wrap_class("ScenePrimitive", { loadTexture = function(safe, texture_name) unsafe = polybox.check_type(safe, "ScenePrimitive") polybox.check_type(texture_name, "string") - unsafe:loadTexture("foo") + local path = __buildat_get_file_path(texture_name) + unsafe:loadTexture(path) end, setPosition = function(safe, x, y, z) unsafe = polybox.check_type(safe, "ScenePrimitive") diff --git a/src/client/app.cpp b/src/client/app.cpp index bf8126a..2d7eaaf 100644 --- a/src/client/app.cpp +++ b/src/client/app.cpp @@ -278,6 +278,7 @@ struct CApp: public Polycode::EventHandler, public App } DEF_BUILDAT_FUNC(send_packet); DEF_BUILDAT_FUNC(get_file_content) + DEF_BUILDAT_FUNC(get_file_path) DEF_BUILDAT_FUNC(get_path) DEF_BUILDAT_FUNC(pcall) @@ -354,6 +355,29 @@ struct CApp: public Polycode::EventHandler, public App } } + // get_file_path(name: string) -> path, hash + static int l_get_file_path(lua_State *L) + { + size_t name_len = 0; + const char *name_c = lua_tolstring(L, 1, &name_len); + ss_ name(name_c, name_len); + + lua_getfield(L, LUA_REGISTRYINDEX, "__buildat_app"); + CApp *self = (CApp*)lua_touserdata(L, -1); + lua_pop(L, 1); + + try { + ss_ hash; + ss_ path = self->m_state->get_file_path(name, &hash); + lua_pushlstring(L, path.c_str(), path.size()); + lua_pushlstring(L, hash.c_str(), hash.size()); + return 1; + } catch(std::exception &e){ + log_w(MODULE, "Exception in get_file_path: %s", e.what()); + return 0; + } + } + // get_file_content(name: string) static int l_get_file_content(lua_State *L) { diff --git a/src/client/state.cpp b/src/client/state.cpp index 6f8d94f..c9607c0 100644 --- a/src/client/state.cpp +++ b/src/client/state.cpp @@ -66,7 +66,7 @@ struct CState: public State }); } - ss_ get_file_content(const ss_ &name) + ss_ get_file_path(const ss_ &name, ss_ *dst_file_hash) { auto it = m_file_hashes.find(name); if(it == m_file_hashes.end()) @@ -74,6 +74,15 @@ struct CState: public State const ss_ &file_hash = it->second; ss_ file_hash_hex = interface::sha1::hex(file_hash); ss_ path = m_cache_path+"/"+file_hash_hex; + if(dst_file_hash != nullptr) + *dst_file_hash = file_hash; + return path; + } + + ss_ get_file_content(const ss_ &name) + { + ss_ file_hash; + ss_ path = get_file_path(name, &file_hash); std::ifstream f(path); if(!f.good()) throw Exception(ss_()+"Could not open file: "+path); diff --git a/src/client/state.h b/src/client/state.h index a26ae0a..ea95e26 100644 --- a/src/client/state.h +++ b/src/client/state.h @@ -18,6 +18,7 @@ namespace client virtual void update() = 0; virtual bool connect(const ss_ &address, const ss_ &port) = 0; virtual void send_packet(const ss_ &name, const ss_ &data) = 0; + virtual ss_ get_file_path(const ss_ &name, ss_ *dst_file_hash = NULL) = 0; virtual ss_ get_file_content(const ss_ &name) = 0; }; diff --git a/test/testmodules/test1/client_lua/init.lua b/test/testmodules/test1/client_lua/init.lua index 41751cf..1ad9c58 100644 --- a/test/testmodules/test1/client_lua/init.lua +++ b/test/testmodules/test1/client_lua/init.lua @@ -21,11 +21,11 @@ local g3d = require("buildat/extension/graphics3d") scene = g3d.Scene(g3d.Scene.SCENE_3D) ground = g3d.ScenePrimitive(g3d.ScenePrimitive.TYPE_PLANE, 5,5) -ground:loadTexture("Resources/green_texture.png") +ground:loadTexture("test1/green_texture.png") scene:addEntity(ground) box = g3d.ScenePrimitive(g3d.ScenePrimitive.TYPE_BOX, 1,1,1) -box:loadTexture("Resources/pink_texture.png") +box:loadTexture("test1/pink_texture.png") box:setPosition(0.0, 0.5, 0.0) scene:addEntity(box)