3 Multimedia Programming with C++ and Multimedia Frameworks



Similar documents
3 Multimedia Programming with C++ and Multimedia Frameworks

6 Images, Vector Graphics, and Scenes

CSC230 Getting Starting in C. Tyler Bletsch

Comp151. Definitions & Declarations

URI and UUID. Identifying things on the Web.

2! Multimedia Programming with! Python and SDL

The C Programming Language course syllabus associate level

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

C++ INTERVIEW QUESTIONS

Visual Studio 2008 Express Editions

C++ Overloading, Constructors, Assignment operator

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

An Incomplete C++ Primer. University of Wyoming MA 5310

6.088 Intro to C/C++ Day 4: Object-oriented programming in C++ Eunsuk Kang and Jean Yang

Copyright 2001, Bill Trudell. Permission is granted to copy for the PLoP 2001 conference. All other rights reserved.

CORBA Programming with TAOX11. The C++11 CORBA Implementation

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Sources: On the Web: Slides will be available on:

Lecture 1 Introduction to Android

Keil C51 Cross Compiler

Virtuozzo Virtualization SDK

Moving from CS 61A Scheme to CS 61B Java

1 Abstract Data Types Information Hiding

Informatica e Sistemi in Tempo Reale

Information Technology Career Field Pathways and Course Structure

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

Crash Course in Java

Course MS10975A Introduction to Programming. Length: 5 Days

Programming Languages

C++ Crash Kurs. C++ Object-Oriented Programming

Surface and Volumetric Data Rendering and Visualisation

How to: Use Basic C++, Syntax and Operators

Abstractions from Multimedia Hardware. Libraries. Abstraction Levels

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

C++ for Safety-Critical Systems. DI Günter Obiltschnig Applied Informatics Software Engineering GmbH

Konzepte objektorientierter Programmierung

Fundamentals of Computer Science (FCPS) CTY Course Syllabus

C++ Programming Language

Coding conventions and C++-style

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

Illustration 1: Diagram of program function and data flow

Drawing Text with SDL

Lecture 22: C Programming 4 Embedded Systems

CSI33 Data Structures

Channel Access Client Programming. Andrew Johnson Computer Scientist, AES-SSG

C# and Other Languages

Outline. 1.! Development Platforms for Multimedia Programming!

iphone Objective-C Exercises

COS 217: Introduction to Programming Systems

CURRICULUM VITAE EDUCATION:

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

ELEC 377. Operating Systems. Week 1 Class 3

Practical Data Visualization and Virtual Reality. Virtual Reality VR Software and Programming. Karljohan Lundin Palmerius

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

The Java Virtual Machine and Mobile Devices. John Buford, Ph.D. Oct 2003 Presented to Gordon College CS 311

Java in Education. Choosing appropriate tool for creating multimedia is the first step in multimedia design

Bachelor of Games and Virtual Worlds (Programming) Subject and Course Summaries

Department of Computer Science

Chapter One Introduction to Programming

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Note: Syntactically, a ; is needed at the end of a struct definition.

ANIMATION a system for animation scene and contents creation, retrieval and display

1 bool operator==(complex a, Complex b) { 2 return a.real()==b.real() 3 && a.imag()==b.imag(); 4 } 1 bool Complex::operator==(Complex b) {

Eastern Washington University Department of Computer Science. Questionnaire for Prospective Masters in Computer Science Students

C++ program structure Variable Declarations

Open Source building blocks for the Internet of Things. Benjamin Cabé JFokus 2013

Ubuntu. Ubuntu. C++ Overview. Ubuntu. History of C++ Major Features of C++

1 Introduction: Network Applications

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Chapter 2 System Structures

Semester Review. CSC 301, Fall 2015

Computer Programming C++ Classes and Objects 15 th Lecture

Introducing PgOpenCL A New PostgreSQL Procedural Language Unlocking the Power of the GPU! By Tim Child

6.S096 Lecture 1 Introduction to C

Chapter 3 Operating-System Structures

sqlpp11 - An SQL Library Worthy of Modern C++

Operating Systems. Privileged Instructions

Programing the Microprocessor in C Microprocessor System Design and Interfacing ECE 362

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

Reading and Writing PCD Files The PCD File Format The Grabber Interface Writing a Custom Grabber PCL :: I/O. Suat Gedikli, Nico Blodow

To Java SE 8, and Beyond (Plan B)

Learning Computer Programming using e-learning as a tool. A Thesis. Submitted to the Department of Computer Science and Engineering.

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

INTERNET PROGRAMMING AND DEVELOPMENT AEC LEA.BN Course Descriptions & Outcome Competency

Network Programming. Writing network and internet applications.

Object Oriented Software Design II

System Structures. Services Interface Structure

Apache Thrift and Ruby

Transcription:

3 Multimedia Programming with C++ and Multimedia Frameworks 3.1 Multimedia Support by Languages and Frameworks 3.2 Introduction to C++ 3.3 SFML: Low-Level Multimedia/Game Framework for C++ 3.4 Cocos2d-x: High Level Game Engine for C++ LMU München, Sommer 2016 Prof. Hußmann: Multimedia-Programmierung Kapitel 3 Teil a, Folie 1

Multimedia Support is Platform Dependent Example: 2D graphics on desktop/laptop computers Multimedia Application Direct X XLib Quartz System-level graphics software Windows Linux OS X Operating system Hardware Computer/mobile device Graphics card LMU München, Sommer 2016 Prof. Hußmann: Multimedia-Programmierung Kapitel 3 Teil a, Folie 2

Multimedia Abstraction Layer Multimedia Application Multimedia Abstraction Layer Direct X XLib Quartz Examples: SDL (Pygame), SFML System-level graphics software Windows Linux OS X Operating system Hardware Computer/mobile device Graphics card LMU München, Sommer 2016 Prof. Hußmann: Multimedia-Programmierung Kapitel 3 Teil a, Folie 3

Core Functionality of a Multimedia Abstraction Layer Graphics Surfaces Vector Drawing Bitmap Images Sound Recording and playing Mixing Sound files and streams Moving Images Video playback Moving 2D images (sprites) Translations, transformations Input/Output and Events External devices Event queuing Network Sockets Protocol support (FTP, HTTP) Time Clock Timers LMU München, Sommer 2016 Prof. Hußmann: Multimedia-Programmierung Kapitel 3 Teil a, Folie 4

Additional Functionality for Advanced Multimedia Applications (Games) Object Structure Layers, scenes Scene graphs Control Structure Activity scheduling Time containers Animation Animated images Sprite sheet support Effects Interpolators Complex transitions Physics Simulation Solid-body physics Particle effects Artificial Intelligence Game logic Adaptation LMU München, Sommer 2016 Prof. Hußmann: Multimedia-Programmierung Kapitel 3 Teil a, Folie 5

High-Level Multimedia Framework Multimedia Application Advanced Functionality High-level multimedia framework Built-in Abstraction Layer Examples: Cocos2d-x, System Libraries OS Hardware LMU München, Sommer 2016 Prof. Hußmann: Multimedia-Programmierung Kapitel 3 Teil a, Folie 6

Terminology Low-Level Multimedia Framework = Multimedia Abstraction Layer Platform independent media handling High-Level Multimedia Framework = Game Engine Advanced multimedia features Specific support for game functionalities In most cases includes low-level multimedia framework LMU München, Sommer 2016 Prof. Hußmann: Multimedia-Programmierung Kapitel 3 Teil a, Folie 7

Pseudocode! Program Control and Frameworks main: <all preparation work> while running do: <process input> <update model> <render model> Traditional program control We write the main program Main control loop is part of our code We call functions of the framework Typical for low-level frameworks def init(): <specific preparations> def update(): <specific updates> schedule(update) Inversion of program control Main program is in the framework Main control loop does not appear in our code The framework calls our functions "Hollywood Principle" Typical for high-level frameworks LMU München, Sommer 2016 Prof. Hußmann: Multimedia-Programmierung Kapitel 3 Teil a, Folie 8

Implementation Language: Criteria Handling of multimedia objects Built into the language? Available in standard library? (e.g. Yes for Java 8) Portability to target platforms Critical in case of multiple platforms, mobile platforms etc. Performance: Memory efficiency Performance: Fast execution Performance: Access to hardware accelerations Performance LMU München, Sommer 2016 Prof. Hußmann: Multimedia-Programmierung Kapitel 3 Teil a, Folie 9

Python, Java, C++ Python Java C++ Ease of use easy to learn and use medium complexity rather demanding Multimedia support not standard available through frameworks (Pygame) Java 8: well-supported through built-in framework (JFX) not standard available through frameworks (SDL, SFML, Cocos2d, ) Multi-platform basically given limitations regarding multimedia frameworks very limited for mobile devices (being improved by e.g. kivy.org) basically OK single-source multiplatform limited e.g. for ios well supported through frameworks (e.g. Cocos2d-x) Performance limited limited very good (memory, native, hardware acceleration) LMU München, Sommer 2016 Prof. Hußmann: Multimedia-Programmierung Kapitel 3 Teil a, Folie10

3 Multimedia Programming with C++ and Multimedia Frameworks 3.1 Multimedia Support by Languages and Frameworks 3.2 Introduction to C++ 3.3 SFML: Low-Level Multimedia/Game Framework for C++ 3.4 Cocos2d-x: High Level Game Engine for C++ Literature: http://www.cprogramming.com/java/ c-and-c++-for-java-programmers.html B. Stroustrup: The C++ programming language, 4th ed., Addison-Wesley 2013 LMU München, Sommer 2016 Prof. Hußmann: Multimedia-Programmierung Kapitel 3 Teil a, Folie 11

C++: History C: Dennis Ritchie, Bell Labs, 1969-1973, an improvement of B, which was a simplified version of BCPL Bjarne Stroustrup, Bell Labs, 1979: Extension of the C programming language for large-scale programming (16 years before Java!) ISO standard since 1998 (ISO/IEC 14882) Important late revisions and extensions, in particular C++11 (2011) C++17 in preparation Latest version: C++14 (ISO standard 14882:2014) Main features of the language: Object-oriented programming with multiple inheritance Flexible storage allocation models Templates for generic programming Lambda expressions Exception handling Powerful standard library LMU München, Sommer 2016 Prof. Hußmann: Multimedia-Programmierung Kapitel 3 Teil a, Folie12

Differences Java C Type Languages Good news first: Syntax is very similar! Native compilation (C++) vs. compilation to VM (Java) Not everything has to be a class in C / C++. Preprocessor instead of import #include <string> Much more flexible (and dangerous) concept of pointers (and references): Pointers may refer to any storage area, in particular to memory on the stack! Objects can be created in C++ on the stack (static memory), not only on the heap (dynamic memory). Allocated memory on the heap (new) does not have automatic garbage collection. Memory has to be freed explicitly by "delete". LMU München, Sommer 2016 Prof. Hußmann: Multimedia-Programmierung Kapitel 3 Teil a, Folie13

Pointers and References in C++ Address-of Operator (&) int myvar = 25; &myvar is the memory address of myvar (on the stack) Pointer type declaration: int* ptr = &myvar; Printing ptr shows a memory address (e.g. 0x7fff5fbff698) Dereference Operator (*) *ptr is the value at the address to which ptr currently points Printing *ptr gives the value of myvar References (rarely used): int& ref = myvar; ref is like an alias of myvar and cannot be re-assigned LMU München, Sommer 2016 Prof. Hußmann: Multimedia-Programmierung Kapitel 3 Teil a, Folie14

Multiple Files, Header Files Using an additional cpp file: Create additional header file: Counter.cpp and Counter.hpp Header file: Contains declarations, is used to refer to the contents of the additional file In Counter.cpp and wherever Counter.cpp is used (e.g. in Main.cpp): #include "Counter.hpp" Macros needed to avoid multiple declarations: #ifndef Counter_hpp #define Counter_hpp // Declarations go here #endif /* Counter_hpp */ LMU München, Sommer 2016 Prof. Hußmann: Multimedia-Programmierung Kapitel 3 Teil a, Folie15

Counter Class in C++, Header File #ifndef Counter_hpp #define Counter_hpp class Counter { int k; public: Counter(); void count(); int getvalue(); ; #endif /* Counter_hpp */ Note: Using one pair of files for a class is just a recommendation not required by C++ Counter.hpp LMU München, Sommer 2016 Prof. Hußmann: Multimedia-Programmierung Kapitel 3 Teil a, Folie16

Counter Class in C++, Body File #include "Counter.hpp" Counter::Counter() { k = 0; void Counter::count() { k++; int Counter::getValue() { return k; Counter.cpp LMU München, Sommer 2016 Prof. Hußmann: Multimedia-Programmierung Kapitel 3 Teil a, Folie17

Counter Class in C++, Main Program #include <iostream> #include "Counter.hpp" int main(int argc, const char * argv[]) { // New counter instance (on stack) Counter ctr; std::cout << "Value of ctr: " << ctr.getvalue() << std::endl; ctr.count(); std::cout << "Value of ctr: " << ctr.getvalue() << std::endl; return 0; main.cpp LMU München, Sommer 2016 Prof. Hußmann: Multimedia-Programmierung Kapitel 3 Teil a, Folie18

C++: Scope Resolution and Namespaces Scope operator (::) of C++: Various purposes First main purpose: Implementing member functions of classes outside the actual class Example Counter::count() In this case, only the function prototype is declared within the class Second main purpose: Qualifying identifiers as belonging to a certain namespace Example std::cout which belongs to the std namespace Declaring a new namespace: namespace ns_name { declarations ; Using a given namespace for unqualified identifiers: using namespace ns_name; LMU München, Sommer 2016 Prof. Hußmann: Multimedia-Programmierung Kapitel 3 Teil a, Folie19

3 Multimedia Programming with C++ and Multimedia Frameworks 3.1 Multimedia Support by Languages and Frameworks 3.2 Introduction to C++ 3.3 SFML: Low-Level Multimedia/Game Library for C++ 3.4 Cocos2d-x: High Level Game Engine for C++ Literature: http://www.sfml-dev.org M. G. Milchev: SFML Essentials, Packt Publishing 2015 LMU München, Sommer 2016 Prof. Hußmann: Multimedia-Programmierung Kapitel 3 Teil a, Folie20

SFML: Simple and Fast Multimedia Library Relatively modern multimedia abstraction layer for C++ Initiator and head of development: Laurent Gomila (France) Latest version: 2.3.2 (September 2015) LMU München, Sommer 2016 Prof. Hußmann: Multimedia-Programmierung Kapitel 3 Teil a, Folie21

Slide Show with SFML (1) #include <SFML/Graphics.hpp> #include "ResourcePath.hpp" #include <array> //Global dimensions const int SCREEN_WIDTH = 712; const int SCREEN_HEIGHT = 712; const int OFFSET_HOR = 100; const int OFFSET_VER = 100; //Directory name for pictures const std::string gpicsdir = "pics"; //Picture file names const int NUM_PICS = 4; std::array<std::string, NUM_PICS> gpicfiles = {"frog.jpg","cows.jpg","elephant.jpg","tiger.jpg"; const sf::color bg = sf::color(255, 228, 95, 255); //background color const sf::time interval = sf::seconds(4.); LMU München, Sommer 2016 Prof. Hußmann: Multimedia-Programmierung Kapitel 3 Teil a, Folie22

Slide Show with SFML (2) int main(int, char const**) { // Create the main window sf::renderwindow window (sf::videomode(screen_width, SCREEN_HEIGHT), "SFML Slide Show"); //Load pictures sf::texture loadedpics[num_pics]; for(int i = 0; i < NUM_PICS; i++) { std::string picpath = resourcepath() +gpicsdir+"/"+gpicfiles[i]; if (!loadedpics[i].loadfromfile(picpath)) { return EXIT_FAILURE; //Create a sprite to display sf::sprite sprite; sprite.setscale(sf::vector2f(2.f, 2.f)); sprite.setposition(sf::vector2f(offset_hor, OFFSET_VER)); LMU München, Sommer 2016 Prof. Hußmann: Multimedia-Programmierung Kapitel 3 Teil a, Folie23

Slide Show with SFML (3) //Main loop int slideindex = 0; //Index of picture to be shown bool updatepicture = true; //Do we need to change the picture? sf::clock clock; //Create and start timer sf::time timer; while (window.isopen()) { //Display next picture if necessary if (updatepicture) { //Clear screen window.clear(bg); //Set picture and draw the sprite sprite.settexture(loadedpics[slideindex]); window.draw(sprite); //Update the window window.display(); updatepicture = false; // Set timer timer = clock.getelapsedtime(); LMU München, Sommer 2016 Prof. Hußmann: Multimedia-Programmierung Kapitel 3 Teil a, Folie24

Slide Show with SFML (4) // Process events sf::event event; while (window.pollevent(event)) { // Close window: exit if (event.type == sf::event::closed) { window.close(); // Arrow left pressed: previous picture if (event.type == sf::event::keypressed && event.key.code == sf::keyboard::left) { slideindex = (slideindex+num_pics-1) % NUM_PICS; //Strange C++ modulo updatepicture = true; // Arrow right pressed: next picture if (event.type == sf::event::keypressed && event.key.code == sf::keyboard::right) { slideindex = (slideindex+1) % NUM_PICS; updatepicture = true; LMU München, Sommer 2016 Prof. Hußmann: Multimedia-Programmierung Kapitel 3 Teil a, Folie25

Slide Show with SFML (5) // Check time interval if (clock.getelapsedtime() > timer+interval) { slideindex = (slideindex+1) % NUM_PICS; updatepicture = true; return EXIT_SUCCESS; LMU München, Sommer 2016 Prof. Hußmann: Multimedia-Programmierung Kapitel 3 Teil a, Folie26