summaryrefslogtreecommitdiff
path: root/frontends/gl/main.c
blob: a8fa9c8803c2a4bf6bed0a894d2392d555df4b1c (plain)
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
#include <stdio.h>
#include <string.h>

#include <glad/gl.h>
#include <GLFW/glfw3.h>

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

#include "cetris.h"

static cetris_game cetris;

const char *vertex_shader_source = "#version 450 core\n"
    "layout (location = 0) in vec3 aPos;\n"
    "layout (location = 1) in vec3 aColor;\n"
    "layout (location = 2) in vec2 aTexCoord;\n"
    "out vec3 ourColor;\n"
    "out vec2 TexCoord;\n"
    "void main()\n"
    "{\n"
    "   gl_Position = vec4(aPos, 1.0);\n"
    "   ourColor = aColor;\n"
    "   TexCoord = aTexCoord;\n"
    "}\0";

const char *fragment_shader_source = "#version 450 core\n"
    "out vec4 FragColor;\n"
    "in vec3 ourColor;\n"
    "in vec2 TexCoord;\n"
    "uniform sampler2D ourTexture;\n"
    "void main()\n"
    "{\n"
    "   FragColor = texture(ourTexture, TexCoord) * vec4(ourColor, 1.0);\n"
    "}\n\0";


GLuint shader_program;

GLfloat default_rect[32] = {
   // positions       // colors          // texture coords
   0.0f, 0.0f, 0.0f,  1.0f, 0.0f, 0.0f,  1.0f, 1.0f,   // top right
   0.0f, 0.0f, 0.0f,  1.0f, 0.0f, 0.0f,  1.0f, 0.0f,   // bottom right
   0.0f, 0.0f, 0.0f,  1.0f, 0.0f, 0.0f,  0.0f, 0.0f,   // bottom left
   0.0f, 0.0f, 0.0f,  1.0f, 0.0f, 0.0f,  0.0f, 1.0f    // top left 
};

typedef struct {
  GLfloat r;
  GLfloat g;
  GLfloat b;
} rbg_color;

GLuint indices[] = {  
  0, 1, 3, // first triangle
  1, 2, 3  // second triangle
};

rbg_color colors[7] = {
  //{0.0f, 0.0f, 0.0f},     // Black
  {0.127f,0.219f,0.255f}, // Aqua
  {0.61f,0.153f,0.112f},  // Olive
  {0.177f,0.13f,0.201f},  // Purple
  {0.240f,0.18f,0.190f},  // Fuchsia
  {0.255f,0.133f,0.27f},  // Orange
  {0.0f,0.31f,0.63f},     // Navy
  {0.255f,0.220f,0.0f}    // Yellow 
};

typedef struct {
  GLuint vao;
  GLuint vbo;
  GLuint ebo;
  GLuint texture;
} drawable;

void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
  glViewport(0, 0, width, height);
}  

void input_callback(GLFWwindow *window, int key, int scancode, int action, int mods) {
  if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
    glfwSetWindowShouldClose(window, 1);

  if (key == GLFW_KEY_D && action == GLFW_PRESS)
    move_piece(&cetris, RIGHT);

  if (key == GLFW_KEY_S && action == GLFW_PRESS)
    move_piece(&cetris, USER_DOWN);

  if (key == GLFW_KEY_A && action == GLFW_PRESS)
    move_piece(&cetris, LEFT);

  if (key == GLFW_KEY_W && action == GLFW_PRESS)
    move_piece(&cetris, ROTATE_CW);

  if (key == GLFW_KEY_SPACE && action == GLFW_PRESS)
    move_piece(&cetris, HARD_DROP);
}

void load_texture(char *file_name, GLuint texture) {
  int w, h, channel;
  unsigned char *data = stbi_load(file_name, &w, &h, &channel, 0);
  glBindTexture(GL_TEXTURE_2D, texture);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
  glGenerateMipmap(GL_TEXTURE_2D);
  stbi_image_free(data);
}

void load_vertex_shader(GLuint program) {
  GLint vertex_shader;
  vertex_shader = glCreateShader(GL_VERTEX_SHADER);

  glShaderSource(vertex_shader, 1, &vertex_shader_source, NULL);
  glCompileShader(vertex_shader);
  glAttachShader(program, vertex_shader);
  glDeleteShader(vertex_shader);
}

void load_fragment_shader(GLuint program) {
  GLint fragment_shader;
  fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);

  glShaderSource(fragment_shader, 1, &fragment_shader_source, NULL);
  glCompileShader(fragment_shader);
  glAttachShader(program, fragment_shader);
  glDeleteShader(fragment_shader);
}

void create_static_2d(drawable *b, char* texture_file) {
  glGenVertexArrays(1, &b->vao);
  glGenBuffers(1, &b->vbo);
  glGenBuffers(1, &b->ebo);
  glGenTextures(1, &b->texture);

  glBindVertexArray(b->vao);
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, b->ebo);
  glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); 

  glBindBuffer(GL_ARRAY_BUFFER, b->vbo);

  GLfloat rect[32];
  memcpy(&rect, &default_rect, sizeof(GLfloat) * 32);
  glBufferData(GL_ARRAY_BUFFER, 32 * sizeof(GLfloat), default_rect, GL_DYNAMIC_DRAW);

  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)0);
  glEnableVertexAttribArray(0);

  glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(3* sizeof(GLfloat)));
  glEnableVertexAttribArray(1);

  glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(6 * sizeof(GLfloat)));
  glEnableVertexAttribArray(2);

  load_texture(texture_file, b->texture);
}

void calc_size(GLfloat *rect, int x, int y) {
  rect[0] = rect[8] = (x + 1.0f) / 10.0f;
  rect[1] = rect[25] = (((y) * -1.0f) + 1.0f) / 20.0f; 
  rect[9] = rect[17] = rect[1] - .05f;
  rect[16] = rect[24] = rect[0] - .1f;
}


void update_block(drawable *b, int x, int y, int color) {
  GLfloat block[32];
  memcpy(block, default_rect, sizeof(GLfloat) * 32);

  x = x - 6;
  y = y - 20;

  block[0] = block[8] = (x + 1.0f) / 10.0f;
  block[1] = block[25] = (((y) * -1.0f) + 1.0f) / 20.0f; 
  block[9] = block[17] = block[1] - .05f;
  block[16] = block[24] = block[0] - .1f;

  block[3] = block[11] = block[19] = block[27] = colors[color].r;
  block[4] = block[12] = block[20] = block[28] = colors[color].g;
  block[5] = block[13] = block[21] = block[29] = colors[color].b;

  glBindVertexArray(b->vao);
  glBindBuffer(GL_ARRAY_BUFFER, b->vbo);
  glBufferData(GL_ARRAY_BUFFER, 32 * sizeof(GLfloat), block, GL_DYNAMIC_DRAW);
}

int main(void) {
  glfwInit();
  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

  GLFWwindow* window = glfwCreateWindow(400, 800, "Cetris", NULL, NULL);
  if (window == NULL) {
    printf("[Error] failed to create GLFW window\n");
    glfwTerminate();
    return -1;
  }

  glfwMakeContextCurrent(window);

  if (!gladLoadGL(glfwGetProcAddress)) {
    printf("[Error] failed to load GLAD\n");
    return -1;
  }

  glViewport(0, 0, 400, 800);

  glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

  shader_program = glCreateProgram();
  load_fragment_shader(shader_program);
  load_vertex_shader(shader_program);
  glLinkProgram(shader_program);

  drawable block;
  create_static_2d(&block, "block.jpg");


  init_game(&cetris);

  double prev_time = glfwGetTime();

  glfwSetKeyCallback(window, input_callback);

  while(!glfwWindowShouldClose(window)) {
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    glBindTexture(GL_TEXTURE_2D, block.texture);
    glUseProgram(shader_program);
    glBindVertexArray(block.vao);
    for (int x = 0; x < CETRIS_BOARD_X; x++) {
      for (int y = 0; y < CETRIS_BOARD_Y; y++) {
        if (cetris.board[x][y].flags & SLOT_OCCUPIED) {
          update_block(&block, x, y, cetris.board[x][y].color);  
          glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
        }
      }
    }

    double current_time = glfwGetTime();
    if (current_time - prev_time >= 0.0166) {
      update_game_tick(&cetris);
      prev_time = current_time;
    }

    glfwSwapBuffers(window);
    glfwPollEvents();
  }
  
  glfwTerminate();
  return 0;
}