Skip to content

Commit

Permalink
Add changes
Browse files Browse the repository at this point in the history
Co-Authored-By: Pepe Levi <[email protected]>
  • Loading branch information
W2Wizard and PepeLevi committed Oct 7, 2024
1 parent c6b2e4c commit 7a0d6f8
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 56 deletions.
7 changes: 5 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ set(BUILD_TESTS OFF CACHE BOOL "Build the tests to verify the integrity of the l
# Reduce the size of LodePNG, we don't need these things.
add_definitions(-D LODEPNG_NO_COMPILE_ENCODER)
add_definitions(-D LODEPNG_NO_COMPILE_ANCILLARY_CHUNKS)
if(EMSCRIPTEN)
add_definitions(-D EMSCRIPTEN)
endif()

if(UNIX AND NOT EMSCRIPTEN)
set(CCSHADER ${PROJECT_SOURCE_DIR}/tools/compile_shader.sh)
Expand All @@ -41,7 +44,7 @@ if(UNIX AND NOT EMSCRIPTEN)
-Wall
-Werror
-Wunreachable-code

# Some low priority warnings that are annoying.
-Wno-char-subscripts
-Wno-sign-compare
Expand Down Expand Up @@ -150,7 +153,7 @@ endif()

# Testing
# -----------------------------------------------------------------------------
# Only build tests if we are the main project or explicitly told to, make sure
# Only build tests if we are the main project or explicitly told to, make sure
# tests are not built when mlx42 is included as a subproject, use MLX42_BUILD_TESTS to overwrite this
# use cmake -DBUILD_TESTS=ON/-DMLX42_BUILD_TESTS=ON to build tests

Expand Down
117 changes: 65 additions & 52 deletions src/mlx_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static bool mlx_create_buffers(mlx_t* mlx)
/**
* Compiles the given shader source code of a given shader type.
* Returns shader object via param.
*
*
* @param code The shader source code.
* @param Type GL_VERTEX_SHADER, GL_FRAGMENT_SHADER, GL_GEOMETRY_SHADER, ...
* @return Non-zero on success, else 0.
Expand All @@ -81,7 +81,7 @@ static uint32_t mlx_compile_shader(const char* code, int32_t type)
int32_t success;
char infolog[512] = {0};

if (!code || (shader = glCreateShader(type)) == 0)
if (!code || (shader = glCreateShader(type)) == 0)
return (0);

GLint len = strlen(code);
Expand All @@ -100,53 +100,56 @@ static uint32_t mlx_compile_shader(const char* code, int32_t type)

static bool mlx_init_render(mlx_t* mlx)
{
uint32_t vshader = 0;
uint32_t fshader = 0;
char infolog[512] = {0};
mlx_ctx_t* mlxctx = mlx->context;

glfwMakeContextCurrent(mlx->window);
glfwSetFramebufferSizeCallback(mlx->window, framebuffer_callback);
glfwSetWindowUserPointer(mlx->window, mlx);
glfwSwapInterval(MLX_SWAP_INTERVAL);

// Load all OpenGL function pointers
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
return (mlx_error(MLX_GLADFAIL));

if (!(vshader = mlx_compile_shader(vert_shader, GL_VERTEX_SHADER)))
return (mlx_error(MLX_VERTFAIL));
if (!(fshader = mlx_compile_shader(frag_shader, GL_FRAGMENT_SHADER)))
return (mlx_error(MLX_FRAGFAIL));
if (!(mlxctx->shaderprogram = glCreateProgram()))
{
glDeleteShader(fshader);
glDeleteShader(vshader);
return (mlx_error(MLX_SHDRFAIL));
}
glAttachShader(mlxctx->shaderprogram, vshader);
glAttachShader(mlxctx->shaderprogram, fshader);
glLinkProgram(mlxctx->shaderprogram);

glDeleteShader(vshader);
glDeleteShader(fshader);
glDetachShader(mlxctx->shaderprogram, vshader);
glDetachShader(mlxctx->shaderprogram, fshader);

int32_t success;
glGetProgramiv(mlxctx->shaderprogram, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(mlxctx->shaderprogram, sizeof(infolog), NULL, infolog);
fprintf(stderr, "%s", infolog);
return (mlx_error(MLX_SHDRFAIL));
}
glUseProgram(mlxctx->shaderprogram);

for (size_t i = 0; i < 16; i++)
mlxctx->bound_textures[i] = 0;

return (true);
uint32_t vshader = 0;
uint32_t fshader = 0;
char infolog[512] = {0};
mlx_ctx_t* mlxctx = mlx->context;

glfwMakeContextCurrent(mlx->window);
glfwSetFramebufferSizeCallback(mlx->window, framebuffer_callback);
glfwSetWindowUserPointer(mlx->window, mlx);
glfwSwapInterval(MLX_SWAP_INTERVAL);

// Load all OpenGL function pointers
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
return (mlx_error(MLX_GLADFAIL));
if (!(vshader = mlx_compile_shader(vert_shader, GL_VERTEX_SHADER)))
return (mlx_error(MLX_VERTFAIL));
if (!(fshader = mlx_compile_shader(frag_shader, GL_FRAGMENT_SHADER)))
return (mlx_error(MLX_FRAGFAIL));;
if (!(mlxctx->shaderprogram = glCreateProgram()))
{
glDeleteShader(fshader);
glDeleteShader(vshader);
return (mlx_error(MLX_SHDRFAIL));
}
glAttachShader(mlxctx->shaderprogram, vshader);
glAttachShader(mlxctx->shaderprogram, fshader);
glLinkProgram(mlxctx->shaderprogram);

int32_t success;
glGetProgramiv(mlxctx->shaderprogram, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(mlxctx->shaderprogram, sizeof(infolog), NULL, infolog);
fprintf(stderr, "%s", infolog);
glDeleteProgram(mlxctx->shaderprogram);
glDeleteShader(vshader);
glDeleteShader(fshader);
return (mlx_error(MLX_SHDRFAIL));
}

// Detach shaders after linking but before deleting them
glDetachShader(mlxctx->shaderprogram, vshader);
glDetachShader(mlxctx->shaderprogram, fshader);

// Delete shaders
glDeleteShader(vshader);
glDeleteShader(fshader);
glUseProgram(mlxctx->shaderprogram);
for (size_t i = 0; i < 16; i++)
mlxctx->bound_textures[i] = 0;
return (true);
}

//= Public =//
Expand Down Expand Up @@ -174,25 +177,35 @@ mlx_t* mlx_init(int32_t width, int32_t height, const char* title, bool resize)
return (free(mlx), (void*)mlx_error(MLX_MEMFAIL));

mlx_ctx_t* const mlxctx = mlx->context;
mlx->window = NULL;
mlx->width = width;
mlx->height = height;
mlxctx->initialWidth = width;
mlxctx->initialHeight = height;

#ifdef EMSCRIPTEN
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_DECORATED, GLFW_TRUE);
glfwWindowHint(GLFW_VISIBLE, GLFW_TRUE);
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#else
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_MAXIMIZED, mlx_settings[MLX_MAXIMIZED]);
glfwWindowHint(GLFW_DECORATED, mlx_settings[MLX_DECORATED]);
glfwWindowHint(GLFW_VISIBLE, !mlx_settings[MLX_HEADLESS]);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#endif
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
glfwWindowHint(GLFW_RESIZABLE, resize);
glfwWindowHint(GLFW_RESIZABLE, resize ? GLFW_TRUE : GLFW_FALSE);
if (!(mlx->window = glfwCreateWindow(width, height, title, mlx_settings[MLX_FULLSCREEN] ? glfwGetPrimaryMonitor() : NULL, NULL)))
return (mlx_terminate(mlx), (void*)mlx_error(MLX_WINFAIL));
return (glfwTerminate(), (void*)mlx_error(MLX_WINFAIL));
if (!mlx_init_render(mlx) || !mlx_create_buffers(mlx))
return (mlx_terminate(mlx), NULL);
glfwMakeContextCurrent(mlx->window);
return (mlx);
}

Expand Down
6 changes: 5 additions & 1 deletion src/mlx_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,14 @@ void mlx_loop(mlx_t* mlx)
MLX_NONNULL(mlx);

double start, oldstart = 0;
#ifndef EMSCRIPTEN
while (!glfwWindowShouldClose(mlx->window))
{
#endif
start = glfwGetTime();
mlx->delta_time = start - oldstart;
oldstart = start;

glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glfwGetWindowSize(mlx->window, &(mlx->width), &(mlx->height));
Expand All @@ -114,5 +116,7 @@ void mlx_loop(mlx_t* mlx)

glfwSwapBuffers(mlx->window);
glfwPollEvents();
#ifndef EMSCRIPTEN
}
#endif
}
9 changes: 8 additions & 1 deletion tools/compile_shader.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
# See README in the root project for more information.
# -----------------------------------------------------------------------------

input_file=$1
output_file=$(basename "$input_file" .frag).c
echo "// Generated shader source from $input_file" > $output_file
echo "const char *shader_source = R\"GLSL(" >> $output_file
cat $input_file >> $output_file
echo ")GLSL\";" >> $output_file

# If no arguments have been given, exit with error code 1
if [ "$#" -ne 1 ]; then
echo "ERROR: missing arguments, use as follows: $0 <ShaderFile>" 1>&2
Expand All @@ -20,7 +27,7 @@ SHADERTYPE="${1##*.}"

echo "// -----------------------------------------------------------------------------"
echo "// Codam Coding College, Amsterdam @ 2022-2023 by W2Wizard. "
echo "// See README in the root project for more information. "
echo "// See README in the root project for more information. "
echo "// -----------------------------------------------------------------------------"
echo ""
echo "// If you wish to modify this file edit the .vert or .frag file!"
Expand Down

0 comments on commit 7a0d6f8

Please sign in to comment.