SHOW:
|
|
- or go back to the newest paste.
1 | ---Game.java--- | |
2 | ||
3 | package com.yaakov.mars; | |
4 | ||
5 | import com.badlogic.gdx.ApplicationAdapter; | |
6 | import com.badlogic.gdx.assets.AssetManager; | |
7 | import com.badlogic.gdx.Gdx; | |
8 | import com.badlogic.gdx.graphics.GL20; | |
9 | import com.badlogic.gdx.graphics.g3d.ModelBatch; | |
10 | import com.badlogic.gdx.graphics.g3d.Environment; | |
11 | import com.badlogic.gdx.graphics.Color; | |
12 | import com.badlogic.gdx.graphics.PerspectiveCamera; | |
13 | import com.badlogic.gdx.graphics.Texture; | |
14 | import com.badlogic.gdx.graphics.VertexAttributes.Usage; | |
15 | import com.badlogic.gdx.graphics.g3d.Material; | |
16 | import com.badlogic.gdx.graphics.g3d.Model; | |
17 | import com.badlogic.gdx.graphics.g3d.ModelInstance; | |
18 | import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute; | |
19 | import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute; | |
20 | import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder; | |
21 | import com.badlogic.gdx.math.Vector3; | |
22 | import com.yaakov.mars.Martian; | |
23 | import com.yaakov.mars.Render; | |
24 | ||
25 | public class Game extends ApplicationAdapter{ | |
26 | PerspectiveCamera camera; | |
27 | AssetManager assetManager; | |
28 | Render render; | |
29 | ModelBuilder modelBuilder; | |
30 | ModelBatch batch; | |
31 | Model box; | |
32 | ModelInstance boxInstance; | |
33 | Environment environment; | |
34 | Material material; | |
35 | ||
36 | @Override | |
37 | public void create(){ | |
38 | batch = new ModelBatch(); | |
39 | //assetManager = new AssetManager(); | |
40 | render = new Render(); | |
41 | modelBuilder = new ModelBuilder(); | |
42 | ||
43 | //assetManager.load("badlogic.jpg", Texture.class); | |
44 | render.load("badlogic.jpg", Texture.class); | |
45 | while(!render.update()){ | |
46 | float progress = assetManager.getProgress(); | |
47 | System.out.println(progress); | |
48 | } | |
49 | //Texture texture = assetManager.get("badlogic.jpg", Texture.class); | |
50 | material = new Material(TextureAttribute.createDiffuse(texture)); | |
51 | ||
52 | box = modelBuilder.createBox(2f, 2f, 2f, material, Usage.Position|Usage.Normal|Usage.TextureCoordinates); | |
53 | boxInstance = new ModelInstance(box, 0.f, 0.f, 0.f); | |
54 | ||
55 | environment = new Environment(); | |
56 | environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.8f, 0.8f, 0.8f, 1.0f)); | |
57 | //environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1.f, -0.8f, -0.2f)); | |
58 | ||
59 | camera = new PerspectiveCamera(75, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); | |
60 | camera.position.set(0.f, 0.f, 3.f); | |
61 | camera.lookAt(0.f, 0.f, 0.f);; | |
62 | camera.near = 0.1f; | |
63 | camera.far = 300.f; | |
64 | } | |
65 | ||
66 | @Override | |
67 | public void dispose(){ | |
68 | box.dispose(); | |
69 | batch.dispose(); | |
70 | } | |
71 | ||
72 | @Override | |
73 | public void render() { | |
74 | Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); | |
75 | Gdx.gl.glClearColor(1, 1, 1, 1); | |
76 | Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT|GL20.GL_DEPTH_BUFFER_BIT); | |
77 | ||
78 | camera.rotateAround(Vector3.Zero, new Vector3(0.f, 1.f, 0.f), 1.f); | |
79 | camera.update(); | |
80 | ||
81 | batch.begin(camera); | |
82 | batch.render(boxInstance, environment); | |
83 | batch.end(); | |
84 | } | |
85 | ||
86 | @Override | |
87 | public void resize(int width, int height){ | |
88 | } | |
89 | ||
90 | @Override | |
91 | public void pause(){ | |
92 | } | |
93 | ||
94 | @Override | |
95 | public void resume(){ | |
96 | } | |
97 | } | |
98 | ||
99 | --Render.java-- | |
100 | package com.yaakov.mars; | |
101 | ||
102 | import com.badlogic.gdx.assets.AssetManager; | |
103 | ||
104 | public class Render{ | |
105 | private AssetManager manager; | |
106 | ||
107 | - | public void load(String fileName, Class<Object> type){ |
107 | + | public synchronized <T> void load (String fileName, Class<T> type) { |
108 | manager.load(fileName, type); | |
109 | } | |
110 | ||
111 | - | public void get(String fileName, Class<Object> type){ |
111 | + | public synchronized <T> T get (String fileName, Class<T> type) { |
112 | - | manager.get(fileName, type); |
112 | + | return manager.get(fileName, type); |
113 | } | |
114 | ||
115 | public boolean update(){ | |
116 | return manager.update(); | |
117 | } | |
118 | } |