Merge pull request #534 from DeXP/gles2

OpenGL ES2.0: Emscripten support
This commit is contained in:
Micha Mettke 2017-10-17 12:23:56 +02:00 committed by GitHub
commit 056af25e27
2 changed files with 100 additions and 96 deletions

View File

@ -9,12 +9,17 @@ OBJ = $(SRC:.c=.o)
UNAME_S := $(shell uname -s) UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Darwin) ifeq ($(UNAME_S),Darwin)
LIBS = -lSDL2 -framework OpenGLES -lm -lGLEW LIBS = -lSDL2 -framework OpenGLES -lm
else else
LIBS = -lSDL2 -lGLESv2 -lm -lGLU -lGLEW LIBS = -lSDL2 -lGLESv2 -lm
endif endif
$(BIN): $(BIN): prepare
$(CC) $(SRC) $(CFLAGS) -o bin/$(BIN) $(LIBS)
web: prepare
emcc $(SRC) -Os -s USE_SDL=2 -o bin/index.html
prepare:
@mkdir -p bin @mkdir -p bin
rm -f bin/$(BIN) $(OBJS) rm -f bin/$(BIN) $(OBJS)
$(CC) $(SRC) $(CFLAGS) -o bin/$(BIN) $(LIBS)

View File

@ -10,10 +10,6 @@
#include <limits.h> #include <limits.h>
#include <time.h> #include <time.h>
#include <GL/glew.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengles2.h>
#define NK_INCLUDE_FIXED_TYPES #define NK_INCLUDE_FIXED_TYPES
#define NK_INCLUDE_STANDARD_IO #define NK_INCLUDE_STANDARD_IO
#define NK_INCLUDE_STANDARD_VARARGS #define NK_INCLUDE_STANDARD_VARARGS
@ -55,70 +51,23 @@
* DEMO * DEMO
* *
* ===============================================================*/ * ===============================================================*/
int
main(int argc, char* argv[])
{
/* Platform */
SDL_Window *win;
SDL_GLContext glContext;
struct nk_color background;
int win_width, win_height;
int running = 1;
/* GUI */
struct nk_context *ctx;
/* SDL setup */ /* Platform */
SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, "0"); SDL_Window *win;
/*SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_EVENTS); // - do NOT init SDL on GL ES 2 */ int running = nk_true;
SDL_GL_SetAttribute (SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
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_DOUBLEBUFFER, 1);
win = SDL_CreateWindow("Demo",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN|SDL_WINDOW_ALLOW_HIGHDPI);
glContext = SDL_GL_CreateContext(win);
SDL_GetWindowSize(win, &win_width, &win_height);
/* OpenGL setup */
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
glewExperimental = 1;
if (glewInit() != GLEW_OK) {
fprintf(stderr, "Failed to setup GLEW\n");
exit(1);
}
ctx = nk_sdl_init(win);
/* Load Fonts: if none of these are loaded a default font will be used */
/* Load Cursor: if you uncomment cursor loading please hide the cursor */
{struct nk_font_atlas *atlas;
nk_sdl_font_stash_begin(&atlas);
/*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/
/*struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 16, 0);*/
/*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/
/*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/
/*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/
/*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/
nk_sdl_font_stash_end();
/*nk_style_load_all_cursors(ctx, atlas->cursors);*/
/*nk_style_set_font(ctx, &roboto->handle)*/;}
/* style.c */ void
/*set_style(ctx, THEME_WHITE);*/ MainLoop(void* loopArg){
/*set_style(ctx, THEME_RED);*/ struct nk_context *ctx = (struct nk_context *)loopArg;
/*set_style(ctx, THEME_BLUE);*/
/*set_style(ctx, THEME_DARK);*/
background = nk_rgb(28,48,62);
while (running)
{
/* Input */ /* Input */
SDL_Event evt; SDL_Event evt;
nk_input_begin(ctx); nk_input_begin(ctx);
while (SDL_PollEvent(&evt)) { while (SDL_PollEvent(&evt)) {
if (evt.type == SDL_QUIT) goto cleanup; if (evt.type == SDL_QUIT) running = nk_false;
nk_sdl_handle_event(&evt); nk_sdl_handle_event(&evt);
} }
nk_input_end(ctx); nk_input_end(ctx);
@ -171,7 +120,8 @@ main(int argc, char* argv[])
/* Draw */ /* Draw */
{float bg[4]; {float bg[4];
nk_color_fv(bg, background); int win_width, win_height;
nk_color_fv(bg, nk_rgb(28,48,62));
SDL_GetWindowSize(win, &win_width, &win_height); SDL_GetWindowSize(win, &win_width, &win_height);
glViewport(0, 0, win_width, win_height); glViewport(0, 0, win_width, win_height);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
@ -183,9 +133,58 @@ main(int argc, char* argv[])
* rendering the UI. */ * rendering the UI. */
nk_sdl_render(NK_ANTI_ALIASING_ON, MAX_VERTEX_MEMORY, MAX_ELEMENT_MEMORY); nk_sdl_render(NK_ANTI_ALIASING_ON, MAX_VERTEX_MEMORY, MAX_ELEMENT_MEMORY);
SDL_GL_SwapWindow(win);} SDL_GL_SwapWindow(win);}
} }
int
main(int argc, char* argv[])
{
/* GUI */
struct nk_context *ctx;
SDL_GLContext glContext;
/* SDL setup */
SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, "0");
/*SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_EVENTS); // - do NOT init SDL on GL ES 2 */
SDL_GL_SetAttribute (SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
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_DOUBLEBUFFER, 1);
win = SDL_CreateWindow("Demo",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN|SDL_WINDOW_ALLOW_HIGHDPI);
glContext = SDL_GL_CreateContext(win);
/* OpenGL setup */
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
ctx = nk_sdl_init(win);
/* Load Fonts: if none of these are loaded a default font will be used */
/* Load Cursor: if you uncomment cursor loading please hide the cursor */
{struct nk_font_atlas *atlas;
nk_sdl_font_stash_begin(&atlas);
/*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../../extra_font/DroidSans.ttf", 14, 0);*/
/*struct nk_font *roboto = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Roboto-Regular.ttf", 16, 0);*/
/*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../../extra_font/kenvector_future_thin.ttf", 13, 0);*/
/*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyClean.ttf", 12, 0);*/
/*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../../extra_font/ProggyTiny.ttf", 10, 0);*/
/*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../../extra_font/Cousine-Regular.ttf", 13, 0);*/
nk_sdl_font_stash_end();
/*nk_style_load_all_cursors(ctx, atlas->cursors);*/
/*nk_style_set_font(ctx, &roboto->handle)*/;}
/* style.c */
/*set_style(ctx, THEME_WHITE);*/
/*set_style(ctx, THEME_RED);*/
/*set_style(ctx, THEME_BLUE);*/
/*set_style(ctx, THEME_DARK);*/
#if defined(__EMSCRIPTEN__)
#include <emscripten.h>
emscripten_set_main_loop_arg(MainLoop, (void*)ctx, 0, nk_true);
#else
while (running) MainLoop((void*)ctx);
#endif
cleanup:
nk_sdl_shutdown(); nk_sdl_shutdown();
SDL_GL_DeleteContext(glContext); SDL_GL_DeleteContext(glContext);
SDL_DestroyWindow(win); SDL_DestroyWindow(win);