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