Book Image

Cinder Creative Coding Cookbook

Book Image

Cinder Creative Coding Cookbook

Overview of this book

Cinder is one of the most exciting frameworks available for creative coding. It is developed in C++ for increased performance and allows for the fast creation of visually complex, interactive applications."Cinder Creative Coding Cookbook" will show you how to develop interactive and visually dynamic applications using simple-to-follow recipes.You will learn how to use multimedia content, draw generative graphics in 2D and 3D, and animate them in compelling ways. Beginning with creating simple projects with Cinder, you will use multimedia, create animations, and interact with the user.From animation with particles to using video, audio, and images, the reader will gain a broad knowledge of creating applications using Cinder.With recipes that include drawing in 3D, image processing, and sensing and tracking in real-time from camera input, the book will teach you how to develop interesting applications."Cinder Creative Coding Cookbook" will give you the necessary knowledge to start creating projects with Cinder that use animations and advanced visuals.
Table of Contents (19 chapters)
Cinder Creative Coding Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Accessing files dropped onto the application window


Cinder applications can respond to files dropped onto the application window through the callback, fileDrop . This method takes a ci::app::FileDropEvent object as a parameter with information about the event.

Getting ready

Your application must implement a fileDrop method which takes a ci::app::FileDropEvent object as a parameter.

Add the following method to the application's class declaration:

void fileDrop( FileDropEvent event );

How to do it…

We will learn how to work with the ci::app::FileDropEvent object to work with file drop events. Perform the following steps to do so:

  1. In the method implementation you can use the ci::app::FileDropEvent parameter to access the list of files dropped onto the application by calling the getFiles method. This method returns a conststd::vector container with fs::path objects:

    const vector<fs::path >& files = event.getFiles();
  2. The position where the files were dropped onto the window can be accessed through the following callback methods:

    • To get a ci::Vec2i object with the position of the files dropped, type in the following line of code:

      Vec2i dropPosition = event.getPos();
    • To get the x and y coordinates separately, you can use the getX and getY methods, for example:

      int pOS X = event.getX();
      int posY = event.getY();
  3. You can find the number of dropped files by using the getNumFiles method:

    int numFiles = event.getNumFiles();
  4. To access a specific file, if you already know its index, you can use the getFile method and pass the index as a parameter.

    For example, to access the file with an index of 2, you can use the following line of code:

    const fs::path& file = event.getFile( 2 );

How it works…

A Cinder application will respond to the system's native event for file drops. It will then create a ci::app::FileDropEvent object with information about the event and call the fileDrop callback in our application. This way Cinder creates a uniform way of responding to file drop events across the Windows and OS X platforms.

There's more…

Cinder uses ci::fs::path objects to define paths. These are typedef instances of boost::filesystem::path objects and allow for much greater flexibility when working with paths. To learn more about the fs::path objects, please refer to the boost::filesystem library reference, available at http://www.boost.org/doc/libs/1_50_0/libs/filesystem/doc/index.htm.