Skip to content

Commit

Permalink
add shader to assets loading
Browse files Browse the repository at this point in the history
  • Loading branch information
julesgrc0 committed Feb 12, 2024
1 parent 5d7d904 commit 33d4722
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 70 deletions.
28 changes: 28 additions & 0 deletions assets/shaders/menu_blur.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#version 330

in vec2 fragTexCoord;
in vec4 fragColor;

uniform sampler2D texture0;
uniform vec4 colDiffuse;

out vec4 finalColor;

const float renderWidth = 640;
const float renderHeight = 360;

float offset[3] = float[](0.0, 1.3846153846, 3.2307692308);
float weight[3] = float[](0.2270270270, 0.3162162162, 0.0702702703);

void main()
{
vec3 texelColor = texture(texture0, fragTexCoord).rgb*weight[0];

for (int i = 1; i < 3; i++)
{
texelColor += texture(texture0, fragTexCoord + vec2(offset[i])/renderWidth, 0.0).rgb*weight[i];
texelColor += texture(texture0, fragTexCoord - vec2(offset[i])/renderWidth, 0.0).rgb*weight[i];
}

finalColor = vec4(texelColor, 1.0);
}
10 changes: 8 additions & 2 deletions src/core/mainframe.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,20 @@ void destroy_mainframe(w_state *state) {
if (state == NULL)
return;

for (size_t i = 0; i < state->len; i++) {
for (size_t i = 0; i < state->textures_len; i++) {
sfree(state->textures_id[i]);
UnloadTexture(state->textures[i]);
}

sfree(state->textures);
sfree(state->textures_id);

for (size_t i = 0; i < state->shaders_len; i++) {
sfree(state->shaders_id[i]);
UnloadShader(state->shaders[i]);
}
sfree(state->shaders);
sfree(state->shaders_id);

UnloadFont(state->font);
UnloadRenderTexture(state->render);
CloseWindow();
Expand Down
13 changes: 12 additions & 1 deletion src/core/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,22 @@ void save_config(w_config *config) {
}

Texture get_texture_by_id(w_state *state, char *id) {
for (size_t i = 0; i < state->len; i++) {
for (size_t i = 0; i < state->textures_len; i++) {
if (strcmp(state->textures_id[i], id) == 0) {
return state->textures[i];
}
}

return (Texture){0};
}

Shader get_shader_by_id(w_state *state, char *id) {

for (size_t i = 0; i < state->shaders_len; i++) {
if (strcmp(state->shaders_id[i], id) == 0) {
return state->shaders[i];
}
}

return (Shader){0};
}
7 changes: 6 additions & 1 deletion src/core/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ typedef struct w_state {

char **textures_id;
Texture *textures;
size_t len;
size_t textures_len;

char **shaders_id;
Shader *shaders;
size_t shaders_len;

Font font;

Expand All @@ -57,3 +61,4 @@ w_config *load_config();
void save_config(w_config *config);

Texture get_texture_by_id(w_state *state, char *id);
Shader get_shader_by_id(w_state *state, char *id);
2 changes: 1 addition & 1 deletion src/entities/player.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ Rectangle check_player_collision_vel(w_player *player, w_chunkview *view) {

bool col_x = false;
bool col_y = false;
for (size_t i = 0; i < view->len; i++) {
for (size_t i = 0; i < view->textures_len; i++) {
Rectangle block = view->blocks[i].dst;
if (!col_x && CheckCollisionRecs(block, next_velx)) {
col_x = true;
Expand Down
2 changes: 1 addition & 1 deletion src/screen/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void game_screen(w_state *state) {
BeginMode2D(*(td->camera));
smooth_vec(&td->camera->target, td->camera_target, speed);

for (unsigned int i = 0; i < td->chunk_view->len; i++) {
for (unsigned int i = 0; i < td->chunk_view->textures_len; i++) {
DrawTexturePro(block_textures[td->chunk_view->blocks[i].block.type - 1],
td->chunk_view->blocks[i].src,
td->chunk_view->blocks[i].dst, VEC_ZERO, 0,
Expand Down
45 changes: 33 additions & 12 deletions src/screen/loading.c
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
#include "loading.h"

void load_assets(w_state *state) {
printf("Loading assets...\n");

state->render = LoadRenderTexture(RENDER_W, RENDER_H);
// LoadRenderTexture(state->config->render_size, state->config->render_size);
state->src_rnd = (Rectangle){0.0f, 0.0f, (float)state->render.texture.width,
-(float)state->render.texture.height};
state->dest_rnd = (Rectangle){0.0f, 0.0f, (float)GetScreenWidth(),
(float)GetScreenHeight()};

size_t len;
w_asset *items = unpack_assets(state->hInstance, &len);
size_t items_len;
w_asset *items = unpack_assets(state->hInstance, &items_len);
if (!items) {
state->state = F_FAILED;
return;
}

state->textures = malloc(sizeof(Texture) * len);
state->textures_id = malloc(sizeof(char *) * len);
state->textures = malloc(sizeof(Texture) * items_len);
state->textures_id = malloc(sizeof(char *) * items_len);

state->shaders = malloc(sizeof(Texture) * items_len);
state->shaders_id = malloc(sizeof(char *) * items_len);

state->font = GetFontDefault();

size_t textures_index = 0;
for (size_t i = 0; i < len; i++) {
for (size_t i = 0; i < items_len; i++) {
const char *ext = strrchr(items[i].name, '.');

if (strcmp(ext, ".png") == 0) {
Image image = LoadImageFromMemory(".png", items[i].buffer, items[i].size);
state->textures[textures_index] = LoadTextureFromImage(image);
state->textures[state->textures_len] = LoadTextureFromImage(image);
UnloadImage(image);

state->textures_id[textures_index] = items[i].name;
textures_index++;
state->textures_id[state->textures_len] = items[i].name;
state->textures_len++;
} else if (strcmp(ext, ".ttf") == 0) {
int fontSize = 72;
char fontChars[73] =
Expand All @@ -40,6 +44,18 @@ void load_assets(w_state *state) {
fontSize, fontChars, 73);

sfree(items[i].name);
} else if (strcmp(ext, ".vs") == 0 || strcmp(ext, ".fs") == 0) {
bool is_vertex = strcmp(ext, ".vs") == 0;

items[i].buffer = realloc(items[i].buffer, items[i].size + 1);
items[i].buffer[items[i].size] = '\0';

state->shaders[state->shaders_len] =
LoadShaderFromMemory(is_vertex ? items[i].buffer : NULL,
is_vertex ? NULL : items[i].buffer);
state->shaders_id[state->shaders_len] = items[i].name;
state->shaders_len++;

} else {
sfree(items[i].name);
sfree(items[i].buffer);
Expand All @@ -49,9 +65,14 @@ void load_assets(w_state *state) {
sfree(items[i].buffer);
}

state->len = textures_index;
state->textures = realloc(state->textures, sizeof(Texture) * state->len);
state->textures_id = realloc(state->textures_id, sizeof(char *) * state->len);
state->textures =
realloc(state->textures, sizeof(Texture) * state->textures_len);
state->textures_id =
realloc(state->textures_id, sizeof(char *) * state->textures_len);

state->shaders = realloc(state->shaders, sizeof(Shader) * state->shaders_len);
state->shaders_id =
realloc(state->shaders_id, sizeof(char *) * state->shaders_len);

sfree(items);

Expand Down
47 changes: 15 additions & 32 deletions src/screen/menu.c
Original file line number Diff line number Diff line change
@@ -1,33 +1,10 @@
#include "menu.h"

const char *blurShaderFS =
"#version 330\n\n"
"in vec2 fragTexCoord;\n"
"in vec4 fragColor;\n\n"
"uniform sampler2D texture0;\n"
"uniform vec4 colDiffuse;\n\n"
"out vec4 finalColor;\n\n"
"const float renderWidth = 640;\n"
"const float renderHeight = 360;\n\n"
"float offset[3] = float[](0.0, 1.3846153846, 3.2307692308);\n"
"float weight[3] = float[](0.2270270270, 0.3162162162, 0.0702702703);\n\n"
"void main()\n"
"{\n"
" vec3 texelColor = texture(texture0, fragTexCoord).rgb*weight[0];\n\n"
" for (int i = 1; i < 3; i++)\n"
" {\n"
" texelColor += texture(texture0, fragTexCoord + "
"vec2(offset[i])/renderWidth, 0.0).rgb*weight[i];\n"
" texelColor += texture(texture0, fragTexCoord - "
"vec2(offset[i])/renderWidth, 0.0).rgb*weight[i];\n"
" }\n\n"
" finalColor = vec4(texelColor, 1.0);\n"
"}\n";
void menu_screen(w_state *state) {
ShowCursor();

bool is_active = true;
Shader blurShader = LoadShaderFromMemory(NULL, blurShaderFS);
Shader blurShader = get_shader_by_id(state, "shaders\\menu_blur.fs");

Texture block_textures[6] = {
get_texture_by_id(state, "blocks\\grass.png"),
Expand All @@ -53,13 +30,23 @@ void menu_screen(w_state *state) {

w_guibutton *play_button = create_button(
ctx, percent_to_pixel(ctx, (Vector2){0.5, 0.45}), WHITE, "Play");
w_guibutton *setting_button = create_button(
ctx, percent_to_pixel(ctx, (Vector2){0.5, 0.52}), WHITE, "Settings");
w_guibutton *setting_button =
create_button(ctx,
(Vector2){
.x = play_button->position.x + play_button->size.x / 2,
.y = play_button->rect.y + play_button->rect.height * 2,
},
WHITE, "Settings");
setting_button->default_color = Fade(WHITE, 0.5);
setting_button->hover_color = Fade(WHITE, 0.5);

w_guibutton *exit_button = create_button(
ctx, percent_to_pixel(ctx, (Vector2){0.5, 0.59}), WHITE, "Exit");
ctx,
(Vector2){
.x = play_button->position.x + play_button->size.x / 2,
.y = setting_button->rect.y + setting_button->rect.height * 2,
},
WHITE, "Exit");

w_guitext *title_text = create_text(
ctx, percent_to_pixel(ctx, (Vector2){0.5, 0.2}), "Wispy", 120, WHITE);
Expand All @@ -86,7 +73,7 @@ void menu_screen(w_state *state) {
DrawRectangleGradientV(0, 0, RENDER_W, RENDER_H, (Color){66, 135, 245, 255},
(Color){142, 184, 250, 255});
BeginMode2D(camera);
for (unsigned int i = 0; i < view->len; i++) {
for (unsigned int i = 0; i < view->textures_len; i++) {
DrawTexturePro(block_textures[view->blocks[i].block.type - 1],
view->blocks[i].src, view->blocks[i].dst, VEC_ZERO, 0,
view->blocks[i].light);
Expand Down Expand Up @@ -131,8 +118,4 @@ void menu_screen(w_state *state) {
destroy_button(setting_button);
destroy_button(exit_button);
destroy_gui(ctx);

UnloadShader(blurShader);
}

void setup_menu_background(w_state *state) {}
2 changes: 0 additions & 2 deletions src/screen/menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,3 @@
#include "../terrain/chunk_view.h"

void menu_screen(w_state *state);

void setup_menu_background(w_state *state);
16 changes: 8 additions & 8 deletions src/terrain/chunk_view.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ w_chunkview *create_chunkview(w_chunk *current) {
}
chunk_view = memset(chunk_view, 0, sizeof(w_chunkview));

chunk_view->blocks = malloc(chunk_view->len);
chunk_view->blocks = malloc(chunk_view->textures_len);
if (chunk_view->blocks == NULL) {
LOG("failed to allocate memory for chunk view blocks");
free(chunk_view);
Expand Down Expand Up @@ -46,15 +46,15 @@ void destroy_chunkview(w_chunkview *chunk_view) {
}

void update_renderblock_async(w_chunkview *chunk_view, w_renderblock *blocks,
size_t len) {
size_t textures_len) {
#ifdef _WIN32
WaitForSingleObject(chunk_view->mutex, INFINITE);
#else
pthread_mutex_lock(&td->mutex);
#endif // _WIN32

free(chunk_view->blocks);
chunk_view->len = len;
chunk_view->textures_len = textures_len;
chunk_view->blocks = blocks;

#ifdef _WIN32
Expand Down Expand Up @@ -94,12 +94,12 @@ bool update_chunkview(w_chunkview *chunk_view, w_chunkgroup *grp,
}

size_t rendercount = 0;
size_t len = CHUNK_W * CHUNK_H * ((chunk_view->next == NULL) ? 1 : 2);
w_renderblock *blocks = malloc(len * sizeof(w_renderblock));
size_t textures_len = CHUNK_W * CHUNK_H * ((chunk_view->next == NULL) ? 1 : 2);
w_renderblock *blocks = malloc(textures_len * sizeof(w_renderblock));
if (blocks == NULL) {
return false;
}
memset(blocks, 0, len * sizeof(w_renderblock));
memset(blocks, 0, textures_len * sizeof(w_renderblock));
filter_chunkview_blocks(chunk_view->target, view, blocks, &rendercount);
if (chunk_view->next != NULL) {
filter_chunkview_blocks(chunk_view->next, view, blocks, &rendercount);
Expand All @@ -109,7 +109,7 @@ bool update_chunkview(w_chunkview *chunk_view, w_chunkgroup *grp,
free(blocks);
update_renderblock_async(chunk_view, NULL, 0);
return true;
} else if (rendercount != len) {
} else if (rendercount != textures_len) {
w_renderblock *tmp = realloc(blocks, sizeof(w_renderblock) * rendercount);
if (tmp == NULL) {
free(blocks);
Expand Down Expand Up @@ -164,7 +164,7 @@ void update_chunkview_lighting(w_chunkview *chunk_view, Vector2 light) {
}

float light_radius = 1000;
for (size_t i = 0; i < chunk_view->len; i++) {
for (size_t i = 0; i < chunk_view->textures_len; i++) {
Vector2 block_center = {chunk_view->blocks[i].dst.x + (CUBE_W / 2),
chunk_view->blocks[i].dst.y + (CUBE_H / 2)};

Expand Down
4 changes: 2 additions & 2 deletions src/terrain/chunk_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ typedef struct w_chunkview {
w_chunk *target;
w_chunk *next;
w_renderblock *blocks;
unsigned int len; // : 15; // 2^15 = 2^14 * 2
unsigned int textures_len; // : 15; // 2^15 = 2^14 * 2

#ifdef _WIN32
HANDLE mutex;
Expand All @@ -24,7 +24,7 @@ w_chunkview *create_chunkview(w_chunk *current);
void destroy_chunkview(w_chunkview *chunk_view);

void update_renderblock_async(w_chunkview *chunk_view, w_renderblock *blocks,
size_t len);
size_t textures_len);
bool update_chunkview(w_chunkview *chunk_view, w_chunkgroup *grp,
Rectangle view);
void filter_chunkview_blocks(w_chunk *chunk, Rectangle view,
Expand Down
Loading

0 comments on commit 33d4722

Please sign in to comment.