From e2063047ee1d145dbdf1963a23f183cc9db9bf52 Mon Sep 17 00:00:00 2001 From: miguel Date: Wed, 13 Sep 2017 10:50:03 +0200 Subject: coins, bigger level, hud, etc --- World.cpp | 172 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- World.h | 32 ++++-------- coin.bmp | Bin 0 -> 120122 bytes hello.bmp | Bin 384122 -> 0 bytes main.cpp | 123 ++++++++++++++++++++++++++++++-------------- 5 files changed, 258 insertions(+), 69 deletions(-) create mode 100644 coin.bmp delete mode 100644 hello.bmp diff --git a/World.cpp b/World.cpp index 1a3e9b5..0e5bff2 100644 --- a/World.cpp +++ b/World.cpp @@ -1,32 +1,184 @@ #include "World.h" -World::World(int level) +World::World(int l):level(l) +{ + level--; + next_level(); + reset(); +} + +void World::reset() { - player.speed=0; player.x2=player.x=0; + player.speed=level; player.anim=0; - // generate level player.y=bricks[0][0].altitude+1; + player.y2=0; + player.dead=false; + player.win=false; + + coins_pos.clear(); + + for(int i=0;i0.4) + { + player.dead=true; + return; + } + } + + player.anim=(int)(player.x2*6*player.speed)%2; while(player.x2>1.0) { player.x2-=1; player.x+=1; - - player.speed--; + + if (player.x>=bricks.size()) + { + player.win=true; + return; + } + + if(last_brick==4){player.y--;player.y2=0;} + if(last_brick==3){player.y++;player.y2=0;} + + bool ok=false; + for(Brick &brick:bricks[player.x]) + { + if(brick.altitude==player.y-1&&brick.type!=3)ok=true; + else if(brick.altitude==player.y&&brick.type==3)ok=true; + else if(brick.altitude==player.y&&brick.type!=3) + { + ok=false; + break; + } + } + + if (!ok) + { + player.dead=true; + return; + } + +// player.speed--; } + + + + + + + + } void World::mouseclick(int x, int y) { + if(player.dead){reset();return;} + if(player.win){next_level();return;} + if(x<0||x>=bricks.size())return; + int idx=0; for(Brick &brick:bricks[x]) { + if(brick.type==0&&brick.altitude>=y) + { + return; + } if(brick.type<=1&&brick.altitude==y) { return; @@ -34,13 +186,17 @@ void World::mouseclick(int x, int y) if(brick.type>1&&brick.altitude==y) { brick.type++; - if(brick.type>=6)brick.type=2; + if(brick.type>=5) + { + bricks[x].erase(bricks[x].begin()+idx); + } return; } + idx++; } bricks[x].push_back({y,2}); - player.speed++; +// player.speed++; } diff --git a/World.h b/World.h index fba93b1..e029604 100644 --- a/World.h +++ b/World.h @@ -5,10 +5,6 @@ struct Brick { int altitude; //meter above sea level int type; -// ~Brick(){std::cout<<"deconstruct"<< std::endl;} -// Brick(int altitude, int type){std::cout<<"construct"<< std::endl;} - - }; struct Player @@ -16,34 +12,24 @@ struct Player int x; int y; double x2; + double y2; int speed; int anim; + bool dead; + bool win; }; struct World { Player player; - std::vector> bricks= - { - { {0,2} }, - { {0,2} }, - { {0,2} }, - { {0,0},{5,1} }, - { {0,0},{5,1} }, - { {0,0},{5,1} }, - { {0,0},{5,1} }, - {}, - { {5,1} }, - { {5,1} }, - { {0,0},{5,1} }, - { {0,0},{5,1} }, - { {0,0},{5,1} }, - { {1,0},{5,1} }, - { {2,0},{5,1},{7,1} }, - { {3,0},{6,1} } - }; + std::vector coins_pos; + std::vector> bricks{{{}}}; + int level; + int coins; World(int level); void sim(double); void mouseclick(int x, int y); + void reset(); + void next_level(); }; diff --git a/coin.bmp b/coin.bmp new file mode 100644 index 0000000..645e839 Binary files /dev/null and b/coin.bmp differ diff --git a/hello.bmp b/hello.bmp deleted file mode 100644 index 5c2e250..0000000 Binary files a/hello.bmp and /dev/null differ diff --git a/main.cpp b/main.cpp index c9bfae0..82e914d 100644 --- a/main.cpp +++ b/main.cpp @@ -1,3 +1,5 @@ +#include + #include #include "World.h" @@ -79,14 +81,57 @@ SDL_Texture* sdl_load_texture(std::string imagePath,int key_r, int key_g, int ke return tex; } +void sdl_put_letter(SDL_Renderer *ren, SDL_Texture *tex,int x,int y, int w, int h, int r, int g, int b, int ch) +{ + + if(ch>=123&&ch<=126)ch-=123-40; + else if(ch>=97&&ch<=122)ch-=97; + else if(ch>=65&&ch<=96)ch-=65; + else if(ch>=32&&ch<=63)ch-=32-50; + else ch=81; + + int col=ch%10; + int row=ch/10; + SDL_SetTextureColorMod(tex,r,g,b); + SDL_Rect src={col*8,row*10,8,10}; + SDL_Rect dst={x,y,w,h}; + SDL_RenderCopy(ren,tex,&src,&dst); + +} + +void sdl_put_str(SDL_Renderer *ren, SDL_Texture *tex,int x, int y,int size,std::string str,int r,int g, int b) +{ + int pos=0; + for(char ch:str) + { + pos++; + sdl_put_letter(ren,tex,x+8*size*pos,y, 8*size,10*size, r,g,b, ch); + } +} + // int main(int, char**){ - // initial values + // 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) { @@ -96,29 +141,16 @@ int main(int, char**){ SDL_Renderer *ren=sdl_init.second.second; + // init Textures std::vector 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("hello.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)); - // SDL_SetTextureColorMod(tex4,255,0,0); - - bool quit = false; - SDL_Event event; - - Uint32 frameTime; - Uint32 lastFrameTime = 0; - Uint32 deltaTime = 0; - Uint32 fpsTime = 0; - Uint32 fpsCount = 0; - - Uint32 mouse_x=0; - Uint32 mouse_y=0; - // init world LEVEL 1 World world(1); @@ -132,9 +164,9 @@ int main(int, char**){ fpsTime+=deltaTime; fpsCount++; - if(fpsTime>3000) + if(fpsTime>1000) { - std::cout << "FPS : " << fpsCount*1000/fpsTime << std::endl; + fps=fpsCount*1000/fpsTime; fpsTime=0; fpsCount=0; } @@ -176,17 +208,20 @@ int main(int, char**){ } } - // rendering & run world simu + // clear screen SDL_SetRenderDrawColor(ren, 0, 0, 0, 255); SDL_RenderClear(ren); - int br=-1; - + // render visible area for(int i=world.player.x-5;i=world.bricks.size())continue; + SDL_Rect rect={br*100-100*world.player.x2,(10-world.coins_pos[i])*100,100,100}; + SDL_RenderCopy(ren,textures[2],NULL,&rect); + + for(int j=0;j