Exemple #1
0
// Dessine l'image d'un widget
void ei_draw_widget_img(	ei_surface_t	surface,
							ei_widget_t*	widget,
							ei_surface_t	img,
							ei_rect_t*		img_rect,
							ei_anchor_t		anchor,
							int				button_border_width	)
{	
    // Paramètres du widget
    ei_point_t wtopleft	= ei_point_add( widget->screen_location.top_left,
            ei_point(2*button_border_width, 2*button_border_width) );
    ei_size_t wsize = ei_size_sub( widget->screen_location.size,
            ei_size(4*button_border_width, 4*button_border_width) );

    // Variables à calculer
    ei_rect_t dst_widget_rect;
    ei_rect_t src_image_rect;

    /***********************************************/
    /* ETAPE 1 : CALCUL DE LA TAILLE DE DST ET SRC */
    /***********************************************/	

    ei_size_t img_size;
    if (img_rect != NULL) {
        img_size = img_rect->size;
    } else {
        img_size = hw_surface_get_size(img);
    }

    // Calcul de la largeur de la zone de copie
    if (img_size.width > wsize.width) {
        src_image_rect.size.width = wsize.width;
        dst_widget_rect.size.width = wsize.width;
    } else {
        src_image_rect.size.width = img_size.width;
        dst_widget_rect.size.width = img_size.width;
    }
    // Calcul de la hauteur de la zone de copie
    if (img_size.height > wsize.height) {
        src_image_rect.size.height = wsize.height;
        dst_widget_rect.size.height = wsize.height;
    } else {
        src_image_rect.size.height = img_size.height;
        dst_widget_rect.size.height = img_size.height;
    }

    /*************************************/
    /* ETAPE 2 : CALCUL DES DEUX TOPLEFT */
    /*************************************/	

    // Calcul du topleft de la destination
    ei_switch_anchor(anchor, &(dst_widget_rect.top_left),
            dst_widget_rect.size, wtopleft, wsize);

    // Calcul du topleft de la source
    if (img_rect != NULL) {
        src_image_rect.top_left = img_rect->top_left;
    //    fprintf(stderr, "x %i\n", img_rect->top_left.x);
    //    fprintf(stderr, "y %i\n", img_rect->top_left.y);
    } else {
        // Par défaut je prend le coin en haut à gauche de l'image
        ei_rect_t rect_total = hw_surface_get_rect(img);
        src_image_rect.top_left = rect_total.top_left;
    }

    /*************************************************************/
    /* ETAPE 2bis : RECALCUL SI SORTIE DU CADRE PRINCIPAL (ROOT) */
    /*************************************************************/	

    ei_surface_t display_surface = ei_app_root_surface();
    ei_rect_t display_rect = hw_surface_get_rect(display_surface);

    int cut;

    // Cas 1 : sortie à gauche
    cut = display_rect.top_left.x - dst_widget_rect.top_left.x;
    if	( cut > 0 ) {
        // Changement de DST
        dst_widget_rect.top_left.x = display_rect.top_left.x;
        dst_widget_rect.size.width = dst_widget_rect.size.width - cut;
        // Changement de SRC
        src_image_rect.top_left.x = src_image_rect.top_left.x + cut;
        src_image_rect.size = dst_widget_rect.size;
    }

    // Cas 2 : sortie à droite
    cut = dst_widget_rect.top_left.x + dst_widget_rect.size.width
        - display_rect.top_left.x - display_rect.size.width;
    if	( cut > 0 ) {
        // Changement de DST
        dst_widget_rect.size.width = dst_widget_rect.size.width - cut;
        // Changement de SRC
        src_image_rect.size = dst_widget_rect.size;
    }


    // Cas 3 : sortie en haut
    cut = display_rect.top_left.y - dst_widget_rect.top_left.y;
    if	( cut > 0 ) {
        // Changement de DST
        dst_widget_rect.top_left.y = display_rect.top_left.y;
        dst_widget_rect.size.height = dst_widget_rect.size.height - cut;
        // Changement de SRC
        src_image_rect.top_left.y = src_image_rect.top_left.y + cut;
        src_image_rect.size = dst_widget_rect.size;
    }

    // Cas 4 : sortie en bas
    cut = dst_widget_rect.top_left.y + dst_widget_rect.size.height
        - display_rect.top_left.y - display_rect.size.height;
    if	( cut > 0 ) {
        // Changement de DST
        dst_widget_rect.size.height = dst_widget_rect.size.height - cut;
        // Changement de SRC
        src_image_rect.size = dst_widget_rect.size;
    }

    /*****************************************/
    /* ETAPE 3 : COPIE DE L'IMAGE A AFFICHEE */
    /*****************************************/	

    hw_surface_lock(img);
    ei_copy_surface(surface, &dst_widget_rect, img, &src_image_rect, EI_FALSE);		
    hw_surface_unlock(img);

}
Exemple #2
0
int ei_main(int argc, char** argv)
{
	ei_surface_t			main_window		= NULL;
	ei_size_t			main_window_size;
	ei_event_t			event;
	int				i;
	ei_linked_point_t		points[4];
	int				coords[]		= { 20, 20, 620, 20, 20, 460, 620, 460 };
	ei_color_t			red			= { 0xff, 0x00, 0x00, 0xff };
	ei_color_t			transp_blue		= { 0x00, 0x00, 0xff, 0x88 };

	// Init acces to hardware.
	hw_init();

	// Create the main window.
	main_window_size.width	= 640;
	main_window_size.height	= 480;
	main_window = hw_create_window(&main_window_size, EI_FALSE);



	// Fill the main window in red, draw a transparent blue polygon:

	// Define the polygon vertices.
	for (i = 0; i < 4; i++) {
		points[i].point.x	= coords[i*2];
		points[i].point.y	= coords[i*2 + 1];
		if (i < 3)
			points[i].next	= &(points[i+1]);
		else
			points[i].next	= NULL;
	}

	// Lock the surface for drawing, fill, draw polygon, unlock, update screen.
	hw_surface_lock(main_window);
	ei_fill(main_window, &red, NULL);
	ei_draw_polygon(main_window, points, transp_blue, NULL);
	hw_surface_unlock(main_window);
	hw_surface_update_rects(main_window, NULL);
	
	#ifdef __APPLE__
		// On osx, the window opens empty and does not perform the first redraw.
		//	Force a redraw after 0.5s
		hw_event_schedule_app(500, (void*)NULL);
	#endif

	// Wait for a key press.
	event.type = ei_ev_none;
	while (event.type != ei_ev_keydown) {
		#ifdef __APPLE__
			if (event.type == ei_ev_app)
				hw_surface_update_rects(main_window, NULL);
		#endif
		hw_event_wait_next(&event);
	}

	// Free hardware resources.
	hw_quit();

	// Terminate program with no error code.
	return 0;
}