1 /*******************************************************************************
\r
2 * Copyright 2011 See AUTHORS file.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
15 ******************************************************************************/
\r
17 package com.badlogic.gdx.backends.ios;
\r
19 import cli.MonoTouch.Foundation.ExportAttribute;
\r
20 import cli.MonoTouch.Foundation.NSSet;
\r
21 import cli.MonoTouch.CoreAnimation.CAEAGLLayer;
\r
22 import cli.MonoTouch.ObjCRuntime.Selector;
\r
23 import cli.MonoTouch.OpenGLES.EAGLColorFormat;
\r
24 import cli.MonoTouch.OpenGLES.EAGLRenderingAPI;
\r
25 import cli.MonoTouch.UIKit.UIDevice;
\r
26 import cli.MonoTouch.UIKit.UIEvent;
\r
27 import cli.MonoTouch.UIKit.UIScreen;
\r
28 import cli.MonoTouch.UIKit.UIUserInterfaceIdiom;
\r
29 import cli.OpenTK.FrameEventArgs;
\r
30 import cli.OpenTK.Platform.iPhoneOS.iPhoneOSGameView;
\r
31 import cli.System.EventArgs;
\r
32 import cli.System.Drawing.RectangleF;
\r
34 import com.badlogic.gdx.Gdx;
\r
35 import com.badlogic.gdx.Graphics;
\r
36 import com.badlogic.gdx.Graphics.DisplayMode;
\r
37 import com.badlogic.gdx.graphics.GL10;
\r
38 import com.badlogic.gdx.graphics.GL11;
\r
39 import com.badlogic.gdx.graphics.GL20;
\r
40 import com.badlogic.gdx.graphics.GLCommon;
\r
41 import com.badlogic.gdx.graphics.GLU;
\r
42 import com.badlogic.gdx.graphics.Pixmap;
\r
44 // FIXME add GL 1.x support by ripping Android's classes
\r
45 public class IOSGraphics extends iPhoneOSGameView implements Graphics {
\r
47 private static final String tag = "IOSGraphics";
\r
59 BufferFormat bufferFormat;
\r
62 private float ppiX = 0;
\r
63 private float ppiY = 0;
\r
64 private float ppcX = 0;
\r
65 private float ppcY = 0;
\r
66 private float density = 1;
\r
70 public IOSGraphics (RectangleF bounds, IOSApplication app, IOSInput input, GL20 gl20) {
\r
73 // setup view and OpenGL
\r
74 width = (int)bounds.get_Width();
\r
75 height = (int)bounds.get_Height();
\r
76 app.debug(tag, bounds.get_Width() + "x" + bounds.get_Height() + ", " + UIScreen.get_MainScreen().get_Scale());
\r
79 set_LayerRetainsBacking(false);
\r
80 set_ContentScaleFactor(1);
\r
81 set_MultipleTouchEnabled(true);
\r
82 set_AutoResize(false);
\r
83 set_LayerColorFormat(EAGLColorFormat.RGB565);
\r
84 set_ContextRenderingApi(EAGLRenderingAPI.wrap(EAGLRenderingAPI.OpenGLES2));
\r
85 // FIXME fix this if we add rgba/depth/stencil flags to IOSApplicationConfiguration
\r
86 bufferFormat = new BufferFormat(5, 6, 5, 0, 16, 0, 0, false);
\r
89 // determine display density and PPI (PPI values via Wikipedia!)
\r
92 if ((UIScreen.get_MainScreen().RespondsToSelector(new Selector("scale")))) {
\r
93 float scale = UIScreen.get_MainScreen().get_Scale();
\r
94 app.debug(tag, "Calculating density, UIScreen.mainScreen.scale: " + scale);
\r
95 if (scale == 2f) density = 2f;
\r
99 if (UIDevice.get_CurrentDevice().get_UserInterfaceIdiom().Value == UIUserInterfaceIdiom.Pad) {
\r
101 ppi = Math.round(density * 132);
\r
103 // iPhone or iPodTouch
\r
104 ppi = Math.round(density * 163);
\r
108 ppcX = ppiX / 2.54f;
\r
109 ppcY = ppcY / 2.54f;
\r
110 app.debug(tag, "Display: ppi=" + ppi + ", density=" + density);
\r
113 lastFrameTime = System.nanoTime();
\r
114 framesStart = lastFrameTime;
\r
120 protected void ConfigureLayer (CAEAGLLayer layer) {
\r
121 layer.set_Opaque(true);
\r
122 super.ConfigureLayer(layer);
\r
126 protected void OnLoad (EventArgs arg0) {
\r
127 super.OnLoad(arg0);
\r
129 app.listener.create();
\r
132 public void resume() {
\r
136 public void pause() {
\r
141 protected void OnRenderFrame (FrameEventArgs arg0) {
\r
142 super.OnRenderFrame(arg0);
\r
147 long time = System.nanoTime();
\r
148 deltaTime = (time - lastFrameTime) / 1000000000.0f;
\r
149 lastFrameTime = time;
\r
152 if (time - framesStart >= 1000000000l) {
\r
153 framesStart = time;
\r
159 ((IOSInput)Gdx.input).processEvents();
\r
160 app.listener.render();
\r
165 protected void OnUpdateFrame (FrameEventArgs frameEventArgs) {
\r
166 super.OnUpdateFrame(frameEventArgs);
\r
167 app.processRunnables();
\r
171 protected void OnResize (EventArgs event) {
\r
172 super.OnResize(event);
\r
175 // FIXME resize could happen if app supports both portrait and landscape, so this should be implemented
\r
176 Gdx.app.debug("IOSGraphics", "iOS OnResize(...) is not implement (don't think it is needed?).");
\r
179 @ExportAttribute.Annotation("layerClass")
\r
180 static cli.MonoTouch.ObjCRuntime.Class LayerClass () {
\r
181 return iPhoneOSGameView.GetLayerClass();
\r
185 public boolean isGL11Available () {
\r
190 public boolean isGL20Available () {
\r
195 public GLCommon getGLCommon () {
\r
200 public GL10 getGL10 () {
\r
205 public GL11 getGL11 () {
\r
210 public GL20 getGL20 () {
\r
215 public GLU getGLU () {
\r
216 // FIXME implement this
\r
221 public int getWidth () {
\r
226 public int getHeight () {
\r
231 public float getDeltaTime () {
\r
236 public float getRawDeltaTime () {
\r
241 public int getFramesPerSecond () {
\r
246 public GraphicsType getType () {
\r
247 return GraphicsType.iOSGL;
\r
251 public float getPpiX () {
\r
256 public float getPpiY () {
\r
261 public float getPpcX () {
\r
266 public float getPpcY () {
\r
270 /** Returns the display density.
\r
272 * @return 1.0f for non-retina devices, 2.0f for retina devices. */
\r
274 public float getDensity () {
\r
279 public boolean supportsDisplayModeChange () {
\r
284 public DisplayMode[] getDisplayModes () {
\r
285 return new DisplayMode[] {getDesktopDisplayMode()};
\r
289 public DisplayMode getDesktopDisplayMode () {
\r
290 return new IOSDisplayMode(getWidth(), getHeight(), 60, 0);
\r
293 private static class IOSDisplayMode extends DisplayMode {
\r
294 protected IOSDisplayMode (int width, int height, int refreshRate, int bitsPerPixel) {
\r
295 super(width, height, refreshRate, bitsPerPixel);
\r
300 public boolean setDisplayMode (DisplayMode displayMode) {
\r
305 public boolean setDisplayMode (int width, int height, boolean fullscreen) {
\r
310 public void setTitle (String title) {
\r
314 public void setVSync (boolean vsync) {
\r
318 public BufferFormat getBufferFormat () {
\r
319 return bufferFormat;
\r
323 public boolean supportsExtension (String extension) {
\r
324 if (extensions == null) extensions = Gdx.gl.glGetString(GL10.GL_EXTENSIONS);
\r
325 return extensions.contains(extension);
\r
329 public void setContinuousRendering (boolean isContinuous) {
\r
330 // FIXME implement this if possible
\r
334 public boolean isContinuousRendering () {
\r
335 // FIXME implement this if possible
\r
340 public void requestRendering () {
\r
341 // FIXME implement this if possible
\r
345 public boolean isFullscreen () {
\r
350 public void TouchesBegan (NSSet touches, UIEvent event) {
\r
351 super.TouchesBegan(touches, event);
\r
352 input.touchDown(touches, event);
\r
356 public void TouchesCancelled (NSSet touches, UIEvent event) {
\r
357 super.TouchesCancelled(touches, event);
\r
358 input.touchUp(touches, event);
\r
362 public void TouchesEnded (NSSet touches, UIEvent event) {
\r
363 super.TouchesEnded(touches, event);
\r
364 input.touchUp(touches, event);
\r
368 public void TouchesMoved (NSSet touches, UIEvent event) {
\r
369 super.TouchesMoved(touches, event);
\r
370 input.touchMoved(touches, event);
\r