Skip to content

Commit

Permalink
menu buttons in menu state now
Browse files Browse the repository at this point in the history
  • Loading branch information
spkvn committed Nov 30, 2017
1 parent 67eb349 commit 422306e
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 13 deletions.
4 changes: 2 additions & 2 deletions Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ bool Game::init(const char* title, int xpos, int ypos, int height, int width, in
{
m_pGameStateMachine = new GameStateMachine();
m_pGameStateMachine->changeState(new MenuState());
m_gameObjects.push_back(new Player(new LoaderParams(100,100,76,53,"animate")));
m_gameObjects.push_back(new Enemy(new LoaderParams(200,200,76,53,"animate")));
// m_gameObjects.push_back(new Player(new LoaderParams(100,100,76,53,"animate")));
// m_gameObjects.push_back(new Enemy(new LoaderParams(200,200,76,53,"animate")));

m_bRunning = TheTextureManager::instance()->load("assets/doggy.png","animate", m_pRenderer);
}
Expand Down
1 change: 1 addition & 0 deletions GameState.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef GAME_STATE_H
#define GAME_STATE_H
#include <string>
#include <iostream>

//Abstract Base Class.
class GameState
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#OBJS specifies which files to compile as part of the project
OBJS = main.cpp Game.cpp Player.cpp Enemy.cpp SDLGameObject.cpp TextureManager.cpp Vector2D.cpp InputHandler.cpp MenuState.cpp PlayState.cpp GameStateMachine.cpp
OBJS = main.cpp Game.cpp Player.cpp Enemy.cpp SDLGameObject.cpp TextureManager.cpp Vector2D.cpp InputHandler.cpp MenuState.cpp PlayState.cpp GameStateMachine.cpp MenuButton.cpp

#CC specifies which compiler we're using
CC = g++
Expand Down
6 changes: 3 additions & 3 deletions MenuButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ MenuButton::MenuButton(const LoaderParams* pParams) : SDLGameObject(pParams)
m_currentFrame = MOUSE_OUT; //start at frame 2, because I've got my sprites backwards;
}

MenuButton::draw()
void MenuButton::draw()
{
SDLGameObject::draw(); // use base class drawing
}

MenuButton::update()
void MenuButton::update()
{
Vector2D* pMousePos = TheInputHandler::instance()->getMousePosition();

Expand All @@ -21,7 +21,7 @@ MenuButton::update()
{
m_currentFrame = MOUSE_OVER;

if(TheInputHandler::instance->getMouseButtonState(LEFT))
if(TheInputHandler::instance()->getMouseButtonState(LEFT))
{
m_currentFrame = CLICKED;
}
Expand Down
5 changes: 3 additions & 2 deletions MenuButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define MENU_BUTTON_H
#include "SDLGameObject.h"
#include "Vector2D.h"
#include "InputHandler.h"

class MenuButton : public SDLGameObject
{
Expand All @@ -17,7 +18,7 @@ class MenuButton : public SDLGameObject
MOUSE_OUT = 2,
MOUSE_OVER = 1,
CLICKED = 0
}
}
};
};

#endif//MENU_BUTTON_H
40 changes: 36 additions & 4 deletions MenuState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,55 @@ const std::string MenuState::s_menuID = "MENU";

void MenuState::update()
{

for(int i =0; i < m_gameObjects.size(); i++)
{
m_gameObjects[i]->update();
}
}

void MenuState::render()
{

for(int i =0; i < m_gameObjects.size(); i++)
{
m_gameObjects[i]->draw();
}
}

bool MenuState::onEnter()
{
std::cout << "enter MenuState" << std::endl;
if(!TheTextureManager::instance()->load("assets/startButton.png","startButton",TheGame::instance()->getRenderer()))
{
//failed to load startButton
return false;
}

if(!TheTextureManager::instance()->load("assets/exitButton.png","exitButton",TheGame::instance()->getRenderer()))
{
//failed to load exitButton
return false;
}

GameObject* button1 = new MenuButton( new LoaderParams(100,100,64,32,"startButton") );
GameObject* button2 = new MenuButton( new LoaderParams(100,300,64,32,"exitButton") );

m_gameObjects.push_back(button1);
m_gameObjects.push_back(button2);

std::cout << "Entering MenuState" << std::endl;
return true;
}


bool MenuState::onExit()
{
std::cout << "exiting MenuState" << std::endl;
for(int i = 0; i < m_gameObjects.size(); i++)
{
m_gameObjects[i]->clean();
}
m_gameObjects.clear();
TheTextureManager::instance()->clearFromTextureMap("startButton");
TheTextureManager::instance()->clearFromTextureMap("exitButton");

std::cout << "Exiting MenuState" << std::endl;
return true;
}
7 changes: 7 additions & 0 deletions MenuState.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#ifndef MENU_STATE_H
#define MENU_STATE_H
#include <iostream>
#include <vector>
#include "GameState.h"
#include "GameObject.h"
#include "LoaderParams.h"
#include "Game.h"
#include "TextureManager.h"
#include "MenuButton.h"

class MenuState : public GameState
{
Expand All @@ -15,6 +21,7 @@ class MenuState : public GameState
virtual std::string getStateID() const { return s_menuID;}
private:
static const std::string s_menuID;
std::vector<GameObject*> m_gameObjects;
};

#endif//MENU_STATE_H
7 changes: 6 additions & 1 deletion TextureManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ void TextureManager::drawFrame(std::string id, int x, int y, int width, int hei
SDL_Rect srcRect;
SDL_Rect destRect;

srcRect.x = currentFrame;
srcRect.x = currentFrame * width;
srcRect.y = height * (currentRow - 1);
srcRect.w = destRect.w = width;
srcRect.h = destRect.h = height;
Expand All @@ -65,4 +65,9 @@ TextureManager* TextureManager::instance()
}

return s_pInstance;
}

void TextureManager::clearFromTextureMap(std::string id)
{
m_textureMap.erase(id);
}
1 change: 1 addition & 0 deletions TextureManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class TextureManager
void draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip filp = SDL_FLIP_NONE);
void drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE);
static TextureManager* instance();
void clearFromTextureMap(std::string id);
};

//defining singleton.
Expand Down
Binary file modified chapter5Executable
Binary file not shown.

0 comments on commit 422306e

Please sign in to comment.