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
|
#include <Windows.h>
#include <string>
#include <iostream>
#include <gl\freeglut.h>
#include <gl\GL.h>
#include <gl\GLU.h>
int keys[5];
float forcex = 0.0f; // 0.0f = no force, 1.0f = full force, -1.0f = reverse force
float forcey = 0.0f;
#define UP 0 // Constants for key array
#define LEFT 1
#define DOWN 2
#define RIGHT 3
#define STOP 4
void setup(void); // Set up OpenGL
void display(void); // OpenGL display function
void logic(int value); // Logic function
void physics(int value); // Physics function
void keydown(unsigned char key, int x, int y); // Keydown function
void keyup(unsigned char key, int x, int y); // Keyup function
struct __Bounds
{
int Left;
int Right;
int Up;
int Down;
};
class __Square
{
public:
__Bounds Bounds;
__Square()
{
Bounds.Left = 0;
Bounds.Right = 20;
Bounds.Up = 0;
Bounds.Down = 20;
}
};
__Square Square;
int main (int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(400, 400);
glutCreateWindow("Game Engine");
setup();
glutDisplayFunc(display);
glutTimerFunc(1000/100, logic, 1);
glutTimerFunc(1000/100, physics, 2);
glutKeyboardFunc(keydown);
glutKeyboardUpFunc(keyup);
glutMainLoop();
return 0;
}
void setup(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.0f, 1.0f, 0.0f);
glRasterPos2f(0.0f, 0.0f);
glRectf((GLfloat)Square.Bounds.Left/400, (GLfloat)Square.Bounds.Up/400, (GLfloat)Square.Bounds.Right/400, (GLfloat)Square.Bounds.Down/400);
glutSwapBuffers();
}
void logic(int value)
{
if(keys[UP] == 1)
{
forcey += 0.001f;
}
else if(keys[LEFT] == 1)
{
forcex -= 0.001f;
}
else if(keys[DOWN] == 1)
{
forcey -= 0.001f;
}
else if(keys[RIGHT] == 1)
{
forcex += 0.001f;
}
else if(keys[STOP] == 1)
{
forcex = 0.0f;
forcey = 0.0f;
}
glutTimerFunc(1000/100, logic, 1);
}
void physics(int value)
{
if(forcex > 0.0f)
{
forcex -= 0.00005f;
}
else if(forcex < 0.0f)
{
forcex += 0.00005f;
}
if(forcey > 0.0f)
{
forcey -= 0.00005f;
}
else if(forcey < 0.0f)
{
forcey += 0.00005f;
}
glTranslatef(forcex, forcey, 0.0f);
glutPostRedisplay();
glutTimerFunc(1000/100, physics, 2);
}
void keydown(unsigned char key, int x, int y)
{
switch(key)
{
case 'w':
{
keys[UP] = 1;
} break;
case 'a':
{
keys[LEFT] = 1;
} break;
case 's':
{
keys[DOWN] = 1;
} break;
case 'd':
{
keys[RIGHT] = 1;
} break;
case 'e':
{
keys[STOP] = 1;
} break;
}
}
void keyup(unsigned char key, int x, int y)
{
switch(key)
{
case 'w':
{
keys[UP] = 0;
} break;
case 'a':
{
keys[LEFT] = 0;
} break;
case 's':
{
keys[DOWN] = 0;
} break;
case 'd':
{
keys[RIGHT] = 0;
} break;
case 'e':
{
keys[STOP] = 0;
} break;
}
}
|