From f76e4c225aceae150491e4c617280d6c347701ea Mon Sep 17 00:00:00 2001
From: dg <dg@51575b47-30f0-44d4-a5cc-537603b46e54>
Date: Tue, 2 Aug 2011 16:21:07 +0000
Subject: [PATCH] pfft

git-svn-id: http://svn.net-core.org/repos/t-engine4@4064 51575b47-30f0-44d4-a5cc-537603b46e54
---
 .../default/modules/boot/dialogs/MainMenu.lua |   2 +-
 .../tome/data/talents/spells/necrosis.lua     |   2 +-
 src/main.c                                    |   5 +-
 src/wait.c                                    | 131 +++++++++++++++---
 4 files changed, 120 insertions(+), 20 deletions(-)

diff --git a/game/engines/default/modules/boot/dialogs/MainMenu.lua b/game/engines/default/modules/boot/dialogs/MainMenu.lua
index 4c1e368fd3..5455861238 100644
--- a/game/engines/default/modules/boot/dialogs/MainMenu.lua
+++ b/game/engines/default/modules/boot/dialogs/MainMenu.lua
@@ -72,7 +72,7 @@ function _M:init()
 			game:registerDialog(require("engine.DebugConsole").new())
 		end
 	end)
-	self.key:addBind("SCREENSHOT", function() self:saveScreenshot() end)
+	self.key:addBind("SCREENSHOT", function() game:saveScreenshot() end)
 
 end
 
diff --git a/game/modules/tome/data/talents/spells/necrosis.lua b/game/modules/tome/data/talents/spells/necrosis.lua
index e7741a90c5..b817171328 100644
--- a/game/modules/tome/data/talents/spells/necrosis.lua
+++ b/game/modules/tome/data/talents/spells/necrosis.lua
@@ -38,7 +38,7 @@ newTalent{
 	end,
 	info = function(self, t)
 		return ([[The line between life and death blurs for you, you can only die when you reach -%d life.
-		However when bellow 0 you can not see how much life you have left.]]):
+		However when below 0 you can not see how much life you have left.]]):
 		format(50 * self:getTalentLevelRaw(t))
 	end,
 }
diff --git a/src/main.c b/src/main.c
index 88201fe462..ba7f86dfbf 100644
--- a/src/main.c
+++ b/src/main.c
@@ -683,9 +683,10 @@ void do_resize(int w, int h, bool fullscreen)
 			printf("error opening screen: %s\n", SDL_GetError());
 			exit(1);
 		}
+		glewInit();
 		screen = SDL_GetWindowSurface(window);
 		maincontext = SDL_GL_CreateContext(window);
-		glewInit();
+		SDL_GL_MakeCurrent(window, maincontext);
 	}
 	else
 	{
@@ -696,6 +697,8 @@ void do_resize(int w, int h, bool fullscreen)
 	SDL_SetWindowFullscreen(window, fullscreen);
 	SDL_GetWindowSize(window, &w, &h);
 	resizeWindow(w, h);
+	SDL_GL_MakeCurrent(window, maincontext);
+	resizeWindow(w, h);
 }
 
 void boot_lua(int state, bool rebooting, int argc, char *argv[])
diff --git a/src/wait.c b/src/wait.c
index b4f21b8163..806ae332aa 100644
--- a/src/wait.c
+++ b/src/wait.c
@@ -30,46 +30,127 @@
 #include "lua_externs.h"
 
 extern SDL_Window *window;
+extern SDL_Surface *screen;
 extern SDL_GLContext maincontext; /* Our opengl context handle */
 SDL_GLContext waitcontext; /* Our opengl context handle */
 static SDL_Thread *wait_thread = NULL;
 static SDL_sem *start_sem, *end_sem;
+static char* payload = NULL;
 static bool enabled = FALSE;
 
+static GLuint bkg_t;
+static GLubyte *bkg_image = NULL;
+static int bkg_realw=1;
+static int bkg_realh=1;
+
 extern int resizeWindow(int width, int height);
+extern void on_redraw();
+
+static int draw_last_frame(lua_State *L)
+{
+	int w, h;
+	SDL_GetWindowSize(window, &w, &h);
+
+	GLfloat btexcoords[2*4] = {
+		0, (float)h/(float)bkg_realh,
+		(float)w/(float)bkg_realw, (float)h/(float)bkg_realh,
+		(float)w/(float)bkg_realw, 0,
+		0, 0
+	};
+	GLfloat bcolors[4*4] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
+	GLfloat bvertices[2*4] = {
+		0, 0,
+		w, 0,
+		w, h,
+		0, h,
+	};
+	glTexCoordPointer(2, GL_FLOAT, 0, btexcoords);
+	glColorPointer(4, GL_FLOAT, 0, bcolors);
+	tglBindTexture(GL_TEXTURE_2D, bkg_t);
+	glVertexPointer(2, GL_FLOAT, 0, bvertices);
+	glDrawArrays(GL_QUADS, 0, 4);
+	return 0;
+}
+
+static const struct luaL_reg waitlib[] =
+{
+	{"drawLastFrame", draw_last_frame},
+	{NULL, NULL},
+};
+
 static int thread_wait(void *data)
 {
 	lua_State *L = lua_open();  /* create state */
 	luaL_openlibs(L);  /* open libraries */
 	luaopen_core(L);
+	luaL_openlib(L, "wait", waitlib, 0);
+	lua_pop(L, 1);
 
 	// And run the lua engine pre init scripts
 	if (!luaL_loadfile(L, "/loader/pre-init.lua")) docall(L, 0, 0);
 	else lua_pop(L, 1);
 
-
+	int rot = 1;
 	while (TRUE)
 	{
-//		SDL_SemWait(start_sem);
-		if (enabled)
+		if (!enabled) SDL_SemWait(start_sem);
+
+		if (enabled > 0)
 		{
 			if (enabled == 2)
 			{
+				int w, h;
+				SDL_GetWindowSize(window, &w, &h);
+
 				enabled = 1;
+
 				if (!waitcontext)
 				{
 					waitcontext = SDL_GL_CreateContext(window);
-					int w, h;
-					SDL_GetWindowSize(window, &w, &h);
 					resizeWindow(w, h);
+					glGenTextures(1, &bkg_t);
 				}
 				SDL_GL_MakeCurrent(window, waitcontext);
+
+				// Bind the texture to read
+				if (bkg_image)
+				{
+					tglBindTexture(GL_TEXTURE_2D, bkg_t);
+					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+					// In case we can't support NPOT textures round up to nearest POT
+					bkg_realw=1;
+					bkg_realh=1;
+					while (bkg_realw < w) bkg_realw *= 2;
+					while (bkg_realh < h) bkg_realh *= 2;
+					glTexImage2D(GL_TEXTURE_2D, 0, 3, bkg_realw, bkg_realh, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
+					glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, bkg_image);
+					printf("Make wait background texture %d : %dx%d (%d, %d)\n", bkg_t, w, h, bkg_realw, bkg_realh);
+				}
+
+				if (payload)
+				{
+					luaL_loadstring(L, payload);
+					lua_call(L, 0, 0);
+				}
 			}
 			SDL_Delay(50);
 
 			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 			glLoadIdentity();
 
+			lua_getglobal(L, "waitDisplay");
+			if (lua_isnil(L, -1)) lua_pop(L, 1);
+			else
+			{
+				if (lua_pcall(L, 0, 0, 0))
+				{
+					printf("Wait thread error: %s\n", lua_tostring(L, -1));
+					lua_pop(L, 1);
+				}
+			}
+
 			GLfloat texcoords[2*4] = {
 				0, 0,
 				1, 0,
@@ -89,21 +170,25 @@ static int thread_wait(void *data)
 			tglBindTexture(GL_TEXTURE_2D, 0);
 
 			GLfloat vertices[2*4] = {
+				-100, -100,
+				-100, 100,
 				100, 100,
-				100, 200,
-				200, 200,
-				200, 100,
+				100, -100,
 			};
 			glVertexPointer(2, GL_FLOAT, 0, vertices);
+			glTranslatef(500, 500, 0);
+			glRotatef(rot, 0, 0, 1);
 			glDrawArrays(GL_QUADS, 0, 4);
+			glRotatef(-rot, 0, 0, 1);
+			glTranslatef(-500, -500, 0);
+			rot++;
 
 			SDL_GL_SwapWindow(window);
-			printf("WAIT!\n");
 		}
-		else
+		else if (enabled == -1)
 		{
-//			SDL_GL_MakeCurrent(window, NULL);
 			SDL_SemPost(end_sem);
+			enabled = 0;
 		}
 	}
 
@@ -147,9 +232,23 @@ static int create_wait_thread(lua_State *L)
 static int enable(lua_State *L)
 {
 	if (!wait_thread) return 0;
-//	SDL_GL_MakeCurrent(window, NULL);
+
+	if (payload) { free(payload); payload = NULL; }
+
+	if (lua_isstring(L, 1))
+	{
+		payload = strdup(lua_tostring(L, 1));
+	}
+
+	// Grab currently displayed stuff
+	glPixelStorei(GL_PACK_ALIGNMENT, 1);
+	if (bkg_image) free(bkg_image);
+	bkg_image = (GLubyte*)malloc(screen->w * screen->h * 4 * sizeof(GLubyte));
+	glReadPixels(0, 0, screen->w, screen->h, GL_RGBA, GL_UNSIGNED_BYTE, bkg_image);
+
+	SDL_GL_MakeCurrent(window, NULL);
 	enabled = 2;
-//	SDL_SemPost(start_sem);
+	SDL_SemPost(start_sem);
 	return 0;
 }
 
@@ -157,12 +256,10 @@ static int enable(lua_State *L)
 static int disable(lua_State *L)
 {
 	if (!wait_thread) return 0;
-	enabled = 0;
+	enabled = -1;
 	SDL_SemWait(end_sem);
 	SDL_GL_MakeCurrent(window, maincontext);
-	int w, h;
-	SDL_GetWindowSize(window, &w, &h);
-	resizeWindow(w, h);
+	on_redraw();
 	return 0;
 }
 
-- 
GitLab