-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainGame.cpp
126 lines (96 loc) · 2.78 KB
/
MainGame.cpp
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
//
// Created by Shishir on 19/02/2018.
//
#include <conio.h>
#include "MainGame.h"
using namespace std;
MainGame::MainGame() {
screenWidth=640; screenHeight=480;
gameState = true;
window = nullptr;
cubeVertexBufferObjectID = 0;
cubeIndexBufferObjectID = 0;
transformMatrixBufferID = 0;
}
MainGame::~MainGame() {
glDeleteBuffers(1, &cubeVertexBufferObjectID);
glDeleteBuffers(1, &cubeIndexBufferObjectID);
glDeleteBuffers(1, &transformMatrixBufferID);
SDL_DestroyWindow(window);
}
void MainGame::fatalError(std::string error_string) {
cout << error_string << endl;
cout << endl << "Press any key to continue...";
getch();
SDL_Quit();
exit(1);
}
void MainGame::run() {
initSystems();
gameLoop();
}
void MainGame::initSystems() {
SDL_Init(SDL_INIT_EVERYTHING);
window = SDL_CreateWindow("Graphics", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, screenWidth, screenHeight,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
if(window == nullptr) {
string errorString = "Could not create SDL Window! Error Code: ";
errorString += SDL_GetError();
fatalError(errorString);
}
SDL_GLContext glContext = SDL_GL_CreateContext(window);
if(glContext == NULL) {
fatalError("Could not create GL context!");
}
glewExperimental = true;
GLenum error = glewInit();
if(error != GLEW_OK) {
fatalError("Could not initialize glew!");
}
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 255);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 255);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 255);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
glClearDepth(1.0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
genVertex();
}
void MainGame::genVertex() {
}
void MainGame::gameLoop() {
while (gameState) {
processInputs();
draw();
}
}
void MainGame::processInputs() {
while (SDL_PollEvent(&evnt)) {
switch(evnt.type) {
case SDL_QUIT:
gameState = false;
break;
case SDL_MOUSEMOTION:
cout << evnt.motion.x << ", " << evnt.motion.y << endl;
break;
case SDL_KEYDOWN:
switch (evnt.key.keysym.sym) {
case SDLK_ESCAPE:
gameState = false;
break;
default:
;
}
break;
default:
gameState = true;
break;
}
}
}
void MainGame::draw() {
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
SDL_GL_SwapWindow(window);
}