Implemented main menu and buttons

This commit is contained in:
Kayne Ruse
2013-05-15 23:20:47 +10:00
parent 613d9cec08
commit 30d163ec80
12 changed files with 108 additions and 34 deletions
+1
View File
@@ -79,6 +79,7 @@ void BaseScene::RunFrame() {
FrameStart();
HandleEvents();
Update();
SDL_FillRect(screen, 0, 0);
Render(screen);
SDL_Flip(screen);
FrameEnd();
+3 -3
View File
@@ -41,7 +41,7 @@ Button::Button(Sint16 i, Sint16 j, SDL_Surface* imageSurface, SDL_Surface* fontS
SetText(s);
}
Button::State Button::MouseMotion(SDL_MouseMotionEvent& motion) {
Button::State Button::MouseMotion(SDL_MouseMotionEvent const& motion) {
if (motion.state & SDL_BUTTON_LMASK) {
return CalcState(motion.x, motion.y, true);
}
@@ -51,14 +51,14 @@ Button::State Button::MouseMotion(SDL_MouseMotionEvent& motion) {
return state;
}
Button::State Button::MouseButtonDown(SDL_MouseButtonEvent& button) {
Button::State Button::MouseButtonDown(SDL_MouseButtonEvent const& button) {
if (button.button == SDL_BUTTON_LEFT) {
return CalcState(button.x, button.y, true);
}
return state;
}
Button::State Button::MouseButtonUp(SDL_MouseButtonEvent& button) {
Button::State Button::MouseButtonUp(SDL_MouseButtonEvent const& button) {
if (button.button == SDL_BUTTON_LEFT) {
return CalcState(button.x, button.y, false);
}
+3 -3
View File
@@ -38,9 +38,9 @@ public:
Button(Sint16 x, Sint16 y, SDL_Surface* imageSurface, SDL_Surface* fontSurface, std::string text = "");
//return the current state
State MouseMotion(SDL_MouseMotionEvent&);
State MouseButtonDown(SDL_MouseButtonEvent&);
State MouseButtonUp(SDL_MouseButtonEvent&);
State MouseMotion(SDL_MouseMotionEvent const&);
State MouseButtonDown(SDL_MouseButtonEvent const&);
State MouseButtonUp(SDL_MouseButtonEvent const&);
State GetState() const {
return state;
}
+25 -5
View File
@@ -49,7 +49,6 @@ void InGame::Update() {
}
void InGame::Render(SDL_Surface* const screen) {
SDL_FillRect(screen, 0, 0);
playerMgr.DrawAllTo(screen);
}
@@ -89,16 +88,16 @@ void InGame::KeyDown(SDL_KeyboardEvent const& key) {
break;
case SDLK_1:
currentPlayer = 0;
SwitchToPlayer(0);
break;
case SDLK_2:
currentPlayer = 1;
SwitchToPlayer(1);
break;
case SDLK_3:
currentPlayer = 2;
SwitchToPlayer(2);
break;
case SDLK_4:
currentPlayer = 3;
SwitchToPlayer(3);
break;
}
}
@@ -128,3 +127,24 @@ void InGame::NewPlayer(int index, std::string avatarName, int x, int y) {
Player* p = playerMgr.New(index, surfaceMgr[avatarName]);
p->SetPosition(Vector2(x, y));
}
void InGame::SwitchToPlayer(int index) {
//dirty hacks for smooth movement
playerMgr[currentPlayer]->SetMotion(Vector2(0,0));
currentPlayer = index;
Uint8* key = SDL_GetKeyState(NULL);
if (key[SDLK_w]) {
playerMgr[currentPlayer]->WalkInDirection(Direction::NORTH);
}
if (key[SDLK_s]) {
playerMgr[currentPlayer]->WalkInDirection(Direction::SOUTH);
}
if (key[SDLK_a]) {
playerMgr[currentPlayer]->WalkInDirection(Direction::WEST);
}
if (key[SDLK_d]) {
playerMgr[currentPlayer]->WalkInDirection(Direction::EAST);
}
}
+1
View File
@@ -32,6 +32,7 @@ protected:
//utilities
void NewPlayer(int index, std::string avatarName, int x, int y);
void SwitchToPlayer(int index);
//members
Delta delta;
+34 -4
View File
@@ -12,9 +12,20 @@ MainMenu::MainMenu() {
#ifdef DEBUG
cout << "entering MainMenu" << endl;
#endif
configUtil = GetSingletonPtr<ConfigUtility>();
surfaceMgr = GetSingletonPtr<SurfaceManager>();
surfaceMgr->Load("button", configUtil->String("interface") + "/button.bmp");
buttonMap["start"] = new Button(50, 50, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "start");
buttonMap["options"] = new Button(50, 100, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "options");
buttonMap["quit"] = new Button(50, 150, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "quit");
}
MainMenu::~MainMenu() {
for (auto it : buttonMap) {
delete it.second;
}
#ifdef DEBUG
cout << "leaving MainMenu" << endl;
#endif
@@ -37,7 +48,9 @@ void MainMenu::Update() {
}
void MainMenu::Render(SDL_Surface* const screen) {
//
for (auto it : buttonMap) {
it.second->DrawTo(screen);
}
}
//-------------------------
@@ -45,15 +58,32 @@ void MainMenu::Render(SDL_Surface* const screen) {
//-------------------------
void MainMenu::MouseMotion(SDL_MouseMotionEvent const& motion) {
//
for (auto it : buttonMap) {
it.second->MouseMotion(motion);
}
}
void MainMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) {
//
for (auto it : buttonMap) {
it.second->MouseButtonDown(button);
}
}
void MainMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
//
if (buttonMap["start"]->MouseButtonUp(button) == Button::State::HOVER) {
//TODO
SetNextScene(SceneList::INGAME);
cout << "start" << endl;
}
if (buttonMap["options"]->MouseButtonUp(button) == Button::State::HOVER) {
//TODO
cout << "options" << endl;
}
if (buttonMap["quit"]->MouseButtonUp(button) == Button::State::HOVER) {
//TODO
SetNextScene(SceneList::QUIT);
cout << "quit" << endl;
}
}
void MainMenu::KeyDown(SDL_KeyboardEvent const& key) {
+22 -3
View File
@@ -3,25 +3,44 @@
#include "base_scene.hpp"
#include "singleton.hpp"
#include "config_utility.hpp"
#include "surface_manager.hpp"
#include "button.hpp"
#include <string>
#include <map>
class MainMenu : public BaseScene {
public:
/* Public access members */
//Public access members
MainMenu();
virtual ~MainMenu();
protected:
/* Frame loop */
//Frame loop
virtual void FrameStart();
virtual void FrameEnd();
virtual void Update();
virtual void Render(SDL_Surface* const);
/* Event handlers */
//Event handlers
virtual void MouseMotion(SDL_MouseMotionEvent const&);
virtual void MouseButtonDown(SDL_MouseButtonEvent const&);
virtual void MouseButtonUp(SDL_MouseButtonEvent const&);
virtual void KeyDown(SDL_KeyboardEvent const&);
virtual void KeyUp(SDL_KeyboardEvent const&);
//utilities
typedef std::map<std::string, Button*> ButtonMap;
//singletons
ConfigUtility* configUtil;
SurfaceManager* surfaceMgr;
//members
ButtonMap buttonMap;
};
#endif
+5 -11
View File
@@ -1,8 +1,5 @@
#include "scene_manager.hpp"
#include "singleton.hpp"
#include "config_utility.hpp"
#include <stdexcept>
//-------------------------
@@ -39,19 +36,16 @@ void SceneManager::Init() {
if (SDL_Init(SDL_INIT_VIDEO))
throw(std::runtime_error("Failed to initialize SDL"));
GetSingletonPtr<ConfigUtility>()->Load("rsc/config.cfg");
configUtil = GetSingletonPtr<ConfigUtility>();
configUtil->Load("rsc/config.cfg");
//set the screen from the config file
int flags = SDL_HWSURFACE|SDL_DOUBLEBUF;
if (GetSingletonPtr<ConfigUtility>()->Boolean("screen.f")) {
if (configUtil->Boolean("screen.f")) {
flags |= SDL_FULLSCREEN;
}
BaseScene::SetScreen(
GetSingletonPtr<ConfigUtility>()->Integer("screen.w"),
GetSingletonPtr<ConfigUtility>()->Integer("screen.h"),
0,
flags
);
BaseScene::SetScreen(configUtil->Integer("screen.w"),configUtil->Integer("screen.h"),0,flags);
}
void SceneManager::Proc() {
+7 -2
View File
@@ -4,11 +4,14 @@
#include "scene_list.hpp"
#include "base_scene.hpp"
#include "singleton.hpp"
#include "config_utility.hpp"
#include "SDL/SDL.h"
class SceneManager {
public:
/* Public access members */
//Public access members
SceneManager();
~SceneManager();
@@ -17,11 +20,13 @@ public:
void Quit();
private:
/* Private access members */
//Private access members
void LoadScene(SceneList sceneIndex);
void UnloadScene();
BaseScene* activeScene;
ConfigUtility* configUtil;
};
#endif
+6 -3
View File
@@ -30,6 +30,8 @@ Splash::~Splash() {
void Splash::RunFrame() {
//skip any event handling here
SDL_Event event;
while(SDL_PollEvent(&event));
//draw the logo in the middle of the screen
int x = (GetScreen()->w - logo->GetClipW()) / 2;
@@ -40,15 +42,16 @@ void Splash::RunFrame() {
if (!loaded) {
LoadResources();
loaded = true;
}
if (clock() - start > CLOCKS_PER_SEC*3) {
SetNextScene(SceneList::INGAME);
SetNextScene(SceneList::MAINMENU);
}
}
void Splash::LoadResources() {
//load the resources during the splash screen
//load the global resources here
surfaceMgr->Load("font", configUtil->String("fonts") + "/pokemon_dark_font.bmp");
//TODO
loaded = true;
}
+1
View File
@@ -10,6 +10,7 @@ fonts = rsc/graphics/fonts
logos = rsc/graphics/logos
sprites = rsc/graphics/sprites
tilesets = rsc/graphics/tilesets
interface = rsc/graphics/interface
#debugging
debug = true
Binary file not shown.

After

Width:  |  Height:  |  Size: 88 KiB