From 362bbed6bdd0d1ecde4b3ae587d6de3169893bf9 Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Sat, 27 Jan 2024 23:59:02 +0300 Subject: [PATCH] Add Emscripten example --- README.md | 2 + sdl_emscripten/.gitignore | 7 ++ sdl_emscripten/Makefile | 22 ++++ sdl_emscripten/abc.c | 44 ++++++++ sdl_emscripten/main.c | 205 ++++++++++++++++++++++++++++++++++++++ sdl_emscripten/shell.html | 142 ++++++++++++++++++++++++++ sdl_emscripten/simple.c | 54 ++++++++++ sdl_emscripten/simple2.c | 27 +++++ 8 files changed, 503 insertions(+) create mode 100644 sdl_emscripten/.gitignore create mode 100644 sdl_emscripten/Makefile create mode 100644 sdl_emscripten/abc.c create mode 100644 sdl_emscripten/main.c create mode 100644 sdl_emscripten/shell.html create mode 100644 sdl_emscripten/simple.c create mode 100644 sdl_emscripten/simple2.c diff --git a/README.md b/README.md index ed6d433..d969048 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ # Примеры кода, алгоритмов и библиотек на Си (C++) - [rxi/log](rxi/log) — ... +- [nuklear] — Пример простого окна на GLFW3 и Nuklear (WIP) - [CppLinuxSerial](communication/cpp_linux_serial) — Библиотека для работы с Serial в связке с Arduino +- [sdl_emscripten] — Пример использования emscripten и SDL (WIP) diff --git a/sdl_emscripten/.gitignore b/sdl_emscripten/.gitignore new file mode 100644 index 0000000..c42d81c --- /dev/null +++ b/sdl_emscripten/.gitignore @@ -0,0 +1,7 @@ +game +*.out.* +*.png +html/ +index.html +index.js +index.wasm diff --git a/sdl_emscripten/Makefile b/sdl_emscripten/Makefile new file mode 100644 index 0000000..7352890 --- /dev/null +++ b/sdl_emscripten/Makefile @@ -0,0 +1,22 @@ +PROGRAM = game +OBJ = main.o +CC = gcc +CC_WASM = /home/user/Git/emsdk/upstream/emscripten/emcc +CFLAGS = -O2 -g +LFLAGS = $(shell pkg-config --libs SDL2_image) + +.PHONY: html + +all: $(PROGRAM) + +$(PROGRAM): $(OBJ) + $(CC) $(CFLAGS) -o $@ $(OBJ) $(LFLAGS) + +main.o: + $(CC) $(CFLAGS) -c $(shell pkg-config --cflags sdl2) -o $@ main.c + +html: + $(CC_WASM) -O2 -sUSE_SDL=2 -sUSE_SDL_IMAGE=2 -sSDL2_IMAGE_FORMATS=' ["png"]' -sUSE_SDL_TTF=2 main.c -o html/index.html --shell-file shell.html + +clean: + rm -f $(OBJ) $(PROGRAM) html/* diff --git a/sdl_emscripten/abc.c b/sdl_emscripten/abc.c new file mode 100644 index 0000000..799b891 --- /dev/null +++ b/sdl_emscripten/abc.c @@ -0,0 +1,44 @@ +#include + +int main(int argc, char **argv) { + SDL_Init(SDL_INIT_VIDEO); + + SDL_Window *window = NULL; + window = + SDL_CreateWindow("Jeu de la vie", SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN); + + // Setup renderer + SDL_Renderer *renderer = NULL; + renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); + + // Set render color to red ( background will be rendered in this color ) + SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); + + // Clear winow + SDL_RenderClear(renderer); + + // Creat a rect at pos ( 50, 50 ) that's 50 pixels wide and 50 pixels high. + SDL_Rect r; + r.x = 50; + r.y = 50; + r.w = 50; + r.h = 50; + + // Set render color to blue ( rect will be rendered in this color ) + SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); + + // Render rect + SDL_RenderFillRect(renderer, &r); + + // Render the rect to the screen + SDL_RenderPresent(renderer); + + // Wait for 5 sec + SDL_Delay(5000); + + SDL_DestroyWindow(window); + SDL_Quit(); + + return EXIT_SUCCESS; +} diff --git a/sdl_emscripten/main.c b/sdl_emscripten/main.c new file mode 100644 index 0000000..c81a96b --- /dev/null +++ b/sdl_emscripten/main.c @@ -0,0 +1,205 @@ +#include +#include + +#include +#include + +#ifdef __EMSCRIPTEN__ +#include +#endif + +const int SCREEN_WIDTH = 640; +const int SCREEN_HEIGHT = 480; + +int main(int argc, char **argv) { + if (SDL_Init(SDL_INIT_VIDEO) != 0) { + printf("error initializing SDL: %s\n", SDL_GetError()); + + return 1; + } + + SDL_Window *window = NULL; + window = + SDL_CreateWindow("Hello, SDL 2!", SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, 0); + + if (window == NULL) { + return 1; + } + + SDL_Renderer* renderer = NULL; + renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED); + SDL_SetRenderDrawColor( renderer, 255, 0, 0, 255 ); + SDL_RenderClear( renderer ); + + + + SDL_Rect dest; + dest.w= 50; + dest.h= 50; + dest.x = 10; + dest.y = 10; + + SDL_SetRenderDrawColor( renderer, 0, 0, 255, 255 ); + SDL_RenderFillRect( renderer, &dest ); + + // SDL_Surface *image = IMG_Load("girl_faces.png"); + // if (!image) { + // printf("IMG_Load: %s\n", IMG_GetError()); + // return 0; + // } + + // SDL_Texture *tex = SDL_CreateTextureFromSurface(rend, image); + // SDL_FreeSurface(image); + // SDL_QueryTexture(tex, NULL, NULL, &dest.w, &dest.h); + + // SDL_RenderClear(rend); + // SDL_RenderCopy(rend, tex, NULL, &dest); + SDL_RenderPresent(renderer); + + SDL_Delay(2000); + SDL_DestroyWindow(window); + SDL_Quit(); + + return 0; + + // printf("hello, world!\n"); + + // puts("0"); + + // if (SDL_Init(SDL_INIT_EVERYTHING) != 0) { + // printf("error initializing SDL: %s\n", SDL_GetError()); + + // return 1; + // } + + // SDL_Surface *screen_surface = NULL; + + // SDL_Window *window = NULL; + + // puts("1"); + + // window = + // SDL_CreateWindow("Hello, SDL 2!", SDL_WINDOWPOS_CENTERED, + // SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, + // 0); + + // if (window == NULL) { + // return 1; + // } + + // Uint32 render_flags = SDL_RENDERER_ACCELERATED; + + // // creates a renderer to render our images + // SDL_Renderer* rend = SDL_CreateRenderer(window, -1, render_flags); + + // screen_surface = SDL_GetWindowSurface(window); + + // // creates a surface to load an image into the main memory + // SDL_Surface *surface; + // // please provide a path for your image + // surface = IMG_Load("girl_faces.png"); + // // loads image to our graphics hardware memory. + // SDL_Texture *tex = SDL_CreateTextureFromSurface(rend, surface); + // // clears main-memory + // SDL_FreeSurface(surface); + // // let us control our image position + // // so that we can move it with our keyboard. + // SDL_Rect dest; + // // connects our texture with dest to control position + // SDL_QueryTexture(tex, NULL, NULL, &dest.w, &dest.h); + // // adjust height and width of our image box. + // dest.w /= 6; + // dest.h /= 6; + // // sets initial x-position of object + // dest.x = (1000 - dest.w) / 2; + // // sets initial y-position of object + // dest.y = (1000 - dest.h) / 2; + + // puts("13"); + + // // controls animation loop + // int close = 0; + + // // speed of box + // int speed = 300; + + // // animation loop + // while (!close) { + // SDL_Event event; + + // // Events management + // while (SDL_PollEvent(&event)) { + // switch (event.type) { + + // case SDL_QUIT: + // // handling of close button + // close = 1; + // break; + + // case SDL_KEYDOWN: + // // keyboard API for key pressed + // switch (event.key.keysym.scancode) { + // case SDL_SCANCODE_ESCAPE: + // close = 1; + // break; + // case SDL_SCANCODE_W: + // case SDL_SCANCODE_UP: + // dest.y -= speed / 30; + // break; + // case SDL_SCANCODE_A: + // case SDL_SCANCODE_LEFT: + // dest.x -= speed / 30; + // break; + // case SDL_SCANCODE_S: + // case SDL_SCANCODE_DOWN: + // dest.y += speed / 30; + // break; + // case SDL_SCANCODE_D: + // case SDL_SCANCODE_RIGHT: + // dest.x += speed / 30; + // break; + // default: + // break; + // } + // } + // } + + // // right boundary + // if (dest.x + dest.w > 1000) + // dest.x = 1000 - dest.w; + + // // left boundary + // if (dest.x < 0) + // dest.x = 0; + + // // bottom boundary + // if (dest.y + dest.h > 1000) + // dest.y = 1000 - dest.h; + + // // upper boundary + // if (dest.y < 0) + // dest.y = 0; + + // // clears the screen + // SDL_RenderClear(rend); + // SDL_RenderCopy(rend, tex, NULL, &dest); + + // /// + // /// Section 4: SDL ttf and rendering text + // /// + + // // triggers the double buffers + // // for multiple rendering + // SDL_RenderPresent(rend); + + // // calculates to 60 fps + // SDL_Delay(1000 / 60); + // } + + // SDL_Delay(2000); + + // SDL_DestroyWindow(window); + + // SDL_Quit(); +} diff --git a/sdl_emscripten/shell.html b/sdl_emscripten/shell.html new file mode 100644 index 0000000..83c9e47 --- /dev/null +++ b/sdl_emscripten/shell.html @@ -0,0 +1,142 @@ + + + + + + Emscripten-Generated Code + + + +
emscripten
+
Downloading...
+
+ +
+ +
+ +
+ +
+ Resize canvas + Lock/hide mouse pointer +     + +
+ + + + + {{{ SCRIPT }}} + + diff --git a/sdl_emscripten/simple.c b/sdl_emscripten/simple.c new file mode 100644 index 0000000..c802c3c --- /dev/null +++ b/sdl_emscripten/simple.c @@ -0,0 +1,54 @@ +#include + +#include + +const int SCREEN_WIDTH = 640; +const int SCREEN_HEIGHT = 480; + +int main(int argc, char **args) { + + if (SDL_Init(SDL_INIT_EVERYTHING) != 0) { + return 1; + } + + SDL_Surface *screen_surface = NULL; + + SDL_Window *window = NULL; + + window = SDL_CreateWindow("Hello, SDL 2!", SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, + SCREEN_HEIGHT, SDL_WINDOW_SHOWN); + + if (window == NULL) { + return 1; + } + + screen_surface = SDL_GetWindowSurface(window); + + SDL_FillRect(screen_surface, NULL, + SDL_MapRGB(screen_surface->format, 0, 255, 0)); + + SDL_UpdateWindowSurface(window); + + SDL_Event e; + bool quit = false; + while (!quit) { + while (SDL_PollEvent(&e)) { + if (e.type == SDL_QUIT) { + quit = true; + } + if (e.type == SDL_KEYDOWN) { + quit = true; + } + if (e.type == SDL_MOUSEBUTTONDOWN) { + quit = true; + } + } + } + + SDL_DestroyWindow(window); + + SDL_Quit(); + + return 0; +}; diff --git a/sdl_emscripten/simple2.c b/sdl_emscripten/simple2.c new file mode 100644 index 0000000..479a277 --- /dev/null +++ b/sdl_emscripten/simple2.c @@ -0,0 +1,27 @@ +#include + +int main() { + SDL_Init(SDL_INIT_VIDEO); + + SDL_Window *window; + SDL_Renderer *renderer; + SDL_CreateWindowAndRenderer(300, 300, 0, &window, &renderer); + + SDL_SetRenderDrawColor(renderer, /* RGBA: green */ 0x00, 0x80, 0x00, 0xFF); + SDL_Rect rect = {.x = 10, .y = 10, .w = 150, .h = 100}; + SDL_RenderFillRect(renderer, &rect); + SDL_RenderPresent(renderer); + + while (1) { + SDL_Event event; + SDL_PollEvent(&event); + if (event.type == SDL_QUIT) { + break; + } + } + + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + + SDL_Quit(); +}