Minggu, 02 November 2014

Codingan Menu Garis

#include <GL/glut.h>
#include <conio.h>
#include <iostream>

using namespace std;

//clear screen
void cls()
{
    cout << string(25, '\n');
}

//variabel yang digunakan untuk menyimpan kordinat awal dan akhir
int tx0,ty0,tx1,ty1;
//k adalah kondisi untuk input. Sesuai atau tidak dengan yang di minta.
int k;

//Initialize OpenGL
void init(void)
{
    glClearColor(0.0,0.0,0.0,0.0);
    glMatrixMode(GL_PROJECTION);
    //gluOrtho2D ini menentukan lokasi titik 0,0
    //nilai yang di atas itu berarti titik 0,0 nya ditengah
    //ada jarak 50 pixel di sebelah kiri titik 0,0 sebelum
    //mencapai pojok kiri layar. Begitu juga di kanan
    //atas dan bawah
    gluOrtho2D(-50.0,50.0,-50.0,50.0);
}

//drawing the line
void display(void)
{
    glClear (GL_COLOR_BUFFER_BIT);
    //line di bawah itu menentukan warna garis
    glColor3f(255.0,255.0,255.0);
    glLineWidth(7.0f);
    //mulai menggambar line
    glBegin(GL_LINES);
    //glVertex2d itu untuk menentukan kordinat titiknya.
    //2d berarti 2 dimensi dan angka yang dimasukkin bertipe double
    //3f berarti 3 dimensi dan bertipe float
        glVertex2d(tx0, ty0); //titik awal
        glVertex2d(tx1, ty1); //titik akhir
    //akhir penggambaran
    glEnd ();
    glFlush();
}

/*
//Untuk keluar dari window opengl
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
close(0); //Exit the application if 'Esc' key is pressed
}
}*/

//Are we done yet?
//Obviously, no.

//inputpart --> fungsi input kordinat.
int inputpart2();
void inputpart();
//3 if dibawah menentukan apakah kordinat yang dimasukkan sesuai
//dengan yang diminta.
void horizonif();
void vertikaif();
void diagonaif();

//Finally, the lovely Main
int main(int argc, char**argv)
{
    k = 0;
    while(k!=1)
    {
        if (k==2)
        {
            cls();
        }
        int pil;
        cout << "This the part when I make you some line\n\n";
        cout << "1. Horizontal line\n";
        cout << "2. Vertikal line\n";
        cout << "3. Diagonal line\n";
        cout << "4. Exit\n";
        cout << "Which one do you want [1..4] : "; cin >> pil;
        cout << endl;
        switch (pil)
        {
            case 1:
                cout << "Horizontal Line?\n\n";
                inputpart();
                horizonif();
                break;
            case 2:
                cout << "Vertical Line?\n\n";
                inputpart();
                vertikaif();
                break;
            case 3:
                cout << "Diagonal Line?\n\n";
                inputpart();
                diagonaif();
                break;
            case 4:
                cout << "BYE!!! or bae. Whatever that means";
                return 0;
            default:
                cout << "What's that? Can't hear you, mate.\n\n";
                getch();
        }
    }
    glutInit(&argc, argv);
    glutInitWindowPosition(100, 100); //posisi window opengl di desktop
    glutInitWindowSize(500, 500); //size window opengl di desktop
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutCreateWindow("GARIS"); //membuat window sekaligus judulnya
    init();
    //glutKeyboardFunc(keyboard); //biar kalau tekan esc bisa keluar dari window opengl
    glutDisplayFunc(display); //menampilkan hasil garis
    glutMainLoop(); //something something rendering
}

void inputpart()
{
    cout << "What's the starting coordinate ?[max is 50/-50]\n";
        //memasukkan titik awal
        cout << "X = "; tx0 = inputpart2();
        cout << "Y = "; ty0 = inputpart2();
        cout << "OK, now what's the end coordinate ?\n";
        //memasukkan titik akhir
        cout << "X = "; tx1 = inputpart2();
        cout << "Y = "; ty1 = inputpart2();
        //ini tempat menentukan horizontal apa enggak garisnya
}

int inputpart2()
{
    int xy;
    do {
        cin >> xy;
    } while(xy > 50 || xy < -50);
    return xy;
}

void horizonif()
{
    if (ty0==ty1)
    {
        cout << "Wundebar!, it's.. an Hline. Reminds me of a particularly messy Battlefield" << endl;
        cout << "Don't forget to press Esc to exit OpenGL window!" << endl;
        k=1;
    }
    else
    {
        cout << "It's not a horizontal line\n" << "Give me a horizontal one.\n\n";
        k=2;
    }
    getch();
}

void vertikaif()
{
    if (tx0==tx1)
    {
        cout << "Wundebar!, it's.. a vline\n" << "Haha.. Geddit? I spell it with a V..\n" << "Nevermind." << endl;
        cout << "You can click the X in the upper right corner of the window with your mouse." << endl;
        cout << "But why do that if you can press Esc!" << endl;
        k=1;
    }
    else
    {
        cout << "It's not a vertical line\n" << "Give me some verticality.\n\n";
        k = 2;
    }
    getch();
}

void diagonaif()
{
    if (tx0!=tx1 && ty0!=ty1)
    {
        cout << "Wundebar!, it's.. a Dline\n" << "I can make some puns about it but I'm about to dine." << endl;
        cout << "Seriously though, don't forget to press Esc to exit OpenGL window." << endl;
        cout << "And then press Enter to exit the cmd. Why? because it's text based." << endl;
        cout << "Try to forget about the existence of your mouse for a second" << endl;
        cout << "You'll be grateful Steve Jobs popularise it." << endl;
        cout << "Don't forget to have fun!" << endl;
        k=1;
    }
    else
    {
        cout << "It's not a diagonal line\n" << "Give me a diagonal one.\n\n";
        k=2;
    }
    getch();
}

Tidak ada komentar:

Posting Komentar

Ikuti @baddyisme