Better button handling
This commit is contained in:
parent
1da224483b
commit
ccc5467d64
Binary file not shown.
Binary file not shown.
|
@ -40,11 +40,9 @@ void Maps::loadMap(std::string path){
|
|||
map >> playerPosY;
|
||||
playerPosX *= sz;
|
||||
playerPosY *= sz;
|
||||
std::cout << playerPosX << " " << playerPosY << std::endl;
|
||||
|
||||
//Get the total of blocks
|
||||
map >> blockTotal;
|
||||
std::cout << blockTotal << std::endl;
|
||||
|
||||
blockRect = new int*[blockTotal];
|
||||
//Build rows
|
||||
|
@ -55,13 +53,10 @@ void Maps::loadMap(std::string path){
|
|||
for(int j = 0; j < 4; j++){
|
||||
map >> blockRect[i][j];
|
||||
blockRect[i][j] *= 40;
|
||||
std::cout << blockRect[i][j] << " ";
|
||||
}
|
||||
if(blockRect[i][0]+blockRect[i][2] > mapWidth) mapWidth = blockRect[i][0]+blockRect[i][2];
|
||||
if(blockRect[i][1]+blockRect[i][3] > mapHeight) mapHeight = blockRect[i][1]+blockRect[i][3];
|
||||
std::cout << std::endl;
|
||||
}
|
||||
std::cout << mapWidth << " - " << mapHeight << std::endl;
|
||||
|
||||
//Very descriptive by itself
|
||||
map.close();
|
||||
|
@ -74,9 +69,9 @@ int Maps::map(std::string mapName){
|
|||
Texture level;
|
||||
level.setRenderer(renderer);
|
||||
SDL_Color textColor = {0xFF,0xFF,0xFF};
|
||||
level.loadFromRendererText(mapName,textColor);
|
||||
level.loadFromRendererText("level: " + mapName,textColor);
|
||||
|
||||
SDL_Rect levelRect = {0,0,level.getWidth(),level.getHeight()};
|
||||
SDL_Rect levelRect = {10,10,level.getWidth(),level.getHeight()};
|
||||
|
||||
//Initialize the player, set its position, pass the map's size and set the renderer
|
||||
Player player(playerPosX,playerPosY,mapHeight,renderer);
|
||||
|
|
|
@ -1,5 +1,49 @@
|
|||
#include "menu.h"
|
||||
|
||||
class Button{
|
||||
public:
|
||||
Button(int x,int y,int w,int h,SDL_Renderer* render,std::string buttonText);
|
||||
void render();
|
||||
void hover();
|
||||
SDL_Rect getRect();
|
||||
private:
|
||||
SDL_Rect buttonRect;
|
||||
SDL_Rect textRect;
|
||||
SDL_Renderer* renderer;
|
||||
Texture text;
|
||||
|
||||
bool isHover;
|
||||
};
|
||||
|
||||
Button::Button(int x,int y,int w,int h,SDL_Renderer* render,std::string buttonText){
|
||||
renderer = render;
|
||||
buttonRect = {x,y,w,h};
|
||||
isHover = false;
|
||||
|
||||
text.setRenderer(renderer);
|
||||
SDL_Color textColor = {0xFF,0xFF,0xFF};
|
||||
text.loadFromRendererText(buttonText,textColor);
|
||||
|
||||
textRect = {x+(w/2)-text.getWidth()/2,y+(h/2)-text.getHeight()/2,text.getWidth(),text.getHeight()};
|
||||
};
|
||||
|
||||
void Button::render(){
|
||||
if(isHover) SDL_SetRenderDrawColor(renderer,80,80,80,0xFF);
|
||||
else SDL_SetRenderDrawColor(renderer,128,128,128,0xFF);
|
||||
|
||||
SDL_RenderFillRect(renderer,&buttonRect);
|
||||
text.render(&textRect);
|
||||
isHover = false;
|
||||
};
|
||||
|
||||
void Button::hover(){
|
||||
isHover = true;
|
||||
};
|
||||
|
||||
SDL_Rect Button::getRect(){
|
||||
return buttonRect;
|
||||
};
|
||||
|
||||
int menu(SDL_Renderer* renderer){
|
||||
int SCREEN_WIDTH, SCREEN_HEIGHT;
|
||||
SDL_GetRendererOutputSize(renderer,&SCREEN_WIDTH,&SCREEN_HEIGHT);
|
||||
|
@ -17,36 +61,13 @@ int menu(SDL_Renderer* renderer){
|
|||
|
||||
int btnSpc = SCREEN_HEIGHT - (logo.y + logo.h);
|
||||
int buttonNum = 3;
|
||||
SDL_Rect button[buttonNum];
|
||||
int btnWidth = SCREEN_WIDTH/3;
|
||||
int btnHeight = btnSpc/buttonNum/2;
|
||||
int spc = (btnSpc-buttonNum*btnHeight)/(buttonNum+1);
|
||||
|
||||
for(int i = 0; i < buttonNum; i++){
|
||||
button[i].x = SCREEN_WIDTH/2-btnWidth/2;
|
||||
button[i].y = ((i+1)*spc+i*btnHeight)+SCREEN_HEIGHT-btnSpc;
|
||||
button[i].w = btnWidth;
|
||||
button[i].h = btnHeight;
|
||||
}
|
||||
|
||||
Texture play;
|
||||
play.setRenderer(renderer);
|
||||
SDL_Color textColor = {0xFF,0xFF,0xFF};
|
||||
play.loadFromRendererText("Play",textColor);
|
||||
|
||||
SDL_Rect playRect = {button[0].x+(button[0].w/2)-play.getWidth()/2,button[0].y,play.getWidth(),play.getHeight()};
|
||||
|
||||
Texture exit;
|
||||
exit.setRenderer(renderer);
|
||||
exit.loadFromRendererText("Quit",textColor);
|
||||
|
||||
SDL_Rect exitRect = {button[2].x+(button[2].w/2)-exit.getWidth()/2,button[2].y,exit.getWidth(),exit.getHeight()};
|
||||
|
||||
Texture options;
|
||||
options.setRenderer(renderer);
|
||||
options.loadFromRendererText("Options",textColor);
|
||||
|
||||
SDL_Rect optionsRect = {button[1].x+(button[1].w/2)-options.getWidth()/2,button[1].y,options.getWidth(),options.getHeight()};
|
||||
Button playBtn(SCREEN_WIDTH/2-btnWidth/2,(1*spc+0*btnHeight)+SCREEN_HEIGHT-btnSpc,btnWidth,btnHeight,renderer,"play");
|
||||
Button optionsBtn(SCREEN_WIDTH/2-btnWidth/2,(2*spc+1*btnHeight)+SCREEN_HEIGHT-btnSpc,btnWidth,btnHeight,renderer,"options");
|
||||
Button quitBtn(SCREEN_WIDTH/2-btnWidth/2,(3*spc+2*btnHeight)+SCREEN_HEIGHT-btnSpc,btnWidth,btnHeight,renderer,"quit");
|
||||
|
||||
int select = 0;
|
||||
bool wait = 0;
|
||||
|
@ -76,26 +97,20 @@ int menu(SDL_Renderer* renderer){
|
|||
|
||||
txLogo.render(&logo);
|
||||
|
||||
for(int i = 0; i < buttonNum; i++){
|
||||
if(static_cast<int>(select) == i){
|
||||
SDL_SetRenderDrawColor(renderer,80,80,80,0xFF);
|
||||
if(currentKeyStates[SDL_SCANCODE_SPACE]) SDL_SetRenderDrawColor(renderer,200,200,200,0xFF);
|
||||
}
|
||||
else SDL_SetRenderDrawColor(renderer,128,128,128,0xFF);
|
||||
|
||||
SDL_RenderFillRect(renderer,&button[i]);
|
||||
}
|
||||
|
||||
play.render(&playRect);
|
||||
exit.render(&exitRect);
|
||||
options.render(&optionsRect);
|
||||
|
||||
if(currentKeyStates[SDL_SCANCODE_SPACE]){
|
||||
if(select == 0) return 1;
|
||||
else if(select == 1) return 0;
|
||||
else if(select == 2) break;
|
||||
}
|
||||
|
||||
if(select == 0) playBtn.hover();
|
||||
else if(select == 1) optionsBtn.hover();
|
||||
else quitBtn.hover();
|
||||
|
||||
playBtn.render();
|
||||
optionsBtn.render();
|
||||
quitBtn.render();
|
||||
|
||||
SDL_RenderPresent(renderer);
|
||||
}
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ Texture::Texture(){
|
|||
IMG_Init(IMG_INIT_PNG);
|
||||
if(TTF_Init() == -1) std::cout << TTF_GetError() << std::endl;
|
||||
|
||||
font = TTF_OpenFont("pangolin.ttf",30);
|
||||
font = TTF_OpenFont("cooper-hewitt-medium.otf",30);
|
||||
};
|
||||
|
||||
void Texture::setRenderer(SDL_Renderer* render){
|
||||
|
|
Loading…
Reference in New Issue