diff options
| author | talha <talha@talhaamir.xyz> | 2025-12-29 13:45:53 +0500 |
|---|---|---|
| committer | talha <talha@talhaamir.xyz> | 2025-12-29 13:45:53 +0500 |
| commit | 9044db4794e95ef9dbc2d5bfde4215ddc82d6ae8 (patch) | |
| tree | a63784ac600579006a650c49f30d6d2f94b6905f /source/game | |
| parent | 0b3d69976819219e71350b6a988d1704fd5f0746 (diff) | |
Diffstat (limited to 'source/game')
| -rw-r--r-- | source/game/game.cpp | 144 | ||||
| -rw-r--r-- | source/game/game.h | 22 |
2 files changed, 139 insertions, 27 deletions
diff --git a/source/game/game.cpp b/source/game/game.cpp index 337fe11..4229483 100644 --- a/source/game/game.cpp +++ b/source/game/game.cpp @@ -1,25 +1,86 @@ #include "game.h" -void game_setup(GameState *state) { - glUseProgram(state->triangle_sp); +Cloth game_cloth = { + .width = 32, + .elasticity = 10.0f, + .padding = 30.0f, +}; + +void game_init(GameState *state) { + i32 cloth_rows = NUM_POINTS / game_cloth.width; + Vec2 start_pos = { + .x = state->width / 4.0f, + .y = state->height - state->height/4.0f + }; + + i32 max_rows = NUM_POINTS / game_cloth.width; + i32 max_cols = game_cloth.width; + i32 curr_row = 0, curr_col = 0; + Vec2 running_pos = start_pos; + for (i32 i = 0; i < NUM_POINTS; i++) { + Point p = { + .id = i, + .active = 1, + .prev_pos = running_pos, + .curr_pos = running_pos, + .force = {0.0f,0.0f} + }; + { + // @fixme + i32 left_ind = i%max_cols == 0 ? -1 : i-1; + i32 right_ind = i%max_cols == max_cols - 1 ? -1 : i+1; + i32 top_ind = i/max_cols == 0 ? -1 : i - max_cols; + i32 bot_ind = i/max_cols == max_rows-1 ? -1 : i + max_cols; + p.constraints[0] = left_ind; + p.constraints[1] = top_ind; + p.constraints[2] = right_ind; + p.constraints[3] = bot_ind; + } + game_cloth.points[i] = p; + + // next position + i32 next_row = (i+1)/max_cols; + i32 next_col = (i+1)%max_cols; + Vec2 next_pos = { + start_pos.x + game_cloth.padding*next_col, + start_pos.y - game_cloth.padding*next_row + }; + running_pos = next_pos; + } +} + +void game_shader_init_uniforms(GameState *state) { Mat4 projection = calcify_ortho4m(0.0f, state->width, 0.0f, state->height, 0.1f, 10.0f); + + glUseProgram(state->triangle_sp); i32 triangle_proj_loc = glGetUniformLocation(state->triangle_sp, "Projection"); glUniformMatrix4fv(triangle_proj_loc, 1, 0, projection.buffer); + + glUseProgram(state->line_sp); + i32 line_proj_loc = glGetUniformLocation(state->line_sp, "Projection"); + glUniformMatrix4fv(line_proj_loc, 1, 0, projection.buffer); + glUseProgram(0); }; void game_handle_event(GameState *state, GameEventType type) { switch (type) { case GAME_EVENT_RESIZE: { - glUseProgram(state->triangle_sp); glViewport(0, 0, state->width, state->height); - Mat4 projection = calcify_ortho4m(0.0f, state->width, 0.0f, state->height, 1.0f, 10.0f); + + glUseProgram(state->triangle_sp); + // @step: update triangle sp i32 triangle_proj_loc = glGetUniformLocation(state->triangle_sp, "Projection"); glUniformMatrix4fv(triangle_proj_loc, 1, 0, projection.buffer); - glUseProgram(0); + glUseProgram(state->line_sp); + // @step: update line sp + i32 line_proj_loc = glGetUniformLocation(state->line_sp, "Projection"); + glUniformMatrix4fv(line_proj_loc, + 1, 0, + projection.buffer); } break; default: @@ -27,30 +88,61 @@ void game_handle_event(GameState *state, GameEventType type) { } } -void game_update_and_render(GameState *state) { - glClearColor(0.1f, 0.1f, 0.1f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); +void draw_line(GameState *state, Vec3 position, Vec2 size, r32 rotation, Vec3 color) { + glUseProgram(state->line_sp); - glUseProgram(state->triangle_sp); Mat4 model = calcify_ident4m(); - Mat4 scale = calcify_scaling_matrix4m(105.0f, 95.0f); - model = calcify_multiply4m(scale, model); + Mat4 rotate = calcify_rotation_matrix4m(rotation, {.x=0,.y=0,.z=1}); + Mat4 origin_set = calcify_translation_matrix4m(size.x/2.0f, + 0.0f, + 0.0f); + Mat4 scale = calcify_scaling_matrix4m(size.x, size.y); + Mat4 pos = calcify_translation_matrix4m(position.x, + position.y, + position.z); - Mat4 pos = calcify_translation_matrix4m(350.0f, 950.0f, -1.0f); + model = calcify_multiply4m(scale, model); + model = calcify_multiply4m(origin_set, model); + model = calcify_multiply4m(rotate, model); model = calcify_multiply4m(pos, model); - i32 model_loc = glGetUniformLocation(state->triangle_sp, + + i32 model_loc = glGetUniformLocation(state->line_sp, "Model"); - glUniformMatrix4fv( - model_loc, - 1, 0, - model.buffer - ); - - Vec3 Color = (Vec3){.r=0.4,.g=0.9,.b=0.7}; - glUniform3f(glGetUniformLocation(state->triangle_sp, - "Color"), - Color.r, Color.g, Color.b); - glBindVertexArray(state->triangle_vao); - glDrawArrays(GL_TRIANGLES, 0, 3); - glBindVertexArray(0); + glUniformMatrix4fv(model_loc, + 1, 0, + model.buffer); + i32 color_loc = glGetUniformLocation(state->line_sp, + "Color"); + glUniform3f(color_loc, color.x, color.y, color.z); + + glBindVertexArray(state->line_vao); + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + glUseProgram(0); +} + +void game_update_and_render(GameState *state) { + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + + for (int i = 0; i < NUM_POINTS; i++) { + Point p = game_cloth.points[i]; + if (!p.active) continue; + + for (int c = 2; c < 4; c++) { + i32 ci = p.constraints[c]; + if (ci == -1) continue; + Point constraint = game_cloth.points[ci]; + Vec2 a = p.curr_pos; + Vec2 b = constraint.curr_pos; + Vec2 dir_vector = {b.x - a.x, b.y - a.y}; + r32 line_length = sqrt(dir_vector.x*dir_vector.x + + dir_vector.y*dir_vector.y); + r32 rot_angle = atan2(dir_vector.y,dir_vector.x); + draw_line(state, + {a.x, a.y, -1.0f}, + {line_length, 3.0f}, + rot_angle, + {0.4f, 0.5f, 0.7f}); + } + } } diff --git a/source/game/game.h b/source/game/game.h index 4c6f1f2..6cd5d56 100644 --- a/source/game/game.h +++ b/source/game/game.h @@ -12,9 +12,29 @@ #endif extern "C" { + EXPORT void game_init(GameState *state); + EXPORT void game_shader_init_uniforms(GameState *state); EXPORT void game_handle_event(GameState *state, GameEventType type); - EXPORT void game_setup(GameState *state); EXPORT void game_update_and_render(GameState *state); } +struct Point { + i32 id; + b8 active; + Vec2 prev_pos; + Vec2 curr_pos; + Vec2 force; + i32 constraints[4]; +}; +typedef struct Point Point; + +#define NUM_POINTS 1024 +struct Cloth { + i32 width; + r32 elasticity; + r32 padding; + Point points[NUM_POINTS]; +}; +typedef struct Cloth Cloth; + #endif // AMR_GAME_H |
