Making of a Pokemon game – Day 3

-Objective:

1. Implement graphics using freeglut and generate windows graphic

2. Change the output to the windows output

 

-Files:

main.cpp

graphics.hpp

dialogueFunctions.hpp

battleMenu.hpp

- Steps:

1. Put freeglut library and header in the project

2. include it in main

3. include graphics.hpp in the project, which contains all the graphic rendering related functions

 

-main.cpp:

#define GLUT_DISABLE_ATEXIT_HACK

#define FREEGLUT_STATIC

 

#include <iostream>

#include <vector>

#include <fstream>

#include "GL/freeglut.h"

 

using namespace std;

    string userMode = "DIALOGUE";

    string dialogueStr = "";

    vector <string> dialogueQueue;

    int dialogueLineNumber = 0;

#include "dialogueFunctions.hpp"

#include "monster.hpp"

#include "person.hpp"

#include "battleMenu.hpp"

#include "map.hpp"

 

//globals

    Hero* hero = new Hero;

    Monster* pMonster;

 

//For Graphics

#include "graphics.hpp"

 

 

 

int main (int argc, char* argv[])

{

    //Initialize Dialogue

    ifstream myfile;

    myfile.open ("dialogue.txt");

    string output;

    string userInput;

    string userName;

 

    int i = 0;

    while (!myfile.eof()) {

        getline(myfile,output);

        dialogueQueue.push_back(output + "\n");

        i++;

    }

 

    //Initialize the gamescreen

    initialize_screen(argc, argv);

 

 

    return 0;

}

 

-graphics.hpp design:

void glEnable2D()

{

    int vPort[4];

    glGetIntegerv(GL_VIEWPORT, vPort); // Get a copy of the viewport

 

    // Save a copy of the projection matrix so that we can restore it

    // when it's time to do 3D rendering again.

    glMatrixMode(GL_PROJECTION);

    glPushMatrix();

    glLoadIdentity();

    glOrtho(0, vPort[2], vPort[3], 0, -1, 1); // Set up the orthographic projection

 

    glMatrixMode(GL_MODELVIEW);

    glPushMatrix();

    glLoadIdentity();

 

    // Make sure depth testing and lighting are disabled for 2D rendering until

    // we are finished rendering in 2D

    glPushAttrib(GL_DEPTH_BUFFER_BIT|GL_LIGHTING_BIT);

    glDisable(GL_DEPTH_TEST);

    glDisable(GL_LIGHTING);

}

 

void glDisable2D()

{

    glPopAttrib();

    glMatrixMode(GL_MODELVIEW);

    glPopMatrix();

    glMatrixMode(GL_PROJECTION);

    glPopMatrix();

}

 

void drawString(string msg,int x, int y) {

    glEnable2D();

 

    // Draw text on screen at (x,y) where top-left is (0,0)

    // in an 18-point Helvetica font

    // TODO: dynamically load text here and center it

    glColor3f(0.8, 0.9, 0.0);

    glRasterPos2f(x, y);//x,y

    glutBitmapString(GLUT_BITMAP_HELVETICA_18, (const GLubyte*)msg.c_str());

 

    glDisable2D();

}

 

 

void process_key(unsigned char key, int x, int y)

{

    switch(key)

    {

        case '1':

            if (userMode == "BATTLE")

            {

                Fight();

            }

            break;

        case '2':

            if (userMode == "BATTLE")

            {

                Catch(hero,pMonster);

            }

            break;

        case '3':

            if (userMode == "BATTLE")

            {

                Run();

            }

            break;

        case 13:

            if (userMode == "DIALOGUE")

            {

                cout << "enter"<< endl;

                string output = dialogueQueue[dialogueLineNumber];

                if (output == "<USERNAME>\n")

                {

                    dialogueStr += "What is your name young pokemon trainer? \n";

                    //cin >> userName;

                    dialogueStr += "So your name is  _________ ?\n";

                }

                else if (output == "<HERO_INIT>\n")

                {

                    hero -> SetName("Ash Ketchum");

                    dialogueStr += hero -> GetName() + "\n";

                }

                else if (output == "<BATTLE>\n")

                {

                    pMonster = new Pikachu();

                    battleMenu(hero,pMonster);

                }

                else

                {

                    dialogueStr += dialogueQueue[dialogueLineNumber];

                }

                dialogueLineNumber ++;

            }

            break;

    }

    glutPostRedisplay();

}

 

void process_special_key(int key, int x, int y)

{

    switch (key) {

    case GLUT_KEY_RIGHT:

    // move right

    break;

    case GLUT_KEY_LEFT:

    // move left

    break;

    case GLUT_KEY_UP:

    // move up

    break;

    case GLUT_KEY_DOWN:

    // move down

    break;

    case GLUT_KEY_F2:

    // TODO: set each Fn key to diff configurations of shapes/positions

    break;

    case GLUT_KEY_F12:

    break;

    default: break;

    }

}

 

void process_mouse(int button, int state, int x, int y)

{

    switch (button) {

    case GLUT_LEFT_BUTTON:

    break;

    default: break;

    }

}

 

void render_scene() {

    glMatrixMode(GL_MODELVIEW);

    // clear all pixels to clear colour

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    //-----------------------my edit starts here ---------------------------------//

    drawString(dialogueStr,50,50);

    //---------------------------------------------------------------------------//

    glFlush(); // draw graphics in buffer immediately

    glutSwapBuffers(); // switch buffers for double buffering

}

 

void resize_scene(int w, int h)

{

    if (h == 0)

    h = 1; // DON'T divide by zero - please?

    GLfloat ratio = 1.0 * w / h;

    glViewport(0,0,w,h); // set the viewport to be the entire window

 

    // reset the coordinate system

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

 

    // set the correct perspective

    gluPerspective(45,ratio,1,1000);

    //GLdouble top = tan(0.5*PI/4.0)*1.0; // top = tan(fov*0.5)*near

    //glFrustum(ratio*(-top),ratio*top,-top,top,1.0,1000.0);

 

    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();

    // perspective transformation

    // LookAt(eyeX,eyeY,eyeZ, eye point

    // centerX,centerY,centerZ, reference point

    // upX,upY,upZ) "up" vector direction

    gluLookAt(0,0,0,

    0,0,-1,

    0,1,0);

 

 

    glutPostRedisplay();

}

 

void idle_event() {

 

}

 

 

void initialize_screen(int argc, char* argv[]) {

    // initialize (free)GLUT/OpenGL

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    GLint init_win_size_x = 640, init_win_size_y = 480;

    glutInitWindowSize(init_win_size_x, init_win_size_y);

    GLint init_win_pos_x = (glutGet(GLUT_SCREEN_WIDTH) - init_win_size_x) / 2;

    GLint init_win_pos_y = (glutGet(GLUT_SCREEN_HEIGHT) - init_win_size_y) / 2;

    cout << "Screen Size: " << glutGet(GLUT_SCREEN_WIDTH) << " " << glutGet(GLUT_SCREEN_HEIGHT) << endl;

    cout << "Window Size: " << init_win_size_x << " " << init_win_size_y << endl;

    cout << "Window Coords: " << init_win_pos_x << " " << init_win_pos_y << endl;

    glutInitWindowPosition(init_win_pos_x, init_win_pos_y);

    GLint window_id = glutCreateWindow("Walking Around");

 

    // register callbacks

    glutDisplayFunc(render_scene);

    glutReshapeFunc(resize_scene);

    glutKeyboardFunc(process_key);

    glutSpecialFunc(process_special_key);

    glutMouseFunc(process_mouse);

    glutIdleFunc(idle_event);

 

    glutMainLoop();

    glutDestroyWindow(window_id);

 

}

 

-dialogueFunctions.hpp design:

void updateDialogue(string str)

{

    dialogueStr += str;

    glutPostRedisplay();

}

 

-battleMenu.hpp:

void Fight()

{

    updateDialogue("Fight! \n");

    userMode = "DIALOGUE";

}

void Catch(Hero *hero,Monster *monster)

{

    updateDialogue("Catch! \n");

    hero->catchMonster(monster);

    updateDialogue("successfully caught\n");

    userMode = "DIALOGUE";

}

void Run()

{

    updateDialogue("Run! \n");

    userMode = "DIALOGUE";

}

 

Hero* battleMenu(Hero *hero, Monster *monster)

{

    userMode = "BATTLE";

    bool battleExit = false;

    updateDialogue("--------Battle Start --------\n");

    updateDialogue(monster -> GetName()+"\n");

    monster -> makeSound();

    updateDialogue(monster -> GetName() +": \n");

    monster -> yell();

 

    updateDialogue("1. Fight\n" );

    updateDialogue("2. Catch\n");

    updateDialogue("3. Run\n" );

 

return hero;

}

 

 

 

Result:

step3.png