diff options
author | alokp@chromium.org <alokp@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-19 20:59:41 +0000 |
---|---|---|
committer | alokp@chromium.org <alokp@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-01-19 20:59:41 +0000 |
commit | 76fbe717255e22a0d326b518165750e8d27a82cb (patch) | |
tree | af6292c02db4c44c449d33bfe3ce70357ce41b22 /gpu | |
parent | d54713426ab132a5ca95d6239641aad580fcf799 (diff) | |
download | chromium_src-76fbe717255e22a0d326b518165750e8d27a82cb.zip chromium_src-76fbe717255e22a0d326b518165750e8d27a82cb.tar.gz chromium_src-76fbe717255e22a0d326b518165750e8d27a82cb.tar.bz2 |
Created a base class for examples taken from gles2 book. It encapsulates the well-defined pattern followed by the examples to eliminate duplicated code. Once we finalize this CL, I will go ahead and change all demos to use this base class.
Review URL: http://codereview.chromium.org/543110
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@36542 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'gpu')
-rw-r--r-- | gpu/demos/gles2_book/example.h | 75 | ||||
-rw-r--r-- | gpu/demos/hello_triangle/main.cc | 65 |
2 files changed, 90 insertions, 50 deletions
diff --git a/gpu/demos/gles2_book/example.h b/gpu/demos/gles2_book/example.h new file mode 100644 index 0000000..839dfe3 --- /dev/null +++ b/gpu/demos/gles2_book/example.h @@ -0,0 +1,75 @@ +// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Base class for OpenGL ES 2.0 book examples. + +#ifndef GPU_DEMOS_GLES2_BOOK_EXAMPLE_H_ +#define GPU_DEMOS_GLES2_BOOK_EXAMPLE_H_ + +#include "gpu/demos/app_framework/application.h" +#include "third_party/gles2_book/Common/Include/esUtil.h" + +namespace gpu { +namespace demos { +namespace gles2_book { + +typedef int InitFunc(ESContext* context); +typedef void UpdateFunc(ESContext* context, float elapsed_sec); +typedef void DrawFunc(ESContext* context); +typedef void ShutDownFunc(ESContext* context); + +// The examples taken from OpenGL 2.0 ES book follow a well-defined pattern. +// They all use one ESContext that holds a reference to a custom UserData. +// Each example provides four functions: +// 1. InitFunc to initialize OpenGL state and custom UserData +// 2. UpdateFunc is called before drawing each frame to update animation etc. +// 3. DrawFunc is called to do the actual frame rendering +// 4. ShutDownFunc is called when program terminates +// This class encapsulates this pattern to make it easier to write classes +// for each example. +template <typename UserData, + InitFunc init_func, + UpdateFunc update_func, + DrawFunc draw_func, + ShutDownFunc shut_down_func> +class Example : public gpu_demos::Application { + public: + Example() { + esInitContext(&context_); + memset(&user_data_, 0, sizeof(UserData)); + context_.userData = &user_data_; + } + virtual ~Example() { + shut_down_func(&context_); + } + + bool Init() { + if (!InitRenderContext()) return false; + + context_.width = width(); + context_.height = height(); + if (!init_func(&context_)) return false; + return true; + } + + protected: + virtual void Draw(float elapsed_sec) { + update_func(&context_, elapsed_sec); + draw_func(&context_); + } + + private: + ESContext context_; + UserData user_data_; + DISALLOW_COPY_AND_ASSIGN(Example); +}; + +// Many examples do need to update anything and hence do not provide an +// update function. This no-op function can be used for such examples. +inline void NoOpUpdateFunc(ESContext* context, float elapsed_sec) {} + +} // namespace gles2_book +} // namespace demos +} // namespace gpu +#endif // GPU_DEMOS_GLES2_BOOK_EXAMPLE_H_ diff --git a/gpu/demos/hello_triangle/main.cc b/gpu/demos/hello_triangle/main.cc index 12a819b..3e45ffc 100644 --- a/gpu/demos/hello_triangle/main.cc +++ b/gpu/demos/hello_triangle/main.cc @@ -7,60 +7,25 @@ // example is to demonstrate the basic concepts of // OpenGL ES 2.0 rendering. -#include "gpu/demos/app_framework/application.h" +#include "gpu/demos/gles2_book/example.h" #include "third_party/gles2_book/Chapter_2/Hello_Triangle/Hello_Triangle.h" -namespace gpu_demos { -class HelloTriangle : public Application { - public: - HelloTriangle(); - ~HelloTriangle(); - - bool Init(); - - protected: - virtual void Draw(float elapsed_sec); - - private: - ESContext context_; - HTUserData user_data_; - - DISALLOW_COPY_AND_ASSIGN(HelloTriangle); -}; - -HelloTriangle::HelloTriangle() { - esInitContext(&context_); - - memset(&user_data_, 0, sizeof(HTUserData)); - context_.userData = &user_data_; -} - -HelloTriangle::~HelloTriangle() { - htShutDown(&context_); -} - -bool HelloTriangle::Init() { - if (!Application::InitRenderContext()) return false; - - context_.width = width(); - context_.height = height(); - if (!htInit(&context_)) return false; - - return true; -} - -void HelloTriangle::Draw(float /*elapsed_sec*/) { - htDraw(&context_); -} -} // namespace gpu_demos +namespace gpu { +namespace demos { +namespace gles2_book { +typedef Example<HTUserData, + htInit, + NoOpUpdateFunc, + htDraw, + htShutDown> HelloTriangle; +} // namespace gles2_book +} // namespace demos +} // namespace gpu int main(int argc, char *argv[]) { - gpu_demos::HelloTriangle app; - if (!app.Init()) { - printf("Could not init.\n"); - return EXIT_FAILURE; - } + gpu::demos::gles2_book::HelloTriangle demo; + CHECK(demo.Init()); - app.MainLoop(); + demo.MainLoop(); return EXIT_SUCCESS; } |