1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
#ifndef BSLAYER_CPP_
#define BSLAYER_CPP_
#include <GL/gl.h>
#include <GL/glut.h>
#include <iostream>
#include<string>
#include<cmath> // for basic math functions such as cos, sin, sqrt
using namespace std;
//defining some MACROS
#define MAX(A,B) ((A) > (B) ? (A):(B)) // finds max of two numbers
#define MIN(A,B) ((A) < (B) ? (A):(B)) // find min of two numbers
#define ABS(A) ((A) < (0) ? -(A):(A)) // find ABS of a given number
// define some constants
// constant FPS (Frame per second) is used to specify
// number of frames per second in your game.. if FPS is small
// your objects will move slowly and if large your objects
// will move faster, see the Timer function below for further help
#define FPS 80 // frame per seconds
// define another constant to hold ASCII for Escape key.
#define KEY_ESC 27 // A
// define some colors as 2D arrays, we have five pre-defined colors here..
// you can add many more if you wish..
float colors[5][3] = { { 1 / 255.0, 164 / 255.0, 164 / 255.0 }, { 215 / 255.0, 0
/ 255.0, 96 / 255.0 }, { 208 / 255.0, 209 / 255.0, 2 / 255.0 }, { 0
/ 255.0, 161 / 255.0, 203 / 255.0 }, { 50 / 255.0, 116 / 255.0, 44
/ 255.0 } };
// defining some utility functions...
/*
* This function converts an input angle from degree to radians */
float Deg2rad(float degree) {
return (degree / 180.0) * M_PI;
}
// seed the random numbers generator by current time (see the documentation of srand for further help)...
void InitRandomizer() {
srand((unsigned int) time(0)); // time(0) returns number of seconds elapsed since January 1, 1970.
}
//This function returns a random value within the specified range of [rmin, rmax] ...
long GetRandInRange(const long &rmin, const long &rmax) {
long range = rmax - rmin; // find the range
long value = (rand() % (long) range) + rmin; // translate the generated number ...
// cout << value << endl << flush;
return value;
}
/*To draw a triangle we need three vertices with each vertex having 2-coordinates [x, y] and a color for the triangle.
* This function takes 4 arguments first three arguments (3 vertices + 1 color) to
* draw the triangle with the given color.
* */
void DrawTriangle(int x1, int y1, int x2, int y2, int x3, int y3,
float color[]) {
glColor3fv(color); // Set the triangle colour
// ask library to draw triangle at given position...
glBegin(GL_TRIANGLES);
/*Draw triangle using given three vertices...*/
glVertex4i(x1, y1, 0, 1);
glVertex4i(x2, y2, 0, 1);
glVertex4i(x3, y3, 0, 1);
glEnd();
}
/*
* Write two functions DrawRectangle and DrawSphere
* */
/*
* Main Canvas drawing function.
* */
void Display()/**/{
// set the background color using function glClearColor.
// to change the background play with the red, green and blue values below.
// Note that r, g and b values must be in the range [0,1] where 0 means dim rid and 1 means pure red and so on.
glClearColor(1/*Red Component*/, 1.0/*Green Component*/,
1.0/*Blue Component*/, 0 /*Alpha component*/); // Red==Green==Blue==1 --> White Colour
glClear(GL_COLOR_BUFFER_BIT); //Update the colors
//write your drawing commands here or call your drawing functions...
// Drawing set of three triangles at different points of a circle using simple trignometry...
int i=150;
int u=839;
int a=1;
while(u>360)
{
if(a==1) // to insert colour
a=2;
else if(a==2)
a=1;
while(i<800) //to draw the bricks
{
DrawTriangle(i, u, i, u-29, i+70, u, colors[a]);
DrawTriangle(i+70, u, i, u-29, i+70, u-29, colors[a]);
i=i+71;
}
i=150;
u=u-31;
}
//to draw a ball
int midx = 110;
int midy = 50;
//for ( float theta = Deg2rad(100); theta != 360 ; theta++ )
float theta = Deg2rad(10);
float theta2 = Deg2rad(30);
while ( theta <=13720 )
{
DrawTriangle(midx, midy, midx + 10 * cos(theta),
midy + 10* sin(theta), midx + 10 * cos(theta2),
midy + 10 * sin(theta2), colors[1]);
theta=theta+10;
theta2=theta2+10;
}
DrawTriangle(30, 15, 30, 5, 120, 15, colors[1]);
DrawTriangle(120, 15, 30, 5, 120, 5, colors[1]);
//
glutSwapBuffers(); // do not modify this line..
}
/* Function sets canvas size (drawing area) in pixels...
* that is what dimensions (x and y) your game will have
* Note that the bottom-left coordinate has value (0,0) and top-right coordinate has value (width-1,height-1)
* */
void SetCanvasSize(int width, int height) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1); // set the screen size to given width and height.
}
/*This function is called (automatically) whenever any non-printable key (such as up-arrow, down-arraw)
* is pressed from the keyboard
*
* You will have to add the necessary code here when the arrow keys are pressed or any other key is pressed...
*
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
*
* */
void NonPrintableKeys(int key, int x, int y) {
if (key == GLUT_KEY_LEFT /*GLUT_KEY_LEFT is constant and contains ASCII for left arrow key*/) {
// what to do when left key is pressed...
} else if (key == GLUT_KEY_RIGHT /*GLUT_KEY_RIGHT is constant and contains ASCII for right arrow key*/) {
} else if (key == GLUT_KEY_UP/*GLUT_KEY_UP is constant and contains ASCII for up arrow key*/) {
}
else if (key == GLUT_KEY_DOWN/*GLUT_KEY_DOWN is constant and contains ASCII for down arrow key*/) {
}
/* This function calls the Display function to redo the drawing. Whenever you need to redraw just call
* this function*/
/*
glutPostRedisplay();
*/
}
/*This function is called (automatically) whenever any printable key (such as x,b, enter, etc.)
* is pressed from the keyboard
* This function has three argument variable key contains the ASCII of the key pressed, while x and y tells the
* program coordinates of mouse pointer when key was pressed.
* */
void PrintableKeys(unsigned char key, int x, int y) {
if (key == KEY_ESC/* Escape key ASCII*/) {
exit(1); // exit the program when escape key is pressed.
}
}
/*
* This function is called after every 1000.0/FPS milliseconds
* (FPS is defined on in the beginning).
* You can use this function to animate objects and control the
* speed of different moving objects by varying the constant FPS.
*
* */
void Timer(int m) {
// implement your functionality here
// once again we tell the library to call our Timer function after next 1000/FPS
glutTimerFunc(1000.0 / FPS, Timer, 0);
}
/*
* our gateway main function
* */
int main(int argc, char*argv[]) {
int width = 1000, height = 900; // i have set my window size to be 800 x 600
InitRandomizer(); // seed the random number generator...
glutInit(&argc, argv); // initialize the graphics library...
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); // we will be using color display mode
glutInitWindowPosition(50, 50); // set the initial position of our window
glutInitWindowSize(width, height); // set the size of our window
glutCreateWindow("ITCs Brick Slayer"); // set the title of our game window
SetCanvasSize(width, height); // set the number of pixels...
// Register your functions to the library,
// you are telling the library names of function to call for different tasks.
glutDisplayFunc(Display); // tell library which function to call for drawing Canvas.
glutSpecialFunc(NonPrintableKeys); // tell library which function to call for non-printable ASCII characters
glutKeyboardFunc(PrintableKeys); // tell library which function to call for printable ASCII characters
// This function tells the library to call our Timer function after 1000.0/FPS milliseconds...
glutTimerFunc(1000.0 / FPS, Timer, 0);
// now handle the control to library and it will call our registered functions when
// it deems necessary...
glutMainLoop();
return 1;
}
#endif /* BSLAYER_CPP_ */
| |