summaryrefslogtreecommitdiff
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
parent842c21d6aab2f38e35ea668194f67f85af2e3539 (diff)
coins, bigger level, hud, etc
-rw-r--r--World.cpp172
-rw-r--r--World.h32
-rw-r--r--coin.bmpbin0 -> 120122 bytes
-rw-r--r--hello.bmpbin384122 -> 0 bytes
-rw-r--r--main.cpp123
5 files changed, 258 insertions, 69 deletions
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;i<bricks.size();i++)
+ {
+ coins_pos.push_back(4);
+ }
+
+
+}
+void World::next_level()
+{
+ level++;
+ reset();
+ bricks=
+ {
+ { {0,1} },
+ { {0,1} },
+ { {0,1} },
+ { {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} },
+ {},
+ {},
+ {},
+ { {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} },
+ {},
+ {},
+ {},
+ {},
+ { {0,1} },
+ { {0,1} },
+ { {0,1} },
+ { {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} },
+ { {0,0}},
+ { {0,0}},
+ { {0,0}},
+ { {0,0}}
+ };
}
void World::sim(double time)
{
+ int last_brick=-1;
+
+ if(player.dead||player.win)return;
+
player.x2+=time/1000.0*player.speed;
- player.anim=(int)(player.x2*5)%2;
+ player.y2=0;
+
+ for(Brick &brick:bricks[player.x])
+ {
+ if(brick.altitude==player.y&&brick.type==3)
+ {
+ player.y2=player.x2;
+ last_brick=3;
+ break;
+ }
+ else if(brick.altitude==player.y-1&&brick.type==4)
+ {
+ player.y2=-player.x2;
+ last_brick=4;
+ }
+ else if(brick.altitude==player.y-1)
+ {
+ last_brick=2;
+ }
+
+
+ }
+ for(Brick &brick:bricks[player.x])
+ {
+ if(brick.altitude==player.y+1&&player.y2>0.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<std::vector<Brick>> 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<int> coins_pos;
+ std::vector<std::vector<Brick>> 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
--- /dev/null
+++ b/coin.bmp
Binary files differ
diff --git a/hello.bmp b/hello.bmp
deleted file mode 100644
index 5c2e250..0000000
--- a/hello.bmp
+++ /dev/null
Binary files differ
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);