Getting Started CRevisions and Introduction to Graphics Next






![What about here? for(int i=0; i < num. Of. Bombs; i++){ b[i]. activate(); b[i]. What about here? for(int i=0; i < num. Of. Bombs; i++){ b[i]. activate(); b[i].](https://slidetodoc.com/presentation_image_h2/a5647447c00090a3e97e4b7a63dcb5c8/image-7.jpg)















![Graphics Functions 159. 234 DISPLAYING TEXT char mx[80]; float N; Get mouse current x-position Graphics Functions 159. 234 DISPLAYING TEXT char mx[80]; float N; Get mouse current x-position](https://slidetodoc.com/presentation_image_h2/a5647447c00090a3e97e4b7a63dcb5c8/image-23.jpg)

















![Fillpoly Example 159. 234 void fillpoly (int numpoints, int *polypoints); int poly[8]; int maxx, Fillpoly Example 159. 234 void fillpoly (int numpoints, int *polypoints); int poly[8]; int maxx,](https://slidetodoc.com/presentation_image_h2/a5647447c00090a3e97e4b7a63dcb5c8/image-41.jpg)




- Slides: 45

Getting Started: C-Revisions and Introduction to Graphics • Next: Simulation Essentials, Memory Handling 1

159. 234 Elements of the Tank Game Compiling your program Simple Animation Double-buffering technique Topics for Discussion Keyboard handling Mouse handling Demo

Tank Game 3

159. 234 Graphics Engine Keyboard & mouse control Physics Engine Dynamic Memory handling Elements of the Game Objects: Tank, Alien, Bomb, Bullet, Wall, Ledge Transformation Equations, Zooming features Trigonometric Equations

Demo Why resort to writing codes using th Object-Oriented approach? • C: CoreMassey Papers159234Animation-2008 -v. 4. 0 • • C: CoreMassey Papers159234Bomb-v. 1. 0 C: CoreMassey Papers159234Bomb-v. 11. 0 C: CoreMassey Papers159234Tank. Game-2008 -v. 1. 0 C: CoreMassey Papers159234AssignmentsSamplesA 1 -06193242 SampleTank 2. 0 5

What can you notice here? if(clock() > detonation. Time){ for(int i=0; i < count; i++){ //Note: append Tank's x and y-components Wx = Xdev(WBound, DBound, x(obj[i]. t, obj[i]. v. O, obj[i]. theta) + Tank. X); Wy = Ydev(WBound, DBound, y(obj[i]. t, obj[i]. v. O, obj[i]. theta) + Tank. Y); setlinestyle(SOLID_LINE, 0, 1); fillellipse(Wx, Wy, Xdev(WBound, DBound, 3. 0) obj[i]. t=obj[i]. t + obj[i]. t. Inc; } t=t+tinc; } 6
![What about here forint i0 i num Of Bombs i bi activate bi What about here? for(int i=0; i < num. Of. Bombs; i++){ b[i]. activate(); b[i].](https://slidetodoc.com/presentation_image_h2/a5647447c00090a3e97e4b7a63dcb5c8/image-7.jpg)
What about here? for(int i=0; i < num. Of. Bombs; i++){ b[i]. activate(); b[i]. tick(tfm); b[i]. draw(tfm); } 7

C-Programming Revisions 159. 234 C-LANGUAGE STRUCTURE /* include statements */ #include <stdio. h> #include <string. h> /* define statements */ #define MAX 2000 /* function prototypes */ void subfunc(int argument); /* global variables */ int i, j; char c; float x, y; char s[80]; /* functions */ void subfunc(int argument) { int i; /* local variables */ statements. . . } /* main program */ int main() { statements. . . } 8

Compiling your Program MAKEFILE (for JFE) JFE 159. 234 Turn on all the warning messages possible My. Prog. exe : My. Prog. o graphics. o gcc -wl, -s -o My. Prog. exe My. Prog. o graphics. o My. Prog. o : My. Prog. cpp graphics. h gcc -c -fpermissive -fconserve-space My. Prog. cpp graphics. o : graphics. cpp graphics. h gcc -c -fpermissive -fconserve-space graphics. cpp Should start with a tab This is the minimum requirement for compilation. 9

Graphics Project 159. 234 WORKSPACE (JFE) MYPROG. CPP GRAPHICS. H MAKEFILE GAME. WSP 10

Compiling your Program 159. 234 MAKEFILE (for scite) scite My. Prog. exe : My. Prog. o graphics. o g++ -wl, -s -o My. Prog. exe My. Prog. o graphics. o My. Prog. o : My. Prog. cpp graphics. h g++ -c -fpermissive -fconserve-space My. Prog. cpp graphics. o : graphics. cpp graphics. h g++ -c -fpermissive -fconserve-space graphics. cpp This is the minimum requirement for compilation. 11

All must reside in the same folder Makefile graphics. cpp graphics. h My. Prog. cpp physics. h It’s better to put classes in separate files. The cpp file will include the function Implementations, while the header file (*. h) *. h will include the function prototypes and the class definition. 12

Incorporating more files. . . 159. 234 MAKEFILE (for scite) My. Prog. exe : My. Prog. o transform. o fuzzylogic. o physics. o bomb. o graphics. o g++ -Wl, -s -o My. Prog. exe My. Prog. o transform. o fuzzylogic. o physics. o bomb. o graphics. o My. Prog. o : My. Prog. cpp graphics. h transform. h fuzzylogic. h bomb. h game. Def. h g++ -c -fpermissive -fconserve-space My. Prog. cpp transform. o : transform. cpp transform. h g++ -c -fpermissive -fconserve-space transform. cpp fuzzylogic. o : fuzzylogic. cpp fuzzylogic. h g++ -c -fpermissive -fconserve-space fuzzylogic. cpp physics. o : physics. cpp physics. h g++ -c -fpermissive -fconserve-space physics. cpp bomb. o : bomb. cpp bomb. h g++ -c -fpermissive -fconserve-space bomb. cpp graphics. o : graphics. cpp graphics. h g++ -c -fpermissive -fconserve-space graphics. cpp 13

C-Programming Revisions 159. 234 INITIALIZING GRAPHICS #include <windows. h> #include <stdio. h> #include <math. h> #include <time. h> #include "graphics. h" User-defined, take note of the double quotes. int main(void) { srand(time(NULL)); // Seed the random number generator int Graph. Driver=0, Graph. Mode=0; initgraph(&Graph. Driver, &Graph. Mode, "", 1280, 1024 ); Tank. Game(); //start the game – this is user-defined function return 0; } 14

Simple Animation 159. 234 Single Page Animation (flickery!) void Single. Page() { int i; int x, y; cleardevice(); y=getmaxy()/2; while( (Get. Async. Key. State(VK_ESCAPE))==0 ) for(x=0; x<getmaxx(); x++) { if(Get. Async. Key. State(VK_ESCAPE) != 0) break; setactivepage(0); cleardevice(); setfillstyle(SOLID_FILL, RED); fillellipse(x, y, 12); rectangle(x, y+(getmaxy()/12), x+100, y+(getmaxy()/11)); settextjustify(CENTER_TEXT, CENTER_TEXT); settextstyle(TRIPLEX_FONT, HORIZ_DIR, 4); outtextxy(getmaxx()/2, getmaxy()/8, "PAGE 0"); } } Check if the ESC key has been pressed 15

Demo • C: CoreMassey Papers159234Tank. Game 2008 -v. 1. 0 • See Single. Page and double buffering animations 16

Simple Animation 159. 234 Double-buffering Animation (flicker-free!) void Multiple. Pages() { int i, x, y; bool Page. Flag=TRUE; setactivepage(1); cleardevice(); outtext("PAGE 1"); setvisualpage(1); y=getmaxy()/2; while( (Get. Async. Key. State(VK_ESCAPE))==0 ) for(x=0; x<getmaxx(); x++) { if(Get. Async. Key. State(VK_ESCAPE) != 0) break; if (Page. Flag) { setactivepage(0); cleardevice(); setfillstyle(SOLID_FILL, RED); fillellipse(x, y, 12); rectangle(x, y+(getmaxy()/12), x+100, y+(getmaxy()/11)); settextjustify(CENTER_TEXT, CENTER_TEXT); settextstyle(TRIPLEX_FONT, HORIZ_DIR, 4); outtextxy(getmaxx()/2, getmaxy()/8, "PAGE 0"); setvisualpage(0); } Continued. . . 17

C-Programming Revisions 159. 234 Double-buffering Animation (flicker-free!) void Multiple. Pages() {. . . while( (Get. Async. Key. State(VK_ESCAPE))==0 ) for(x=0; x<getmaxx(); x++) { if(Get. Async. Key. State(VK_ESCAPE) != 0) break; if (Page. Flag) {. . . } else { setactivepage(1); cleardevice(); setfillstyle(SOLID_FILL, RED); fillellipse(x, y, 12); rectangle(x, y+(getmaxy()/12), x+100, y+(getmaxy()/11)); settextjustify(CENTER_TEXT, CENTER_TEXT); settextstyle(TRIPLEX_FONT, HORIZ_DIR, 4); outtextxy(getmaxx()/2, getmaxy()/8, "PAGE 1"); setvisualpage(1); } if(mousedown()) { } Page. Flag=!Page. Flag; } } 18

Graphics Functions 159. 234 void cleardevice (void); cleardevice erases (that is, fills with the current background color) the entire graphics screen and moves the CP (current position) to home (0, 0). 0 +x e. g. 1280 x 1024 pixels (XDevice, YDevice) +y Device System of Coordinates 19

Maximum Boundaries 159. 234 int getmaxx (void); int getmaxy (void); outtextxy(getmaxx()/2, getmaxy()/2, “Graphics"); (0, 0) Graphics (getmaxx(), getmaxy()) 20

Graphics Functions 159. 234 DISPLAYING TEXT See graphics. h for more options setcolor(YELLOW); settextstyle(DEFAULT_FONT, HORIZ_DIR, 3); settextjustify(CENTER_TEXT, CENTER_TEXT); outtextxy(200, 300, “Graphics"); 21

Demo • C: CoreMassey Papers159234Tank. Game 2008 -v. 1. 0 • See Graphics. Demo() 22
![Graphics Functions 159 234 DISPLAYING TEXT char mx80 float N Get mouse current xposition Graphics Functions 159. 234 DISPLAYING TEXT char mx[80]; float N; Get mouse current x-position](https://slidetodoc.com/presentation_image_h2/a5647447c00090a3e97e4b7a63dcb5c8/image-23.jpg)
Graphics Functions 159. 234 DISPLAYING TEXT char mx[80]; float N; Get mouse current x-position sprintf(mx, "%d", mousecurrentx()); moveto(105, 224); outtext(mx); N=4. 5; sprintf(mx, "%3. 2 f", N); outtextxy(100, 250, mx); 23

Graphics Functions 159. 234 DISPLAYING TEXT void settextstyle (int font, int direction, int charsize); Name Value Description DEFAULT_FONT 0 8 x 8 bit-mapped font TRIPLEX_FONT 1 Stroked triplex font SMALL_FONT 2 Stroked small font SANS_SERIF_FONT 3 Stroked sans-serif font GOTHIC_FONT 4 Stroked gothic font SCRIPT_FONT 5 Stroked script font SIMPLEX_FONT 6 Stroked triplex script font TRIPLEX_SCR_FONT 7 Stroked triplex script font COMPLEX_FONT 8 Stroked complex font EUROPEAN_FONT 9 Stroked European font BOLD_FONT 10 Stroked bold font 24

Graphics Functions 159. 234 DISPLAYING TEXT void settextjustify (int horiz, int vert); Description Name Value Action horiz LEFT_TEXT 0 left-justify text CENTER_TEXT 1 center text RIGHT_TEXT 2 right-justify text BOTTOM_TEXT 0 bottom-justify text CENTER_TEXT 1 center text TOP_TEXT 2 top-justify text vertical 25

Text Height, Text Width 159. 234 int textheight (char *textstring); int textwidth (char *textstring); Use textheight to compute the height of strings, instead of doing the computations manually. By using this function, no source code modifications have to be made when different fonts are selected 26

Mouse Routines 159. 234 int mousecurrentx(); int mousecurrenty(); int whichmousebutton(); LEFT_BUTTON RIGHT_BUTTON bool mouseup(); bool mousedown(); void clearmouse(); 27

Introducing Delay 159. 234 void delay (int millisec); the length of time to sleep in milliseconds. delay(50); Sleep(100); 28

Setting the Color 159. 234 void setcolor (int color); • Sets the text, line, circle, rectangle, ellipse, arc colors • Affects outline color of all filled shapes setcolor(RED); setcolor(50); //From 0 to 64 29

Line Style 159. 234 void setlinestyle (int linestyle, unsigned upattern, int thickness); Name Value Description SOLID_LINE 0 Solid line DOTTED_LINE 1 Dotted line CENTER_LINE 2 Centered line DASHED_LINE 3 Dashed line USERBIT_LINE 4 User-defined line style thickness specifies whether the width of subsequent lines drawn will be normal or thick. Name Value Description NORM_WIDTH 1 1 pixel wide THICK_WIDTH 3 3 pixels wide 30

Rectangle 159. 234 void rectangle (int left, int top, int right, int bottom); 31

Fill Style 159. 234 void setfillstyle (int pattern, int color); Pattern: EMPTY_FILL, SOLID_FILL, LINE_FILL, LTSLASH_FILL, BKSLASH_FILL, LTBKSLASH_FILL, HATCH_FILL, XHATCH_FILL, INTERLEAVE_FILL, WIDE_DOT_FILL, CLOSE_DOT_FILL, USER_FILL • Affects filled-shapes 32

bar 159. 234 void bar (int left, int top, int right, int bottom); The upper left and lower right corners of the rectangle are given by (left, top) and (right, bottom), respectively. The coordinates refer to pixels. 33

Bar 3 D 159. 234 void bar 3 d (int left, int top, int right, int bottom, int depth, int topflag); bar 3 d draws a three-dimensional rectangular bar, then fills it using the current fill pattern and fill color. The three-dimensional outline of the bar is drawn in the current line style and color. The bar's depth in pixels is given by depth. The topflag parameter governs whether a three-dimensional top is put on the bar. If topflag is nonzero, a top is put on; otherwise, no top is put on the bar (making it possible to stack several bars on top of one another). The upper left and lower right corners of the rectangle are given by (left, top) and (right, bottom), respectively. To calculate a typical depth for bar 3 d, take 25% of the width of the bar, like this: bar 3 d(left, top, right, bottom, (right-left)/4, 1); 34

Circle 159. 234 void circle (int x, int y, int radius); 35

Ellipse 159. 234 void ellipse (int x, int y, int stangle, int endangle, int xradius, int yradius); void fillellipse (int x, int y, int xradius, int yradius); ellipse draws an elliptical arc in the current drawing color with its center at (x, y) and the horizontal and vertical axes given by xradius and yradius, respectively. The ellipse travels from stangle to endangle. If stangle equals 0 and endangle equals 360, the call to ellipse draws a complete ellipse. The angle for ellipse is reckoned counterclockwise, with 0 degrees at 3 o'clock, 90 degrees at 12 o'clock, and so on. The linestyle parameter does not affect arcs, circles, ellipses, or pie slices. Only the thickness parameter is used. 36

Putpixel 159. 234 void putpixel (int x, int y, int color); 37

Arc 159. 234 void arc (int x, int y, int stangle, int endangle, int radius); Angles in degrees 0 -360. 0 -right. Counter-clockwise (90 -up, 180 -left, 270 -down) The linestyle parameter does not affect arcs, circles, ellipses, or pie slices. Only the thickness parameter is used. 38

Pieslice 159. 234 void pieslice (int x, int y, int stangle, int endangle, int radius); Use with setcolor() and setfillstyle() functions 39

Fillpoly 159. 234 void fillpoly (int numpoints, int *polypoints); Array of integers fillpoly draws the outline of a polygon with numpoints in the current line style and color (just as drawpoly does), then fills the polygon using the current fill pattern and fill color. polypoints to a sequence of (numpoints * 2) integers. Each pair of integers gives the x- and y-coordinates of a point on the polygon. 40
![Fillpoly Example 159 234 void fillpoly int numpoints int polypoints int poly8 int maxx Fillpoly Example 159. 234 void fillpoly (int numpoints, int *polypoints); int poly[8]; int maxx,](https://slidetodoc.com/presentation_image_h2/a5647447c00090a3e97e4b7a63dcb5c8/image-41.jpg)
Fillpoly Example 159. 234 void fillpoly (int numpoints, int *polypoints); int poly[8]; int maxx, maxy; maxx = getmaxx(); maxy = getmaxy(); poly[0] = 20; /* first vertex */ poly[1] = maxy / 2; poly[2] = maxx - 20; /* second vertex */ poly[3] = 20; poly[4] = maxx - 50; /* third vertex */ poly[5] = maxy - 20; poly[6] = maxx / 2; /* fourth, vertex */ poly[7] = maxy / 2; /* automatically closes the polygon */ fillpoly(4, poly); 4 points, 8 array elements 41

Keyboard Handling 159. 234 Get. Async. Key. State The Get. Async. Key. State function determines whether a key is up or down at the time the function is called, and whether the key was pressed after a previous call to Get. Async. Key. State. SHORT Get. Async. Key. State( int v. Key ); // v. Key - virtual-key code Virtual-key code e. g. vk_shift vk_control To find other pre-defined constants: Using google, google type the following keywords: msdn vk_shift 42

Keyboard Handling 159. 234 Get. Async. Key. State void Move. Sprite() { if(Get. Async. Key. State(VK_UP) < 0) { Sprite. Y = Sprite. Y - 2; //up outtext("UP"); } if(Get. Async. Key. State(VK_DOWN) < 0) { Sprite. Y = Sprite. Y + 2; //down outtext("DOWN"); …. To find other pre-defined constants: Using google, type the following keywords: msdn virtual key codes http: //msdn. microsoft. com/en-us/library/ms 645540(VS. 85). aspx 43

Keyboard Handling Monitoring the Control and Shift keys: bool Control. Flag, Shift. Flag; if(Get. Async. Key. State(VK_CONTROL)<0) { Control. Flag =! Control. Flag; } if(Get. Async. Key. State(VK_SHIFT)<0) { Shift. Flag =! Shift. Flag; } For the Tank to Jump to the Right: Control + Shift + Right Arrow key For the Tank to Jump to the Left: Control + Shift + Left Arrow key 44

Keyboard Handling Possible approach in monitoring key combinations : if(Get. Async. Key. State(VK_RIGHT)<0) { XDir=RIGHT; if(Shift. Flag) { outtext("SHIFT + RIGHT"); Shift. Flag=!Shift. Flag; } if(Control. Flag) { outtext("CTRL + RIGHT"); if (Tank. X < getmaxx()-W) Tank. X += 2; Angle=Angle-5; Raise. Wheel. Flag=TRUE; Control. Flag=!Control. Flag; } … 45