A Basic Game Loop
Android Game Loop
- Yêu cầu SV chuẩn bị:
Chú ý nghe giảng.
Tích cực tham gia phát biểu ý kiến
Bài giảng 5: Đồ họa 2D
Tiết thứ: 11-12 Tuần thứ: 4
- Mục đích, yêu cầu:
• Nắm được cách vẽ điểm, đường thẳng, đường tròn, elipse, đường cong, tam giác, đa giác và cách làm việc với Text, cách tô màu.
- Hình thức tổ chức dạy học: Lý thuyết, bài tập, thực hành, tự học, tự nghiên cứu
- Thời gian: Lý thuyết: 2t; Tự học, tự nghiên cứu: 4t
- Địa điểm: Giảng đường do P2 phân công.
- Nội dung chính:
5.1. The InitGraphics Program
How to initialize a full-screen video mode or window of a particular resolution. Figure 3.1 shows the program running.
Figure 3.1
The InitGraphics program.
Drawing Graphics Primitives
You are able to see every pixel or line in a
vector-based program.
Vector displays are contrasted with bitmapped displays, in which the screen is a bitmap array (the videobuffer).
5.2. Basic 2D graphics
5.2.1. Drawing Pixels
The simplest graphics primitive is obviously the pixel-drawing function, and
Allegro provides one:
void putpixel(BITMAP *bmp, int x, int y, int color)
Figure 3.2 shows the output of the Pixels program, which draws random pixels
on the screen using whatever video mode and resolution you prefer.
#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();
//initialize the random number seed
srand(time(NULL));
//initialize video mode to 640x480
int ret = set gfx mode(GFX AUTODETECT WINDOWED, 640, 480, 0, 0);
Figure 3.8
The Pixels program fills the screen with dots. (The Linux version is shown.)
if (ret != 0) {
allegro message(allegro error);
return 1;
}
//display screen resolution
textprintf ex(screen, font, 0, 0, 15, -1,
"Pixels Program - %dx%d - Press ESC to quit",
SCREEN W, SCREEN H);
//wait for keypress
while(!key[KEY ESC])
{
//set a random location
x = 10 + rand() % (SCREEN W-20);
y = 10 + rand() % (SCREEN H-20);
//set a random color
red = rand() % 255;
green = rand() % 255;
blue = rand() % 255;
color = makecol(red,green,blue);
//draw the pixel
putpixel(screen, x, y, color);
}
//end program
allegro exit();
return 0;
}
END OF MAIN()
This program should be clear to you, although it uses a C function called srand to initialize the random-number seed. This program performs a while loop continually until the ESC key is pressed, during which time a pixel of random color and location is drawn using the putpixel function.
The InitGraphics Source Code
The InitGraphics program source code listing follows. Several new functions in
this program are included for convenience; I will go over them shortly. As is the
case with most of the programs in this book, hit the Escape key to quit.
#include
#include "allegro.h"
int main(void)
{
//initialize Allegro
allegro init();
//initialize the keyboard
install keyboard();
//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(screen, font, 0, 0, makecol(255, 255, 255),
"%dx%d", SCREEN W, SCREEN H);
102 Chapter 3 n 2D Vector Graphics Programming
//wait for keypress
while(!key[KEY ESC]);
//end program
allegro exit();
return 0;
}
END OF MAIN()
Chia sẻ với bạn bè của bạn: |