/* function to squeeze a large bitmap to fit within the GPU's maximum texture size */ ALLEGRO_BITMAP * t3f_squeeze_bitmap(ALLEGRO_BITMAP * bp, int * ow, int * oh) { int start_w = al_get_bitmap_width(bp); int start_h = al_get_bitmap_height(bp); int width = al_get_display_option(t3f_display, ALLEGRO_MAX_BITMAP_SIZE); int height = width; printf("max size = %d\n", width); if(start_w < width) { width = start_w; } if(start_h < height) { height = start_h; } /* store original bitmap size if pointers passed */ if(ow) { *ow = start_w; } if(oh) { *oh = start_h; } /* return original bitmap if it already fits */ if(start_w <= width && start_h <= height) { printf("clone (%d, %d) (%d, %d)\n", start_w, start_h, width, height); return al_clone_bitmap(bp); } return t3f_resize_bitmap(bp, width, width); }
void RenderTarget::resize(int32_t w, int32_t h) { ALLEGRO_BITMAP *newBitmap = al_create_bitmap(w, h); // Check if we have changed Smooth2D in between creating this rendertarget // and calling this function. If so, we need to clone this bitmap, because // bitmap resize operation flags have changed. ALLEGRO_BITMAP *bitmapToDrawResized; int bmFlags = al_get_bitmap_flags(this->bitmap); int32_t origW = this->width(); int32_t origH = this->height(); bool smooth2d = CBEnchanted::instance()->isSmooth2D(); if ((!smooth2d && (bmFlags & ALLEGRO_MAG_LINEAR) == ALLEGRO_MAG_LINEAR) || (smooth2d && (bmFlags & ALLEGRO_MAG_LINEAR) != ALLEGRO_MAG_LINEAR)) { // We need to clone. bitmapToDrawResized = al_clone_bitmap(this->bitmap); al_destroy_bitmap(this->bitmap); } else { // Phew, no need to clone. bitmapToDrawResized = this->bitmap; } al_set_target_bitmap(newBitmap); al_clear_to_color(al_map_rgb(0, 0, 0)); int32_t a, b, c; al_get_blender(&a, &b, &c); al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ZERO); al_draw_scaled_bitmap(bitmapToDrawResized, 0, 0, origW, origH, 0, 0, w, h, 0); al_set_blender(a, b, c); al_destroy_bitmap(bitmapToDrawResized); this->bitmap = newBitmap; bindRenderTarget = this; }
ALLEGRO_BITMAP * clone_bitmap (ALLEGRO_BITMAP *bitmap) { ALLEGRO_BITMAP *new_bitmap = al_clone_bitmap (bitmap); if (! new_bitmap) error (-1, 0, "%s (%p): cannot clone bitmap", __func__, bitmap); return new_bitmap; }
static int allua_Bitmap_clone(lua_State * L) { ALLUA_bitmap bitmap = allua_check_bitmap(L, 1); ALLUA_bitmap clone = al_clone_bitmap(bitmap); if (clone) allua_pushBitmap(L, clone, true); else lua_pushnil(L); return 1; }
twoDImage::twoDImage(ALLEGRO_BITMAP *bitmap){ int w, h; this->bitmap = al_clone_bitmap(bitmap); w = al_get_bitmap_width(this->bitmap); h = al_get_bitmap_height(this->bitmap); this->x = 0; this->y = 0; this->width = w; this->height = h; }
sprite& sprite::operator=(const sprite& rhs) { if (this == &rhs) return *this; // handle self assignment //assignment operator position=rhs.position; bitmap = al_clone_bitmap(rhs.bitmap); filename = rhs.filename; width = rhs.width; height = rhs.height; return *this; }
/* The menu item owns the icon bitmap. It is converted to a memory bitmap * when set to make sure any system threads will be able to read the data. */ static void set_item_icon(ALLEGRO_MENU_ITEM *item, ALLEGRO_BITMAP *icon) { item->icon = icon; if (icon && al_get_bitmap_flags(item->icon) & ALLEGRO_VIDEO_BITMAP) { int old_flags = al_get_new_bitmap_flags(); al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); item->icon = al_clone_bitmap(icon); al_destroy_bitmap(icon); al_set_new_bitmap_flags(old_flags); } }
ALLEGRO_BITMAP * t3f_resize_bitmap(ALLEGRO_BITMAP * bp, int w, int h) { ALLEGRO_BITMAP * rbp = NULL; int start_w = al_get_bitmap_width(bp); int start_h = al_get_bitmap_height(bp); int x, y; float pixx, pixx_f, pixy, pixy_f; ALLEGRO_COLOR a, b, c, d, ab, cd, result; ALLEGRO_STATE old_state; /* don't resize if size is already correct */ if(w == start_w && h == start_h) { return al_clone_bitmap(bp); } /* scale with software filtering */ rbp = al_create_bitmap(w, h); if(!rbp) { printf("failed to create return bitmap\n"); return NULL; } al_lock_bitmap(rbp, ALLEGRO_LOCK_READWRITE, ALLEGRO_PIXEL_FORMAT_ANY); al_store_state(&old_state, ALLEGRO_STATE_TARGET_BITMAP); al_set_target_bitmap(rbp); for(y = 0; y < h; y++) { pixy = ((float)y / h) * ((float)start_h - 1); pixy_f = floor(pixy); for(x = 0; x < w; x++) { pixx = ((float)x / w) * ((float)start_w - 1); pixx_f = floor(pixx); a = al_get_pixel(bp, pixx_f, pixy_f); b = al_get_pixel(bp, pixx_f + 1, pixy_f); c = al_get_pixel(bp, pixx_f, pixy_f + 1); d = al_get_pixel(bp, pixx_f + 1, pixy_f + 1); ab = interpolate(a, b, pixx - pixx_f); cd = interpolate(c, d, pixx - pixx_f); result = interpolate(ab, cd, pixy - pixy_f); al_put_pixel(x, y, result); } } al_unlock_bitmap(rbp); al_restore_state(&old_state); return rbp; }
bool Imgui_ImplA5_CreateDeviceObjects() { ImGuiIO &io = ImGui::GetIO(); // Build texture unsigned char *pixels; int width, height; io.Fonts->GetTexDataAsRGBA32(&pixels, &width, &height); // Create texture int flags = al_get_new_bitmap_flags(); int fmt = al_get_new_bitmap_format(); al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP|ALLEGRO_MIN_LINEAR|ALLEGRO_MAG_LINEAR); al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE); ALLEGRO_BITMAP* img = al_create_bitmap(width, height); al_set_new_bitmap_flags(flags); al_set_new_bitmap_format(fmt); if (!img) return false; ALLEGRO_LOCKED_REGION *locked_img = al_lock_bitmap(img, al_get_bitmap_format(img), ALLEGRO_LOCK_WRITEONLY); if (!locked_img) { al_destroy_bitmap(img); return false; } memcpy(locked_img->data, pixels, sizeof(int)*width*height); al_unlock_bitmap(img); // Convert software texture to hardware texture. ALLEGRO_BITMAP* cloned_img = al_clone_bitmap(img); al_destroy_bitmap(img); if (!cloned_img) return false; // Store our identifier io.Fonts->TexID = (void*)cloned_img; g_Texture = cloned_img; // Cleanup (don't clear the input data if you want to append new fonts later) io.Fonts->ClearInputData(); io.Fonts->ClearTexData(); // Create an invisible mouse cursor // Because al_hide_mouse_cursor() seems to mess up with the actual inputs.. ALLEGRO_BITMAP* mouse_cursor = al_create_bitmap(8,8); g_MouseCursorInvisible = al_create_mouse_cursor(mouse_cursor, 0, 0); al_destroy_bitmap(mouse_cursor); return true; }
animation & animation::operator=(const animation &rightValue) { frames_.clear(); bottomFrames_.clear(); for(int i = 0; i < rightValue.frames_.size(); ++i) { ALLEGRO_BITMAP * tempBitmap = al_clone_bitmap(rightValue.frames_.at(i)); frames_.push_back(tempBitmap); } for(int i = 0; i < rightValue.bottomFrames_.size(); ++i) { ALLEGRO_BITMAP * tempBitmap = al_clone_bitmap(rightValue.bottomFrames_.at(i)); bottomFrames_.push_back(tempBitmap); } xOffset_ = rightValue.xOffset_; yOffset_ = rightValue.yOffset_; split_ = rightValue.split_; oneThrough_ = rightValue.oneThrough_; xBottomOffset_ = rightValue.xBottomOffset_; yBottomOffset_ = rightValue.yBottomOffset_; speed_ = rightValue.speed_; return *this; }
GraphicsBuffer& GraphicsBuffer::operator=( const GraphicsBuffer& rhs ) { if( &rhs == this )//self assignment { return *this; } //cleanup al_destroy_bitmap( mpBitmap ); //clone bitmap mpBitmap = al_clone_bitmap( rhs.mpBitmap ); return *this; }
/* Renders the next frame in a GIF animation to the given position. * You need to call this in order on the same destination for frames * [0..gif->frames_count - 1] to properly render all the frames in the GIF. * The current target bitmap should have the same height as the animation, * and blending should be set to fully copy RGBA. */ void algif_render_frame(ALGIF_ANIMATION *gif, int frame, int xpos, int ypos) { int x, y, w, h; ALGIF_FRAME *f = &gif->frames[frame]; ALGIF_PALETTE *pal; if (frame == 0) { al_draw_filled_rectangle(xpos, ypos, xpos + gif->width, ypos + gif->height, al_map_rgba_f(0, 0, 0, 0)); } else { ALGIF_FRAME *p = &gif->frames[frame - 1]; if (p->disposal_method == 2) { al_draw_filled_rectangle(xpos + p->xoff, ypos + p->yoff, xpos + p->xoff + p->bitmap_8_bit->w, ypos + p->yoff + p->bitmap_8_bit->h, al_map_rgba_f(0, 0, 0, 0)); } else if (p->disposal_method == 3 && gif->store) { al_draw_bitmap_region(gif->store, xpos + p->xoff, ypos + p->yoff, p->bitmap_8_bit->w, p->bitmap_8_bit->h, xpos + p->xoff, ypos + p->yoff, 0); al_destroy_bitmap(gif->store); gif->store = NULL; } } w = f->bitmap_8_bit->w; h = f->bitmap_8_bit->h; if (f->disposal_method == 3) { if (gif->store) al_destroy_bitmap(gif->store); gif->store = al_clone_bitmap(al_get_target_bitmap()); } pal = &gif->frames[frame].palette; if (pal->colors_count == 0) pal = &gif->palette; for (y = 0; y < h; y++) { for (x = 0; x < w; x++) { int c = f->bitmap_8_bit->data[x + y * f->bitmap_8_bit->w]; if (c != f->transparent_index) { al_draw_pixel(xpos + f->xoff + x, ypos + f->yoff + y, al_map_rgb(pal->colors[c].r, pal->colors[c].g, pal->colors[c].b)); } } } }
void Copy_DropBox_item(DropBox_Item *dbi_source, DropBox_Item *dbi_dest) //sourc, dest { if(dbi_source == nullptr) { if(dbi_dest != nullptr) { delete dbi_dest; } dbi_dest = nullptr; return; } if(dbi_dest != nullptr) { delete dbi_dest; } dbi_dest = new DropBox_Item; dbi_dest->bmp_str = dbi_source->bmp_str; dbi_dest->load_bmp_fom_file = dbi_source->load_bmp_fom_file; if(dbi_source->bmp != nullptr) { dbi_dest->bmp = al_clone_bitmap(dbi_source->bmp); } dbi_dest->print_x = dbi_source->print_x; dbi_dest->print_y = dbi_source->print_y; dbi_dest->print_w = dbi_source->print_w; dbi_dest->print_h= dbi_source->print_h; if(dbi_source->data != nullptr) { dbi_dest->data = dbi_source->data; } if(dbi_dest->load_bmp_fom_file == true) { if(dbi_dest->bmp != nullptr) { al_destroy_bitmap(dbi_dest->bmp); } dbi_dest->bmp = al_load_bitmap(dbi_dest->bmp_str.c_str()); if(dbi_dest == nullptr) { error_message("Failed to laod Image " + dbi_dest->bmp_str); } } }
void RenderTarget::copyBox(RenderTarget *src, int32_t sx, int32_t sy, int32_t w, int32_t h, int32_t tx, int32_t ty) { setAsCurrent(); int32_t a, b, c; al_get_blender(&a, &b, &c); al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ZERO); if(src->getBitmap() == this->getBitmap()) { ALLEGRO_BITMAP *src_clone = al_clone_bitmap(src->getBitmap()); al_draw_bitmap_region(src_clone, sx, sy, w, h, tx, ty, 0); al_destroy_bitmap(src_clone); } else { al_draw_bitmap_region(src->getBitmap(), sx, sy, w, h, tx, ty, 0); } al_set_blender(a, b, c); }
/* Create an example bitmap. */ static ALLEGRO_BITMAP *create_example_bitmap(void) { ALLEGRO_BITMAP *raw; ALLEGRO_BITMAP *bitmap; int i, j; ALLEGRO_LOCKED_REGION *locked; unsigned char *data; /* Create out example bitmap as a memory bitmap with a fixed format. */ al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ABGR_8888); raw = al_create_bitmap(100, 100); locked = al_lock_bitmap(raw, ALLEGRO_PIXEL_FORMAT_ANY, ALLEGRO_LOCK_WRITEONLY); data = locked->data; for (j = 0; j < 100; j++) { for (i = 0; i < 100; i++) { int x = i - 50, y = j - 50; int r = sqrt(x * x + y * y); float rc = 1 - r / 50.0; if (rc < 0) rc = 0; data[i * 4 + 0] = i * 255 / 100; data[i * 4 + 1] = j * 255 / 100; data[i * 4 + 2] = rc * 255; data[i * 4 + 3] = rc * 255; } data += locked->pitch; } al_unlock_bitmap(raw); /* Now clone it into a fast display bitmap. */ al_set_new_bitmap_flags(0); al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ANY_WITH_ALPHA); bitmap = al_clone_bitmap(raw); /* And don't forget to destroy the memory copy. */ al_destroy_bitmap(raw); return bitmap; }
T3F_ANIMATION * t3f_clone_animation(T3F_ANIMATION * ap) { int i; T3F_ANIMATION * clone = NULL; clone = t3f_create_animation(); if(clone) { if(ap->flags & T3F_ANIMATION_FLAG_EXTERNAL_BITMAPS) { clone->bitmaps = ap->bitmaps; } else { for(i = 0; i < ap->bitmaps->count; i++) { /* clone->bitmaps->bitmap[i] = al_clone_bitmap(ap->bitmaps->bitmap[i]); if(!clone->bitmaps->bitmap[i]) { return NULL; } */ if(!t3f_clone_resource((void **)&clone->bitmaps->bitmap[i], ap->bitmaps->bitmap[i])) { clone->bitmaps->bitmap[i] = al_clone_bitmap(ap->bitmaps->bitmap[i]); } } clone->bitmaps->count = ap->bitmaps->count; } for(i = 0; i < ap->frames; i++) { if(!t3f_animation_add_frame(clone, ap->frame[i]->bitmap, ap->frame[i]->x, ap->frame[i]->y, ap->frame[i]->z, ap->frame[i]->width, ap->frame[i]->height, ap->frame[i]->angle, ap->frame[i]->ticks, ap->frame[i]->flags)) { return NULL; } clone->frame[i]->flags = ap->frame[i]->flags; } clone->flags = ap->flags; } return clone; }
/* A helper function for al_clone_menu() and al_clone_menu_for_popup(). * Note that only the root menu is created as a "popup" (if popup == TRUE). */ static ALLEGRO_MENU *clone_menu(ALLEGRO_MENU *menu, bool popup) { ALLEGRO_MENU *clone = NULL; size_t i; if (menu) { clone = popup ? al_create_popup_menu() : al_create_menu(); for (i = 0; i < _al_vector_size(&menu->items); ++i) { const ALLEGRO_MENU_ITEM *item = *(ALLEGRO_MENU_ITEM **)_al_vector_ref(&menu->items, i); ALLEGRO_BITMAP *icon = item->icon; if (icon) icon = al_clone_bitmap(icon); al_append_menu_item(clone, item->caption ? al_cstr(item->caption) : NULL, item->id, item->flags, icon, al_clone_menu(item->popup)); } } return clone; }
GraphicsBuffer::GraphicsBuffer( const GraphicsBuffer& rhs ) { mpBitmap = al_clone_bitmap( rhs.mpBitmap ); }
int main(int argc, char *argv[]) { ALLEGRO_DISPLAY *display; ALLEGRO_FONT *font; (void)argc; (void)argv; if (!al_init()) { abort_example("Could not init Allegro\n"); return 1; } al_init_primitives_addon(); al_install_keyboard(); al_install_mouse(); al_init_font_addon(); al_init_image_addon(); al_set_new_display_flags(ALLEGRO_GENERATE_EXPOSE_EVENTS); display = al_create_display(800, 600); if (!display) { abort_example("Unable to create display\n"); return 1; } font = al_load_font("data/fixed_font.tga", 0, 0); if (!font) { abort_example("Failed to load data/fixed_font.tga\n"); return 1; } allegro = al_load_bitmap("data/allegro.pcx"); if (!allegro) { abort_example("Failed to load data/allegro.pcx\n"); return 1; } mysha = al_load_bitmap("data/mysha.pcx"); if (!mysha) { abort_example("Failed to load data/mysha.pcx\n"); return 1; } target = al_create_bitmap(320, 200); al_add_new_bitmap_flag(ALLEGRO_MEMORY_BITMAP); allegro_bmp = al_clone_bitmap(allegro); mysha_bmp = al_clone_bitmap(mysha); target_bmp = al_clone_bitmap(target); /* Don't remove these braces. */ { Theme theme(font); Prog prog(theme, display); prog.run(); } al_destroy_bitmap(allegro); al_destroy_bitmap(allegro_bmp); al_destroy_bitmap(mysha); al_destroy_bitmap(mysha_bmp); al_destroy_bitmap(target); al_destroy_bitmap(target_bmp); al_destroy_font(font); return 0; }
static void render(void) { double t = al_get_time(); if (regenerate) { al_destroy_bitmap(logo); al_destroy_bitmap(logo_flash); logo = NULL; regenerate = false; } if (!logo) { /* Generate a new logo. */ ALLEGRO_BITMAP *fullflash; ALLEGRO_BITMAP *fulllogo = generate_logo(param_values[0], param_values[1], strtol(param_values[2], NULL, 10), strtod(param_values[3], NULL), strtod(param_values[4], NULL), strtod(param_values[5], NULL), strtod(param_values[6], NULL), strtod(param_values[7], NULL), strtod(param_values[8], NULL), &fullflash); ALLEGRO_BITMAP *crop; int x, y, left = 640, top = 480, right = -1, bottom = -1; /* Crop out the non-transparent part. */ al_lock_bitmap(fulllogo, ALLEGRO_PIXEL_FORMAT_ANY, ALLEGRO_LOCK_READONLY); for (y = 0; y < 480; y++) { for (x = 0; x < 640; x++) { ALLEGRO_COLOR c = al_get_pixel(fulllogo, x, y); float r, g, b, a; al_unmap_rgba_f(c, &r, &g, &b, &a); if (a > 0) { if (x < left) left = x; if (y < top) top = y; if (x > right) right = x; if (y > bottom) bottom = y; } } } al_unlock_bitmap(fulllogo); if (right < left) right = left; if (bottom < top) bottom = top; crop = al_create_sub_bitmap(fulllogo, left, top, 1 + right - left, 1 + bottom - top); logo = al_clone_bitmap(crop); al_destroy_bitmap(crop); al_destroy_bitmap(fulllogo); crop = al_create_sub_bitmap(fullflash, left, top, 1 + right - left, 1 + bottom - top); logo_flash = al_clone_bitmap(crop); al_destroy_bitmap(crop); al_destroy_bitmap(fullflash); logo_x = left; logo_y = top; anim = t; } draw_background(); /* For half a second, display our flash animation. */ if (t - anim < 0.5) { ALLEGRO_STATE state; int w, h, i, j; float f = sin(ALLEGRO_PI * ((t - anim) / 0.5)); ALLEGRO_COLOR c = al_map_rgb_f(f * 0.3, f * 0.3, f * 0.3); w = al_get_bitmap_width(logo); h = al_get_bitmap_height(logo); al_store_state(&state, ALLEGRO_STATE_BLENDER); al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA); al_draw_tinted_bitmap(logo, al_map_rgba_f(1, 1, 1, 1 - f), logo_x, logo_y, 0); al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ONE); for (j = -2; j <= 2; j += 2) { for (i = -2; i <= 2; i += 2) { al_draw_tinted_bitmap(logo_flash, c, logo_x + i, logo_y + j, 0); } } al_restore_state(&state); } else al_draw_bitmap(logo, logo_x, logo_y, 0); print_parameters(); }
void RenderTarget::copy(const RenderTarget *r) { if (bitmap) { al_destroy_bitmap(bitmap); } bitmap = al_clone_bitmap(r->bitmap); }
RenderTarget *RenderTarget::clone() { RenderTarget *ret = new RenderTarget; ret->bitmap = al_clone_bitmap(this->bitmap); return ret; }
int main(int argc, char **argv) { ALLEGRO_DISPLAY *display, *ms_display; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_TIMER *timer; ALLEGRO_BITMAP *memory; char title[1024]; bool quit = false; bool redraw = true; int wx, wy; (void)argc; (void)argv; if (!al_init()) { abort_example("Couldn't initialise Allegro.\n"); } al_init_primitives_addon(); al_install_keyboard(); al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); memory = create_bitmap(); /* Create the normal display. */ al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 0, ALLEGRO_REQUIRE); al_set_new_display_option(ALLEGRO_SAMPLES, 0, ALLEGRO_SUGGEST); display = al_create_display(300, 450); if (!display) { abort_example("Error creating display\n"); } al_set_window_title(display, "Normal"); /* Create bitmaps for the normal display. */ al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR); bitmap_filter = al_clone_bitmap(memory); al_set_new_bitmap_flags(0); bitmap_normal = al_clone_bitmap(memory); font = al_create_builtin_font(); al_get_window_position(display, &wx, &wy); if (wx < 160) wx = 160; /* Create the multi-sampling display. */ al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_REQUIRE); al_set_new_display_option(ALLEGRO_SAMPLES, 4, ALLEGRO_SUGGEST); ms_display = al_create_display(300, 450); if (!ms_display) { abort_example("Multisampling not available.\n"); } sprintf(title, "Multisampling (%dx)", al_get_display_option( ms_display, ALLEGRO_SAMPLES)); al_set_window_title(ms_display, title); /* Create bitmaps for the multi-sampling display. */ al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR); bitmap_filter_ms = al_clone_bitmap(memory); al_set_new_bitmap_flags(0); bitmap_normal_ms = al_clone_bitmap(memory); font_ms = al_create_builtin_font(); /* Move the windows next to each other, because some window manager * would put them on top of each other otherwise. */ al_set_window_position(display, wx - 160, wy); al_set_window_position(ms_display, wx + 160, wy); timer = al_create_timer(1.0 / 30.0); queue = al_create_event_queue(); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_display_event_source(display)); al_register_event_source(queue, al_get_display_event_source(ms_display)); al_register_event_source(queue, al_get_timer_event_source(timer)); al_start_timer(timer); while (!quit) { ALLEGRO_EVENT event; /* Check for ESC key or close button event and quit in either case. */ al_wait_for_event(queue, &event); switch (event.type) { case ALLEGRO_EVENT_DISPLAY_CLOSE: quit = true; break; case ALLEGRO_EVENT_KEY_DOWN: if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) quit = true; break; case ALLEGRO_EVENT_TIMER: bitmap_move(); redraw = true; break; } if (redraw && al_is_event_queue_empty(queue)) { /* Draw the multi-sampled version into the first window. */ al_set_target_backbuffer(ms_display); al_clear_to_color(al_map_rgb_f(1, 1, 1)); draw(bitmap_filter_ms, 0, "filtered, multi-sample"); draw(bitmap_normal_ms, 250, "no filter, multi-sample"); al_flip_display(); /* Draw the normal version into the second window. */ al_set_target_backbuffer(display); al_clear_to_color(al_map_rgb_f(1, 1, 1)); draw(bitmap_filter, 0, "filtered"); draw(bitmap_normal, 250, "no filter"); al_flip_display(); redraw = false; } } return 0; }
//Render for States void state_render () { if (state_switched) return; if (state_terminated) return; state_to_render = 1; state_to_force_render = 0; //To Set up for Transition Render if (state_trans_curf >= 0) { if (rbufferorig == NULL) rbufferorig = al_clone_bitmap (rbuffer); if (transrbuffer == NULL) transrbuffer = al_clone_bitmap (rbuffer); } //Common for All Games al_set_target_bitmap (rbuffer); handlers[state].proc_render (); //Transition Render if (state_trans_curf >= 0 && state_trans_curf < state_trans_totf && strans && rbufferorig && transrbuffer) { strans (rbufferorig, rbuffer, transrbuffer, state_trans_curf++, state_trans_totf); state_to_force_render = 1; } //To Destroy Transition if (strans && (state_trans_curf >= state_trans_totf)) { trans_stop (); state_to_force_render = 1; } //Switch Modes if (state_to_switch_render) init_screen (); //This is Because of GUI. state_to_force_render = 1; if (state_to_force_render || state_to_switch_render || state_to_render) { //Render to Screen al_set_target_backbuffer (screen); if (transrbuffer) blit (transrbuffer, 0, 0, 0, 0, xres, yres, 0); else blit (rbuffer, 0, 0, 0, 0, xres, yres, 0); gui_render (); #ifdef LINUX //VSync helped remove bad artifacts in Linux al_wait_for_vsync (); #endif //LINUX al_flip_display (); } state_to_switch_render = 0; }
/** Clones this bitmap. @return a copy of this bitmap. */ Bitmap clone() const { return al_clone_bitmap(get()); }
MBITMAP *m_clone_bitmap(MBITMAP *b) { ALLEGRO_BITMAP *bmp = al_clone_bitmap(b->bitmap); MBITMAP *m = new_mbitmap(bmp); return m; }
/* Function: al_grab_font_from_bitmap */ ALLEGRO_FONT *al_grab_font_from_bitmap(ALLEGRO_BITMAP *bmp, int ranges_n, const int ranges[]) { ALLEGRO_FONT *f; ALLEGRO_FONT_COLOR_DATA *cf, *prev = NULL; ALLEGRO_STATE backup; int i; ALLEGRO_COLOR mask = al_get_pixel(bmp, 0, 0); ALLEGRO_BITMAP *glyphs = NULL, *unmasked = NULL; int import_x = 0, import_y = 0; ALLEGRO_LOCKED_REGION *lock = NULL; int w, h; ASSERT(bmp); w = al_get_bitmap_width(bmp); h = al_get_bitmap_height(bmp); f = al_calloc(1, sizeof *f); f->vtable = &_al_font_vtable_color; al_store_state(&backup, ALLEGRO_STATE_NEW_BITMAP_PARAMETERS); al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ANY_WITH_ALPHA); unmasked = al_clone_bitmap(bmp); /* At least with OpenGL, texture pixels at the very border of * the glyph are sometimes partly sampled from the yellow mask * pixels. To work around this, we replace the mask with full * transparency. * And we best do it on a memory copy to avoid loading back a texture. */ al_convert_mask_to_alpha(unmasked, mask); al_restore_state(&backup); al_store_state(&backup, ALLEGRO_STATE_BITMAP | ALLEGRO_STATE_BLENDER); // Use the users preferred format, so don't set this below! //al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ANY_WITH_ALPHA); for (i = 0; i < ranges_n; i++) { int first = ranges[i * 2]; int last = ranges[i * 2 + 1]; int n = 1 + last - first; cf = al_calloc(1, sizeof(ALLEGRO_FONT_COLOR_DATA)); if (prev) prev->next = cf; else f->data = cf; cf->bitmaps = al_malloc(sizeof(ALLEGRO_BITMAP*) * n); cf->bitmaps[0] = NULL; if (!glyphs) { glyphs = al_clone_bitmap(unmasked); if (!glyphs) goto cleanup_and_fail_on_error; lock = al_lock_bitmap(bmp, ALLEGRO_PIXEL_FORMAT_RGBA_8888, ALLEGRO_LOCK_READONLY); } cf->glyphs = glyphs; if (import_bitmap_font_color(lock->data, lock->pitch, w, h, cf->bitmaps, cf->glyphs, n, &import_x, &import_y)) { goto cleanup_and_fail_on_error; } else { cf->begin = first; cf->end = last + 1; prev = cf; } } al_restore_state(&backup); cf = f->data; if (cf && cf->bitmaps[0]) f->height = al_get_bitmap_height(cf->bitmaps[0]); if (lock) al_unlock_bitmap(bmp); if (unmasked) al_destroy_bitmap(unmasked); f->dtor_item = _al_register_destructor(_al_dtor_list, "font", f, (void (*)(void *))al_destroy_font); return f; cleanup_and_fail_on_error: if (lock) al_unlock_bitmap(bmp); al_restore_state(&backup); al_destroy_font(f); if (unmasked) al_destroy_bitmap(unmasked); return NULL; }
int main(int argc, const char *argv[]) { const char *filename; ALLEGRO_DISPLAY *display; ALLEGRO_BITMAP *membitmap, *bitmap; ALLEGRO_TIMER *timer; ALLEGRO_EVENT_QUEUE *queue; bool redraw = true; double zoom = 1; double t0; double t1; if (argc > 1) { filename = argv[1]; } else { filename = "data/mysha.pcx"; } if (!al_init()) { abort_example("Could not init Allegro.\n"); } if (argc > 2) { al_set_new_display_adapter(atoi(argv[2])); } al_install_mouse(); al_install_keyboard(); al_init_image_addon(); display = al_create_display(640, 480); if (!display) { abort_example("Error creating display\n"); } al_set_window_title(display, filename); /* We load the bitmap into a memory bitmap, because creating a * display bitmap could fail if the bitmap is too big to fit into a * single texture. * FIXME: Or should A5 automatically created multiple display bitmaps? */ al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); t0 = al_get_time(); membitmap = al_load_bitmap(filename); t1 = al_get_time(); if (!membitmap) { abort_example("%s not found or failed to load\n", filename); } al_set_new_bitmap_flags(ALLEGRO_VIDEO_BITMAP); printf("Loading took %.4f seconds\n", t1 - t0); // FIXME: // Now try to split the memory bitmap into display bitmaps? bitmap = al_clone_bitmap(membitmap); if (!bitmap) bitmap = membitmap; timer = al_create_timer(1.0 / 30); queue = al_create_event_queue(); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_display_event_source(display)); al_register_event_source(queue, al_get_timer_event_source(timer)); al_start_timer(timer); while (1) { ALLEGRO_EVENT event; al_wait_for_event(queue, &event); if (event.type == ALLEGRO_EVENT_DISPLAY_ORIENTATION) { int o = event.display.orientation; if (o == ALLEGRO_DISPLAY_ORIENTATION_0_DEGREES) { printf("0 degrees\n"); } else if (o == ALLEGRO_DISPLAY_ORIENTATION_90_DEGREES) { printf("90 degrees\n"); } else if (o == ALLEGRO_DISPLAY_ORIENTATION_180_DEGREES) { printf("180 degrees\n"); } else if (o == ALLEGRO_DISPLAY_ORIENTATION_270_DEGREES) { printf("270 degrees\n"); } else if (o == ALLEGRO_DISPLAY_ORIENTATION_FACE_UP) { printf("Face up\n"); } else if (o == ALLEGRO_DISPLAY_ORIENTATION_FACE_DOWN) { printf("Face down\n"); } } if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) break; if (event.type == ALLEGRO_EVENT_KEY_CHAR) { if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) break; if (event.keyboard.unichar == '1') zoom = 1; if (event.keyboard.unichar == '+') zoom *= 1.1; if (event.keyboard.unichar == '-') zoom /= 1.1; if (event.keyboard.unichar == 'f') zoom = (double)al_get_display_width(display) / al_get_bitmap_width(bitmap); } if (event.type == ALLEGRO_EVENT_TIMER) redraw = true; if (redraw && al_is_event_queue_empty(queue)) { redraw = false; al_clear_to_color(al_map_rgb_f(0, 0, 0)); if (zoom == 1) al_draw_bitmap(bitmap, 0, 0, 0); else al_draw_scaled_rotated_bitmap( bitmap, 0, 0, 0, 0, zoom, zoom, 0, 0); al_flip_display(); } } al_destroy_bitmap(bitmap); return 0; }