\hypertarget{evas-buffer-simple_8c-example}{}\section{evas-\/buffer-\/simple.\+c}

\begin{DoxyCodeInclude}

\textcolor{preprocessor}{#include <Evas.h>}
\textcolor{preprocessor}{#include <Evas\_Engine\_Buffer.h>}
\textcolor{preprocessor}{#include <stdio.h>}
\textcolor{preprocessor}{#include <errno.h>}

\textcolor{preprocessor}{#define WIDTH (320)}
\textcolor{preprocessor}{#define HEIGHT (240)}

\textcolor{comment}{/*}
\textcolor{comment}{ * create\_canvas(), destroy\_canvas() and draw\_scene() are support functions.}
\textcolor{comment}{ *}
\textcolor{comment}{ * They are only required to use raw Evas, but for real world usage,}
\textcolor{comment}{ * it is recommended to use ecore and its ecore-evas submodule, that}
\textcolor{comment}{ * provide convenience canvas creators, integration with main loop and}
\textcolor{comment}{ * automatic render of updates (draw\_scene()) when system goes back to}
\textcolor{comment}{ * main loop.}
\textcolor{comment}{ */}

\textcolor{keyword}{static} \hyperlink{group__Evas__Canvas_ga5ff87cc4ce6bc43e3b640a6d37f73043}{Evas} *create\_canvas(\textcolor{keywordtype}{int} width, \textcolor{keywordtype}{int} height);
\textcolor{keyword}{static} \textcolor{keywordtype}{void} destroy\_canvas(\hyperlink{group__Evas__Canvas_ga5ff87cc4ce6bc43e3b640a6d37f73043}{Evas} *canvas);
\textcolor{keyword}{static} \textcolor{keywordtype}{void} draw\_scene(\hyperlink{group__Evas__Canvas_ga5ff87cc4ce6bc43e3b640a6d37f73043}{Evas} *canvas);

\textcolor{comment}{// support function to save scene as PPM image}
\textcolor{keyword}{static} \textcolor{keywordtype}{void} save\_scene(\hyperlink{group__Evas__Canvas_ga5ff87cc4ce6bc43e3b640a6d37f73043}{Evas} *canvas, \textcolor{keyword}{const} \textcolor{keywordtype}{char} *dest);

\textcolor{keywordtype}{int} main(\textcolor{keywordtype}{void})
\{
   \hyperlink{group__Evas__Canvas_ga5ff87cc4ce6bc43e3b640a6d37f73043}{Evas} *canvas;
   \hyperlink{group__Evas__Object__Group_ga9e19e6dd1f517a0ba437c0114d3e7c97}{Evas\_Object} *bg, *r1, *r2, *r3;

   \hyperlink{group__Evas__Main__Group_ga65b3f060b6dd9e488afd0b8cbe101869}{evas\_init}();

   \textcolor{comment}{// create your canvas}
   \textcolor{comment}{// NOTE: consider using ecore\_evas\_buffer\_new() instead!}
   canvas = create\_canvas(WIDTH, HEIGHT);
   \textcolor{keywordflow}{if} (!canvas)
     \textcolor{keywordflow}{return} -1;

   bg = \hyperlink{group__Evas__Object__Rectangle_ga4d80fc32ad1cac28428cebd65d746751}{evas\_object\_rectangle\_add}(canvas);
   \hyperlink{group__Evas__Object__Group__Basic_gae34bdcfd7f5100151f6ea8ef4a392949}{evas\_object\_color\_set}(bg, 255, 255, 255, 255); \textcolor{comment}{// white bg}
   \hyperlink{group__Evas__Font__Group_ga02da8091bbac768b8e86c7b74b2a94f9}{evas\_object\_move}(bg, 0, 0);                    \textcolor{comment}{// at origin}
   \hyperlink{group__Evas__Font__Group_ga21e8604c0e4a93e469bff4bd069e82b5}{evas\_object\_resize}(bg, WIDTH, HEIGHT);         \textcolor{comment}{// covers full canvas}
   \hyperlink{group__Evas__Object__Group__Basic_ga9cbc13661584e49fb9d9cdab514a1eeb}{evas\_object\_show}(bg);

   puts(\textcolor{stringliteral}{"initial scene, with just background:"});
   draw\_scene(canvas);

   r1 = \hyperlink{group__Evas__Object__Rectangle_ga4d80fc32ad1cac28428cebd65d746751}{evas\_object\_rectangle\_add}(canvas);
   \hyperlink{group__Evas__Object__Group__Basic_gae34bdcfd7f5100151f6ea8ef4a392949}{evas\_object\_color\_set}(r1, 255, 0, 0, 255); \textcolor{comment}{// 100% opaque red}
   \hyperlink{group__Evas__Font__Group_ga02da8091bbac768b8e86c7b74b2a94f9}{evas\_object\_move}(r1, 10, 10);
   \hyperlink{group__Evas__Font__Group_ga21e8604c0e4a93e469bff4bd069e82b5}{evas\_object\_resize}(r1, 100, 100);
   \hyperlink{group__Evas__Object__Group__Basic_ga9cbc13661584e49fb9d9cdab514a1eeb}{evas\_object\_show}(r1);

   \textcolor{comment}{// pay attention to transparency! Evas color values are pre-multiplied by}
   \textcolor{comment}{// alpha, so 50% opaque green is:}
   \textcolor{comment}{// non-premul: r=0, g=255, b=0    a=128 (50% alpha)}
   \textcolor{comment}{// premul:}
   \textcolor{comment}{//         r\_premul = r * a / 255 =      0 * 128 / 255 =      0}
   \textcolor{comment}{//         g\_premul = g * a / 255 =    255 * 128 / 255 =    128}
   \textcolor{comment}{//         b\_premul = b * a / 255 =      0 * 128 / 255 =      0}
   \textcolor{comment}{//}
   \textcolor{comment}{// this 50% green is over a red background, so it will show in the}
   \textcolor{comment}{// final output as yellow (green + red = yellow)}
   r2 = \hyperlink{group__Evas__Object__Rectangle_ga4d80fc32ad1cac28428cebd65d746751}{evas\_object\_rectangle\_add}(canvas);
   \hyperlink{group__Evas__Object__Group__Basic_gae34bdcfd7f5100151f6ea8ef4a392949}{evas\_object\_color\_set}(r2, 0, 128, 0, 128); \textcolor{comment}{// 50% opaque green}
   \hyperlink{group__Evas__Font__Group_ga02da8091bbac768b8e86c7b74b2a94f9}{evas\_object\_move}(r2, 10, 10);
   \hyperlink{group__Evas__Font__Group_ga21e8604c0e4a93e469bff4bd069e82b5}{evas\_object\_resize}(r2, 50, 50);
   \hyperlink{group__Evas__Object__Group__Basic_ga9cbc13661584e49fb9d9cdab514a1eeb}{evas\_object\_show}(r2);

   r3 = \hyperlink{group__Evas__Object__Rectangle_ga4d80fc32ad1cac28428cebd65d746751}{evas\_object\_rectangle\_add}(canvas);
   \hyperlink{group__Evas__Object__Group__Basic_gae34bdcfd7f5100151f6ea8ef4a392949}{evas\_object\_color\_set}(r3, 0, 128, 0, 255); \textcolor{comment}{// 100% opaque dark green}
   \hyperlink{group__Evas__Font__Group_ga02da8091bbac768b8e86c7b74b2a94f9}{evas\_object\_move}(r3, 60, 60);
   \hyperlink{group__Evas__Font__Group_ga21e8604c0e4a93e469bff4bd069e82b5}{evas\_object\_resize}(r3, 50, 50);
   \hyperlink{group__Evas__Object__Group__Basic_ga9cbc13661584e49fb9d9cdab514a1eeb}{evas\_object\_show}(r3);

   puts(\textcolor{stringliteral}{"final scene (note updates):"});
   draw\_scene(canvas);
   save\_scene(canvas, \textcolor{stringliteral}{"/tmp/evas-buffer-simple-render.ppm"});

   \textcolor{comment}{// NOTE: use ecore\_evas\_buffer\_new() and here ecore\_evas\_free()}
   destroy\_canvas(canvas);

   \hyperlink{group__Evas__Main__Group_ga95a8864f81d944a92bb012b77ffd1b09}{evas\_shutdown}();

   \textcolor{keywordflow}{return} 0;
\}

\textcolor{keyword}{static} \hyperlink{group__Evas__Canvas_ga5ff87cc4ce6bc43e3b640a6d37f73043}{Evas} *create\_canvas(\textcolor{keywordtype}{int} width, \textcolor{keywordtype}{int} height)
\{
   \hyperlink{group__Evas__Canvas_ga5ff87cc4ce6bc43e3b640a6d37f73043}{Evas} *canvas;
   Evas\_Engine\_Info\_Buffer *einfo;
   \textcolor{keywordtype}{int} method;
   \textcolor{keywordtype}{void} *pixels;

   method = \hyperlink{group__Evas__Output__Method_ga9f9871fdcc40ce1062d6565308020eb8}{evas\_render\_method\_lookup}(\textcolor{stringliteral}{"buffer"});
   \textcolor{keywordflow}{if} (method <= 0)
     \{
        fputs(\textcolor{stringliteral}{"ERROR: evas was not compiled with 'buffer' engine!\(\backslash\)n"}, stderr);
        \textcolor{keywordflow}{return} NULL;
     \}

   canvas = \hyperlink{group__Evas__Canvas_ga67e8eb17d2a676bfdfb8794fc5f72bc0}{evas\_new}();
   \textcolor{keywordflow}{if} (!canvas)
     \{
        fputs(\textcolor{stringliteral}{"ERROR: could not instantiate new evas canvas.\(\backslash\)n"}, stderr);
        \textcolor{keywordflow}{return} NULL;
     \}

   \hyperlink{group__Evas__Output__Method_ga17f2ac9f3bef135cd825774c33762ef2}{evas\_output\_method\_set}(canvas, method);
   \hyperlink{group__Evas__Output__Size_ga681f2af7d325735af201e18f56f6ef61}{evas\_output\_size\_set}(canvas, width, height);
   \hyperlink{group__Evas__Output__Size_ga0a14db31aa30b701688caf67a4649239}{evas\_output\_viewport\_set}(canvas, 0, 0, width, height);

   einfo = (Evas\_Engine\_Info\_Buffer *)\hyperlink{group__Evas__Output__Method_ga33a6cd095bc7499dbfcb39ad253607b1}{evas\_engine\_info\_get}(canvas);
   \textcolor{keywordflow}{if} (!einfo)
     \{
        fputs(\textcolor{stringliteral}{"ERROR: could not get evas engine info!\(\backslash\)n"}, stderr);
        \hyperlink{group__Evas__Canvas_gaa0d5d3f1a1e1ffa3b14b09bb374215b4}{evas\_free}(canvas);
        \textcolor{keywordflow}{return} NULL;
     \}

   \textcolor{comment}{// ARGB32 is sizeof(int), that is 4 bytes, per pixel}
   pixels = malloc(width * height * \textcolor{keyword}{sizeof}(\textcolor{keywordtype}{int}));
   \textcolor{keywordflow}{if} (!pixels)
     \{
        fputs(\textcolor{stringliteral}{"ERROR: could not allocate canvas pixels!\(\backslash\)n"}, stderr);
        \hyperlink{group__Evas__Canvas_gaa0d5d3f1a1e1ffa3b14b09bb374215b4}{evas\_free}(canvas);
        \textcolor{keywordflow}{return} NULL;
     \}

   einfo->info.depth\_type = EVAS\_ENGINE\_BUFFER\_DEPTH\_ARGB32;
   einfo->info.dest\_buffer = pixels;
   einfo->info.dest\_buffer\_row\_bytes = width * \textcolor{keyword}{sizeof}(int);
   einfo->info.use\_color\_key = 0;
   einfo->info.alpha\_threshold = 0;
   einfo->info.func.new\_update\_region = NULL;
   einfo->info.func.free\_update\_region = NULL;
   \hyperlink{group__Evas__Output__Method_ga7244385cf5bad0591ab82e4437c4694b}{evas\_engine\_info\_set}(canvas, (\hyperlink{struct__Evas__Engine__Info}{Evas\_Engine\_Info} *)einfo);

   \textcolor{keywordflow}{return} canvas;
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void} destroy\_canvas(\hyperlink{group__Evas__Canvas_ga5ff87cc4ce6bc43e3b640a6d37f73043}{Evas} *canvas)
\{
   Evas\_Engine\_Info\_Buffer *einfo;

   einfo = (Evas\_Engine\_Info\_Buffer *)\hyperlink{group__Evas__Output__Method_ga33a6cd095bc7499dbfcb39ad253607b1}{evas\_engine\_info\_get}(canvas);
   \textcolor{keywordflow}{if} (!einfo)
     \{
        fputs(\textcolor{stringliteral}{"ERROR: could not get evas engine info!\(\backslash\)n"}, stderr);
        \hyperlink{group__Evas__Canvas_gaa0d5d3f1a1e1ffa3b14b09bb374215b4}{evas\_free}(canvas);
        \textcolor{keywordflow}{return};
     \}

   free(einfo->info.dest\_buffer);
   \hyperlink{group__Evas__Canvas_gaa0d5d3f1a1e1ffa3b14b09bb374215b4}{evas\_free}(canvas);
\}

\textcolor{keyword}{static} \textcolor{keywordtype}{void} draw\_scene(\hyperlink{group__Evas__Canvas_ga5ff87cc4ce6bc43e3b640a6d37f73043}{Evas} *canvas)
\{
   \hyperlink{struct__Eina__List}{Eina\_List} *updates, *n;
   \hyperlink{struct__Eina__Rectangle}{Eina\_Rectangle} *update;

   \textcolor{comment}{// render and get the updated rectangles:}
   updates = \hyperlink{group__Evas__Canvas_ga1867d9f5f97545418598496af93e4c71}{evas\_render\_updates}(canvas);

   \textcolor{comment}{// informative only here, just print the updated areas:}
   \hyperlink{group__Eina__List__Group_gad02f746a08ee0b12b7ed66d4bc0d71d8}{EINA\_LIST\_FOREACH}(updates, n, update)
     printf("UPDATED REGION: pos: %3d, %3d    size: %3dx%3d\(\backslash\)n",
            update->x, update->y, update->w, update->h);

   \textcolor{comment}{// free list of updates}
   \hyperlink{group__Evas__Canvas_ga038d98c7c13a86b6ba4de51f8aba3595}{evas\_render\_updates\_free}(updates);
\}

static \textcolor{keywordtype}{void} save\_scene(\hyperlink{group__Evas__Canvas_ga5ff87cc4ce6bc43e3b640a6d37f73043}{Evas} *canvas, const \textcolor{keywordtype}{char} *dest)
\{
   Evas\_Engine\_Info\_Buffer *einfo;
   \textcolor{keyword}{const} \textcolor{keywordtype}{unsigned} \textcolor{keywordtype}{int} *pixels, *pixels\_end;
   \textcolor{keywordtype}{int} width, height;
   FILE *f;

   einfo = (Evas\_Engine\_Info\_Buffer *)\hyperlink{group__Evas__Output__Method_ga33a6cd095bc7499dbfcb39ad253607b1}{evas\_engine\_info\_get}(canvas);
   \textcolor{keywordflow}{if} (!einfo)
     \{
        fputs(\textcolor{stringliteral}{"ERROR: could not get evas engine info!\(\backslash\)n"}, stderr);
        \textcolor{keywordflow}{return};
     \}
   \hyperlink{group__Evas__Output__Size_gab2bf31509c58449bc94473f81b1e5ed5}{evas\_output\_size\_get}(canvas, &width, &height);

   f = fopen(dest, \textcolor{stringliteral}{"wb+"});
   \textcolor{keywordflow}{if} (!f)
     \{
        fprintf(stderr, \textcolor{stringliteral}{"ERROR: could not open for writing '%s': %s\(\backslash\)n"},
                dest, strerror(errno));
        \textcolor{keywordflow}{return};
     \}

   pixels = einfo->info.dest\_buffer;
   pixels\_end = pixels + (width * height);

   \textcolor{comment}{// PPM P6 format is dead simple to write:}
   fprintf(f, \textcolor{stringliteral}{"P6\(\backslash\)n%d %d\(\backslash\)n255\(\backslash\)n"},  width, height);
   \textcolor{keywordflow}{for} (; pixels < pixels\_end; pixels++)
     \{
        \textcolor{keywordtype}{int} r, g, b;

        r = ((*pixels) & 0xff0000) >> 16;
        g = ((*pixels) & 0x00ff00) >> 8;
        b = (*pixels) & 0x0000ff;

        fprintf(f, \textcolor{stringliteral}{"%c%c%c"}, r, g, b);
     \}

   fclose(f);
   printf(\textcolor{stringliteral}{"saved scene as '%s'\(\backslash\)n"}, dest);
\}
\end{DoxyCodeInclude}
 