source: trunk/src/platform/nacl/nacl_instance.cpp @ 1087

Last change on this file since 1087 was 1087, checked in by sam, 12 years ago

nacl: quick and dirty mouse support.

  • Property svn:keywords set to Id
File size: 3.4 KB
Line 
1// Copyright (c) 2011 The Native Client Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#if defined HAVE_CONFIG_H
6#   include "config.h"
7#endif
8
9#include <cstdlib>
10#include <cstdio>
11#include <cstring>
12#include <string>
13#include <vector>
14
15#include <ppapi/cpp/rect.h>
16#include <ppapi/cpp/size.h>
17#include <ppapi/cpp/var.h>
18#include <ppapi/cpp/module.h>
19#include <ppapi/cpp/completion_callback.h>
20#include <ppapi/cpp/input_event.h>
21
22#include "core.h"
23#include "debug/quad.h"
24
25#include "platform/nacl/nacl_instance.h"
26#include "platform/nacl/opengl_context.h"
27
28namespace lol
29{
30
31NaClInstance::NaClInstance(PP_Instance instance)
32    : pp::Instance(instance),
33      m_size(0, 0)
34{
35    RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE);
36}
37
38NaClInstance::~NaClInstance()
39{
40    // Destroy the cube view while GL context is current.
41    opengl_context_->MakeContextCurrent(this);
42}
43
44static double const DELTA_MS = 1000.0 / 60.0;
45
46void TickCallback(void* data, int32_t result)
47{
48    NaClInstance *instance = (NaClInstance *)data;
49    instance->DrawSelf();
50
51    /* FIXME: only set if if Ticker isn't finished */
52    pp::Module::Get()->core()->CallOnMainThread(
53            DELTA_MS, pp::CompletionCallback(&TickCallback, data), PP_OK);
54}
55
56}
57#define main OLDMAIN
58#include "../test/tutorial/tut03.cpp"
59#undef main
60namespace lol {
61
62bool NaClInstance::Init(uint32_t /* argc */,
63                        const char* /* argn */[],
64                        const char* /* argv */[])
65{
66    Ticker::Setup(60.0f);
67
68    //new Kub();
69    //new DebugQuad();
70    new Fractal(ivec2(640, 480));
71
72    // My timer callback
73    pp::Module::Get()->core()->CallOnMainThread(
74            DELTA_MS, pp::CompletionCallback(&TickCallback, this), PP_OK);
75
76    return true;
77}
78
79void NaClInstance::HandleMessage(const pp::Var& message)
80{
81    if (!message.is_string())
82        return;
83    /* FIXME: do some shit here */
84}
85
86void NaClInstance::DidChangeView(const pp::Rect& position, const pp::Rect& clip)
87{
88    if (position.size().width() == m_size.x &&
89        position.size().height() == m_size.y)
90        return;  // Size didn't change, no need to update anything.
91
92    m_size = ivec2(position.size().width(), position.size().height());
93
94    if (opengl_context_ == NULL)
95        opengl_context_.reset(new OpenGLContext(this));
96    opengl_context_->InvalidateContext(this);
97    opengl_context_->ResizeContext(position.size());
98    if (!opengl_context_->MakeContextCurrent(this))
99        return;
100
101    Video::Setup(m_size);
102    DrawSelf();
103}
104
105bool NaClInstance::HandleInputEvent(const pp::InputEvent& event)
106{
107    switch (event.GetType())
108    {
109    case PP_INPUTEVENT_TYPE_MOUSEDOWN:
110        Input::SetMouseButton(pp::MouseInputEvent(event).GetButton());
111        break;
112    case PP_INPUTEVENT_TYPE_MOUSEUP:
113        Input::UnsetMouseButton(pp::MouseInputEvent(event).GetButton());
114        break;
115    case PP_INPUTEVENT_TYPE_MOUSEMOVE:
116        Input::SetMousePos(ivec2(pp::MouseInputEvent(event).GetPosition().x(), opengl_context_->GetSize().height() - 1 - pp::MouseInputEvent(event).GetPosition().y()));
117        break;
118    default:
119        break;
120    }
121    return true;
122}
123
124void NaClInstance::DrawSelf()
125{
126    if (opengl_context_ == NULL)
127        return;
128
129    Ticker::ClampFps();
130    Ticker::TickGame();
131
132    opengl_context_->MakeContextCurrent(this);
133    Ticker::TickDraw();
134    opengl_context_->FlushContext();
135}
136
137}  // namespace lol
138
Note: See TracBrowser for help on using the repository browser.