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 --- main.cpp | 123 +++++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 85 insertions(+), 38 deletions(-) (limited to 'main.cpp') 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