summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Opalach <andrew@akon.city> 2019-11-02 21:02:11 -0400
committerAndrew Opalach <andrew@akon.city> 2019-11-02 21:02:11 -0400
commit8d7cfce7064ea240e18c65119978cfa3b3c6998a (patch)
tree8e48282b29346a9e1ff714c75daf3780e9236c9b
parent0018ca413afff6de68feb6b883dbbd1f5a32e5a5 (diff)
downloadcetris-8d7cfce7064ea240e18c65119978cfa3b3c6998a.tar.gz
cetris-8d7cfce7064ea240e18c65119978cfa3b3c6998a.tar.bz2
cetris-8d7cfce7064ea240e18c65119978cfa3b3c6998a.zip
shine animation
-rw-r--r--frontends/gl/audio.c1
-rw-r--r--frontends/gl/drawable.c44
-rw-r--r--frontends/gl/drawable.h3
-rw-r--r--frontends/gl/events.c17
-rw-r--r--frontends/gl/main.c20
-rw-r--r--frontends/gl/shader.c11
-rw-r--r--frontends/gl/skin.c7
-rw-r--r--frontends/gl/skin.h6
-rw-r--r--frontends/gl/ui.c72
-rw-r--r--frontends/gl/ui.h14
m---------ini0
-rw-r--r--lib/cetris.c6
-rw-r--r--lib/cetris.h1
13 files changed, 154 insertions, 48 deletions
diff --git a/frontends/gl/audio.c b/frontends/gl/audio.c
index 587cb0b..1c3d8dd 100644
--- a/frontends/gl/audio.c
+++ b/frontends/gl/audio.c
@@ -7,6 +7,7 @@
#include <SDL_mixer.h>
#include <stdio.h>
+#include <stdlib.h>
#include "audio.h"
void load_multiple_audio(char* name, char* dir_name, int *count, Mix_Chunk*** list) {
diff --git a/frontends/gl/drawable.c b/frontends/gl/drawable.c
index d6fd8a1..38502d2 100644
--- a/frontends/gl/drawable.c
+++ b/frontends/gl/drawable.c
@@ -9,10 +9,10 @@
GLfloat vertices[] = {
// Position Texcoords
- 0.0f, 0.0f, 0.0f, 0.0f, // Top-left
- 0.0f, 0.0f, 1.0f, 0.0f, // Top-right
- 0.0f, 0.0f, 1.0f, 1.0f, // Bottom-right
- 0.0f, 0.0f, 0.0f, 1.0f // Bottom-left
+ -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, // Top-left
+ 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, // Top-right
+ 1.0f, -1.0f, 0.0f, 1.0f, 1.0f, // Bottom-right
+ -1.0f, -1.0f, 0.0f, 0.0f, 1.0f // Bottom-left
};
GLuint elements[] = {
@@ -38,27 +38,25 @@ bool load_image(char* file_name, drawable_t *drawable) {
return true;
}
-void crop_texture(drawable_t *drawable, int h, int w) {
- drawable->vertices[2] = drawable->vertices[14] = w;
- drawable->vertices[3] = drawable->vertices[7] = h;
- drawable->vertices[6] = drawable->vertices[10] = 1.0f;
- drawable->vertices[15] = drawable->vertices[11] = h;
-}
-
void set_block_texture(drawable_t *drawable, uint8_t mino) {
GLfloat index = (mino + 2) * .0625f;
- drawable->vertices[2] = drawable->vertices[14] = index;
- drawable->vertices[6] = drawable->vertices[10] = index + .0625f;
+ drawable->vertices[3] = drawable->vertices[18] = index;
+ drawable->vertices[8] = drawable->vertices[13] = index + .0625f;
+}
+
+void set_shine(drawable_t *drawable, GLfloat amount) {
+ drawable->vertices[2] = drawable->vertices[7] = drawable->vertices[12] =
+ drawable->vertices[17] = amount;
}
void update_rect(drawable_t *drawable, GLfloat x, GLfloat y,
GLfloat w, GLfloat h, GLfloat window_width, GLfloat window_height) {
GLfloat x_ratio = window_width / 2.0f;
GLfloat y_ratio = window_height / 2.0f;
- drawable->vertices[0] = drawable->vertices[12] = -1.0f + (x / x_ratio);
- drawable->vertices[1] = drawable->vertices[5] = 1.0f - (y / y_ratio);
- drawable->vertices[9] = drawable->vertices[13] = drawable->vertices[1] - (h) / y_ratio;
- drawable->vertices[4] = drawable->vertices[8] = drawable->vertices[0] + (w) / x_ratio;
+ drawable->vertices[0] = drawable->vertices[15] = -1.0f + (x / x_ratio);
+ drawable->vertices[1] = drawable->vertices[6] = 1.0f - (y / y_ratio);
+ drawable->vertices[11] = drawable->vertices[16] = drawable->vertices[1] - (h) / y_ratio;
+ drawable->vertices[5] = drawable->vertices[10] = drawable->vertices[0] + (w) / x_ratio;
glBindBuffer(GL_ARRAY_BUFFER, drawable->vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), drawable->vertices, GL_DYNAMIC_DRAW);
@@ -81,13 +79,13 @@ void new_rectangle(drawable_t* drawable) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, drawable->ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_DYNAMIC_DRAW);
- glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (void*)0);
+ glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (void*)0);
glEnableVertexAttribArray(0);
-
- //glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(2* sizeof(GLfloat)));
- //glEnableVertexAttribArray(1);
-
- glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));
+
+ glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
+ glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));
+ glEnableVertexAttribArray(2);
+
}
diff --git a/frontends/gl/drawable.h b/frontends/gl/drawable.h
index 6b1e964..21ae453 100644
--- a/frontends/gl/drawable.h
+++ b/frontends/gl/drawable.h
@@ -1,5 +1,6 @@
#pragma once
+#include <glad/glad.h>
#include <cetris.h>
typedef struct {
@@ -14,6 +15,8 @@ typedef struct {
GLfloat *vertices;
} drawable_t;
+void set_shine(drawable_t *drawable, GLfloat amount);
+void decrease_shine(drawable_t *drawable, GLfloat amount);
void new_rectangle(drawable_t* drawable);
void update_rect(drawable_t *drawable, GLfloat x, GLfloat y, GLfloat w, GLfloat h, GLfloat window_width, GLfloat window_height);
void set_block_texture(drawable_t *drawable, uint8_t mino);
diff --git a/frontends/gl/events.c b/frontends/gl/events.c
index b62584f..4faee32 100644
--- a/frontends/gl/events.c
+++ b/frontends/gl/events.c
@@ -8,6 +8,7 @@
#endif
#include "timer.h"
+#include "drawable.h"
#include "ui.h"
#include <SDL.h>
@@ -25,14 +26,24 @@ void handle_game_event(cetris_ui *ui) {
}
if (ui->board.game.line_event > 0) {
- int index = ui->board.game.line_combo - 1;
+ int index = ui->board.game.line_event - 1;
+ ui->board.skin.overlay_shine = 0.25f;
Mix_PlayChannel( 1, ui->board.skin.clear_sound[index], 0 );
- ui->board.game.line_event--;
+ ui->board.game.line_event = 0;
+ }
+
+ if (ui->board.game.combo_event > 0) {
+ int index = ui->board.game.line_combo - 1;
+ if (index >= ui->board.skin.combo_sound_count) {
+ index = ui->board.skin.combo_sound_count - 1;
+ }
+ Mix_PlayChannel( 2, ui->board.skin.combo_sound[index], 0 );
+ ui->board.game.combo_event--;
}
if (ui->board.game.tetris_event > 0) {
int index = rand() % ui->board.skin.tetris_sound_count;
- Mix_PlayChannel( 1, ui->board.skin.tetris_sound[index], 0 );
+ Mix_PlayChannel( 2, ui->board.skin.tetris_sound[index], 0 );
ui->board.game.tetris_event--;
}
diff --git a/frontends/gl/main.c b/frontends/gl/main.c
index 4880540..a493889 100644
--- a/frontends/gl/main.c
+++ b/frontends/gl/main.c
@@ -21,8 +21,8 @@
static const int FRAME_RATE = 144;
static const int SCREEN_FULLSCREEN = 0;
-static const int SCREEN_WIDTH = 720;
-static const int SCREEN_HEIGHT = 900;
+static const int SCREEN_WIDTH = 900;
+static const int SCREEN_HEIGHT = 720;
static SDL_Window *window = NULL;
static SDL_GLContext maincontext;
@@ -132,7 +132,7 @@ void handle_key(SDL_Event e, key_bindings_t *keys, tetris_board_t* board) {
unhold_move(&board->game, DOWN);
} else if (sym == keys->key_drop) {
unhold_move(&board->game, HARD_DROP);
- } else if (sym == keys->key_rotate_cw) {
+ } else if (sym == keys->key_rotate_cw || sym == 'z') {
unhold_move(&board->game, ROTATE_CW);
} else if (sym == keys->key_rotate_ccw) {
unhold_move(&board->game, ROTATE_CCW);
@@ -149,6 +149,7 @@ int main(void) {
Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 512);
Mix_AllocateChannels(4);
Mix_Volume(0, 20);
+ Mix_Volume(1, 70);
SDL_GL_LoadLibrary(NULL);
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 4 );
@@ -185,11 +186,11 @@ int main(void) {
ui.keys = default_keys;
- glViewport(0, 0, 720, 900);
+ glViewport(0, 0, 900, 720);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- glClearColor(0.21f, 0.12f, 0.11f, 1.0f);
+ glClearColor(0.56f, 0.67f, 0.72f, 1.0f);
glEnable(GL_DEPTH_TEST);
glClearDepth(1.0);
glDepthFunc(GL_LEQUAL);
@@ -203,13 +204,13 @@ int main(void) {
init_game(&ui.board.game);
cetris_start_game(&ui.board.game);
- load_tetris_board(&ui, &ui.board, 235.0f, 200.0f, 250.0f, 500.0f);
- load_held_piece(&ui, &ui.board, 152.0f, 210.0f, 88.0f, 88.0f);
-
+ load_tetris_board(&ui, &ui.board, 325.0f, 110.0f, 250.0f, 500.0f);
+ load_piece_queue(&ui, &ui.board, 610.0f, 100.0f, 100.0f, 400.0f);
+ load_held_piece(&ui, &ui.board, 242.0f, 110.0f, 88.0f, 88.0f);
+
start_event_thread(&ui);
SDL_Event e;
- int delay = 1000/FRAME_RATE;
for (;;) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
@@ -219,6 +220,7 @@ int main(void) {
draw_tetris_board(&ui);
draw_held_piece(&ui);
+ draw_piece_queue(&ui);
SDL_GL_SwapWindow(window);
}
diff --git a/frontends/gl/shader.c b/frontends/gl/shader.c
index 7e9ef3c..2980205 100644
--- a/frontends/gl/shader.c
+++ b/frontends/gl/shader.c
@@ -2,24 +2,25 @@
const char *vertex_shader_source = "#version 450 core\n"
"layout (location = 0) in vec2 aPos;\n"
- "layout (location = 1) in vec2 aTexCoord;\n"
+ "layout (location = 1) in float aShine;\n"
+ "layout (location = 2) in vec2 aTexCoord;\n"
"out vec2 TexCoord;\n"
+ "out float shineColor;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(aPos, 0.0, 1.0);\n"
" TexCoord = aTexCoord;\n"
+ " shineColor = aShine;\n"
"}\0";
const char *fragment_shader_source = "#version 450 core\n"
"out vec4 FragColor;\n"
"in vec2 TexCoord;\n"
+ "in float shineColor;\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(tex, TexCoord);\n"
+ " FragColor = mix(texture(tex, TexCoord), vec4(1.0, 1.0, 1.0, 1.0), shineColor);\n"
"}\0";
//"uniform sampler2D overlayTexture;\n"
diff --git a/frontends/gl/skin.c b/frontends/gl/skin.c
index 4cdb1ac..5975aec 100644
--- a/frontends/gl/skin.c
+++ b/frontends/gl/skin.c
@@ -49,11 +49,12 @@ void load_skin(char* name, cetris_skin_t* skin) {
load_element(&skin->playboard, "playboard.png", dir_name);
load_element(&skin->border, "border.png", dir_name);
- char file[125];
-
- load_multiple_audio("clear", dir_name,
+ load_multiple_audio("erase", dir_name,
&skin->clear_sound_count, &skin->clear_sound);
+ load_multiple_audio("combo", dir_name,
+ &skin->combo_sound_count, &skin->combo_sound);
+
load_multiple_audio("four_clear", dir_name,
&skin->tetris_sound_count, &skin->tetris_sound);
diff --git a/frontends/gl/skin.h b/frontends/gl/skin.h
index 09e0251..8cb8f7e 100644
--- a/frontends/gl/skin.h
+++ b/frontends/gl/skin.h
@@ -10,6 +10,12 @@ typedef struct {
drawable_t border;
drawable_t background;
+ GLfloat current_shine;
+ GLfloat overlay_shine;
+
+ Mix_Chunk **combo_sound;
+ int combo_sound_count;
+
Mix_Chunk **clear_sound;
int clear_sound_count;
diff --git a/frontends/gl/ui.c b/frontends/gl/ui.c
index f252cbf..d2747e5 100644
--- a/frontends/gl/ui.c
+++ b/frontends/gl/ui.c
@@ -29,9 +29,27 @@ void load_held_piece(cetris_ui *ui, tetris_board_t *board, GLfloat x, GLfloat y,
board->held.y_offset = y;
}
+void load_piece_queue(cetris_ui *ui, tetris_board_t *board, GLfloat x, GLfloat y, GLfloat w, GLfloat h) {
+ board->queue.block_width = w / 4.0f;
+ board->queue.block_height = w / 4.0f;
+
+ board->queue.h = h;
+ board->queue.w = w;
+
+ board->queue.x_offset = x;
+ board->queue.y_offset = y;
+}
+
+
void draw_tetris_board(cetris_ui *ui) {
glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, ui->board.skin.background.texture);
+ glBindVertexArray(ui->board.skin.background.vao);
+ glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
+
+ glBindTexture(GL_TEXTURE_2D, 0);
+
glBindTexture(GL_TEXTURE_2D, ui->board.skin.playboard.texture);
glBindVertexArray(ui->board.skin.playboard.vao);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
@@ -45,16 +63,19 @@ void draw_tetris_board(cetris_ui *ui) {
GLfloat height = ui->board.block_height;
GLfloat y_pos = (ui->board.y_offset - ui->board.block_height) + (y * ui->board.block_height);
if (y == 0) {
- ui->board.skin.overlay.vertices[3] = ui->board.skin.overlay.vertices[7] = 0.6f;
+ ui->board.skin.overlay.vertices[4] = ui->board.skin.overlay.vertices[9] = 0.6f;
height = 10;
y_pos += 15;
}
+ set_shine(&ui->board.skin.overlay, ui->board.skin.overlay_shine);
update_rect(&ui->board.skin.overlay, ui->board.x_offset,
y_pos, ui->board.block_width * ui->board.game.config.board_x,
height, ui->window_width,
ui->window_height);
+ if (ui->board.skin.overlay_shine > 0)
+ ui->board.skin.overlay_shine -= 0.00025f;
if (y == 0)
- ui->board.skin.overlay.vertices[3] = ui->board.skin.overlay.vertices[7] = 0.0f;
+ ui->board.skin.overlay.vertices[4] = ui->board.skin.overlay.vertices[9] = 0.0f;
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}
@@ -161,3 +182,50 @@ void draw_held_piece(cetris_ui *ui) {
}
glBindTexture(GL_TEXTURE_2D, 0);
}
+
+
+void draw_piece_queue(cetris_ui *ui) {
+ glActiveTexture(GL_TEXTURE0);
+ glBindTexture(GL_TEXTURE_2D, 0);
+ glBindTexture(GL_TEXTURE_2D, ui->board.skin.block.texture);
+ glBindVertexArray(ui->board.skin.block.vao);
+
+ for (int i = 0; i < 5; i++) {
+ int index = (ui->board.game.current_index + i);
+ int mino;
+
+ if (index <= 6) {
+ mino = ui->board.game.piece_queue[index];
+ } else {
+ index = index % 7;
+ mino = ui->board.game.next_queue[index];
+ }
+
+ for (int s = 0; s < 4; s++) {
+ for (int j = 0; j < 4; j++) {
+
+ if ((default_matrices[mino][s]>>(3 - j))&1) {
+ GLfloat block_x =
+ ui->board.queue.x_offset + (j * ui->board.queue.block_width);
+ GLfloat block_y =
+ ui->board.queue.y_offset + (ui->board.queue.h * (i / 5.0)) + (s * ui->board.queue.block_height);
+
+ if (ui->board.game.held.t == MINO_O) {
+ block_x -= ui->board.queue.block_width / 1.5f;
+ }
+ if (ui->board.game.held.t == MINO_I) {
+ block_x -= ui->board.queue.block_width / 1.5f;
+ }
+
+ set_block_texture(&ui->board.skin.block, mino);
+ update_rect(&ui->board.skin.block, block_x, block_y,
+ ui->board.queue.block_width, ui->board.queue.block_height,
+ ui->window_width, ui->window_height);
+
+ glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
+ }
+ }
+ }
+ }
+ glBindTexture(GL_TEXTURE_2D, 0);
+} \ No newline at end of file
diff --git a/frontends/gl/ui.h b/frontends/gl/ui.h
index 8a6f2c6..7b82a51 100644
--- a/frontends/gl/ui.h
+++ b/frontends/gl/ui.h
@@ -16,6 +16,17 @@ typedef struct {
typedef struct {
GLfloat block_width;
GLfloat block_height;
+
+ GLfloat h;
+ GLfloat w;
+
+ GLfloat x_offset;
+ GLfloat y_offset;
+} piece_queue_t;
+
+typedef struct {
+ GLfloat block_width;
+ GLfloat block_height;
GLfloat block_offset;
GLfloat x_offset;
@@ -24,6 +35,7 @@ typedef struct {
cetris_game game;
held_piece_t held;
+ piece_queue_t queue;
cetris_skin_t skin;
} tetris_board_t;
@@ -54,6 +66,8 @@ typedef struct {
void draw_tetris_board(cetris_ui *ui);
void draw_held_piece(cetris_ui *ui);
+void draw_piece_queue(cetris_ui *ui);
void draw_current(cetris_ui *ui);
void load_tetris_board(cetris_ui *ui, tetris_board_t *board, GLfloat x, GLfloat y, GLfloat w, GLfloat h);
void load_held_piece(cetris_ui *ui, tetris_board_t *board, GLfloat x, GLfloat y, GLfloat w, GLfloat h);
+void load_piece_queue(cetris_ui *ui, tetris_board_t *board, GLfloat x, GLfloat y, GLfloat w, GLfloat h);
diff --git a/ini b/ini
-Subproject 9b15f721fd53b1beb8364f8763e2207963ac4fd
+Subproject 3401f2ed3153c6093c2f434e205cf6e7775a02d
diff --git a/lib/cetris.c b/lib/cetris.c
index ea53576..14c3a45 100644
--- a/lib/cetris.c
+++ b/lib/cetris.c
@@ -167,7 +167,7 @@ static void move_current(cetris_game *g, uint8_t move) {
g->current.pos.x += basic_movements[move].x;
int check = check_matrix(g, &g->current.m);
- if (check > 0 && move != DOWN) g->move_event++;
+ if (check > 0) g->move_event++;
if (check <= 0) {
g->current.pos.y -= basic_movements[move].y;
g->current.pos.x -= basic_movements[move].x;
@@ -393,10 +393,11 @@ void update_board(cetris_game *g) {
g->lines += lines_cleared;
if (lines_cleared > 0) {
+ g->line_event = lines_cleared;
if (lines_cleared == 4) {
g->tetris_event++;
} else {
- g->line_event++;
+ g->combo_event++;
}
g->line_combo++;
}
@@ -531,7 +532,6 @@ CETRIS_EXPORT bool update_game_tick(cetris_game *g) {
bool did_move = false;
if (g->next_drop_tick && g->tick >= g->next_drop_tick) {
- if (g->held_moves[DOWN]) g->move_event++;
move_current(g, DOWN);
g->next_drop_tick = 0;
did_move = true;
diff --git a/lib/cetris.h b/lib/cetris.h
index 1c3d8f0..3db7d37 100644
--- a/lib/cetris.h
+++ b/lib/cetris.h
@@ -147,6 +147,7 @@ struct cetris_game {
// events
uint8_t move_event;
uint8_t line_event;
+ uint8_t combo_event;
uint8_t lock_event;
uint8_t tetris_event;