2D game engine Allegro series tutorial (3) load and display pictures!

Source: Internet
Author: User
Tags mail exchange

This series of articles is written by sky. For more information, see the source. Http://blog.csdn.net/qq573011406/article/details/8220208

Author: Yuan quanwei mail: qq573011406@126.com welcome mail exchange programming experience

Index of this series of tutorials:
Tutorial series of 2D game engine Allegro (1) configure the allegro Development Environment
2D game engine Allegro series tutorial (2) Hello world!
2D game engine Allegro series tutorial (3) load and display pictures!

2D game engine Allegro series tutorial (4) text rendering and display of Chinese Characters

I. Basic Steps

The goal of this chapter is to display images in the window. below is the running result graph. I used a map image extracted from fantasy westward journey. the following describes how to display an image.

The basic steps are as follows:

Step 1: Include the header file of the image plug-in for image loading.

Step 2: link the lib of the image plug-in used to load images

Step 3: Define a pointer to the ALLEGRO_BITMAP type to point to the created or loaded image.

Step 4: Use the al_init_image_addon () function to initialize the image plug-in.

Step 5: Use the al_load_bitmap ("file name") function to load images.

Step 6: Use al_draw_bitmap () to render the image to the screen.

Step 7: release the resources occupied by ALLEGRO_BITMAP.

Ii. Steps

Next let's take a look at the specific steps:

Step 1: Include the header file of the image plug-in used to load images. The header file of the image is Allegro5/allegro_image.hDo not forget to include this header file. Step 1: link the lib of the image plug-in used to load images and then link the library file. The corresponding library file is Allegro_image-5.0.7-mt-debug.libYou can use the following line of code to directly link this library file.
# Pragma comment (lib, "allegro_image-5.0.7-mt-debug.lib") // link the image library to load the image

Step 3: Define a pointer to the ALLEGRO_BITMAP type to point to the created or loaded image. ALLEGRO_BITMAP:As a game library, image processing is very important. Allegro provides the concept of BITMAP, which is also supported by almost all image-related programs.Translate the image into a bitmap, that is, an image composed of bits.ALLEGRO_BITMAP is a structure that contains a memory block of a rectangular image, which is stored in bytes consecutively. Of course, this memory may not be memory or display memory.You can even directly map to the screen, or share a part of another bitmap.. -- My programming insights on the game journey. The cloud is inspired by the concept of BITMAP. With the above, we don't have to say much. Let's continue. Use the following code to declare an ALLEGRO_BITMAP pointer.

ALLEGRO_BITMAP *bmp=NULL;

Step 4: Use the al_init_image_addon () function to initialize the image plug-in. This step is also very important. Without this step, the function for loading images cannot work at all. Put this function in your game initialization function, and put it after the al_init () function. remember the framework program we wrote in the previous chapter, where we wrote the game_init () function. it initializes most games. now let's put the al_init_image_addon () function into game_init.

al_init_image_addon();

Step 5: Use the al_load_bitmap ("file name") function to load images.

ALLEGRO_BITMAP *al_load_bitmap(const char *filename)

Load an image to ALLEGRO_BITMAP. If it succeeds, the pointer of the ALLEGRO_BITMAP is returned. If it fails, NULL is returned. Similarly, put this function in game_init.

bmp=al_load_bitmap("bmp.jpg");if(!bmp)return 3;

You can put the image file in the debug directory under the project directory. Step 6: Use al_draw_bitmap () to render the image to the screen.

void al_draw_bitmap(ALLEGRO_BITMAP *bitmap, float dx, float dy, int flags)

Render ALLEGRO_BITMAP to the specified position (dx, dy) of the target BITMAP. The last parameter can be set to 0. We will use this function in game_render.

Int game_render () {// fill the screen with the specified color al_clear_to_color (al_map_rgb (, 0); // flip al_draw_bitmap (bmp, 0); al_flip_display (); return 0 ;}

Step 7: release the resources occupied by ALLEGRO_BITMAP.

void al_destroy_bitmap(ALLEGRO_BITMAP *bitmap)

Use this function in game_destory () to release the memory occupied by BITMAP.

int game_destory(){al_destroy_bitmap(bmp);return 0;}
3. complete source code and Images
// Header file # define allegro_no_magic_main # include <windows. h> # include <allegro5/Allegro. h> # include <allegro5/allegro_image.h> # pragma comment (Lib, "allegro-5.0.7-mt-debug.lib") // link the library of Allegro # pragma comment (Lib, "allegro_image-5.0.7-mt-debug.lib ") // link to the image library, used to load the image ///////////////////////////////////// //////////////////////////////////////// //// // function int game_init (); // initialize the game int game_run (); // enter the game loop int ga Me_frame (); // logic processing function int game_render (); // rendering function int game_destory (); // release the resource int game_msg (); // Message Processing // constant const int win_width = 800; // window width const int win_height = 600; // window height // global variable allegro_display * display; // display the device allegro_event_queue * queue; // event queue allegro_event my_event; // event allegro_bitmap * BMP = 0; //////////////////////////////////////// //////////////////////////////////////// //// // int winapi winmain (hinstance H Instance, hinstance hprevinstance, lpstr lpcmdline, int nshowcmd) {int error = 0; // write the game initialization code in our game_init () in the function // put it here and execute // assign the returned value of game_init () to error // If the returned value is 0, it means everything is normal and Initialization is successful! // If the value is not 0, an error occurs during initialization. // End the program immediately and return the error code error = game_init (); If (error! = 0) Return Error; // After initialization, the main loop of the game starts to enter. // The main loop is executed cyclically, messages are processed, and logic is processed, and the rendering function error = game_run (); If (error! = 0) Return Error; // destroy the occupied resource after the game ends. Error = game_destory (); If (error! = 0) Return Error; return 0;} int game_init () {If (! Al_init () return 1; /*************************************** ********************************** use al_init () al_init is actually a macro. It actually calls al_install_system (allegro_version_int, atexit). The return value of this function is boolean. If the initialization is successful, true is returned, otherwise, false * // ********************************* is returned *//********************************** * ***********************************/al_install_mouse (); al_install_keyboard (); al_init_image_addon (); /************************************* ********************************** // Initialize the mouse, and keyboard devices, so that Allegro can receive mouse and keyboard messages *//**************************** **************************************** * *** // al_set_new_display_flags (allegro_fullscreen ); // If you cancel the annotation of this line, the full screen mode is display = al_create_display (win_width, win_height); If (! Display) return 2; BMP = al_load_bitmap ("BMP .jpg"); If (! BMP) return 3; /*************************************** ******************************** // * allegro_dispaly * al_create_display (int W, int h) when the display device parameter W is the width of the window to be created, and H is the height of the window, a disply pointer is returned, otherwise, null/*********************************** is returned /************************************ ********************************** // set windows title al_set_window_title (display, "2D game engine Allegro series tutorial (3) how to load and display pictures! "); // Initialize the event queue. The received messages will be stored in this queue: queue = al_create_event_queue (); // specify the device message al_register_event_source (queue, struct (); al_register_event_source (queue, al_get_display_event_source (Display); Return 0;} int game_msg () {al_wait_for_event (queue, & my_event ); /*************************************** ******************************* // void al_wait_for_event (allegro_event_queue * queue, all Egro_event * ret_event) when a new event exists in the queue, copy the content of the new event to ret_event, and remove it from the event queue *//****************************** **************************************** **/If (my_event.type = allegro_event_display_close) return 98; // when the window is closed, if (my_event.type = allegro_event_key_char) {If (my_event.keyboard.keycode = allegro_key_escape) // return 99 when the ESC key is pressed;} return 0 ;} int game_frame () {return 0;} int game_render () {// fill the screen with the specified color al_cle Ar_to_color (al_map_rgb (0, 0); // flip al_draw_bitmap (BMP, 0, 0); al_flip_display (); Return 0;} int game_run () {// the following variables are used to record the time. The unit is second // used to control the frame rate (FPS), that is, the number of frames refreshed per second. // here, we set the FPS to 30, 1 second time unit/FPs // that is, 0.033, that is, the interval between each refresh cannot be less than 0.033 seconds double t_now = 0.0; // double t_pre = 0.0 at the current time; int error = 0; while (true) {If (! Al_is_event_queue_empty (Queue) {// check whether a new event error = game_msg () exists in the correct event; // If (error! = 0) Return Error;} else {// if no new event exists in the event queue, // when calculating the event interval, it is greater than 0.033t _ now = al_get_time (); if (t_now-t_pre> = 0.033) {// If the interval is appropriate then // update the new frame of the game logic error = game_frame (); If (error! = 0) Return Error; // refresh the screen. Error = game_render (); If (error! = 0) Return Error; t_pre = t_now;} else {// when the CPU can process game logic beyond our needs, // call sleep (0) // This Windows API will allow the current thread to release its control, // This is very important to the Windows platform, so that the game will not occupy all of the // CPU, sleep (0) ;}}return 0 ;}int game_destory () {al_destroy_bitmap (BMP); Return 0;} is used up for each game switching process ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.