Commit 9bd10674 authored by Mark Frohnmayer's avatar Mark Frohnmayer
Browse files

render and mouse input for test game now works correctly from nacl

parent e9bc8b67
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
nacl_test_tnl2_release.nexe: nacl_test_tnl2.cc
	/Users/markf/Work/gg/nativeclient/native_client/toolchain/mac_x86/sdk/nacl-sdk/bin/nacl-g++ nacl_test_tnl2.cc -O2 -DXP_UNIX -I../../nativeclient -I../tnl2 -I../../torque_sockets -I../test_tnl2 -Ilibtomcrypt/src/headers -Ilibtommath -Llibtommath -Llibtomcrypt -ltomcrypt -ltommath -lm -lgoogle_nacl_imc -lgoogle_nacl_npruntime -lgoogle_nacl_pgl -lgoogle_nacl_gpu -lpthread -lsrpc -o nacl_test_tnl2_release.nexe 
nacl_test_tnl2.nexe: nacl_test_tnl2.cc
	/Users/markf/Work/gg/nativeclient/native_client/toolchain/mac_x86/sdk/nacl-sdk/bin/nacl-g++ nacl_test_tnl2.cc -O2 -DXP_UNIX -I../../nativeclient -I../tnl2 -I../../torque_sockets -I../test_tnl2 -Ilibtomcrypt/src/headers -Ilibtommath -Llibtommath -Llibtomcrypt -ltomcrypt -ltommath -lm -lgoogle_nacl_imc -lgoogle_nacl_npruntime -lgoogle_nacl_pgl -lgoogle_nacl_gpu -lpthread -lsrpc -o nacl_test_tnl2.nexe 
	
	
+32 −2
Original line number Diff line number Diff line
@@ -119,7 +119,7 @@ public:
		glClearColor(_color, _color, 0, 1);
		glClear(GL_COLOR_BUFFER_BIT);
		
		tnl_test::test_game_render_frame_open_gl(0);
		tnl_test::test_game_render_frame_open_gl(_game);
		
		glFlush();
		
@@ -130,7 +130,37 @@ public:
	
	core::int16 handle_event(void *event)
	{
		return 0;
		NPPepperEvent* npevent = reinterpret_cast<NPPepperEvent*>(event);
		tnl_test::position p;
		switch (npevent->type) {
			case NPEventType_MouseDown:
				p.x = npevent->u.mouse.x / float(_width);
				p.y = npevent->u.mouse.y / float(_height);
				logprintf("Got mouse click at (%d, %d) -> (%g, %g)", npevent->u.mouse.x, npevent->u.mouse.y, float(p.x), float(p.y));
				_game->move_my_player_to(p);
				break;
			case NPEventType_MouseUp:
			case NPEventType_MouseMove:
			case NPEventType_MouseEnter:
			case NPEventType_MouseLeave:
				break;
			case NPEventType_MouseWheel:
				break;
			case NPEventType_RawKeyDown:
			case NPEventType_KeyDown:
			case NPEventType_KeyUp:
				break;
			case NPEventType_Char:
				break;
			case NPEventType_Minimize:
			case NPEventType_Focus:
			case NPEventType_Device:
				break;
			case NPEventType_Undefined:
			default:
				break;
		}
		return 1;
	}
	
	bool module_ready()
+9 −15
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@
var nacl_elt;

// Before scripting the loaded module, ensure the Native Client module is loaded.
var startupTimeout;
var startupInterval;

var PostLoadInit = function()
{
@@ -19,7 +19,7 @@ var PostLoadInit = function()
	{
		var embed = document.getElementById("embed1");
		var ready = embed.module_ready();
		clearTimeout(startupTimeout);
		clearInterval(startupInterval);
		alert('The Native Client plugin is ready');
		return;
	}
@@ -27,16 +27,13 @@ var PostLoadInit = function()
	{
		// do nothing
	}
	startupTimeout = setTimeout(PostLoadInit, 100);
}

// Init is called when the document has completed loading.  It downloads the
// NativeClient modules.
var Init = function() {
  // service_url contains the location of the NativeClient module to be
  // loaded as a service.
  nacl_elt = document.getElementById('embed1');
  PostLoadInit();
var Init = function()
{
	startupInterval = setInterval("PostLoadInit()", 100);
}

-->
@@ -47,17 +44,14 @@ var Init = function() {

<h1>test_tnl2 embedded in a web page</h1>
 <p>
  You should see a yellow box below, representing the game world.  Red rectangles represent buildings in 
  the sample game world and the small blue squares represent players.  The demo starts in client mode and will
  attempt to connect to a local server.
  You should a yellow box below, representing a server game world.  Red rectangles represent buildings in 
  the sample game world and the small blue squares represent players.
 </p>
 <p>
  This demo uses the open source Torque Network Library, which
  provides high performance real-time networking for games and simulations.  Please see
  <a href="http://www.libtnl.com/">http://www.libtnl.com/</a>
  for more information about the Torque Network Library.
  provides high performance real-time networking for games and simulations.
 </p>
<embed id="embed1" src="nacl_test_tnl2_release.nexe" type="application/x-nacl-srpc" width=512 height=512>
<embed id="embed1" src="nacl_test_tnl2.nexe" type="application/x-nacl-srpc" width=400 height=400>
<br>

</BODY>
+19 −34
Original line number Diff line number Diff line
@@ -24,7 +24,7 @@ static GLuint load_shader(const char *shaderSrc, GLenum type)
		{
			char* infoLog = (char *) malloc(sizeof(char) * infoLen);
			glGetShaderInfoLog(shader, infoLen, NULL, infoLog);
			logprintf("Error compiling shader:\n%s\n", infoLog);
			logprintf("Error compiling shader:\n%s\n%s\n", shaderSrc, infoLog);
			free(infoLog);
		}
		glDeleteShader(shader);
@@ -94,20 +94,17 @@ static void box(GLfloat render_vertices[12], float left, float top, float right,
	   [6, 7, 8],
	   [9, 10, 11], */
	
	render_vertices[0] = left; render_vertices[1] = top; render_vertices[2] = 0;
	render_vertices[3] = left; render_vertices[4] = bottom; render_vertices[5] = 0;
	render_vertices[6] = right; render_vertices[7] = top; render_vertices[8] = 0;
	render_vertices[9] = right; render_vertices[10] = bottom; render_vertices[11] = 0;
	render_vertices[0] = left * 2 - 1; render_vertices[1] = 1 - top * 2; render_vertices[2] = 0;
	render_vertices[3] = left * 2 - 1; render_vertices[4] = 1 - bottom * 2; render_vertices[5] = 0;
	render_vertices[6] = right * 2 - 1; render_vertices[7] = 1 - top * 2; render_vertices[8] = 0;
	render_vertices[9] = right * 2 - 1; render_vertices[10] = 1 - bottom * 2; render_vertices[11] = 0;
}

/// test_game_render_frame_open_gl is called by the platform windowing code to notify the game that it should render the current world using OpenGL.
static void test_game_render_frame_open_gl(test_game *the_game)
{	
	static GLuint g_programObject = 0;
	static GLuint g_worldMatrixLoc = 0;
	static GLuint g_vbo = 0;	
	
	static GLuint program_object = 0;
	static GLuint color_loc = 0;
	if(!program_object)
	{
		char vShaderStr[] =
@@ -117,10 +114,11 @@ static void test_game_render_frame_open_gl(test_game *the_game)
		"   gl_Position = vPosition; \n"
		"}                           \n";
		char fShaderStr[] =
		"precision mediump float;                   \n"
		"uniform vec4 color;  \n"
		//"precision mediump float;                   \n"
		"void main()                                \n"
		"{                                          \n"
		" gl_FragColor = vec4(.5, 1.0, .5, 1.0); \n"
		" gl_FragColor = color; \n"
		"}                                          \n";
		
		array<attribute> attributes;
@@ -128,27 +126,12 @@ static void test_game_render_frame_open_gl(test_game *the_game)
		program_object = init_program_object(vShaderStr, fShaderStr, attributes);
		if(!program_object)
			return;
		color_loc = glGetUniformLocation(program_object, "color");
	}
	glDisable(GL_CULL_FACE);
	glDisable(GL_DEPTH_TEST);
	glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
	
	GLfloat vVertices[] =
	{
		-0.5f, 0.5f, 0.0f,
		-0.5f, -0.5f, 0.0f,
		0.5, 0.5, 0.0f,
		0.5f, -0.5f, 0.0f,
	};
    // Clear the color buffer
	glClear(GL_COLOR_BUFFER_BIT);
    // Use the program object
	glUseProgram(program_object);
	// Load the vertex data
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
	glEnableVertexAttribArray(0);
	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

	glClearColor(1, 1, 0, 0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

@@ -171,12 +154,13 @@ static void test_game_render_frame_open_gl(test_game *the_game)
	
	GLfloat render_vertices[12];
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, render_vertices);
	glEnableVertexAttribArray(0);
	
	// then draw all the _buildings.
	glUniform4f(color_loc, 1, 0, 0, 1);
	for(int32 i = 0; i < the_game->_buildings.size(); i++)
	{
		building *b = the_game->_buildings[i];
		//glColor3f(1, 0, 0);
		
		box(render_vertices, b->upper_left.x, b->upper_left.y, b->lower_right.x, b->lower_right.y);
		glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
@@ -186,7 +170,7 @@ static void test_game_render_frame_open_gl(test_game *the_game)
	for(int32 i = 0; i < the_game->_players.size(); i++)
	{
		player *p = the_game->_players[i];
		//glColor3f(0,0,0);
		glUniform4f(color_loc, 0, 0, 0, 1);
		
		box(render_vertices, p->_render_pos.x - 0.012f, p->_render_pos.y - 0.012f, p->_render_pos.x + 0.012f, p->_render_pos.y + 0.012f);
		glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
@@ -195,16 +179,17 @@ static void test_game_render_frame_open_gl(test_game *the_game)
		{
			case player::player_type_ai:
			case player::player_type_ai_dummy:
				//glColor3f(0, 0, 1);
				glUniform4f(color_loc, 0, 0, 1, 1);
				break;
			case player::player_type_client:
				//glColor3f(0.5, 0.5, 1);
				glUniform4f(color_loc, 0.5, 0.5, 1, 1);
				break;
			case player::player_type_my_client:
				//glColor3f(1, 1, 1);
				glUniform4f(color_loc, 1, 1, 1, 1);
				break;
		}
		box(render_vertices, p->_render_pos.x - 0.01f, p->_render_pos.y - 0.01f, p->_render_pos.x + 0.01f, p->_render_pos.y + 0.01f);
		glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
	}
	logprintf("finished draw.");
}