Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 123 lines (96 sloc) 3.261 kb
03f761c Android sample application
Julien Rebetez authored
1 #include "Activity.h"
2
3 #include "Font.h"
4 #include "FTLib.h"
5 #include "FontHelper.h"
6 #include "Circle.h"
7
8 #define GLERR do {int err=glGetError();if (err != 0) LOGE("GL Error at %s:%i : %i", __FILE__, __LINE__, err); } while(0)
9
10 #define SCALE_MIN 50
11 #define SCALE_MAX 100
12
13 class SampleActivity : public Activity {
14 public:
15 SampleActivity (android_app* app) : Activity(app), font(NULL) {
16 helloState.rot = 0;
17 helloState.rotIncr = 30;
18 helloState.scale = 70;
19 helloState.scaleIncr = 10;
20 }
21
22 protected:
23 void postInit () {
24 srand(time(NULL));
25
26 _loadFont();
27 GLERR;
28
29 glMatrixMode(GL_PROJECTION);
30 glLoadIdentity();
31 // glClearColor(0.7f,0.7f,0.7f,0.0f);
32 glClearColor(0.3,0.3,0.3,1);
33 glOrthof(0, getWidth(), getHeight(), 0, -1, 1);
34
35 glMatrixMode(GL_MODELVIEW);
36 glLoadIdentity();
37 glDisable(GL_DEPTH_TEST);
38 glDisable(GL_CULL_FACE);
39 glColor4f(1,1,1,1);
40 glEnable(GL_TEXTURE_2D);
41
42 glEnable(GL_BLEND);
43 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
44
45 glEnableClientState(GL_VERTEX_ARRAY);
46 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
47
48
49 Circle::create(10);
50 GLERR;
51 }
52
53 void drawImpl () {
54 glClear(GL_COLOR_BUFFER_BIT);
55 glColor4f(1,1,1,1);
56
57 glLoadIdentity();
58
59 drawText(font, "This is a rotating/scaling multiline hello world !", 0, 25, 25);
60
61 fontlib::FontHelper::drawCenteredAt(font, "hello\nworld !", helloState.scale, getWidth()/2, getHeight()/2, false, helloState.rot);
62
63 /*glDisable(GL_TEXTURE_2D);
64 glPushMatrix();
65 glColor4f(1,0,0,1);
66 glTranslatef(getWidth()/2, getHeight()/2, 0);
67 glScalef(5,5,1);
68 Circle::draw(false);
69 glPopMatrix();
70 glEnable(GL_TEXTURE_2D);*/
71 }
72
73 void simulate (double elapsedS) {
74 // LOGI("Simulate (%f), rot=%f, scale=%f", elapsedS, helloState.rot, helloState.scale);
75 helloState.rot += helloState.rotIncr*elapsedS;
76 if (helloState.rot > 360)
77 helloState.rot -= 360;
78
79 helloState.scale += helloState.scaleIncr*elapsedS;
80 if (helloState.scale <= SCALE_MIN || helloState.scale >= SCALE_MAX)
81 helloState.scaleIncr *= -1;
82 }
83
84 void drawText (fontlib::Font* font, const char* msg, int x, int y, int size) {
85 glPushMatrix();
86
87 glTranslatef(x, y, 0);
88 font->draw(msg, size, false);
89
90 glPopMatrix();
91 }
92
93 private:
94 void _loadFont () {
95 LOGI("Loading font");
96 AAssetManager* manager = getApp()->activity->assetManager;
97 AAsset* fontFile = AAssetManager_open(manager, "LiberationSans-Bold.ttf", AASSET_MODE_BUFFER);
98 const void* fontData = AAsset_getBuffer(fontFile);
99 off_t fontLen = AAsset_getLength(fontFile);
100 font = fontlib::FTLib::getInstance()->loadMemoryFont((const char*)fontData, fontLen, 30);
101 AAsset_close(fontFile);
102 if (!font) {
103 LOGE("Error loading font");
104 }
105 }
106
107 struct {
108 float rot;
109 float rotIncr;
110 float scale;
111 float scaleIncr;
112 } helloState;
113
114 fontlib::Font* font;
115 };
116
117 void android_main (android_app* app) {
118 app_dummy();
119 LOGI("Running build @ %s (%s)", __TIME__, __DATE__);
120 SampleActivity activity(app);
121 activity.run();
122 }
Something went wrong with that request. Please try again.