/* 生命游戏,网页版,基于SDL和WebAssembly created by VisualGMQ this game used SDL2 library to build, if you want to build this source code, please input below code in Bash: em++ life_game.cpp -s WASM=1 -s USE_SDL=2 -o index.html then use a server to run `index.html`, for example: emrun --port 8080 . */ #include #include #include #include #include "SDL.h" #include "emscripten.h" using namespace std; using Map = vector>; const int Width = 600; const int Height = 600; int DelayTime = 100; int cell_num = 40; int cell_w = Width/cell_num; int cell_h = Height/cell_num; bool isstep = false; int mx, my; int button = 0; bool isquit = false; Map map; void drawCell(SDL_Renderer* render, int x, int y, int r=255, int g=255, int b=255){ SDL_SetRenderDrawColor(render, r, g, b, 255); SDL_Rect rect; rect.x = x;rect.y = y;rect.w = cell_w;rect.h = cell_h; SDL_RenderDrawRect(render, &rect); } void parserArgv(int argc, char** argv){ stringstream ss; if(argc > 1){ ss<>cell_num; } ss.clear(); if(argc > 2){ ss<>DelayTime; } cell_w = Width/cell_num; cell_h = Height/cell_num; } Map initMap(){ Map map; for(int i=0;i inner(cell_num, false); map.push_back(inner); } return map; } void step(Map& map){ Map tmp; tmp.assign(map.begin(), map.end()); for(int col=0;col=0 && col+i=0 && row+j(arg); SDL_SetRenderDrawColor(render, 0, 0, 0, 255); SDL_RenderClear(render); while(SDL_PollEvent(&event)){ if(event.type == SDL_QUIT) isquit = true; if(event.type == SDL_MOUSEMOTION){ mx = event.button.x; my = event.button.y; } if(!isstep){ if(event.type == SDL_MOUSEBUTTONDOWN){ if(event.button.button == SDL_BUTTON_LEFT) button = 1; if(event.button.button == SDL_BUTTON_RIGHT) button = -1; } if(event.type == SDL_MOUSEBUTTONUP) button = 0; } if(event.type == SDL_KEYDOWN){ if(event.key.keysym.sym == SDLK_SPACE) isstep = !isstep; if(event.key.keysym.sym == SDLK_q || event.key.keysym.sym == SDLK_ESCAPE) isquit = true; if(event.key.keysym.sym == SDLK_c) for(int i=0;i