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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
// specific headers
#include "SDL.h"
#include <glut.h>
#include <iostream>
#include <string>
#include <Windows.h>
#include <cmath>
float guyX = 0.0, guyY = 0.0;
const float PI = 3.1415926535;
float slopeY, slopeX, slope;
float speedX, speedY, totalDistance;
float angle;
int mouseX = 20, mouseY = 0;
bool mouseIn = false;
float x = 0;
void calcLine(){
slopeY = mouseY - guyY;
slopeX = mouseX - guyX;
slope = slopeY/slopeX;
totalDistance = sqrt((slopeX * slopeX) + (slopeY * slopeY));
speedX = 0.3 * slopeX / totalDistance;
speedY = 0.3 * slopeY / totalDistance;
angle = atan2(slopeY, slopeX);
angle = angle*(180/PI);
}
void control(float &angle, bool mouseIn)
{
if (mouseIn == true){
SDL_ShowCursor(SDL_DISABLE);
SDL_GetMouseState(&mouseX, &mouseY);
}
}
void shoot(){
}
int main( int agrc, char* args[] )
{
SDL_Init(SDL_INIT_EVERYTHING);
//Set OpenGL memory usage
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute( SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
//Caption of the window
SDL_WM_SetCaption( "ZOMB GAME", NULL );
//Size of the window
SDL_SetVideoMode(900,600,32, SDL_OPENGL );
//Specific the clear color
glClearColor(1,1,1,1); //RED,GREEN,BLUE,ALPHA
//What portion of the screen we will display
glViewport(0,0,900,600);
//2D rendering
glMatrixMode(GL_PROJECTION);
//Disable depth checking
glDisable(GL_DEPTH_TEST);
std::cout << "OpenGL is running" << std::endl;
//Handle the main loop
bool isRunning = true;
//For handling events
SDL_Event event;
//Main Variables
bool reseting = false;
bool guyL = false, guyR = false, guyF = false, guyB = false;
float guyFA = 0.5;
float guyW = 10, guyH = 10;
float gunW = 2, gunL = 15;
float screenRight = 450.0, screenLeft = -450.0, screenBottom = 300.0, screenTop = -300.0;
bool shooting;
//Main game loop
while ( isRunning )
{
//EVENTS
while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT ) //If X on windows has been pressed
{
isRunning = false;
}
//If a key has been released, and the key is ESCAPE
if ( event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE )
{
mouseIn = false;
}
//If a key has been released and the key is 'r'
if ( event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_r )
{
glClearColor(1,0,0,1); //Red clear color
}
if (event.type == SDL_MOUSEBUTTONDOWN){
shooting = true;
mouseIn = true;
std::cout << mouseX << ", " << mouseY << std::endl;
}
if (event.type == SDL_MOUSEBUTTONUP){
}
if ( event.type == SDL_KEYDOWN )
{
if (event.key.keysym.sym == SDLK_a){
guyL = true;
}
else if (event.key.keysym.sym == SDLK_d){
guyR = true;
}
else if (event.key.keysym.sym == SDLK_s){
guyB = true;
}
else if (event.key.keysym.sym == SDLK_w){
guyF = true;
}
}
else if ( event.type == SDL_KEYUP ){
if (event.key.keysym.sym == SDLK_a){
guyL = false;
}
else if (event.key.keysym.sym == SDLK_d){
guyR = false;
}
else if (event.key.keysym.sym == SDLK_s){
guyB = false;
}
else if (event.key.keysym.sym == SDLK_w){
guyF = false;
}
}
}
//LOGIC
calcLine();
if (guyL == true){
}
if (guyR == true){
}
if (guyB == true){
if (guyX != mouseX && guyY != mouseY){
//guyX-=speedX;
//guyY-=speedY;
}
}
if (guyF == true){
if (guyX != mouseX && guyY != mouseY){
//guyX+=speedX;
//guyY+=speedY;
}
}
//collision here
control(angle, mouseIn);
//RENDERING
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glLoadIdentity();
glOrtho(screenLeft, screenRight, screenBottom, screenTop, -1, 1);
glPushMatrix();
glRotatef(angle, 0, 0, 1);
//glTranslatef(guyX, guyY, 0);
glColor4ub(255, 0, 0, 255);
glBegin(GL_QUADS);
glVertex2f(guyX - guyW, guyY - guyH);
glVertex2f(guyX + guyW, guyY - guyH);
glVertex2f(guyX + guyW, guyY + guyH);
glVertex2f(guyX - guyW, guyY + guyH);
glEnd();
glPopMatrix();
glColor4ub(0, 255, 0, 255);
glBegin(GL_LINES);
glVertex2f(guyX, guyY);
glVertex2f(mouseX, mouseY);
glEnd();
glColor4ub(0, 0, 255, 255);
glBegin(GL_LINES);
if (mouseIn == true){
glVertex2f(mouseX - 6, mouseY);
glVertex2f(mouseX + 5, mouseY);
glVertex2f(mouseX, mouseY - 6);
glVertex2f(mouseX, mouseY + 5);
}
glEnd();
glPopMatrix();
SDL_GL_SwapBuffers();
SDL_Delay(1);
if (reseting == true)
Sleep (2000);reseting = false;
}
//Quit SDL
SDL_Quit();
return 0;
}
|