Chủ nhiệm Bộ môn Ngô Hữu Phúc ĐỀ CƯƠng chi tiết bài giảNG


Drawing Lines and Rectangles



tải về 3.11 Mb.
trang8/21
Chuyển đổi dữ liệu24.11.2017
Kích3.11 Mb.
#34506
1   ...   4   5   6   7   8   9   10   11   ...   21
5.2.2 Drawing Lines and Rectangles

The next step up from the lowly pixel is the line, and Allegro provides several line-drawing functions. To keep things as efficient as possible, Allegro divides line drawing among three functions—one for horizontal lines, one for vertical lines, and a third for every other type of line. Drawing horizontal and vertical lines can be an extremely optimized process using a simple high-speed memory copy, but non-aligned lines must be drawn using an algorithm to fill in the pixels between two points specified for the line (see Figure 3.3).





5.2.2.1 Horizontal Lines

The horizontal line-drawing function is called hline:

void hline(BITMAP *bmp, int x1, int y, int x2, int color)

Because this is your first function for drawing lines, allow me to elaborate. The

first parameter, BITMAP *bmp, is the destination bitmap for the line, which can be

‘‘screen’’ if you want to draw directly to the screen. The next three parameters,

int x1, int y, and int x2, specify the two points on the single horizontal Y-axis

where the line should be drawn. The HLines program (shown in Figure 3.4)



demonstrates how to use this function.

Figure 3.4

The HLines program draws horizontal lines.

#include

#include "allegro.h"

int main(void)

{

int x,y,x1,y1,x2,y2;

int red,green,blue,color;

//initialize Allegro

allegro init();

//initialize the keyboard

install keyboard();

110 Chapter 3 n 2D Vector Graphics Programming

Figure 3.9

A line is comprised of pixels filled in between point A and point B.

//initialize random seed

srand(time(NULL));

//initialize video mode to 640x480

int ret = set gfx mode(GFX AUTODETECT WINDOWED, 640, 480, 0, 0);

if (ret != 0) {

allegro message(allegro error);

return 1;

}

//display screen resolution

textprintf ex(screen, font, 0, 0, 15, -1,

"HLines Program - %dx%d - Press ESC to quit",

SCREEN W, SCREEN H);

//wait for keypress

while(!key[KEY ESC])

{

//set a random location

x1 = 10 + rand() % (SCREEN W-20);

y = 10 + rand() % (SCREEN H-20);

x2 = 10 + rand() % (SCREEN W-20);

Drawing Graphics Primitives 111

Figure 3.10

The HLines program draws horizontal lines.

//set a random color

red = rand() % 255;

green = rand() % 255;

blue = rand() % 255;

color = makecol(red,green,blue);

//draw the horizontal line

hline(screen, x1,y,x2,color);

}

//end program

allegro exit();

return 0;

}

END OF MAIN()

You have probably noticed that the HLines program is very similar to the Pixels

program, with only a few lines that differ inside the while loop. I’ll just show the

differences from this point forward, rather than listing the entire source code for

each program, because in most cases you simply need to replace a few lines inside

main. It is pretty obvious that just a few lines inside the while loop need to be

changed. The entire programs are available on the CD-ROM in complete form,

but I will provide only partial listings where such changes are needed to

demonstrate each of these graphics primitives.


Каталог: files -> FileMonHoc
FileMonHoc -> NGÂn hàng câu hỏi lập trình cơ BẢn nhóm câu hỏI 2 ĐIỂM
FileMonHoc -> CHƯƠng 2 giới thiệu về LÝ thuyết số
FileMonHoc -> CÁc hệ MẬt khoá CÔng khai kháC
FileMonHoc -> BỘ MÔn duyệt chủ nhiệm Bộ môn
FileMonHoc -> Khoa công nghệ thông tin cộng hòa xã HỘi chủ nghĩa việt nam
FileMonHoc -> Chủ nhiệm Bộ môn Ngô Thành Long ĐỀ CƯƠng chi tiết bài giảNG
FileMonHoc -> Chủ nhiệm Bộ môn Phan Nguyên Hải ĐỀ CƯƠng chi tiết bài giảNG
FileMonHoc -> Khoa: CÔng nghệ thông tin cộng hòa xã HỘi chủ nghĩa việt nam
FileMonHoc -> MẬt mã khóA ĐỐi xứng lý thuyết cơ bản của Shannon
FileMonHoc -> Khoa công nghệ thông tin bài giảng LẬp trình cơ BẢn biên soạn

tải về 3.11 Mb.

Chia sẻ với bạn bè của bạn:
1   ...   4   5   6   7   8   9   10   11   ...   21




Cơ sở dữ liệu được bảo vệ bởi bản quyền ©hocday.com 2024
được sử dụng cho việc quản lý

    Quê hương