diff options
| author | miguel <miguel@debian-acer.softwarefools.com> | 2017-09-21 19:55:07 +0200 |
|---|---|---|
| committer | miguel <miguel@debian-acer.softwarefools.com> | 2017-09-21 19:55:07 +0200 |
| commit | 91a0bc329297a7b3c597e9ae98535f8ccc3af4d7 (patch) | |
| tree | 714979924f3408602fdb598e9fdac9e9480e804f | |
| parent | 7e8a41ebcafe3a1fdaf47abff006af01e211114a (diff) | |
still porting to emscripten
| -rw-r--r-- | Makefile | 3 | ||||
| -rw-r--r-- | main.cpp | 268 |
2 files changed, 151 insertions, 120 deletions
@@ -9,8 +9,9 @@ game: World.o main.o clean: rm -f game rm -f *.o + rm -f *.bc browser-game: emcc --std=c++11 -O2 World.cpp -o World.bc emcc --std=c++11 -O2 main.cpp -o main.bc - emcc -O2 World.bc main.bc -o out.html -s USE_SDL=2 --preload-file coin.bmp --preload-file earth01.bmp --preload-file fonts.bmp --preload-file gridder01.bmp --preload-file guy01.bmp --preload-file guy02.bmp --preload-file gridder02.bmp + emcc -O2 World.bc main.bc -o out.html -s USE_SDL=2 --preload-file coin.bmp --preload-file earth01.bmp --preload-file fonts.bmp --preload-file gridder01.bmp --preload-file guy01.bmp --preload-file guy02.bmp --preload-file gridder02.bmp --preload-file gridder03.bmp @@ -1,7 +1,9 @@ #include <sstream> #include <cmath> -//#include <emscripten.h> +#ifdef __EMSCRIPTEN__ +#include <emscripten.h> +#endif #include <SDL.h> @@ -9,6 +11,42 @@ using SDLInitType=std::pair<int,std::pair<SDL_Window*,SDL_Renderer*>>; +struct Context +{ + // initial window size + int win_width=640; + int win_height=480; + + // some other vars + bool quit = false; + + Uint32 frameTime; + Uint32 lastFrameTime = 0; + Uint32 deltaTime = 0; + Uint32 fpsTime = 0; + Uint32 fpsCount = 0; + Uint32 fps = 0; + + Uint32 mouse_x=0; + Uint32 mouse_y=0; + + SDL_Renderer *ren; + SDLInitType sdl_init; + std::vector<SDL_Texture*> textures; + + int show_tiles_back=6; + int show_tiles_front=10; + int show_tiles_size=20; + int show_tiles_vertical_move=12; + int show_tiles_max_height=800; + int show_tiles_water_level=-1; + + // init world LEVEL 1 + World world=1; + +}; + + // audio from http://www.brainybetty.com/soundsforpowerpoint2.htm // https://freesound.org/people/ProjectsU012/sounds/341695/ void sdl_play_sound() @@ -32,7 +70,9 @@ void sdl_play_sound() // SDL_LoadWAV("music.wav", &wavSpec, &wavBuffer, &wavLength); // SDL_LoadWAV("win.wav", &wavSpec, &wavBuffer, &wavLength); +#ifndef __EMSCRIPTEN__ int success = SDL_QueueAudio(deviceId, wavBuffer, wavLength); +#endif //SDL_CloseAudioDevice(deviceId); //SDL_FreeWAV(wavBuffer); @@ -161,115 +201,60 @@ void sdl_put_str(SDL_Renderer *ren, SDL_Texture *tex,int x, int y,int size,std:: // -int main(int, char**){ - - // initial window size - int win_width=640; - int win_height=480; - - // some other vars - bool quit = false; - SDL_Event event; - - Uint32 frameTime; - Uint32 lastFrameTime = 0; - Uint32 deltaTime = 0; - Uint32 fpsTime = 0; - Uint32 fpsCount = 0; - Uint32 fps = 0; - - Uint32 mouse_x=0; - Uint32 mouse_y=0; - - // init SDL and assign SDL_Renderer on success - SDLInitType sdl_init=sdl_start(win_width,win_height); - if(sdl_init.first!=0) - { - sdl_stop(sdl_init); - return sdl_init.first; - } - - SDL_Renderer *ren=sdl_init.second.second; - - // init Textures - std::vector<SDL_Texture*> textures; - textures.push_back(sdl_load_texture("guy01.bmp",0,0,255,ren)); - textures.push_back(sdl_load_texture("guy02.bmp",0,0,255,ren)); - textures.push_back(sdl_load_texture("coin.bmp",255,255,255,ren)); - textures.push_back(sdl_load_texture("fonts.bmp",0,0,0,ren)); - textures.push_back(sdl_load_texture("earth01.bmp",0,0,0,ren)); - textures.push_back(sdl_load_texture("gridder01.bmp",255,255,255,ren)); - textures.push_back(sdl_load_texture("gridder02.bmp",255,255,255,ren)); - textures.push_back(sdl_load_texture("gridder03.bmp",255,255,255,ren)); - - // init world LEVEL 1 - World world(1); - - - - - int show_tiles_back=6; - int show_tiles_front=10; - int show_tiles_size=20; - int show_tiles_vertical_move=12; - int show_tiles_max_height=800; - int show_tiles_water_level=-1; - - show_tiles_size=win_height/15; - show_tiles_back=win_width/show_tiles_size*0.25; - show_tiles_front=win_width/show_tiles_size*1; +void main_loop(void *arg) +{ + Context *ctx=(Context *)arg; + SDL_Event event; - while (!quit) - { // calc delta and FPS - frameTime = SDL_GetTicks(); - deltaTime = frameTime - lastFrameTime; - lastFrameTime = frameTime; - fpsTime+=deltaTime; - fpsCount++; + ctx->frameTime = SDL_GetTicks(); + ctx->deltaTime = ctx->frameTime - ctx->lastFrameTime; + ctx->lastFrameTime = ctx->frameTime; + ctx->fpsTime+=ctx->deltaTime; + ctx->fpsCount++; - if(fpsTime>1000) + if(ctx->fpsTime>1000) { - fps=fpsCount*1000/fpsTime; - fpsTime=0; - fpsCount=0; + ctx->fps=ctx->fpsCount*1000/ctx->fpsTime; + ctx->fpsTime=0; + ctx->fpsCount=0; } // handle events (mouse & kb & win resize) - while(!quit && SDL_PollEvent(&event)) + while(!ctx->quit && SDL_PollEvent(&event)) { if (event.type == SDL_WINDOWEVENT && (event.window.event==SDL_WINDOWEVENT_RESIZED || event.window.event==SDL_WINDOWEVENT_SIZE_CHANGED)) { // SDL_Log("Window %d size changed to %dx%d", // event.window.windowID, event.window.data1, //event.window.data2); - win_width=event.window.data1; - win_height=event.window.data2; + ctx->win_width=event.window.data1; + ctx->win_height=event.window.data2; - show_tiles_size=win_height/15; - show_tiles_back=win_width/show_tiles_size*0.25; - show_tiles_front=win_width/show_tiles_size*1; + ctx->show_tiles_size=ctx->win_height/15; + ctx->show_tiles_back=ctx->win_width/ctx->show_tiles_size*0.25; + ctx->show_tiles_front=ctx->win_width/ctx->show_tiles_size*1; // while(show_tiles_size%20!=0)show_tiles_size--; } if (event.type==SDL_MOUSEBUTTONDOWN) { - mouse_x=event.button.x; - mouse_y=event.button.y; + ctx->mouse_x=event.button.x; + ctx->mouse_y=event.button.y; - world.mouseclick((mouse_x+show_tiles_size/2-world.player.x2)/show_tiles_size+world.player.x-show_tiles_back,show_tiles_vertical_move-mouse_y/show_tiles_size); + ctx->world.mouseclick((ctx->mouse_x+ctx->show_tiles_size/2-ctx->world.player.x2)/ctx->show_tiles_size+ctx->world.player.x-ctx->show_tiles_back,ctx->show_tiles_vertical_move-ctx->mouse_y/ctx->show_tiles_size); } switch (event.type) { case SDL_QUIT: - quit = true; + ctx->quit = true; break; case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_ESCAPE) - quit = true; + ctx->quit = true; break; default: break; @@ -277,43 +262,43 @@ int main(int, char**){ } // clear screen - SDL_SetRenderDrawColor(ren, 0, 0, 0, 255); - SDL_RenderClear(ren); + SDL_SetRenderDrawColor(ctx->ren, 0, 0, 0, 255); + SDL_RenderClear(ctx->ren); // render visible area - for(int i=world.player.x-show_tiles_back;i<world.player.x+show_tiles_front;i++) + for(int i=ctx->world.player.x-ctx->show_tiles_back;i<ctx->world.player.x+ctx->show_tiles_front;i++) { - int tile_col=i-world.player.x+show_tiles_back; - if(i<0||i>=world.bricks.size())continue; + int tile_col=i-ctx->world.player.x+ctx->show_tiles_back; + if(i<0||i>=ctx->world.bricks.size())continue; - SDL_Rect rect={(int)(tile_col*show_tiles_size-show_tiles_size*world.player.x2),(show_tiles_vertical_move-world.coins_pos[i])*show_tiles_size,show_tiles_size,show_tiles_size}; - SDL_RenderCopy(ren,textures[2],NULL,&rect); + SDL_Rect rect={(int)(tile_col*ctx->show_tiles_size-ctx->show_tiles_size*ctx->world.player.x2),(ctx->show_tiles_vertical_move-ctx->world.coins_pos[i])*ctx->show_tiles_size,ctx->show_tiles_size,ctx->show_tiles_size}; + SDL_RenderCopy(ctx->ren,ctx->textures[2],NULL,&rect); - for(int j=0;j<world.bricks[i].size();j++) + for(int j=0;j<ctx->world.bricks[i].size();j++) { - SDL_Rect rect={(int)(tile_col*show_tiles_size-show_tiles_size*world.player.x2),(show_tiles_vertical_move-world.bricks[i][j].altitude)*show_tiles_size,show_tiles_size,show_tiles_size}; + SDL_Rect rect={(int)(tile_col*ctx->show_tiles_size-ctx->show_tiles_size*ctx->world.player.x2),(ctx->show_tiles_vertical_move-ctx->world.bricks[i][j].altitude)*ctx->show_tiles_size,ctx->show_tiles_size,ctx->show_tiles_size}; - switch(world.bricks[i][j].type) + switch(ctx->world.bricks[i][j].type) { case 0: - SDL_RenderCopy(ren,textures[4],NULL,&rect); - SDL_SetRenderDrawColor(ren, 150+19*(i%2),115,70, 255); - rect.h=show_tiles_max_height; - rect.y+=show_tiles_size; - SDL_RenderFillRect(ren,&rect); + SDL_RenderCopy(ctx->ren,ctx->textures[4],NULL,&rect); + SDL_SetRenderDrawColor(ctx->ren, 150+19*(i%2),115,70, 255); + rect.h=ctx->show_tiles_max_height; + rect.y+=ctx->show_tiles_size; + SDL_RenderFillRect(ctx->ren,&rect); break; case 1: - SDL_RenderCopy(ren,textures[4],NULL,&rect); + SDL_RenderCopy(ctx->ren,ctx->textures[4],NULL,&rect); break; case 2: - SDL_RenderCopy(ren,textures[5],NULL,&rect); + SDL_RenderCopy(ctx->ren,ctx->textures[5],NULL,&rect); break; case 3: - SDL_RenderCopy(ren,textures[6],NULL,&rect); + SDL_RenderCopy(ctx->ren,ctx->textures[6],NULL,&rect); break; case 4: - SDL_RenderCopy(ren,textures[7],NULL,&rect); + SDL_RenderCopy(ctx->ren,ctx->textures[7],NULL,&rect); break; } @@ -323,55 +308,100 @@ int main(int, char**){ // water level - SDL_SetRenderDrawColor(ren, 100*(i%2),100*(i%2),255, 255); - SDL_Rect rect2={(int)(tile_col*show_tiles_size-show_tiles_size*world.player.x2),(int)(show_tiles_vertical_move*show_tiles_size-show_tiles_water_level*show_tiles_size+sin(frameTime/1000.0+i)*show_tiles_size*0.33),show_tiles_size,show_tiles_max_height}; - SDL_RenderFillRect(ren,&rect2); + SDL_SetRenderDrawColor(ctx->ren, 100*(i%2),100*(i%2),255, 255); + SDL_Rect rect2={(int)(tile_col*ctx->show_tiles_size-ctx->show_tiles_size*ctx->world.player.x2),(int)(ctx->show_tiles_vertical_move*ctx->show_tiles_size-ctx->show_tiles_water_level*ctx->show_tiles_size+sin(ctx->frameTime/1000.0+i)*ctx->show_tiles_size*0.33),ctx->show_tiles_size,ctx->show_tiles_max_height}; + SDL_RenderFillRect(ctx->ren,&rect2); } // render player char - SDL_Rect rect={(int)((show_tiles_back-0.5)*show_tiles_size),(int)((show_tiles_vertical_move-world.player.y)*show_tiles_size-world.player.y2*show_tiles_size),show_tiles_size,show_tiles_size}; - if(world.player.anim==0)SDL_RenderCopy(ren,textures[0],NULL,&rect); - else SDL_RenderCopy(ren,textures[1],NULL,&rect); + SDL_Rect rect={(int)((ctx->show_tiles_back-0.5)*ctx->show_tiles_size),(int)((ctx->show_tiles_vertical_move-ctx->world.player.y)*ctx->show_tiles_size-ctx->world.player.y2*ctx->show_tiles_size),ctx->show_tiles_size,ctx->show_tiles_size}; + if(ctx->world.player.anim==0)SDL_RenderCopy(ctx->ren,ctx->textures[0],NULL,&rect); + else SDL_RenderCopy(ctx->ren,ctx->textures[1],NULL,&rect); // HUD std::stringstream strbuffer; - strbuffer << "SCORE:" << world.coins << " | LVL:" << world.level << " | FPS:" << fps << " | RES:"<<win_width << "*" << win_height ; - sdl_put_str(ren, textures[3],10, 10, 10,strbuffer.str(),222,255,222,60,win_width,win_height,1,1); + strbuffer << "SCORE:" << ctx->world.coins << " | LVL:" << ctx->world.level << " | FPS:" << ctx->fps << " | RES:"<<ctx->win_width << "*" << ctx->win_height ; + sdl_put_str(ctx->ren, ctx->textures[3],10, 10, 10,strbuffer.str(),222,255,222,60,ctx->win_width,ctx->win_height,1,1); - if(world.player.dead) + if(ctx->world.player.dead) { - sdl_put_str(ren, textures[3],300, -1, 10,"LUNATIC LEMMY DIED!",250,100,100,19,win_width,win_height,1,2); - sdl_put_str(ren, textures[3],280, -1, 5,"[click to retry]",200,190,222,15,win_width,win_height,2,2); + sdl_put_str(ctx->ren, ctx->textures[3],300, -1, 10,"LUNATIC LEMMY DIED!",250,100,100,19,ctx->win_width,ctx->win_height,1,2); + sdl_put_str(ctx->ren, ctx->textures[3],280, -1, 5,"[click to retry]",200,190,222,15,ctx->win_width,ctx->win_height,2,2); } - if(world.player.win) + if(ctx->world.player.win) { - sdl_put_str(ren, textures[3],300, -1, 10," ! LEVEL UP ",100,250,100,22,win_width,win_height,1,2); - sdl_put_str(ren, textures[3],280, -1, 5,"[click for next level]",100,250,222,22,win_width,win_height,2,2); + sdl_put_str(ctx->ren, ctx->textures[3],300, -1, 10," ! LEVEL UP ",100,250,100,22,ctx->win_width,ctx->win_height,1,2); + sdl_put_str(ctx->ren, ctx->textures[3],280, -1, 5,"[click for next level]",100,250,222,22,ctx->win_width,ctx->win_height,2,2); } // // finish rendering - SDL_RenderPresent(ren); - if(world.player.collected_coin) + SDL_RenderPresent(ctx->ren); + if(ctx->world.player.collected_coin) { sdl_play_sound(); - world.player.collected_coin=false; + ctx->world.player.collected_coin=false; } // progress world simu - world.sim(deltaTime); + ctx->world.sim(ctx->deltaTime); + +#ifdef __EMSCRIPTEN + if(ctx->quit)emscripten_cancel_main_loop(); +#endif + +} + +int main(int, char**){ + + Context ctx1; + Context *ctx=&ctx1; + + // init SDL and assign SDL_Renderer on success + ctx->sdl_init=sdl_start(ctx->win_width,ctx->win_height); + if(ctx->sdl_init.first!=0) + { + sdl_stop(ctx->sdl_init); + return ctx->sdl_init.first; } + ctx->ren=ctx->sdl_init.second.second; + + // init Textures + ctx->textures.push_back(sdl_load_texture("guy01.bmp",0,0,255,ctx->ren)); + ctx->textures.push_back(sdl_load_texture("guy02.bmp",0,0,255,ctx->ren)); + ctx->textures.push_back(sdl_load_texture("coin.bmp",255,255,255,ctx->ren)); + ctx->textures.push_back(sdl_load_texture("fonts.bmp",0,0,0,ctx->ren)); + ctx->textures.push_back(sdl_load_texture("earth01.bmp",0,0,0,ctx->ren)); + ctx->textures.push_back(sdl_load_texture("gridder01.bmp",255,255,255,ctx->ren)); + ctx->textures.push_back(sdl_load_texture("gridder02.bmp",255,255,255,ctx->ren)); + ctx->textures.push_back(sdl_load_texture("gridder03.bmp",255,255,255,ctx->ren)); + + ctx->show_tiles_size=ctx->win_height/15; + ctx->show_tiles_back=ctx->win_width/ctx->show_tiles_size*0.25; + ctx->show_tiles_front=ctx->win_width/ctx->show_tiles_size*1; + +#ifdef __EMSCRIPTEN__ + + int simulate_infinite_loop = 1; + emscripten_set_main_loop_arg(main_loop, ctx, -1, simulate_infinite_loop); + +#endif + + while (!ctx->quit) + { + main_loop(ctx); + } //Clean up and quit - for(SDL_Texture *tex:textures) + for(SDL_Texture *tex:ctx->textures) { SDL_DestroyTexture(tex); } - sdl_stop(sdl_init); + sdl_stop(ctx->sdl_init); return 0; } |
