d_define_method(fonts, add_font)(struct s_object *self, unsigned int id, struct s_object *stream, int size) { d_using(fonts); struct s_stream_attributes *stream_attributes = d_cast(stream, stream); char buffer[d_string_buffer_size]; SDL_RWops *font_block; if (fonts_attributes->fonts[id].font) { TTF_CloseFont(fonts_attributes->fonts[id].font); munmap(fonts_attributes->fonts[id].font_block, fonts_attributes->fonts[id].file_stats.st_size); } fstat(stream_attributes->descriptor, &(fonts_attributes->fonts[id].file_stats)); if ((fonts_attributes->fonts[id].font_block = mmap(NULL, fonts_attributes->fonts[id].file_stats.st_size, PROT_READ, MAP_SHARED, stream_attributes->descriptor, 0)) != MAP_FAILED) { font_block = SDL_RWFromMem(fonts_attributes->fonts[id].font_block, fonts_attributes->fonts[id].file_stats.st_size); if (!(fonts_attributes->fonts[id].font = TTF_OpenFontRW(font_block, d_true, size))) { munmap(fonts_attributes->fonts[id].font_block, fonts_attributes->fonts[id].file_stats.st_size); snprintf(buffer, d_string_buffer_size, "ungenerable TTF for font %s exception", d_string_cstring(stream_attributes->string_name)); d_throw(v_exception_ttf, buffer); } } else { snprintf(buffer, d_string_buffer_size, "wrong type for file %s exception", d_string_cstring(stream_attributes->string_name)); d_throw(v_exception_wrong_type, buffer); } return self; }
struct s_object *f_stream_new_file(struct s_object *self, struct s_object *string_name, const char *action, int permission) { struct s_stream_attributes *attributes = p_stream_alloc(self); char buffer[d_string_buffer_size]; attributes->string_name = d_retain(string_name); attributes->parameters = -1; switch(action[0]) { case 'r': case 'R': attributes->parameters = d_stream_flag_read; if ((action[1] == 'w') || (action[1] == 'W')) attributes->parameters = d_stream_flag_write_read; break; case 'w': case 'W': attributes->parameters = d_stream_flag_truncate; if ((action[1] == 'a') || (action[1] == 'A')) attributes->parameters = d_stream_flag_append; } if (attributes->parameters != -1) { if ((attributes->descriptor = open(d_string_cstring(attributes->string_name), attributes->parameters, permission)) > -1) attributes->flags = e_stream_flag_opened; else { snprintf(buffer, d_string_buffer_size, "unreachable file %s exception", d_string_cstring(attributes->string_name)); d_throw(v_exception_unreachable, buffer); } } else d_throw(v_exception_malformed, "malformed action format"); return self; }
struct s_object *f_track_new_channel(struct s_object *self, struct s_object *stream, int channel) { struct s_track_attributes *attributes = p_track_alloc(self); struct s_stream_attributes *stream_attributes = d_cast(stream, stream); char *memblock, buffer[d_string_buffer_size]; struct stat file_stats; SDL_RWops *tracked_block; if (attributes->chunk) Mix_FreeChunk(attributes->chunk); fstat(stream_attributes->descriptor, &file_stats); if ((memblock = mmap(NULL, file_stats.st_size, PROT_READ, MAP_SHARED, stream_attributes->descriptor, 0)) != MAP_FAILED) { tracked_block = SDL_RWFromMem(memblock, file_stats.st_size); if ((attributes->chunk = Mix_LoadWAV_RW(tracked_block, d_true))) { if ((attributes->channel = channel) == d_track_auto_channel) attributes->auto_channel = d_true; attributes->next_channel = channel; attributes->volume = d_track_default_volume; } else { snprintf(buffer, d_string_buffer_size, "unable to retrieve informations for track %s exception", d_string_cstring(stream_attributes->string_name)); d_throw(v_exception_chunk, buffer); } munmap(memblock, file_stats.st_size); } else { snprintf(buffer, d_string_buffer_size, "wrong type for file %s exception", d_string_cstring(stream_attributes->string_name)); d_throw(v_exception_wrong_type, buffer); } return self; }
d_define_method(label, set_content)(struct s_object *self, struct s_object *string_content, TTF_Font *font, struct s_object *environment) { d_using(label); struct s_drawable_attributes *drawable_attributes = d_cast(self, drawable); struct s_environment_attributes *environment_attributes = d_cast(environment, environment); char buffer[d_string_buffer_size]; int width, height; SDL_Surface *unoptimized_surface; SDL_Color white = { 255, 255, 255, 255 }; if (label_attributes->string_content) d_delete(label_attributes->string_content); label_attributes->string_content = d_retain(string_content); if (label_attributes->image) SDL_DestroyTexture(label_attributes->image); if ((unoptimized_surface = TTF_RenderText_Blended(font, d_string_cstring(string_content), white))) { label_attributes->image = SDL_CreateTextureFromSurface(environment_attributes->renderer, unoptimized_surface); if (SDL_QueryTexture(label_attributes->image, NULL, NULL, &width, &height) == 0) { d_call(&(drawable_attributes->point_dimension), m_point_set_x, (double)width); d_call(&(drawable_attributes->point_dimension), m_point_set_y, (double)height); d_call(&(drawable_attributes->point_center), m_point_set_x, (double)(width/2.0)); d_call(&(drawable_attributes->point_center), m_point_set_y, (double)(height/2.0)); if (label_attributes->last_blend != e_drawable_blend_undefined) d_call(self, m_drawable_set_blend, label_attributes->last_blend); d_call(self, m_drawable_set_maskRGB, (unsigned int)label_attributes->last_mask_R, (unsigned int)label_attributes->last_mask_G, (unsigned int)label_attributes->last_mask_B); d_call(self, m_drawable_set_maskA, (unsigned int)label_attributes->last_mask_A); } else { snprintf(buffer, d_string_buffer_size, "unable to retrieve informations for label \"%s\" exception", d_string_cstring(string_content)); d_throw(v_exception_texture, buffer); } SDL_FreeSurface(unoptimized_surface); } else { snprintf(buffer, d_string_buffer_size, "ungenerable texture for label \"%s\" exception", d_string_cstring(string_content)); d_throw(v_exception_texture, buffer); } return self; }
struct s_object *f_bitmap_new(struct s_object *self, struct s_object *stream, struct s_object *environment) { struct s_bitmap_attributes *attributes = p_bitmap_alloc(self); struct s_drawable_attributes *drawable_attributes = d_cast(self, drawable); struct s_stream_attributes *stream_attributes = d_cast(stream, stream); struct s_environment_attributes *environment_attributes = d_cast(environment, environment); char *memblock, buffer[d_string_buffer_size]; struct stat file_stats; int width, height; SDL_RWops *surfaced_block; SDL_Surface *unoptimized_surface; fstat(stream_attributes->descriptor, &file_stats); if ((memblock = mmap(NULL, file_stats.st_size, PROT_READ, MAP_SHARED, stream_attributes->descriptor, 0)) != MAP_FAILED) { surfaced_block = SDL_RWFromMem(memblock, file_stats.st_size); if ((unoptimized_surface = IMG_Load_RW(surfaced_block, d_true))) { attributes->image = SDL_CreateTextureFromSurface(environment_attributes->renderer, unoptimized_surface); if (SDL_QueryTexture(attributes->image, NULL, NULL, &width, &height) == 0) { d_call(&(drawable_attributes->point_dimension), m_point_set_x, (double)width); d_call(&(drawable_attributes->point_dimension), m_point_set_y, (double)height); d_call(&(drawable_attributes->point_center), m_point_set_x, (double)(width/2.0)); d_call(&(drawable_attributes->point_center), m_point_set_y, (double)(height/2.0)); } else { snprintf(buffer, d_string_buffer_size, "unable to retrieve informations for bitmap %s exception", d_string_cstring(stream_attributes->string_name)); d_throw(v_exception_texture, buffer); } } else { snprintf(buffer, d_string_buffer_size, "ungenerable texture for bitmap %s exception", d_string_cstring(stream_attributes->string_name)); d_throw(v_exception_texture, buffer); } SDL_FreeSurface(unoptimized_surface); munmap(memblock, file_stats.st_size); } else { snprintf(buffer, d_string_buffer_size, "wrong type for file %s exception", d_string_cstring(stream_attributes->string_name)); d_throw(v_exception_wrong_type, buffer); } return self; }
d_define_method(stream, write_string)(struct s_object *self, struct s_object *string, size_t *written) { size_t dimension; d_call(string, m_string_length, &dimension); return d_call(self, m_stream_write, (unsigned char *)d_string_cstring(string), dimension, written); }