550 likes | 730 Views
Chapter 2 Graphics Programming with C++ and the Dark GDK Library. Starting Out with Games & Graphics in C++ Tony Gaddis. 2.1 Getting Your Feet Wet with C++ and the Dark GDK Library. Concept:
E N D
Chapter 2Graphics Programming with C++ and the Dark GDK Library Starting Out with Games & Graphics in C++ Tony Gaddis
2.1 Getting Your Feet Wet with C++ and the Dark GDK Library Concept: All C++ programs that use the Dark GDK library start out with the same code. The first step in learning to write a graphics program is to learn how to start a Dark GDK program in C++.
2.1 Getting Your Feet Wet with C++ and the Dark GDK Library Minimum framework of a C++/Dark GDK program Figure 2-6 Summary of the skeleton program
2.1 Getting Your Feet Wet with C++ and the Dark GDK Library • Include directive – causes the contents of a file to be included in a program • DarkGDK.h contains the setup code needed for the Dark GDK library to work correctly with our C++ programs • We must include the DarkGDK.h file in every program that will use the Dark GDK library
2.1 Getting Your Feet Wet with C++ and the Dark GDK Library • Function – group of statements that collectively has a name • Function named DarkGDK is required by any program that uses the Dark GDK library • DarkGDK function contains the statements that will be executed when program runs
2.1 Getting Your Feet Wet with C++ and the Dark GDK Library • C++ is a case-sensitive language, which means it regards uppercase letters as being entirely different than their lowercase counterparts • The name of the function DarkGDK must be written with • uppercase D • lowercase ark • uppercase GDK • Pay attention to the case! • C++ does not see • darkgdk the same as DarkGDK • VOID the same as void
2.1 Getting Your Feet Wet with C++ and the Dark GDK Library • dbWaitKey– when this function is called, it causes the program to pause until a key is pressed on the keyboard Function Call – causes the statements in a function to execute
2.1 Getting Your Feet Wet with C++ and the Dark GDK Library Comments • Comments are: • Short notes explaining how parts of a program work • Not intended for the compiler • Intended for anyone who is reading the code
2.1 Getting Your Feet Wet with C++ and the Dark GDK Library Comments • Two types of comments: • Line comments • begin with two forward slashes • // used to comment a single line • Block comments • Begin with /* and end with */ • /* used to comment • multiple lines */
2.1 Getting Your Feet Wet with C++ and the Dark GDK Library Comments • An example of a program that contains line comments:
2.1 Getting Your Feet Wet with C++ and the Dark GDK Library Comments • An example of how block comments may be used:
2.1 Getting Your Feet Wet with C++ and the Dark GDK Library Programming Style: Making Your Code Easier to Read • Use blank lines and indentations to create a sense of visual organization • For example, one convention virtually all programmers follow is: • indenting statements inside a function • Following conventions, such as indenting statements inside a function, is known as programming style
2.2 The Screen Coordinate System Concept: A system of X and Y coordinates is used to identify the locations of pixels in a window.
2.2 The Screen Coordinate System The images that are displayed on a computer screen are made up of tiny dots called pixels The default Dark GDK window is 640 pixels wide and 480 pixels high It has a resolution of 640 by 480
2.2 The Screen Coordinate System Figure 2-3 The width and height of the default window
2.2 The Screen Coordinate System • A screen coordinate system is used to identify the position of each pixel in the window. • Each pixel has • X coordinate • Identifies the horizontal position And • Y coordinate • identifies the vertical position
2.2 The Screen Coordinate System • Coordinates are written in the form (X, Y) • For example: • Upper-left corner pixel coordinates are (0, 0) • X is 0 • Y is 0 • The X coordinates increase from left to right • The Y coordinates increase from top to bottom • This is different from the Cartesian coordinate system you learned about in mathematics • Coordinate numbering begins at 0 in the upper-left corner • Lower-right corner pixel coordinates are (639, 479) • X is 639 • Y is 479
2.2 The Screen Coordinate System Figure 2-4 various pixel locations in a 640 by 480 window
2.2 The Screen Coordinate System Drawing Dots with the dbDot Function dbDot(x, y); • The dbDot function draws a dot at a specific pixel location in the Dark GDK window. • Here is the general format of how you call the dbDot function:
2.2 The Screen Coordinate System Drawing Dots with the dbDot Function dbDot(319, 239); • In the general format, • the x argument is the X coordinate • the y argument is the Y coordinate • For example, the following statement draws a dot at the X coordinate 319 and the Y coordinate 239:
2.2 The Screen Coordinate System The values that you write inside the function’s parentheses are called arguments Arguments are pieces of data that you send to a function when you call it When an argument is sent to a function, you are passing the argument to the function
2.2 The Screen Coordinate System The dbWait Function dbWait(time); • The dbWait function causes the program to wait for a specified amount of time before continuing • Here is the general format of how you call the dbWait function:
2.2 The Screen Coordinate System The dbWait Function dbWait(1000); • The value that you provide for the time argument is the number of milliseconds you want the program to wait. • There are 1000 milliseconds in a second • The following statement will cause the program to wait for 1 second:
2.2 The Screen Coordinate System • Dark GDK function names start with the letters db • For example: • dbDot • dbWait • dbWaitKey • The Game Creators, the software company that created the Dark GDK library, have also created a programming language called Dark BASIC • Most Dark GDK functions are the C++ equivalents of Dark BASIC commands • For this reason, Dark GDK function names start with the letters db, meaning Dark BASIC
2.3 Basic 2D Shapes Concept: The Dark GDK library contains several functions for drawing basic 2D shapes.
2.3 Basic 2D Shapes Figure 2-6 A two-dimensional game character • Objects that appear in 2D have only two dimensions: • Width • Height
2.3 Basic 2D Shapes • The Dark GDK library provides several functions for drawing simple 2D shapes • Lines • Circles • Ellipses • Rectangles
2.3 Basic 2D Shapes Drawing Lines: The dbLine Function dbLine(x1, y1, x2, y2); • The dbLine function draws a line between two points in the Dark GDK window. • Here is the general format of how you call the dbLine function:
2.3 Basic 2D Shapes Drawing Lines: The dbLine Function • You pass four arguments to the dbLine function • X1 and Y1 are the coordinates for the starting point of the line • X2 and Y2 are the coordinates for the line’s ending point
2.3 Basic 2D Shapes Drawing Lines: The dbLine Function dbLine(80, 120, 400, 520); Figure 2-6 A line drawn from (80, 120) to (400, 520) For example, the following statement draws a line between the points (80, 120) and (400, 520):
2.3 Basic 2D Shapes Drawing Rectangles: The dbBox Function dbBox(x1, y1, x2, y2); • The dbBox function draws a filled rectangle • Filled means it is filled with color • Here is the general format of how you call the dbBox function:
2.3 Basic 2D Shapes Drawing Rectangles: The dbBox Function • You pass four arguments to the dbBox function • X1 and Y1 are the X and Y coordinates for the rectangle’s upper-left corner • X2 and Y2 are the X and Y coordinates for the rectangle’s lower-right corner
2.3 Basic 2D Shapes Drawing Rectangles: The dbBox Function dbBox(100, 80, 540, 380); Figure 2-12 A rectangle with corners at (100, 80) and (540, 380) For example, look at the following statement:
2.3 Basic 2D Shapes Drawing Circles: The dbCircle Function dbCircle(x, y, radius); • The dbCircle function draws a circle • Here is the general format of how you call the dbCircle function:
2.3 Basic 2D Shapes Drawing Circles: The dbCircle Function • The x and y arguments are the coordinates of the circle’s center point • The radius argument specifies the circle’s radius • The radius is the distance, in pixels, from the center point to the outer edge
2.3 Basic 2D Shapes Drawing Circles: The dbCircle Function dbCircle(320, 240, 100); Figure 2-16 A circle with its center at (320, 240) and a radius of 100 Here is an example:
2.3 Basic 2D Shapes Drawing Ellipses: The dbEllipse Function dbEllipse(x, y, xrad, yrad); • The dbEllipse function draws an ellipse • an ellipse is an oval shape • Here is the general format of how you call the dbEllipse function:
2.3 Basic 2D Shapes Drawing Ellipses: The dbEllipse Function • The x and y arguments are the coordinates of the ellipse’s center point • The xrad argument specifies the ellipse’s radius along the X axis • The yrad argument specifies the ellipse’s radius along the Y axis
2.3 Basic 2D Shapes Drawing Ellipses: The dbEllipse Function dbEllipse(320, 240, 140, 100); Figure 2-18 An ellipse’s center point, x-radius, and y-radius Here is an example:
2.3 Basic 2D Shapes Drawing Outside the Dark GDK Window • Any point that has • X coordinate from 0 through 639 • Y coordinate from 0 through 479 • Is visible in the Dark GDK window • You can use points that have coordinates outside these ranges but… • They are not visible in the window
2.3 Basic 2D Shapes Drawing Outside the Dark GDK Window dbDot(800, 600); • But because that location is outside the Dark GDK window, it will not be visible For example, the following statement draws a dot at the coordinates (800, 600)
2.3 Basic 2D Shapes Drawing Outside the Dark GDK Window Figure 2-20 Negative coordinates • Points that are above the top row of pixels in the window have a negative Y coordinate • Points that are to the left of the leftmost column of pixels have a negative X coordinate
2.3 Basic 2D Shapes Drawing Outside the Dark GDK Window dbCircle(-50, -20, 200); Figure 2-21 Circle drawn partially off-screen The following statement draws a circle with its center point located at (-50, -20) and with a radius of 200
2.4 Displaying Text Concept: You can use the dbPrint, dbText, or dbCenterText functions to display text in the Dark GDK window. You can use the dbSetWindowTitle function to display text in the window’s title bar.
2.4 Displaying Text Displaying Text Inside the Dark GDK Window • You can use these functions to display text in the Dark GDK Window • dbPrint • dbText • dbCenterText
2.4 Displaying Text Displaying Text Inside the Dark GDK Window dbPrint(string); • The dbPrint function displays a string of characters • String is a term used in programming to mean “string of characters” • Here is the general format of how you call the dbPrint function:
2.4 Displaying Text Displaying Text Inside the Dark GDK Window • The string argument is the string you want to display • The string is printed as a line of output in the Dark GDK window • If no argument is passed • a blank line will be displayed • The first call to the dbPrint function • output is printed at the top of the window, justified along the left side • Each subsequent call to the dbPrint function • prints a line of output below the previous line of output.
2.4 Displaying Text Displaying Text Inside the Dark GDK Window dbText(x, y, string); The dbText function displays a string of characters at a specific location in the window Here is the general format of how you call the dbText function:
2.4 Displaying Text Displaying Text Inside the Dark GDK Window • The x and y arguments are a set of coordinates • The string argument is the string that is to be displayed • When the string is displayed • The upper-left corner of the first character will be positioned at the X and Y coordinates
2.4 Displaying Text Displaying Text Inside the Dark GDK Window dbText(10, 10, “Hello World”); Figure 2-23 Results of the dbText function For example, the following statement displays the string “Hello World” in the window