HelloWorld.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright 2017 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "example/HelloWorld.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkFont.h"
  10. #include "include/core/SkGraphics.h"
  11. #include "include/core/SkSurface.h"
  12. #include "include/effects/SkGradientShader.h"
  13. using namespace sk_app;
  14. Application* Application::Create(int argc, char** argv, void* platformData) {
  15. return new HelloWorld(argc, argv, platformData);
  16. }
  17. HelloWorld::HelloWorld(int argc, char** argv, void* platformData)
  18. : fBackendType(Window::kNativeGL_BackendType)
  19. , fRotationAngle(0) {
  20. SkGraphics::Init();
  21. fWindow = Window::CreateNativeWindow(platformData);
  22. fWindow->setRequestedDisplayParams(DisplayParams());
  23. // register callbacks
  24. fWindow->pushLayer(this);
  25. fWindow->attach(fBackendType);
  26. }
  27. HelloWorld::~HelloWorld() {
  28. fWindow->detach();
  29. delete fWindow;
  30. }
  31. void HelloWorld::updateTitle() {
  32. if (!fWindow || fWindow->sampleCount() <= 1) {
  33. return;
  34. }
  35. SkString title("Hello World ");
  36. title.append(Window::kRaster_BackendType == fBackendType ? "Raster" : "OpenGL");
  37. fWindow->setTitle(title.c_str());
  38. }
  39. void HelloWorld::onBackendCreated() {
  40. this->updateTitle();
  41. fWindow->show();
  42. fWindow->inval();
  43. }
  44. void HelloWorld::onPaint(SkSurface* surface) {
  45. auto canvas = surface->getCanvas();
  46. // Clear background
  47. canvas->clear(SK_ColorWHITE);
  48. SkPaint paint;
  49. paint.setColor(SK_ColorRED);
  50. // Draw a rectangle with red paint
  51. SkRect rect = SkRect::MakeXYWH(10, 10, 128, 128);
  52. canvas->drawRect(rect, paint);
  53. // Set up a linear gradient and draw a circle
  54. {
  55. SkPoint linearPoints[] = { { 0, 0 }, { 300, 300 } };
  56. SkColor linearColors[] = { SK_ColorGREEN, SK_ColorBLACK };
  57. paint.setShader(SkGradientShader::MakeLinear(linearPoints, linearColors, nullptr, 2,
  58. SkTileMode::kMirror));
  59. paint.setAntiAlias(true);
  60. canvas->drawCircle(200, 200, 64, paint);
  61. // Detach shader
  62. paint.setShader(nullptr);
  63. }
  64. // Draw a message with a nice black paint
  65. SkFont font;
  66. font.setSubpixel(true);
  67. font.setSize(20);
  68. paint.setColor(SK_ColorBLACK);
  69. canvas->save();
  70. static const char message[] = "Hello World";
  71. // Translate and rotate
  72. canvas->translate(300, 300);
  73. fRotationAngle += 0.2f;
  74. if (fRotationAngle > 360) {
  75. fRotationAngle -= 360;
  76. }
  77. canvas->rotate(fRotationAngle);
  78. // Draw the text
  79. canvas->drawSimpleText(message, strlen(message), SkTextEncoding::kUTF8, 0, 0, font, paint);
  80. canvas->restore();
  81. }
  82. void HelloWorld::onIdle() {
  83. // Just re-paint continously
  84. fWindow->inval();
  85. }
  86. bool HelloWorld::onChar(SkUnichar c, ModifierKey modifiers) {
  87. if (' ' == c) {
  88. fBackendType = Window::kRaster_BackendType == fBackendType ? Window::kNativeGL_BackendType
  89. : Window::kRaster_BackendType;
  90. fWindow->detach();
  91. fWindow->attach(fBackendType);
  92. }
  93. return true;
  94. }