summaryrefslogtreecommitdiff
path: root/main.cpp
diff options
context:
space:
mode:
authormiguel <miguel@localhost>2017-09-13 10:50:03 +0200
committermiguel <miguel@localhost>2017-09-13 10:50:03 +0200
commite2063047ee1d145dbdf1963a23f183cc9db9bf52 (patch)
tree6c4d876781b072f4937d40acf22367b7c1ccb642 /main.cpp
parent842c21d6aab2f38e35ea668194f67f85af2e3539 (diff)
coins, bigger level, hud, etc
Diffstat (limited to 'main.cpp')
-rw-r--r--main.cpp123
1 files changed, 85 insertions, 38 deletions
diff --git a/main.cpp b/main.cpp
index c9bfae0..82e914d 100644
--- a/main.cpp
+++ b/main.cpp
@@ -1,3 +1,5 @@
+#include <sstream>
+
#include <SDL.h>
#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<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("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.player.x+30;i++)
{
- br++;
+ int br=i-world.player.x+5;
if(i<0||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<world.bricks[i].size();j++)
{
SDL_Rect rect={br*100-100*world.player.x2,(10-world.bricks[i][j].altitude)*100,100,100};
@@ -217,39 +252,51 @@ int main(int, char**){
}
+
+ // water level
SDL_SetRenderDrawColor(ren, 100*(i%2),100*(i%2),255, 255);
SDL_Rect rect2={br*100-100*world.player.x2,(15)*100+std::sin(frameTime/1000.0+i)*30,100,800};
SDL_RenderFillRect(ren,&rect2);
}
- SDL_Rect rect={450,(9-world.bricks[0][0].altitude)*100,100,100};
+ // render player char
+ SDL_Rect rect={450,(10-world.player.y)*100-world.player.y2*100,100,100};
if(world.player.anim==0)SDL_RenderCopy(ren,textures[0],NULL,&rect);
else SDL_RenderCopy(ren,textures[1],NULL,&rect);
- SDL_Rect rect2={300,50,640+std::sin(frameTime/1000.0)*100,200};
- SDL_RenderCopyEx(ren,textures[2],NULL,&rect2,std::sin(frameTime/1000.0)*10,NULL,SDL_FLIP_NONE);
-
-// SDL_SetRenderDrawColor(ren, 255, 255, 255, 255);
-// SDL_Rect rect={mouse_x-10,mouse_y-10,20,20};
-
-
- // test text:
-
- std::string testString="Hello World\nMotherfuckas";
-//. SDL_RenderCopyEx(ren,tex4,NULL,NULL,0,NULL,SDL_FLIP_NONE);
-
-
+ // HUD
+ std::stringstream strbuffer;
+ strbuffer << "COINS: " << world.coins << " LEVEL: " << world.level << " FPS: " << fps ;
+ sdl_put_str(ren, textures[3],10, 10, 4,strbuffer.str(),190,190,222);
+ if(world.player.dead)
+ {
+ SDL_SetRenderDrawColor(ren, 100,100,100, 255);
+ SDL_Rect rect2={250,350,1200,300};
+ SDL_RenderFillRect(ren,&rect2);
+ sdl_put_str(ren, textures[3],300, 400, 7,"LUNATIC LEMMY DIED!",250,100,100);
+ sdl_put_str(ren, textures[3],280, 500, 4,"[click to retry]",200,190,222);
+ }
+ if(world.player.win)
+ {
+ SDL_SetRenderDrawColor(ren, 100,100,100, 255);
+ SDL_Rect rect2={250,350,800,300};
+ SDL_RenderFillRect(ren,&rect2);
+ sdl_put_str(ren, textures[3],300, 400, 7,"! LEVEL UP !",100,250,100);
+ sdl_put_str(ren, textures[3],280, 500, 4,"[click for next level]",100,250,222);
+ }
+
+ // finish rendering
SDL_RenderPresent(ren);
- // run world simu
+ // progress world simu
world.sim(deltaTime);
}
- //Clean up our objects and quit
+ //Clean up and quit
for(SDL_Texture *tex:textures)
{
SDL_DestroyTexture(tex);