Skip to content

Commit

Permalink
Added SmartGLViewController for a better MVC architecture. SmartGLVie…
Browse files Browse the repository at this point in the history
…w is no more abstract.
  • Loading branch information
Arnaud Guyon committed Nov 19, 2016
1 parent 9ed61d5 commit 6d23aea
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 122 deletions.
94 changes: 30 additions & 64 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
@@ -1,100 +1,66 @@
# SmartGL #

A SmartGL scene works with different components:
**SmartGL** is an Android Studio library that simplifies the use of **OpenGL** to display **2D Sprites** and **3D Textured Objects**.

* a SmartGL**View** in your Layout
* a SmartGL**Renderer** linked to the SmartGLView
* Render**Object**s (Object3D or Sprite) which are added to the SmartGLRenderer
It has been used for several games and apps on the store.

## Preparing the code and layout ##
## Usage ##

### Custom SmartGLView ###

This is the View which will be displayed on screen.

acquireResources() and releaseResources() must be used to load and unload data used in the scene, like Textures. They are called when the View is presented or dismissed. acquireResource() is a good place to build your scene, which means to construct the objects and add them to the Renderer.

onPreRender() method is called every frame. This is where you can move the objects in space.

```java
public class CustomGLView extends SmartGLView {

public CustomGLView(Context context) {
super(context);
}

public CustomGLView(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
protected void acquireResources() {
super.acquireResources();
// load resources like common Textures
// build your objects here and add them to the Renderer
}

@Override
protected void releaseResources() {
super.releaseResources();
// release resources like Textures
}

@Override
public void onPreRender(OpenGLRenderer renderer) {
super.onPreRender(renderer);
// is called before rendering the next frame
// typically move your objects on screen here
}


```

## Activity ##
The OpenGL scene is displayed using a **SmartGLView** and a **SmartGLViewController**.

```xml

<fr.arnaudguyon.smartglapp.MainGLView
android:id="@+id/customGLView"
<fr.arnaudguyon.smartgl.opengl.SmartGLView
android:id="@+id/smartGLView"
android:layout_width="match_parent"
android:layout_height="150dp" />

android:layout_height="match_parent" />
```

You need to set an OpenGLRenderer to your CustomGLView. There is a default SmartGLRenderer that you can use. You can also set a default background color for the View using setClearColor.
The SmartGLViewController is just an interface, so it could be implemented by your Activity or Fragment, but I recommend to create a separate class for it.

```java
public class MainActivity extends AppCompatActivity {

private MainGLView mCustomGLView;
private SmartGLView mSmartGLView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mCustomGLView = (MainGLView) findViewById(R.id. customGLView);
SmartGLRenderer renderer = new SmartGLRenderer(this); // use the default renderer
renderer.setClearColor(0,0,0, 1); // background color (R,G,B,A)
mCustomGLView.setRenderer(renderer);
mSmartGLView = (SmartGLView) findViewById(R.id.smartGLView);
mSmartGLView.setController(new GLViewController());
}
}
```

### LifeCycle ###

It is really important to inform the SmartGLView when the Activity or Fragment is paused or resumed. **If you miss this step the scene will not be initialized or restored correctly**.

```java
public class MainActivity extends AppCompatActivity {
@Override
protected void onPause() {
if (mCustomGLView != null) {
mCustomGLView.onPause();
if (mSmartGLView != null) {
mSmartGLView.onPause();
}
super.onPause();
}

@Override
protected void onResume() {
super.onResume();
if (mCustomGLView != null) {
mCustomGLView.onResume();
if (mSmartGLView != null) {
mSmartGLView.onResume();
}
}
}

```

When the SmartGLView is ready, **onPrepareView**(SmartGLView smartGLView) is called on the SmartGLViewController.

This method is the best place to prepare the scene (load textures, and add objects to the scene).

When the SmartGLView is dismissed, **onReleaseView**(SmartGLView smartGLView) is called. This is the best place to release content from memory (like textures).

4 changes: 2 additions & 2 deletions smartgl/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 24
versionCode 5
versionName "0.9.5"
versionCode 6
versionName "0.9.6"
}
buildTypes {
release {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@ public void onResume() {

protected void acquireResources() {}
protected void releaseResources() {}
protected void onViewResized(int width, int height) {}
protected abstract void onViewResized(int width, int height);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import fr.arnaudguyon.smartgl.touch.TouchHelper;
import fr.arnaudguyon.smartgl.touch.TouchHelperEvent;

public abstract class SmartGLView extends OpenGLView {
public class SmartGLView extends OpenGLView {

private TouchHelper mTouchHelper;
private Sprite mInputSprite;
private SmartGLViewController mListener;

public SmartGLView(Context context) {
super(context);
Expand All @@ -24,11 +26,24 @@ public SmartGLView(Context context, AttributeSet attrs) {
super(context, attrs);
}

// TODO: improve that
public void setDefaultRenderer(@NonNull Context context) {
SmartGLRenderer renderer = new SmartGLRenderer(context);
renderer.setClearColor(0,0,0, 1); // background color (R,G,B,A)
setRenderer(renderer);
}

public SmartGLRenderer getSmartGLRenderer() {
return (SmartGLRenderer) getOpenGLRenderer();
OpenGLRenderer renderer = getOpenGLRenderer();
if (renderer instanceof SmartGLRenderer) {
return (SmartGLRenderer) renderer;
}
return null;
}

public void setController(SmartGLViewController controller) {
mListener = controller;
}

@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
Expand All @@ -47,7 +62,11 @@ public void onTouchEventFromOtherView(View fromView, MotionEvent event) {
@Override
public void onPreRender(OpenGLRenderer renderer) {
super.onPreRender(renderer);


if (mListener != null) {
mListener.onTick(this);
}

// Handle Touch Events: skip several moves in a row and send OnTouchEvent (on OpenGLThread)
if (mTouchHelper != null) {
TouchHelperEvent event = mTouchHelper.getNextEvent();
Expand Down Expand Up @@ -117,18 +136,27 @@ private boolean touchEventOnSprites(TouchHelperEvent event, float frameDuration)
}

protected void onTouchEvent(TouchHelperEvent event) {
if (mListener != null) {
mListener.onTouchEvent(this, event);
}
}

@Override
protected void acquireResources() {
super.acquireResources();
activateTouch(true); // TODO: only if necessary
if (mListener != null) {
mListener.onPrepareView(this);
}
}

@Override
protected void releaseResources() {
super.releaseResources();
mTouchHelper = null;
if (mListener != null) {
mListener.onReleaseView(this);
}
}

public void activateTouch(boolean activate) {
Expand All @@ -139,4 +167,10 @@ public void activateTouch(boolean activate) {
}
}

@Override
protected void onViewResized(int width, int height) {
if (mListener != null) {
mListener.onResizeView(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package fr.arnaudguyon.smartgl.opengl;

import fr.arnaudguyon.smartgl.touch.TouchHelperEvent;

/**
* Created by arnaud on 19/11/2016.
*/

public interface SmartGLViewController {
void onPrepareView(SmartGLView smartGLView);
void onReleaseView(SmartGLView smartGLView);
void onResizeView(SmartGLView smartGLView);
void onTick(SmartGLView smartGLView);
void onTouchEvent(SmartGLView smartGLView, TouchHelperEvent event);
}
4 changes: 2 additions & 2 deletions smartglapp/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "fr.arnaudguyon.smartglapp"
minSdkVersion 14
targetSdkVersion 24
versionCode 5
versionName "0.9.5"
versionCode 6
versionName "0.9.6"
}
buildTypes {
release {
Expand Down
Loading

0 comments on commit 6d23aea

Please sign in to comment.