-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
537 lines (424 loc) · 18.7 KB
/
main.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
#include "glad/glad.h"
#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include <GLFW/glfw3.h>
#include "VertexBuffer.h"
#include "IndexBuffer.h"
#include "VertexArray.h"
#include "Shader.h"
#include "Renderer.h"
#include "Texture.h"
#include "stb_image.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cmath>
#include <cstdlib> /* srand, rand */
#include <ctime>
#include <queue>
#include <utility>
#include <vector>
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
//void processInput(GLFWwindow *window);
// settings
const unsigned int SCR_WIDTH = 640;
const unsigned int SCR_HEIGHT = 640;
const unsigned int GRID_WIDTH = 10;
const unsigned int GRID_HEIGHT = 10;
const float cellWidth = 50.0f;
const float cellHeight = 50.0f;
enum CellState {
HIDDEN,
REVEALED,
MEME,
FLAGGED
};
struct Cell {
CellState state = CellState::HIDDEN;
bool isMeme = false; // Whether this cell contains a meme
int neighboringMemeCount = 0; // Number of memes in adjacent cells
bool dirty;
};
struct Vertex {
glm::vec2 position;
glm::vec2 texCoords; // Assuming you have texture coordinates too
float textureID;
};
Cell grid[GRID_HEIGHT][GRID_WIDTH];
bool firstClick = false;
std::vector<const char *> filesPath = {
"res/zero.png", // For 0 adjacent memes
"res/one.png", // For 1 adjacent meme
"res/two.png", // For 2 adjacent memes
"res/three.png", // For 3 adjacent memes
"res/four.png", // For 4 adjacent memes
"res/five.png", // For 5 adjacent memes
"res/six.png", // For 6 adjacent memes
"res/seven.png", // For 7 adjacent memes
"res/eight.png", // For 8 adjacent memes
"res/hidden.png",
"res/flag.png",
"res/mine.png",
};
void calculateMemeCounts() {
// Directions for the 8 neighboring cells
const int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
for (int y = 0; y < GRID_HEIGHT; ++y) {
for (int x = 0; x < GRID_WIDTH; ++x) {
// Skip cells that are memes
if (grid[y][x].isMeme) continue;
// Check the 8 neighboring cells
for (int i = 0; i < 8; ++i) {
int nx = x + dx[i];
int ny = y + dy[i];
// Ensure the neighbor is within the grid bounds
if (nx >= 0 && nx < GRID_WIDTH && ny >= 0 && ny < GRID_HEIGHT) {
if (grid[ny][nx].isMeme) {
grid[y][x].neighboringMemeCount++;
}
}
}
}
}
}
void placeMemes(int numMemes) {
int placedMemes = 0;
std::srand(std::time(nullptr));
while (placedMemes < numMemes) {
int x = std::rand() % GRID_WIDTH;
int y = std::rand() % GRID_HEIGHT;
// If there's no meme at this position, place one
if (!grid[y][x].isMeme) {
grid[y][x].isMeme = true;
placedMemes++;
}
}
}
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
if (action == GLFW_PRESS) {
double xpos, ypos;
// Get the mouse position
glfwGetCursorPos(window, &xpos, &ypos);
// Define constants
int cell_size = 50; // Actual cell size
int grid_size = 500; // 10x10 grid with 50x50 cells
int window_size = 640; // Window size
int offset = (window_size - grid_size) / 2; // Offset for centering (70 pixels)
// Invert the y-coordinate
ypos = window_size - ypos;
// Adjust for the offset
xpos -= offset;
ypos -= offset;
// Calculate the grid cell based on mouse position
int grid_x = static_cast<int>(std::round(xpos)) / cell_size;
int grid_y = static_cast<int>(std::round(ypos)) / cell_size;
// The grid is assumed to be a 2D array of Cells, where each Cell has a boolean isRevealed and isMeme.
if(!firstClick){
// Use a flood-fill algorithm to reveal a "safe" area
// Check if the starting position is safe
if (grid[grid_y][grid_x].isMeme) {
// In some versions of Minesweeper, the game would reposition the meme if you click on one first
// You can handle this however you'd like.
return; // Or reposition the meme at this point
}
// Use a queue for breadth-first search (BFS) to reveal neighboring squares
std::queue<std::pair<int, int>> toReveal;
toReveal.push({grid_x, grid_y});
while (!toReveal.empty()) {
auto [x, y] = toReveal.front();
toReveal.pop();
// Check bounds
if (x < 0 || x >= GRID_WIDTH || y < 0 || y >= GRID_HEIGHT)
continue;
// If the cell is already revealed or is a meme, skip it
if (grid[y][x].state == CellState::REVEALED || grid[y][x].isMeme)
continue;
// Reveal the cell
grid[y][x].state = CellState::REVEALED;
// Mark the cell as dirty
grid[y][x].dirty = true;
// Count neighboring memes
int memeCount = grid[y][x].neighboringMemeCount;
// If no adjacent memes, reveal neighbors
if (memeCount == 0) {
// Add neighboring cells to the queue
for (int offsetY = -1; offsetY <= 1; ++offsetY) {
for (int offsetX = -1; offsetX <= 1; ++offsetX) {
if (offsetX == 0 && offsetY == 0) continue; // Skip the current cell
toReveal.push({x + offsetX, y + offsetY});
}
}
firstClick = true;
}
}
} else {
// Ensure the click is within the grid bounds
if (xpos >= 0 && xpos < grid_size && ypos >= 0 && ypos < grid_size) {
if (button == GLFW_MOUSE_BUTTON_LEFT) {
std::cout << "Left mouse button pressed at (" << xpos << ", " << ypos << ")" << std::endl;
grid[grid_y][grid_x].state = CellState::FLAGGED;
}
if (button == GLFW_MOUSE_BUTTON_RIGHT) {
std::cout << "Right mouse button pressed at (" << xpos << ", " << ypos << ")" << std::endl;
if(grid[grid_y][grid_x].isMeme){
grid[grid_y][grid_x].state = CellState::MEME;
} else if (!grid[grid_y][grid_x].isMeme){
grid[grid_y][grid_x].state = CellState::REVEALED;
}
}
// Mark the cell as dirty
grid[grid_y][grid_x].dirty = true;
} else {
std::cout << "Click was outside the grid." << std::endl;
}
}
}
}
unsigned int load_textures(){
unsigned int texArrayId;
glGenTextures(1, &texArrayId);
glBindTexture(GL_TEXTURE_2D_ARRAY, texArrayId);
//glTexStorage3D(GL_TEXTURE_2D_ARRAY, 1, GL_RGBA8, 128, 128, 12);
// Specify the storage for the texture array
glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA8, 128, 128, 12, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
for (int i=0; i < 12; i++){
int width, height, channels;
stbi_set_flip_vertically_on_load(true);
//std::cout << filesPath[i] << std::endl;
unsigned char* data = stbi_load(filesPath[i], &width, &height, &channels, STBI_rgb_alpha);
glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, i, 128, 128, 1, GL_RGBA, GL_UNSIGNED_BYTE, data);
stbi_image_free(data);
}
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_R, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_REPEAT);
return texArrayId;
}
int main()
{
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// Set the mouse button callback
glfwSetMouseButtonCallback(window, mouse_button_callback);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
{
placeMemes(10);
calculateMemeCounts();
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_DEPTH_TEST);
for (size_t y = 0; y < GRID_HEIGHT; y++)
{
for (size_t x = 0; x < GRID_WIDTH; x++)
{
std::cout << " grid cell [ " << y << ", " << x << " ] " << grid[y][x].neighboringMemeCount << ", "<< grid[y][x].isMeme << std::endl;
}
}
// float vertices[] = {
// -250.0f, -250.0f, 0.0f, 0.0f, // left
// -200.0f, -250.0f, 1.0f, 0.0f, // right
// -200.0f, -200.0f, 1.0f, 1.0f, // top
// -250.0f, -200.0f, 0.0f, 1.0f
// };
// unsigned int indices[]= {
// 0, 1, 2,
// 2, 3, 0
// };
glm::vec2 quadVertices[] = {
{-250.0f, -250.0f}, // Bottom-left
{-200.0f, -250.0f}, // Bottom-right
{-200.0f, -200.0f}, // Top-right
{-250.0f, -200.0f} // Top-left
};
glm::vec2 quadTextureIndices[] = {
{0.0f, 0.0f}, // Bottom-left
{1.0f, 0.0f}, // Bottom-right
{1.0f, 1.0f}, // Top-right
{0.0f, 1.0f} // Top-left
};
glm::vec2 offsets[GRID_WIDTH * GRID_HEIGHT];
for (int y = 0; y < GRID_HEIGHT; y++) {
for (int x = 0; x < GRID_WIDTH; x++) {
offsets[y * GRID_WIDTH + x] = glm::vec2(x * cellWidth, y * cellHeight); // Position each cell in the grid
}
}
std::vector<Vertex> finalVertexBuffer;
for (int y = 0; y < GRID_HEIGHT; ++y) {
for (int x = 0; x < GRID_WIDTH; ++x) {
// Get the offset for the current grid cell
glm::vec2 gridOffset = offsets[y * GRID_WIDTH + x];
// For each quad vertex, add the offset to the local quad position
for (int i = 0; i < 4; ++i) { // Assuming 4 vertices per quad
Vertex v;
v.position = quadVertices[i] + gridOffset; // Add offset to quad vertex
v.texCoords = quadTextureIndices[i]; /* Set appropriate texture coordinates here */
v.textureID = 9.0f;
finalVertexBuffer.push_back(v);
}
}
}
std::vector<unsigned int> indices;
int quadIndexOffset = 0;
for (int y = 0; y < GRID_HEIGHT; ++y) {
for (int x = 0; x < GRID_WIDTH; ++x) {
// Add indices for each quad
indices.push_back(quadIndexOffset + 0);
indices.push_back(quadIndexOffset + 1);
indices.push_back(quadIndexOffset + 2);
indices.push_back(quadIndexOffset + 2);
indices.push_back(quadIndexOffset + 3);
indices.push_back(quadIndexOffset + 0);
// Move the offset to the next set of vertices (each quad has 4 vertices)
quadIndexOffset += 4;
}
}
//Create the vertex Array and vertex Buffer
VertexArray va;
//VertexBuffer vb (vertices, 4 * 4 * sizeof(float));
//VertexBuffer vb( &finalVertexBuffer, ( 4 * 4 + 4 ) * GRID_WIDTH * GRID_HEIGHT * sizeof(float), true );
VertexBuffer vb( &finalVertexBuffer[0], 5 * finalVertexBuffer.size() * sizeof(float));
// Create the vertex array layout and bind the buffer and the layout
VertexBufferLayout layout;
//layout.push(2, VALUETYPE::FLOAT);
//layout.push(2, VALUETYPE::FLOAT);
layout.Push<float>(2);
layout.Push<float>(2);
layout.Push<float>(1);
va.AddBuffer(vb, layout);
//IndexBuffer ibo( indices.data(), GRID_HEIGHT * GRID_WIDTH * 6);
IndexBuffer ibo( &indices[0], indices.size());
//glm::mat4 proj = glm::ortho(-2.0f, 2.0f, -1.5f, 1.5f);
glm::mat4 proj = glm::ortho(-320.0f, 320.0f, -320.0f, 320.0f, -1.0f, 1.0f);
Shader shader("basic.shader");
//shader.SetUniform4f("u_Color", 1.0f, 0.5f, 0.2f, 1.0f);
unsigned int textures = load_textures();
shader.Bind();
shader.SetUniformMat4f("u_MVP", proj);
shader.Bind();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D_ARRAY, textures);
shader.SetUniform1i("textureArray", 0);
// note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
// You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
// VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
// va.Unbind();
// vb.Unbind();
// shader.Unbind();
Renderer renderer;
// uncomment this call to draw in wireframe polygons.
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
// Update the buffer using glBufferSubData
// finalVertexBuffer[0].textureID = 5.0f;
// GLintptr offset = 0 * sizeof(Vertex);
// glBufferSubData(GL_ARRAY_BUFFER, offset, sizeof(Vertex), &finalVertexBuffer[0]);
// render loop
// -----------
while (!glfwWindowShouldClose(window))
{
// input
// -----
//processInput(window);
// render
// ------
renderer.Clear();
shader.Bind();
// glActiveTexture(GL_TEXTURE0);
// glBindTexture(GL_TEXTURE_2D_ARRAY, textures);
for (int y = 0; y < GRID_HEIGHT; ++y) {
for (int x = 0; x < GRID_WIDTH; ++x) {
Cell &cell = grid[y][x];
int cellIndex = y * GRID_WIDTH + x;
int firstVertexIndex = cellIndex * 4; // 4 vertices per quad
// Set the texture based on the cell state
if (cell.dirty){
if (cell.state == CellState::REVEALED) {
for (int i = 0; i < 4; ++i) {
finalVertexBuffer[firstVertexIndex + i].textureID = static_cast<float>(cell.neighboringMemeCount); // Or whichever texture ID you need
}
}
if (cell.state == CellState::FLAGGED) {
for (int i = 0; i < 4; ++i) {
finalVertexBuffer[firstVertexIndex + i].textureID = 10.0f;
}
}
if (cell.state == CellState::MEME) {
for (int i = 0; i < 4; ++i) {
finalVertexBuffer[firstVertexIndex + i].textureID = 11.0f;
}
}
// Update the buffer using glBufferSubData
GLintptr offset = cellIndex * 5 * sizeof(float);
va.Bind();
vb.Bind();
glBufferSubData(GL_ARRAY_BUFFER, firstVertexIndex * sizeof(Vertex), 4 * sizeof(Vertex), &finalVertexBuffer[firstVertexIndex]);
std::cout << "cell index" << cellIndex << "yx" << y << x << std::endl;
std::cout << "offset" << offset << "vertex size" << sizeof(Vertex) << std::endl;
// Reset the dirty flag
cell.dirty = false;
}
}
}
// draw our first triangle
renderer.Draw(va, ibo, shader);
// if (r > 1.0f)
// increment = -0.05f;
// else if (r < 0.0f)
// increment = 0.05f;
// r += increment;
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
// optional: de-allocate all resources once they've outlived their purpose:
// ------------------------------------------------------------------------
// glfw: terminate, clearing all previously allocated GLFW resources.
// ------------------------------------------------------------------
}
glfwTerminate();
return 0;
}
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
// void processInput(GLFWwindow *window)
// {
// if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
// glfwSetWindowShouldClose(window, true);
// }
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
}