Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <assert.h>
- #include <stdlib.h>
- #include <time.h>
- #include <stdarg.h>
- #include <string.h>
- #include <errno.h>
- #include <math.h>
- #include <sys/time.h>
- #include <cairo.h>
- #include "puzzles.h"
- #define PACKAGE "Cube"
- #define CAIRO_KEY "Cairo4EvasImage"
- #ifdef DEBUGGING
- void dputs(char *buf)
- {
- }
- void debug_printf(char *fmt, ...)
- {
- }
- #endif
- void fatal(char *fmt, ...)
- {
- exit(1);
- }
- struct frontend {
- Evas_Object *win;
- midend *me;
- Evas_Object *evas_image;
- cairo_t *cr;
- cairo_surface_t *image;
- int w, h;
- Evas_Object* nf;
- Evas_Object* bg;
- Evas_Object* ly;
- Evas *evas;
- int ncolours;
- float* colours;
- int bbox_l, bbox_r, bbox_u, bbox_d;
- int timer_active, timer_id;
- };
- struct blitter {
- cairo_surface_t *image;
- Evas_Object *evas_image;
- int w,h,x,y;
- };
- void get_random_seed(void **randseed, int *randseedsize)
- {
- struct timeval *tvp = snew(struct timeval);
- gettimeofday(tvp, NULL);
- *randseed = (void *)tvp;
- *randseedsize = sizeof(struct timeval);
- }
- void frontend_default_colour(frontend *fe, float *output)
- {
- int r,g,b;
- // elm_bg_color_get(fe->win, &r,&g,&b);
- r = g = b = 65535;
- output[0] = r/65535.0;
- output[1] = g/65535.0;
- output[2] = b/65535.0;
- }
- static void set_colour(frontend *fe, int colour)
- {
- cairo_set_source_rgb(fe->cr,
- fe->colours[3*colour + 0],
- fe->colours[3*colour + 1],
- fe->colours[3*colour + 2]);
- }
- void slp_draw_text(void *handle, int x, int y, int fonttype, int fontsize, int align, int colour, char *text)
- {
- frontend *fe = (frontend*)handle;
- // Evas_Object *text = evas_object_text_add(fe->evas);
- }
- void slp_draw_rect(void *handle, int x, int y, int w, int h, int colour)
- {
- frontend *fe = (frontend*)handle;
- set_colour(fe, colour);
- cairo_save(fe->cr);
- cairo_new_path(fe->cr);
- cairo_set_antialias(fe->cr, CAIRO_ANTIALIAS_NONE);
- cairo_rectangle(fe->cr, x, y, w, h);
- cairo_fill(fe->cr);
- cairo_restore(fe->cr);
- }
- void slp_draw_line(void *handle, int x1, int y1, int x2, int y2, int colour)
- {
- frontend *fe = (frontend*)handle;
- set_colour(fe, colour);
- cairo_new_path(fe->cr);
- cairo_move_to(fe->cr, x1 + 0.5, y1 + 0.5);
- cairo_line_to(fe->cr, x2 + 0.5, y2 + 0.5);
- cairo_stroke(fe->cr);
- }
- void slp_draw_polygon(void *handle, int *coords, int npoints, int fillcolour, int outlinecolour)
- {
- int i;
- frontend *fe = (frontend*)handle;
- cairo_new_path(fe->cr);
- for (i = 0; i < npoints; i++)
- cairo_line_to(fe->cr, coords[i*2] + 0.5, coords[i*2 + 1] + 0.5);
- cairo_close_path(fe->cr);
- if (fillcolour >= 0) {
- set_colour(fe, fillcolour);
- cairo_fill_preserve(fe->cr);
- }
- assert(outlinecolour >= 0);
- set_colour(fe, outlinecolour);
- cairo_stroke(fe->cr);
- }
- void slp_draw_circle(void *handle, int cx, int cy, int radius, int fillcolour, int outlinecolour)
- {
- frontend *fe = (frontend*)handle;
- cairo_new_path(fe->cr);
- cairo_arc(fe->cr, cx + 0.5, cy + 0.5, radius, 0, 2*PI);
- cairo_close_path(fe->cr); /* Just in case... */
- if (fillcolour >= 0) {
- set_colour(fe, fillcolour);
- cairo_fill_preserve(fe->cr);
- }
- assert(outlinecolour >= 0);
- set_colour(fe, outlinecolour);
- cairo_stroke(fe->cr);
- }
- void slp_draw_update(void *handle, int x, int y, int w, int h)
- {
- frontend *fe = (frontend*)handle;
- if (fe->bbox_l > x ) fe->bbox_l = x ;
- if (fe->bbox_r < x+w) fe->bbox_r = x+w;
- if (fe->bbox_u > y ) fe->bbox_u = y ;
- if (fe->bbox_d < y+h) fe->bbox_d = y+h;
- }
- void slp_clip(void *handle, int x, int y, int w, int h)
- {
- frontend *fe = (frontend*)handle;
- cairo_new_path(fe->cr);
- cairo_rectangle(fe->cr, x, y, w, h);
- cairo_clip(fe->cr);
- }
- void slp_unclip(void *handle)
- {
- frontend *fe = (frontend*)handle;
- cairo_reset_clip(fe->cr);
- }
- void slp_start_draw(void *handle)
- {
- frontend *fe = (frontend *)handle;
- fe->bbox_l = fe->w;
- fe->bbox_r = 0;
- fe->bbox_u = fe->h;
- fe->bbox_d = 0;
- fe->cr = cairo_create(fe->image);
- cairo_set_antialias(fe->cr, CAIRO_ANTIALIAS_GRAY);
- cairo_set_line_width(fe->cr, 1.0);
- cairo_set_line_cap(fe->cr, CAIRO_LINE_CAP_SQUARE);
- cairo_set_line_join(fe->cr, CAIRO_LINE_JOIN_ROUND);
- }
- void slp_end_draw(void *handle)
- {
- int w,h;
- frontend *fe = (frontend *)handle;
- cairo_t *cr;
- cairo_destroy(fe->cr);
- fe->cr = NULL;
- if(!fe->evas_image){
- return;
- }
- void* pixels = evas_object_image_data_get(fe->evas_image, EINA_TRUE);
- evas_object_image_size_get(fe->evas_image, &w, &h);
- cairo_surface_t *surface = cairo_image_surface_create_for_data(pixels, CAIRO_FORMAT_ARGB32, w, h, w*4);
- cr = cairo_create(surface);
- cairo_set_source_surface(cr, fe->image, 0, 0);
- cairo_rectangle(cr, 0, 0, w, h);
- cairo_fill(cr);
- cairo_destroy(cr);
- w = cairo_image_surface_get_width(fe->image);
- h = cairo_image_surface_get_width(fe->image);
- evas_object_image_alpha_set(fe->evas_image, EINA_TRUE);
- evas_object_image_size_set(fe->evas_image, w, h);
- evas_object_image_fill_set(fe->evas_image, 0, 0, w, h);
- evas_object_image_data_set(fe->evas_image, cairo_image_surface_get_data(fe->image));
- evas_object_resize(fe->evas_image, w, h);
- // if (fe->bbox_l < fe->bbox_r && fe->bbox_u < fe->bbox_d) {
- evas_object_image_data_update_add(fe->evas_image, 0, 0, w, h);
- evas_object_image_pixels_dirty_set(fe->evas_image, EINA_TRUE);
- // }
- }
- void slp_status_bar(void *handle, char *text)
- {
- frontend *fe = (frontend *)handle;
- }
- blitter *slp_blitter_new(void *handle, int w, int h)
- {
- blitter *bl = snew(blitter);
- bl->image = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, w, h);
- bl->w = w;
- bl->h = h;
- return bl;
- }
- void slp_blitter_free(void *handle, blitter *bl)
- {
- cairo_surface_destroy(bl->image);
- sfree(bl);
- }
- void slp_blitter_save(void *handle, blitter *bl, int x, int y)
- {
- frontend *fe = (frontend *)handle;
- cairo_t *cr = cairo_create(bl->image);
- cairo_set_source_surface(cr, fe->image, -x, -y);
- cairo_paint(cr);
- cairo_destroy(cr);
- bl->x = x;
- bl->y = y;
- }
- void slp_blitter_load(void *handle, blitter *bl, int x, int y)
- {
- frontend *fe = (frontend *)handle;
- if (x == BLITTER_FROMSAVED && y == BLITTER_FROMSAVED) {
- x = bl->x;
- y = bl->y;
- }
- cairo_set_source_surface(fe->cr, bl->image, x, y);
- cairo_paint(fe->cr);
- }
- void slp_draw_thick_line(void *handle, float thickness, float x1, float y1, float x2, float y2, int colour)
- {
- frontend *fe = (frontend *)handle;
- set_colour(fe, colour);
- cairo_save(fe->cr);
- cairo_set_line_width(fe->cr, thickness);
- cairo_new_path(fe->cr);
- cairo_move_to(fe->cr, x1, y1);
- cairo_line_to(fe->cr, x2, y2);
- cairo_stroke(fe->cr);
- cairo_restore(fe->cr);
- }
- const struct drawing_api slp_drawing = {
- slp_draw_text,
- slp_draw_rect,
- slp_draw_line,
- slp_draw_polygon,
- slp_draw_circle,
- slp_draw_update,
- slp_clip,
- slp_unclip,
- slp_start_draw,
- slp_end_draw,
- slp_status_bar,
- slp_blitter_new,
- slp_blitter_free,
- slp_blitter_save,
- slp_blitter_load,
- NULL, NULL, NULL, NULL, NULL, NULL,
- NULL, NULL,
- NULL,
- slp_draw_thick_line
- };
- void deactivate_timer(frontend *fe)
- {
- }
- void activate_timer(frontend *fe)
- {
- }
- int main(int argc, char **argv)
- {
- frontend fe;
- ecore_init();
- ecore_main_loop_glib_integrate();
- ecore_evas_init();
- fe.me = midend_new(&fe, &thegame, &slp_drawing, &fe);
- Ecore_Evas* ee = ecore_evas_software_x11_new(NULL, 0, 0, 0, 480, 640);
- // ecore_evas_callback_resize_set(ee, efl_resize_window);
- ecore_evas_title_set(ee, PACKAGE);
- ecore_evas_borderless_set(ee, 0);
- ecore_evas_shaped_set(ee, 0);
- fe.w = 480;
- fe.h = 640;
- ecore_evas_show(ee);
- Evas* evas = ecore_evas_get(ee);
- fe.evas = evas;
- Evas_Object *b = evas_object_rectangle_add(evas);
- evas_object_color_set(b, 255, 255, 255, 255);
- evas_object_move(b, 0, 0);
- evas_object_resize(b, fe.w, fe.h);
- evas_object_show(b);
- fe.evas = evas_object_evas_get(b);
- fe.image = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, fe.w, fe.h);
- fe.evas_image = evas_object_image_add(evas);
- // evas_object_move(fe.evas_image, 0, 0);
- // evas_object_resize(fe.evas_image, 480, 640);
- fe.colours = midend_colours(fe.me, &fe.ncolours);
- evas_object_show(fe.evas_image);
- // just start new game ;)
- midend_new_game(fe.me);
- midend_force_redraw(fe.me);
- ecore_main_loop_begin();
- midend_free(fe.me);
- evas_shutdown();
- ecore_shutdown();
- return 0;
- }
Add Comment
Please, Sign In to add comment