Create a gist now

Instantly share code, notes, and snippets.

#include <cstdio>
#include <string>
#include <GLES2/gl2.h>
#include <tr1/memory>
#include <ppapi/cpp/instance.h>
#include <ppapi/cpp/module.h>
#include <ppapi/cpp/var.h>
#include <ppapi/gles2/gl2ext_ppapi.h>
#include <ppapi/c/ppb_opengles.h>
#include <ppapi/cpp/graphics_3d_client.h>
#include <ppapi/cpp/graphics_3d.h>
#include <ppapi/cpp/size.h>
#include <ppapi/cpp/completion_callback.h>
#include <ppapi/cpp/input_event.h>
/* To link to gles2 libraries, add this to your build.scons script (after "nacl_env = make_nacl_env.NaClEnvironment ..." )
nacl_env.Append(
LIBS=['ppapi_gles2']
)
*/
pp::Instance* g_instance;
void nacl_log_message(const char* msg)
{
g_instance->PostMessage(msg);
}
void flush_callback(void* data, int32_t result);
class CustomInstance
: public pp::Instance
, public pp::Graphics3DClient
{
public:
CustomInstance(PP_Instance instance, pp::Module* module)
: pp::Instance(instance)
, pp::Graphics3DClient(this)
, m_width(1024)
, m_height(768)
{
g_instance = this;
m_gles2 = static_cast<const struct PPB_OpenGLES2*>(module->GetBrowserInterface(PPB_OPENGLES2_INTERFACE));
RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE | PP_INPUTEVENT_CLASS_WHEEL);
RequestFilteringInputEvents(PP_INPUTEVENT_CLASS_KEYBOARD);
}
virtual ~CustomInstance()
{
}
virtual bool Init(uint32_t argc, const char* argn[], const char* argv[])
{
nacl_log_message("Initializing NaCl module");
InitGL();
FlushContext();
return true;
}
virtual void DidChangeView(const pp::Rect& position, const pp::Rect& clip)
{
}
virtual void HandleMessage(const pp::Var& var_message)
{
}
void DrawSelf()
{
glViewport(0, 0, m_width, m_height);
// do our rendering here
glClearColor(0.1f, 0.2f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// done rendering, submit command buffer to actual render thread
FlushContext();
}
bool InitGL()
{
if( m_context.is_null() )
{
// TODO: get correct size
int32_t attrib_list[] =
{
PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 8,
PP_GRAPHICS3DATTRIB_DEPTH_SIZE, 24,
PP_GRAPHICS3DATTRIB_STENCIL_SIZE, 8,
PP_GRAPHICS3DATTRIB_SAMPLES, 0,
PP_GRAPHICS3DATTRIB_SAMPLE_BUFFERS, 0,
PP_GRAPHICS3DATTRIB_WIDTH, m_width,
PP_GRAPHICS3DATTRIB_HEIGHT, m_height,
PP_GRAPHICS3DATTRIB_NONE
};
m_context = pp::Graphics3D(*this, pp::Graphics3D(), attrib_list);
if( m_context.is_null() )
{
glSetCurrentContextPPAPI(0);
return false;
}
this->BindGraphics(m_context);
}
glSetCurrentContextPPAPI(m_context.pp_resource());
return true;
}
virtual void Graphics3DContextLost()
{
nacl_log_message("Graphics3DContextLost");
}
virtual bool HandleInputEvent(const pp::InputEvent& ev)
{
switch(ev.GetType())
{
case PP_INPUTEVENT_TYPE_MOUSEDOWN:
case PP_INPUTEVENT_TYPE_MOUSEUP:
case PP_INPUTEVENT_TYPE_MOUSEMOVE:
case PP_INPUTEVENT_TYPE_MOUSELEAVE:
//custom_process_mouse_event(pp::MouseInputEvent(ev));
break;
case PP_INPUTEVENT_TYPE_KEYDOWN:
case PP_INPUTEVENT_TYPE_KEYUP:
//custom_process_key_event(pp::KeyboardInputEvent(ev));
break;
}
return true;
}
void FlushContext()
{
m_context.SwapBuffers(pp::CompletionCallback(flush_callback, this));
}
private:
const struct PPB_OpenGLES2* m_gles2;
pp::Graphics3D m_context;
int32_t m_width;
int32_t m_height;
};
void flush_callback(void* data, int32_t result)
{
static_cast<CustomInstance*>(data)->DrawSelf();
}
class CustomModule : public pp::Module {
public:
CustomModule() : pp::Module() {}
virtual ~CustomModule() {}
virtual pp::Instance* CreateInstance(PP_Instance instance) {
return new CustomInstance(instance, this);
}
virtual bool Init() {
return glInitializePPAPI(get_browser_interface()) == GL_TRUE;
}
};
namespace pp {
Module* CreateModule() {
return new CustomModule();
}
} // namespace pp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment