Guest User

Ghost OS - Canvas sample code

a guest
Aug 19th, 2016
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.12 KB | None | 0 0
  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  2.  *                                                                           *
  3.  *             Canvas example for the Ghost operating system                 *
  4.  *                                                                           *
  5.  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  6.  
  7. #include <ghostuser/ui/ui.hpp>
  8. #include <ghostuser/ui/window.hpp>
  9. #include <ghostuser/ui/canvas.hpp>
  10. #include <ghostuser/ui/textfield.hpp>
  11. #include <iostream>
  12. #include <math.h>
  13.  
  14. /**
  15.  * This example application creates a window with a canvas and draws one of the
  16.  * cairo samples on it.
  17.  */
  18. int main(int argc, char** argv) {
  19.  
  20.     // Open the UI
  21.     g_ui_open_status open_stat = g_ui::open();
  22.     if (open_stat != G_UI_OPEN_STATUS_SUCCESSFUL) {
  23.         std::cerr << "User interface could not be initialized" << std::endl;
  24.         return -1;
  25.     }
  26.  
  27.     // Create a window
  28.     g_window* window = g_window::create();
  29.     window->setTitle("Test Window");
  30.  
  31.     // Create the canvas
  32.     g_canvas* canvas = g_canvas::create();
  33.     window->addChild(canvas);
  34.  
  35.     // Resize the window
  36.     g_rectangle windowBounds(200, 200, 270, 270);
  37.     window->setBounds(windowBounds);
  38.     window->setVisible(true);
  39.     canvas->setBounds(g_rectangle(0, 0, windowBounds.width, windowBounds.height));
  40.  
  41.     // Variables for our surface
  42.     cairo_surface_t* surface = 0;
  43.     g_color_argb* surfaceBuffer = 0;
  44.  
  45.     while (true) {
  46.         // Resize canvas when window is resized
  47.         auto windowBounds = window->getBounds();
  48.         if (canvas->getBounds() != windowBounds) {
  49.             canvas->setBounds(g_rectangle(0, 0, windowBounds.width, windowBounds.height));
  50.         }
  51.  
  52.         // Get buffer
  53.         auto bufferInfo = canvas->getBuffer();
  54.         if (bufferInfo.buffer == 0) {
  55.             g_sleep(100);
  56.             continue;
  57.         }
  58.  
  59.         // Get the surface ready and go:
  60.         if (surface == 0 || surfaceBuffer != bufferInfo.buffer) {
  61.             surface = cairo_image_surface_create_for_data((uint8_t*) bufferInfo.buffer, CAIRO_FORMAT_ARGB32,
  62.                     bufferInfo.width, bufferInfo.height, cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32,
  63.                     bufferInfo.width));
  64.             surfaceBuffer = bufferInfo.buffer;
  65.         }
  66.  
  67.         // Perform the painting
  68.         auto cr = cairo_create(surface);
  69.  
  70.         // Clear the background
  71.         cairo_save(cr);
  72.         cairo_set_source_rgba(cr, 0, 0, 0, 0);
  73.         cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
  74.         cairo_paint(cr);
  75.         cairo_restore(cr);
  76.  
  77.         // Draw the example
  78.         double xc = 128.0;
  79.         double yc = 128.0;
  80.         double radius = 100.0;
  81.         double angle1 = 45.0 * (M_PI / 180.0);
  82.         double angle2 = 180.0 * (M_PI / 180.0);
  83.  
  84.         cairo_set_line_width(cr, 10.0);
  85.         cairo_arc_negative(cr, xc, yc, radius, angle1, angle2);
  86.         cairo_stroke(cr);
  87.  
  88.         cairo_set_source_rgba(cr, 1, 0.2, 0.2, 0.6);
  89.         cairo_set_line_width(cr, 6.0);
  90.  
  91.         cairo_arc(cr, xc, yc, 10.0, 0, 2 * M_PI);
  92.         cairo_fill(cr);
  93.  
  94.         cairo_arc(cr, xc, yc, radius, angle1, angle1);
  95.         cairo_line_to(cr, xc, yc);
  96.         cairo_arc(cr, xc, yc, radius, angle2, angle2);
  97.         cairo_line_to(cr, xc, yc);
  98.         cairo_stroke(cr);
  99.  
  100.         // Blit the content to screen
  101.         canvas->blit(g_rectangle(0, 0, bufferInfo.width, bufferInfo.height));
  102.  
  103.         g_sleep(100);
  104.     }
  105. }
Add Comment
Please, Sign In to add comment