emscripten~

This commit is contained in:
shweet 2024-09-26 05:30:45 -04:00
parent 664e33cb79
commit 93d0ff1406
15 changed files with 69 additions and 21 deletions

View File

@ -35,14 +35,14 @@ file(GLOB src
add_executable(${PROJECT_NAME} ${src})
set (CMAKE_C_FLAGS "-O2 -Wall -Wextra -pedantic -Wno-unused-variable -Wno-unused-parameter -Wno-discarded-qualifiers -Wno-parantheses")
set (CMAKE_C_FLAGS "-O2 -Wall -Wextra -pedantic -Wno-unused-variable -Wno-unused-parameter -Wno-parentheses")
if (EMSCRIPTEN)
set(CMAKE_EXECUTABLE_SUFFIX ".html")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -sMIN_WEBGL_VERSION=2 -sMAX_WEBGL_VERSION=2 -sFULL_ES2 -sFULL_ES3 -sUSE_SDL=2 -sUSE_SDL_IMAGE=2 -sSDL2_IMAGE_FORMATS='['png']' -sUSE_SDL_MIXER=2 -sSDL2_MIXER_FORMATS='['ogg']' -sUSE_SDL_TTF=2")
add_link_options(" --preload-file res --use-preload-plugins sNO_DYNAMIC_EXECUTION=1 -sALLOW_MEMORY_GROWTH=1 -sASSERTIONS=1 -sDISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR=0")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -s USE_SDL=2 -s USE_SDL_IMAGE=2 -s SDL2_IMAGE_FORMATS='['png']' -s USE_SDL_MIXER=2 -s SDL2_MIXER_FORMATS='['ogg']' -s USE_SDL_TTF=2 -s ASSERTIONS=1 -s ALLOW_MEMORY_GROWTH=1 -Wno-strict-prototypes -Wno-incompatible-pointer-types-discards-qualifiers -Wno-unused-command-line-argument -Wno-ignored-qualifiers")
target_link_options(${PROJECT_NAME} PRIVATE --preload-file res -s USE_WEBGL2=1 )
elseif (WIN32)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mwindows")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mwindows -Wno-discarded-qualifers")
target_link_libraries(${PROJECT_NAME} mingw32 m SDL2main SDL2 SDL2_image SDL2_mixer SDL2_ttf opengl32 glew32)
elseif (UNIX)
target_link_libraries(${PROJECT_NAME} m SDL2 SDL2_image SDL2_mixer SDL2_ttf GL GLEW)

View File

@ -1,7 +1,11 @@
#pragma once
#ifdef EMSCRIPTEN
#include <GLES3/gl32.h>
#else
#include <GL/glew.h>
#include <GL/gl.h>
#endif
#include "window.h"

View File

@ -1,7 +1,11 @@
#pragma once
#ifdef EMSCRIPTEN
#include <GLES3/gl32.h>
#else
#include <GL/glew.h>
#include <GL/gl.h>
#endif
#include "../COMMON.h"

View File

@ -57,9 +57,21 @@ renderer_init(Renderer* self, Window* window, CameraType type, const ivec2* buff
self->window = window;
#ifdef EMSCRIPTEN
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
self->glContext = SDL_GL_CreateContext(self->window->sdl);
#else
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
self->glContext = SDL_GL_CreateContext(self->window->sdl);
glew_init();
#endif
vao_init(&self->vao);
vbo_init(&self->vbo, GL_ARRAY_BUFFER, true);
@ -145,7 +157,9 @@ renderer_buffer_use(Renderer* self, RendererBuffer buffer)
void
renderer_buffer_clear(Renderer* self, RendererBuffer buffer)
{
texture_clear(&self->fboTextures[buffer]);
fbo_bind(&self->fbos[buffer]);
glClearBufferfv(GL_COLOR, 0, TRANSPARENT);
fbo_unbind();
}
void

View File

@ -1,7 +1,6 @@
#pragma once
#include "camera.h"
#include "glew.h"
#include "window.h"
#include "shader.h"
#include "vao.h"
@ -10,6 +9,10 @@
#include "vbo.h"
#include "vertexattribute.h"
#ifndef EMSCRIPTEN
#include "glew.h"
#endif
#define RENDERER_BUFFER_COUNT RENDERER_BUFFER_CURSOR + 1
typedef enum RendererBuffer
{

View File

@ -47,14 +47,6 @@ sdl_init(void)
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
/*
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
*/
}
/* Quits SDL. */

View File

@ -86,7 +86,11 @@ texture_free(Texture* self)
void
texture_clear(Texture* self)
{
vec4 color = {1.0f, 0.0f, 0.0f, 1.0f};
texture_bind(self);
glClearTexImage(self->handle, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, self->size[0], self->size[1], GL_RGBA, GL_FLOAT, color);
texture_unbind();
}

View File

@ -1,7 +1,13 @@
#pragma once
#ifdef EMSCRIPTEN
#include <GLES3/gl32.h>
#include <GL/gl.h>
#else
#include <GL/glew.h>
#include <GL/gl.h>
#endif
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>

View File

@ -1,7 +1,11 @@
#pragma once
#ifdef EMSCRIPTEN
#include <GLES3/gl32.h>
#else
#include <GL/glew.h>
#include <GL/gl.h>
#endif
#include "../COMMON.h"

View File

@ -1,7 +1,11 @@
#pragma once
#ifdef EMSCRIPTEN
#include <GLES3/gl32.h>
#else
#include <GL/glew.h>
#include <GL/gl.h>
#endif
#include "../COMMON.h"

View File

@ -12,9 +12,6 @@ vertex_attribute_set(GLuint index, GLint size, GLenum type, GLsizei stride, size
case GL_INT:
case GL_UNSIGNED_INT:
case GL_INT_2_10_10_10_REV:
case GL_UNSIGNED_INT_2_10_10_10_REV:
glVertexAttribIPointer(index, size, type, stride, (void*)offset);
break;
default:
glVertexAttribPointer(index, size, type, GL_FALSE, stride, (void*)offset);
break;

View File

@ -1,7 +1,11 @@
#pragma once
#ifdef EMSCRIPTEN
#include <GLES3/gl32.h>
#else
#include <GL/glew.h>
#include <GL/gl.h>
#endif
#include "../COMMON.h"

View File

@ -67,9 +67,9 @@ _game_draw(Game* self)
renderer_clear_color_set(&self->renderer, (f32*)RENDERER_CLEAR_COLOR);
renderer_clear(&self->renderer);
for (s32 i = 0; i < RENDERER_BUFFER_COUNT; i++)
renderer_buffer_clear(&self->renderer, (RendererBuffer)i);
renderer_buffer_init(&self->renderer, (RendererBuffer)i);
ecs_draw(&self->ecs);
@ -85,6 +85,9 @@ _game_draw(Game* self)
}
renderer_present(&self->renderer);
for (s32 i = 0; i < RENDERER_BUFFER_COUNT; i++)
renderer_buffer_free(&self->renderer, (RendererBuffer)i);
}
void
game_init(Game* self)

View File

@ -21,10 +21,19 @@ typedef struct ShaderPaths
static const ShaderPaths SHADER_PATHS[SHADER_COUNT] =
{
#ifdef EMSCRIPTEN
{
.vertex = "res/shader/texture_quad_es.vs",
.fragment = "res/shader/texture_quad_es.fs"
}
#else
{
.vertex = "res/shader/texture_quad.vs",
.fragment = "res/shader/texture_quad.fs"
}
#endif
};
#define TEXTURE_COUNT TEXTURE_ENDING_THREE + 1

View File

@ -12,7 +12,7 @@ main(s32 argc, char** argv)
game_init(&game);
#ifdef EMSCRIPTEN
emscripten_set_main_loop(loop, 60, true);
emscripten_set_main_loop(loop, -1, true);
#else
while (true)
loop();