Xin Liu Sep 16, 2013. Introduction Xin (Shane) Liu PhD Candidate in Computer Science Research Area:...

Post on 26-Dec-2015

212 views 0 download

Transcript of Xin Liu Sep 16, 2013. Introduction Xin (Shane) Liu PhD Candidate in Computer Science Research Area:...

CPSC 453 TutorialXin Liu

Sep 16, 2013

IntroductionXin (Shane) Liu

PhD Candidate in Computer ScienceResearch Area: Computer GraphicsTutorial Page:

pages.cpsc.ucalgary.ca/~liuxin/CPSC453Email: liuxin@ucalgary.ca

What do we need?Computer Graphics

C/C++

OpenGL/GLSL

GLUT

RulesLinux/Unix: √Windows: √Mac OS: ×

c/c++: √Java: × Java = ZEROGNU c/c++: √Visual c++: √Objective c: ×

Getting started with C ++C/ C ++

The most widely used programming language Operating systems Applications

Fast Good for real-time applications

Full control of your computer A better understanding of the hardware Can be “dangerous”

Professional Good for your future career

C vs. C++ vs. JavaJava is the son of C++

Java is a totally different language thoughC++ is the son of CC++ is a superset of C

You can compile pure C with a C++ compiler

Java is a good start for c/c++ programmingSimilar grammar

C++ vs Java – some differencesFunction C++ Java

Reference Yes Yes

Pointer Yes No

Garbage collection No (new, delete) Yes

Operator overloading Yes No

Class Yes Yes

Struct Yes No

Destructor Yes No

Hello worldEdit program

Compile

Run

#include “iostream.h”int main (){ cout << “Hello\n”;}

g++ hello.c –o hello----------- OR ------------g++ -c hellog++ hello.o hello

hello

Compile options-Wall

Generate many warnings about questionable looking code

g++ -Wall myprog.c –o myprog-O

Generate optimized codeg++ -O myprog.c –o myprog

-gGenerate code with symbolic info for debuggingg++ -g myprog.c myprog

Compiling multi source filesA program composed of several souce files,

“file1.c”, “file2.c”

g++ -file1.c file2.c –o myprog------------ OR ------------g++ -c file1.cg++ -c file2.cg++ file1.o file2.o –o myprog

Make fileThe “make program”

Compile all source files by a “make” commandTrack changes of source filesRecompile only affected files

according to the dependency of source filesRequires a makefile

to define the dependency

Writing a makefileFour basic types of statements

Comments any line beginning with a #

Macros defined by: name = data used by: $(name), “$(name)” is then replaced by “data”

Explicit rules defines the dependency take the form of

Example:

Implicit rules similar to explicit rules, except listed without commands. uses the suffixes on the files to determine what command to perform

targetfile: sourcefilescommands # there is a TAB before each

command

main: main.c List.h # to create main, these files must exist

gcc –o main main.m List.h # the command to create main

Using IDEFunctions of an IDE (an Integrated Development

Environment)Edit programGenerating a makefile automaticallyCompile programDebug programRun program

Popular IDEsEclipse on Linux - suggestedVisual Studio on Windows - suggestedXcode on Mac OS - no OpenGL 3.3

Example – MyColor.h#include <stdio.h>

class CMyColor {public:

double r;double g;double b;

CMyColor (double argR = 0.0, double argG = 0.0, double argB = 0.0);void print();

};

CMyColor operator + (const CMyColor& a, const CMyColor& b);CMyColor operator - (const CMyColor& a, const CMyColor& b);CMyColor operator * (const CMyColor& a, const CMyColor& b);CMyColor operator * (const CMyColor& a, const double& k);

Example – MyColor.cpp#include "stdafx.h"#include "MyColor.h"

CMyColor:: CMyColor (double argR, double argG, double argB){

r = argR;g = argG;b = argB;

}

void CMyColor:: print(){

printf("[%f, %f, %f]\n", r, g, b);}

CMyColor operator + (const CMyColor& a, const CMyColor& b){

return CMyColor(a.r + b.r, a.g + b.g, a.b + b.b);}

CMyColor operator - (const CMyColor& a, const CMyColor& b){

return CMyColor(a.r - b.r, a.g - b.g, a.b - b.b);}

CMyColor operator * (const CMyColor& a, const CMyColor& b){

return CMyColor(a.r * b.r, a.g * b.g, a.b * b.b);}

CMyColor operator * (const CMyColor& a, const double& k){

return CMyColor(k * a.r, k * a.g, k * a.b);}

Example – Colors.cpp#include "MyColor.h"

int main(int argc, char* argv[]){

CMyColor clr;clr.print();

CMyColor *pClr = 0;pClr = new CMyColor(1.0, 1.0, 1.0);pClr->print();

CMyColor clr2 = (clr + (*pClr)) * 2.0;clr2.print();

delete pClr;return 0;

}

The End