Adding Variables to Script

Let's modify Listing 9–5 a little bit to learn more about the reflected layer. So far, we only saw how the hello_world routine from the script was reflected in Listing 9–2. Here we are going to add more variables in the script, which we will call hellorendering2.rs, as shown in Listing 9–9.

Listing 9–9. Changing Background Color (Second Version)

#pragma version(1)
#pragma rs java_package_name(com.apress.proandroid.ch9)

#include "rs_graphics.rsh"

// we removed init() as it was empty anyway

float red = 0.0f; // initialized to 1.0f

float green; // purposely not initialized

static float blue; // purposely not initialized, static

const float alpha = 1.0f; // constant

int root() {
    // clear the background color
    blue = rsRand(1.0f);
    rsgClearColor(red, green, blue, alpha);

    // 50 frames per second = 20 milliseconds per frame
    return 20;
}

As you can see, the four variables—red, green, blue, and alpha—are all defined differently. While this does not necessarily make sense for the actual script, it allows us to see how the reflected layer is created. Listing 9–10 shows the ScriptC_hellorendering2.java class.

Listing 9–10. ScriptC_hellorendering2.java

/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * This file is auto-generated. DO NOT MODIFY!
 * The source RenderScript file: E:wsChapter
9srccomapressproandroidch9hellorendering2.rs
 */
package com.apress.proandroid.ch9;

import android.renderscript.*;
import android.content.res.Resources;

/**
 * @hide
 */
public class ScriptC_hellorendering2 extends ScriptC {
    // Constructor
    public  ScriptC_hellorendering2(RenderScript rs, Resources resources, int id) {
        super(rs, resources, id);
        mExportVar_red = 0f;
        mExportVar_alpha = 1f;
    }

    private final static int mExportVarIdx_red = 0;
    private float mExportVar_red;
    public void set_red(float v) {
        mExportVar_red = v;
        setVar(mExportVarIdx_red, v);
    }

    public float get_red() {
        return mExportVar_red;
    }

    private final static int mExportVarIdx_green = 1;
    private float mExportVar_green;
    public void set_green(float v) {
        mExportVar_green = v;
        setVar(mExportVarIdx_green, v);
    }

    public float get_green() {
        return mExportVar_green;
    }

    private final static int mExportVarIdx_alpha = 2;
    private float mExportVar_alpha;
    public float get_alpha() {
        return mExportVar_alpha;
    }
}

The global red variable was defined in the script and initialized to zero. Consequently, the reflected layer defines a private mExportVar_red floating-point member, initialized to zero in the constructor, as well as two methods to set and get the red value: set_red() and get_red().

The global green variable in the script was very similar to the red one, except for the fact that it was not initialized. The reflected layer therefore also defines a private mExportVar_green floating-point member as well as two methods, set_green() and get_green(). However, the mExportVar_green member is not initialized in the constructor.

The blue variable was defined as static, which means it is not exported outside the script. As a consequence, the reflected layer does not define any member or method related to the script's blue component.

Finally, the alpha component was defined as a constant, and therefore the reflected layer contains the initialization of the mExportVar_alpha member in the constructor and only defines one method, get_alpha(), to access the value of the member. Since the alpha component is constant, there is indeed no need to define any set_alpha() method in Java.

NOTE: Global pointers defined in the script would result in a bind_pointer_name() method in the reflected layer instead of a set_pointer_name() method.

As you can see, a get() method in the reflected layer returns the last value set in Java. For example, get_green() simply returns mExportVar_green, which can only be modified by a call to set_green(). This basically means that if the script modifies the value of the global green variable, the change won't propagate to the Java layer. This is a very important detail to be aware of.

We will now review one of the simple examples provided by Android as sample code to learn more about one of the most important RenderScript functions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.75.43