d_define_method_override(container, event)(struct s_object *self, struct s_object *environment, SDL_Event *current_event) { d_using(container); struct s_eventable_attributes *eventable_attributes; struct s_uiable_attributes *uiable_attributes; struct s_container_drawable *current_container; struct s_object *result = d_call_owner(self, uiable, m_eventable_event, environment, current_event); struct s_exception *exception; d_try { d_foreach(&(container_attributes->entries), current_container, struct s_container_drawable) { if (((uiable_attributes = d_cast(current_container->drawable, uiable))) && ((eventable_attributes = d_cast(current_container->drawable, eventable)))) { d_call_owner(current_container->drawable, uiable, m_eventable_event, environment, current_event); if ((uiable_attributes->is_selected) && (eventable_attributes->enable)) d_call(current_container->drawable, m_eventable_event, environment, current_event); } } } d_catch(exception) { d_exception_dump(stderr, exception); d_raise; } d_endtry; return result; }
d_define_method_override(container, draw)(struct s_object *self, struct s_object *environment) { d_using(container); struct s_drawable_attributes *drawable_attributes_self = d_cast(self, drawable), *drawable_attributes_entry; struct s_uiable_attributes *uiable_attributes_self = d_cast(self, uiable), *uiable_attributes_entry; struct s_square_attributes *square_attributes; struct s_environment_attributes *environment_attributes = d_cast(environment, environment); struct s_camera_attributes *camera_attributes = d_cast(environment_attributes->current_camera, camera); struct s_container_drawable *current_container; double position_x_self, position_y_self, normalized_position_x_self, normalized_position_y_self, position_x_entry, position_y_entry, center_x_self, center_y_self, center_x_entry, center_y_entry, max_w = container_attributes->border_left + container_attributes->border_right, max_h = container_attributes->border_top + container_attributes->border_bottom, current_w, current_h; int result = d_drawable_return_last; d_call(&(drawable_attributes_self->point_destination), m_point_get, &position_x_self, &position_y_self); d_call(&(drawable_attributes_self->point_normalized_destination), m_point_get, &normalized_position_x_self, &normalized_position_y_self); d_call(&(drawable_attributes_self->point_center), m_point_get, ¢er_x_self, ¢er_y_self); d_foreach(&(container_attributes->entries), current_container, struct s_container_drawable) { drawable_attributes_entry = d_cast(current_container->drawable, drawable); position_x_entry = position_x_self + current_container->position_x + container_attributes->border_left; position_y_entry = position_y_self + current_container->position_y + container_attributes->border_top; center_x_entry = (position_x_self + center_x_self) - position_x_entry; center_y_entry = (position_y_self + center_y_self) - position_y_entry; d_call(current_container->drawable, m_drawable_set_position, position_x_entry, position_y_entry); if (((drawable_attributes_entry->flags & e_drawable_kind_ui_no_attribute_angle) != e_drawable_kind_ui_no_attribute_angle) && ((drawable_attributes_entry->flags & e_drawable_kind_ui_no_attribute_zoom) != e_drawable_kind_ui_no_attribute_zoom)) d_call(current_container->drawable, m_drawable_set_center, center_x_entry, center_y_entry); if ((drawable_attributes_entry->flags & e_drawable_kind_ui_no_attribute_angle) != e_drawable_kind_ui_no_attribute_angle) drawable_attributes_entry->angle = drawable_attributes_self->angle; if ((drawable_attributes_entry->flags & e_drawable_kind_ui_no_attribute_zoom) != e_drawable_kind_ui_no_attribute_zoom) drawable_attributes_entry->zoom = container_attributes->distributed_zoom; if ((drawable_attributes_entry->flags & e_drawable_kind_ui_no_attribute_flip) != e_drawable_kind_ui_no_attribute_flip) drawable_attributes_entry->flip = drawable_attributes_self->flip; if ((d_call(current_container->drawable, m_drawable_normalize_scale, camera_attributes->scene_reference_w, camera_attributes->scene_reference_h, camera_attributes->scene_offset_x, camera_attributes->scene_offset_y, camera_attributes->scene_center_x, camera_attributes->scene_center_y, camera_attributes->screen_w, camera_attributes->screen_h, camera_attributes->scene_zoom))) { square_attributes = d_cast(&(drawable_attributes_entry->square_collision_box), square); current_w = d_math_max(d_math_max(square_attributes->normalized_top_left_x, square_attributes->normalized_top_right_x), d_math_max(square_attributes->normalized_bottom_left_x, square_attributes->normalized_bottom_right_x)) - normalized_position_x_self; current_h = d_math_max(d_math_max(square_attributes->normalized_top_left_y, square_attributes->normalized_top_right_y), d_math_max(square_attributes->normalized_bottom_left_y, square_attributes->normalized_bottom_right_y)) - normalized_position_y_self; if ((uiable_attributes_entry = d_cast(current_container->drawable, uiable))) { /* we need to take in consideration the border of the object that is not considered in the collision square */ current_w += uiable_attributes_entry->border_w; current_h += uiable_attributes_entry->border_h; } /* normalization for ratio and environmental zoom */ current_w = ((current_w * camera_attributes->scene_reference_w) / camera_attributes->screen_w) / camera_attributes->scene_zoom; current_h = ((current_h * camera_attributes->scene_reference_h) / camera_attributes->screen_h) / camera_attributes->scene_zoom; max_w = d_math_max(max_w, current_w); max_h = d_math_max(max_h, current_h); } } d_call(self, m_drawable_set_dimension, (max_w + uiable_attributes_self->border_w), (max_h + uiable_attributes_self->border_h)); if ((d_call(self, m_drawable_normalize_scale, camera_attributes->scene_reference_w, camera_attributes->scene_reference_h, camera_attributes->scene_offset_x, camera_attributes->scene_offset_y, camera_attributes->scene_center_x, camera_attributes->scene_center_y, camera_attributes->screen_w, camera_attributes->screen_h, camera_attributes->scene_zoom))) { result = (intptr_t)d_call_owner(self, uiable, m_drawable_draw, environment); /* recall the father's draw method */ d_foreach(&(container_attributes->entries), current_container, struct s_container_drawable) while (((intptr_t)d_call(current_container->drawable, m_drawable_draw, environment)) == d_drawable_return_continue); }
d_define_method_override(label, set_dimension_h)(struct s_object *self, double h) { d_using(label); struct s_drawable_attributes *drawable_attributes = d_cast(self, drawable); struct s_object *result = d_call_owner(self, drawable, m_drawable_set_dimension_h, h); label_attributes->last_height = h; label_attributes->format = e_label_background_format_fixed; d_call(&(drawable_attributes->point_center), m_point_set_y, (double)(label_attributes->last_height/2.0)); return result; }
d_define_method_override(checkbox, draw)(struct s_object *self, struct s_object *environment) { d_using(checkbox); struct s_uiable_attributes *uiable_attributes = d_cast(self, uiable); struct s_environment_attributes *environment_attributes = d_cast(environment, environment); struct s_drawable_attributes *drawable_attributes_self = d_cast(self, drawable), *drawable_attributes_selected; struct s_object *selected_component = NULL; double position_x, position_y, new_position_x, new_position_y, center_x, center_y, new_center_x, new_center_y, dimension_w_self, dimension_h_self, dimension_w_selected, dimension_h_selected, new_dimension_w, new_dimension_h; int result = (intptr_t)d_call_owner(self, label, m_drawable_draw, environment); /* recall the father's draw method */ d_call(&(drawable_attributes_self->point_destination), m_point_get, &position_x, &position_y); d_call(&(drawable_attributes_self->point_dimension), m_point_get, &dimension_w_self, &dimension_h_self); d_call(&(drawable_attributes_self->point_center), m_point_get, ¢er_x, ¢er_y); if ((checkbox_attributes->is_checked) && (checkbox_attributes->checked)) selected_component = checkbox_attributes->checked; else if ((!checkbox_attributes->is_checked) && (checkbox_attributes->unchecked)) selected_component = checkbox_attributes->unchecked; if (selected_component) { drawable_attributes_selected = d_cast(selected_component, drawable); d_call(&(drawable_attributes_selected->point_dimension), m_point_get, &dimension_w_selected, &dimension_h_selected); new_dimension_h = dimension_h_self - (uiable_attributes->border_h * 2.0); new_dimension_w = (dimension_w_selected * new_dimension_h)/dimension_h_selected; new_position_x = position_x + dimension_w_self - new_dimension_w - uiable_attributes->border_w; new_position_y = position_y + ((dimension_h_self - new_dimension_h) / 2.0); new_center_x = (position_x + center_x) - new_position_x; new_center_y = (position_y + center_y) - new_position_y; d_call(selected_component, m_drawable_set_position, new_position_x, new_position_y); d_call(selected_component, m_drawable_set_center, new_center_x, new_center_y); d_call(selected_component, m_drawable_set_dimension, new_dimension_w, new_dimension_h); drawable_attributes_selected->angle = drawable_attributes_self->angle; drawable_attributes_selected->zoom = drawable_attributes_self->zoom; drawable_attributes_selected->flip = drawable_attributes_self->flip; if ((d_call(selected_component, m_drawable_normalize_scale, environment_attributes->reference_w[environment_attributes->current_surface], environment_attributes->reference_h[environment_attributes->current_surface], environment_attributes->camera_origin_x[environment_attributes->current_surface], environment_attributes->camera_origin_y[environment_attributes->current_surface], environment_attributes->camera_focus_x[environment_attributes->current_surface], environment_attributes->camera_focus_y[environment_attributes->current_surface], environment_attributes->current_w, environment_attributes->current_h, environment_attributes->zoom[environment_attributes->current_surface]))) while(((int)d_call(selected_component, m_drawable_draw, environment)) == d_drawable_return_continue); } d_cast_return(result); }
d_define_method_override(label, draw)(struct s_object *self, struct s_object *environment) { d_using(label); double position_x, position_y, dimension_w, dimension_h, center_x, center_y; struct s_drawable_attributes *drawable_attributes = d_cast(self, drawable); struct s_environment_attributes *environment_attributes = d_cast(environment, environment); SDL_Rect destination; SDL_Point center; d_call_owner(self, uiable, m_drawable_draw, environment); /* recall the father's draw method */ d_call(&(drawable_attributes->point_normalized_destination), m_point_get, &position_x, &position_y); d_call(&(drawable_attributes->point_normalized_dimension), m_point_get, &dimension_w, &dimension_h); d_call(&(drawable_attributes->point_normalized_center), m_point_get, ¢er_x, ¢er_y); destination.x = position_x; destination.y = position_y; destination.w = dimension_w; destination.h = dimension_h; center.x = center_x; center.y = center_y; SDL_RenderCopyEx(environment_attributes->renderer, label_attributes->image, NULL, &destination, drawable_attributes->angle, ¢er, drawable_attributes->flip); d_cast_return(d_drawable_return_last); }
d_define_method_override(label, draw)(struct s_object *self, struct s_object *environment) { d_using(label); struct s_drawable_attributes *drawable_attributes = d_cast(self, drawable); struct s_uiable_attributes *uiable_attributes = d_cast(self, uiable); struct s_environment_attributes *environment_attributes = d_cast(environment, environment); double position_x, position_y, dimension_w, dimension_h, center_x, center_y, width_factor, height_factor; int result = (intptr_t)d_call_owner(self, uiable, m_drawable_draw, environment); /* recall the father's draw method */ SDL_Rect source, destination; SDL_Point center; if (label_attributes->image) { d_call(&(drawable_attributes->point_normalized_destination), m_point_get, &position_x, &position_y); d_call(&(drawable_attributes->point_normalized_dimension), m_point_get, &dimension_w, &dimension_h); d_call(&(drawable_attributes->point_normalized_center), m_point_get, ¢er_x, ¢er_y); width_factor = (dimension_w/label_attributes->last_width); height_factor = (dimension_h/label_attributes->last_height); source.x = 0; source.y = 0; destination.x = (position_x + uiable_attributes->border_w); destination.y = (position_y + uiable_attributes->border_h); if (label_attributes->format == e_label_background_format_fixed) { source.w = d_math_min((label_attributes->last_width - (uiable_attributes->border_w * 2.0)), label_attributes->string_width); source.h = d_math_min((label_attributes->last_height - (uiable_attributes->border_h * 2.0)), label_attributes->string_height); switch (label_attributes->alignment_x) { case e_label_alignment_center: if ((source.x = d_math_max(((label_attributes->string_width-label_attributes->last_width)/2.0), 0)) == 0) destination.x = position_x + (((label_attributes->last_width - label_attributes->string_width)/2.0) * width_factor); break; case e_label_alignment_right: if ((source.x = d_math_max((label_attributes->string_width-label_attributes->last_width), 0)) == 0) destination.x = position_x + ((label_attributes->last_width - label_attributes->string_width) * width_factor); default: break; } switch (label_attributes->alignment_y) { case e_label_alignment_center: if ((source.y = d_math_max(((label_attributes->string_height-label_attributes->last_height)/2.0), 0)) == 0) destination.y = position_y + (((label_attributes->last_height - label_attributes->string_height)/2.0) * height_factor); break; case e_label_alignment_bottom: if ((source.y = d_math_max((label_attributes->string_height-label_attributes->last_height), 0)) == 0) destination.y = position_y + ((label_attributes->last_height - label_attributes->string_height) * height_factor); default: break; } } else if (label_attributes->format == e_label_background_format_adaptable) { source.w = label_attributes->string_width; source.h = label_attributes->string_height; } destination.w = source.w * width_factor; destination.h = source.h * height_factor; center.x = (position_x + center_x) - destination.x; center.y = (position_y + center_y) - destination.y; label_attributes->last_source = source; label_attributes->last_destination = destination; SDL_RenderCopyEx(environment_attributes->renderer, label_attributes->image, &source, &destination, drawable_attributes->angle, ¢er, (SDL_RendererFlip)drawable_attributes->flip); } d_cast_return(result); }
d_define_method_override(illuminable_bitmap, draw)(struct s_object *self, struct s_object *environment) { d_using(illuminable_bitmap); struct s_environment_attributes *environment_attributes = d_cast(environment, environment); struct s_drawable_attributes *drawable_attributes = d_cast(self, drawable); struct s_lights_emitter_description *lights_emitter; struct s_list affected_lights; struct s_object *result = d_call_owner(self, bitmap, m_drawable_draw, environment); /* recall the father's draw method */ double image_x, image_y, image_center_x, image_center_y, image_principal_point_x, image_principal_point_y, new_x, new_y, radians_rotation = -(drawable_attributes->angle * d_math_radians_conversion), radians_incident, cosine = cos(radians_rotation), sine = sin(radians_rotation), light_normalized_percentage[e_illuminable_bitmap_side_NULL], light_final_mask[e_illuminable_bitmap_side_NULL], local_factor, center_factor, center_factor_reduction; unsigned int index_side; if (illuminable_bitmap_attributes->lights) { memset(&(affected_lights), 0, sizeof(s_list)); for (index_side = 0; index_side < e_illuminable_bitmap_side_NULL; ++index_side) light_final_mask[index_side] = 0; d_call(self, m_drawable_get_scaled_position, &image_x, &image_y); d_call(self, m_drawable_get_scaled_center, &image_center_x, &image_center_y); d_call(self, m_drawable_get_scaled_principal_point, &image_principal_point_x, &image_principal_point_y); d_call(illuminable_bitmap_attributes->lights, m_lights_get_affecting_lights, self, &(affected_lights), environment); d_foreach(&(affected_lights), lights_emitter, struct s_lights_emitter_description) { for (index_side = 0; index_side < e_illuminable_bitmap_side_NULL; ++index_side) light_normalized_percentage[index_side] = 0; /* we need to re-calculate the coordinates of the light in respect of the angle generated by this image */ new_x = lights_emitter->position_x - (image_x + image_center_x); new_y = lights_emitter->position_y - (image_y + image_center_y); lights_emitter->position_x = (new_x * cosine) - (new_y * sine) + (image_x + image_center_x); lights_emitter->position_y = (new_x * sine) + (new_y * cosine) + (image_y + image_center_y); radians_incident = fmod(atan2(lights_emitter->position_y - image_principal_point_y, lights_emitter->position_x - image_principal_point_x), d_math_two_pi); /* we need to convert it to a positive angle */ if (radians_incident < 0) radians_incident = d_math_two_pi + radians_incident; center_factor_reduction = 0.0; switch (illuminable_bitmap_attributes->main_axis) { case e_illuminable_bitmap_axis_x: center_factor = 1.0 - (fabs(lights_emitter->position_x - image_principal_point_x) / ((image_principal_point_x + lights_emitter->radius) - image_x)); if ((intptr_t)d_call(&(drawable_attributes->square_collision_box), m_square_inside_coordinates, lights_emitter->position_x, lights_emitter->position_y)) center_factor_reduction = 1.0 - (fabs(lights_emitter->position_x - image_principal_point_x) / (image_principal_point_x - image_x)); break; case e_illuminable_bitmap_axis_y: center_factor = 1.0 - (fabs(lights_emitter->position_y - image_principal_point_y) / ((image_principal_point_y + lights_emitter->radius) - image_y)); if ((intptr_t)d_call(&(drawable_attributes->square_collision_box), m_square_inside_coordinates, lights_emitter->position_x, lights_emitter->position_y)) center_factor_reduction = 1.0 - (fabs(lights_emitter->position_y - image_principal_point_y) / (image_principal_point_y - image_y)); break; } light_normalized_percentage[e_illuminable_bitmap_side_front] = center_factor; if ((radians_incident >= 0) && (radians_incident < d_math_half_pi)) { light_normalized_percentage[e_illuminable_bitmap_side_bottom] = (radians_incident / d_math_half_pi) * (1.0 - center_factor_reduction); light_normalized_percentage[e_illuminable_bitmap_side_right] = (1.0 - (radians_incident / d_math_half_pi)) * (1.0 - center_factor_reduction); } else if ((radians_incident >= d_math_half_pi) && (radians_incident < d_math_pi)) { light_normalized_percentage[e_illuminable_bitmap_side_left] = ((radians_incident - d_math_half_pi) / d_math_half_pi) * (1.0 - center_factor_reduction); light_normalized_percentage[e_illuminable_bitmap_side_bottom] = (1.0 - ((radians_incident - d_math_half_pi) / d_math_half_pi)) * (1.0 - center_factor_reduction); } else if ((radians_incident >= d_math_pi) && (radians_incident < (d_math_pi + d_math_half_pi))) { light_normalized_percentage[e_illuminable_bitmap_side_top] = ((radians_incident - d_math_pi) / d_math_half_pi) * (1.0 - center_factor_reduction); light_normalized_percentage[e_illuminable_bitmap_side_left] = (1.0 - ((radians_incident - d_math_pi) / d_math_half_pi)) * (1.0 - center_factor_reduction); } else if ((radians_incident >= (d_math_pi + d_math_half_pi)) && (radians_incident < d_math_two_pi)) { light_normalized_percentage[e_illuminable_bitmap_side_right] = ((radians_incident - (d_math_pi + d_math_half_pi)) / d_math_half_pi) * (1.0 - center_factor_reduction); light_normalized_percentage[e_illuminable_bitmap_side_top] = (1.0 - ((radians_incident - (d_math_pi + d_math_half_pi)) / d_math_half_pi)) * (1.0 - center_factor_reduction); } for (index_side = 0; index_side < e_illuminable_bitmap_side_NULL; ++index_side) if ((local_factor = (light_normalized_percentage[index_side] * (((lights_emitter->radius - lights_emitter->distance) / lights_emitter->radius) * 255.0))) > 0) { /* now we have a factor that is proportional with the angle, with the distance and with the radius of the light. What we should do is to normalized * that value using the intensity and the penetration of the light into the */ if ((light_final_mask[index_side] += local_factor) > 255.0) light_final_mask[index_side] = 255.0; } } for (index_side = 0; index_side < e_illuminable_bitmap_side_NULL; ++index_side) { if ((light_final_mask[index_side] > 0) && (illuminable_bitmap_attributes->drawable_mask[index_side])) { /* we don't need to check the visibility because, if we are in this function, means that the visibility of the object has been already confirmed by the * called */ d_call(illuminable_bitmap_attributes->drawable_mask[index_side], m_drawable_set_maskRGB, (unsigned int)light_final_mask[index_side], (unsigned int)light_final_mask[index_side], (unsigned int)light_final_mask[index_side]); while (((intptr_t)d_call(illuminable_bitmap_attributes->drawable_mask[index_side], m_drawable_draw, environment)) == d_drawable_return_continue); } } /* clean the content */ while ((lights_emitter = (struct s_lights_emitter_description *)(affected_lights.head))) { f_list_delete(&(affected_lights), (struct s_list_node *)lights_emitter); d_free(lights_emitter); } }