| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #include "mojo/examples/sample_app/gles2_client_impl.h" | 5 #include "mojo/examples/sample_app/gles2_client_impl.h" |
| 6 | 6 |
| 7 #include <GLES2/gl2.h> | 7 #include <GLES2/gl2.h> |
| 8 #include <GLES2/gl2ext.h> | 8 #include <GLES2/gl2ext.h> |
| 9 #include <math.h> | 9 #include <math.h> |
| 10 #include <stdlib.h> | 10 #include <stdlib.h> |
| 11 | 11 |
| 12 #include "mojo/public/c/gles2/gles2.h" | 12 #include "mojo/public/c/gles2/gles2.h" |
| 13 #include "ui/events/event_constants.h" | 13 #include "ui/events/event_constants.h" |
| 14 | 14 |
| 15 namespace examples { | 15 namespace examples { |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 float CalculateDragDistance(const gfx::PointF& start, const mojo::Point& end) { | 18 float CalculateDragDistance(const mojo::Point& start, const mojo::Point& end) { |
| 19 return hypot(start.x() - end.x, start.y() - end.y); | 19 return hypot(static_cast<float>(start.x - end.x), |
| 20 static_cast<float>(start.y - end.y)); |
| 20 } | 21 } |
| 21 | 22 |
| 22 float GetRandomColor() { | 23 float GetRandomColor() { |
| 23 return static_cast<float>(rand()) / static_cast<float>(RAND_MAX); | 24 return static_cast<float>(rand()) / static_cast<float>(RAND_MAX); |
| 24 } | 25 } |
| 25 | 26 |
| 26 } | 27 } |
| 27 | 28 |
| 28 GLES2ClientImpl::GLES2ClientImpl(mojo::CommandBufferPtr command_buffer) | 29 GLES2ClientImpl::GLES2ClientImpl(mojo::CommandBufferPtr command_buffer) |
| 29 : getting_animation_frames_(false) { | 30 : getting_animation_frames_(false) { |
| 30 context_ = MojoGLES2CreateContext( | 31 context_ = MojoGLES2CreateContext( |
| 31 command_buffer.PassMessagePipe().release().value(), | 32 command_buffer.PassMessagePipe().release().value(), |
| 32 &ContextLostThunk, | 33 &ContextLostThunk, |
| 33 &DrawAnimationFrameThunk, | 34 &DrawAnimationFrameThunk, |
| 34 this); | 35 this); |
| 35 MojoGLES2MakeCurrent(context_); | 36 MojoGLES2MakeCurrent(context_); |
| 36 } | 37 } |
| 37 | 38 |
| 38 GLES2ClientImpl::~GLES2ClientImpl() { | 39 GLES2ClientImpl::~GLES2ClientImpl() { |
| 39 MojoGLES2DestroyContext(context_); | 40 MojoGLES2DestroyContext(context_); |
| 40 } | 41 } |
| 41 | 42 |
| 42 void GLES2ClientImpl::SetSize(const mojo::Size& size) { | 43 void GLES2ClientImpl::SetSize(const mojo::Size& size) { |
| 43 size_ = gfx::Size(size.width, size.height); | 44 size_ = size; |
| 44 if (size_.IsEmpty()) | 45 if (size_.width == 0 || size_.height == 0) |
| 45 return; | 46 return; |
| 46 cube_.Init(size_.width(), size_.height()); | 47 cube_.Init(size_.width, size_.height); |
| 47 RequestAnimationFrames(); | 48 RequestAnimationFrames(); |
| 48 } | 49 } |
| 49 | 50 |
| 50 void GLES2ClientImpl::HandleInputEvent(const mojo::Event& event) { | 51 void GLES2ClientImpl::HandleInputEvent(const mojo::Event& event) { |
| 51 switch (event.action) { | 52 switch (event.action) { |
| 52 case ui::ET_MOUSE_PRESSED: | 53 case ui::ET_MOUSE_PRESSED: |
| 53 case ui::ET_TOUCH_PRESSED: | 54 case ui::ET_TOUCH_PRESSED: |
| 54 if (event.flags & ui::EF_RIGHT_MOUSE_BUTTON) | 55 if (event.flags & ui::EF_RIGHT_MOUSE_BUTTON) |
| 55 break; | 56 break; |
| 56 CancelAnimationFrames(); | 57 CancelAnimationFrames(); |
| 57 capture_point_.SetPoint(event.location->x, event.location->y); | 58 capture_point_ = *event.location; |
| 58 last_drag_point_ = capture_point_; | 59 last_drag_point_ = capture_point_; |
| 59 drag_start_time_ = mojo::GetTimeTicksNow(); | 60 drag_start_time_ = mojo::GetTimeTicksNow(); |
| 60 break; | 61 break; |
| 61 case ui::ET_MOUSE_DRAGGED: | 62 case ui::ET_MOUSE_DRAGGED: |
| 62 case ui::ET_TOUCH_MOVED: | 63 case ui::ET_TOUCH_MOVED: |
| 63 if (event.flags & ui::EF_RIGHT_MOUSE_BUTTON) | 64 if (event.flags & ui::EF_RIGHT_MOUSE_BUTTON) |
| 64 break; | 65 break; |
| 65 if (!getting_animation_frames_) { | 66 if (!getting_animation_frames_) { |
| 66 int direction = event.location->y < last_drag_point_.y() || | 67 int direction = event.location->y < last_drag_point_.y || |
| 67 event.location->x > last_drag_point_.x() ? 1 : -1; | 68 event.location->x > last_drag_point_.x ? 1 : -1; |
| 68 cube_.set_direction(direction); | 69 cube_.set_direction(direction); |
| 69 cube_.UpdateForDragDistance( | 70 cube_.UpdateForDragDistance( |
| 70 CalculateDragDistance(last_drag_point_, *event.location)); | 71 CalculateDragDistance(last_drag_point_, *event.location)); |
| 71 cube_.Draw(); | 72 cube_.Draw(); |
| 72 MojoGLES2SwapBuffers(); | 73 MojoGLES2SwapBuffers(); |
| 73 | 74 |
| 74 last_drag_point_.SetPoint(event.location->x, event.location->y); | 75 last_drag_point_ = *event.location; |
| 75 } | 76 } |
| 76 break; | 77 break; |
| 77 case ui::ET_MOUSE_RELEASED: | 78 case ui::ET_MOUSE_RELEASED: |
| 78 case ui::ET_TOUCH_RELEASED: { | 79 case ui::ET_TOUCH_RELEASED: { |
| 79 if (event.flags & ui::EF_RIGHT_MOUSE_BUTTON) { | 80 if (event.flags & ui::EF_RIGHT_MOUSE_BUTTON) { |
| 80 cube_.set_color(GetRandomColor(), GetRandomColor(), GetRandomColor()); | 81 cube_.set_color(GetRandomColor(), GetRandomColor(), GetRandomColor()); |
| 81 break; | 82 break; |
| 82 } | 83 } |
| 83 MojoTimeTicks offset = mojo::GetTimeTicksNow() - drag_start_time_; | 84 MojoTimeTicks offset = mojo::GetTimeTicksNow() - drag_start_time_; |
| 84 float delta = static_cast<float>(offset) / 1000000.; | 85 float delta = static_cast<float>(offset) / 1000000.; |
| 85 cube_.SetFlingMultiplier( | 86 cube_.SetFlingMultiplier( |
| 86 CalculateDragDistance(capture_point_, *event.location), | 87 CalculateDragDistance(capture_point_, *event.location), |
| 87 delta); | 88 delta); |
| 88 | 89 |
| 89 capture_point_ = last_drag_point_ = gfx::PointF(); | 90 capture_point_ = last_drag_point_ = mojo::Point(); |
| 90 RequestAnimationFrames(); | 91 RequestAnimationFrames(); |
| 91 break; | 92 break; |
| 92 } | 93 } |
| 93 default: | 94 default: |
| 94 break; | 95 break; |
| 95 } | 96 } |
| 96 } | 97 } |
| 97 | 98 |
| 98 void GLES2ClientImpl::ContextLost() { | 99 void GLES2ClientImpl::ContextLost() { |
| 99 CancelAnimationFrames(); | 100 CancelAnimationFrames(); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 123 MojoGLES2RequestAnimationFrames(context_); | 124 MojoGLES2RequestAnimationFrames(context_); |
| 124 last_time_ = mojo::GetTimeTicksNow(); | 125 last_time_ = mojo::GetTimeTicksNow(); |
| 125 } | 126 } |
| 126 | 127 |
| 127 void GLES2ClientImpl::CancelAnimationFrames() { | 128 void GLES2ClientImpl::CancelAnimationFrames() { |
| 128 getting_animation_frames_ = false; | 129 getting_animation_frames_ = false; |
| 129 MojoGLES2CancelAnimationFrames(context_); | 130 MojoGLES2CancelAnimationFrames(context_); |
| 130 } | 131 } |
| 131 | 132 |
| 132 } // namespace examples | 133 } // namespace examples |
| OLD | NEW |