| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <math.h> | 5 #include <math.h> |
| 6 #include <stdio.h> | 6 #include <stdio.h> |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "ppapi/cpp/completion_callback.h" | 9 #include "ppapi/cpp/completion_callback.h" |
| 10 #include "ppapi/cpp/graphics_2d.h" | 10 #include "ppapi/cpp/graphics_2d.h" |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 return false; | 68 return false; |
| 69 } | 69 } |
| 70 | 70 |
| 71 void View::Draw() { | 71 void View::Draw() { |
| 72 uint32_t* pixels = static_cast<uint32_t*>(pixel_buffer_->data()); | 72 uint32_t* pixels = static_cast<uint32_t*>(pixel_buffer_->data()); |
| 73 if (NULL == pixels) | 73 if (NULL == pixels) |
| 74 return; | 74 return; |
| 75 // Clear the buffer | 75 // Clear the buffer |
| 76 const int32_t height = pixel_buffer_->size().height(); | 76 const int32_t height = pixel_buffer_->size().height(); |
| 77 const int32_t width = pixel_buffer_->size().width(); | 77 const int32_t width = pixel_buffer_->size().width(); |
| 78 const float radius2 = (ball_rect_.width() / 2) * (ball_rect_.width() / 2); |
| 78 for (int32_t py = 0; py < height; ++py) { | 79 for (int32_t py = 0; py < height; ++py) { |
| 79 for (int32_t px = 0; px < width; ++px) { | 80 for (int32_t px = 0; px < width; ++px) { |
| 80 const int32_t pos = px + py * width; | 81 const int32_t pos = px + py * width; |
| 81 uint32_t color = kOpaqueColorMask; | 82 uint32_t color = kOpaqueColorMask; |
| 82 // Draw the paddles | 83 // Draw the paddles |
| 83 if (left_paddle_rect_.Contains(px, py) || | 84 if (left_paddle_rect_.Contains(px, py) || |
| 84 right_paddle_rect_.Contains(px, py)) { | 85 right_paddle_rect_.Contains(px, py)) { |
| 85 color |= kWhiteMask; | 86 color |= kWhiteMask; |
| 86 } else { | 87 } else { |
| 87 pp::Point center_point = ball_rect_.CenterPoint(); | 88 pp::Point center_point = ball_rect_.CenterPoint(); |
| 88 float radius = ball_rect_.width() / 2; | |
| 89 float distance_x = px - center_point.x(); | 89 float distance_x = px - center_point.x(); |
| 90 float distance_y = py - center_point.y(); | 90 float distance_y = py - center_point.y(); |
| 91 float distance = | 91 float distance2 = distance_x * distance_x + distance_y * distance_y; |
| 92 sqrt(distance_x * distance_x + distance_y * distance_y); | |
| 93 // Draw the ball | 92 // Draw the ball |
| 94 if (distance <= radius) | 93 if (distance2 <= radius2) |
| 95 color |= kWhiteMask; | 94 color |= kWhiteMask; |
| 96 } | 95 } |
| 97 pixels[pos] = color; | 96 pixels[pos] = color; |
| 98 } | 97 } |
| 99 } | 98 } |
| 100 | 99 |
| 101 FlushPixelBuffer(); | 100 FlushPixelBuffer(); |
| 102 } | 101 } |
| 103 | 102 |
| 104 void View::UpdateView(const pp::Rect& position, | 103 void View::UpdateView(const pp::Rect& position, |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 return; | 152 return; |
| 154 flush_pending_ = true; | 153 flush_pending_ = true; |
| 155 graphics_2d_context_->Flush(pp::CompletionCallback(&FlushCallback, this)); | 154 graphics_2d_context_->Flush(pp::CompletionCallback(&FlushCallback, this)); |
| 156 } | 155 } |
| 157 | 156 |
| 158 bool View::IsContextValid() const { | 157 bool View::IsContextValid() const { |
| 159 return graphics_2d_context_ != NULL; | 158 return graphics_2d_context_ != NULL; |
| 160 } | 159 } |
| 161 | 160 |
| 162 } // namespace pong | 161 } // namespace pong |
| OLD | NEW |