-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
189 lines (136 loc) · 3.73 KB
/
Game.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
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
#include "Game.hpp"
#include "Joystick.hpp"
#include <map>
Game::Game()
{}
Game::~Game()
{}
Controller *controller = nullptr;
//int win_width = 800;
//int win_height = 600;
int rectX;
int rectY;
//const SDL_VideoInfo* info = NULL;
const SDL_RendererInfo* info = NULL;
short unsigned int drawValue = 0;
std::map<unsigned int, unsigned short int> drawSet;
void Game::init(const char *title, int xpos, int ypos, int width, int height, bool fullscreen)
{
int flags = 0;
//bpp = info->vfmt->BitsPerPixel;
win_width = width;
win_height = height;
flags = SDL_WINDOW_OPENGL;
if(fullscreen)
{
flags |= SDL_WINDOW_FULLSCREEN;
}
if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
std::cerr << "Intitalization Failed: " << SDL_GetError()<< std::endl;
isRunning = false;
}
std::cout << "Subsystems initialized..." << std::endl;
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
window = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
if(window)
{
std::cout << "Window Created..." << std::endl;
}
renderer = SDL_CreateRenderer(window, -1, 0);
if(renderer)
{
//SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
std::cout << "Renderer Created..." << std::endl;
}
// INIT OpenGL: //
openGLContext = SDL_GL_CreateContext(window);
if(!openGLContext)
{
//SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
std::cout << "OpenGL init failed!" << std::endl;
}
//flags = SDL_OPENGL | SDL_FULLSCREEN;
//if(SDL_SetVideoMode(width, height, bpp, flags) == 0)
//{
// std::cerr << "Video mode set failed: " << SDL_GetError() << std::endl;
//}
controller = new Controller();
controller->init(0);
std::cout << "Controller Initialized..." << std::endl;
isRunning = true;
}
void Game::handleEvents()
{
SDL_Event event;
SDL_PollEvent(&event);
switch (event.type)
{
case SDL_QUIT:
isRunning = false;
break;
case SDL_JOYAXISMOTION: case SDL_JOYBUTTONDOWN:
controller->handleEvents(&event);
break;
default:
//std::cerr << "Warning: Unhandled Event..." << std::endl;
break;
}
}
void Game::update()
{
if (rectX < win_width && rectY < win_height && rectX >= 0 && rectY >= 0 && drawValue>10)
{
unsigned int cordHex = (rectY << 16) | rectX;
//std::cout << "Value: " << drawValue << " | " << (drawValue * (255.0/(2^15-1))) << std::endl;
drawSet[cordHex] = drawValue;
}
/*
if (rectX < 800 && rectY < 600)
{
int cordHex = (rectX << 2) | rectY;
drawSet[cordHex] = drawValue;
}
*/
}
void Game::preDraw()
{
}
void Game::render()
{
SDL_SetRenderDrawColor(renderer,0,0,0,255);
SDL_RenderClear(renderer);
//this is where add stuff to render
SDL_SetRenderDrawColor(renderer,255,255,255,255);
SDL_RenderDrawPoint(renderer,100,100);
for (const auto& [key, value]: drawSet)
{
int pValue = value;
SDL_SetRenderDrawColor(renderer, (int)(value), (int)(value), (int)(value), 255);
//std::cout << "Cords: " << std::hex << key << std::endl;
SDL_RenderDrawPoint(renderer, (int) (key & 0x0000FFFF), (int)((key & 0xFFFF0000) >> 16));
}
SDL_SetRenderDrawColor(renderer,255,255,255,255);
SDL_Rect rect;
rect.w = 100;
rect.h = 100;
rect.x = rectX;
rect.y = rectY;
SDL_RenderDrawRect(renderer, &rect);
//end section
SDL_RenderPresent(renderer);
SDL_GL_SwapWindow(window);
}
void Game::clean()
{
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_Quit();
std::cout << "Program Cleanup Complete!" << std::endl;
}