OLD | NEW |
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Base class for OpenGL ES 2.0 book examples. | 5 // Base class for OpenGL ES 2.0 book examples. |
6 | 6 |
7 #ifndef GPU_DEMOS_GLES2_BOOK_EXAMPLE_H_ | 7 #ifndef GPU_DEMOS_GLES2_BOOK_EXAMPLE_H_ |
8 #define GPU_DEMOS_GLES2_BOOK_EXAMPLE_H_ | 8 #define GPU_DEMOS_GLES2_BOOK_EXAMPLE_H_ |
9 | 9 |
10 #include "gpu/demos/app_framework/application.h" | 10 #include "gpu/demos/framework/demo.h" |
11 #include "third_party/gles2_book/Common/Include/esUtil.h" | 11 #include "third_party/gles2_book/Common/Include/esUtil.h" |
12 | 12 |
13 namespace gpu { | 13 namespace gpu { |
14 namespace demos { | 14 namespace demos { |
15 namespace gles2_book { | 15 namespace gles2_book { |
16 | 16 |
17 typedef int InitFunc(ESContext* context); | 17 typedef int InitFunc(ESContext* context); |
18 typedef void UpdateFunc(ESContext* context, float elapsed_sec); | 18 typedef void UpdateFunc(ESContext* context, float elapsed_sec); |
19 typedef void DrawFunc(ESContext* context); | 19 typedef void DrawFunc(ESContext* context); |
20 typedef void ShutDownFunc(ESContext* context); | 20 typedef void ShutDownFunc(ESContext* context); |
21 | 21 |
22 // The examples taken from OpenGL 2.0 ES book follow a well-defined pattern. | 22 // The examples taken from OpenGL 2.0 ES book follow a well-defined pattern. |
23 // They all use one ESContext that holds a reference to a custom UserData. | 23 // They all use one ESContext that holds a reference to a custom UserData. |
24 // Each example provides four functions: | 24 // Each example provides four functions: |
25 // 1. InitFunc to initialize OpenGL state and custom UserData | 25 // 1. InitFunc to initialize OpenGL state and custom UserData |
26 // 2. UpdateFunc is called before drawing each frame to update animation etc. | 26 // 2. UpdateFunc is called before drawing each frame to update animation etc. |
27 // 3. DrawFunc is called to do the actual frame rendering | 27 // 3. DrawFunc is called to do the actual frame rendering |
28 // 4. ShutDownFunc is called when program terminates | 28 // 4. ShutDownFunc is called when program terminates |
29 // This class encapsulates this pattern to make it easier to write classes | 29 // This class encapsulates this pattern to make it easier to write classes |
30 // for each example. | 30 // for each example. |
31 template <typename UserData, | 31 template <typename UserData> |
32 InitFunc init_func, | 32 class Example : public gpu::demos::Demo { |
33 UpdateFunc update_func, | |
34 DrawFunc draw_func, | |
35 ShutDownFunc shut_down_func> | |
36 class Example : public gpu_demos::Application { | |
37 public: | 33 public: |
38 Example() { | 34 Example() |
| 35 : init_func_(NULL), |
| 36 update_func_(NULL), |
| 37 draw_func_(NULL), |
| 38 shut_down_func_(NULL) { |
39 esInitContext(&context_); | 39 esInitContext(&context_); |
40 memset(&user_data_, 0, sizeof(UserData)); | 40 memset(&user_data_, 0, sizeof(UserData)); |
41 context_.userData = &user_data_; | 41 context_.userData = &user_data_; |
42 } | 42 } |
43 virtual ~Example() { | 43 virtual ~Example() { |
44 shut_down_func(&context_); | 44 shut_down_func_(&context_); |
45 } | 45 } |
46 | 46 |
47 bool Init() { | 47 virtual bool InitGL() { |
48 if (!InitRenderContext()) return false; | 48 // Note that update_func is optional. |
| 49 CHECK(init_func_ && draw_func_ && shut_down_func_); |
49 | 50 |
50 context_.width = width(); | 51 context_.width = width(); |
51 context_.height = height(); | 52 context_.height = height(); |
52 if (!init_func(&context_)) return false; | 53 if (!init_func_(&context_)) return false; |
53 return true; | 54 return true; |
54 } | 55 } |
55 | 56 |
56 protected: | 57 protected: |
57 virtual void Draw(float elapsed_sec) { | 58 void RegisterCallbacks(InitFunc* init_func, |
58 update_func(&context_, elapsed_sec); | 59 UpdateFunc* update_func, |
59 draw_func(&context_); | 60 DrawFunc* draw_func, |
| 61 ShutDownFunc* shut_down_func) { |
| 62 init_func_ = init_func; |
| 63 update_func_ = update_func; |
| 64 draw_func_ = draw_func; |
| 65 shut_down_func_ = shut_down_func; |
| 66 } |
| 67 |
| 68 virtual void Render(float elapsed_sec) { |
| 69 if (update_func_) update_func_(&context_, elapsed_sec); |
| 70 draw_func_(&context_); |
60 } | 71 } |
61 | 72 |
62 private: | 73 private: |
63 ESContext context_; | 74 ESContext context_; |
64 UserData user_data_; | 75 UserData user_data_; |
| 76 |
| 77 // Callback functions. |
| 78 InitFunc* init_func_; |
| 79 UpdateFunc* update_func_; |
| 80 DrawFunc* draw_func_; |
| 81 ShutDownFunc* shut_down_func_; |
| 82 |
65 DISALLOW_COPY_AND_ASSIGN(Example); | 83 DISALLOW_COPY_AND_ASSIGN(Example); |
66 }; | 84 }; |
67 | 85 |
68 // Many examples do need to update anything and hence do not provide an | |
69 // update function. This no-op function can be used for such examples. | |
70 inline void NoOpUpdateFunc(ESContext* context, float elapsed_sec) {} | |
71 | |
72 } // namespace gles2_book | 86 } // namespace gles2_book |
73 } // namespace demos | 87 } // namespace demos |
74 } // namespace gpu | 88 } // namespace gpu |
75 #endif // GPU_DEMOS_GLES2_BOOK_EXAMPLE_H_ | 89 #endif // GPU_DEMOS_GLES2_BOOK_EXAMPLE_H_ |
OLD | NEW |