Skip to content

Commit

Permalink
Start menu works, next do some ai
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriel Mannheimer authored and Gabriel Mannheimer committed Oct 14, 2023
1 parent ea5526c commit 81db09f
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 36 deletions.
Binary file added main
Binary file not shown.
46 changes: 29 additions & 17 deletions src/SDLManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ SDLManager::SDLManager()
{
}

void SDLManager::RenderStart(SDL_Renderer* renderer, int windowWidth, int windowHeight, TTF_Font *font){
Menu SDLManager::RenderStart(SDL_Renderer* renderer, int windowWidth, int windowHeight, TTF_Font *font){
SDL_RenderClear(renderer);
SDL_Surface *neshaSurface = IMG_Load("./src/images/SBG.png");
if (neshaSurface)
Expand All @@ -22,18 +22,14 @@ void SDLManager::RenderStart(SDL_Renderer* renderer, int windowWidth, int window
int rH = windowHeight*0.18;
int rX = (windowWidth/2) - rW/2;
int rY = (windowHeight/2) - rH/2;
SDL_Rect startRect = {rX, rY, rW, rH};
SDL_Color color={0,0,0};
SDL_Surface* textSurface = TTF_RenderText_Solid(font,"Start",color);
if(textSurface){
SDL_Texture* tt = SDL_CreateTextureFromSurface(renderer,textSurface);
if(tt){
SDL_RenderCopy(renderer, tt,NULL, &startRect);
}
SDL_DestroyTexture(tt);
}
SDL_FreeSurface(textSurface);
SDL_FreeSurface(neshaSurface);

Menu StartMenu(windowWidth, windowHeight);
vector<string> text = {"Start"};
StartMenu.setText(text);
StartMenu.RenderText(renderer, font);

return StartMenu;

}

bool SDLManager::InitSDL()
Expand Down Expand Up @@ -146,7 +142,12 @@ bool SDLManager::InitSDL()

if (StartUp)
{
RenderStart(renderer, windowWidth, windowHeight, font);
Menu startMenu = RenderStart(renderer, windowWidth, windowHeight, font);
if(e.type == SDL_MOUSEBUTTONDOWN && e.button.button == SDL_BUTTON_LEFT)
{
if(startMenu.mouseCheck(mouseX, mouseY) == "Start")
StartUp = false;
}
}
else
{
Expand Down Expand Up @@ -203,9 +204,11 @@ bool SDLManager::InitSDL()
{
highlight = false;
Menu curMenu = renderMenu(renderer, font, TM.getTileList(), windowWidth, windowHeight, clickedPlayer);

if (e.type == SDL_MOUSEBUTTONDOWN && e.button.button == SDL_BUTTON_LEFT)
{
if (curMenu.mouseCheck(mouseX, mouseY) == 1)

if (curMenu.mouseCheck(mouseX, mouseY) == "Shoot")
{
cout << clickedPlayer->getTeam() << endl;
if (clickedPlayer->getTeam() == "Team1")
Expand All @@ -225,7 +228,7 @@ bool SDLManager::InitSDL()
clickedPlayer->setClicked(false);
}

else if (curMenu.mouseCheck(mouseX, mouseY) == 2)
else if (curMenu.mouseCheck(mouseX, mouseY) == "Move")
{
cout << "MOVE" << endl;
clicked = true;
Expand All @@ -234,13 +237,18 @@ bool SDLManager::InitSDL()
clickedPlayer->setClicked(false);
}

else if (curMenu.mouseCheck(mouseX, mouseY) == 3 && clickedPlayer->GetTile() == GM.getBallTile())
else if (curMenu.mouseCheck(mouseX, mouseY) == "Pass" && clickedPlayer->GetTile() == GM.getBallTile())
{
cout << "Pass" << endl;
menu = false;
highlight = true;
pass = true;
}
else
{
cout << "Null" << endl;
}

}
if (clickedPlayer != NULL)
GM.RenderShotPercent(renderer, font, windowWidth, windowHeight, clickedPlayer, PM.getPlayerList());
Expand Down Expand Up @@ -366,3 +374,7 @@ Menu SDLManager::renderMenu(SDL_Renderer *renderer, TTF_Font *font, vector<Tile>
curMenu.RenderText(renderer, font);
return curMenu;
}

bool SDLManager::checkStart(int mx, int my){

}
3 changes: 2 additions & 1 deletion src/SDLManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ class SDLManager
SDLManager();
void Highlight(SDL_Renderer *renderer, Tile *tile);
void renderBackground(SDL_Renderer *renderer, SDL_Texture *texture, int windowWidth, int windoHeight);
void RenderStart(SDL_Renderer*,int,int, TTF_Font*);
Menu RenderStart(SDL_Renderer*,int,int, TTF_Font*);

Menu renderMenu(SDL_Renderer *renderer, TTF_Font *font, vector<Tile> *tList, int windowWidth, int windowHeight, Player *p);
bool InitSDL();
bool checkStart(int, int);
};

#endif
5 changes: 5 additions & 0 deletions src/gameManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,8 @@ void GameManager::renderAllText(SDL_Renderer *renderer, vector<Tile> *tList, TTF
}
}
}


void GameManager::setTurn(bool t){
team1turn = t;
}
2 changes: 2 additions & 0 deletions src/gameManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class GameManager
vector<int> T2StartingPoints;
Player *ballPlayer;
int ifDefense(Player &, vector<Player>);
bool team1turn;

public:
GameManager();
Expand All @@ -42,6 +43,7 @@ class GameManager
void MoveAI(vector<Player> *);
void RenderBall(SDL_Renderer *, vector<Tile> *);
void renderAllText(SDL_Renderer *renderer, vector<Tile> *tile, TTF_Font *font);
void setTurn(bool);

int getBallTile();
int madeShot(Tile *, Player *, vector<Player>);
Expand Down
30 changes: 13 additions & 17 deletions src/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,16 @@ Menu::Menu(int windowWidth, int windowHeight)
menuData.menuH = windowHeight * 0.40;
}

int Menu::mouseCheck(int mX, int mY)
string Menu::mouseCheck(int mX, int mY)
{
for (TextData &md : textData)
{
if (md.menuX < mX && md.menuY < mY && (md.menuW + md.menuX) > mX && (md.menuH + md.menuY) > mY)
{
if (md.name == "Shoot")
return 1;
else if (md.name == "Move")
return 2;
else if(md.name == "Pass")
return 3;
return md.name;
}
}
return 0;
return "";
}

MenuData Menu::getMenuData()
Expand All @@ -41,11 +36,11 @@ MenuData Menu::getMenuData()

void Menu::RenderText(SDL_Renderer *renderer, TTF_Font *font)
{
vector<string> textList = {"Shoot", "Move", "Pass"};
int numItems = textList.size();
//vector<string> textList = {"Shoot", "Move", "Pass"};
int numItems = menuText.size();
int offsetConst = (menuData.menuH * 0.4) / numItems;
SDL_Color color = {0, 0, 0};
for (string &text : textList)
for (string &text : menuText)
{
if (font != NULL)
{
Expand All @@ -56,18 +51,18 @@ void Menu::RenderText(SDL_Renderer *renderer, TTF_Font *font)
if (textTexture != NULL)
{
SDL_Rect textRect;
if (textList[0] == text)
textRect = {menuData.menuX, menuData.menuY, menuData.menuW / 2, (int)((menuData.menuH / 2) / textList.size())};
if (menuText[0] == text)
textRect = {menuData.menuX, menuData.menuY, menuData.menuW / 2, (int)((menuData.menuH / 2) / menuText.size())};
else
textRect = {menuData.menuX, menuData.menuY + offsetConst, menuData.menuW / 2, (int)((menuData.menuH / 2) / textList.size())};

textRect = {menuData.menuX, menuData.menuY + offsetConst, menuData.menuW / 2, (int)((menuData.menuH / 2) / menuText.size())};
TextData curText = {text, textRect.x, textRect.y, textRect.w, textRect.h};
textData.emplace_back(curText);
SDL_RenderCopy(renderer, textTexture, nullptr, &textRect);
const char *rendererError = SDL_GetError();
if (strlen(rendererError) > 0)
{
cout << rendererError << endl;
cout << SDL_GetError() << endl;
}
SDL_FreeSurface(textSurface);
SDL_DestroyTexture(textTexture);
Expand All @@ -76,6 +71,7 @@ void Menu::RenderText(SDL_Renderer *renderer, TTF_Font *font)
}
}
}

}

vector<TextData> &Menu::getTextData()
Expand Down Expand Up @@ -104,6 +100,6 @@ void Menu::setData(Player *p, vector<Tile> *tList, int windowWidth)

void Menu::setText(vector<string> tlist){
for(string& s: tlist){
textData.emplace_back(s);
menuText.emplace_back(s);
}
}
3 changes: 2 additions & 1 deletion src/menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@ class Menu
bool isMouse;
MenuData menuData;
vector<TextData> textData;
vector<string> menuText;

public:
Menu();
Menu(int, int);

int mouseCheck(int, int);
string mouseCheck(int, int);
MenuData getMenuData();
vector<TextData> &getTextData();

Expand Down

0 comments on commit 81db09f

Please sign in to comment.