Guest User

Untitled

a guest
Jan 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <assert.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <stdarg.h>
  6. #include <string.h>
  7. #include <errno.h>
  8. #include <math.h>
  9.  
  10. #include <sys/time.h>
  11.  
  12. #include <cairo.h>
  13.  
  14. #include "puzzles.h"
  15.  
  16. #define PACKAGE "Cube"
  17. #define CAIRO_KEY "Cairo4EvasImage"
  18.  
  19.  
  20. #ifdef DEBUGGING
  21.  
  22. void dputs(char *buf)
  23. {
  24. }
  25.  
  26. void debug_printf(char *fmt, ...)
  27. {
  28. }
  29. #endif
  30.  
  31. void fatal(char *fmt, ...)
  32. {
  33.     exit(1);
  34. }
  35.  
  36. struct frontend {
  37.     Evas_Object *win;
  38.     midend *me;
  39.     Evas_Object *evas_image;
  40.     cairo_t *cr;
  41.     cairo_surface_t *image;
  42.     int w, h;
  43.  
  44.     Evas_Object* nf;
  45.     Evas_Object* bg;
  46.     Evas_Object* ly;
  47.     Evas *evas;
  48.  
  49.     int ncolours;
  50.     float* colours;
  51.     int bbox_l, bbox_r, bbox_u, bbox_d;
  52.     int timer_active, timer_id;
  53. };
  54.  
  55. struct blitter {
  56.     cairo_surface_t *image;
  57.     Evas_Object *evas_image;
  58.     int w,h,x,y;
  59. };
  60.  
  61. void get_random_seed(void **randseed, int *randseedsize)
  62. {
  63.     struct timeval *tvp = snew(struct timeval);
  64.     gettimeofday(tvp, NULL);
  65.     *randseed = (void *)tvp;
  66.     *randseedsize = sizeof(struct timeval);
  67. }
  68.  
  69. void frontend_default_colour(frontend *fe, float *output)
  70. {
  71.     int r,g,b;
  72. //    elm_bg_color_get(fe->win, &r,&g,&b);
  73.     r = g = b = 65535;
  74.     output[0] = r/65535.0;
  75.     output[1] = g/65535.0;
  76.     output[2] = b/65535.0;
  77. }
  78.  
  79. static void set_colour(frontend *fe, int colour)
  80. {
  81.     cairo_set_source_rgb(fe->cr,
  82.             fe->colours[3*colour + 0],
  83.             fe->colours[3*colour + 1],
  84.             fe->colours[3*colour + 2]);
  85. }
  86.  
  87. void slp_draw_text(void *handle, int x, int y, int fonttype, int fontsize, int align, int colour, char *text)
  88. {
  89.     frontend *fe = (frontend*)handle;
  90. //    Evas_Object *text = evas_object_text_add(fe->evas);
  91. }
  92.  
  93. void slp_draw_rect(void *handle, int x, int y, int w, int h, int colour)
  94. {
  95.     frontend *fe = (frontend*)handle;
  96.     set_colour(fe, colour);
  97.  
  98.     cairo_save(fe->cr);
  99.     cairo_new_path(fe->cr);
  100.     cairo_set_antialias(fe->cr, CAIRO_ANTIALIAS_NONE);
  101.     cairo_rectangle(fe->cr, x, y, w, h);
  102.     cairo_fill(fe->cr);
  103.     cairo_restore(fe->cr);
  104. }
  105.  
  106. void slp_draw_line(void *handle, int x1, int y1, int x2, int y2, int colour)
  107. {
  108.     frontend *fe = (frontend*)handle;
  109.     set_colour(fe, colour);
  110.  
  111.     cairo_new_path(fe->cr);
  112.     cairo_move_to(fe->cr, x1 + 0.5, y1 + 0.5);
  113.     cairo_line_to(fe->cr, x2 + 0.5, y2 + 0.5);
  114.     cairo_stroke(fe->cr);
  115. }
  116.  
  117. void slp_draw_polygon(void *handle, int *coords, int npoints, int fillcolour, int outlinecolour)
  118. {
  119.     int i;
  120.     frontend *fe = (frontend*)handle;
  121.     cairo_new_path(fe->cr);
  122.     for (i = 0; i < npoints; i++)
  123.     cairo_line_to(fe->cr, coords[i*2] + 0.5, coords[i*2 + 1] + 0.5);
  124.     cairo_close_path(fe->cr);
  125.     if (fillcolour >= 0) {
  126.         set_colour(fe, fillcolour);
  127.     cairo_fill_preserve(fe->cr);
  128.     }
  129.     assert(outlinecolour >= 0);
  130.     set_colour(fe, outlinecolour);
  131.     cairo_stroke(fe->cr);
  132. }
  133.  
  134. void slp_draw_circle(void *handle, int cx, int cy, int radius, int fillcolour, int outlinecolour)
  135. {
  136.     frontend *fe = (frontend*)handle;
  137.  
  138.     cairo_new_path(fe->cr);
  139.     cairo_arc(fe->cr, cx + 0.5, cy + 0.5, radius, 0, 2*PI);
  140.     cairo_close_path(fe->cr);       /* Just in case... */
  141.     if (fillcolour >= 0) {
  142.     set_colour(fe, fillcolour);
  143.     cairo_fill_preserve(fe->cr);
  144.     }
  145.     assert(outlinecolour >= 0);
  146.     set_colour(fe, outlinecolour);
  147.     cairo_stroke(fe->cr);
  148. }
  149.  
  150. void slp_draw_update(void *handle, int x, int y, int w, int h)
  151. {
  152.     frontend *fe = (frontend*)handle;
  153.     if (fe->bbox_l > x  ) fe->bbox_l = x  ;
  154.     if (fe->bbox_r < x+w) fe->bbox_r = x+w;
  155.     if (fe->bbox_u > y  ) fe->bbox_u = y  ;
  156.     if (fe->bbox_d < y+h) fe->bbox_d = y+h;
  157. }
  158.  
  159. void slp_clip(void *handle, int x, int y, int w, int h)
  160. {
  161.     frontend *fe = (frontend*)handle;
  162.     cairo_new_path(fe->cr);
  163.     cairo_rectangle(fe->cr, x, y, w, h);
  164.     cairo_clip(fe->cr);
  165. }
  166.  
  167. void slp_unclip(void *handle)
  168. {
  169.     frontend *fe = (frontend*)handle;
  170.     cairo_reset_clip(fe->cr);
  171. }
  172.  
  173. void slp_start_draw(void *handle)
  174. {
  175.     frontend *fe = (frontend *)handle;
  176.  
  177.     fe->bbox_l = fe->w;
  178.     fe->bbox_r = 0;
  179.     fe->bbox_u = fe->h;
  180.     fe->bbox_d = 0;
  181.  
  182.     fe->cr = cairo_create(fe->image);
  183.     cairo_set_antialias(fe->cr, CAIRO_ANTIALIAS_GRAY);
  184.     cairo_set_line_width(fe->cr, 1.0);
  185.     cairo_set_line_cap(fe->cr, CAIRO_LINE_CAP_SQUARE);
  186.     cairo_set_line_join(fe->cr, CAIRO_LINE_JOIN_ROUND);
  187. }
  188.  
  189. void slp_end_draw(void *handle)
  190. {
  191.     int w,h;
  192.     frontend *fe = (frontend *)handle;
  193.     cairo_t *cr;
  194.  
  195.     cairo_destroy(fe->cr);
  196.     fe->cr = NULL;
  197.  
  198.     if(!fe->evas_image){
  199.         return;
  200.     }
  201.  
  202.     void* pixels = evas_object_image_data_get(fe->evas_image, EINA_TRUE);
  203.  
  204.     evas_object_image_size_get(fe->evas_image, &w, &h);
  205.  
  206.     cairo_surface_t *surface = cairo_image_surface_create_for_data(pixels, CAIRO_FORMAT_ARGB32, w, h, w*4);
  207.     cr = cairo_create(surface);
  208.     cairo_set_source_surface(cr, fe->image, 0, 0);
  209.     cairo_rectangle(cr, 0, 0, w, h);
  210.     cairo_fill(cr);
  211.     cairo_destroy(cr);
  212.  
  213.     w = cairo_image_surface_get_width(fe->image);
  214.     h = cairo_image_surface_get_width(fe->image);
  215.     evas_object_image_alpha_set(fe->evas_image, EINA_TRUE);
  216.     evas_object_image_size_set(fe->evas_image, w, h);
  217.     evas_object_image_fill_set(fe->evas_image, 0, 0, w, h);
  218.     evas_object_image_data_set(fe->evas_image, cairo_image_surface_get_data(fe->image));
  219.     evas_object_resize(fe->evas_image, w, h);
  220.  
  221. //    if (fe->bbox_l < fe->bbox_r && fe->bbox_u < fe->bbox_d) {
  222.         evas_object_image_data_update_add(fe->evas_image, 0, 0, w, h);
  223.         evas_object_image_pixels_dirty_set(fe->evas_image, EINA_TRUE);
  224. //    }
  225. }
  226.  
  227. void slp_status_bar(void *handle, char *text)
  228. {
  229.     frontend *fe = (frontend *)handle;
  230. }
  231.  
  232. blitter *slp_blitter_new(void *handle, int w, int h)
  233. {
  234.     blitter *bl = snew(blitter);
  235.     bl->image = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
  236.     bl->w = w;
  237.     bl->h = h;
  238.     return bl;
  239. }
  240.  
  241. void slp_blitter_free(void *handle, blitter *bl)
  242. {
  243.     cairo_surface_destroy(bl->image);
  244.     sfree(bl);
  245. }
  246.  
  247. void slp_blitter_save(void *handle, blitter *bl, int x, int y)
  248. {
  249.     frontend *fe = (frontend *)handle;
  250.  
  251.     cairo_t *cr = cairo_create(bl->image);
  252.     cairo_set_source_surface(cr, fe->image, -x, -y);
  253.     cairo_paint(cr);
  254.     cairo_destroy(cr);
  255.  
  256.     bl->x = x;
  257.     bl->y = y;
  258. }
  259.  
  260. void slp_blitter_load(void *handle, blitter *bl, int x, int y)
  261. {
  262.     frontend *fe = (frontend *)handle;
  263.     if (x == BLITTER_FROMSAVED && y == BLITTER_FROMSAVED) {
  264.         x = bl->x;
  265.         y = bl->y;
  266.     }
  267.     cairo_set_source_surface(fe->cr, bl->image, x, y);
  268.     cairo_paint(fe->cr);
  269. }
  270.  
  271. void slp_draw_thick_line(void *handle, float thickness, float x1, float y1, float x2, float y2, int colour)
  272. {
  273.     frontend *fe = (frontend *)handle;
  274.     set_colour(fe, colour);
  275.  
  276.     cairo_save(fe->cr);
  277.     cairo_set_line_width(fe->cr, thickness);
  278.     cairo_new_path(fe->cr);
  279.     cairo_move_to(fe->cr, x1, y1);
  280.     cairo_line_to(fe->cr, x2, y2);
  281.     cairo_stroke(fe->cr);
  282.     cairo_restore(fe->cr);
  283. }
  284.  
  285.  
  286. const struct drawing_api slp_drawing = {
  287.     slp_draw_text,
  288.     slp_draw_rect,
  289.     slp_draw_line,
  290.     slp_draw_polygon,
  291.     slp_draw_circle,
  292.     slp_draw_update,
  293.     slp_clip,
  294.     slp_unclip,
  295.     slp_start_draw,
  296.     slp_end_draw,
  297.     slp_status_bar,
  298.     slp_blitter_new,
  299.     slp_blitter_free,
  300.     slp_blitter_save,
  301.     slp_blitter_load,
  302.     NULL, NULL, NULL, NULL, NULL, NULL,
  303.     NULL, NULL,
  304.     NULL,
  305.     slp_draw_thick_line
  306. };
  307.  
  308. void deactivate_timer(frontend *fe)
  309. {
  310. }
  311.  
  312. void activate_timer(frontend *fe)
  313. {
  314. }
  315.  
  316. int main(int argc, char **argv)
  317. {
  318.     frontend fe;
  319.     ecore_init();
  320.     ecore_main_loop_glib_integrate();
  321.     ecore_evas_init();
  322.    
  323.  
  324.     fe.me = midend_new(&fe, &thegame, &slp_drawing, &fe);
  325.  
  326.     Ecore_Evas* ee = ecore_evas_software_x11_new(NULL, 0, 0, 0, 480, 640);
  327. //    ecore_evas_callback_resize_set(ee, efl_resize_window);
  328.     ecore_evas_title_set(ee, PACKAGE);
  329.     ecore_evas_borderless_set(ee, 0);
  330.     ecore_evas_shaped_set(ee, 0);
  331.  
  332.     fe.w = 480;
  333.     fe.h = 640;
  334.     ecore_evas_show(ee);
  335.     Evas* evas = ecore_evas_get(ee);
  336.     fe.evas = evas;
  337.  
  338.     Evas_Object *b = evas_object_rectangle_add(evas);
  339.     evas_object_color_set(b, 255, 255, 255, 255);
  340.     evas_object_move(b, 0, 0);
  341.     evas_object_resize(b, fe.w, fe.h);
  342.     evas_object_show(b);
  343.     fe.evas = evas_object_evas_get(b);
  344.  
  345.     fe.image = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, fe.w, fe.h);
  346.     fe.evas_image = evas_object_image_add(evas);
  347.  
  348.  
  349. //    evas_object_move(fe.evas_image, 0, 0);
  350. //    evas_object_resize(fe.evas_image, 480, 640);
  351.     fe.colours = midend_colours(fe.me, &fe.ncolours);
  352.     evas_object_show(fe.evas_image);
  353.  
  354.     // just start new game ;)
  355.     midend_new_game(fe.me);
  356.     midend_force_redraw(fe.me);
  357.  
  358.     ecore_main_loop_begin();
  359.  
  360.     midend_free(fe.me);
  361.     evas_shutdown();
  362.     ecore_shutdown();
  363.  
  364.     return 0;
  365. }
Add Comment
Please, Sign In to add comment