Chicken Hero
Chicken Hero - Project Pilihan
===
9.3 Chicken Hero
9.3.1 Konsep
Chicken Hero adalah sebuah game yang diadopsi dari game Flappy Bird. Pada game ini kita menggunakan ayam sebagai pemeran utama dalam permainan ini. Permainan ini cukup sederhana hanya dengan menekan layar atau meng-klik anda dapat memainkannya dengan baik. Game ini dibuat bertujuan untuk melatih pemain dalam berkonsentrasi dan fokus pada suatu permasalah yang harus dilesesaikannya, dalam game ini permasalahannya adalah Chicken Hero adalah sebuah game yang diadopsi dari game Flappy Bird. Pada game ini kita menggunakan ayam sebagai pemeran utama dalam permainan ini. Permainan ini cukup sederhana hanya dengan menekan layar atau meng-klik anda dapat memainkannya dengan baik. Game ini dibuat bertujuan untuk melatih pemain dalam berkonsentrasi dan fokus pada suatu permasalah yang harus dilesesaikannya, dalam game ini permasalahannya adalah
9.3.2 Fungsi Dasar Pembentuk Objek
Komponen pada pembangunan game edukasi Chicken Hero ini meliputi berbagai fungsi dalam OpenGL antara lain yang paling utama adalah fungsi GL_BLEND. Yaitu untuk membuat objek agar objek ter- sebut berbentuk seperti tekstur mappingnya. Mayoritas dari pemben- tuk game ini memanfaatkan fungsi tersebut. Mulai dari tampilan chick- en, tiang, background, tulisan game over, dan angka score.
Tabel 9.1 Fungsi Dasar Pembentuk Objek
Keterangan Fungsi
gl.glPushMatrix(); gl.glDisable(GL10. GL_LIGHTING );
Untuk membuat
gl.glEnable(GL10. GL_BLEND );
objek agar ber-
bentuk seperti
tekstur yang telah
. gl.glEnable(GL10. GL_LIGHTING );
di blendingkan
gl.glDisable(GL10. GL_BLEND ); gl.glPopMatrix(); gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING )
gl.glEnable(GL10. GL_TEXTURE_2D ) gl.glEnable(GL10. GL_BLEND );
Menampilkan
gl.glDisable(GL10. GL_DEPTH_TEST );
Highscore yang te-
gl.glBlendFunc(GL10. GL_SRC_ALPHA , GL10. GL_ONE_MINUS_SRC_ALPHA );
lah diraih selama
glText .begin(1.0f, 1.0f, 0.0f, 1.0f); glText .draw( "HIghscore :
bermain game
" + highscore , 25, myheight - 25); glText .end(); gl.glLoadIdentity(); glText .begin(1.0f, 1.0f, 0.0f, 1.0f); glText .draw( "" , 25, myheight - 25); glText .end(); gl.glLoadIdentity(); " + highscore , 25, myheight - 25); glText .end(); gl.glLoadIdentity(); glText .begin(1.0f, 1.0f, 0.0f, 1.0f); glText .draw( "" , 25, myheight - 25); glText .end(); gl.glLoadIdentity();
gl.glPopMatrix();
9.3.3 Tampilan Implementasi
OpenGL dalam pembuatan game edukasi “Chicken Hero” ini dapat digunakan untuk mengimplementasikan texture mapping dengan tampilan seekor ayam yang berusaha untuk menghindari rintangan berupa tiang – tiang berada di depannya. Di sini juga terekam hasil skor tertinggi dari keberhasilan seekor ayam melewati rintangan – rintangan yang ada. Di mana semakin banyak rintangan yang dilewati, maka skor akan semakin tinggi.
Gambar 9.15 Tampilan Awal Untuk Memulai Game
Gambar 9.16 Tampilan Game ketika di play
Gambar 9.17 Tampilan Score
9.3.4 Source Code
Source Code 9.3 Code Chicken Hero ESRender.java
package com.mcsentul.flappybirdopengl; package com.mcsentul.flappybirdopengl;
import android.app.Activity; import android.content.Context;
import android.opengl.GLSurfaceView.Renderer; import android.util.Log;
publicclass ESRender implements Renderer { private ObjectBall objectballstick ; private ObjectBall min ; private ObjectBall tiang ; private ObjectBall tiangatas ; private ObjectBall plus ; private ObjectBall gameover ; private ObjectBall play ; private ObjectBall angka0 ; private ObjectBall angka1 ; private ObjectBall angka2 ; private ObjectBall angka3 ; private ObjectBall angka4 ; private ObjectBall angka5 ; private ObjectBall angka6 ; private ObjectBall angka7 ; private ObjectBall angka8 ; private ObjectBall angka9 ; private ObjectBall tap ;
private ObjectBall burung1 ; private ObjectBall burung2 ; private ObjectBall burung3 ; private ObjectBall burung4 ; private ObjectBall burung5 ; private ObjectBall burung6 ; private ObjectBall burung7 ; private ObjectBall burung8 ;
private ObjectBall objectballbgstick , background ;
private ESText glText ; boolean jatuh ; int tiang_ke ; int score ; int batas_atas , batas_bawah ; Context context ; int Run_Mode = 0; float CurrentAngle = 0.0f; // Angle in degrees
float AnimateStep = 0.2f; // Rotation step per up- date
boolean tiang1 , tiang2 , tiang3 , tiang4 , tiang5 , ganti_posisi , tiang6 , tiang7 , tiang8 , tiang9 , tiang10 , mati , main , tampil_score ; float ScaleStep = 1.0f; int state ; boolean tiang1 , tiang2 , tiang3 , tiang4 , tiang5 , ganti_posisi , tiang6 , tiang7 , tiang8 , tiang9 , tiang10 , mati , main , tampil_score ; float ScaleStep = 1.0f; int state ;
float radius = 50.0f; // Ball's radius float x = radius ; // Ball's center (x,y) float y = radius ; float speedX = 5f; // Ball's speed (x,y) float speedY = 3f;
int posx ; int posy ; int naik ; boolean up , nilaiplus ; float kurang = 1.5f; boolean isTap ; int jalan ; int jalan2 ; boolean run ; float turun = 3.5f; float tambahnaik = 2.2f; int waktu ; boolean reset ; int skalax , skalaz , skalay ; int xMin , xMax , yMin , yMax ; int pos_tiang ; privatefloat move_left_right1 = 0.f; privatefloat move_left_right2 = 0.f; boolean mati_kon ; privatefloat kecepatan_ground = 5.0f; privateint faktor_naik_turun = 0; privateint flag_naik = 1; int translasi_atas ; int translasi_bawah ; privatelong startTime ; privatelong fpsStartTime ; privatelong numFrames ; privatefloat fps_temp ; privatelong fpsElapsed_temp ; privatelong numFrames_temp ; privateboolean startTimeInit = true ; int m =0;
privatelong fpsStartTime2 ; privatelong numFrames2 ; privateint numIDFrame = 0; int highscore ; privateint mywidth = 0; privateint myheight = 0;
privatefloat x_player = 0; privatefloat y_player = 0;
privateint Navigate = 0;
privateint numberRow = 0; privatefloat WidthObject = 0;
privatefloat x_anim_stick = 0; privatefloat y_anim_stick = 0; privatefloat x_anim_stick = 0; privatefloat y_anim_stick = 0;
privatefloat x_pointer = 0; privatefloat y_pointer = 0;
privatefloat x_pointer_awal = 0; privatefloat y_pointer_awal = 0;
privatefloat x_lebar_layar = 0; privatefloat y_tinggi_layar = 0;
privatefloat sudut_pointer = 0;
privatefloat jarak_center = 0;
private String arah_joystick = "" ;
boolean get_size_screen = true ; int n2 = 0;
float black [] = newfloat [] { 0.0f, 0.0f, 0.0f, 1.0f };
float yellow [] = newfloat [] { 1.0f, 1.0f, 0.0f, 1.0f }; float cyan [] = newfloat [] { 0.0f, 1.0f, 1.0f, 1.0f };
float white [] = newfloat [] { 1.0f, 1.0f, 1.0f, 1.0f };
float diffuseMaterial [] = newfloat [] { 0.73f, 0.13f, 0.86f, 1.0f };
float diffuseMaterial2 [] = newfloat [] { 0.5f, 0.5f, 0.5f, 1.0f };
float lightAmbient [] = newfloat [] { 0.2f, 0.3f, 0.6f, 1.0f };
float mat_specular [] = { 1.0f, 1.0f, 1.0f, 1.0f }; float light_position [] = { 0.0f, 0.0f, 1.0f, 0.0f
}; float light_position2 [] = { 0.0f, 0.0f, 1.0f, 0.0f };
/** Constructor to set the handed over context */ public ESRender(Context context) {
// super();
this . context = context;
burung1 = new ObjectBall(); burung2 = new ObjectBall(); burung3 = new ObjectBall(); burung4 = new ObjectBall(); burung5 = new ObjectBall(); burung6 = new ObjectBall(); burung7 = new ObjectBall(); burung8 = new ObjectBall(); gameover = new ObjectBall(); play = new ObjectBall(); angka0 = new ObjectBall(); burung1 = new ObjectBall(); burung2 = new ObjectBall(); burung3 = new ObjectBall(); burung4 = new ObjectBall(); burung5 = new ObjectBall(); burung6 = new ObjectBall(); burung7 = new ObjectBall(); burung8 = new ObjectBall(); gameover = new ObjectBall(); play = new ObjectBall(); angka0 = new ObjectBall();
Ball(); // mengambilnilaixyx (posisiawal) labirin player
@Override publicvoid onDrawFrame(GL10 gl) {
// menangkapukuranlayar if ( get_size_screen ){
this . x_lebar_layar = mywidth ; this . y_tinggi_layar = myheight ; posx = 0; posy = 0; jalan = mywidth ; naik = myheight / 2; jalan2 = mywidth + 200; skalax = 2; skalaz = 2; skalay = 2;
get_size_screen = false ; tiang_ke = 1;
this . translasi_atas = myheight - 200; this . translasi_bawah = -100;
batas_atas = 251; batas_bawah = 133; tiang1 = true ; main = true ; isTap = true ;
// Draw background color gl.glClear(GL10. GL_COLOR_BUFFER_BIT |
GL10. GL_DEPTH_BUFFER_BIT ); gl.glEnable(GL10. GL_DEPTH_TEST ); gl.glEnable(GL10. GL_NORMALIZE ); gl.glMaterialfv(GL10. GL_FRONT ,
GL10. GL_SPECULAR , white , 0);
gl.glMaterialfv(GL10. GL_FRONT ,
GL10. GL_SHININESS , white , 0);
gl.glMaterialfv(GL10. GL_FRONT ,
GL10. GL_DIFFUSE , white , 0);
gl.glLoadIdentity();
// update updateposx(); updateposy();
updateskala(); boolean t= false ; if (t) {
// membuat background joystickdarilingkarandengan texture gl.glPushMatrix(); gl.glDisable(GL10. GL_LIGHTING ); gl.glLineWidth(1.0f); gl.glEnable(GL10. GL_LINE_SMOOTH ); gl.glTranslatef( mywidth - (2 * ra-
dius )- radius , 25, 0.0f);
objectballbg-
stick .draw_circle_color(gl); gl.glEnable(GL10. GL_LIGHTING ); gl.glPopMatrix();
// membuatjoystickdarilingkarandengan texture gl.glPushMatrix(); gl.glDisable(GL10. GL_LIGHTING ); gl.glLineWidth(1.0f); gl.glEnable(GL10. GL_LINE_SMOOTH ); gl.glTranslatef( x_anim_stick ,
y_anim_stick , 0.0f); gl.glTranslatef( mywidth - (1.5f * radius )- radius , (0.5f * radius ) +( radius / 2), 0.0f); gl.glScalef(0.5f, 0.5f, 1.0f); object-
ballstick .draw_circle_color(gl); gl.glEnable(GL10. GL_LIGHTING ); gl.glPopMatrix();
} // background gl.glPushMatrix(); gl.glDisable(GL10. GL_LIGHTING ); gl.glLineWidth(1.0f); // gl.glEnable(GL10.GL_LINE_SMOOTH); // gl.glTranslatef(0, 0, -30.0f); gl.glTranslatef(0.0f, 0.0f, 0.0f); gl.glScalef( mywidth / 4, myheight / 3.0f,
1.0f); // background.drawbg2(gl); background .draw_bg(gl); gl.glEnable(GL10. GL_LIGHTING ); gl.glPopMatrix();
// tiangatas gl.glPushMatrix(); gl.glDisable(GL10. GL_LIGHTING ); gl.glEnable(GL10. GL_BLEND ); gl.glLineWidth(1.0f); // gl.glEnable(GL10. GL_LINE_SMOOTH );
gl.glTranslatef( jalan , translasi_atas , 0.0f); gl.glScalef(9.0f, 90.0f, 1.0f); tiangatas .draw_bg(gl); gl.glEnable(GL10. GL_LIGHTING ); gl.glDisable(GL10. GL_BLEND ); gl.glPopMatrix();
// tiangbawah gl.glPushMatrix(); gl.glDisable(GL10. GL_LIGHTING ); gl.glEnable(GL10. GL_BLEND ); gl.glLineWidth(1.0f); gl.glTranslatef( jalan , translasi_bawah ,
0.0f);
// background.drawbg2(gl); gl.glScalef(9.0f, 80.0f, 1.0f); tiang .draw_bg(gl); gl.glEnable(GL10. GL_LIGHTING ); gl.glDisable(GL10. GL_BLEND ); gl.glPopMatrix();
if ( jatuh == true ){ gl.glPushMatrix(); gl.glDisable(GL10. GL_LIGHTING ); gl.glEnable(GL10. GL_BLEND ); gl.glLineWidth(1.0f);
gl.glTranslatef(( mywidth / 2) - 130, myheight /
2, 0.0f); // background.drawbg2(gl); gl.glScalef(50.0f, 20.0f, 1.0f); gameover .draw_bg(gl); gl.glEnable(GL10. GL_LIGHTING ); gl.glDisable(GL10. GL_BLEND ); gl.glPopMatrix();
gl.glPushMatrix(); gl.glDisable(GL10. GL_LIGHTING ); gl.glEnable(GL10. GL_BLEND ); gl.glLineWidth(1.0f);
gl.glTranslatef(25, 25, 0.0f); // background.drawbg2(gl); gl.glScalef(25.0f, 20.0f, 1.0f); play .draw_bg(gl); gl.glEnable(GL10. GL_LIGHTING ); gl.glDisable(GL10. GL_BLEND ); gl.glPopMatrix();
gl.glPushMatrix(); gl.glDisable(GL10. GL_LIGHTING ); gl.glPushMatrix(); gl.glDisable(GL10. GL_LIGHTING );
// burungdisini
if ( isTap == true ){ gl.glPushMatrix(); gl.glDisable(GL10. GL_LIGHTING ); gl.glEnable(GL10. GL_BLEND ); gl.glLineWidth(1.0f); // gl.glEnable(GL10. GL_LINE_SMOOTH );
gl.glTranslatef(( mywidth / 2)-70, ( myheight / 2)-40, 0.0f); //
gl.glScalef(24.0f, 52.0f, 1.0f); tap .draw_bg(gl); gl.glEnable(GL10. GL_LIGHTING ); gl.glDisable(GL10. GL_BLEND ); gl.glPopMatrix();
} else {
numFrames2 ++; long fpsElapsed2 = Sys-
tem.currentTimeMillis() - fpsStartTime2 ; if (fpsElapsed2 > 0.09 * 1000) { // every 5 seconds float fps2 = ( numFrames2 * 1000.0F) / fpsElapsed2;
fpsStartTime2 = Sys- tem.currentTimeMillis(); numFrames2 = 0; // numIDFrame if ( numIDFrame == 0) {
gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING ); if (( posx >- ( mywidth / 2) && posx <( mywidth / 2)) || ( posy > 0 && posy < myheight )) {
} else { posx = 0; posy = 0;
} gl.glTranslatef(0.0f, naik , 0.0f); gl.glTranslatef( mywidth / 3, 0.0f,
0.0f); gl.glScalef( skalax , skalay , skalaz ); 0.0f); gl.glScalef( skalax , skalay , skalaz );
5.0f);
if ( up == true ){ gl.glRotatef(15, 0.0f, 0.0f, 1.0f); } burung1 .draw_circle_color(gl); gl.glEnable(GL10. GL_LIGHTING ); gl.glPopMatrix(); } elseif ( numIDFrame == 1) {
gl.glPushMatrix(); gl.glDisable(GL10. GL_LIGHTING );
if (( posx > -( mywidth / 2) && posx <( mywidth / 2))|| ( posy > 0 && posy < myheight )) {
} else { posx = 0; posy = 0;
} gl.glTranslatef(0.0f, naik , 0.0f); gl.glTranslatef( mywidth / 3, 0.0f,
0.0f); gl.glScalef( skalax , skalay , skalaz ); gl.glScalef(0.2f, 0.2f, 0.2f); gl.glTranslatef(0.0f, 0.0f, -
5.0f);
if ( up == true ){ gl.glRotatef(15, 0.0f, 0.0f, 1.0f); } bu-
rung2 .draw_circle_color(gl);
gl.glEnable(GL10. GL_LIGHTING ); gl.glPopMatrix();
} elseif ( numIDFrame == 2) { // 3
gl.glPushMatrix(); gl.glDisable(GL10. GL_LIGHTING ); if (( posx >- ( mywidth / 2) && posx <( mywidth / 2)) || ( posy > 0 && posy < myheight )) {
} else { posx = 0; posy = 0;
} gl.glTranslatef(0.0f, naik , 0.0f); gl.glTranslatef( mywidth / 3, 0.0f,
0.0f); 0.0f);
5.0f);
if ( up == true ){ gl.glRotatef(15, 0.0f, 0.0f, 1.0f); } burung3 .draw_circle_color(gl);
gl.glEnable(GL10. GL_LIGHTING ); gl.glPopMatrix();
} elseif ( numIDFrame == 3) { // 4
gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING ); if (( posx >- ( mywidth / 2) && posx <( mywidth / 2)) || ( posy > 0 && posy < myheight )) {
} else { posx = 0; posy = 0;
} gl.glTranslatef(0.0f, naik , 0.0f); gl.glTranslatef( mywidth / 3, 0.0f,
0.0f); gl.glScalef( skalax , skalay , skalaz ); gl.glScalef(0.2f, 0.2f, 0.2f); gl.glTranslatef(0.0f, 0.0f, -
5.0f);
if ( up == true ){ gl.glRotatef(15, 0.0f, 0.0f, 1.0f); } burung4 .draw_circle_color(gl);
gl.glEnable(GL10. GL_LIGHTING ); gl.glPopMatrix();
} elseif ( numIDFrame == 4) { // 5 gl.glPushMatrix(); gl.glDisable(GL10. GL_LIGHTING ); if (( posx > -( mywidth / 2) && posx <
( mywidth / 2)) || ( posy > 0 && posy < myheight )) {
} else { posx = 0; } else { posx = 0;
0.0f); gl.glScalef( skalax , skalay , skalaz ); gl.glScalef(0.2f, 0.2f, 0.2f); gl.glTranslatef(0.0f, 0.0f, -
5.0f);
if ( up == true ){ gl.glRotatef(15, 0.0f, 0.0f, 1.0f); } burung5 .draw_circle_color(gl);
gl.glEnable(GL10. GL_LIGHTING ); gl.glPopMatrix();
} elseif ( numIDFrame == 5) { // 6
gl.glPushMatrix(); gl.glDisable(GL10. GL_LIGHTING ); if (( posx > -( mywidth / 2) && posx <
( mywidth / 2)) || ( posy > 0 && posy < myheight )) { } else {
posx = 0; posy = 0;
} gl.glTranslatef(0.0f, naik , 0.0f); gl.glTranslatef( mywidth / 3, 0.0f,
0.0f); gl.glScalef( skalax , skalay , skalaz ); gl.glScalef(0.2f, 0.2f, 0.2f); gl.glTranslatef(0.0f, 0.0f, -
5.0f);
if ( up == true ){ gl.glRotatef(15, 0.0f, 0.0f,
1.0f); } burung6 .draw_circle_color(gl); gl.glEnable(GL10. GL_LIGHTING ); gl.glPopMatrix(); } elseif ( numIDFrame == 6) {
gl.glPushMatrix(); gl.glDisable(GL10. GL_LIGHTING ); if (( posx > -( mywidth / 2) && posx <
( mywidth / 2)) || ( posy > 0 && posy < myheight )) {
} else { posx = 0; posy = 0; } else { posx = 0; posy = 0;
0.0f); gl.glScalef( skalax , skalay , skalaz ); gl.glScalef(0.2f, 0.2f, 0.2f); gl.glTranslatef(0.0f, 0.0f, -
5.0f);
if ( up == true ){ gl.glRotatef(15, 0.0f, 0.0f, 1.0f); } bu-
rung7 .draw_circle_color(gl);
gl.glEnable(GL10. GL_LIGHTING ); gl.glPopMatrix();
} elseif ( numIDFrame == 7) { // 8
gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING ); if (( posx > -( mywidth / 2) && posx <( mywidth / 2))
|| ( posy >0 && posy < myheight )) {
} else { posx = 0; posy = 0;
} gl.glTranslatef(0.0f, naik , 0.0f); gl.glTranslatef( mywidth / 3, 0.0f,
0.0f); gl.glScalef( skalax , skalay , skalaz ); gl.glScalef(0.2f, 0.2f, 0.2f); gl.glTranslatef(0.0f, 0.0f, -
5.0f);
if ( up == true ){ gl.glRotatef(15, 0.0f, 0.0f, 1.0f); } burung8 .draw_circle_color(gl);
gl.glEnable(GL10. GL_LIGHTING ); gl.glPopMatrix();
} elseif ( n2 > 160) { n2 = 0;
} n2 += 1; } n2 += 1;
if ( flag_naik == 1) { faktor_naik_turun += 2; } elseif ( flag_naik == -1) { faktor_naik_turun - = 2;
if ( faktor_naik_turun % 20 == 0) { flag_naik *= -1;
if ( numIDFrame % 8 == 0) { numIDFrame = 0;
} } else { if ( numIDFrame == 0) { // 1
gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING ); if (( posx >- ( mywidth / 2) && posx <( mywidth / 2)) || ( posy >0 && posy < myheight )) {
} else { posx = 0; posy = 0;
} gl.glTranslatef(0.0f, naik , 0.0f); gl.glTranslatef( mywidth / 3, 0.0f,
0.0f); gl.glScalef( skalax , skalay , skalaz ); gl.glScalef(0.2f, 0.2f, 0.2f); gl.glTranslatef(0.0f, 0.0f, -
5.0f);
if ( up == true ){ gl.glRotatef(15, 0.0f, 0.0f, 1.0f); } burung1 .draw_circle_color(gl);
gl.glEnable(GL10. GL_LIGHTING ); gl.glPopMatrix(); } elseif ( numIDFrame == 1)
gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING ); if (( posx > -( mywidth / 2) && posx <
( mywidth / 2)) || ( posy > 0 && posy < myheight )) {
} else { posx = 0; posy = 0;
} gl.glTranslatef(0.0f, naik , 0.0f); gl.glTranslatef( mywidth / 3, 0.0f,
0.0f); gl.glScalef( skalax , skalay , skalaz ); gl.glScalef(0.2f, 0.2f, 0.2f); gl.glTranslatef(0.0f, 0.0f, -
5.0f);
if ( up == true ){ gl.glRotatef(15, 0.0f, 0.0f,
1.0f); } burung2 .draw_circle_color(gl);
gl.glEnable(GL10. GL_LIGHTING ); gl.glPopMatrix();
} elseif ( numIDFrame == 2) { // 3 gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING ); if (( posx > -( mywidth / 2) && posx < ( mywidth / 2)) || ( posy > 0 && posy < myheight )) {
} else { posx = 0; posy = 0;
} gl.glTranslatef(0.0f, naik , 0.0f); gl.glTranslatef( mywidth / 3, 0.0f,
0.0f); gl.glScalef( skalax , skalay , skalaz ); gl.glScalef(0.2f, 0.2f, 0.2f); gl.glTranslatef(0.0f, 0.0f, -
5.0f);
if ( up == true ){ gl.glRotatef(15, 0.0f, 0.0f, 1.0f); } burung3 .draw_circle_color(gl);
gl.glEnable(GL10. GL_LIGHTING ); gl.glPopMatrix();
} elseif ( numIDFrame == 3) { // 4 } elseif ( numIDFrame == 3) { // 4
gl.glDisable(GL10. GL_LIGHTING ); if (( posx > -( mywidth / 2) && posx < ( mywidth / 2)) || ( posy > 0 && posy < myheight )) { } else { posx = 0; posy = 0;
} gl.glTranslatef(0.0f, naik , 0.0f); gl.glTranslatef( mywidth / 3, 0.0f,
0.0f); gl.glScalef( skalax , skalay , skalaz ); gl.glScalef(0.2f, 0.2f, 0.2f); gl.glTranslatef(0.0f, 0.0f, -
5.0f); if ( up == true ){ gl.glRotatef(15, 0.0f, 0.0f, 1.0f); } burung4 .draw_circle_color(gl);
gl.glEnable(GL10. GL_LIGHTING ); gl.glPopMatrix();
} elseif ( numIDFrame == 4) { // 5
gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING ); if (( posx > -( mywidth / 2) && posx < ( mywidth / 2)) || ( posy > 0 && posy < myheight )) { } else { posx = 0; posy = 0;
} gl.glTranslatef(0.0f, naik , 0.0f); gl.glTranslatef( mywidth / 3, 0.0f,
0.0f); gl.glScalef( skalax , skalay , skalaz ); gl.glScalef(0.2f, 0.2f, 0.2f); gl.glTranslatef(0.0f, 0.0f, -
5.0f);
if ( up == true ){ gl.glRotatef(15, 0.0f, 0.0f, 1.0f); }
bu-
rung5 .draw_circle_color(gl);
gl.glEnable(GL10. GL_LIGHTING ); gl.glPopMatrix();
} elseif ( numIDFrame == 5) { // 6 gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING ); if (( posx > -( mywidth / 2) && posx < ( mywidth / 2)) || ( posy > 0 && posy < myheight )) { } else { posx = 0; posy = 0;
} gl.glTranslatef(0.0f, naik , 0.0f); gl.glTranslatef( mywidth / 3, 0.0f,
0.0f); gl.glScalef( skalax , skalay , skalaz ); gl.glScalef(0.2f, 0.2f, 0.2f); gl.glTranslatef(0.0f, 0.0f, -
5.0f);
if ( up == true ){ gl.glRotatef(15, 0.0f, 0.0f,
1.0f); } burung6 .draw_circle_color(gl);
gl.glEnable(GL10. GL_LIGHTING ); gl.glPopMatrix();
} elseif ( numIDFrame == 6) { // 7 gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING ); if (( posx > -( mywidth / 2) && posx < ( mywidth / 2)) || ( posy > 0 && posy < myheight )) { } else { posx = 0; posy = 0;
} gl.glTranslatef(0.0f, naik , 0.0f); gl.glTranslatef( mywidth / 3, 0.0f,
0.0f); gl.glScalef( skalax , skalay , skalaz ); gl.glScalef(0.2f, 0.2f, 0.2f); gl.glTranslatef(0.0f, 0.0f, -
5.0f);
if ( up == true ){ gl.glRotatef(15, 0.0f, 0.0f, 1.0f); } burung7 .draw_circle_color(gl);
gl.glEnable(GL10. GL_LIGHTING ); gl.glPopMatrix();
} elseif ( numIDFrame == 7) { // 8
gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING ); if (( posx > -( mywidth / 2) && posx < ( mywidth / 2)) || ( posy > 0 && posy < myheight )) {
} else { posx = 0; posy = 0;
} gl.glTranslatef(0.0f, naik , 0.0f); gl.glTranslatef( mywidth / 3, 0.0f,
0.0f); gl.glScalef( skalax , skalay , skalaz ); gl.glScalef(0.2f, 0.2f, 0.2f); gl.glTranslatef(0.0f, 0.0f, -
5.0f);
if ( up == true ){ gl.glRotatef(15, 0.0f, 0.0f, 1.0f); } bu-
rung8 .draw_circle_color(gl);
gl.glEnable(GL10. GL_LIGHTING ); gl.glPopMatrix();
} elseif ( n2 > 160) { n2 = 0;
} n2 += 1;
} } boolean n= false ; if (!n) {
gl.glPushMatrix(); gl.glDisable(GL10. GL_LIGHTING ); gl.glEnable(GL10. GL_TEXTURE_2D );
// Enable Texture Mapping gl.glEnable(GL10. GL_BLEND ); // En- able Alpha Blend gl.glDisable(GL10. GL_DEPTH_TEST ); // Turn depth testing off (NEW) gl.glBlendFunc(GL10. GL_SRC_ALPHA , GL10. GL_ONE_MINUS_SRC_ALPHA ); // Set
glText .begin(1.0f, 1.0f, 0.0f, 1.0f); glText .draw( "HIghscore : " + highscore , 25, myheight - 25); glText .end(); // End Text Render- glText .begin(1.0f, 1.0f, 0.0f, 1.0f); glText .draw( "HIghscore : " + highscore , 25, myheight - 25); glText .end(); // End Text Render-
glText .begin(1.0f, 1.0f, 0.0f, 1.0f); // Draw Test String glText .draw( "" , 25, myheight -
25); glText .end(); // End Text Render- ing gl.glLoadIdentity(); // disable texture + alpha gl.glEnable(GL10. GL_LIGHTING );
gl.glDisable(GL10. GL_BLEND ); // Disable Alpha Blend gl.glDisable(GL10. GL_TEXTURE_2D ); // Disable Texture Mapping gl.glPopMatrix(); } if ( jalan >= 170 && jalan <= 180) {
// ganti_posisi = true ; nilaiplus = true ;
} if ( jalan >= 65 && jalan <= 70 && nilaiplus
== true ){ nilaiplus = false ; score += 1; Music.playOnce( context ,
R.raw. sfx_point );
if ( tampil_score == true ){
String s = Inte-
ger.toString( score ); int len = s.length();
for ( int
i = 0; i < len; i++) { char scor = s.charAt(i);
int tampilscore = Charac- ter.getNumericValue(scor); if (tampilscore == 0) {
gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING );
gl.glEnable(GL10. GL_BLEND );
gl.glLineWidth(1.0f); //
gl.glEnable(GL10. GL_LINE_SMOOTH ); // gl.glTranslatef(0, 0,
// - 30.0f); // - 30.0f);
gl.glScalef(6.0f, 14.0f, 1.0f); angka0 .draw_bg(gl);
gl.glEnable(GL10. GL_LIGHTING );
gl.glDisable(GL10. GL_BLEND ); gl.glPopMatrix(); } elseif (tampilscore ==
1) { gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING );
gl.glEnable(GL10. GL_BLEND );
gl.glLineWidth(1.0f); //
gl.glEnable(GL10. GL_LINE_SMOOTH ); gl.glTranslatef((( mywidth / 2) - 30) + 27 * i,
370, 0.0f); // gl.glScalef(6.0f, 14.0f, 1.0f); angka1 .draw_bg(gl);
gl.glEnable(GL10. GL_LIGHTING );
gl.glDisable(GL10. GL_BLEND ); gl.glPopMatrix(); } elseif (tampilscore ==
2) { gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING );
gl.glEnable(GL10. GL_BLEND );
gl.glLineWidth(1.0f); //
gl.glEnable(GL10. GL_LINE_SMOOTH ); gl.glTranslatef((( mywidth / 2) -
30) + 27 * i, 370, 0.0f); //
gl.glScalef(6.0f, 14.0f, 1.0f); angka2 .draw_bg(gl);
gl.glEnable(GL10. GL_LIGHTING );
gl.glDisable(GL10. GL_BLEND ); gl.glPopMatrix(); } elseif (tampilscore ==
3) { gl.glPushMatrix(); 3) { gl.glPushMatrix();
gl.glEnable(GL10. GL_BLEND );
gl.glLineWidth(1.0f); //
gl.glEnable(GL10. GL_LINE_SMOOTH );
gl.glTranslatef((( mywidth / 2) - 30) + 27 * i, 370, 0.0f); // gl.glScalef(6.0f, 14.0f, 1.0f); angka3 .draw_bg(gl);
gl.glEnable(GL10. GL_LIGHTING );
gl.glDisable(GL10. GL_BLEND ); gl.glPopMatrix(); } elseif (tampilscore ==
4) { gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING );
gl.glEnable(GL10. GL_BLEND );
gl.glLineWidth(1.0f); //
gl.glEnable(GL10. GL_LINE_SMOOTH );
gl.glTranslatef((( mywidth / 2) - 30) + 27 * i, 370, 0.0f); //
gl.glScalef(6.0f, 14.0f, 1.0f); angka4 .draw_bg(gl);
gl.glEnable(GL10. GL_LIGHTING );
gl.glDisable(GL10. GL_BLEND ); gl.glPopMatrix(); } elseif (tampilscore ==
5) { gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING );
gl.glEnable(GL10. GL_BLEND );
gl.glLineWidth(1.0f); //
gl.glEnable(GL10. GL_LINE_SMOOTH );
gl.glTranslatef((( mywidth / 2) - 30) + 27 * i, 370, 0.0f); //
gl.glScalef(6.0f, 14.0f, 1.0f); angka5 .draw_bg(gl); gl.glScalef(6.0f, 14.0f, 1.0f); angka5 .draw_bg(gl);
gl.glDisable(GL10. GL_BLEND ); gl.glPopMatrix(); } elseif (tampilscore ==
6) { gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING );
gl.glEnable(GL10. GL_BLEND );
gl.glLineWidth(1.0f); //
gl.glEnable(GL10. GL_LINE_SMOOTH );
gl.glTranslatef((( mywidth / 2) - 30) + 27 * i, 370, 0.0f); //
gl.glScalef(6.0f, 14.0f, 1.0f); angka6 .draw_bg(gl);
gl.glEnable(GL10. GL_LIGHTING );
gl.glDisable(GL10. GL_BLEND ); gl.glPopMatrix(); } elseif (tampilscore ==
7) { gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING );
gl.glEnable(GL10. GL_BLEND );
gl.glLineWidth(1.0f); //
gl.glEnable(GL10. GL_LINE_SMOOTH );
gl.glTranslatef((( mywidth / 2) - 30) + 27 * i, 370, 0.0f); //
gl.glScalef(6.0f, 14.0f, 1.0f); angka7 .draw_bg(gl);
gl.glEnable(GL10. GL_LIGHTING );
gl.glDisable(GL10. GL_BLEND ); gl.glPopMatrix(); } elseif (tampilscore ==
8) { gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING );
gl.glEnable(GL10. GL_BLEND ); gl.glEnable(GL10. GL_BLEND );
gl.glEnable(GL10. GL_LINE_SMOOTH );
gl.glTranslatef((( mywidth / 2) - 30) + 27 * i, 370, 0.0f); //
gl.glScalef(6.0f, 14.0f, 1.0f); angka8 .draw_bg(gl);
gl.glEnable(GL10. GL_LIGHTING );
gl.glDisable(GL10. GL_BLEND ); gl.glPopMatrix(); } elseif (tampilscore ==
9) { gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING );
gl.glEnable(GL10. GL_BLEND );
gl.glLineWidth(1.0f); //
gl.glEnable(GL10. GL_LINE_SMOOTH );
gl.glTranslatef((( mywidth / 2) - 30) + 27 * i, 370, 0.0f); //
gl.glScalef(6.0f, 14.0f, 1.0f); angka9 .draw_bg(gl);
gl.glEnable(GL10. GL_LIGHTING );
gl.glDisable(GL10. GL_BLEND ); gl.glPopMatrix();
} } // /score akhir if ( tampil_score == false ){
String s = Inte-
ger.toString( score ); int len = s.length();
for ( int
i = 0; i < len; i++) { char scor = s.charAt(i);
int tampilscore = Charac- ter.getNumericValue(scor); if (tampilscore == 0) {
gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING );
gl.glEnable(GL10. GL_BLEND ); gl.glEnable(GL10. GL_BLEND );
gl.glEnable(GL10. GL_LINE_SMOOTH );
gl.glTranslatef((( mywidth / 2) - 30) + 27 * i, 360, 0.0f); //
gl.glScalef(6.0f, 14.0f, 1.0f); angka0 .draw_bg(gl);
gl.glEnable(GL10. GL_LIGHTING );
gl.glDisable(GL10. GL_BLEND ); gl.glPopMatrix(); } elseif (tampilscore ==
1) { gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING );
gl.glEnable(GL10. GL_BLEND );
gl.glLineWidth(1.0f); //
gl.glEnable(GL10. GL_LINE_SMOOTH );
gl.glTranslatef((( mywidth / 2) - 30) + 27 * i, 360, 0.0f); //
gl.glScalef(6.0f, 14.0f, 1.0f); angka1 .draw_bg(gl);
gl.glEnable(GL10. GL_LIGHTING );
gl.glDisable(GL10. GL_BLEND ); gl.glPopMatrix(); } elseif (tampilscore ==
2) { gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING );
gl.glEnable(GL10. GL_BLEND );
gl.glLineWidth(1.0f); //
gl.glEnable(GL10. GL_LINE_SMOOTH );
gl.glTranslatef((( mywidth / 2) - 30) + 27 * i, 360, 0.0f); //
gl.glScalef(6.0f, 14.0f, 1.0f); angka2 .draw_bg(gl);
gl.glEnable(GL10. GL_LIGHTING ); gl.glEnable(GL10. GL_LIGHTING );
3) { gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING );
gl.glEnable(GL10. GL_BLEND );
gl.glLineWidth(1.0f); //
gl.glEnable(GL10. GL_LINE_SMOOTH );
gl.glTranslatef((( mywidth / 2) - 30) + 27 * i, 360, 0.0f); //
gl.glScalef(6.0f, 14.0f, 1.0f); angka3 .draw_bg(gl);
gl.glEnable(GL10. GL_LIGHTING );
gl.glDisable(GL10. GL_BLEND ); gl.glPopMatrix(); } elseif (tampilscore ==
4) { gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING );
gl.glEnable(GL10. GL_BLEND );
gl.glLineWidth(1.0f); //
gl.glEnable(GL10. GL_LINE_SMOOTH );
gl.glTranslatef((( mywidth / 2) - 30) + 27 * i, 360, 0.0f); //
gl.glScalef(6.0f, 14.0f, 1.0f); angka4 .draw_bg(gl);
gl.glEnable(GL10. GL_LIGHTING );
gl.glDisable(GL10. GL_BLEND ); gl.glPopMatrix(); } elseif (tampilscore ==
5) { gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING );
gl.glEnable(GL10. GL_BLEND );
gl.glLineWidth(1.0f); // gl.glLineWidth(1.0f); //
gl.glTranslatef((( mywidth / 2) - 30) + 27 * i, 360, 0.0f); //
gl.glScalef(6.0f, 14.0f, 1.0f); angka5 .draw_bg(gl);
gl.glEnable(GL10. GL_LIGHTING );
gl.glDisable(GL10. GL_BLEND ); gl.glPopMatrix(); } elseif (tampilscore ==
6) { gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING );
gl.glEnable(GL10. GL_BLEND );
gl.glLineWidth(1.0f); //
gl.glEnable(GL10. GL_LINE_SMOOTH ); // gl.glTranslatef(0, 0,
// - 30.0f);
gl.glTranslatef((( mywidth / 2) - 30) + 27 * i, 360, 0.0f); //
gl.glScalef(6.0f, 14.0f, 1.0f); angka6 .draw_bg(gl);
gl.glEnable(GL10. GL_LIGHTING );
gl.glDisable(GL10. GL_BLEND ); gl.glPopMatrix(); } elseif (tampilscore ==
7) { gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING );
gl.glEnable(GL10. GL_BLEND );
gl.glLineWidth(1.0f); //
gl.glEnable(GL10. GL_LINE_SMOOTH );
gl.glTranslatef((( mywidth / 2) - 30) + 27 * i, 360, 0.0f); //
gl.glScalef(6.0f, 14.0f, 1.0f); angka7 .draw_bg(gl); gl.glScalef(6.0f, 14.0f, 1.0f); angka7 .draw_bg(gl);
gl.glDisable(GL10. GL_BLEND ); gl.glPopMatrix(); } elseif (tampilscore ==
8) { gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING );
gl.glEnable(GL10. GL_BLEND );
gl.glLineWidth(1.0f); //
gl.glEnable(GL10. GL_LINE_SMOOTH );
gl.glTranslatef((( mywidth / 2) - 30) + 27 * i, 360, 0.0f); //
gl.glScalef(6.0f, 14.0f, 1.0f); angka8 .draw_bg(gl);
gl.glEnable(GL10. GL_LIGHTING );
gl.glDisable(GL10. GL_BLEND ); gl.glPopMatrix(); } elseif (tampilscore ==
9) { gl.glPushMatrix();
gl.glDisable(GL10. GL_LIGHTING );
gl.glEnable(GL10. GL_BLEND );
gl.glLineWidth(1.0f); //
gl.glEnable(GL10. GL_LINE_SMOOTH );
gl.glTranslatef((( mywidth / 2) - 30) + 27 * i, 360, 0.0f); //
gl.glScalef(6.0f, 14.0f, 1.0f); angka9 .draw_bg(gl);
gl.glEnable(GL10. GL_LIGHTING );
gl.glDisable(GL10. GL_BLEND ); gl.glPopMatrix();
} } // end score akhir
if ( jalan >= -80 && jalan <= -73
&& ganti_posisi == true ){
if ( tiang1 == true ){ if ( tiang1 == true ){
myheight - 140; // translasi_bawah = -20; this . translasi_atas =
myheight - 200; this . translasi_bawah =- 100; tiang1 = false ; tiang2 = true ;
} elseif ( tiang2 == true ){ tiang_ke = 2; batas_atas = 360; batas_bawah = 220; this . translasi_atas =
myheight - 100; this . translasi_bawah = 0; tiang3 = true ; tiang2 = false ;
} elseif ( tiang3 == true ){ tiang_ke = 3; batas_atas = 250; batas_bawah = 100; this . translasi_atas =
myheight - 220; this . translasi_bawah =- 120; tiang3 = false ; tiang4 = true ;
} elseif ( tiang4 == true ){ tiang_ke = 4; batas_atas = 340; batas_bawah = 205; this . translasi_atas =
myheight - 120; this . translasi_bawah =- 20; tiang4 = false ; tiang5 = true ;
} elseif ( tiang5 == true ){ tiang_ke = 5; this . translasi_atas =
myheight - 250; this . translasi_bawah =- 150; batas_atas = 210; batas_bawah = 80; tiang5 = false ; tiang6 = true ;
} elseif ( tiang6 == true ){ batas_atas = 220; batas_bawah = 80; tiang_ke = 6; this . translasi_atas =
myheight - 200; this . translasi_bawah =- 100; myheight - 200; this . translasi_bawah =- 100;
} elseif ( tiang7 == true ){ tiang_ke = 7; batas_atas = 280; batas_bawah = 130; this . translasi_atas =
myheight - 190; this . translasi_bawah =- 90; tiang8 = true ; tiang7 = false ;
} elseif ( tiang8 == true ){ tiang_ke = 8;
this . translasi_atas = myheight - 100; this . translasi_bawah = 0; batas_atas = 370; batas_bawah = 220; tiang8 = false ; tiang9 = true ;
} elseif ( tiang9 == true ){ tiang_ke = 9; this . translasi_atas =
myheight - 150; this . translasi_bawah =- 50; batas_atas = 320; batas_bawah = 170; tiang9 = false ; tiang10 = true ;
} elseif ( tiang10 == true ){ tiang_ke = 10; this . translasi_atas =
myheight - 250; this . translasi_bawah =- 150; batas_atas = 210; batas_bawah = 70; tiang10 = false ; tiang1 = true ;
} ganti_posisi = false ;
} if ( jalan >= 65 && jalan <= 138 && naik <= ba-
tas_bawah ){
run = false ; jatuh = true ;
} if ( jalan >= 65 && jalan <= 138 && naik >= ba-
tas_atas ){
run = false ; jatuh = true ;
} if ( naik <= 10) {
run = false ; jatuh = true ; run = false ; jatuh = true ;
} if ( state == 2) {
Music.playOnce( context , R.raw. sfx_hit ); tampil_score = false ; mati = false ;
} if ( run ){
main = true ; tampil_score = true ; if ( up == true && ( waktu <= 150
&& waktu > 0)) { if ( naik <= y_tinggi_layar - 60 && naik >= 0) { naik += tambahnaik ;
} waktu --;
} elseif ( up == false && waktu == 0 && naik >= 0) { naik -= turun ; }
if ( waktu == 0) { up = false ; }
if ( jalan >= -90) { jalan -= kurang ; } else { jalan = mywidth ; }
if ( jalan2 >= -300) { jalan2 -= kurang ; } else { jalan2 = mywidth + 200; }
} if ( reset == true ){
jalan = mywidth ; naik = myheight / 2; reset = false ; isTap = true ; jatuh = false ; score = 0; state = 0; mati = false ; translasi_atas = myheight - 140; translasi_bawah = -50; batas_atas = 305; batas_bawah = 165; tiang5 = false ; jalan = mywidth ; naik = myheight / 2; reset = false ; isTap = true ; jatuh = false ; score = 0; state = 0; mati = false ; translasi_atas = myheight - 140; translasi_bawah = -50; batas_atas = 305; batas_bawah = 165; tiang5 = false ;
} CurrentAngle += AnimateStep ; if ( CurrentAngle > 360.0) {
CurrentAngle = 0.0f; CurrentAngle += AnimateStep ;
CurrentScale += ScaleStep ; if ( CurrentScale % 10.0f == 0) {
ScaleStep = -1 * ScaleStep ; } if ( score > highscore ){
highscore = score ; } } @Override publicvoid onSurfaceCreated(GL10 gl, EGLConfig
config) { gl.glClearColor(0.5f, 0.5f, 0.5f, 1.0f); gl.glClearDepthf(1.0f);
gl.glEnable(GL10. GL_DEPTH_TEST ); // surface removal gl.glDepthFunc(GL10. GL_LEQUAL ); // The
type of depth testing to do
gl.glHint(GL10. GL_PERSPECTIVE_CORRECTION_HINT , GL10. GL_NICEST ); gl.glShadeModel(GL10. GL_SMOOTH ); // Ena- ble smooth shading of color gl.glDisable(GL10. GL_DITHER ); // Disable dithering for better // performance glText = new ESText(gl, con-
text .getAssets()); glText .load( "Roboto-Regular.ttf" , 14, 2, 2); // Create Font (Height: 14
gl.glEnable(GL10. GL_LIGHTING ); gl.glEnable(GL10. GL_LIGHT0 ); gl.glColor4f(1.0f, 1.0f, 1.0f, 0.5f); //
Full brightness, 50% alpha // (NEW) gl.glBlendFunc(GL10. GL_SRC_ALPHA , GL10. GL_ONE ); // Select blending min .loadBallTexture(gl, context , 6); background .loadbg(gl, context , 7); tiang .loadbg(gl, context , 8); tiangatas .loadbg(gl, context , 9); play .loadBallTexture(gl, context , 20); angka0 .loadAngkaTexture(gl, context , 0); angka1 .loadAngkaTexture(gl, context , 1); angka2 .loadAngkaTexture(gl, context , 2); angka3 .loadAngkaTexture(gl, context , 3); angka4 .loadAngkaTexture(gl, context , 4); angka5 .loadAngkaTexture(gl, context , 5); angka6 .loadAngkaTexture(gl, context , 6); angka7 .loadAngkaTexture(gl, context , 7); angka8 .loadAngkaTexture(gl, context , 8); Full brightness, 50% alpha // (NEW) gl.glBlendFunc(GL10. GL_SRC_ALPHA , GL10. GL_ONE ); // Select blending min .loadBallTexture(gl, context , 6); background .loadbg(gl, context , 7); tiang .loadbg(gl, context , 8); tiangatas .loadbg(gl, context , 9); play .loadBallTexture(gl, context , 20); angka0 .loadAngkaTexture(gl, context , 0); angka1 .loadAngkaTexture(gl, context , 1); angka2 .loadAngkaTexture(gl, context , 2); angka3 .loadAngkaTexture(gl, context , 3); angka4 .loadAngkaTexture(gl, context , 4); angka5 .loadAngkaTexture(gl, context , 5); angka6 .loadAngkaTexture(gl, context , 6); angka7 .loadAngkaTexture(gl, context , 7); angka8 .loadAngkaTexture(gl, context , 8);
burung1 .loadBallTexture(gl, context , 10); burung2 .loadBallTexture(gl, context , 11); burung3 .loadBallTexture(gl, context , 12); burung4 .loadBallTexture(gl, context , 13); burung5 .loadBallTexture(gl, context , 14); burung6 .loadBallTexture(gl, context , 15); burung7 .loadBallTexture(gl, context , 16); burung8 .loadBallTexture(gl, context , 17); gameover .loadBallTexture(gl, context ,
tap .loadBallTexture(gl, context , 21); gl.glEnable(GL10. GL_TEXTURE_2D ); // Ena-
ble texture (NEW) }
@Override publicvoid onSurfaceChanged(GL10 gl, int width,
int height) { mywidth = width; myheight = height;
gl.glViewport(0, 0, width, height); gl.glMatrixMode(GL10. GL_PROJECTION );
gl.glLoadIdentity(); // Load Identity Matrix
gl.glOrthof( // Set Ortho Projection (Left,Right,Bottom,Top,Front,Back)
0, width, 0, height, 500.0f, -500.0f); gl.glMatrixMode(GL10. GL_MODELVIEW ); // Select model-view matrix
gl.glLoadIdentity(); // Reset
} publicint updateskala() {
re-
turnthis . skalax & this . skalay & this . skalaz ; } publicfloat getX_touch() {
return x_touch ; } publicint getsNaik() {
returnthis . naik ; }
publicvoid setNaik( int x) { this . naik = x; }
publicvoid setJalan() { this . isTap = false ; this . run = true ;
publicvoid setJalanStop() { this . run = false ; } publicvoid setJalanStop() { this . run = false ; }
publicint getskalax() { returnthis . skalax ; }
publicint getskalay() { returnthis . skalay ; }
publicint getskalaz() { returnthis . skalaz ; }
publicvoid setSkala( int x, int y, int z) { this . skalax = x; this . skalay = y; this . skalaz = z;
publicint getposx() { return posx ; }
publicint getposy() { return posy ; }
publicvoid setposx( int x) { this . pos_tiang = x; // posy=y;
publicvoid setposy( int y) { // posx=x; this . posy = y;
publicint updateposx() {
returnthis . posx ; }
publicint updateposy() {
returnthis . posy ; }
publicvoid setNaikku() { this . waktu = 35; this . up = true ;
publicvoid setX_touch( float x_touch) { this . x_touch = x_touch; publicvoid setX_touch( float x_touch) { this . x_touch = x_touch;
publicvoid setY_touch( float y_touch) { this . y_touch = y_touch; }
public String getArah_joystick() { return arah_joystick ; }
publicvoid setArah_joystick(String arah_joystick) { this . arah_joystick = arah_joystick; }
publicfloat getJarak_center() { return jarak_center ; }
publicvoid setJarak_center( float jarak_center) { this . jarak_center = jarak_center; }
publicfloat getSudut_pointer() { return sudut_pointer ; }
publicvoid setSudut_pointer( float sudut_pointer) { this . sudut_pointer = sudut_pointer; }
publicfloat getX_lebar_layar() { return x_lebar_layar ; }
publicvoid setX_lebar_layar( float x_lebar_layar) { this . x_lebar_layar = x_lebar_layar; }
publicfloat getY_tinggi_layar() { return y_tinggi_layar ; }
publicvoid setY_tinggi_layar( float y_tinggi_layar) { this . y_tinggi_layar = y_tinggi_layar; }
publicfloat getX_pointer_awal() { return x_pointer_awal ; }
publicvoid setX_pointer_awal( float x_pointer_awal) { this . x_pointer_awal = x_pointer_awal; publicvoid setX_pointer_awal( float x_pointer_awal) { this . x_pointer_awal = x_pointer_awal;
publicvoid setY_pointer_awal( float y_pointer_awal) { this . y_pointer_awal = y_pointer_awal; }
publicfloat getX_pointer() { return x_pointer ; }
publicvoid setX_pointer( float x_pointer) { this . x_pointer = x_pointer;
publicfloat getY_pointer() { return y_pointer ; }
publicvoid setY_pointer( float y_pointer) { this . y_pointer = y_pointer;
publicfloat getX_anim_stick() { return x_anim_stick ; }
publicvoid setX_anim_stick( float x_anim_stick) { this . x_anim_stick = x_anim_stick; }
publicfloat getY_anim_stick() { return y_anim_stick ; }
publicvoid setY_anim_stick( float y_anim_stick) { this . y_anim_stick = y_anim_stick; }
publicfloat getRadius() { return radius ; }
publicvoid setRadius( float radius) { this . radius = radius; }
publicint getMywidth() { return mywidth ; }
publicvoid setMywidth( int mywidth) { this . mywidth = mywidth; } publicvoid setMywidth( int mywidth) { this . mywidth = mywidth; }
publicvoid setMyheight( int myheight) { this . myheight = myheight; }
publicint getNumberRow() { return numberRow ; }
publicvoid setNumberRow( int numberRow) {
this . numberRow = numberRow;
publicfloat getWidthObject() { return WidthObject ; }
publicvoid setWidthObject( float widthObject) {
WidthObject = widthObject;
publicfloat getX_player() { return x_player ; }
publicvoid setX_player( float x_player) { this . x_player = x_player; }
publicfloat getY_player() { return y_player ; }
publicboolean getRun() { returnthis . run ; }
publicboolean getJatuh() { returnthis . jatuh ; }
publicvoid setY_player( float y_player) { this . y_player = y_player; }
publicint getNavigate() { return Navigate ; }
publicvoid setNavigate( int navigate) { Navigate = navigate; }
publicint getSscore() { // TODO Auto-generated method stub returnthis . score ; publicint getSscore() { // TODO Auto-generated method stub returnthis . score ;
// TODO Auto-generated method stub returnthis . highscore ;
ESSurfaceView.java
package com.mcsentul.flappybirdopengl;
import android.app.Activity;
import android.os.Bundle;
publicclass Penutup extends Activity {
@Override protectedvoid onCreate(Bundle savedInstanceState)
{ super .onCreate(savedInstanceState);
setContentView(R.layout. activity_about ); } }
ESText.java
// This is a OpenGL ES 1.0 dynamic font rendering sys- tem. It loads actual font // files, generates a font map (texture) from them, and allows rendering of // text strings. // // NOTE: the rendering portions of this class uses a sprite batcher in order // provide decent speed rendering. Also, rendering as- sumes a BOTTOM-LEFT // origin, and the (x,y) positions are relative to that, as well as the
// bottom-left of the string to render.
package com.mcsentul.flappybirdopengl;
import javax.microedition.khronos.opengles.GL10;
import android.content.res.AssetManager; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Typeface; import android.opengl.GLUtils;
public class ESText {
//--Constants--// public final static int CHAR_START = 32;
// First Character (ASCII Code) public final static int CHAR_END = 126; // Last Character (ASCII Code) public final static int CHAR_CNT = ( ( ( CHAR_END - CHAR_START ) + 1 ) + 1 ); // Character Count (Includ- ing Character to use for Unknown)
public final static int CHAR_NONE = 32; // Character to Use for Unknown (ASCII Code) public final static int CHAR_UNKNOWN = ( CHAR_CNT -
1 ); // Index of the Unknown Character
public final static int FONT_SIZE_MIN = 6; // Minumum Font Size (Pixels) public final static int FONT_SIZE_MAX = 180; // Maximum Font Size (Pixels)
public final static int CHAR_BATCH_SIZE = 100; // Number of Characters to Render Per Batch
//--Members--// GL10 gl;
// GL10 Instance AssetManager assets; // Asset Manager SpriteBatch batch; // Batch Renderer
int fontPadX, fontPadY; // Font Padding (Pixels; On Each Side, ie. Doubled on Both X+Y Axis)
float fontHeight; // Font Height (Actual; Pixels) float fontAscent; // Font Ascent (Above Baseline; Pixels) float fontDescent; // Font Descent (Below Baseline; Pixels)
int textureId; // Font Texture ID [NOTE: Public for Testing Purposes Only!] int textureId; // Font Texture ID [NOTE: Public for Testing Purposes Only!]
TextureRegion textureRgn; // Full Texture Region
float charWidthMax; // Character Width (Maximum; Pixels) float charHeight; // Character Height (Maximum; Pixels) final float[] charWidths; // Width of Each Character (Actual; Pixels) TextureRegion[] charRgn; // Region of Each Character (Texture Coordinates) int cellWidth, cellHeight; // Character Cell Width/Height int rowCnt, colCnt; // Number of Rows/Columns
float scaleX, scaleY; // Font Scale (X,Y Axis) float spaceX; // Additional (X,Y Axis) Spacing (Unscaled)
//--Constructor--// // D: save GL instance + asset manager, create ar-
rays, and initialize the members // A: gl - OpenGL ES 10 Instance public ESText(GL10 gl, AssetManager assets) {
this.gl = gl; // Save the GL10 Instance this.assets = assets; // Save the Asset Manager Instance
batch = new SpriteBatch( gl, CHAR_BATCH_SIZE ); // Create Sprite Batch (with Defined Size)
charWidths = new float[CHAR_CNT]; // Create the Array of Character Widths charRgn = new TextureRegion[CHAR_CNT]; // Create the Array of Character Regions
// initialize remaining members fontPadX = 0; fontPadY = 0;
fontHeight = 0.0f; fontAscent = 0.0f; fontDescent = 0.0f;
textureId = -1; textureSize = 0;
charWidthMax = 0; charHeight = 0;
cellWidth = 0; cellHeight = 0; cellWidth = 0; cellHeight = 0;
scaleX = 1.0f; // Default Scale = 1 (Unscaled) scaleY = 1.0f; // Default Scale = 1 (Unscaled) spaceX = 0.0f; }
//--Load Font--// // description // this will load the specified font file, cre-
ate a texture for the defined
// character range, and setup all required val- ues used to render with it. // arguments: // file - Filename of the font (.ttf, .otf) to
use. In 'Assets' folder.
// size - Requested pixel size of font (height) // padX, padY - Extra padding per character (X+Y
Axis); to prevent overlapping characters. public boolean load(String file, int size, int padX, int padY) {
// setup requested values fontPadX = padX;
// Set Requested X Axis Padding fontPadY = padY; // Set Requested Y Axis Padding
// load the font and setup paint instance for drawing Typeface tf = Typeface.createFromAsset( assets, file ); // Create the Typeface from Font File Paint paint = new Paint(); // Create Android Paint Instance paint.setAntiAlias( true ); // Enable Anti Alias paint.setTextSize( size ); // Set Text Size paint.setColor( 0xffffffff ); // Set ARGB (White, Opaque) paint.setTypeface( tf ); // Set Typeface
// get font metrics Paint.FontMetrics fm = paint.getFontMetrics();
// Get Font Metrics fontHeight = (float)Math.ceil( Math.abs( fm.bottom ) + Math.abs( fm.top ) ); // Calculate Font Height
fontAscent = (float)Math.ceil( Math.abs( fm.ascent ) ); // Save Font Ascent
fontDescent = (float)Math.ceil( Math.abs( fm.descent ) ); // Save Font Descent
// determine the width of each character (in- cluding unknown character)
// also determine the maximum character width char[] s = new char[2];
// Create Character Array charWidthMax = charHeight = 0; // Reset Character Width/Height Maximums float[] w = new float[2]; // Working Width Value int cnt = 0; // Array Counter for ( char c = CHAR_START; c <= CHAR_END; c++ ) { // FOR Each Character s[0] = c; // Set Character
paint.getTextWidths( s, 0, 1, w ); // Get Character Bounds charWidths[cnt] = w[0]; // Get Width
if ( charWidths[cnt] > charWidthMax ) // IF Width Larger Than Max Width
charWidthMax = charWidths[cnt];
// Save New Max Width cnt++; // Advance Array Counter } s[0] = CHAR_NONE;
// Set Unknown Character paint.getTextWidths( s, 0, 1, w ); // Get Character Bounds charWidths[cnt] = w[0]; // Get Width if ( charWidths[cnt] > charWidthMax ) // IF Width Larger Than Max Width charWidthMax = charWidths[cnt]; // Save New Max Width cnt++; // Advance Array Counter
// set character height to font height charHeight = fontHeight;
// Set Character Height
// find the maximum size, validate, and setup cell sizes cellWidth = (int)charWidthMax + ( 2 * fontPadX ); // Set Cell Width cellHeight = (int)charHeight + ( 2 * fontPadY ); // Set Cell Height int maxSize = cellWidth > cellHeight ? cellWidth : cellHeight; // Save Max Size (Width/Height)
if ( maxSize < FONT_SIZE_MIN || maxSize > FONT_SIZE_MAX ) // IF Maximum Size Outside Valid Bounds
return false; // Return Error
// set texture size based on max font size (width or height) // NOTE: these values are fixed, based on the defined characters. when
// changing start/end characters (CHAR_START/CHAR_END) this will need adjustment too! if ( maxSize <= 24 ) // IF Max Size is 18 or Less textureSize = 256; // Set 256 Texture Size else if ( maxSize <= 40 ) // ELSE IF Max Size is 40 or Less textureSize = 512; // Set 512 Texture Size else if ( maxSize <= 80 ) // ELSE IF Max Size is 80 or Less textureSize = 1024; // Set 1024 Texture Size else // ELSE IF Max Size is Larger Than 80 (and Less than FONT_SIZE_MAX)
textureSize = 2048; // Set 2048 Texture Size
// create an empty bitmap (alpha only) Bitmap bitmap = Bitmap.createBitmap( texture-
Size, textureSize, Bitmap.Config.ALPHA_8 ); // Create Bitmap
Canvas canvas = new Canvas( bitmap ); // Create Canvas for Rendering to Bitmap bitmap.eraseColor( 0x00000000 ); // Set Transparent Background (ARGB)
// calculate rows/columns // NOTE: while not required for anything, these
may be useful to have :) colCnt = textureSize / cellWidth; // Calculate Number of Columns
rowCnt = (int)Math.ceil( (float)CHAR_CNT / (float)colCnt ); // Calculate Number of Rows
// render each of the characters to the canvas (ie. build the font map) float x = fontPadX; // Set Start Position (X)
float y = ( cellHeight - 1 ) - fontDescent - fontPadY; // Set Start Position (Y) for ( char c = CHAR_START; c <= CHAR_END; c++ ) { // FOR Each Character s[0] = c; // Set Character to Draw canvas.drawText( s, 0, 1, x, y, paint ); // Draw Character x += cellWidth; // Move to Next Character if ( ( x + cellWidth - fontPadX ) > texture- Size ) { // IF End of Line Reached x = fontPadX; // Set X for New Row y += cellHeight; // Move Down a Row } } float y = ( cellHeight - 1 ) - fontDescent - fontPadY; // Set Start Position (Y) for ( char c = CHAR_START; c <= CHAR_END; c++ ) { // FOR Each Character s[0] = c; // Set Character to Draw canvas.drawText( s, 0, 1, x, y, paint ); // Draw Character x += cellWidth; // Move to Next Character if ( ( x + cellWidth - fontPadX ) > texture- Size ) { // IF End of Line Reached x = fontPadX; // Set X for New Row y += cellHeight; // Move Down a Row } }
canvas.drawText( s, 0, 1, x, y, paint ); // Draw Character
// generate a new texture int[] textureIds = new int[1];
// Array to Get Texture Id gl.glGenTextures( 1, textureIds, 0 ); // Generate New Texture textureId = textureIds[0]; // Save Texture Id
// setup filters for texture gl.glBindTexture( GL10.GL_TEXTURE_2D, textureId
); // Bind Texture gl.glTexParameterf( GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST ); // Set Minification Filter
gl.glTexParameterf( GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR ); // Set Magnification Filter
gl.glTexParameterf( GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE ); // Set U Wrapping
gl.glTexParameterf( GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE ); // Set V Wrapping
// load the generated bitmap onto the texture GLUtils.texImage2D( GL10.GL_TEXTURE_2D, 0, bit-
map, 0 ); // Load Bitmap to Texture
gl.glBindTexture( GL10.GL_TEXTURE_2D, 0 ); // Unbind Texture
// release the bitmap bitmap.recycle();
// Release the Bitmap
// setup the array of character texture regions x = 0;
// Initialize X y = 0; // Initialize Y for ( int c = 0; c < CHAR_CNT; c++ ) { // FOR Each Character (On Texture) charRgn[c] = new TextureRegion( textureSize, textureSize, x, y, cellWidth-1, cellHeight-1 ); // Create Region for Character
x += cellWidth; // Move to Next Char (Cell)
if ( x + cellWidth > textureSize ) {
x = 0; // Reset X Position to Start y += cellHeight; // Move to Next Row (Cell) } }
// create full texture region textureRgn = new TextureRegion( textureSize,
textureSize, 0, 0, textureSize, textureSize ); // Create Full Texture Region
// return success return true;
// Return Success }
//--Begin/End Text Drawing--// // D: call these methods before/after (respectively
all draw() calls using a text instance
// NOTE: color is set on a per-batch basis, and fonts should be 8-bit alpha only!!!
// A: red, green, blue - RGB values for font (de- fault = 1.0)
// alpha - optional alpha value for font (de- fault = 1.0) // R: [none] public void begin() {
begin( 1.0f, 1.0f, 1.0f, 1.0f ); // Begin with White Opaque } public void begin(float alpha) {
begin( 1.0f, 1.0f, 1.0f, alpha ); // Begin with White (Explicit Alpha) } public void begin(float red, float green, float
blue, float alpha) {
gl.glColor4f( red, green, blue, alpha ); // Set Color+Alpha gl.glBindTexture( GL10.GL_TEXTURE_2D, textureId ); // Bind the Texture batch.beginBatch(); // Begin Batch } public void end() {
batch.endBatch(); // End Batch gl.glColor4f( 1.0f, 1.0f, 1.0f, 1.0f ); // Restore Default Color/Alpha }
//--Draw Text--// // D: draw text at the specified x,y position // A: text - the string to draw // x, y - the x,y position to draw text at (bot-
tom left of text; including descent) // R: [none] public void draw(String text, float x, float y) {
float chrHeight = cellHeight * scaleY; // Calculate Scaled Character Height float chrWidth = cellWidth * scaleX; // Calculate Scaled Character Width int len = text.length(); // Get String Length x += ( chrWidth / 2.0f ) - ( fontPadX * scaleX ); // Adjust Start X float chrHeight = cellHeight * scaleY; // Calculate Scaled Character Height float chrWidth = cellWidth * scaleX; // Calculate Scaled Character Width int len = text.length(); // Get String Length x += ( chrWidth / 2.0f ) - ( fontPadX * scaleX ); // Adjust Start X
if ( c < 0 || c >= CHAR_CNT ) // IF Character Not In Font
c = CHAR_UNKNOWN; // Set to Unknown Character Index batch.drawSprite( x, y, chrWidth, chrHeight, charRgn[c] ); // Draw the Character x += ( charWidths[c] + spaceX ) * scaleX; // Advance X Position by Scaled Character Width } }
//--Draw Text Centered--// // D: draw text CENTERED at the specified x,y posi-
tion // A: text - the string to draw // x, y - the x,y position to draw text at (bot-
tom left of text)
// R: the total width of the text that was drawn public float drawC(String text, float x, float y)
{ float len = getLength( text ); // Get Text Length draw( text, x - ( len / 2.0f ), y - ( getChar- Height() / 2.0f ) ); // Draw Text Centered return len; // Return Length } public float drawCX(String text, float x, float y)
{ float len = getLength( text ); // Get Text Length draw( text, x - ( len / 2.0f ), y ); // Draw Text Centered (X-Axis Only) return len; // Return Length } public void drawCY(String text, float x, float y)
{ draw( text, x, y - ( getCharHeight() / 2.0f ) ); // Draw Text Centered (Y-Axis Only) }
//--Set Scale--// // D: set the scaling to use for the font // A: scale - uniform scale for both x and y axis
scaling
// sx, sy - separate x and y axis scaling fac- tors // R: [none] public void setScale(float scale) {
scaleX = scaleY = scale; // Set Uniform Scale
} public void setScale(float sx, float sy) {
scaleX = sx; // Set X Scale scaleY = sy; // Set Y Scale }
//--Get Scale--// // D: get the current scaling used for the font // A: [none] // R: the x/y scale currently used for scale public float getScaleX() {
return scaleX; // Return X Scale } public float getScaleY() {
return scaleY; // Return Y Scale }
//--Set Space--// // D: set the spacing (unscaled; ie. pixel size) to
use for the font // A: space - space for x axis spacing // R: [none] public void setSpace(float space) {
spaceX = space; // Set Space }
//--Get Space--// // D: get the current spacing used for the font // A: [none] // R: the x/y space currently used for scale public float getSpace() {
return spaceX; // Return X Space }
//--Get Length of a String--// // D: return the length of the specified string if
rendered using current settings // A: text - the string to get length for // R: the length of the specified string (pixels) public float getLength(String text) {
float len = 0.0f; // Working Length int strLen = text.length(); // Get String Length (Characters) for ( int i = 0; i < strLen; i++ ) { // For Each Character in String (Except Last int c = (int)text.charAt( i ) - CHAR_START; // Calculate Character Index (Offset by First Char in Font)
len += ( charWidths[c] * scaleX ); // Add Scaled Character Width to Total Length } len += ( strLen > 1 ? ( ( strLen - 1 ) * spaceX
) * scaleX : 0 ); // Add Space Length return len; // Return Total Length } //--Get Width/Height of Character--// // D: return the scaled width/height of a charac-
ter, or max character width // NOTE: since all characters are the same height, no character index is required! // NOTE: excludes spacing!! // A: chr - the character to get width for // R: the requested character size (scaled) public float getCharWidth(char chr) {
int c = chr - CHAR_START; // Calculate Character Index (Offset by First Char in Font)
return ( charWidths[c] * scaleX ); // Return Scaled Character Width } public float getCharWidthMax() {
return ( charWidthMax * scaleX ); // Return Scaled Max Character Width } public float getCharHeight() {
return ( charHeight * scaleY ); // Return Scaled Character Height } //--Get Font Metrics--// // D: return the specified (scaled) font metric // A: [none] // R: the requested font metric (scaled) public float getAscent() {
return ( fontAscent * scaleY ); // Return Font Ascent } public float getDescent() {
return ( fontDescent * scaleY ); // Return Font Descent } public float getHeight() {
return ( fontHeight * scaleY ); // Return Font Height (Actual) }
//--Draw Font Texture--// // D: draw the entire font texture (NOTE: for test-
ing purposes only)
// A: width, height - the width and height of the area to draw to. this is used
// to draw the texture to the top-left corner. public void drawTexture(int width, int height) {
batch.beginBatch( textureId ); // Begin Batch (Bind Texture) batch.drawSprite( textureSize / 2, height - ( textureSize / 2 ), textureSize, textureSize, textur- eRgn ); // Draw
batch.endBatch(); // End Batch }
MainActivity.java
package com.mcsentul.flappybirdopengl;
import android.net.Uri; import android.opengl.GLSurfaceView; import android.os.Bundle;
import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences;
import android.text.Html; import android.util.Log; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.WindowManager;
import android.view.Window; import android.widget.Button; import android.widget.Toast;
publicclass MainActivity extends Activity { Button button2 ; /** The OpenGL view */ int counter ; int myhighscore =0; private ESRender esRender ; private GLSurfaceView glSurfaceView ; //private static final int
SCREEN_ORIENTATION_SENSOR_LANDSCAPE = 0;
@Override protectedvoid onCreate(Bundle savedInstanceState)
{ super .onCreate(savedInstanceState); // setCon-
tentView(R.layout.activity_main); esRender = new ESRen- der(getBaseContext()); counter =0;
//setRequestedOrientation(MainActivity.SCREEN_ORI ENTATION_SENSOR_LANDSCAPE); getWin- dow().addFlags(WindowManager.LayoutParams. FLAG_KEEP_SC
REEN_ON ); // requesting to turn the title OFF requestWindowFea-
ture(Window. FEATURE_NO_TITLE ); // making it full screen // getWin-
dow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCR EEN,
// WindowManag- er.LayoutParams.FLAG_FULLSCREEN);
// Initiate the Open GL view and // create an instance with this activity glSurfaceView = new ESSurfaceView( this );
// set our renderer to be the main ren- derer with
// the current activity context // glSurfaceView.setRenderer(new ESRen-
der(this));
setContentView( glSurfaceView );
/** * Remember to resume the glSurface */
@Override protectedvoid onResume() {
super .onResume(); Music.play( this , R.raw. bgm_main ); glSurfaceView .onResume(); int hs = LoadInt();
if ( esRender .getHighscore()==0){ esRender .seths(hs); } else { esRender .seths(hs); } Log.d( this .getClass().getName(),
"Highscore : " +hs); }
publicvoid SaveInt( int value){
SharedPreferences myPrefs = get-
SharedPreferences( "myPrefs" , MODE_WORLD_READABLE); SharedPreferences.Editor prefsEditor = myPrefs.edit(); prefsEditor.putInt( "highscore" , value); prefsEditor.commit();
} publicint LoadInt(){
SharedPreferences myPrefs = get-
SharedPreferences( "myPrefs" , MODE_WORLD_READABLE); int userId = myPrefs.getInt( "highscore" , -1); return userId;
* Also pause the glSurface */
@Override publicboolean onKeyDown( int keyCode, KeyEvent
event) { Log.d( this .getClass().getName(), "back lagi" ); if ((keyCode == KeyEvent. KEYCODE_BACK ) &&( counter <2)) { Log.d( this .getClass().getName(), "back button pressed" + counter ); counter +=1; int value= esRender .getHighscore();
SaveInt(value); Log.d( this .getClass().getName(), "Simpan
Highscore : " +value);
if ( counter ==1) Toast.makeText( this , "Tekan sekali lagi untuk exit." , 0).show(); } elseif ((keyCode == KeyEvent. KEYCODE_BACK ) && counter ==2){
Intent mainIntent = new In- tent(MainActivity. this ,Penutup. class ); MainActivi- ty. this .startActivity(mainIntent);
MainActivity. this .finish(); counter =0; }
returnsuper .onKeyDown(keyCode, event); }
@Override protectedvoid onPause() {
super .onPause(); //Music.stop(this); Music.pauseSong( this ); glSurfaceView .onPause();
@Override publicboolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu. main , menu); returntrue ; }
Maths.java package com.mcsentul.flappybirdopengl;
import android.app.Activity; import android.content.SharedPreferences;
/** * @author JimCornmell re - edit by XTech * */
publicfinalclass Maths {
/** 180 in radians. */ publicstaticfinaldouble ONE_EIGHTY_DEGREES = Math. PI ;
/** 360 in radians. */ publicstaticfinaldouble THREE_SIXTY_DEGREES = ONE_EIGHTY_DEGREES * 2;
/** 120 in radians. */ publicstaticfinaldouble ONE_TWENTY_DEGREES = THREE_SIXTY_DEGREES / 3;
/** 90 degrees, North pole. */ publicstaticfinaldouble NINETY_DEGREES = Math. PI / 2;
/** Used by power. */ privatestaticfinallong POWER_CLAMP = 0x00000000ffffffffL;
/** * Constructor, although not used at the moment. */
private Maths() { }
/** * Quick integer power function. * * @param base * number to raise. * @param raise * to this power. * @return base ^ raise. */
publicstaticint power( finalint base, finalint raise) { int p = 1;
long
b = raise & POWER_CLAMP ;
// bits in b correspond to values of powerN // so start with p=1, and for each set bit in b, multiply corresponding // table entry long powerN = base;
while (b != 0) { while (b != 0) {
b >>>= 1; powerN = powerN * powerN; }
return p; }
Music.java
package com.mcsentul.flappybirdopengl;
import android.content.Context; import android.media.MediaPlayer;
publicclass Music {
privatestatic MediaPlayer mp = null ; privatestatic MediaPlayer mpOnce = null ;
/** Stop old song and start new one */ publicstaticvoid playOnce(Context context,
int resource) { //stop(context); mpOnce = MediaPlayer.create(context,
resource); mpOnce .setLooping( false ); mpOnce .start();
publicstaticvoid play(Context context, int resource) { stop(context); mp = MediaPlayer.create(context, re-
source); mp .setLooping( true ); mp .start();
/** Stop the music */ publicstaticvoid stop(Context context) {
if ( mp != null ){
mp .stop(); mp .release(); mp = null ; mp .stop(); mp .release(); mp = null ;
mp .pause(); //mp.start(); //mp.play(this, resource); //mp.release(); //mp = null;
publicstaticvoid stopOnce(Context context) { if ( mpOnce != null ){ mpOnce .stop(); mpOnce .release(); mpOnce = null ;
ObjectBall.java
package com.mcsentul.flappybirdopengl;
import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.opengl.GLES20; import android.opengl.GLUtils;
publicclass ObjectBall {
privatefloat [] texCoordsbg ={ // Texture coords for the above face (NEW) 0.0f, 1.0f, // A. left-bottom (NEW) 1.0f, 1.0f, // B. right-bottom
(NEW) 0.0f, 0.0f, // C. left-top (NEW) 1.0f, 0.0f // D. right-top (NEW) }; privateint [] imageFileIDs ={ // Image file
IDs R.drawable. nature , // 0 R.drawable. mule , // 1 R.drawable. joystick , // 2 R.drawable. joystick_bg , // 3 R.drawable. meow , // 4 R.drawable. plus , // 5 R.drawable. min , // 6 R.drawable. background , R.drawable. pipaijo , // 8 R.drawable. pipaijoback , // 9 R.drawable. naik1 , // 10 R.drawable. naik2 , /1 R.drawable. naik3 , // 12 R.drawable. naik4 , // 13 R.drawable. naik5 , //14 R.drawable. naik6 , // 15 R.drawable. naik7 , // 16 R.drawable. naik8 , //17 R.drawable. underpanel , //18 R.drawable. gameover , //19 R.drawable. play , //20 R.drawable. tap //21
}; privateint [] imageFileIDs2 ={ // Image file IDs R.drawable. bird1 , R.drawable. bird2 , R.drawable. bird3 , R.drawable. bird4 ,
R.drawable. bird5 , R.drawable. bird6 , R.drawable. bird7 , R.drawable. bird8 };
privateint [] imageFileIDs3 ={ // Image file IDs R.drawable. angka0 , R.drawable. angka1 , R.drawable. angka2 , R.drawable. angka3 , R.drawable. angka4 , R.drawable. angka5 , R.drawable. angka6 , R.drawable. angka7 , R.drawable. angka8 ,
R.drawable. angka9 ,
privatefloat vertices_circle [] = { 0.0f, 0.0f, 0.0f };
privatefloat vertices_circle_color [] = { 0.0f, 0.0f, 0.0f, 0.5f }; privatefloat textCoord []; privatefloat vertices_circle1 [];
int [] textures_indek = newint [1];
privateint batas_sudut = 360; float jari_jari ;
float a , b ; float x , y ; float step = 3.0f, step_line = 0.2f; float x1 , y1 ; float x2 , y2 ; float teta ; privateint loop , loop_color ; private FloatBuffer vertexBuffer ; // Buffer
for vertex-array
privatefloat [] verticesbg ={ // Vertices for a face 0.0f, 0.0f, 0.0f, // 0. left- bottom-front 5.0f, 0.0f, 0.0f, // 1. right- bottom-front 0.0f, 3.0f, 0.0f, // 2. left- top-front 5.0f, 3.0f, 0.0f // 3. right- top-front }; private FloatBuffer texBuffer ;
public ObjectBall() {
float [] texCoordsbg = { // Texture coords for the above face (NEW) 0.0f, 1.0f, // A. left- bottom (NEW) 1.0f, 1.0f, // B. right-bottom (NEW) 0.0f, 0.0f, // C. left- top (NEW) 1.0f, 0.0f // D. right- top (NEW) };
/// kotak
// Setup vertex-array buffer. Verti- ces in float. An float has 4 bytes
ByteBuffer vbb = ByteBuff-
er.allocateDirect( verticesbg . length * 4); vbb.order(ByteOrder.nativeOrder()); // Use native byte order vertexBuffer = vbb.asFloatBuffer(); // Convert from byte to float vertexBuffer .put( verticesbg ); // Copy data into buffer vertexBuffer .position(0); // Rewind // Setup texture-coords-array buffer,
in float. An float has 4 bytes // (NEW) ByteBuffer tbb = ByteBuff-
er.allocateDirect(texCoordsbg. length * 4); tbb.order(ByteOrder.nativeOrder()); texBuffer = tbb.asFloatBuffer(); texBuffer .put(texCoordsbg); texBuffer .position(0);
// ============ start to generate stetch texture coordinat // ========================== // Inisialisasi jari_jari = 0.5f;
// TitikPusat
a = 0.5f;
b = 0.5f; // x=a+jari_jari; y=b; teta = 0;
// generate stretch texture coordinat teta = 0; textCoord = newfloat [ batas_sudut *
for ( int
ii = 0; ii < batas_sudut * 3;
ii += 3) { // membentuk textCoord textCoord [ii] = ( jari_jari *
(( float ) Math.cos(- teta ))) + a ; textCoord [ii + 1] = ( jari_jari * (( float ) Math.sin(- teta ))) + b ; textCoord [ii + 2] = 0.0f; teta += Math. PI / 90;
// ============ start to generate vertices to circle (Cara 1) // ========================== // Inisialisasi // ============ start to generate vertices to circle (Cara 1) // ========================== // Inisialisasi
// TitikPusat
a = 50.0f;
b = 50.0f;
vertices_circle1 = new-
float [ batas_sudut * 3];
for ( int
ii = 0; ii < batas_sudut * 3;
ii += 3) { // membentuk vertices_circle1 vertices_circle1 [ii] =
( jari_jari * (( float ) Math.cos( teta ))) + a ; vertices_circle1 [ii + 1] = ( jari_jari * (( float ) Math.sin( teta ))) + b ; vertices_circle1 [ii + 2] = 0.0f; teta += Math. PI / 90; }
// ============ start to generate vertices to circle (Cara 2) // ========================== // Inisialisasi jari_jari = 50.0f;
// TitikPusat
a = 50.0f;
b = 50.0f; x = a + jari_jari ; y = b ;
loop = 0; loop_color = 0; vertices_circle = newfloat [( int ) (3 *
batas_sudut / step ) * 3];
vertices_circle_color = new-
float [( int ) (3 * batas_sudut / step ) * 4]; for ( teta = 0; teta <= 2 * ba- tas_sudut ; teta += step ){ vertices_circle [ loop ]= ( float ) (( x - a ) * Math.cos(( teta / 180) * (22 / 7)) - (( y - b )* Math.sin(( teta / 180) * (22 / 7))) + a ); vertices_circle [ loop + 1] = ( float ) (( x - a ) * Math.sin(( teta / 180) * (22 / 7)) - (( y - b )* Math.cos(( teta / 180) * (22 / 7))) + b ); float [( int ) (3 * batas_sudut / step ) * 4]; for ( teta = 0; teta <= 2 * ba- tas_sudut ; teta += step ){ vertices_circle [ loop ]= ( float ) (( x - a ) * Math.cos(( teta / 180) * (22 / 7)) - (( y - b )* Math.sin(( teta / 180) * (22 / 7))) + a ); vertices_circle [ loop + 1] = ( float ) (( x - a ) * Math.sin(( teta / 180) * (22 / 7)) - (( y - b )* Math.cos(( teta / 180) * (22 / 7))) + b );
// mengeneratewarnauntuksetiap vertex // verti- ces_circle_color[loop_color]=(float) // ((x- a)*Math.cos((teta/180)*(22/7)) - // ((y- b)*Math.sin((teta/180)*(22/7))) + a); // verti- ces_circle_color[loop_color+1]=(float) // ((x- a)*Math.sin((teta/180)*(22/7)) - // ((y- b)*Math.cos((teta/180)*(22/7))) + b);
verti- ces_circle_color [ loop_color ]=( float ) (Math.cos(( teta / 180)
* (22 / 7))); verti- ces_circle_color [ loop_color + 1] = ( float ) (Math .sin(( teta / 180) * (22 / 7)));
verti- ces_circle_color [ loop_color + 2] = 0.5f; verti- ces_circle_color [ loop_color + 3] = 0.5f; loop_color += 4; } // ============= end for generate
vertices to circle // ====================
// Point to our vertex buffer, return buff- er holding the vertices
publicstatic FloatBuffer makeFloatBuff- er( float [] arr) { ByteBuffer bb = ByteBuff- er.allocateDirect(arr. length * 4); bb.order(ByteOrder.nativeOrder()); FloatBuffer fb = bb.asFloatBuffer(); fb.put(arr); fb.position(0); return fb;
// Setup index-array buffer. Indices in // Setup index-array buffer. Indices in
ByteBuffer bb = ByteBuff-
er.allocateDirect(arr. length ); bb.put(arr); bb.position(0); return bb;
publicvoid draw_circle_color(GL10 gl) {
gl.glEnableClientState(GL10. GL_VERTEX_ARRAY ); // gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glEnableClientState(GL10. GL_TEXTURE_COOR D_ARRAY ); // Enable // set the colour edge for the object circle // gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
// create VBO from buffer with glBufferData() gl.glVertexPointer(3, GL10. GL_FLOAT , 0, makeFloatBuff- er( vertices_circle1 ));
// memetakanwarnauntuksetiap vertex // gl.glColorPointer(4,
GL10.GL_FLOAT, 0, // makeFloatBuff- er(vertices_circle_color));
// menempelkanteksturkeobjek gl.glEnable(GL10. GL_TEXTURE_2D );
gl.glEnable(GL10. GL_BLEND ); gl.glBlendFunc(GL10. GL_SRC_ALPHA ,
GL10. GL_ONE_MINUS_SRC_ALPHA );
gl.glBindTexture(GL10. GL_TEXTURE_2D , textures_indek [0]); // 4
// gl.glTexCoordPointer(3,
GL10.GL_FLOAT, 1, // makeFloatBuffer(vertices_circle)); // 5 gl.glTexCoordPointer(3, GL10. GL_FLOAT , 0, makeFloatBuffer( textCoord )); // 5 GL10.GL_FLOAT, 1, // makeFloatBuffer(vertices_circle)); // 5 gl.glTexCoordPointer(3, GL10. GL_FLOAT , 0, makeFloatBuffer( textCoord )); // 5
// GL11ExtensionPack gl_ =
(GL11ExtensionPack) gl;
// GL11 gl11 = (GL11) gl; //
gl.glBindTexture(GL10.GL_TEXTURE_2D, tex- tures_indek[0]);
// Set the face rotation // gl.glFrontFace(GL10.GL_CW);
// draw circle as filled shape //
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, (int) ((int)
// 2*batas_sudut/step)); //
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0, (int) ((int)
// 2*batas_sudut/step)); gl.glDrawArrays(GL10. GL_TRIANGLE_FAN ,
0, batas_sudut ); // gl.glDrawArrays(GL10.GL_LINE_STRIP, 0, (int) ((int)
// 2*batas_sudut/step)); //
gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 1, 120); // Log.i("Nilai 2*batas_sudut/step : ", ""+2*batas_sudut/step);
// gl.glDrawArrays(GL10.GL_LINE_STRIP, 1, (int) ((int)
// 2*batas_sudut/step)); //
gl.glDrawElements(GL10.GL_TRIANGLES, (int) ((int)
// 2*batas_sudut/step), // GL10.GL_UNSIGNED_SHORT, makeFloat-
Buffer(vertices_circle));
// draw circle contours // gl.glDrawArrays(GL10.GL_LINES, 1,
(int) ((int) 2*batas_sudut/step)); // // membuatgarisputus- putuspadatepilingkaran // gl.glDrawArrays(GL10.GL_LINE_STRIP, 1, (int)
((int) // 2*batas_sudut/step)); // gl.glDrawArrays(GL10.GL_POINTS, 1,
(int) ((int) 2*batas_sudut/step));
// Disable the client state before leaving
gl.glDisableClientState(GL10. GL_TEXTURE_COO RD_ARRAY ); // Disable
gl.glDisableClientState(GL10. GL_VERTEX_ARRA Y ); // gl.glDisableClientState(GL10.GL_COLOR_ARRAY); gl.glDisable(GL10. GL_BLEND ); // Disa- ble Alpha Blend gl.glDisable(GL10. GL_TEXTURE_2D ); // Disable Texture Mapping }
publicvoid drawbg2(GL10 gl) {
gl.glEnableClientState(GL10. GL_VERTEX_ARRAY ); // gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glEnableClientState(GL10. GL_TEXTURE_COOR D_ARRAY ); // Enable // set the colour edge for the object circle // gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
// create VBO from buffer with glBufferData() gl.glVertexPointer(3, GL10. GL_FLOAT ,
0, makeFloatBuffer( newfloat [] {
0.0f, 0.0f, 0.0f, // 0. left-bottom-front 5.0f, 0.0f, 0.0f, // 1. right-bottom-front 0.0f, 3.0f, 0.0f, // 2. left-top-front 5.0f, 3.0f, 0.0f // 3. right-top-front
// memetakanwarnauntuksetiap vertex // gl.glColorPointer(4,
GL10.GL_FLOAT, 0, // makeFloatBuff- er(vertices_circle_color));
// menempelkanteksturkeobjek gl.glEnable(GL10. GL_TEXTURE_2D );
gl.glEnable(GL10. GL_BLEND ); gl.glBlendFunc(GL10. GL_SRC_ALPHA ,
GL10. GL_ONE_MINUS_SRC_ALPHA );
gl.glBindTexture(GL10. GL_TEXTURE_2D , textures_indek [0]); // 4
// gl.glTexCoordPointer(3,
GL10.GL_FLOAT, 1, // makeFloatBuffer(vertices_circle)); // 5 gl.glTexCoordPointer(3, GL10. GL_FLOAT , 0, makeFloatBuffer( texCoordsbg )); // 5
gl.glEnableClientState(GL10. GL_TEXTURE_COOR D_ARRAY );
//gl.glDrawArrays(GL10.GL_TRIANGLE_FAN, 0,
gl.glDrawArrays(GL10. GL_TRIANGLE_STRIP , 0,
gl.glDisableClientState(GL10. GL_TEXTURE_COO RD_ARRAY ); // Disable
gl.glDisableClientState(GL10. GL_VERTEX_ARRA Y ); // gl.glDisableClientState(GL10.GL_COLOR_ARRAY); gl.glDisable(GL10. GL_BLEND ); // Disa- ble Alpha Blend gl.glDisable(GL10. GL_TEXTURE_2D ); // Disable Texture Mapping }
publicvoid draw_bg(GL10 gl) {
// gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
//gl.glEnableClientState(GL10.GL_TEXTURE_CO ORD_ARRAY); // Enable // set the colour edge for the object circle // gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f); // create VBO from buffer with glBufferData()
gl.glVertexPointer(3, GL10. GL_FLOAT ,
0, vertexBuffer );
gl.glEnableClientState(GL10. GL_TEXTURE_COOR D_ARRAY ); // Enable
// texture-coords-array
// (NEW) gl.glTexCoordPointer(2, GL10. GL_FLOAT , 0, texBuffer );
// menempelkanteksturkeobjek gl.glEnable(GL10. GL_TEXTURE_2D );
gl.glEnable(GL10. GL_BLEND );
gl.glEnableClientState(GL10. GL_VERTEX_ARRAY ); //gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); gl.glBindTexture(GL10. GL_TEXTURE_2D , textures_indek [0]); // 4
// gl.glTexCoordPointer(3,
GL10.GL_FLOAT, 1, // makeFloatBuffer(vertices_circle)); // 5
gl.glDrawArrays(GL10. GL_TRIANGLE_STRIP , 0, 4);
// Disable the client state before leaving
gl.glDisableClientState(GL10. GL_TEXTURE_COO RD_ARRAY ); // Disable
gl.glDisableClientState(GL10. GL_VERTEX_ARRA Y ); // gl.glDisableClientState(GL10.GL_COLOR_ARRAY); gl.glDisable(GL10. GL_BLEND ); // Disa- ble Alpha Blend gl.glDisable(GL10. GL_TEXTURE_2D ); //
Disable Texture Mapping }
publicvoid draw_segitiga(GL10 gl) {
// gl.glFrontFace(GL10.GL_CCW); // Front face in counter-clockwise gl.glEnable(GL10. GL_TEXTURE_2D ); gl.glEnable(GL10. GL_BLEND ); // gl.glBlendFunc(GL10.GL_SRC_ALPHA,
GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnableClientState(GL10. GL_VERTEX_ARRAY );
gl.glEnableClientState(GL10. GL_TEXTURE_COOR D_ARRAY );
// gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
// set the colour for the triangle // gl.glColor4f(1.0f, 0.0f, 0.0f,
1.0f);
gl.glVertexPointer(3, GL10. GL_FLOAT ,
0, makeFloatBuffer( newfloat [] {
0.0f, 0.0f, 0.0f, // 0. left-bottom- front 5.0f, 0.0f, 0.0f, // 1. right-bottom-front 0.0f, 3.0f, 0.0f, // 2. left-top-front 5.0f, 3.0f, 0.0f // 3. right-top-front
// Draw the vertices as triangle // gl.glColorPointer(4,
GL10.GL_FLOAT, 0, // makeFloatBuffer(vertices_color));
// menempelkanteksturkeobjek // gl.glEnable(GL10.GL_TEXTURE_2D);
// gl.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
gl.glDrawArrays(GL10. GL_TRIANGLE_STRIP , 0, 4);
// Disable the client state before // Disable the client state before
gl.glDisableClientState(GL10. GL_TEXTURE_COO RD_ARRAY ); // Disable
gl.glDisableClientState(GL10. GL_VERTEX_ARRA Y ); // gl.glDisableClientState(GL10.GL_COLOR_ARRAY); gl.glDisable(GL10. GL_BLEND ); // Disa- ble Alpha Blend gl.glDisable(GL10. GL_TEXTURE_2D ); }
publicvoid loadbg(GL10 gl, Context context, int index) { // Bitmap bitmap = BitmapFacto- ry.decodeResource(context.getResources(), // resource);
Bitmap bitmap = BitmapFacto-
ry.decodeStream(context.getResources()
.openRawResource( imageFileIDs [index]));
gl.glGenTextures(1, textures_indek , 0); gl.glBindTexture(GL10. GL_TEXTURE_2D , textures_indek [0]);
gl.glTexParameterf(GLES20. GL_TEXTURE_2D , GLES20. GL_TEXTURE_MAG_FILTER , GLES20. GL_NEAREST );
gl.glTexParameterf(GLES20. GL_TEXTURE_2D , GLES20. GL_TEXTURE_MIN_FILTER , GLES20. GL_NEAREST );
GLU- tils.texImage2D(GLES20. GL_TEXTURE_2D , 0, bitmap, 0);
bitmap.recycle(); }
publicvoid loadBallTexture(GL10 gl, Context context, int index_Texture) { // Bitmap bitmap = BitmapFacto- ry.decodeResource(context.getResources(), // resource);
/* * Bitmap bitmap = BitmapFacto- /* * Bitmap bitmap = BitmapFacto-
Bitmap bitmap = BitmapFacto-
ry.decodeStream(context.getResources()
.openRawResource( imageFileIDs [index_Texture ]));
gl.glGenTextures(1, textures_indek , 0); gl.glBindTexture(GL10. GL_TEXTURE_2D , textures_indek [0]);
gl.glTexParameterf(GLES20. GL_TEXTURE_2D , GLES20. GL_TEXTURE_MAG_FILTER , GLES20. GL_NEAREST );
gl.glTexParameterf(GLES20. GL_TEXTURE_2D , GLES20. GL_TEXTURE_MIN_FILTER , GLES20. GL_NEAREST );
GLU- tils.texImage2D(GLES20. GL_TEXTURE_2D , 0, bitmap, 0);
bitmap.recycle(); }
publicvoid loadBurungTexture(GL10 gl, Con- text context, int index_Texture) {
Bitmap bitmap = BitmapFacto-
ry.decodeStream(context.getResources()
.openRawResource( imageFileIDs2 [index_Textur e]));
gl.glGenTextures(1, textures_indek , 0); gl.glBindTexture(GL10. GL_TEXTURE_2D , textures_indek [0]);
gl.glTexParameterf(GLES20. GL_TEXTURE_2D , GLES20. GL_TEXTURE_MAG_FILTER , GLES20. GL_NEAREST );
gl.glTexParameterf(GLES20. GL_TEXTURE_2D , GLES20. GL_TEXTURE_MIN_FILTER , GLES20. GL_NEAREST );
GLU- tils.texImage2D(GLES20. GL_TEXTURE_2D , 0, bitmap, 0);
bitmap.recycle(); }
publicvoid loadAngkaTexture(GL10 gl, Con- text context, int index_Texture) {
Bitmap bitmap = BitmapFacto-
ry.decodeStream(context.getResources()
.openRawResource( imageFileIDs3 [index_Textur e]));
gl.glGenTextures(1, textures_indek , 0); gl.glBindTexture(GL10. GL_TEXTURE_2D , textures_indek [0]);
gl.glTexParameterf(GLES20. GL_TEXTURE_2D , GLES20. GL_TEXTURE_MAG_FILTER , GLES20. GL_NEAREST );
gl.glTexParameterf(GLES20. GL_TEXTURE_2D , GLES20. GL_TEXTURE_MIN_FILTER , GLES20. GL_NEAREST );
GLU- tils.texImage2D(GLES20. GL_TEXTURE_2D , 0, bitmap, 0);
bitmap.recycle(); }
publicvoid loadTexture(GL10 gl, Context c, int index) {
Bitmap bitmap = BitmapFacto-
ry.decodeStream(c.getResources()
.openRawResource( imageFileIDs [index]));
// InputStream istream = c.getResources().openRawResource( // imageFileIDs[index]);
gl.glGenTextures(1, textures_indek , 0); gl.glBindTexture(GL10. GL_TEXTURE_2D , textures_indek [0]); gl.glGenTextures(1, textures_indek , 0); gl.glBindTexture(GL10. GL_TEXTURE_2D , textures_indek [0]);
gl.glTexParameterf(GLES20. GL_TEXTURE_2D , GLES20. GL_TEXTURE_MIN_FILTER , GLES20. GL_NEAREST );
GLU- tils.texImage2D(GLES20. GL_TEXTURE_2D , 0, bitmap, 0);
bitmap.recycle();
Penutup.java
package com.mcsentul.flappybirdopengl;
import android.app.Activity;
import android.os.Bundle;
publicclass Penutup extends Activity { @Override protectedvoid onCreate(Bundle savedIn-
stanceState) { super .onCreate(savedInstanceState);
setCon- tentView(R.layout. activity_about ); } }
Splash.java
package com.mcsentul.flappybirdopengl; import android.app.Activity; import android.content.Intent; package com.mcsentul.flappybirdopengl; import android.app.Activity; import android.content.Intent;
getWin- dow().requestFeature(Window. FEATURE_ACTION_BAR );
this .requestWindowFeature(Window. FEATURE_NO_TITL
E ); setContentView(R.layout. splashscreen ); WebView webview; WebSettings websettings;
webview = (WebView) findViewBy- Id(R.id. webview ); websettings = webview.getSettings(); webset-
tings.setJavaScriptEnabled( true ); webview.setWebViewClient( new WebView- Client()); web- view.loadUrl( "file:///android_asset/www/splash.g if" ); /* New Handler to start the Menu-Activity
* and close this Splash-Screen after some seconds.*/ new Handler().postDelayed( new Runnable(){
@Override publicvoid run() {
/* Create an Intent that will start the Menu- Activity. */
Intent mainIntent = new In- tent(splash. this ,MainActivity. class );
splash. this .startActivity(mainIntent); splash. this .finish(); } }, SPLASH_DISPLAY_LENGHT );
SpriteBatch.java
package com.mcsentul.flappybirdopengl;
import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.opengl.GLES20; import android.opengl.GLUtils;
publicclass ObjectBall {
privatefloat [] texCoordsbg ={ // Texture coords for the above face (NEW) 0.0f, 1.0f, // A. left-bottom (NEW) 1.0f, 1.0f, // B. right-bottom (NEW) 0.0f, 0.0f, // C. left-top (NEW) 1.0f, 0.0f // D. right-top (NEW)
}; privateint [] imageFileIDs ={ // Image file IDs R.drawable. nature , // 0
R.drawable. mule , // 1 R.drawable. joystick , // 2 R.drawable. joystick_bg , // 3 R.drawable. meow , // 4 R.drawable. plus , // 5 R.drawable. min , // 6 R.drawable. background , R.drawable. pipaijo , // 8 R.drawable. pipaijoback , // 9 R.drawable. naik1 , // 10 R.drawable. naik2 , /1 R.drawable. naik3 , // 12 R.drawable. naik4 , // 13 R.drawable. naik5 , //14 R.drawable. naik6 , // 15 R.drawable. naik7 , // 16 R.drawable. naik8 , //17 R.drawable. underpanel , //18 R.drawable. gameover , //19 R.drawable. play , //20 R.drawable. tap //21
}; privateint [] imageFileIDs2 ={ // Image file IDs
R.drawable. bird1 ,
R.drawable. bird2 , R.drawable. bird3 , R.drawable. bird4 ,
R.drawable. bird5 ,
R.drawable. bird6 , R.drawable. bird7 , R.drawable. bird8 };
privateint [] imageFileIDs3 ={ // Image file IDs R.drawable. angka0 , R.drawable. angka1 , R.drawable. angka2 , R.drawable. angka3 , R.drawable. angka4 , R.drawable. angka5 , R.drawable. angka6 , R.drawable. angka7 , R.drawable. angka8 , R.drawable. angka9 ,
privatefloat vertices_circle [] = { 0.0f, 0.0f, 0.0f };
privatefloat vertices_circle_color [] = { 0.0f, 0.0f, 0.0f, 0.5f }; privatefloat textCoord []; privatefloat vertices_circle1 [];
int [] textures_indek = newint [1];
privateint batas_sudut = 360; float jari_jari ;
float a , b ; float x , y ; float step = 3.0f, step_line = 0.2f; float x1 , y1 ; float x2 , y2 ; float teta ; privateint loop , loop_color ; private FloatBuffer vertexBuffer ; // Buffer for
vertex-array
privatefloat [] verticesbg ={ // Vertices for a
face 0.0f, 0.0f, 0.0f, // 0. left- bottom-front 5.0f, 0.0f, 0.0f, // 1. right- bottom-front 0.0f, 3.0f, 0.0f, // 2. left-top- front 5.0f, 3.0f, 0.0f // 3. right-top- front }; private FloatBuffer texBuffer ;
public ObjectBall() {
float [] texCoordsbg = { // Texture coords for the float [] texCoordsbg = { // Texture coords for the
/// kotak // Setup vertex-array buffer. Vertices in float. An float has 4 bytes ByteBuffer vbb = ByteBuff- er.allocateDirect( verticesbg . length * 4); vbb.order(ByteOrder.nativeOrder()); // Use native byte order vertexBuffer = vbb.asFloatBuffer(); // Convert from byte to float vertexBuffer .put( verticesbg ); // Copy da- ta into buffer vertexBuffer .position(0); // Rewind // Setup texture-coords-array buffer, in
float. An float has 4 bytes // (NEW) ByteBuffer tbb = ByteBuff-
er.allocateDirect(texCoordsbg. length * 4); tbb.order(ByteOrder.nativeOrder()); texBuffer = tbb.asFloatBuffer(); texBuffer .put(texCoordsbg); texBuffer .position(0);
// ============ start to generate stetch texture coordinat
// ========================== // Inisialisasi jari_jari = 0.5f;
// TitikPusat
a = 0.5f;
b = 0.5f; // x=a+jari_jari; y=b; teta = 0;
// generate stretch texture coordinat teta = 0; textCoord = newfloat [ batas_sudut * 3];
ii = 0; ii < batas_sudut * 3; ii += 3) { // membentuk textCoord textCoord [ii] = ( jari_jari *
for ( int
(( float ) Math.cos(- teta ))) + a ;
textCoord [ii + 1] = ( jari_jari *
(( float ) Math.sin(- teta ))) + b ;
textCoord [ii + 2] = 0.0f; teta += Math. PI / 90;
// ============ start to generate verti- ces to circle (Cara 1)
// ========================== // Inisialisasi jari_jari = 50.0f;
// TitikPusat
a = 50.0f;
b = 50.0f;
vertices_circle1 = newfloat [ batas_sudut * 3];
ii = 0; ii < batas_sudut * 3; ii += 3) { // membentuk vertices_circle1 vertices_circle1 [ii] = ( jari_jari
for ( int
* (( float ) Math.cos( teta ))) + a ;
vertices_circle1 [ii + 1] = ( jari_jari * (( float ) Math.sin( teta ))) + b ; vertices_circle1 [ii + 2] = 0.0f; teta += Math. PI / 90;
// ============ start to generate verti- ces to circle (Cara 2)
// ========================== // Inisialisasi jari_jari = 50.0f;
// TitikPusat
a = 50.0f;
b = 50.0f; x = a + jari_jari ; y = b ;
loop = 0; loop_color = 0; vertices_circle = newfloat [( int ) (3 * ba-
tas_sudut / step ) * 3]; vertices_circle_color = newfloat [( int ) (3 * batas_sudut / step ) * 4]; for ( teta = 0; teta <= 2 * batas_sudut ; teta += step ){ vertices_circle [ loop ]=( float ) (( x - a ) * Math.cos(( teta / 180) * (22 / 7)) - (( y - b )* Math.sin(( teta / 180) * (22 / 7))) + a ); vertices_circle [ loop + 1] = ( float ) (( x - a ) * Math.sin(( teta / 180) * (22 / 7)) - (( y - b )* Math.cos(( teta / 180) * (22 / 7))) + b ); vertices_circle [ loop + 2] = 0; tas_sudut / step ) * 3]; vertices_circle_color = newfloat [( int ) (3 * batas_sudut / step ) * 4]; for ( teta = 0; teta <= 2 * batas_sudut ; teta += step ){ vertices_circle [ loop ]=( float ) (( x - a ) * Math.cos(( teta / 180) * (22 / 7)) - (( y - b )* Math.sin(( teta / 180) * (22 / 7))) + a ); vertices_circle [ loop + 1] = ( float ) (( x - a ) * Math.sin(( teta / 180) * (22 / 7)) - (( y - b )* Math.cos(( teta / 180) * (22 / 7))) + b ); vertices_circle [ loop + 2] = 0;
// mengeneratewarnauntuksetiap vertex // verti- ces_circle_color[loop_color]=(float) // ((x- a)*Math.cos((teta/180)*(22/7)) - // ((y- b)*Math.sin((teta/180)*(22/7))) + a); // verti- ces_circle_color[loop_color+1]=(float) // ((x- a)*Math.sin((teta/180)*(22/7)) - // ((y- b)*Math.cos((teta/180)*(22/7))) + b);
vertices_circle_color [ loop_color ] =( float ) (Math.cos(( teta / 180) * (22 / 7))); vertices_circle_color [ loop_color + 1] = ( float ) (Math .sin(( teta / 180) * (22 / 7)));
vertices_circle_color [ loop_color + 2] = 0.5f; vertices_circle_color [ loop_color + 3] = 0.5f;
loop_color += 4;
} // ============= end for generate verti-
ces to circle // ====================
// Point to our vertex buffer, return buffer holding the vertices publicstatic FloatBuffer makeFloatBuffer( float [] arr) { ByteBuffer bb = ByteBuff- er.allocateDirect(arr. length * 4); bb.order(ByteOrder.nativeOrder()); FloatBuffer fb = bb.asFloatBuffer(); fb.put(arr); fb.position(0); return fb;
// Setup index-array buffer. Indices in byte. publicstatic ByteBuffer makeByteBuffer( byte []
arr) { ByteBuffer bb = ByteBuff- er.allocateDirect(arr. length ); bb.put(arr); bb.position(0); return bb; arr) { ByteBuffer bb = ByteBuff- er.allocateDirect(arr. length ); bb.put(arr); bb.position(0); return bb;
gl.glEnableClientState(GL10. GL_VERTEX_ARRAY );
// gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glEnableClientState(GL10. GL_TEXTURE_COORD_ARRA Y ); // Enable // set the colour edge for the object circle // gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
// create VBO from buffer with glBuffer- Data() gl.glVertexPointer(3, GL10. GL_FLOAT , 0, makeFloatBuff- er( vertices_circle1 ));
// memetakanwarnauntuksetiap vertex // gl.glColorPointer(4, GL10.GL_FLOAT, 0, // makeFloatBuff-
er(vertices_circle_color));
// menempelkanteksturkeobjek gl.glEnable(GL10. GL_TEXTURE_2D );
gl.glEnable(GL10. GL_BLEND ); gl.glBlendFunc(GL10. GL_SRC_ALPHA ,
GL10. GL_ONE_MINUS_SRC_ALPHA );
gl.glBindTexture(GL10. GL_TEXTURE_2D , tex- tures_indek [0]); // 4 // gl.glTexCoordPointer(3, GL10.GL_FLOAT, 1, // makeFloatBuffer(vertices_circle)); //
5 gl.glTexCoordPointer(3, GL10. GL_FLOAT , 0, makeFloatBuffer( textCoord )); // 5
gl.glEnableClientState(GL10. GL_TEXTURE_COORD_ARRA Y );
// GL11ExtensionPack gl_ = (GL11ExtensionPack) gl; // GL11 gl11 = (GL11) gl; // gl.glBindTexture(GL10.GL_TEXTURE_2D,
textures_indek[0]);
// Set the face rotation // gl.glFrontFace(GL10.GL_CW);
// draw circle as filled shape // gl.glDrawArrays(GL10.GL_TRIANGLE_FAN,
0, (int) ((int) // 2*batas_sudut/step)); // gl.glDrawArrays(GL10.GL_TRIANGLE_FAN,
0, (int) ((int)
// 2*batas_sudut/step)); gl.glDrawArrays(GL10. GL_TRIANGLE_FAN , 0,
batas_sudut ); // gl.glDrawArrays(GL10.GL_LINE_STRIP, 0, (int) ((int) // 2*batas_sudut/step)); // gl.glDrawArrays(GL10.GL_TRIANGLE_FAN,
1, 120); // Log.i("Nilai 2*batas_sudut/step : ", ""+2*batas_sudut/step);
// gl.glDrawArrays(GL10.GL_LINE_STRIP, 1, (int) ((int) // 2*batas_sudut/step)); // gl.glDrawElements(GL10.GL_TRIANGLES,
(int) ((int) // 2*batas_sudut/step), // GL10.GL_UNSIGNED_SHORT, makeFloatBuff-
er(vertices_circle));
// draw circle contours // gl.glDrawArrays(GL10.GL_LINES, 1,
(int) ((int) 2*batas_sudut/step)); // // membuatgarisputus- putuspadatepilingkaran // gl.glDrawArrays(GL10.GL_LINE_STRIP, 1, (int) ((int) // 2*batas_sudut/step)); // gl.glDrawArrays(GL10.GL_POINTS, 1,
(int) ((int) 2*batas_sudut/step));
// Disable the client state before leav- ing
gl.glDisableClientState(GL10. GL_TEXTURE_COORD_ARR AY ); // Disable
gl.glDisableClientState(GL10. GL_VERTEX_ARRAY );
// gl.glDisableClientState(GL10.GL_COLOR_ARRAY); gl.glDisable(GL10. GL_BLEND ); // Disable Alpha Blend gl.glDisable(GL10. GL_TEXTURE_2D ); // Dis- able Texture Mapping }
publicvoid drawbg2(GL10 gl) {
gl.glEnableClientState(GL10. GL_VERTEX_ARRAY );
// gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glEnableClientState(GL10. GL_TEXTURE_COORD_ARRA Y ); // Enable // set the colour edge for the object circle // gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
// create VBO from buffer with glBuffer- Data() gl.glVertexPointer(3, GL10. GL_FLOAT , 0, makeFloatBuffer( newfloat [] {
0.0f, 0.0f, 0.0f, // 0. left-bottom-front 5.0f, 0.0f, 0.0f, // 1. right-bottom-front 0.0f, 3.0f, 0.0f, // 2. left-top-front 5.0f, 3.0f, 0.0f // 3. right-top-front
// memetakanwarnauntuksetiap vertex // gl.glColorPointer(4, GL10.GL_FLOAT, 0, // makeFloatBuff-
er(vertices_circle_color));
// menempelkanteksturkeobjek gl.glEnable(GL10. GL_TEXTURE_2D );
gl.glEnable(GL10. GL_BLEND ); gl.glBlendFunc(GL10. GL_SRC_ALPHA ,
GL10. GL_ONE_MINUS_SRC_ALPHA );
gl.glBindTexture(GL10. GL_TEXTURE_2D , tex- tures_indek [0]); // 4 // gl.glTexCoordPointer(3, GL10.GL_FLOAT, 1, // makeFloatBuffer(vertices_circle)); //
5 gl.glTexCoordPointer(3, GL10. GL_FLOAT , 0, makeFloatBuffer( texCoordsbg )); // 5
gl.glEnableClientState(GL10. GL_TEXTURE_COORD_ARRA Y );
//gl.glDrawArrays(GL10.GL_TRIANGLE_FAN,
0, 4); gl.glDrawArrays(GL10. GL_TRIANGLE_STRIP ,
gl.glDisableClientState(GL10. GL_TEXTURE_COORD_ARR AY ); // Disable
gl.glDisableClientState(GL10. GL_VERTEX_ARRAY );
// gl.glDisableClientState(GL10.GL_COLOR_ARRAY); gl.glDisable(GL10. GL_BLEND ); // Disable Alpha Blend gl.glDisable(GL10. GL_TEXTURE_2D ); // Dis- able Texture Mapping } // gl.glDisableClientState(GL10.GL_COLOR_ARRAY); gl.glDisable(GL10. GL_BLEND ); // Disable Alpha Blend gl.glDisable(GL10. GL_TEXTURE_2D ); // Dis- able Texture Mapping }
// gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
//gl.glEnableClientState(GL10.GL_TEXTURE_COORD_AR RAY); // Enable // set the colour edge for the object circle // gl.glColor4f(0.0f, 0.0f, 1.0f, 1.0f); // create VBO from buffer with glBuffer-
Data()
gl.glVertexPointer(3, GL10. GL_FLOAT , 0, vertexBuffer );
gl.glEnableClientState(GL10. GL_TEXTURE_COORD_ARRA Y ); // Enable
// texture-coords-array
// (NEW) gl.glTexCoordPointer(2, GL10. GL_FLOAT , 0, texBuffer );
// menempelkanteksturkeobjek gl.glEnable(GL10. GL_TEXTURE_2D );
gl.glEnable(GL10. GL_BLEND );
gl.glEnableClientState(GL10. GL_VERTEX_ARRAY );
//gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA); gl.glBindTexture(GL10. GL_TEXTURE_2D , tex- tures_indek [0]); // 4 // gl.glTexCoordPointer(3, GL10.GL_FLOAT, 1, // makeFloatBuffer(vertices_circle)); //
gl.glDrawArrays(GL10. GL_TRIANGLE_STRIP ,
// Disable the client state before leav- ing
gl.glDisableClientState(GL10. GL_TEXTURE_COORD_ARR AY ); // Disable
gl.glDisableClientState(GL10. GL_VERTEX_ARRAY );
// gl.glDisableClientState(GL10.GL_COLOR_ARRAY); gl.glDisable(GL10. GL_BLEND ); // Disable Alpha Blend gl.glDisable(GL10. GL_TEXTURE_2D ); // Dis- // gl.glDisableClientState(GL10.GL_COLOR_ARRAY); gl.glDisable(GL10. GL_BLEND ); // Disable Alpha Blend gl.glDisable(GL10. GL_TEXTURE_2D ); // Dis-
publicvoid draw_segitiga(GL10 gl) {
// gl.glFrontFace(GL10.GL_CCW); // Front face in counter-clockwise gl.glEnable(GL10. GL_TEXTURE_2D ); gl.glEnable(GL10. GL_BLEND ); // gl.glBlendFunc(GL10.GL_SRC_ALPHA,
GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnableClientState(GL10. GL_VERTEX_ARRAY );
gl.glEnableClientState(GL10. GL_TEXTURE_COORD_ARRA Y );
// gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
// set the colour for the triangle // gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
gl.glVertexPointer(3, GL10. GL_FLOAT , 0, makeFloatBuffer( newfloat [] {
0.0f, 0.0f, 0.0f, // 0. left-bottom-front 5.0f, 0.0f, 0.0f, // 1. right-bottom-front 0.0f, 3.0f, 0.0f, // 2. left-top-front 5.0f, 3.0f, 0.0f // 3. right-top-front
// Draw the vertices as triangle // gl.glColorPointer(4, GL10.GL_FLOAT, 0, // makeFloatBuffer(vertices_color));
// menempelkanteksturkeobjek // gl.glEnable(GL10.GL_TEXTURE_2D);
// gl.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4); gl.glDrawArrays(GL10. GL_TRIANGLE_STRIP ,
// Disable the client state before leav- ing
gl.glDisableClientState(GL10. GL_TEXTURE_COORD_ARR AY ); // Disable
gl.glDisableClientState(GL10. GL_VERTEX_ARRAY );
// gl.glDisableClientState(GL10.GL_COLOR_ARRAY); gl.glDisable(GL10. GL_BLEND ); // Disable Alpha Blend gl.glDisable(GL10. GL_TEXTURE_2D ); // gl.glDisableClientState(GL10.GL_COLOR_ARRAY); gl.glDisable(GL10. GL_BLEND ); // Disable Alpha Blend gl.glDisable(GL10. GL_TEXTURE_2D );
// Bitmap bitmap = BitmapFacto-
ry.decodeResource(context.getResources(), // resource);
Bitmap bitmap = BitmapFacto-
ry.decodeStream(context.getResources()
.openRawResource( imageFileIDs [index]));
gl.glGenTextures(1, textures_indek , 0); gl.glBindTexture(GL10. GL_TEXTURE_2D , tex-
tures_indek [0]);
gl.glTexParameterf(GLES20. GL_TEXTURE_2D , GLES20. GL_TEXTURE_MAG_FILTER , GLES20. GL_NEAREST ); gl.glTexParameterf(GLES20. GL_TEXTURE_2D , GLES20. GL_TEXTURE_MIN_FILTER , GLES20. GL_NEAREST );
GLUtils.texImage2D(GLES20. GL_TEXTURE_2D ,
0, bitmap, 0);
bitmap.recycle(); }
publicvoid loadBallTexture(GL10 gl, Context con- text, int index_Texture) {
// Bitmap bitmap = BitmapFacto-
ry.decodeResource(context.getResources(), // resource);
/* * Bitmap bitmap = BitmapFacto- ry.decodeStream(context.getResources() * .openRawResource(R.drawable.nature)); */
Bitmap bitmap = BitmapFacto-
ry.decodeStream(context.getResources()
.openRawResource( imageFileIDs [index_Texture]));
gl.glGenTextures(1, textures_indek , 0); gl.glBindTexture(GL10. GL_TEXTURE_2D , tex-
tures_indek [0]);
/* * gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, * GL10.GL_LINEAR); gl.glTexParameterf(GL10.GL_TEXTURE_2D,
* GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); */
// gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,
// GL10.GL_CLAMP_TO_EDGE ); // gl.glTexParameterf(GL10.GL_TEXTURE_2D,
GL10.GL_TEXTURE_WRAP_T, // GL10.GL_CLAMP_TO_EDGE );
gl.glTexParameterf(GLES20. GL_TEXTURE_2D , GLES20. GL_TEXTURE_MAG_FILTER , GLES20. GL_NEAREST ); gl.glTexParameterf(GLES20. GL_TEXTURE_2D , GLES20. GL_TEXTURE_MIN_FILTER , GLES20. GL_NEAREST );
// gl.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,
// GLES20.GL_REPEAT ); //
gl.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,
// GLES20.GL_REPEAT );
// /gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
// GL10.GL_NEAREST); // gl.glTexParameterf(GL10.GL_TEXTURE_2D,
GL10.GL_TEXTURE_MAG_FILTER, // GL10.GL_LINEAR);
GLUtils.texImage2D(GLES20. GL_TEXTURE_2D ,
0, bitmap, 0);
bitmap.recycle(); }
publicvoid loadBurungTexture(GL10 gl, Context context, int index_Texture) {
// Bitmap bitmap = BitmapFacto-
ry.decodeResource(context.getResources(), // resource);
/* * Bitmap bitmap = BitmapFacto- ry.decodeStream(context.getResources() * .openRawResource(R.drawable.nature)); */
Bitmap bitmap = BitmapFacto-
ry.decodeStream(context.getResources()
.openRawResource( imageFileIDs2 [index_Texture])); .openRawResource( imageFileIDs2 [index_Texture]));
tures_indek [0]);
/* * gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, * GL10.GL_LINEAR); gl.glTexParameterf(GL10.GL_TEXTURE_2D,
* GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); */
// gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, // GL10.GL_CLAMP_TO_EDGE ); // gl.glTexParameterf(GL10.GL_TEXTURE_2D,
GL10.GL_TEXTURE_WRAP_T, // GL10.GL_CLAMP_TO_EDGE );
gl.glTexParameterf(GLES20. GL_TEXTURE_2D , GLES20. GL_TEXTURE_MAG_FILTER , GLES20. GL_NEAREST ); gl.glTexParameterf(GLES20. GL_TEXTURE_2D , GLES20. GL_TEXTURE_MIN_FILTER , GLES20. GL_NEAREST );
// gl.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,
// GLES20.GL_REPEAT ); //
gl.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,
// GLES20.GL_REPEAT );
// /gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
// GL10.GL_NEAREST); // gl.glTexParameterf(GL10.GL_TEXTURE_2D,
GL10.GL_TEXTURE_MAG_FILTER, // GL10.GL_LINEAR);
GLUtils.texImage2D(GLES20. GL_TEXTURE_2D ,
0, bitmap, 0);
bitmap.recycle(); }
publicvoid loadAngkaTexture(GL10 gl, Context con- text, int index_Texture) {
// Bitmap bitmap = BitmapFacto-
ry.decodeResource(context.getResources(), // resource);
/* * Bitmap bitmap = BitmapFacto- ry.decodeStream(context.getResources()
* .openRawResource(R.drawable.nature)); */
Bitmap bitmap = BitmapFacto-
ry.decodeStream(context.getResources()
.openRawResource( imageFileIDs3 [index_Texture]));
gl.glGenTextures(1, textures_indek , 0); gl.glBindTexture(GL10. GL_TEXTURE_2D , tex-
tures_indek [0]);
gl.glTexParameterf(GLES20. GL_TEXTURE_2D , GLES20. GL_TEXTURE_MAG_FILTER , GLES20. GL_NEAREST ); gl.glTexParameterf(GLES20. GL_TEXTURE_2D , GLES20. GL_TEXTURE_MIN_FILTER , GLES20. GL_NEAREST );
GLUtils.texImage2D(GLES20. GL_TEXTURE_2D ,
0, bitmap, 0);
bitmap.recycle(); }
// gl.glGenTextures(1, textureIDs, 0); // Gener- ate texture-ID array
// gl.glBindTexture(GL10.GL_TEXTURE_2D, tex- tureIDs[0]); // Bind to texture // ID // Set up texture filters publicvoid loadTexture(GL10 gl, Context c, int
index) {
Bitmap bitmap = BitmapFacto-
ry.decodeStream(c.getResources()
.openRawResource( imageFileIDs [index])); // InputStream istream = c.getResources().openRawResource( // imageFileIDs[index]);
gl.glGenTextures(1, textures_indek , 0); gl.glBindTexture(GL10. GL_TEXTURE_2D , tex-
tures_indek [0]);
// gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,
// GL10.GL_CLAMP_TO_EDGE ); // gl.glTexParameterf(GL10.GL_TEXTURE_2D,
GL10.GL_TEXTURE_WRAP_T, // GL10.GL_CLAMP_TO_EDGE );
gl.glTexParameterf(GLES20. GL_TEXTURE_2D , GLES20. GL_TEXTURE_MAG_FILTER , GLES20. GL_NEAREST ); gl.glTexParameterf(GLES20. GL_TEXTURE_2D , GLES20. GL_TEXTURE_MAG_FILTER , GLES20. GL_NEAREST );
GLUtils.texImage2D(GLES20. GL_TEXTURE_2D ,
0, bitmap, 0);
bitmap.recycle();
TextureRegion.java
package com.mcsentul.flappybirdopengl;
class TextureRegion {
//--Members--// publicfloat u1 , v1 ; // Top/Left U,V Coordinates publicfloat u2 , v2 ; // Bottom/Right U,V Coordinates
//--Constructor--// // D: calculate U,V coordinates from specified texture coordinates // A: texWidth, texHeight - the width and height of the texture the region is for // x, y - the top/left (x,y) of the region on the texture (in pixels) // width, height - the width and height of the region on the texture (in pixels) public TextureRegion( float texWidth, float texHeight, float x, float y, float width, float height) { this . u1 = x / texWidth; // Calculate U1 this . v1 = y / texHeight; // Calculate V1 this . u2 = this . u1 + ( width / texWidth ); // Calculate U2 this . v2 = this . v1 + ( height / texHeight ); // Calculate V2
Vertices.java
package com.mcsentul.flappybirdopengl;
import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.IntBuffer; import java.nio.ShortBuffer;
import javax.microedition.khronos.opengles.GL10;
publicclass Vertices {
//--Constants--// finalstaticint POSITION_CNT_2D = 2; // Number of Components in Vertex Position for
2D finalstaticint POSITION_CNT_3D = 3; // Number of Components in Vertex Position for
3D finalstaticint COLOR_CNT = 4; // Number of Components in Vertex Color finalstaticint TEXCOORD_CNT = 2; // Number of Components in Vertex Texture Coords finalstaticint NORMAL_CNT = 3;
// Number of Components in Vertex Normal
finalstaticint INDEX_SIZE = Short. SIZE / 8; // Index Byte Size (Short.SIZE = bits)
//--Members--// // NOTE: all members are constant, and initial- ized in constructor! final GL10 gl ; // GL Instance finalboolean hasColor ; // Use Color in Vertices finalboolean hasTexCoords ; // Use Texture Coords in Vertices finalboolean hasNormals ; // Use Normals in Vertices publicfinalint positionCnt ; // Number of Position Components (2=2D, 3=3D) publicfinalint vertexStride ; // Vertex Stride (Element Size of a Single Ver- tex) publicfinalint vertexSize ; // Bytesize of a Single Vertex final IntBuffer vertices ; // Vertex Buffer //--Members--// // NOTE: all members are constant, and initial- ized in constructor! final GL10 gl ; // GL Instance finalboolean hasColor ; // Use Color in Vertices finalboolean hasTexCoords ; // Use Texture Coords in Vertices finalboolean hasNormals ; // Use Normals in Vertices publicfinalint positionCnt ; // Number of Position Components (2=2D, 3=3D) publicfinalint vertexStride ; // Vertex Stride (Element Size of a Single Ver- tex) publicfinalint vertexSize ; // Bytesize of a Single Vertex final IntBuffer vertices ; // Vertex Buffer
//--Constructor--// // D: create the vertices/indices as specified (for 2d/3d) // A: gl - the gl instance to use // maxVertices - maximum vertices allowed in buffer // maxIndices - maximum indices allowed in buffer // hasColor - use color values in vertices // hasTexCoords - use texture coordinates in vertices // hasNormals - use normals in vertices // use3D - (false, default) use 2d positions (ie. x/y only) // (true) use 3d positions (ie. x/y/z) public Vertices(GL10 gl, int maxVertices, int maxIndices, boolean hasColor, boolean hasTex- Coords, boolean hasNormals) { this ( gl, maxVertices, maxIndices, hasColor, hasTexCoords, hasNormals, false ); // Call Overloaded Constructor
} public Vertices(GL10 gl, int maxVertices, int maxIndices, boolean hasColor, boolean hasTex- Coords, boolean hasNormals, boolean use3D) { this . gl = gl; // Save GL Instance this . hasColor = hasColor; // Save Color Flag this . hasTexCoords = hasTexCoords; // Save Texture Coords Flag this . hasNormals = hasNormals; // Save Normals Flag this . positionCnt = use3D ? POSITION_CNT_3D : POSITION_CNT_2D ; // Set Position Component Count this . vertexStride = this . positionCnt + ( has- Color ? COLOR_CNT : 0 ) + ( hasTexCoords ? TEXCOORD_CNT : 0 ) + ( hasNormals ? NORMAL_CNT :
0 ); // Calculate Vertex Stride this . vertexSize = this . vertexStride * 4; // Calculate Vertex Byte Size
ByteBuffer buffer = ByteBuff- er.allocateDirect( maxVertices * vertexSize ); // Allocate Buffer for Vertices (Max)
buffer.order( ByteOrder.nativeOrder() ); // Set Native Byte Order this . vertices = buffer.asIntBuffer(); // Save Vertex Buffer
if ( maxIndices > 0 ) { // IF Indices Required
buffer = ByteBuffer.allocateDirect( maxIndices * INDEX_SIZE ); // Allocate Buffer for Indices (MAX)
buffer.order( ByteOrder.nativeOrder() );
// Set Native Byte Order this . indices = buffer.asShortBuffer();
// Save Index Buffer } else // ELSE Indices Not Required indices = null ;
// No Index Buffer
numVertices = 0; // Zero Vertices in Buffer numIndices = 0; // Zero Indices in Buffer
this . tmpBuffer = newint [maxVertices * vertexSize / 4]; // Create Temp Buffer
//--Set Vertices--// // D: set the specified vertices in the vertex buffer // NOTE: optimized to use integer buffer! // A: vertices - array of vertices (floats) to set // offset - offset to first vertex in array // length - number of floats in the vertex array (total) // for easy setting use: vtx_cnt * (this.vertexSize / 4) // R: [none] publicvoid setVertices( float [] vertices, int offset, int length) { this . vertices .clear(); // Remove Existing Vertices int last = offset + length; // Calculate Last Element for ( int i = offset, j = 0; i < last; i++, j++ ) // FOR Each Specified Vertex tmpBuffer [j] = Float.floatToRawIntBits( verti- ces[i] ); // Set Vertex as Raw Integer Bits in
Buffer this . vertices .put( tmpBuffer , 0, length ); // Set New Vertices this . vertices .flip(); // Flip Vertex Buffer this . numVertices = length / this . vertexStride ; // Save Number of Vertices //this.numVertices = length / ( this.vertexSize / 4 ); // Save Number of Vertices
//--Set Indices--// // D: set the specified indices in the index buffer // A: indices - array of indices (shorts) to set // offset - offset to first index in array // length - number of indices in array (from offset) // R: [none] publicvoid setIndices( short [] indices, int off- set, int length) { this . indices .clear(); // Clear Existing Indices this . indices .put( indices, offset, length ); // Set New Indices this . indices .flip(); // Flip Index Buffer this . numIndices = length; // Save Number of Indices
//--Bind--// // D: perform all required binding/state changes before rendering batches. // USAGE: call once before calling draw() multiple times for this buffer. // A: [none] // R: [none] publicvoid bind() { gl .glEnableClientState( GL10. GL_VERTEX_ARRAY ); // Enable Position in Vertices vertices .position( 0 ); // Set Vertex Buffer to Position gl .glVertexPointer( positionCnt , GL10. GL_FLOAT , vertexSize , vertices ); // Set Vertex Pointer
if ( hasColor ) { // IF Vertices Have Color gl .glEnableClientState( GL10. GL_COLOR_ARRAY ); // Enable Color in Vertices vertices .position( positionCnt );
// Set Vertex Buffer to Color gl .glColorPointer( COLOR_CNT , GL10. GL_FLOAT , // Set Vertex Buffer to Color gl .glColorPointer( COLOR_CNT , GL10. GL_FLOAT ,
if ( hasTexCoords ) { // IF Vertices Have Texture Coords gl .glEnableClientState( GL10. GL_TEXTURE_COORD_ARRAY ); // Enable Tex- ture Coords in Vertices vertices .position( positionCnt +( hasColor ? COLOR_CNT : 0 ) ); // Set Vertex Buffer to Tex- ture Coords (NOTE: position based on whether color is also specified) gl .glTexCoordPointer( TEXCOORD_CNT , GL10. GL_FLOAT , vertexSize , vertices ); // Set Texture Coords Pointer
if ( hasNormals ) { gl .glEnableClientState( GL10. GL_NORMAL_ARRAY ); // Enable Normals in Vertices vertices .position( positionCnt +( hasColor ? COLOR_CNT :0)+( hasTexCoords ? TEXCOORD_CNT : 0 ) ); // Set Vertex Buffer to Normals (NOTE: position based on whether color/texcoords is al- so specified) gl .glNormalPointer( GL10. GL_FLOAT , vertexSize , vertices ); // Set Normals Pointer
//--Draw--// // D: draw the currently bound vertices in the vertex/index buffers // USAGE: can only be called after calling bind() for this buffer. // A: primitiveType - the type of primitive to draw // offset - the offset in the vertex/index buffer to start at // numVertices - the number of vertices (in- dices) to draw // R: [none] publicvoid draw( int primitiveType, int offset, int numVertices) { if ( indices != null ) { // IF Indices Exist indices .position( offset );
// Set Index Buffer to Specified Offset gl .glDrawElements( primitiveType, numVertices, GL10. GL_UNSIGNED_SHORT , indices ); // Draw In- dexed
} else {
// ELSE No Indices Exist gl .glDrawArrays( primitiveType, offset, numVer- tices ); // Draw Direct (Array)
//--Unbind--// // D: clear binding states when done rendering batches. // USAGE: call once before calling draw() multiple times for this buffer. // A: [none] // R: [none] publicvoid unbind() { if ( hasColor ) // IF Vertices Have Color gl .glDisableClientState( GL10. GL_COLOR_ARRAY ); // Clear Color State
if ( hasTexCoords ) // IF Vertices Have Texture Coords gl .glDisableClientState( GL10. GL_TEXTURE_COORD_ARRAY ); // Clear Texture Coords State
if ( hasNormals ) // IF Vertices Have Normals
gl .glDisableClientState( GL10. GL_NORMAL_ARRAY ); // Clear Normals State
//--Draw Full--// // D: draw the vertices in the vertex/index buffers // NOTE: unoptimized version! use bind()/draw()/unbind() for batches // A: primitiveType - the type of primitive to draw // offset - the offset in the vertex/index buffer to start at // numVertices - the number of vertices (in- dices) to draw // R: [none] publicvoid drawFull( int primitiveType, int off- set, int numVertices) { gl .glEnableClientState( GL10. GL_VERTEX_ARRAY ); // Enable Position in Vertices vertices .position( 0 ); // Set Vertex Buffer to Position gl .glVertexPointer( positionCnt , GL10. GL_FLOAT , vertexSize , vertices ); // Set Vertex Pointer
if ( hasColor ) {
// IF Vertices Have Color gl .glEnableClientState( GL10. GL_COLOR_ARRAY ); // Enable Color in Vertices vertices .position( positionCnt );
// Set Vertex Buffer to Color gl .glColorPointer( COLOR_CNT , GL10. GL_FLOAT , vertexSize , vertices ); // Set Color Pointer
if ( hasTexCoords ) { // IF Vertices Have Texture Coords gl .glEnableClientState( GL10. GL_TEXTURE_COORD_ARRAY ); // Enable Tex- ture Coords in Vertices vertices .position( positionCnt +( hasColor ? COLOR_CNT : 0 ) ); // Set Vertex Buffer to Tex- ture Coords (NOTE: position based on whether color is also specified) gl .glTexCoordPointer( TEXCOORD_CNT , GL10. GL_FLOAT , vertexSize , vertices ); // Set Texture Coords Pointer
if ( indices != null ) { // IF Indices Exist indices .position( offset );
// Set Index Buffer to Specified Offset gl .glDrawElements( primitiveType, numVertices, GL10. GL_UNSIGNED_SHORT , indices ); // Draw In- dexed
} else { // ELSE No Indices Exist gl .glDrawArrays( primitiveType, offset, numVer- tices ); // Draw Direct (Array)
if ( hasTexCoords ) // IF Vertices Have Texture Coords gl .glDisableClientState( GL10. GL_TEXTURE_COORD_ARRAY ); // Clear Texture Coords State
if ( hasColor ) // IF Vertices Have Color gl .glDisableClientState( GL10. GL_COLOR_ARRAY ); // Clear Color State
//--Set Vertex Elements--// // D: use these methods to alter the values (po- sition, color, textcoords, normals) for vertices // WARNING: these do NOT validate any values, //--Set Vertex Elements--// // D: use these methods to alter the values (po- sition, color, textcoords, normals) for vertices // WARNING: these do NOT validate any values,
} void setVtxPosition( int vtxIdx, float x, float y, float z) { int index = vtxIdx * vertexStride ; // Calculate Actual Index vertices .put( index + 0, Float.floatToRawIntBits( x ) ); // Set X vertices .put( index + 1, Float.floatToRawIntBits( y ) ); // Set Y vertices .put( index + 2, Float.floatToRawIntBits( z ) ); // Set Z
} void setVtxColor( int vtxIdx, float r, float g,
float b, float
a) { int index = ( vtxIdx * vertexStride )+ posi-
tionCnt ; // Calculate Actual Index vertices .put( index + 0, Float.floatToRawIntBits( r ) ); // Set Red vertices .put( index + 1, Float.floatToRawIntBits( g ) ); // Set Green vertices .put( index + 2, Float.floatToRawIntBits( b ) ); // Set Blue vertices .put( index + 3, Float.floatToRawIntBits( a ) ); // Set Alpha
} void setVtxColor( int vtxIdx, float r, float g,
float
b) { int index = ( vtxIdx * vertexStride )+ posi- tionCnt ; // Calculate Actual Index vertices .put( index + 0, Float.floatToRawIntBits( r ) ); // Set Red vertices .put( index + 1, Float.floatToRawIntBits( g ) ); // Set Green vertices .put( index + 2,
Float.floatToRawIntBits( b ) ); // Set Blue }
void setVtxColor( int vtxIdx, float
a) { int index = ( vtxIdx * vertexStride )+ posi- tionCnt ; // Calculate Actual Index vertices .put( index + 3, Float.floatToRawIntBits( a ) ); // Set Alpha
} void setVtxTexCoords( int vtxIdx, float u, float
v) { int index = ( vtxIdx * vertexStride )+ posi- tionCnt +( hasColor ? COLOR_CNT : 0 ); // Cal-
culate Actual Index vertices .put( index + 0, Float.floatToRawIntBits( u ) ); // Set U vertices .put( index + 1, Float.floatToRawIntBits( v ) ); // Set V
} void setVtxNormal( int vtxIdx, float x, float y, float z) { int index = ( vtxIdx * vertexStride )+ posi- tionCnt +( hasColor ? COLOR_CNT :0)+( hasT- exCoords ? TEXCOORD_CNT : 0 ); // Calculate Ac- tual Index vertices .put( index + 0, Float.floatToRawIntBits( x ) ); // Set X vertices .put( index + 1, Float.floatToRawIntBits( y ) ); // Set Y vertices .put( index + 2, Float.floatToRawIntBits( z ) ); // Set Z