Lesson 3.


65 views
Uploaded on:
Category: Music / Dance
Description
Lesson 3 Essential 2D Representation Programming with Allegro Doing 2D with Allegro Since we are stretching the 2D envelope as far as possible in this course, it is fitting that we ought to begin toward the starting and cover vector design.
Transcripts
Slide 1

Lesson 3 Basic 2D Graphics Programming with Allegro

Slide 2

Doing 2D with Allegro Because we are stretching the 2D envelope as far as possible in this course, it is fitting that we ought to begin toward the starting and spread vector illustrations. The term vector depicts the CRT ( Cathode Ray Tube) screens of the past and the vector representation equipment incorporated with the PCs that utilized this early innovation.

Slide 3

Doing 2D with Allegro A representation primitive is a capacity that draws a straightforward geometric shape, for example, a point, line, rectangle, or circle. I ought to call attention to likewise that these illustrations primitives shape the premise of every one of the 3D representation, over a wide span of time; all things considered, the mantra of the 3D card is the sacred polygon.

Slide 5

Video Cards The truth is that feature cards are not intended to render recreations; they are intended to render geometric primitives with enhancements. To the extent the feature card is worried, there is one and only triangle on the screen. The developer advises the feature card to move starting with one triangle then onto the next. The feature card does this so rapidly (on the request of 100 million or more polygons for each second) that it tricks the viewer into trusting that the feature card is rendering a whole scene all alone.

Slide 6

Video Cards The triangles are concealed away in the scene\'s grid (in a manner of speaking), and it is turning out to be more hard to observe reality from virtual reality because of the propelled elements incorporated with the most recent design chips.

Slide 7

Xbox 360 The Xbox 360 can mash through 3 trillion operations for every second with its triple-center, hexa-strung processor.

Slide 8

Vertices Taken a stage closer, every triangle is comprised of three vertices , which is truly every one of the design chip thinks about. Filling pixels between the three focuses and applying impacts, (for example, lighting) are errands that the representation chip has been intended to do rapidly and effectively.

Slide 9

2D Acceleration The most punctual Windows quickening agents , as they were known, delivered for Windows 3.1 and Windows 95 gave equipment blitting. Blit is a term that implies bit-square exchange, a strategy for exchanging a lump of memory starting with one place then onto the next. On account of a graphical blit, the procedure includes duplicating a piece of information from framework memory through the transport to the memory present on the feature card. In the early years of the PC, feature cards were fortunate to have 1 MB of memory.

Slide 11

Video Memory Contrast this with the most recent 3D cards that have 256 MB of DDR ( Double Data Rate ) memory and are upgraded with direct access to the AGP transport! It essentially must be as quick as could reasonably be expected to continue sustaining the avaricious illustrations chip, which eats surfaces in feature memory and regurgitates them out into the casing cushion, which is sent straightforwardly to the screen.

Slide 13

Parallel Processing In a manifestly obvious sense, the design card is a little PC all alone. When you consider that the run of the mill top of the line PC likewise has an elite sound preparing card, (for example, the Sound Blaster Audigy 2 by Creative Labs) equipped for Dolby DTS and Dolby Digital 5.1 encompass sound, what we are truly discussing here is a multi-processor framework.

Slide 14

Graphics Fundamentals The premise of this whole section can be abridged in a solitary word: pixel . The word "pixel" is short for "picture element," kind of the nuclear component of the screen. The pixel is the littlest unit of estimation in a feature framework.

Slide 15

Graphics Fundamentals But like the iota you know from material science, even the littlest building square is involved yet littler things. On account of a pixel, those quantum components of the pixel are red, green, and blue electron streams that give every pixel a particular shading. This is not simple hypothesis or similarity; every pixel is contained three little surges of electrons of fluctuating shades of red, green, and blue

Slide 17

Allegro Graphics Starting with this most fundamental building piece, you can develop a whole amusement one pixel at once. Allegro makes a worldwide screen pointer when you call allegro_init . This basic pointer is called screen , and you can pass it to the greater part of the drawing capacities.

Slide 18

Allegro Graphics A procedure called twofold buffering (which uses off-screen rendering for velocity) works like this: drawing schedules must attract out to a memory bitmap, which is then blitted to the screen in a solitary capacity call. Until you begin utilizing a twofold cradle, you\'ll simply work with the worldwide screen object.

Slide 19

Setting Graphics Mode The first capacity you\'ll find out about is set_gfx_mode , which sets the illustrations mode. This capacity is truly stacked, in spite of the fact that you would not realize that just from calling it. set_gfx_mode does a considerable measure of work when called: distinguishing the illustrations card recognizing and instating the representation framework checking or setting the shading profundity entering full-screen or windowed mode setting the determination

Slide 20

Setting Graphics Mode A similar DirectX introduction is 20 to 30 lines of code. This capacity has the accompanying assertion: int set_gfx_mode(int card, int w, int h, int v_w, int v_h);

Slide 21

Setting Graphics Mode If a lapse happens setting a specific feature mode, set_gfx_mode will give back a non-zero quality (0 is achievement) and store a slip message in allegro_error , which you can then print out. For a case, have a go at utilizing an invalid determination for a full-screen showcase, similar to this: ret = set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 645, 485, 0, 0);

Slide 22

Setting Graphics Mode However, in the event that you indicate GFX_AUTODETECT and send an invalid width and stature to set_gfx_mode , it will really keep running in a window with the determination you needed. Running in windowed mode is a smart thought when you are trying a diversion and you don\'t need it to bounce into and out of full-screen mode each time you run the project.

Slide 23

Setting Graphics Mode The first parameter, int card , indicates the presentation mode (or the feature card in a double card arrangement) and will as a rule be GFX_AUTODETECT . In the event that you need a full-screen show, you can utilize GFX_AUTODETECT_FULLSCREEN , while you can summon a windowed showcase utilizing GFX_AUTODETECT_WINDOWED .

Slide 24

Setting Graphics Mode The following two parameters, int w and int h , indicate the fancied determination, for example, 640x480, 800x600, or 1024x768. The last two parameters, int v_w and int v_h , indicate the virtual determination and are utilized to make a vast virtual screen for equipment looking over or page flipping.

Slide 25

Setting Graphics Mode After you have called set_gfx_mode to change the feature mode, Allegro populates the variables SCREEN_W , SCREEN_H , VIRTUAL_W , and VIRTUAL_H with the proper qualities, which prove to be useful when you lean toward not to hard-code the screen determination in your projects.

Slide 26

#include "allegro.h" void main(void) { allegro_init();/instate Allegro install_keyboard();/introduce the console/introduce feature mode to 640x480 int ret = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0); if (ret != 0) { allegro_message(allegro_error); return; } /presentation screen determination textprintf(screen, textual style, 0, 0, makecol(255, 255, 255), "%dx%d", SCREEN_W, SCREEN_H); while(!key[KEY_ESC]); allegro_exit(); } END_OF_MAIN();

Slide 27

textprintf Another intriguing capacity is textprintf , which, as you may have speculated, shows a message in any feature mode. The principal parameter indicates the destination, which can be the physical showcase screen or a memory bitmap. void textprintf(BITMAP *bmp, const FONT *f, int x, y, shading, const burn *fmt, ...);

Slide 28

Colors You may have saw a capacity called makecol . This capacity makes a RGB shading utilizing the part hues went to it. On the off chance that you need to characterize custom hues you can make your own particular hues like this: #define COLOR_BROWN makecol(174,123,0)

Slide 29

allegro_exit The last capacity that you ought to be mindful of is allegro_exit , which close down the design framework and annihilates the memory utilized by Allegro. In principle, the destructors will deal with expelling everything from memory, yet it’s a smart thought to call this capacity unequivocally. One essential motivation behind why is for the advantage of restoring the feature show. (Inability to call allegro_exit may leave the desktop in an adjusted determination or shading profundity relying upon the illustrations card being utilized.)

Slide 30

Drawing Bitmaps Use the load_bitmap capacity to stack a picture record (numerous organizations upheld). At that point utilize the blit capacity to draw the bitmap. I’ll go over bitmaps and sprites in more detail in the following lesson.

Slide 31

#include "allegro.h" void main(void) { roast *filename = "allegro.pcx"; int colordepth = 32; BITMAP *image; int ret; allegro_init(); install_keyboard(); set_color_depth(colordepth); ret = set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0); if (ret != 0) { allegro_message(allegro_error); return; } //load the picture record picture = load_bitmap(filename, NULL); if (!picture) { allegro_message("Error stacking %s", filename); return; }

Slide 32

/show the picture blit(image, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);/done drawing- - erase bitmap from memory destroy_bitmap(image);/draw textual style with straightforwardness text_mode(- 1);/show feature mode data textprintf(screen, textual style, 0, 0, makecol(255, 255, 255), "%dx%d %ibpp", SCREEN_W, SCREEN_H, colordepth);/sit tight for keypress while (!key[KEY_ESC]);/way out system allegro_exit(); } END_OF_MAIN();

Slide 33

Loading A

Recommended
View more...