Cocos2d-x C++ Windows 8 &Windows Phone 8

Post on 02-Jul-2015

1.136 views 4 download

description

In the first of two sessions, we will explore using Cocos2d-x, a free and open source game engine. It runs on Windows, Mac OS X, and Linux and builds games for every popular phone, tablet, and desktop platform. It uses C++ to get close to metal performance and a smart and logical set of classes to make it easy. Don't let the C++ scare you off. We are using Cocos2d-x to create fun platform games, not building complicated financial software. So long as you know at least one curly brace language you will be able to write Cocos2d-x style C++. We will explore how to install Cocos2d-x. How to build the test apps and finally how to build your own apps specifically for Windows 8 and Windows Phone 8, but the code will run on iOS, Mac, and Android too.

Transcript of Cocos2d-x C++ Windows 8 &Windows Phone 8

Cross Platform Game Programming with Cocos2d-x (Windows 8)SoCal Code Camp - 15 November 2014 Troy Miles

Who am I?Troy Miles

Wrote a few hit computer games in the 80’s and 90’s

rockncoder@gmail.com

https://github.com/Rockncoder

http://www.slideshare.net/rockncoder

@therockncoder

Cocos2d-x: Introduction

What is Cocos2d-x?

Open source game engine under MIT license

It is optimized for 2D graphics using OpenGL

Used by more than 400,000 developers worldwide

History

2008 Ricardo Quesada in Argentina creates Cocos2d

Ported to iPhone with the opening of the iPhone App Store

2010 Zhe Wang in China branches it creating Cocos2d-x

2013 Ricardo joins Cocos2d-x team

Target Platforms

Windows Phone 8+

iPhone 5+

Android 2.3+

Windows 7+

Mac OS X 10.6+

Development Platforms

Windows 7+, VS 2012+

Mac OS X 10.7+, Xcode 4.6+

Ubuntu 12.10+, CMake 2.6+

All platforms need Python 2.7.5 (not 3!)

Development Languages

C++

Lua

JavaScript (all except WP8)

Why C++?

It is the most popular game programming language

It is universal

It is fast

It really isn’t that hard

C++ 11 makes it even better

Mandatory Tools

In order to build iPhone apps, you need Xcode

In order to build Windows Phone apps, you need Visual Studio

Titles using Cocos2d-x

Text

Avengers AllianceMarvel Entertainment

Text

Family Guy: The Quest for StuffTinyCo, Inc.

Diamond DashWooga

Star Wars: Tiny Death StarLucasArts

BADLANDFrogmind

Key Points

Cocos2d has been around since 2008

Cocos2d-x uses C++ for speed

A lot of big name titles use it

It has always been free and always will be

Cocos2d-x: Installation

Windows Development

Python 2.7.5

Visual Studio 2012+

Windows Phone SDK 8

cocos2d-x.org

Tutorials

Forum

Blog

Downloads

Download & Installation

http://www.cocos2d-x.org/download

Click “Looking for an older version?”

Click “v3.2 Jul. 18, 2014”

Unzip it

v2 vs v3Current version is 3.2, 3.3 is in beta

Version 3.0 is a re-write using C++ 11

Many V2 classes have been deprecated and renamed

Updated directory structure

Better command line tooling

Drops Xcode templates

Windows 8 PathSettings -> Control Panel -> System -> Advanced system settings

Environmental Variables…

System variables modal

Path

Edit...

Add "[cocos2d-x root]\tools\cocos2d-console\bin\"

Running C++ Tests

Open file "cocos2d-wp8.vc2012.sln" in build directory

Make cpp-tests the StartUp project

Compile and run

Key Points

Cocos2d-x version 3 was a complete re-write

Installing cocos2d-x on windows is pretty easy

Getting the C++ Test app to compile and run is essential

Cocos2d-x: Feature Overview

Widgets

Button

CheckBox

ListView

Slider

TextField

Audio

Sound effects

Background music

CocosDenshion library (Open AL)

Support audio is platform dependent

PhysicsChipmunk2D

Portable 2-dimensional real-time rigid body physics engine

Written in C99 by Scott Lembcke

Box2d

Free open source 2-dimensional physics simulator engine

Written in C++ by Erin Catto

Published under the zlib license

Network

HTTP + SSL

WebSocket API

XMLHttpRequest API

User Input

Touch/Accelerometer on mobile devices

Touch/Mouse/Keyboard on desktop

Game controller support

2D GraphicsTransitions between scenes

Sprites and Sprite Sheets

Effects: Lens, Ripple, Waves, Liquid, etc.

Transformation Actions: Move, Rotate, Scale, Fade, Tint, etc.

Composable actions: Sequence, Spawn, Repeat, Reverse

2D Graphics

Ease Actions: Exp, Sin, Cubic, Elastic, etc.

Misc actions: CallFunc, OrbitCamera, Follow, Tween

Particle system

Skeleton Animations: Spine and Adobe DragonBone

Fast font rendering using Fixed and Variable width fonts

2D Graphics

TrueType fonts

Tile Map support: Orthogonal, Isometric and Hexagonal

Parallax scrolling

Motion Streak

Render To Texture

Right handed coordinate system

Different than web

Origin (0,0) located at lower left hand of screen

x position increases going right

y position increases going up

max x,y at upper right hand corner

C++ Test Demo

Lexicon

Director

Node

Scene

Layer

Sprite

Action

Particle

Event

Key Points

Cocos2d-x is feature packed

Its classes follow a logical progression which makes them easy to learn

Cocos2d-x: Building a Game

Cocos command optionsnew - creates a new game

run - compile, deploy, and run on game on target

deploy - deploy game to target

compile - compiles game

luacompile - minifies Lua files and compiles

jscompile - minifies JS files and compiles

Cocos command options

-h, —help - Shows the help

-v, —version - Shows the current version

Creating a new game

Open the cmd window

Validate that cocos is in your path, cocos <enter>

cocos new GameName -p com.company.name -l cpp

new command explained

MyGame: name of your project

-p com.MyCompany.MyGame: package name

-l cpp: programming language used for the project, valid values are: cpp, lua, and js

Directory Structure

Classes

cocos2d

Resources

Platform directories

WP8 Platform Directory

The solution file

Leave everything else alone

Class Directory

AppDelegate initializes cocos2d-x

HelloWorldScene is where your game begins

Hello World

Use Visual Studio to build and deploy the game

Best to use actual device

Debugging

Right click on project name

Click on Properties

Click on Debug tab (left side, second from top)

Under UI Task, select Native Only

Graphics Diagnostics

82 <-- number of draw calls

0.016 <-- time it took to render the frame

60.0 <-- frames per second

In the file, AppDelegate.cpp find the line: director->setDisplayStats(true);

Memory Diagnostics

Windows Phone adds a Memory Diagnostics

First value is current memory usage, second is the peak

Like the Graphics Diagnostics, it can be removed

In MainPage.xaml.cs, near the top of the file

Comment out definition of DISPLAY_MEMORY

Examining the code

AppDelegate.cpp initializes cocos2d-x and launches your game

HelloWorldScene.cpp is your game

Initially it doesn’t do much

Adding Scenes

Scenes are actually derived from the Layer class

Add scene for main, game, pause, end game

Add colors to scenes

Add transitions

Adding Classes

For cross platform-ness we want preserve our directory structure

Visual Studio needs to be tricked

We add the class, remove it, move it to correct directory, then add it back

Class Template

Most of the classes we add look like this

Prefer using #ifndef over #pragma once

Remember to include base class

CREATE_FUNCUnlike Java, C#, or even JavaScript, C++ programs manage their own memory

cocos has memory manage

CREATE_FUNC adds it to your class

How the Scene is launched

Currently we are still using the HelloWorldScene instead of our fancy new MainScene

Open file AppDelegate.cpp, find:

auto scene = HelloWorld::createScene();

The Game Loop

The central component of any game

Allows game to run smoothly regardless of user input

Allows game to run at same speed regardless of machine

Game Loop Reality

In the scene init add, this->scheduleUpdate()

Add an update method, void HelloWorld::update(float dt)

The update method should be as fast as possible

Steps

Add a background

Add a hero

Move the hero

Detect touches

Animate Hero

Steps

Adding sound

Adding music

Adding scenes

Resources

http://www.nosoapradio.us/

http://www.freesound.org/

http://spritedatabase.net/

http://opengameart.org/

http://www.lostgarden.com/search/label/free%20game%20graphics

Summary

Cocos2d-x is a free open source game engine

It supports Windows, WP8, iOS, Android, and more

https://github.com/Rockncoder/PlaneGame2

Contacting Me

@therockncoder

rockncoder@gmail.com

https://github.com/Rockncoder

http://www.slideshare.net/rockncoder

Text

http://spkr8.com/t/39721Please rate this talk!