Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 2 Graphics Programming with C++ and the Dark GDK Library Starting.

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 2 Getting Started with Java.
Advertisements

Use Tables for Layout Control Day 7. You will learn to: Understand Tables Create a Simple Table Modify Your Tables Appearance Create Page Layouts with.
Graphics Shapes. Setup for using graphics You have to import the graphics library You can use either “import graphics” or “from graphics import *” or.
BGI graphics library And Visual Studio.
PHY-102 SAPIntroductory GraphicsSlide 1 Introductory Graphics In this section we will learn how about how to draw graphics on the screen in Java:  Drawing.
Python Programming, 2/e1 CS177: Programming in Multimedia Objects Recitation Topic: Graphics Library.
Chapter 2 First Java Programs
Creating a Document with a Table, Chart, and Watermark
IT151: Introduction to Programming
Chapter 1_2 Becoming Skilled at Information Technology.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
Creating a Web Page with Tables. Objectives Create a text table with preformatted text Create the basic structure of a graphical table Organize table.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition by Tony Gaddis, Judy Walters,
1 Python Programming: An Introduction to Computer Science Chapter 3 Objects and Graphics.
IBA First Semester Exam Review. Microsoft Office 2010 Basics and the Internet 1.The tabs on the Ribbon organize the commands into related tasks. The commands.
Copyright © 2012 Pearson Education, Inc. Chapter 2 Introduction to Visual C#
Games and Simulations O-O Programming in Java The Walker School
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter One An Introduction to Visual Basic 2010.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 3 Variables, Calculations, and Colors Starting Out with Games.
Review Blocks of code {.. A bunch of ‘statements’; } Structured programming Learning Processing: Slides by Don Smith 1.
Invitation to Computer Science 5th Edition
Topics Introduction Hardware and Software How Computers Store Data
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 1 Introduction to Computer Science.
Key Applications Module Lesson 16 — Excel Essentials Computer Literacy BASICS.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Microsoft Office 2007 Excel Graphics Feature SmartArt and Images.
IC 3 BASICS, Internet and Computing Core Certification Key Applications Lesson 10 Creating and Formatting an Excel Worksheet.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Functions.
C++ Basics Structure of a Program. C++ Source Code Plain text file Typical file extension .CPP Must compile the C++ source code without errors before.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 5 Working with Images Starting Out with Games & Graphics in.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 3 Simple.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming First Edition.
Input, Output, and Processing
XP New Perspectives on Microsoft Word 2002 Tutorial 31 Microsoft Word 2002 Tutorial 3 – Creating a Multiple-Page Report.
Designing a Web Page with Tables. A text table: contains only text, evenly spaced on the Web page in rows and columns uses only standard word processing.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 7 The Game Loop and Animation Starting Out with Games & Graphics.
Programming for Artists ART 315 Dr. J. R. Parker Art/Digital Media Lab Lec 10 Fall 2010.
Tables Sacramento City College Engineering Design Technology.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Graphic Basics in C ATS 315. The Graphics Window Will look something like this.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Visual Basic Programming Introduction VB is one of the High level language VB has evolved from the BASIC language. BASIC stands for Beginners All-purpose.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
CSC 1010 Programming for All Lecture 7 Input, Output & Graphics.
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 12 Object-Oriented Programming Starting Out with Games & Graphics.
FUNCTIONS. Topics Introduction to Functions Defining and Calling a Void Function Designing a Program to Use Functions Local Variables Passing Arguments.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Creating a Java Application and Applet
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter One An Introduction to Visual Basic 2008.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley STARTING OUT WITH Python Python First Edition by Tony Gaddis Chapter 2 Input,
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Starting Out with Games & Graphics in C++ Tony Gaddis Chapter 8 The.
A lesson approach © 2011 The McGraw-Hill Companies, Inc. All rights reserved. a lesson approach Microsoft® Word 2010 © 2011 The McGraw-Hill Companies,
Chapter 3 Introducing Java. Objectives and Goals 1. Define terminology associated with object- oriented programming. 2. Explain why Java is a widely used.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
MS WORD INFORMATION TECHNOLOGY MANAGEMENT SERVICE Training & Research Division.
Formatting and Editing Skills Keyboarding Objective 4.01 – Apply formatting and editing features and operational keys appropriately.
Topics Designing a Program Input, Processing, and Output
Chapter 3 Syntax, Errors, and Debugging
Chapter 2 First Java Programs
Chapter 5 Working with Images
Chapter 3 Variables, Calculations, and Colors
Chapter 4 void Functions
Chapter 2 Graphics Programming with C++ and the Dark GDK Library
Topics Introduction to Functions Defining and Calling a Function
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 7 The Game Loop and Animation
Presentation transcript:

Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 2 Graphics Programming with C++ and the Dark GDK Library Starting Out with Games & Graphics in C++ Tony Gaddis

Copyright © 2010 Pearson Addison-Wesley 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++.

Copyright © 2010 Pearson Addison-Wesley 2.1 Getting Your Feet Wet with C++ and the Dark GDK Library 1-3 Minimum framework of a C++/Dark GDK program Figure 2-6 Summary of the skeleton program

Copyright © 2010 Pearson Addison-Wesley 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 1-4

Copyright © 2010 Pearson Addison-Wesley 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 1-5

Copyright © 2010 Pearson Addison-Wesley 2.1 Getting Your Feet Wet with C++ and the Dark GDK Library 1-6 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

Copyright © 2010 Pearson Addison-Wesley 2.1 Getting Your Feet Wet with C++ and the Dark GDK Library 1-7 Function Call – causes the statements in a function to execute dbWaitKey – when this function is called, it causes the program to pause until a key is pressed on the keyboard

Copyright © 2010 Pearson Addison-Wesley 2.1 Getting Your Feet Wet with C++ and the Dark GDK Library Comments are: –Short notes explaining how parts of a program work –Not intended for the compiler –Intended for anyone who is reading the code 1-8 Comments

Copyright © 2010 Pearson Addison-Wesley 2.1 Getting Your Feet Wet with C++ and the Dark GDK Library 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 */ 1-9 Comments

Copyright © 2010 Pearson Addison-Wesley 2.1 Getting Your Feet Wet with C++ and the Dark GDK Library 1-10 Comments An example of a program that contains line comments:

Copyright © 2010 Pearson Addison-Wesley 2.1 Getting Your Feet Wet with C++ and the Dark GDK Library 1-11 Comments An example of how block comments may be used:

Copyright © 2010 Pearson Addison-Wesley 2.1 Getting Your Feet Wet with C++ and the Dark GDK Library 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 1-12 Programming Style: Making Your Code Easier to Read

Copyright © 2010 Pearson Addison-Wesley The Screen Coordinate System Concept: A system of X and Y coordinates is used to identify the locations of pixels in a window.

Copyright © 2010 Pearson Addison-Wesley 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

Copyright © 2010 Pearson Addison-Wesley 2.2 The Screen Coordinate System 1-15 Figure 2-3 The width and height of the default window

Copyright © 2010 Pearson Addison-Wesley 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 1-16

Copyright © 2010 Pearson Addison-Wesley 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

Copyright © 2010 Pearson Addison-Wesley 2.2 The Screen Coordinate System 1-18 Figure 2-4 various pixel locations in a 640 by 480 window

Copyright © 2010 Pearson Addison-Wesley 2.2 The Screen Coordinate System 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: 1-19 dbDot(x, y); Drawing Dots with the dbDot Function

Copyright © 2010 Pearson Addison-Wesley 2.2 The Screen Coordinate System 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: 1-20 Drawing Dots with the dbDot Function dbDot(319, 239);

Copyright © 2010 Pearson Addison-Wesley 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 1-21

Copyright © 2010 Pearson Addison-Wesley 2.2 The Screen Coordinate System 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: 1-22 dbWait(time); The dbWait Function

Copyright © 2010 Pearson Addison-Wesley 2.2 The Screen Coordinate System 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: 1-23 dbWait(1000); The dbWait Function

Copyright © 2010 Pearson Addison-Wesley 2.2 The Screen Coordinate System 1-24 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

Copyright © 2010 Pearson Addison-Wesley 2.3 Basic 2D Shapes 1-25 Concept: The Dark GDK library contains several functions for drawing basic 2D shapes.

Copyright © 2010 Pearson Addison-Wesley 2.3 Basic 2D Shapes Objects that appear in 2D have only two dimensions: –Width –Height 1-26 Figure 2-6 A two- dimensional game character

Copyright © 2010 Pearson Addison-Wesley 2.3 Basic 2D Shapes The Dark GDK library provides several functions for drawing simple 2D shapes –Lines –Circles –Ellipses –Rectangles 1-27

Copyright © 2010 Pearson Addison-Wesley 2.3 Basic 2D Shapes 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: 1-28 dbLine(x1, y1, x2, y2); Drawing Lines: The dbLine Function

Copyright © 2010 Pearson Addison-Wesley 2.3 Basic 2D Shapes 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 1-29 Drawing Lines: The dbLine Function

Copyright © 2010 Pearson Addison-Wesley 2.3 Basic 2D Shapes For example, the following statement draws a line between the points (80, 120) and (400, 520): 1-30 Drawing Lines: The dbLine Function dbLine(80, 120, 400, 520); Figure 2-6 A line drawn from (80, 120) to (400, 520)

Copyright © 2010 Pearson Addison-Wesley 2.3 Basic 2D Shapes 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: 1-31 Drawing Rectangles: The dbBox Function dbBox(x1, y1, x2, y2);

Copyright © 2010 Pearson Addison-Wesley 2.3 Basic 2D Shapes 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 1-32 Drawing Rectangles: The dbBox Function

Copyright © 2010 Pearson Addison-Wesley 2.3 Basic 2D Shapes For example, look at the following statement: 1-33 Drawing Rectangles: The dbBox Function dbBox(100, 80, 540, 380); Figure 2-12 A rectangle with corners at (100, 80) and (540, 380)

Copyright © 2010 Pearson Addison-Wesley 2.3 Basic 2D Shapes The dbCircle function draws a circle Here is the general format of how you call the dbCircle function: 1-34 Drawing Circles: The dbCircle Function dbCircle(x, y, radius);

Copyright © 2010 Pearson Addison-Wesley 2.3 Basic 2D Shapes 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 1-35 Drawing Circles: The dbCircle Function

Copyright © 2010 Pearson Addison-Wesley 2.3 Basic 2D Shapes Here is an example: 1-36 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

Copyright © 2010 Pearson Addison-Wesley 2.3 Basic 2D Shapes The dbEllipse function draws an ellipse –an ellipse is an oval shape Here is the general format of how you call the dbEllipse function: 1-37 Drawing Ellipses: The dbEllipse Function dbEllipse(x, y, xrad, yrad);

Copyright © 2010 Pearson Addison-Wesley 2.3 Basic 2D Shapes 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 1-38 Drawing Ellipses: The dbEllipse Function

Copyright © 2010 Pearson Addison-Wesley 2.3 Basic 2D Shapes Here is an example: 1-39 Drawing Ellipses: The dbEllipse Function Figure 2-18 An ellipse’s center point, x-radius, and y-radius dbEllipse(320, 240, 140, 100);

Copyright © 2010 Pearson Addison-Wesley 2.3 Basic 2D Shapes 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 1-40 Drawing Outside the Dark GDK Window

Copyright © 2010 Pearson Addison-Wesley 2.3 Basic 2D Shapes For example, the following statement draws a dot at the coordinates (800, 600) 1-41 Drawing Outside the Dark GDK Window dbDot(800, 600); But because that location is outside the Dark GDK window, it will not be visible

Copyright © 2010 Pearson Addison-Wesley 2.3 Basic 2D Shapes 1-42 Drawing Outside the Dark GDK Window 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 Figure 2-20 Negative coordinates

Copyright © 2010 Pearson Addison-Wesley 2.3 Basic 2D Shapes The following statement draws a circle with its center point located at (-50, -20) and with a radius of Drawing Outside the Dark GDK Window dbCircle(-50, -20, 200); Figure 2-21 Circle drawn partially off- screen

Copyright © 2010 Pearson Addison-Wesley 2.4 Displaying Text 1-44 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.

Copyright © 2010 Pearson Addison-Wesley 2.4 Displaying Text You can use these functions to display text in the Dark GDK Window –dbPrint –dbText –dbCenterText 1-45 Displaying Text Inside the Dark GDK Window

Copyright © 2010 Pearson Addison-Wesley 2.4 Displaying Text 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: 1-46 Displaying Text Inside the Dark GDK Window dbPrint(string);

Copyright © 2010 Pearson Addison-Wesley 2.4 Displaying Text 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 Displaying Text Inside the Dark GDK Window

Copyright © 2010 Pearson Addison-Wesley 2.4 Displaying Text 1-48 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: Displaying Text Inside the Dark GDK Window dbText(x, y, string);

Copyright © 2010 Pearson Addison-Wesley 2.4 Displaying Text 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 1-49 Displaying Text Inside the Dark GDK Window

Copyright © 2010 Pearson Addison-Wesley 2.4 Displaying Text For example, the following statement displays the string “Hello World” in the window 1-50 Displaying Text Inside the Dark GDK Window dbText(10, 10, “Hello World”); Figure 2-23 Results of the dbText function

Copyright © 2010 Pearson Addison-Wesley 2.4 Displaying Text Text can be centered horizontally with the dbCenterText Function Here is an example: 1-51 Displaying Text Inside the Dark GDK Window dbCenterText(319, 239, “Game Over”); Figure 2-25 A string centered just below the point at (319, 239)

Copyright © 2010 Pearson Addison-Wesley 2.4 Displaying Text The dbSetWindowTitle function displays text in the window’s title bar Here is the general format of how you call the dbSetWindowTitle function: 1-52 Displaying Text in the Window’s Title Bar dbSetWindowTitle(string);

Copyright © 2010 Pearson Addison-Wesley 2.5 The Program Development Cycle 1-53 Concept: When creating programs, programmers typically follow a process known as the program development cycle.

Copyright © 2010 Pearson Addison-Wesley 2.5 The Program Development Cycle Design the Program Write the Code Correct Syntax Errors Test the Program Correct Logic Errors Repeat until error free 1-54 Figure 2-37 The program development cycle

Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Chapter 2 Graphics Programming with C++ and the Dark GDK Library QUESTIONS ?