Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8c76cef0 authored by Romain Guy's avatar Romain Guy
Browse files

Smoother transitions in GrassRS

parent 584a375d
Loading
Loading
Loading
Loading
+34 −6
Original line number Diff line number Diff line
// Grass live wallpaper
/*
 * Copyright (C) 2008 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.
 */
 
#pragma version(1)
#pragma stateVertex(default)
@@ -8,7 +22,10 @@
#define WVGA_PORTRAIT_WIDTH 480.0f
#define WVGA_PORTRAIT_HEIGHT 762.0f

#define RSID_SKY_TEXTURES 0
#define RSID_STATE 0
#define RSID_FRAMECOUNT 0

#define RSID_SKY_TEXTURES 1
#define RSID_SKY_TEXTURE_NIGHT 0
#define RSID_SKY_TEXTURE_SUNRISE 1
#define RSID_SKY_TEXTURE_NOON 2
@@ -19,8 +36,15 @@
#define AFTERNOON 0.6f
#define DUSK 0.8f

float time() {
    return (second() % 60) / 60.0f;
#define SECONDS_IN_DAY 24.0f * 3600.0f

#define REAL_TIME 0

float time(int frameCount) {
    if (REAL_TIME) {
        return (hour() * 3600.0f + minute() * 60.0f + second()) / SECONDS_IN_DAY;
    }
    return (frameCount % 180) / 180.0f;
}

void alpha(float a) {
@@ -53,7 +77,8 @@ void drawSunset() {
}

int main(int launchID) {
    float now = time();
    int frameCount = loadI32(RSID_STATE, RSID_FRAMECOUNT);
    float now = time(frameCount);
    alpha(1.0f);

    if (now >= MIDNIGHT && now < MORNING) {
@@ -80,5 +105,8 @@ int main(int launchID) {
        drawNight();
    }

    frameCount++;
    storeI32(RSID_STATE, RSID_FRAMECOUNT, frameCount);

    return 1;
}
+12 −4
Original line number Diff line number Diff line
/*
 * Copyright (C) 2008 The Android Open Source Project
 * Copyright (C) 2009 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.
@@ -25,13 +25,12 @@ import static android.renderscript.RenderScript.EnvMode.*;
import static android.renderscript.RenderScript.DepthFunc.*;
import static android.renderscript.RenderScript.BlendSrcFunc;
import static android.renderscript.RenderScript.BlendDstFunc;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap;

import java.util.TimeZone;

class GrassRS {
    private static final int RSID_SKY_TEXTURES = 0;
    private static final int RSID_STATE = 0;
    private static final int RSID_SKY_TEXTURES = 1;
    private static final int SKY_TEXTURES_COUNT = 4;

    private Resources mResources;
@@ -52,6 +51,8 @@ class GrassRS {
    private RenderScript.Allocation[] mSkyTextures;
    @SuppressWarnings({"FieldCanBeLocal"})
    private int[] mSkyBufferIDs;
    @SuppressWarnings({"FieldCanBeLocal"})
    private RenderScript.Allocation mState;

    public GrassRS() {
    }
@@ -66,6 +67,7 @@ class GrassRS {
        createProgramVertex();
        createProgramFragmentStore();
        createProgramFragment();
        createScriptStructures();
        
        mRS.scriptCBegin();
        mRS.scriptCSetClearColor(0.0f, 0.0f, 0.0f, 1.0f);
@@ -76,11 +78,17 @@ class GrassRS {
        mScript = mRS.scriptCCreate();

        loadSkyTextures();        
        mScript.bindAllocation(mState, RSID_STATE);
        mScript.bindAllocation(mSkyTexturesIDs, RSID_SKY_TEXTURES);

        mRS.contextBindRootScript(mScript);        
    }

    private void createScriptStructures() {
        mState = mRS.allocationCreatePredefSized(RenderScript.ElementPredefined.USER_I32, 1);    
        mState.data(new int[1]);
    }

    private void loadSkyTextures() {
        mSkyBufferIDs = new int[SKY_TEXTURES_COUNT];
        mSkyTextures = new RenderScript.Allocation[SKY_TEXTURES_COUNT];