Skip to content

Commit

Permalink
Fixed menu bug after game end
Browse files Browse the repository at this point in the history
  • Loading branch information
TanishTuteja committed Apr 17, 2022
1 parent 823da91 commit 8e3486c
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 3 deletions.
14 changes: 14 additions & 0 deletions Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Button::Button(int x, int y, int w, int h, SDL_Color bgColor, LWindow &window, s
void Button::render(SDL_Renderer *renderer)
{
bool renderDefault = true;
// std::cout << "IN RENDER" << std::endl;

if (centerX)
{
Expand All @@ -31,6 +32,7 @@ void Button::render(SDL_Renderer *renderer)
int newY = y - (newH - h) / 2;
clickTexture->render(renderer, newX, newY);
renderDefault = false;
// std::cout << "HERE2" << std::endl;
}

else if (highlighted)
Expand All @@ -57,6 +59,8 @@ void Button::render(SDL_Renderer *renderer)
if (tex != NULL)
{
tex->render(renderer, x, y);

// std::cout << x << " " << y << std::endl;
}
else
{
Expand Down Expand Up @@ -149,4 +153,14 @@ int Button::getWidth()
int Button::getHeight()
{
return h;
}

void Button::setX(int x)
{
this->x = x;
}

void Button::setY(int y)
{
this->y = y;
}
2 changes: 2 additions & 0 deletions Button.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class Button
void setOnClickListener(std::function<void(LWindow &window)> f);
int getX();
int getY();
void setX(int x);
void setY(int y);
int getWidth();
int getHeight();
};
Expand Down
2 changes: 1 addition & 1 deletion Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void LGame::initTasks()

void LGame::handleEvent(SDL_Event &e)
{
if (waitingEnd)
if (waitingEnd && !gameEnded)
{
return;
}
Expand Down
18 changes: 16 additions & 2 deletions MainMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ MainMenu::MainMenu(LWindow &window, int &sockfd, sockaddr_in &theiraddr) : LScre
window.loadTexture(buttonBg, "resources/ButtonBG.png");
window.loadTexture(buttonBgHighlighted, "resources/ButtonBGHighlighted.png");
window.loadTexture(buttonBgClicked, "resources/ButtonBGClicked.png");

int x = (window.getWidth() - buttonBg.getWidth()) / 2;
int y = (window.getHeight() - buttonBg.getHeight()) / 2;
int w = buttonBg.getWidth();
int h = buttonBg.getHeight();
int x = (window.getWidth() - w) / 2;
int y = (window.getHeight() - h) / 2;

SDL_Color txtColor = {2, 10, 255, 255};
TTF_Font *font = TTF_OpenFont("resources/FrostbiteBossFight-dL0Z.ttf", 28);

Expand Down Expand Up @@ -99,6 +101,16 @@ void MainMenu::handleEvent(SDL_Event &e)

void MainMenu::update()
{

int x = (window.getWidth() - buttonBg.getWidth()) / 2;
int y = (window.getHeight() - buttonBg.getHeight()) / 2;

buttons[0]->setX(x);
buttons[0]->setY(y);

textInputs[0]->setX(x);
textInputs[0]->setY(y - 40 - 20);

unsigned int len = sizeof(theirAddr);
int n = recvfrom(sockfd, (char *)recBuf, 512, MSG_WAITALL, (struct sockaddr *)&theirAddr, &len);
if (n != -1)
Expand All @@ -118,9 +130,11 @@ void MainMenu::render(SDL_Renderer *renderer)
{
SDL_Rect defViewport = {0, 0, window.getWidth(), window.getHeight()};
SDL_RenderSetViewport(renderer, &defViewport);
// std::cout << window.getHeight() << std::endl;

SDL_SetRenderDrawColor(renderer, 3, 211, 252, 255);
SDL_RenderClear(renderer);

for (int i = 0; i < buttons.size(); i++)
{
buttons[i]->render(renderer);
Expand Down
1 change: 1 addition & 0 deletions MyWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ void LWindow::handleEvent(SDL_Event &e)
mWidth = e.window.data1;
mHeight = e.window.data2;
SDL_RenderPresent(mRenderer);
// std::cout << "HERE" << std::endl;
break;

// Repaint on exposure
Expand Down
10 changes: 10 additions & 0 deletions TextInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ void TextInput::handleEvent(SDL_Event &e)
}
}

void TextInput::setX(int x)
{
this->x = x;
}

void TextInput::setY(int y)
{
this->y = y;
}

std::string TextInput::getText()
{
return mText.getText();
Expand Down
2 changes: 2 additions & 0 deletions TextInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ class TextInput
void setCenterX(bool centerX);
void handleEvent(SDL_Event &e);
std::string getText();
void setX(int x);
void setY(int y);
};
#endif

0 comments on commit 8e3486c

Please sign in to comment.