فهرست منبع

menu pauses game, able to resume the game. exit game by pause menu

Remco 9 سال پیش
والد
کامیت
98f3645b6b
5فایلهای تغییر یافته به همراه82 افزوده شده و 47 حذف شده
  1. 2 2
      Button.cpp
  2. 69 45
      CrystalPoint.cpp
  3. 2 0
      CrystalPoint.h
  4. 1 0
      Cursor.cpp
  5. 8 0
      Menu.cpp

+ 2 - 2
Button.cpp

@@ -22,8 +22,8 @@ Button::Button(const std::string & text, Vec2f position, float width, float heig
 
 Button::~Button()
 {
-	if (action != nullptr)
-		delete action;
+	/*if (action != nullptr)
+		delete action;*/
 }
 
 void Button::draw(void)

+ 69 - 45
CrystalPoint.cpp

@@ -15,6 +15,9 @@ int CrystalPoint::width = 0;
 int CrystalPoint::height = 0;
 
 SoundSystem CrystalPoint::sound_system;
+bool state = false;
+
+
 
 void CrystalPoint::init()
 {
@@ -23,17 +26,10 @@ void CrystalPoint::init()
 	cursor = Cursor::getInstance();
 
 	menu = new Menu();
-	menu->AddMenuElement(new Text("Hello", Vec2f(10, 10)));
-	menu->AddMenuElement(new Button("Start", Vec2f(1920 / 2 - 50, 1080 / 2 - 25), 100, 50));
-	Button* b = new Button("Test", Vec2f(1920 / 2 - 50, 1080 / 2 - 100), 100, 50);
-	
-	b->addAction([](Button* b)
-	{
-		b->setColor(Vec3f(0, 255, 0));
-	});
-	menu->AddMenuElement(b);
+	buildMenu();
 
 	lastFrameTime = 0;
+	state = true;
 
 	glClearColor(0.7f, 0.7f, 1.0f, 1.0f);
 }
@@ -53,8 +49,10 @@ void CrystalPoint::draw()
 	glMatrixMode(GL_MODELVIEW);
 	glLoadIdentity();
 
-	//worldhandler->draw();
-	menu->draw();
+	
+	worldhandler->draw();
+	if(!state)
+		menu->draw();
 
 	glutSwapBuffers();
 }
@@ -66,41 +64,45 @@ void CrystalPoint::update()
 	float deltaTime = frameTime - lastFrameTime;
 	lastFrameTime = frameTime;
 
-	if (keyboardState.special[GLUT_KEY_LEFT] && !prevKeyboardState.special[GLUT_KEY_LEFT])
-		worldhandler->PreviousWorld();
-	if (keyboardState.special[GLUT_KEY_RIGHT] && !prevKeyboardState.special[GLUT_KEY_RIGHT])
-		worldhandler->NextWorld();
-	if (keyboardState.keys[27])
-		exit(0);
-
-	Player* player = Player::getInstance();
-
-	player->rotation.y += mouseOffset.x / 10.0f;
-	player->rotation.x += mouseOffset.y / 10.0f;
-	if (player->rotation.x > 90)
-		player->rotation.x = 90;
-	if (player->rotation.x < -90)
-		player->rotation.x = -90;
-
-	float speed = 10;
-
-	Vec3f oldPosition = player->position;
-	if (keyboardState.keys['a']) player->setPosition(0, deltaTime*speed, false);
-	if (keyboardState.keys['d']) player->setPosition(180, deltaTime*speed, false);
-	if (keyboardState.keys['w']) player->setPosition(90, deltaTime*speed, false);
-	if (keyboardState.keys['s']) player->setPosition(270, deltaTime*speed, false);
-	if (keyboardState.keys['q']) player->setPosition(1, deltaTime*speed, true);
-	if (keyboardState.keys['e']) player->setPosition(-1, deltaTime*speed, true);
-
-	if (!worldhandler->isPlayerPositionValid())
-		player->position = oldPosition;
-
-	//player->position.y = worldhandler->getHeight(player->position.x, player->position.z) + 1.7f;
-
-	//worldhandler->update(deltaTime);
+	if (state)
+	{
+		if (keyboardState.special[GLUT_KEY_LEFT] && !prevKeyboardState.special[GLUT_KEY_LEFT])
+			worldhandler->PreviousWorld();
+		if (keyboardState.special[GLUT_KEY_RIGHT] && !prevKeyboardState.special[GLUT_KEY_RIGHT])
+			worldhandler->NextWorld();
+		if (keyboardState.keys[27])
+			state = false;
+
+		Player* player = Player::getInstance();
+
+		player->rotation.y += mouseOffset.x / 10.0f;
+		player->rotation.x += mouseOffset.y / 10.0f;
+		if (player->rotation.x > 90)
+			player->rotation.x = 90;
+		if (player->rotation.x < -90)
+			player->rotation.x = -90;
+
+		float speed = 10;
+
+		Vec3f oldPosition = player->position;
+		if (keyboardState.keys['a']) player->setPosition(0, deltaTime*speed, false);
+		if (keyboardState.keys['d']) player->setPosition(180, deltaTime*speed, false);
+		if (keyboardState.keys['w']) player->setPosition(90, deltaTime*speed, false);
+		if (keyboardState.keys['s']) player->setPosition(270, deltaTime*speed, false);
+		if (keyboardState.keys['q']) player->setPosition(1, deltaTime*speed, true);
+		if (keyboardState.keys['e']) player->setPosition(-1, deltaTime*speed, true);
+
+		if (!worldhandler->isPlayerPositionValid())
+			player->position = oldPosition;
+		player->position.y = worldhandler->getHeight(player->position.x, player->position.z) + 1.7f;
+		worldhandler->update(deltaTime);
+	}	
+	else
+	{
+		menu->update();
+		cursor->update(cursor->mousePosition + mouseOffset);
+	}
 
-	menu->update();
-	cursor->update(cursor->mousePosition + mouseOffset);
 
 	mouseOffset = Vec2f(0, 0);
 	prevKeyboardState = keyboardState;
@@ -109,6 +111,28 @@ void CrystalPoint::update()
 	sound_system.SetListener(player->position, Vec3f(), Vec3f());
 }
 
+void CrystalPoint::buildMenu()
+{
+	Button* start = new Button("Resume", Vec2f(1920 / 2 - 50, 1080 / 2 - 30), 100, 50);
+	auto toWorld = [](Button* b)
+	{
+		state = true;
+	};
+	start->addAction(toWorld);
+	menu->AddMenuElement(start);
+
+
+	Button* test = new Button("Exit", Vec2f(1920 / 2 - 50, 1080 / 2 + 30), 100, 50);
+	test->addAction([](Button* b)
+	{
+		exit(0);
+	});
+	menu->AddMenuElement(test);
+	Text* t = new Text("Pause", Vec2f(1920 / 2 - Util::glutTextWidth("Pause") / 2, 1080 / 2 - 75));
+	t->setColor(Vec3f(255, 255, 0));
+	menu->AddMenuElement(t);
+}
+
 
 
 KeyboardState::KeyboardState()

+ 2 - 0
CrystalPoint.h

@@ -41,7 +41,9 @@ public:
 	float lastFrameTime;
 
 	static SoundSystem& GetSoundSystem() { return sound_system; }
+	
 
 private:
 	static SoundSystem sound_system;
+	void buildMenu();
 };

+ 1 - 0
Cursor.cpp

@@ -14,6 +14,7 @@ Cursor::Cursor()
 
 Cursor::~Cursor()
 {
+	
 }
 
 Cursor* Cursor::getInstance(void)

+ 8 - 0
Menu.cpp

@@ -25,6 +25,14 @@ void Menu::draw(void)
 	glDisable(GL_DEPTH_TEST);
 	glDisable(GL_TEXTURE_2D);
 
+	glColor4f(60/255.0f, 60/255.0f, 60/255.0f, 0.8f);
+	glBegin(GL_QUADS);
+	glVertex2f(0,0);
+	glVertex2f(0, CrystalPoint::height);
+	glVertex2f(CrystalPoint::width, CrystalPoint::height);
+	glVertex2f(CrystalPoint::width, 0);
+	glEnd();
+
 
 	for (MenuElement* e : elements)
 	{