diff options
| author | 2019-11-01 15:07:27 -0400 | |
|---|---|---|
| committer | 2019-11-01 15:07:27 -0400 | |
| commit | 4db0534fa12fc64d9530149a8f593647d916ccf4 (patch) | |
| tree | 17df26e9f5456fde9667aa396d2e290d301b3ffb | |
| parent | 3518eef7c5dc09c63dd07fbdc145c32bdd3698c3 (diff) | |
| download | cetris-4db0534fa12fc64d9530149a8f593647d916ccf4.tar.gz cetris-4db0534fa12fc64d9530149a8f593647d916ccf4.tar.bz2 cetris-4db0534fa12fc64d9530149a8f593647d916ccf4.zip | |
more work on new opengl frontend
| -rw-r--r-- | frontends/gl/drawable.c | 61 | ||||
| -rw-r--r-- | frontends/gl/drawable.h | 10 | ||||
| -rw-r--r-- | frontends/gl/main.c | 18 | ||||
| -rw-r--r-- | frontends/gl/shader.c | 22 | ||||
| -rw-r--r-- | frontends/gl/skin.c | 17 | ||||
| -rw-r--r-- | frontends/gl/skin.h | 8 | ||||
| -rw-r--r-- | frontends/gl/ui.c | 58 | ||||
| -rw-r--r-- | frontends/gl/ui.h | 5 | ||||
| m--------- | ini | 0 | ||||
| -rwxr-xr-x | test/line_count.sh | 5 |
10 files changed, 137 insertions, 67 deletions
diff --git a/frontends/gl/drawable.c b/frontends/gl/drawable.c index 4d88a0a..9abdd31 100644 --- a/frontends/gl/drawable.c +++ b/frontends/gl/drawable.c @@ -9,10 +9,10 @@ GLfloat vertices[] = { // Position Color Texcoords - 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, // Top-left - 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, // Top-right - 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, // Bottom-right - 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f // Bottom-left + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // Top-left + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, // Top-right + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, // Bottom-right + 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f // Bottom-left }; GLuint elements[] = { @@ -21,64 +21,69 @@ GLuint elements[] = { }; void calc_pos(GLfloat *rect, GLfloat x, GLfloat y, GLfloat w, GLfloat h) { - rect[0] = rect[21] = -1.0f + (x / 200.0f); - rect[1] = rect[8] = 1.0f - (y / 400.0f); - rect[15] = rect[22] = rect[1] - (h) / 400.0f; - rect[7] = rect[14] = rect[0] + (w) / 200.0f; + rect[0] = rect[24] = -1.0f + (x / 200.0f); + rect[1] = rect[9] = 1.0f - (y / 400.0f); + rect[17] = rect[25] = rect[1] - (h) / 400.0f; + rect[8] = rect[16] = rect[0] + (w) / 200.0f; } -bool load_image(char* file_name, GLuint texture) { - glBindTexture(GL_TEXTURE_2D, texture); - +bool load_image(char* file_name, drawable_t *drawable) { int w, h, channel; - unsigned char *data = stbi_load(file_name, &w, &h, &channel, 0); + unsigned char *data = stbi_load(file_name, &w, &h, &channel, STBI_rgb_alpha); if (data == NULL) return false; - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); glGenerateMipmap(GL_TEXTURE_2D); + + //glBindTexture(GL_TEXTURE_2D, 0); stbi_image_free(data); + + drawable->w = w; + drawable->h = h; return true; } -void set_block_texture(GLfloat *rect, uint8_t mino) { - GLfloat index = (mino + 2) * .1f; - rect[5] = rect[26] = index; - rect[12] = rect[19] = index + .1f; +void set_block_texture(drawable_t *drawable, uint8_t mino) { + GLfloat index = (mino + 2) * .0625f; + drawable->vertices[6] = drawable->vertices[30] = index; + drawable->vertices[14] = drawable->vertices[22] = index + .0625f; + //vertices[5] = vertices[13] = vertices[21] = vertices[29] = opacity; } -void update_rect(drawable_t *block, GLfloat x, GLfloat y, GLfloat w, GLfloat h, uint8_t mino) { - calc_pos(&vertices[0], x, y, w, h); - set_block_texture(&vertices[0], mino); +void update_rect(drawable_t *drawable, GLfloat x, GLfloat y, GLfloat w, GLfloat h) { + calc_pos(drawable->vertices, x, y, w, h); - glBindBuffer(GL_ARRAY_BUFFER, block->vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, drawable->vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), drawable->vertices, GL_DYNAMIC_DRAW); //glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices); } void new_rectangle(drawable_t* drawable) { + drawable->vertices = malloc(sizeof(vertices)); + memcpy(drawable->vertices, &vertices[0], sizeof(vertices)); + glGenVertexArrays(1, &drawable->vao); glBindVertexArray(drawable->vao); glGenBuffers(1, &drawable->vbo); glBindBuffer(GL_ARRAY_BUFFER, drawable->vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), drawable->vertices, GL_DYNAMIC_DRAW); glGenBuffers(1, &drawable->ebo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, drawable->ebo); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_DYNAMIC_DRAW); - - glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), (void*)0); + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)0); glEnableVertexAttribArray(0); - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), (void*)(2* sizeof(GLfloat))); + glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(2* sizeof(GLfloat))); glEnableVertexAttribArray(1); - glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 7 * sizeof(GLfloat), (void*)(5 * sizeof(GLfloat))); + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(6 * sizeof(GLfloat))); glEnableVertexAttribArray(2); } diff --git a/frontends/gl/drawable.h b/frontends/gl/drawable.h index 41b9a72..4137d37 100644 --- a/frontends/gl/drawable.h +++ b/frontends/gl/drawable.h @@ -7,8 +7,14 @@ typedef struct { GLuint vbo; GLuint ebo; GLuint texture; + + GLfloat w; + GLfloat h; + + GLfloat *vertices; } drawable_t; void new_rectangle(drawable_t* drawable); -void update_rect(drawable_t *block, GLfloat x, GLfloat y, GLfloat w, GLfloat h, uint8_t mino); -bool load_image(char* file_name, GLuint texture); +void update_rect(drawable_t *drawable, GLfloat x, GLfloat y, GLfloat w, GLfloat h); +void set_block_texture(drawable_t *drawable, uint8_t mino); +bool load_image(char* file_name, drawable_t *drawable); diff --git a/frontends/gl/main.c b/frontends/gl/main.c index 25d8d87..7e1aa66 100644 --- a/frontends/gl/main.c +++ b/frontends/gl/main.c @@ -116,10 +116,17 @@ int main(void) { ui.keys = default_keys; glViewport(0, 0, 400, 800); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + + glEnable(GL_DEPTH_TEST); + glClearDepth(1.0); + glDepthFunc(GL_LEQUAL); GLuint shaderProgram = glCreateProgram(); new_shader_program(&shaderProgram); glUseProgram(shaderProgram); + ui.shader_program = shaderProgram; printf("%i\n", tetris_ds_config.board_x); ui.board.config = tetris_ds_config; @@ -127,13 +134,10 @@ int main(void) { init_game(&ui.board.game, &ui.board.config); cetris_start_game(&ui.board.game); - load_tetris_board(&ui.board, 50.0f, 155.0f, 150.0f, 645.0f); - new_rectangle(&ui.board.block); + load_tetris_board(&ui.board, 50.0f, 155.0f, 250.0f, 500.0f); load_skin("test", &ui.skin); - glBindTexture(GL_TEXTURE_2D, ui.skin.block_texture); - SDL_Event e; int delay = 1000/FRAME_RATE; @@ -142,11 +146,11 @@ int main(void) { handle_key(e, &ui.keys, &ui.board); } - glClearColor(0.2f, 0.3f, 0.3f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); + glClearColor(0.21f, 0.12f, 0.11f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + //draw_current(&ui); draw_tetris_board(&ui); - draw_current(&ui); SDL_GL_SwapWindow(window); diff --git a/frontends/gl/shader.c b/frontends/gl/shader.c index 85e0c12..8d66106 100644 --- a/frontends/gl/shader.c +++ b/frontends/gl/shader.c @@ -2,9 +2,9 @@ const char *vertex_shader_source = "#version 450 core\n" "layout (location = 0) in vec2 aPos;\n" - "layout (location = 1) in vec3 aColor;\n" + "layout (location = 1) in vec4 aColor;\n" "layout (location = 2) in vec2 aTexCoord;\n" - "out vec3 ourColor;\n" + "out vec4 ourColor;\n" "out vec2 TexCoord;\n" "void main()\n" "{\n" @@ -15,15 +15,18 @@ const char *vertex_shader_source = "#version 450 core\n" const char *fragment_shader_source = "#version 450 core\n" "out vec4 FragColor;\n" - "in vec3 ourColor;\n" + "in vec4 ourColor;\n" "in vec2 TexCoord;\n" - "uniform sampler2D ourTexture;\n" + "uniform sampler2D tex;\n" + "vec4 layer(vec4 foreground, vec4 background) {\n" + " return foreground * foreground.a + background * (1.0 - foreground.a);\n" + "}\n" "void main()\n" "{\n" - " FragColor = texture(ourTexture, TexCoord);\n" - "}\n\0"; -// " FragColor = texture(ourTexture, TexCoord) * vec4(ourColor, 1.0);\n" + " FragColor = texture(tex, TexCoord);\n" + "}\0"; + //"uniform sampler2D overlayTexture;\n" void new_shader_program(GLuint *shaderProgram) { GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertexShader, 1, &vertex_shader_source, 0); @@ -37,6 +40,9 @@ void new_shader_program(GLuint *shaderProgram) { // Link the vertex and fragment shader into a shader program glAttachShader(*shaderProgram, vertexShader); glAttachShader(*shaderProgram, fragmentShader); - //glBindFragDataLocation(shaderProgram, 0, "outColor"); glLinkProgram(*shaderProgram); + + glUseProgram(*shaderProgram); + + glUniform1i(glGetUniformLocation(*shaderProgram, "tex"), 0); } diff --git a/frontends/gl/skin.c b/frontends/gl/skin.c index 2157552..1784d72 100644 --- a/frontends/gl/skin.c +++ b/frontends/gl/skin.c @@ -29,8 +29,23 @@ void load_skin(char* name, cetris_skin_t* skin) { #endif char file[125]; + format_str(file, 125, "%s/blocks.png", dir_name); - if (load_image(file, skin->block_texture)) { + glGenTextures(1, &skin->block.texture); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, skin->block.texture); + new_rectangle(&skin->block); + if (load_image(file, &skin->block)) { skin->has_block_texture = true; } else skin->has_block_texture = false; + glBindTexture(GL_TEXTURE_2D, 0); + + format_str(file, 125, "%s/overlay.png", dir_name); + glGenTextures(1, &skin->overlay.texture); + glBindTexture(GL_TEXTURE_2D, skin->overlay.texture); + new_rectangle(&skin->overlay); + if (load_image(file, &skin->overlay)) { + skin->has_overlay_texture = true; + } else skin->has_overlay_texture = false; + glBindTexture(GL_TEXTURE_2D, 0); } diff --git a/frontends/gl/skin.h b/frontends/gl/skin.h index 4d7ae01..36e2e8b 100644 --- a/frontends/gl/skin.h +++ b/frontends/gl/skin.h @@ -1,8 +1,14 @@ #pragma once +#include "drawable.h" + typedef struct { bool has_block_texture; - GLuint block_texture; + drawable_t block; + + bool has_overlay_texture; + drawable_t overlay; + } cetris_skin_t; void load_skin(char* name, cetris_skin_t* skin); diff --git a/frontends/gl/ui.c b/frontends/gl/ui.c index 963c1aa..fadfa4f 100644 --- a/frontends/gl/ui.c +++ b/frontends/gl/ui.c @@ -6,34 +6,49 @@ void load_tetris_board(tetris_board_t *board, GLfloat x, GLfloat y, GLfloat w, GLfloat h) { board->block_width = w / (GLfloat)board->config.board_x; - board->block_height = h / (GLfloat)board->config.board_y; - printf("%i, %f, %i, %f\n", board->config.board_x, board->block_width, - board->config.board_y, board->block_height); + board->block_height = h / board->config.board_visible; board->x_offset = x; board->y_offset = y; + board->block_offset = (board->config.board_y - board->config.board_visible); } void draw_tetris_board(cetris_ui *ui) { - glBindVertexArray(ui->board.block.vao); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, ui->skin.overlay.texture); + glBindVertexArray(ui->skin.overlay.vao); + + for (int y = 0; y < ui->board.game.config.board_visible; y++) { + update_rect(&ui->skin.overlay, ui->board.x_offset, + ui->board.y_offset + (y * ui->board.block_height), + ui->board.block_width * ui->board.game.config.board_x, + ui->board.block_height); + + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + } + + glBindTexture(GL_TEXTURE_2D, 0); + + glBindTexture(GL_TEXTURE_2D, ui->skin.block.texture); + + glBindVertexArray(ui->skin.block.vao); for (int x = 0; x < ui->board.game.config.board_x; x++) { for (int y = ui->board.game.highest_line; y < ui->board.game.config.board_y; y++) { if (ui->board.game.board[x][y] & SLOT_OCCUPIED) { - update_rect(&ui->board.block, + set_block_texture(&ui->skin.block, (ui->board.game.board[x][y] >> 5)); + + update_rect(&ui->skin.block, ui->board.x_offset + (x * ui->board.block_width), - ui->board.y_offset + (y * ui->board.block_height), + ui->board.y_offset + ((y - ui->board.block_offset) * ui->board.block_height), ui->board.block_width, - ui->board.block_height, - (ui->board.game.board[x][y] >> 5)); + ui->board.block_height); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); } } } -} -void draw_current(cetris_ui *ui) { - glBindVertexArray(ui->board.block.vao); for (int s = 0; s < 4; s++) { for (int j = 0; j < 4; j++) { if ((ui->board.game.current.m[s]>>(3 - j))&1) { @@ -43,23 +58,28 @@ void draw_current(cetris_ui *ui) { GLfloat block_y = ui->board.y_offset + - (s + ui->board.game.current.pos.y) * ui->board.block_height; + (s + ui->board.game.current.pos.y - ui->board.block_offset) + * ui->board.block_height; - update_rect(&ui->board.block, block_x, block_y, - ui->board.block_width, ui->board.block_height, - ui->board.game.current.t); + set_block_texture(&ui->skin.block, ui->board.game.current.t); + update_rect(&ui->skin.block, block_x, block_y, + ui->board.block_width, ui->board.block_height); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); GLfloat ghost_y = ui->board.y_offset + - (s + ui->board.game.current.ghost_y) * ui->board.block_height; + (s + ui->board.game.current.ghost_y - ui->board.block_offset) + * ui->board.block_height; + + set_block_texture(&ui->skin.block, ui->board.game.current.t + 7); + update_rect(&ui->skin.block, block_x, ghost_y, + ui->board.block_width, ui->board.block_height); - update_rect(&ui->board.block, block_x, ghost_y, - ui->board.block_width, ui->board.block_height, - ui->board.game.current.t); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); } } } + glBindTexture(GL_TEXTURE_2D, 0); + } diff --git a/frontends/gl/ui.h b/frontends/gl/ui.h index 4854ab0..4b5a94f 100644 --- a/frontends/gl/ui.h +++ b/frontends/gl/ui.h @@ -7,10 +7,11 @@ typedef struct { GLfloat block_width; GLfloat block_height; + GLfloat block_offset; + GLfloat x_offset; GLfloat y_offset; - drawable_t block; cetris_game game; cetris_config config; } tetris_board_t; @@ -33,6 +34,8 @@ typedef struct { cetris_skin_t skin; tetris_board_t board; + + GLuint shader_program; } cetris_ui; void draw_tetris_board(cetris_ui *ui); diff --git a/ini b/ini -Subproject 3401f2ed3153c6093c2f434e205cf6e7775a02d +Subproject 9b15f721fd53b1beb8364f8763e2207963ac4fd diff --git a/test/line_count.sh b/test/line_count.sh new file mode 100755 index 0000000..4d72d46 --- /dev/null +++ b/test/line_count.sh @@ -0,0 +1,5 @@ +#!/bin/bash +wc frontends/curses/curses_ui.c frontends/curses/meson.build \ + frontends/sdl/cetris_sdl.c frontends/sdl/cetris_sdl.h frontends/gl/drawable.c frontends/gl/drawable.h \ + frontends/gl/skin.c frontends/gl/skin.h frontends/gl/main.c frontends/gl/shader.c frontends/gl/shader.h \ + frontends/gl/ui.c frontends/gl/ui.h frontends/gl/meson.build lib/* |