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



tải về 3.11 Mb.
trang18/21
Chuyển đổi dữ liệu24.11.2017
Kích3.11 Mb.
#34506
1   ...   13   14   15   16   17   18   19   20   21
//update ticks

ticks++;

//calculate framerate once per second

if (clock() > start + 1000)

{

counter++;

start = clock();

framerate = ticks;

ticks = 0;

}

//display framerate

blit(back, buffer, 320-70, 330, 320-70, 330, 140, 20);

textprintf centre ex(buffer,font,320,330,WHITE,-1,

"COUNTER %d", counter);

textprintf centre ex(buffer,font,320,340,WHITE,-1,

"FRAMERATE %d",framerate);

//update the screen

acquire screen();

blit(buffer,screen,0,0,0,0,SCREEN W-1,SCREEN H-1);

release screen();

}

//remove objects from memory

destroy bitmap(back);

destroy bitmap(buffer);

for (n=0; n

{

for (f=0; fmaxframe+1; f++)

destroy bitmap(sprite images[n][f]);

free(sprites[n]);

}

allegro exit();

return 0;

}

END OF MAIN()

9.2. Interrupt Handlers

This is a limited form ofmulti-threading in concept, although interrupt handlers do not run in parallel, but sequentially and based on the interval. Because no two interrupt handlers will ever be running at the same time, you don’t need to worry about corrupted data as you do with threading.

9.2.1 Creating an Interrupt Handler

After you have installed the timer using install timer, you can create one or more interrupt handlers using the install int function.



int install int(void (*proc)(), int speed)

9.2.1 The InterruptTest Program

Figure 9.2

The InterruptTest program demonstrates how you can use an interrupt callback function to calculate theframe rate.

Instead of using a ticks variable and the clock function to determine when to mark each second, this new program uses an interrupt handler that is set to 1,000 milliseconds to automatically tick off a second. To make things easier, I have modified the TimerTest program (which was quite lengthy) to use an interrupt instead of a simple timer;

Let’s get started on the changes, fewthat they are. The first change is up near the top of the program, where the counter, ticks, and framerate variables are declared. Add volatile to their definitions. This volatile key word tells the compiler that the variable should be protected because it is used by the interrupt.

/timer variables

volatile int counter;

volatile int ticks;

volatile int framerate;

Next, you need to add the interrupt handler callback function, timer1, to the

program. You can add this function right above main or up at the top of the

program, as long as it’s visible to main. Note how simple this function is; it

increments counter (for seconds), sets the framerate variable, and resets the ticks

variable.



//calculate framerate once per second

void timer1(void)

{

counter++;

framerate = ticks;

ticks=0;

}

END OF FUNCTION(timer1)

9.2.3 The TimedLoop Program

First, up near the top of the program, add another volatile variable.



//timer variables

volatile int counter;

volatile int ticks;

volatile int framerate;

volatile int resting, rested;

Scroll down to the timer1 interrupt callback function and add a line to it.



//calculate framerate every second

void timer1(void)

{

counter++;

framerate = ticks;

ticks=0;

rested = resting;

}

END OF FUNCTION(timer1)

Now you create the function that is called by rest callback. You can add this

function below timer1.

//do something while resting (?)

void rest1(void)

{

resting++;

}

The next change takes place in main, adding the code to call the rest callback

function, which is a call to rest1, just added. Note also the changes to the section

of code that displays the counter and frame rate. I have changed the last parameter of blit from 20 to 30 to erase the new line, which is also listed below,

highlighted in bold. This displays the number of ticks that transpired while the

program was waiting inside the rest1 callback function.

//update ticks

ticks++;


//slow the game down

resting=0;

rest callback(8, rest1);

//display framerate



Каталог: 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   ...   13   14   15   16   17   18   19   20   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