The displaying of bitmaps can be quite a rapid process. Most modern games involve the display and animation of bitmaps. This section demonstrates the use of timers to animate the display of bitmaps. In this case, a bitmapped ball will be shifted around a window. A snapshot of the bouncing ball is shown below.
The creation message (see source code) of the client window does little except to query the aspect ratio of a pixel and start a timer, as shown in the code fragment below.
case message::create: { ... handle device_context = get_device_context(window_handle); data->x_pixel = get_device_capabilities(device_context,capability::aspect_x); data->y_pixel = get_device_capabilities(device_context,capability::aspect_y); release_device_context(window_handle,device_context); } break;
The timer is set at 10 milliseconds - which exceeds the maximum rate for a standard clock (so high performance counter hardware is assumed to be installed). Most of the hard work is done when sizing the window, where the bitmap for the ball is created - as shown below.
case message::size: { data->x_center = (data->width_of_client = low_part(parameter2)) / 2; data->y_center = (data->height_of_client = high_part(parameter2)) / 2; int scale = minimum(width_of_client * data->y_pixel, height_of_client * yPixel) / 16; data->x_radius = scale/data->x_pixel; data->y_radius = scale/data->y_pixel; data->x_move = maximum(1,data->x_radius/8); data->y_move = maximum(1,data->y_radius/8); data->xtotal = 2 * (data->x_radius + data->x_move); data->ytotal = 2 * (data->y_radius + data->y_move); if (bitmap) delete_object(bitmap); handle device_context = get_device_context(window_handle); handle memory_device = create_memory_device_context(device_context); bitmap = create_compatible_bitmap(device_context,data->x_total,data->y_total); release_device_context(window_handle,device_context); select_object(memory_device,bitmap); draw_rectangle(memory_device,-1, -1,data->x_total+1,data->y_total+1); handle brush_handle = create_hatch_brush(hatch_style::diagonal_cross,0); select_object(memory_device,brush); set_background_color(memory_device,red_green_blue(255,0,255)); draw_ellipse(memory_device, data->x_move, data->y_move, data->x_total-XMmove, data->y_total-data->y_move); delete_device_context(memory_device); delete_object(brush); } break;
The inital position of the ball is the center of the client window (data->x_center,data->y_center). The size of the ball is 1/16th the size of the width or height of the client window (whichever is smaller). The ball is moved 1/8th of its original size each time the timer ticks. If a smaller fraction like 1/16th is used, the ball appears to travel more smoothly and slowly. A device context and a memory device context are created. The bitmap is replaced and then selected into the newly created memory device context. A rectangle is drawn and a hatch brush is set, the background color is changed and the ball is drawn using the ellipse drawing function. This completes the creation of the bitmap for the ball.
When a timer message is received, a bit block transfer is used to shift the ball - as shown below.
case message::timer: if (bitmap) { handle device_context = get_device_context(window_handle); handle memory_device = create_memory_device_context(device_context); select_object(memory_device,bitmap); bit_block_transfer(device_context, data->x_center-data->x_total/2, data->y_center-data->y_total/2, data->x_total, data->y_total, memory_device, 0, 0, raster_operation::source_copy); release_device_context(window_handle,device_context); delete_device_context(memory_device); data->x_center += data->x_move; data->y_center += data->y_move; ... } break;
Firstly, a device context and a memory device context are created and the ball bitmap is selected into the memory device context. bit block transfer is then used to draw the ball at its center. The new center is then recalculated for when the next timer message is received. The center is displaced 1/8th the radius of the ball (as previously mentioned).