From 4ec178ac5eab7d19e5389f389d5de37606430ad3 Mon Sep 17 00:00:00 2001
From: dg <dg@51575b47-30f0-44d4-a5cc-537603b46e54>
Date: Fri, 12 Aug 2011 13:25:05 +0000
Subject: [PATCH] Fixed a random white texture bug

Loading bar while loading the game/savefile will indicate progress


git-svn-id: http://svn.net-core.org/repos/t-engine4@4121 51575b47-30f0-44d4-a5cc-537603b46e54
---
 game/engines/default/engine/Faction.lua    |  2 +-
 game/engines/default/engine/Module.lua     | 43 ++++++++++++++++--
 game/engines/default/engine/Savefile.lua   | 39 ++++++++++++++++-
 game/engines/default/engine/init.lua       |  4 +-
 game/engines/default/modules/boot/init.lua |  1 +
 game/modules/tome/init.lua                 |  1 +
 src/core_lua.c                             | 12 ++---
 src/main.c                                 |  2 +
 src/tgl.h                                  |  4 ++
 src/wait.c                                 | 51 ++++++++++++++++++++--
 10 files changed, 141 insertions(+), 18 deletions(-)

diff --git a/game/engines/default/engine/Faction.lua b/game/engines/default/engine/Faction.lua
index 449bee94da..9ed2ed6268 100644
--- a/game/engines/default/engine/Faction.lua
+++ b/game/engines/default/engine/Faction.lua
@@ -54,7 +54,7 @@ end
 -- @param reaction a numerical value representing the reaction, 0 is neutral, <0 is aggressive, >0 is friendly.
 -- @param mutual if true the same status will be set for f2 toward f1.
 function _M:setInitialReaction(f1, f2, reaction, mutual)
-	print("[FACTION] initial", f1, f2, reaction, mutual)
+--	print("[FACTION] initial", f1, f2, reaction, mutual)
 	-- Faction always like itself
 	if f1 == f2 then return end
 	if not self.factions[f1] then return end
diff --git a/game/engines/default/engine/Module.lua b/game/engines/default/engine/Module.lua
index eee4c9d7da..a0513261fe 100644
--- a/game/engines/default/engine/Module.lua
+++ b/game/engines/default/engine/Module.lua
@@ -234,7 +234,9 @@ end
 function _M:loadScreen(mod)
 	core.display.forceRedraw()
 	core.wait.enable(10000, function()
-		local i, max, dir = 0, 20, 1
+		local has_max = mod.loading_wait_ticks
+		if has_max then core.wait.addMaxTicks(has_max) end
+		local i, max, dir = has_max or 20, has_max or 20, -1
 
 		local bkgs = core.display.loadImage("/data/gfx/background/"..mod.short_name..".png") or core.display.loadImage("/data/gfx/background/tome.png")
 		local sw, sh = core.display.size()
@@ -248,6 +250,8 @@ function _M:loadScreen(mod)
 		local middle = {core.display.loadImage("/data/gfx/waiter/middle.png"):glTexture()}
 		local bar = {core.display.loadImage("/data/gfx/waiter/bar.png"):glTexture()}
 
+		local font = core.display.newFont("/data/font/Vera.ttf", 12)
+
 		local dw, dh = math.floor(sw / 2), left[7]
 		local dx, dy = math.floor((sw - dw) / 2), sh - dh
 
@@ -270,21 +274,35 @@ function _M:loadScreen(mod)
 
 			-- Progressbar
 			local x
-			i = i + dir
-			if dir > 0 and i >= max then dir = -1
-			elseif dir < 0 and i <= -max then dir = 1
+			if has_max then
+				i, max = core.wait.getTicks()
+				i = util.bound(i, 0, max)
+			else
+				i = i + dir
+				if dir > 0 and i >= max then dir = -1
+				elseif dir < 0 and i <= -max then dir = 1
+				end
 			end
 
 			local x = dw * (i / max)
 			local x2 = x + dw
 			x = util.bound(x, 0, dw)
 			x2 = util.bound(x2, 0, dw)
+			if has_max then x, x2 = 0, x end
 			local w, h = x2 - x, dh
 
 			middle[1]:toScreenFull(dx, dy, dw, middle[7], middle[2], middle[3])
 			bar[1]:toScreenFull(dx + x, dy, w, bar[7], bar[2], bar[3])
 			left[1]:toScreenFull(dx - left[6] + 5, dy + (middle[7] - left[7]) / 2, left[6], left[7], left[2], left[3])
 			right[1]:toScreenFull(dx + dw - 5, dy + (middle[7] - right[7]) / 2, right[6], right[7], right[2], right[3])
+
+			if has_max then
+				font:setStyle("bold")
+				local txt = {core.display.drawStringBlendedNewSurface(font, math.min(100, math.floor(core.wait.getTicks() * 100 / max)).."%", 255, 255, 255):glTexture()}
+				font:setStyle("normal")
+				txt[1]:toScreenFull(dx + (dw - txt[6]) / 2 + 2, dy + (bar[7] - txt[7]) / 2 + 2, txt[6], txt[7], txt[2], txt[3], 0, 0, 0, 0.6)
+				txt[1]:toScreenFull(dx + (dw - txt[6]) / 2, dy + (bar[7] - txt[7]) / 2, txt[6], txt[7], txt[2], txt[3])
+			end
 		end
 	end)
 end
@@ -316,7 +334,22 @@ function _M:instanciate(mod, name, new_game, no_reboot)
 	-- Init the module directories
 	mod.load("setup")
 
+	-- Check the savefile if possible, to add to the progress bar size
+	local savesize = 0
+	local save = Savefile.new("")
+	savesize = save:loadWorldSize()
+	save:close()
+
+	-- Load the savefile if it exists, or create a new one if not (or if requested)
+	local save = engine.Savefile.new(name)
+	if save:check() and not new_game then
+		savesize = savesize + save:loadGameSize()
+	end
+	save:close()
+
+	-- Display the loading bar
 	self:loadScreen(mod)
+	core.wait.addMaxTicks(savesize)
 
 	-- Check MD5sum with the server
 	local md5 = require "md5"
@@ -361,6 +394,7 @@ function _M:instanciate(mod, name, new_game, no_reboot)
 	_G.game:setPlayerName(name)
 
 	-- Load the world, or make a new one
+	core.wait.enableManualTick(true)
 	if W then
 		local save = Savefile.new("")
 		_G.world = save:loadWorld()
@@ -381,6 +415,7 @@ function _M:instanciate(mod, name, new_game, no_reboot)
 		save:delete()
 	end
 	save:close()
+	core.wait.enableManualTick(false)
 
 	-- And now run it!
 	_G.game:run()
diff --git a/game/engines/default/engine/Savefile.lua b/game/engines/default/engine/Savefile.lua
index c03ff60fcf..c3b69caac2 100644
--- a/game/engines/default/engine/Savefile.lua
+++ b/game/engines/default/engine/Savefile.lua
@@ -363,7 +363,7 @@ function _M:loadReal(load)
 	-- Resolve self referencing tables now
 	resolveSelf(o, o, true)
 
---	core.wait.manualTick()
+	core.wait.manualTick(1)
 	self.loaded[load] = o
 	return o
 end
@@ -393,6 +393,24 @@ function _M:loadWorld()
 	return loadedWorld
 end
 
+--- Loads a world
+function _M:loadWorldSize()
+	local path = fs.getRealPath(self.save_dir..self:nameLoadWorld())
+	if not path or path == "" then return nil, "no savefile" end
+
+	fs.mount(path, self.load_dir)
+
+	local f = fs.open(self.load_dir.."nb", "r")
+	local nb = 0
+	if f then
+		nb = tonumber(f:read()) or 100
+		f:close()
+	end
+
+	fs.umount(path)
+	return nb
+end
+
 --- Loads a game
 function _M:loadGame()
 	local path = fs.getRealPath(self.save_dir..self:nameLoadGame())
@@ -420,6 +438,25 @@ function _M:loadGame()
 	return loadedGame, delay_fct
 end
 
+
+--- Loads a game
+function _M:loadGameSize()
+	local path = fs.getRealPath(self.save_dir..self:nameLoadGame())
+	if not path or path == "" then return nil, "no savefile" end
+
+	fs.mount(path, self.load_dir)
+
+	local f = fs.open(self.load_dir.."nb", "r")
+	local nb = 0
+	if f then
+		nb = tonumber(f:read()) or 100
+		f:close()
+	end
+
+	fs.umount(path)
+	return nb
+end
+
 --- Loads a zone
 function _M:loadZone(zone)
 	local path = fs.getRealPath(self.save_dir..self:nameLoadZone(zone))
diff --git a/game/engines/default/engine/init.lua b/game/engines/default/engine/init.lua
index 55bdb92479..d5f3d676cd 100644
--- a/game/engines/default/engine/init.lua
+++ b/game/engines/default/engine/init.lua
@@ -116,7 +116,7 @@ profile = engine.PlayerProfile.new()
 -- Create a savefile pipe
 savefile_pipe = engine.SavefilePipe.new()
 
-util.showMainMenu(true)
-
 -- Setup FPS
 core.game.setFPS(config.settings.display_fps)
+
+util.showMainMenu(true)
diff --git a/game/engines/default/modules/boot/init.lua b/game/engines/default/modules/boot/init.lua
index 1dd00d698b..1b0d09fbb0 100644
--- a/game/engines/default/modules/boot/init.lua
+++ b/game/engines/default/modules/boot/init.lua
@@ -29,3 +29,4 @@ description = [[
 Bootmenu!
 ]]
 starter = "mod.load"
+loading_wait_ticks = 1600
diff --git a/game/modules/tome/init.lua b/game/modules/tome/init.lua
index fca9fbab3c..d16cfebdad 100644
--- a/game/modules/tome/init.lua
+++ b/game/modules/tome/init.lua
@@ -45,6 +45,7 @@ Still, this is a golden age. Civilisations are healing the wounds of thousands o
 You are an adventurer, set out to discover wonders, explore old places, and venture into the unknown for wealth and glory.
 ]]
 starter = "mod.load"
+loading_wait_ticks = 260
 profile_stats_fields = {"artifacts", "characters", "deaths", "uniques", "scores", "lore", "escorts"}
 allow_userchat = true -- We can talk to the online community
 no_get_name = true -- Name setting for new characters is done by the module itself
diff --git a/src/core_lua.c b/src/core_lua.c
index 7215f71361..70212c8370 100644
--- a/src/core_lua.c
+++ b/src/core_lua.c
@@ -510,7 +510,7 @@ static font_make_texture_line(lua_State *L, SDL_Surface *s, int id, bool is_sepa
 	lua_rawset(L, -3);
 
 	glGenTextures(1, t);
-	tglBindTexture(GL_TEXTURE_2D, *t);
+	tfglBindTexture(GL_TEXTURE_2D, *t);
 	int fw, fh;
 	make_texture_for_surface(s, &fw, &fh);
 	copy_surface_to_texture(s);
@@ -909,7 +909,7 @@ int init_blank_surface()
 	SDL_FillRect(s, NULL, SDL_MapRGBA(s->format, 255, 255, 255, 255));
 
 	glGenTextures(1, &gl_tex_white);
-	tglBindTexture(GL_TEXTURE_2D, gl_tex_white);
+	tfglBindTexture(GL_TEXTURE_2D, gl_tex_white);
 	int fw, fh;
 	make_texture_for_surface(s, &fw, &fh);
 	copy_surface_to_texture(s);
@@ -1198,7 +1198,7 @@ static int sdl_surface_toscreen(lua_State *L)
 
 	GLuint t;
 	glGenTextures(1, &t);
-	tglBindTexture(GL_TEXTURE_2D, t);
+	tfglBindTexture(GL_TEXTURE_2D, t);
 
 	make_texture_for_surface(*s, NULL, NULL);
 	copy_surface_to_texture(*s);
@@ -1267,7 +1267,7 @@ static int sdl_surface_to_texture(lua_State *L)
 	auxiliar_setclass(L, "gl{texture}", -1);
 
 	glGenTextures(1, t);
-	tglBindTexture(GL_TEXTURE_2D, *t);
+	tfglBindTexture(GL_TEXTURE_2D, *t);
 
 	int fw, fh;
 	make_texture_for_surface(*s, &fw, &fh);
@@ -1546,7 +1546,7 @@ static int sdl_texture_outline(lua_State *L)
 	GLuint *img = (GLuint*)lua_newuserdata(L, sizeof(GLuint));
 	auxiliar_setclass(L, "gl{texture}", -1);
 	glGenTextures(1, img);
-	tglBindTexture(GL_TEXTURE_2D, *img);
+	tfglBindTexture(GL_TEXTURE_2D, *img);
 	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,  w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
@@ -1786,7 +1786,7 @@ static int gl_new_fbo(lua_State *L)
 
 	// Now setup a texture to render to
 	glGenTextures(1, &(fbo->texture));
-	tglBindTexture(GL_TEXTURE_2D, fbo->texture);
+	tfglBindTexture(GL_TEXTURE_2D, fbo->texture);
 	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8,  w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
diff --git a/src/main.c b/src/main.c
index 36a5f4cb3d..720edba031 100644
--- a/src/main.c
+++ b/src/main.c
@@ -68,6 +68,7 @@ int mouse_cursor_down_tex = 0, mouse_cursor_down_tex_ref = LUA_NOREF;
 int mouse_cursor_ox = 0, mouse_cursor_oy = 0;
 int mousex = 0, mousey = 0;
 float gamma_correction = 1;
+int requested_fps = 30;
 SDL_TimerID display_timer_id = NULL;
 SDL_TimerID realtime_timer_id = NULL;
 
@@ -614,6 +615,7 @@ void setupRealtime(float freq)
 void setupDisplayTimer(int fps)
 {
 	if (display_timer_id) SDL_RemoveTimer(display_timer_id);
+	requested_fps = fps;
 	display_timer_id = SDL_AddTimer(1000 / fps, redraw_timer, NULL);
 	printf("[ENGINE] Setting requested FPS to %d (%d ms)\n", fps, 1000 / fps);
 }
diff --git a/src/tgl.h b/src/tgl.h
index d0757a04dd..da5255945f 100644
--- a/src/tgl.h
+++ b/src/tgl.h
@@ -40,3 +40,7 @@ extern GLenum gl_c_texture;
 	{ \
 	if ((t) != gl_c_texture) { glBindTexture((w), (t)); gl_c_texture=(t); } \
 	}
+#define tfglBindTexture(w, t) \
+	{ \
+	glBindTexture((w), (t)); gl_c_texture=(t); \
+	}
diff --git a/src/wait.c b/src/wait.c
index cd6c07b976..5547bcb6b4 100644
--- a/src/wait.c
+++ b/src/wait.c
@@ -32,9 +32,11 @@
 extern SDL_Window *window;
 extern SDL_Surface *screen;
 
-static bool wait_hooked = FALSE;
+static int wait_hooked = 0;
+static bool manual_ticks_enabled = FALSE;
 static int waiting = 0;
 static int waited_count = 0;
+static int waited_count_max = 0;
 static long waited_ticks = 0;
 static int bkg_realw, bkg_realh, bkg_w, bkg_h;
 static GLuint bkg_t = 0;
@@ -88,15 +90,16 @@ bool is_waiting()
 	return TRUE;
 }
 
+extern int requested_fps;
 extern void on_redraw();
 static void hook_wait_display(lua_State *L, lua_Debug *ar)
 {
-	waited_count++;
+	if (!manual_ticks_enabled) waited_count++;
 	SDL_PumpEvents();
 
 	static int last_tick = 0;
 	int now = SDL_GetTicks();
-	if (now - last_tick < 300) return;
+	if (now - last_tick < (3000 / requested_fps)) return;
 	last_tick = now;
 	on_redraw();
 }
@@ -110,6 +113,8 @@ static int enable(lua_State *L)
 	if (waiting == 1)
 	{
 		waited_count = 0;
+		waited_count_max = 0;
+		manual_ticks_enabled = FALSE;
 		waited_ticks = SDL_GetTicks();
 
 		int w, h;
@@ -138,7 +143,7 @@ static int enable(lua_State *L)
 		if (!lua_gethookmask(L))
 		{
 			lua_sethook(L, hook_wait_display, LUA_MASKCOUNT, count);
-			wait_hooked = TRUE;
+			wait_hooked = count;
 		}
 
 		if (lua_isfunction(L, 2))
@@ -171,17 +176,55 @@ static int disable(lua_State *L)
 		if (wait_draw_ref != LUA_NOREF) luaL_unref(L, LUA_REGISTRYINDEX, wait_draw_ref);
 		waited_ticks = SDL_GetTicks() - waited_ticks;
 		printf("Wait finished, counted %d, %ld ticks\n", waited_count, waited_ticks);
+
+		waited_count = 0;
+		waited_count_max = 0;
 	}
 	lua_pushboolean(L, waiting > 0);
 	lua_pushnumber(L, waiting);
 	return 2;
 }
 
+static int enable_manual_tick(lua_State *L)
+{
+	manual_ticks_enabled = lua_toboolean(L, 1);
+	if (!manual_ticks_enabled) lua_sethook(L, hook_wait_display, LUA_MASKCOUNT, wait_hooked);
+	else lua_sethook(L, NULL, 0, 0);
+	return 0;
+}
+
+static int manual_tick(lua_State *L)
+{
+	if (manual_ticks_enabled)
+	{
+		waited_count += lua_tonumber(L, 1);
+		hook_wait_display(L, NULL);
+	}
+	return 0;
+}
+
+static int add_max_ticks(lua_State *L)
+{
+	waited_count_max += lua_tonumber(L, 1);
+	return 0;
+}
+
+static int get_ticks(lua_State *L)
+{
+	lua_pushnumber(L, waited_count);
+	lua_pushnumber(L, waited_count_max);
+	return 2;
+}
+
 static const struct luaL_reg mainlib[] =
 {
 	{"drawLastFrame", draw_last_frame},
 	{"enable", enable},
 	{"disable", disable},
+	{"enableManualTick", enable_manual_tick},
+	{"manualTick", manual_tick},
+	{"addMaxTicks", add_max_ticks},
+	{"getTicks", get_ticks},
 	{NULL, NULL},
 };
 
-- 
GitLab