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

Last change on this file since 1084 was 1084, checked in by sam, 11 years ago

nacl: the Mandelbrot zoomer is starting to work on NaCl.

  • Property svn:keywords set to Id
File size: 2.7 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
21#include "core.h"
22#include "debug/quad.h"
23
24#include "platform/nacl/nacl_instance.h"
25#include "platform/nacl/opengl_context.h"
26
27namespace lol
28{
29
30NaClInstance::NaClInstance(PP_Instance instance)
31    : pp::Instance(instance),
32      m_size(0, 0)
33{
34    ;
35}
36
37NaClInstance::~NaClInstance()
38{
39    // Destroy the cube view while GL context is current.
40    opengl_context_->MakeContextCurrent(this);
41}
42
43static double const DELTA_MS = 1000.0 / 60.0;
44
45void TickCallback(void* data, int32_t result)
46{
47    NaClInstance *instance = (NaClInstance *)data;
48    instance->DrawSelf();
49
50    /* FIXME: only set if if Ticker isn't finished */
51    pp::Module::Get()->core()->CallOnMainThread(
52            DELTA_MS, pp::CompletionCallback(&TickCallback, data), PP_OK);
53}
54
55}
56#define main OLDMAIN
57#include "../test/tutorial/tut03.cpp"
58#undef main
59namespace lol {
60
61bool NaClInstance::Init(uint32_t /* argc */,
62                        const char* /* argn */[],
63                        const char* /* argv */[])
64{
65    Ticker::Setup(60.0f);
66
67    //new Kub();
68    //new DebugQuad();
69    new Fractal(ivec2(640, 480));
70
71    // My timer callback
72    pp::Module::Get()->core()->CallOnMainThread(
73            DELTA_MS, pp::CompletionCallback(&TickCallback, this), PP_OK);
74
75    return true;
76}
77
78void NaClInstance::HandleMessage(const pp::Var& message)
79{
80    if (!message.is_string())
81        return;
82    /* FIXME: do some shit here */
83}
84
85void NaClInstance::DidChangeView(const pp::Rect& position, const pp::Rect& clip)
86{
87    if (position.size().width() == m_size.x &&
88        position.size().height() == m_size.y)
89        return;  // Size didn't change, no need to update anything.
90
91    m_size = ivec2(position.size().width(), position.size().height());
92
93    if (opengl_context_ == NULL)
94        opengl_context_.reset(new OpenGLContext(this));
95    opengl_context_->InvalidateContext(this);
96    opengl_context_->ResizeContext(position.size());
97    if (!opengl_context_->MakeContextCurrent(this))
98        return;
99
100    Video::Setup(m_size);
101    DrawSelf();
102}
103
104void NaClInstance::DrawSelf()
105{
106    if (opengl_context_ == NULL)
107        return;
108
109    Ticker::ClampFps();
110    Ticker::TickGame();
111
112    opengl_context_->MakeContextCurrent(this);
113    Ticker::TickDraw();
114    opengl_context_->FlushContext();
115}
116
117}  // namespace lol
118
Note: See TracBrowser for help on using the repository browser.