Day 5 of C++ Boot Camp - Stepping Up to Mobile

41
© 2016 Embarcadero Technologies, Inc. All rights reserved. C++ Boot Camp - Aug 2016 - http://embt.co/2016cbootcamp C++ BUILDER BOOT CAMP 8 th to 12 th of August, 2016 More information http://embt.co/2016cbootcamp

Transcript of Day 5 of C++ Boot Camp - Stepping Up to Mobile

Page 1: Day 5 of C++ Boot Camp - Stepping Up to Mobile

© 2016 Embarcadero Technologies, Inc. All rights reserved.C++ Boot Camp - Aug 2016 - http://embt.co/2016cbootcamp

C++BUILDER BOOTCAMP8thto12thofAugust,2016

Moreinformationhttp://embt.co/2016cbootcamp

Page 2: Day 5 of C++ Boot Camp - Stepping Up to Mobile

© 2016 Embarcadero Technologies, Inc. All rights reserved.C++ Boot Camp - Aug 2016 - http://embt.co/2016cbootcamp

DAY5: STEPPINGUPTOMOBILE

[email protected]

&JimMcKeeth

[email protected]

Moreinfohttp://embt.co/2016cppbootcamp5

Page 3: Day 5 of C++ Boot Camp - Stepping Up to Mobile

© 2016 Embarcadero Technologies, Inc. All rights reserved.C++ Boot Camp - Aug 2016 - http://embt.co/2016cbootcamp

DAY5:AGENDA▪ Introductiontothemobileplatforms▪ PrototypingmobileappsonWindows▪Usingplatformservices▪ Conditionaldefines▪ Themobilecompilers▪UnderstandingARCmemorymanagement▪ Deploymentmanager▪Makingthemovetomobile

Moreinfohttp://embt.co/2016cppbootcamp5

Page 4: Day 5 of C++ Boot Camp - Stepping Up to Mobile

© 2016 Embarcadero Technologies, Inc. All rights reserved.C++ Boot Camp - Aug 2016 - http://embt.co/2016cbootcamp

ABOUTJIMMCKEETH▪ LeadWorldWideDeveloperEvangelist&Engineer▪ Longtimesoftwaredeveloper▪ Inventedandpatentedpatternandswipetounlock▪ USPatent#8352745&6766456,etc.

▪ BuiltthoughtcontrolleddronewithGoogleGlass▪ HostofPodcastatDelphi.org▪ LivesnearBoise,Idaho,USAwithhisfamily▪ ImprovisationalcomedyperformerwithCSzBoise▪ FollowonTwitter@JimMcKeeth▪ Availableviaemail:[email protected]

Page 5: Day 5 of C++ Boot Camp - Stepping Up to Mobile

© 2016 Embarcadero Technologies, Inc. All rights reserved.C++ Boot Camp - Aug 2016 - http://embt.co/2016cbootcamp

ABOUTDAVIDMILLINGTON▪ C++ProductManager,EmbarcaderoTechnologies▪ FromAustralia,livinginEurope▪ LongtimesoftwaredeveloperwithTurboPascal,

C++Builder,C,ObjectiveC,Java,PHPandothers▪ Projectsincludeautonomousunderwatervehicle

controls,dataprocessing,datavisualization,etc.▪ OwnerofParnassus,makersofRADStudioIDEPlugins▪ NavigatorandBookmarks.

▪ Availableviaemail:[email protected]

Page 6: Day 5 of C++ Boot Camp - Stepping Up to Mobile

© 2016 Embarcadero Technologies, Inc. All rights reserved.C++ Boot Camp - Aug 2016 - http://embt.co/2016cbootcamp

OBJECTIVEOFTHISSESSIONShowyoueverythingyouneedtogetmostoftheworkdoneonyourmobileappswithStarterEditionbeforeyouneedtoupgradetoProfessional+MobiletodeploytoAndroidandiOS.

Page 7: Day 5 of C++ Boot Camp - Stepping Up to Mobile

© 2016 Embarcadero Technologies, Inc. All rights reserved.C++ Boot Camp - Aug 2016 - http://embt.co/2016cbootcamp

INTRODUCTIONTOTHEMOBILEPLATFORMS

▪ BasiccharacteristicsofAndroidandiOS▪ Somedifferencesbetweenthetwo▪ Commonresolutions&orientations▪ Sensorsandfunctionality▪ Platformdefaultbehaviors▪ Moreresources

Page 8: Day 5 of C++ Boot Camp - Stepping Up to Mobile

© 2016 Embarcadero Technologies, Inc. All rights reserved.C++ Boot Camp - Aug 2016 - http://embt.co/2016cbootcamp

iOSINTRODUCEDRETINA(AKAHIDPI)▪ iPhonewasoriginally320x480▪ iPhoneRetinabecame640x960▪ Thedots(orpixels)perinchdoubled▪ Everythingstayedthesamesizeand

justgotmoredetailed▪ Thelogicalpixelsstayed320x480▪ Retinauseshigherdensitygraphics

Page 9: Day 5 of C++ Boot Camp - Stepping Up to Mobile

© 2016 Embarcadero Technologies, Inc. All rights reserved.C++ Boot Camp - Aug 2016 - http://embt.co/2016cbootcamp

ANDROIDSCREENSIZESANDDENSITIES▪ Densities• ldpi0.75x(low)~120dpi• mdpi1x(medium)~160dpi• hdpi1.5x(high)~240dpi• xhdpi2x(extra-high)~320dpi• xxhdpi3x(xx-high)~480dpi• xxxhdpi4x(xxx-high)~640dpi

▪ Sizes:• xlarge>960dpx720dp• large>640dpx480dp• normal>470dpx320dp• small>426dpx320dp

Page 10: Day 5 of C++ Boot Camp - Stepping Up to Mobile

© 2016 Embarcadero Technologies, Inc. All rights reserved.C++ Boot Camp - Aug 2016 - http://embt.co/2016cbootcamp

COMMONANDROIDDEVICERESOLUTIONS

▪ Nexus6p• Physical1440x2560• Logical411x731• 518ppior3.5x• Aspect16:9

▪ SamsungGalaxyS6• Physical1440x2560• Logical360x640• 576ppior4.0x• Aspect16:9

▪ SonyXperiaZ2/Z3• Physical1080x1920• Logical360x640• 424ppior3.0x• Aspect16:9

▪ Nexus7(2013)• Physical1200x1920• Logical600x960• 323ppior2.0x• Aspect16:10

https://design.google.com/devices/

Page 11: Day 5 of C++ Boot Camp - Stepping Up to Mobile

© 2016 Embarcadero Technologies, Inc. All rights reserved.C++ Boot Camp - Aug 2016 - http://embt.co/2016cbootcamp

COMMONIOSRESOLUTIONS

▪ iPhoneSE&iPhone5• Physical640x1136• Logical320x568• 326ppior2.0x• Aspect16:9

▪ iPhone6&6s• Physical750x1334• Logical375x667• 326ppior2.0x• Aspect16:9

▪ iPhone6Plus&6sPlue• Physical1242x2208• Logical414x736• 401ppior3.0x• Aspect16:9

▪ iPadMini&Air• Physical1536x2048• Logical768x1024• 326to264ppior2.0x• Aspect4:

http://iosres.com/

Page 12: Day 5 of C++ Boot Camp - Stepping Up to Mobile

C++ Boot Camp - Aug 2016 - http://embt.co/2016cbootcamp© 2016 Embarcadero Technologies, Inc. All rights reserved.

THEMULTIRESOLUTIONBITMAP

▪ Supportsbitmapsofmultiplepixeldensities▪ Selectscorrectresolutionforcurrentdevice▪ Automaticallycalculatesintermediaries▪ Scalingdownisbetterthanup,

soprovidethe4xataminimum▪ Stylesnativelysupportmultipleresolutions

http://embt.co/MultiResBitmap

Page 13: Day 5 of C++ Boot Camp - Stepping Up to Mobile

© 2016 Embarcadero Technologies, Inc. All rights reserved.C++ Boot Camp - Aug 2016 - http://embt.co/2016cbootcamp

PLATFORMANDSENSORCOMPONENTS

▪ CommonSensors:▪ TLocationSensor(GPS)▪ TMotionSensor*(Accelerometer)▪ TOrientationSensor*(Gyroscope&Compass)▪ TSensorManagertoaccessallsensors&data

▪ TNotificationCenterforschedulingnotifications▪ TCameraComponentaccessthecamera▪ TMediaPlayerrecordaudio,playaudioandvideo▪ TGestureManagerforinteractivegestures▪ TStyleBookforcustomandpremiumstyles

*NotusuallyavailableonWindows

Page 14: Day 5 of C++ Boot Camp - Stepping Up to Mobile

© 2016 Embarcadero Technologies, Inc. All rights reserved.C++ Boot Camp - Aug 2016 - http://embt.co/2016cbootcamp

MEDIALIBRARYACTIONS

▪ Actionsprovidedtoaccess:▪ Camera▪ CameraRoll▪ Text&Imagesharingservices

▪ Mail,Facebook,Twitter,socialnetworking,etc.

▪ Message(SMSorMMS)▪ Printing

▪ AnimatedTransitions

Allspecifictomobile

Page 15: Day 5 of C++ Boot Camp - Stepping Up to Mobile

© 2016 Embarcadero Technologies, Inc. All rights reserved.C++ Boot Camp - Aug 2016 - http://embt.co/2016cbootcamp

STANDARDRTLPATHFUNCTIONS

▪ #include<System.IOUtils.hpp>▪ System::Ioutils::TPath::GetHomePath()▪ Windows:C:\Users\<username>\AppData\Roaming▪ OSX:/Users/<username>▪ iOS(Device):/private/var/mobile/Applications/<applicationID>▪ Android:/data/data/<applicationID>/files

▪ Othersinclude:NamedGet<X>Path▪ Temp,Library,Documents,SharedDocuments,Cache,Pictures,

SharedPictures,Public,Camera,SharedCamera,Music,SharedMusic,Movies,SharedMovies,Alarms,Ringtones,SharedRingtones,Downloads,andSharedDownloads

Moreinformation:http://embt.co/pathfunctions

Page 16: Day 5 of C++ Boot Camp - Stepping Up to Mobile

C++ Boot Camp - Aug 2016 - http://embt.co/2016cbootcamp© 2016 Embarcadero Technologies, Inc. All rights reserved.

PROTOTYPINGMOBILEAPPSONWINDOWS

Followingamobiletutorial,onWindowsJimMcKeeth

Page 17: Day 5 of C++ Boot Camp - Stepping Up to Mobile

© 2016 Embarcadero Technologies, Inc. All rights reserved.C++ Boot Camp - Aug 2016 - http://embt.co/2016cbootcamp

MYPHOTOSANDEFFECTSAPP

▪ Usethecameratotakeapicture

▪ Selectfilterforimage

▪ Addafiltereffecttotheimage

▪ PrototypeonWindows

▪ UpgradetoMobile

▪ DeployoniOSandAndroid

Page 18: Day 5 of C++ Boot Camp - Stepping Up to Mobile

C++ Boot Camp - Aug 2016 - http://embt.co/2016cbootcamp© 2016 Embarcadero Technologies, Inc. All rights reserved.

USINGPLATFORMSERVICESDefinessomefunctionalitythatmightormightnotbe

implementedonaparticularplatformDaveMillington

Page 19: Day 5 of C++ Boot Camp - Stepping Up to Mobile

FMX Platform Services• FMXisacross-platformframework• Aimstosupportwidevarietyoffunctionality,evenifthatfunctionality

isveryplatform-specific• IfiOSdoessomethingthatWindowsdoesn’t,aFMXapponiOSshould

supportthat• Thus–platformservices

Page 20: Day 5 of C++ Boot Camp - Stepping Up to Mobile

What are all the platform services?• Systemfonts,messageboxes,menus,clipboard,messagedialogs• Goingfullscreen,showingwindowchrome• Mouseposition,mousecursors,multipledisplays,textinput,keyboards,

virtualkeyboards,draganddrop,webbrowsing,gesturerecognition• Phonedialing,ads,camera,photos

Andmanymore!http://docwiki.embarcadero.com/RADStudio/Berlin/en/FireMonkey_Platform_Services

Page 21: Day 5 of C++ Boot Camp - Stepping Up to Mobile

How do they work?• Eachhasaninterfacedefined• Eachinterfaceisimplementedforeachplatform(thatsupportsthat

functionality)• RegisteredwithFMX:canqueryifaninterfacehasanimplementation

registered,andgetthatimplementation• Canimplementandregisteryourown,includingreplacingdefaultones

Page 22: Day 5 of C++ Boot Camp - Stepping Up to Mobile

Interfaces in C++ - a digression• C++doesn’thaveinterfaces–anothercompilerextensionofours• (Delphi:COMisfirst-classlanguagefeature,interfaceskey,irrelevantforthisbut

useful)• Keybits:• __interfacekeyword• EachhasaGUID• WrapinSystem::DelphiInterface<I>templateclass,eg:

InFMX.Platform.hpp:typedefSystem::DelphiInterface<IFMXLocaleService> _di_FMXLocaleService;Inyourcode:_di_FMXLocalServiceMyInterface;

Page 23: Day 5 of C++ Boot Camp - Stepping Up to Mobile

FMX Platform Services• Interfacesandimplementationsforawidevarietyofplatform-specific

behaviour• Thingsthatmayormaynotexistonanyplatform• Thingsthataredifferentonvariousplatforms• UsedinternallybyFMX,butyoucanaccessanduse–andalsodefine

yourownimplementation!

Page 24: Day 5 of C++ Boot Camp - Stepping Up to Mobile

C++ Boot Camp - Aug 2016 - http://embt.co/2016cbootcamp© 2016 Embarcadero Technologies, Inc. All rights reserved.

CONDITIONALDEFINESControlwhatcodegetscompiledondifferentplatformsbased

onthecompilersandconfigurations.DaveMillington

Page 25: Day 5 of C++ Boot Camp - Stepping Up to Mobile

Platform-specific conditional compilation• Allowsyoutocompilecodeonlyforoneplatform(oranothercondition)• Whatisit?

#ifdefined(_PLAT_MSWINDOWS)//Thiscodewillonlybecompiledif_PLAT_MSWINDOWSis//defined,whichitonlyisforaWindowstarget.returnL”HellofromWindows!”#elsereturnL”HellofromanyotherOS!”#endif

• Macros(eg_PLAT_WINDOWS)canbedefinedincode,inthecompileroptions,orbythecompileritself.

• Seehttp://docwiki.embarcadero.com/RADStudio/Berlin/en/ Conditional_Compilation_Overview_Index

Page 26: Day 5 of C++ Boot Camp - Stepping Up to Mobile

Key macrosPerplatform• _PLAT_MSWINDOWS• _PLAT_MACOS• _PLAT_IOS• _PLAT_ANDROID

Other• __clang__-anyClangcompiler• __APPLE__-anyAppleOS• __CONSOLE__-ifit’sacommand-line

app• Etc…

Foreachspecificcompiler• BCC32(old/classicWin32)• BCC32C(newClangWin32)• BCC64(newClangWin64)• BCCOSX(macOS)• BCCIOSARM(iOS,ARM32)• BCCIOSARM64(iOS,ARM64)• BCCAARM(Android)http://docwiki.embarcadero.com/RADStudio/ Seattle/en/Predefined_Macroshttp://docwiki.embarcadero.com/RADStudio/ Berlin/en/C%2B%2B_Compilers

Page 27: Day 5 of C++ Boot Camp - Stepping Up to Mobile

Platform-specific compilation guidelines• Doyoureallyneedto?FMX,FireDAC,REST,Indyetcalldoagoodjob• Doyoureallyneedto?Wemightalreadyhavecodetodowhatyouneed,eg• Wantaplatform-specificfolder(Documents,say)thatvariesWin/Mac/iOS/Android?Try

System::IOUtils::TPath–GetDocumentsPath,GetPicturesPath,GetAlarmsPath,GetDownloadPath,…

• OrfileIO–concatenatingpathswith\or/(TPathagain)

• Isolateit• Useinterfacesandhidethattherearemultipleimplementations• Separateunitsperplatform,ifcodesizeismorethanalineortwo

But,itcanbenecessary/useful.It’seasy.Widevarietyofmacrosdefinedforplatforms,compilers,bitness,andmore.

Page 28: Day 5 of C++ Boot Camp - Stepping Up to Mobile

C++ Boot Camp - Aug 2016 - http://embt.co/2016cbootcamp© 2016 Embarcadero Technologies, Inc. All rights reserved.

UNDERSTANDINGARCMEMORYMANAGEMENT

UsingAutomaticReferenceCountingtodeterministicallymanagememoryandobjectreferences

(BetterthanGarbageCollection)DaveMillington

Page 29: Day 5 of C++ Boot Camp - Stepping Up to Mobile

Automatic Reference Counting (ARC)Notusuallysomethingtodealwith,butinteresting…

Dealswiththeproblemofleakedobject:voidLeak(){Foo*bar=newFoo();}‘bar’isneverdeleted!

• Garbagecollection(Android(notus!),.Net)• Referencecounting(Apple/iOS,Delphi)• Othertechniqueslikesmartpointers(C++)

Page 30: Day 5 of C++ Boot Camp - Stepping Up to Mobile

How does it affect C++?Notmuch.Here’sanormalC++class:

classRealCppClass{};voidTest(){RealCppClass*test=newRealCppClass();}^leaks

voidTest(){unique_ptr<RealCppClass>test(newRealCppClass());}^safe

Page 31: Day 5 of C++ Boot Camp - Stepping Up to Mobile

How does it affect C++?OnlywhenusingDelphiclassesfromC++(C++BuildercanconsumeDelphicode;theDelphicompileremitsheaders,thereismagic…twolanguagesmix!)Here,therearenoleaksineithercodesample:

voidTestDelphi(){TObject*obj=newTObject();}//1

voidTestDelphi(){TObject*a=newTObject();//1{TObject*b=newTObject();//2b=a;//3}//4}//5

Page 32: Day 5 of C++ Boot Camp - Stepping Up to Mobile

Only when accessing Delphi frameworksMostframeworksimplementedinDelphi.NoneedtobeawareofARC,usually.Windows:never.Mobile:onlyDelphiclasses.

void__fastcallTForm1::ButtonClick(Tcomponent*Sender){TButton*button=dynamic_cast<TButton*>(Sender);//…}

‘button’isreferencecounted;FMXisaDelphi-implementedframework.

Page 33: Day 5 of C++ Boot Camp - Stepping Up to Mobile

Questions…WhatifIcalldeleteonanARC-edobject?Itdecrementstherefcount.Butusesmartpointers,neverdelete!

Doesitworkwithsmartpointers?Absolutelyyes.Keepusingthem!

WhichclassesareDelphiones?FMXandotherframeworksareallimplementedinDelphi.Onehint:theclassnamesareprefixedwithT,egTForm,TFDConnection.

Howdoesitrelatedtoshared_ptr<>?Similarconcept;shared_ptrcountsexternally;ARCisfaster.

Page 34: Day 5 of C++ Boot Camp - Stepping Up to Mobile

I like interesting compiler extensions…MakeaDelphi-styleclassinC++,itisARC-ed:class__declspec(delphiclass)RefCounted:publicTObject{}

__weak–weakpointer,norefcount,givesnilwhentheobjectisdeletedelsewhere__unsafe–unmanagedpointer,norefcount,noguarantees

__closuremethod=&(MyButton.Click);method(foo);//CallsMyButton.Click(foo)Objectinstanceinside‘method’isaweakpointer-isnullifMyButtonisdeleted.

Page 35: Day 5 of C++ Boot Camp - Stepping Up to Mobile

Do I need to do anything?• Justuseasmartpointerasyounormallywould(bestpractice;manual

lifetimewithdeleteisevil!)• Noneedtochangecodestyle.• DoesnotaffectnormalC++.• BeawareDelphi-styleclasseswillbealiveuntiltheirlastreference

vanishes.

Page 36: Day 5 of C++ Boot Camp - Stepping Up to Mobile

C++ Boot Camp - Aug 2016 - http://embt.co/2016cbootcamp© 2016 Embarcadero Technologies, Inc. All rights reserved.

DEPLOYINGTOMOBILEJimMcKeeth

Page 37: Day 5 of C++ Boot Camp - Stepping Up to Mobile

© 2016 Embarcadero Technologies, Inc. All rights reserved.C++ Boot Camp - Aug 2016 - http://embt.co/2016cbootcamp

USINGTHEDEPLOYMENTMANAGER

▪ Managesfilesandcreatesappbundles

Page 38: Day 5 of C++ Boot Camp - Stepping Up to Mobile

C++ Boot Camp - Aug 2016 - http://embt.co/2016cbootcamp© 2016 Embarcadero Technologies, Inc. All rights reserved.

ANDROIDPERMISSIONS

▪ MustspecifypermissionforwhattheappwilldoonAndroid

▪ Failuretodosowillresultinerrororlackoffeatures

▪ Permissionsappearintheappstore▪ Commonpermissionsinclude

Location,Camera,Internet,Bluetooth,etc.

Page 39: Day 5 of C++ Boot Camp - Stepping Up to Mobile

© 2016 Embarcadero Technologies, Inc. All rights reserved.C++ Boot Camp - Aug 2016 - http://embt.co/2016cbootcamp

SETTINGUPFORMOBILEDEVELOPMENT▪ Basicsofgettingstarted▪ PAServer▪ AndroidUSBDrivers▪ Instructions▪ https://www.embarcadero.com/products/cbuilder/videos

Page 40: Day 5 of C++ Boot Camp - Stepping Up to Mobile

© 2016 Embarcadero Technologies, Inc. All rights reserved.C++ Boot Camp - Aug 2016 - http://embt.co/2016cbootcamp

MAKINGTHEMOVETOMOBILE

▪ Loadingourprototypeappintoprofessional▪ DeployingtoiOS▪ DeployingtoAndroid▪ DebuggingonMobile

Page 41: Day 5 of C++ Boot Camp - Stepping Up to Mobile

© 2016 Embarcadero Technologies, Inc. All rights reserved.C++ Boot Camp - Aug 2016 - http://embt.co/2016cbootcamp

Q&AJoinusatCommunity.Embarcadero.com

[email protected]/cppbuilder

DavidMillington<[email protected]>&

JimMcKeeth<[email protected]>

Moreinfohttp://embt.co/2016cppbootcamp5