SDL compiling for Emscripten

This commit is contained in:
Vladislav Khorev 2023-12-03 21:36:04 +03:00
parent 3bfac34979
commit cb9f6c6932
10 changed files with 74 additions and 52 deletions

View File

@ -86,7 +86,7 @@ namespace ZL {
if (depthRange <= 0) if (depthRange <= 0)
{ {
throw std::exception("zFar must be greater than zNear"); throw std::runtime_error("zFar must be greater than zNear");
} }
Matrix4f r; Matrix4f r;

3
Math.h
View File

@ -1,6 +1,9 @@
#pragma once #pragma once
#include <array> #include <array>
#include <exception>
#include <stdexcept>
namespace ZL { namespace ZL {

View File

@ -2,6 +2,8 @@
#include "Utils.h" #include "Utils.h"
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
//==================================================== //====================================================
//===================== GLSL Shaders ================= //===================== GLSL Shaders =================
//==================================================== //====================================================
@ -112,10 +114,13 @@ PFNGLGENVERTEXARRAYSPROC glGenVertexArrays = NULL;
PFNGLBINDVERTEXARRAYPROC glBindVertexArray = NULL; PFNGLBINDVERTEXARRAYPROC glBindVertexArray = NULL;
PFNGLDELETEVERTEXARRAYSPROC glDeleteVertexArray = NULL; PFNGLDELETEVERTEXARRAYSPROC glDeleteVertexArray = NULL;
#endif
namespace ZL { namespace ZL {
bool BindOpenGlFunctions() bool BindOpenGlFunctions()
{ {
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
//char* extensionList = (char*)glGetString(GL_EXTENSIONS); //char* extensionList = (char*)glGetString(GL_EXTENSIONS);
char* glVersion = (char*)glGetString(GL_VERSION); char* glVersion = (char*)glGetString(GL_VERSION);
bool ok = true; bool ok = true;
@ -231,8 +236,7 @@ namespace ZL {
ok = false; ok = false;
} }
//if (findString("GL_ARB_framebuffer_object", extensionList))
{
glIsRenderbuffer = (PFNGLISRENDERBUFFERPROC)wglGetProcAddress("glIsRenderbuffer"); glIsRenderbuffer = (PFNGLISRENDERBUFFERPROC)wglGetProcAddress("glIsRenderbuffer");
glBindRenderbuffer = (PFNGLBINDRENDERBUFFERPROC)wglGetProcAddress("glBindRenderbuffer"); glBindRenderbuffer = (PFNGLBINDRENDERBUFFERPROC)wglGetProcAddress("glBindRenderbuffer");
glDeleteRenderbuffers = (PFNGLDELETERENDERBUFFERSPROC)wglGetProcAddress("glDeleteRenderbuffers"); glDeleteRenderbuffers = (PFNGLDELETERENDERBUFFERSPROC)wglGetProcAddress("glDeleteRenderbuffers");
@ -279,15 +283,6 @@ namespace ZL {
} }
}
/*else
{
ok = false;
}*/
//if (findString("GL_ARB_uniform_buffer_object", extensionList))
{
glGetUniformIndices = (PFNGLGETUNIFORMINDICESPROC)wglGetProcAddress("glGetUniformIndices"); glGetUniformIndices = (PFNGLGETUNIFORMINDICESPROC)wglGetProcAddress("glGetUniformIndices");
glGetActiveUniformsiv = (PFNGLGETACTIVEUNIFORMSIVPROC)wglGetProcAddress("glGetActiveUniformsiv"); glGetActiveUniformsiv = (PFNGLGETACTIVEUNIFORMSIVPROC)wglGetProcAddress("glGetActiveUniformsiv");
glGetActiveUniformName = (PFNGLGETACTIVEUNIFORMNAMEPROC)wglGetProcAddress("glGetActiveUniformName"); glGetActiveUniformName = (PFNGLGETACTIVEUNIFORMNAMEPROC)wglGetProcAddress("glGetActiveUniformName");
@ -308,11 +303,7 @@ namespace ZL {
{ {
ok = false; ok = false;
} }
}
/*else
{
ok = false;
}*/
glGenVertexArrays = (PFNGLGENVERTEXARRAYSPROC)wglGetProcAddress("glGenVertexArrays"); glGenVertexArrays = (PFNGLGENVERTEXARRAYSPROC)wglGetProcAddress("glGenVertexArrays");
glBindVertexArray = (PFNGLBINDVERTEXARRAYPROC)wglGetProcAddress("glBindVertexArray"); glBindVertexArray = (PFNGLBINDVERTEXARRAYPROC)wglGetProcAddress("glBindVertexArray");
@ -327,6 +318,9 @@ namespace ZL {
return ok; return ok;
#else
return true;
#endif
} }
void CheckGlError() void CheckGlError()
@ -335,7 +329,7 @@ namespace ZL {
if (error != GL_NO_ERROR) if (error != GL_NO_ERROR)
{ {
throw std::exception("Gl error"); throw std::runtime_error("Gl error");
} }
} }
} }

View File

@ -1,5 +1,20 @@
#pragma once #pragma once
#include "SDL.h"
#ifdef EMSCRIPTEN
//#define GL_GLEXT_PROTOTYPES 1
//#define EGL_EGLEXT_PROTOTYPES 1
//#include <SDL2/SDL_opengl.h>
#include <GLES3/gl3.h>
#include "emscripten.h"
#endif
#include <exception>
#include <stdexcept>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
#include "windows.h" #include "windows.h"
#define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp)) #define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
@ -126,9 +141,13 @@ extern PFNGLBINDBUFFERBASEPROC glBindBufferBase;
extern PFNGLGENVERTEXARRAYSPROC glGenVertexArrays; extern PFNGLGENVERTEXARRAYSPROC glGenVertexArrays;
extern PFNGLBINDVERTEXARRAYPROC glBindVertexArray; extern PFNGLBINDVERTEXARRAYPROC glBindVertexArray;
extern PFNGLDELETEVERTEXARRAYSPROC glDeleteVertexArray; extern PFNGLDELETEVERTEXARRAYSPROC glDeleteVertexArray;
#else
#endif
namespace ZL { namespace ZL {
bool BindOpenGlFunctions(); bool BindOpenGlFunctions();
void CheckGlError(); void CheckGlError();

View File

@ -21,13 +21,16 @@ namespace ZL {
VAOHolder::VAOHolder() VAOHolder::VAOHolder()
{ {
#ifndef EMSCRIPTEN
glGenVertexArrays(1, &vao); glGenVertexArrays(1, &vao);
#endif
} }
VAOHolder::~VAOHolder() VAOHolder::~VAOHolder()
{ {
#ifndef EMSCRIPTEN
glDeleteVertexArray(1, &vao); glDeleteVertexArray(1, &vao);
//glDeleteBuffers(1, &Buffer); #endif
} }
GLuint VAOHolder::getBuffer() GLuint VAOHolder::getBuffer()
@ -122,13 +125,14 @@ namespace ZL {
{ {
//Check if main thread, check if data is not empty... //Check if main thread, check if data is not empty...
#ifndef EMSCRIPTEN
if (!vao) if (!vao)
{ {
vao = std::make_shared<VAOHolder>(); vao = std::make_shared<VAOHolder>();
} }
glBindVertexArray(vao->getBuffer()); glBindVertexArray(vao->getBuffer());
#endif
if (!positionVBO) if (!positionVBO)
{ {
positionVBO = std::make_shared<VBOHolder>(); positionVBO = std::make_shared<VBOHolder>();
@ -159,8 +163,9 @@ namespace ZL {
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
#ifndef EMSCRIPTEN
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
#endif
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDepthFunc(GL_LEQUAL); glDepthFunc(GL_LEQUAL);
@ -176,7 +181,7 @@ namespace ZL {
if (ProjectionMatrixStack.size() > CONST_MATRIX_STACK_SIZE) if (ProjectionMatrixStack.size() > CONST_MATRIX_STACK_SIZE)
{ {
throw std::exception("Projection matrix stack overflow!!!!"); throw std::runtime_error("Projection matrix stack overflow!!!!");
} }
} }
@ -185,7 +190,7 @@ namespace ZL {
{ {
if (ProjectionMatrixStack.size() == 0) if (ProjectionMatrixStack.size() == 0)
{ {
throw std::exception("Projection matrix stack underflow!!!!"); throw std::runtime_error("Projection matrix stack underflow!!!!");
} }
ProjectionMatrixStack.pop(); ProjectionMatrixStack.pop();
SetMatrix(); SetMatrix();
@ -196,12 +201,12 @@ namespace ZL {
{ {
if (ProjectionMatrixStack.size() <= 0) if (ProjectionMatrixStack.size() <= 0)
{ {
throw std::exception("Projection matrix stack out!"); throw std::runtime_error("Projection matrix stack out!");
} }
if (ModelviewMatrixStack.size() <= 0) if (ModelviewMatrixStack.size() <= 0)
{ {
throw std::exception("Modelview matrix stack out!"); throw std::runtime_error("Modelview matrix stack out!");
} }
ProjectionModelViewMatrix = ProjectionMatrixStack.top() * ModelviewMatrixStack.top(); ProjectionModelViewMatrix = ProjectionMatrixStack.top() * ModelviewMatrixStack.top();
@ -220,14 +225,14 @@ namespace ZL {
{ {
if (ModelviewMatrixStack.size() == 0) if (ModelviewMatrixStack.size() == 0)
{ {
throw std::exception("Modelview matrix stack underflow!!!!"); throw std::runtime_error("Modelview matrix stack underflow!!!!");
} }
ModelviewMatrixStack.push(ModelviewMatrixStack.top()); ModelviewMatrixStack.push(ModelviewMatrixStack.top());
if (ModelviewMatrixStack.size() > CONST_MATRIX_STACK_SIZE) if (ModelviewMatrixStack.size() > CONST_MATRIX_STACK_SIZE)
{ {
throw std::exception("Modelview matrix stack overflow!!!!"); throw std::runtime_error("Modelview matrix stack overflow!!!!");
} }
} }
@ -235,7 +240,7 @@ namespace ZL {
{ {
if (ModelviewMatrixStack.size() == 0) if (ModelviewMatrixStack.size() == 0)
{ {
throw std::exception("Modelview matrix stack underflow!!!!"); throw std::runtime_error("Modelview matrix stack underflow!!!!");
} }
ModelviewMatrixStack.pop(); ModelviewMatrixStack.pop();
@ -256,7 +261,7 @@ namespace ZL {
if (ModelviewMatrixStack.size() == 0) if (ModelviewMatrixStack.size() == 0)
{ {
throw std::exception("Modelview matrix stack underflow!!!!"); throw std::runtime_error("Modelview matrix stack underflow!!!!");
} }
ModelviewMatrixStack.pop(); ModelviewMatrixStack.pop();
@ -276,7 +281,7 @@ namespace ZL {
if (ModelviewMatrixStack.size() == 0) if (ModelviewMatrixStack.size() == 0)
{ {
throw std::exception("Modelview matrix stack underflow!!!!"); throw std::runtime_error("Modelview matrix stack underflow!!!!");
} }
ModelviewMatrixStack.pop(); ModelviewMatrixStack.pop();
@ -296,7 +301,7 @@ namespace ZL {
if (ModelviewMatrixStack.size() == 0) if (ModelviewMatrixStack.size() == 0)
{ {
throw std::exception("Modelview matrix stack underflow!!!!"); throw std::runtime_error("Modelview matrix stack underflow!!!!");
} }
ModelviewMatrixStack.pop(); ModelviewMatrixStack.pop();
@ -326,7 +331,7 @@ namespace ZL {
if (ModelviewMatrixStack.size() == 0) if (ModelviewMatrixStack.size() == 0)
{ {
throw std::exception("Modelview matrix stack underflow!!!!"); throw std::runtime_error("Modelview matrix stack underflow!!!!");
} }
ModelviewMatrixStack.pop(); ModelviewMatrixStack.pop();
@ -340,7 +345,7 @@ namespace ZL {
{ {
if (ModelviewMatrixStack.size() > 64) if (ModelviewMatrixStack.size() > 64)
{ {
throw std::exception("Modelview matrix stack overflow!!!!"); throw std::runtime_error("Modelview matrix stack overflow!!!!");
} }
ModelviewMatrixStack.push(m); ModelviewMatrixStack.push(m);
SetMatrix(); SetMatrix();
@ -351,7 +356,7 @@ namespace ZL {
{ {
if (ModelviewMatrixStack.size() == 0) if (ModelviewMatrixStack.size() == 0)
{ {
throw std::exception("Modelview matrix stack underflow!!!!"); throw std::runtime_error("Modelview matrix stack underflow!!!!");
} }
ModelviewMatrixStack.pop(); ModelviewMatrixStack.pop();

View File

@ -2,7 +2,8 @@
#include "OpenGlExtensions.h" #include "OpenGlExtensions.h"
#include "Math.h" #include "Math.h"
#include <exception>
#include <stdexcept>
#include "ShaderManager.h" #include "ShaderManager.h"
namespace ZL { namespace ZL {

View File

@ -40,12 +40,12 @@ namespace ZL {
if (!vertexShaderCompiled) if (!vertexShaderCompiled)
{ {
throw std::exception("Failed to compile vertex shader code!"); throw std::runtime_error("Failed to compile vertex shader code!");
} }
if (!fragmentShaderCompiled) if (!fragmentShaderCompiled)
{ {
throw std::exception("Failed to compile fragment shader code!"); throw std::runtime_error("Failed to compile fragment shader code!");
} }
shaderProgram = glCreateProgram(); shaderProgram = glCreateProgram();
@ -64,7 +64,7 @@ namespace ZL {
if (!programLinked) if (!programLinked)
{ {
shaderProgram = 0; shaderProgram = 0;
throw std::exception("Failed to link shader program!"); throw std::runtime_error("Failed to link shader program!");
} }
@ -133,12 +133,12 @@ namespace ZL {
{ {
if (shaderStack.size() >= CONST_MAX_SHADER_STACK_SIZE) if (shaderStack.size() >= CONST_MAX_SHADER_STACK_SIZE)
{ {
throw std::exception("Shader stack overflow!"); throw std::runtime_error("Shader stack overflow!");
} }
if (shaderResourceMap.find(shaderName) == shaderResourceMap.end()) if (shaderResourceMap.find(shaderName) == shaderResourceMap.end())
{ {
throw std::exception("Shader does not exist!"); throw std::runtime_error("Shader does not exist!");
} }
shaderStack.push(shaderName); shaderStack.push(shaderName);
@ -151,7 +151,7 @@ namespace ZL {
{ {
if (shaderStack.size() == 0) if (shaderStack.size() == 0)
{ {
throw std::exception("Shader stack underflow!"); throw std::runtime_error("Shader stack underflow!");
} }
shaderStack.pop(); shaderStack.pop();
@ -170,7 +170,7 @@ namespace ZL {
{ {
if (shaderStack.size() == 0) if (shaderStack.size() == 0)
{ {
throw std::exception("Shader stack underflow!"); throw std::runtime_error("Shader stack underflow!");
} }

View File

@ -13,7 +13,7 @@ namespace ZL
if (texID == 0) if (texID == 0)
{ {
throw std::exception("glGenTextures did not work"); throw std::runtime_error("glGenTextures did not work");
} }
glBindTexture(GL_TEXTURE_2D, texID); glBindTexture(GL_TEXTURE_2D, texID);
@ -77,7 +77,7 @@ namespace ZL
if (fileSize < 22) if (fileSize < 22)
{ {
throw std::exception("File is too short or not correct!"); throw std::runtime_error("File is too short or not correct!");
} }
//This refers to BITMAPV5HEADER //This refers to BITMAPV5HEADER
@ -99,7 +99,7 @@ namespace ZL
if (pos + 3 > fileSize) if (pos + 3 > fileSize)
{ {
throw std::exception("File is too short!"); throw std::runtime_error("File is too short!");
} }
@ -125,7 +125,7 @@ namespace ZL
if (fileSize < 22) if (fileSize < 22)
{ {
throw std::exception("File is too short or not correct!"); throw std::runtime_error("File is too short or not correct!");
} }
//This refers to BITMAPV5HEADER //This refers to BITMAPV5HEADER
@ -147,7 +147,7 @@ namespace ZL
if (pos + 4 > fileSize) if (pos + 4 > fileSize)
{ {
throw std::exception("File is too short!"); throw std::runtime_error("File is too short!");
} }

View File

@ -2,6 +2,9 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <exception>
#include <map>
#include <stack>
namespace ZL namespace ZL
{ {

View File

@ -5,9 +5,6 @@
#include "Physics.h" #include "Physics.h"
#include <string> #include <string>
//#include <windows.h>
#include "SDL.h"
//#include "SDL_image.h"
#include <cstdlib> #include <cstdlib>
#include <ctime> #include <ctime>
@ -290,7 +287,7 @@ int main(int argc, char* argv[])
{ {
#ifdef EMSCRIPTEN #ifdef EMSCRIPTEN
SDL_Renderer* renderer = NULL; SDL_Renderer* renderer = NULL;
SDL_CreateWindowAndRenderer(512, 512, SDL_WINDOW_OPENGL, &window, &renderer); SDL_CreateWindowAndRenderer(512, 512, SDL_WINDOW_OPENGL, &ZL::window, &renderer);
#else #else
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) != 0) { if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) != 0) {
SDL_Log("Failed to initialize SDL: %s", SDL_GetError()); SDL_Log("Failed to initialize SDL: %s", SDL_GetError());
@ -309,7 +306,7 @@ int main(int argc, char* argv[])
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
ZL::window = SDL_CreateWindow("title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, CONST_WIDTH, CONST_HEIGHT, SDL_WINDOW_OPENGL); ZL::window = SDL_CreateWindow("Jumping Bird", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, CONST_WIDTH, CONST_HEIGHT, SDL_WINDOW_OPENGL);
//todo //todo
ZL::Env::windowHeaderHeight = 0; ZL::Env::windowHeaderHeight = 0;