Jump to content

  • Log In with Google      Sign In   
  • Create Account

[SDL/C++] Asking player for text


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 selve   Members   -  Reputation: 145

Like
0Likes
Like

Posted 10 November 2012 - 06:26 PM

Hi guys.
I want to do in my SDL Game asking for text. For example when I'll come close to the doors, there will be a code and u will have to type it by a keyboard. How can I do it?

Sponsor:

#2 ultramailman   Prime Members   -  Reputation: 1571

Like
0Likes
Like

Posted 10 November 2012 - 11:00 PM

Hello
I've never tried this before... but consider this:

Make a doubly linked list.
Every time a key is pressed, add the pressed button's to the end of the list. Then, scan backwards to see if the last few buttons match what you are looking for. If it doesn't match, do nothing. It it matches, clear the list.

Example: you want to match the string "hi"
list: []
press a
add a to list
list: [a]
does the last 2 characters of list match "hi"? i.e. does [a] match [h, i]? no.

list: [a]
press h
add h to list
list: [a, h]
does the last 2 characters of list match "hi"? i.e. does [a, h] match [h, i]? no.

list: [a, h]
press d
add d to list
list: [a, h, d]
does the last 2 characters of list match "hi"? i.e. does [h, d] match [h, i]? no.

list: [a, h, d]
press h
add h to list
list: [a, h, d, h]
does the last 2 characters of list match "hi"? i.e. does [d, h] match [h, i]? no.

list: [a, h, d, h]
press i (finally!)
add i to list
list: [a, h, d, h, i]
does the last 2 characters of list match "hi"? i.e. does [h, i] match [h, i]? yes! clear the list.

list: []

Thats how I might do this, no idea how the big games do it.

#3 kd7tck   Members   -  Reputation: 715

Like
1Likes
Like

Posted 10 November 2012 - 11:05 PM

Just use a state machine. Certain keys will shift what state you are in for example:

say the message is abc, you would start in a nuetral state. If in nuetral state if 'a' is pressed you enter A state. If in A state if 'b' is pressed you enter B state... and so on.

#4 selve   Members   -  Reputation: 145

Like
0Likes
Like

Posted 11 November 2012 - 02:54 AM

So if I would like to make asking for name should I do states for all characters? How it will look?

#5 kd7tck   Members   -  Reputation: 715

Like
-2Likes
Like

Posted 11 November 2012 - 04:05 AM

Make a tree of all possible combinations, map each tree node to a state. The tree paths are state stransitions triggered by an input event like pressing a key.
You start in a nuetral game state, when you get to the door trigger text enter state. In text_enter state loop around for text. When enter key is pressed tranisition to text check state. In text check state examine the text for equality to pre defined strings, if equality is found then transition to activated state. If equality is not found in text check state then transition back to neutral games state.


Here is a roughed out system done in sfml. Incomplete and buggy, but it should get accross the idea of what I mean.
Examples are in the code, so reverse engineering it shouldn't be too hard. The State_manager.hpp should have the examples for using this system.

state.hpp
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2012 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
//	you must not claim that you wrote the original software.
//	If you use this software in a product, an acknowledgment
//	in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
//	and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_STATE_HPP
#define SFML_STATE_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include "Export.hpp"
#include <iostream>
#include <string>
#include <map>

namespace sf
{
class State_Event;
class State_Manager;
////////////////////////////////////////////////////////////
/// \brief The Util State Abstract class.
///
////////////////////////////////////////////////////////////
class SFML_UTIL_API State
{
public :
	////////////////////////////////////////////////////////////
	/// \brief constructor
	///
	/// \param string name
	///
	////////////////////////////////////////////////////////////
	State(std::string name);
	////////////////////////////////////////////////////////////
	/// \brief destructor
	///
	////////////////////////////////////////////////////////////
	virtual ~State(){
		if(manager != 0)
			manager->unRegisterState(this);
		if(registered_events.size() > 0){
			it = registered_events.begin();
			(*it)->unRegisterState(this);
		}
	}


	//Virtual functions for assigning user desired functionality
	////////////////////////////////////////////////////////////
	/// \brief second thing to be called every game cycle
	///
	/// \param double current time in milliseconds
	///
	/// \return 0 if successfull
	////////////////////////////////////////////////////////////
	virtual int Update (const double time) = 0;
	////////////////////////////////////////////////////////////
	/// \brief first thing to be called every game cycle
	///
	/// \return 0 if successfull
	////////////////////////////////////////////////////////////
	virtual int Events () = 0;
	////////////////////////////////////////////////////////////
	/// \brief third thing to be called every game cycle
	///
	/// \param Image, canvas to draw on
	///
	/// \return 0 if successfull
	////////////////////////////////////////////////////////////
	virtual int Draw (Image& canvas) = 0;
	////////////////////////////////////////////////////////////
	/// \brief called once when state is done
	///
	/// \return 0 if successfull
	////////////////////////////////////////////////////////////
	virtual int CleanUp () = 0;
	////////////////////////////////////////////////////////////
	/// \brief called once when state is entered
	///
	/// \return 0 if successfull
	////////////////////////////////////////////////////////////
	virtual int Init () = 0;




	////////////////////////////////////////////////////////////
	/// \brief an event was triggered
	///
	/// \param int ID of event
	///
	/// \return bool true on success
	////////////////////////////////////////////////////////////
	bool trigger(int event_id);
	////////////////////////////////////////////////////////////
	/// \brief change state name
	///
	/// \param string new name
	///
	/// \return true if successful
	////////////////////////////////////////////////////////////
	bool setName(std::string name);
	////////////////////////////////////////////////////////////
	/// \brief get state name
	///
	/// \return string name
	////////////////////////////////////////////////////////////
	std::string getName();
	////////////////////////////////////////////////////////////
	/// \brief get next state name
	///
	/// \return string name, "" if none
	////////////////////////////////////////////////////////////
	std::string nextState();
	////////////////////////////////////////////////////////////
	/// \brief change DefaultNextStateName
	///
	/// \param string new name
	///
	/// \return true if successful
	////////////////////////////////////////////////////////////
	bool setDefaultNextStateName(std::string name);
	////////////////////////////////////////////////////////////
	/// \brief check if state is done, then transition to next state
	///
	/// \return bool ,this is the triggered variable
	////////////////////////////////////////////////////////////
	bool stateDone();

	////////////////////////////////////////////////////////////
	/// \brief register a state_event with this state
	///
	/// \param State_Event*
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void registerEvent(State_Event*);

	////////////////////////////////////////////////////////////
	/// \brief unregister a state_event from this state
	/// un-registers StateChange and removes event from registered_events
	/// \param State_Event*
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void unRegisterEvent(State_Event*);

	////////////////////////////////////////////////////////////
	/// \brief register a state transition from this state node
	///
	/// \param int event id
	///
	/// \param string name of next state
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void registerStateChange(int event_id, std::string next_state_name);

	////////////////////////////////////////////////////////////
	/// \brief unregister a state transition from this state node
	///
	/// \param int event id
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void unRegisterStateChange(int event_id);
	////////////////////////////////////////////////////////////
	/// \brief set a manager for this state
	///
	/// \param State_Manager*
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void setStateManager(State_Manager*);
	////////////////////////////////////////////////////////////
	/// \brief Change Active status, active status meaning is this state running?
	///
	/// \param bool, active status true or false, the isActive variable
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void setActiveStatus(bool);
	////////////////////////////////////////////////////////////
	/// \brief Get the Active status, active status meaning is this state running?
	///
	/// \return bool, the isActive variable
	////////////////////////////////////////////////////////////
	bool getActiveStatus();
	////////////////////////////////////////////////////////////
	/// \brief reset the trigger by setting it to false
	////////////////////////////////////////////////////////////
	void resetTrigger();
protected :
	//state identifier
	std::string state_name;
	std::vector <State_Event*> registered_events;
	std::vector <State_Event*>::iterator it;
	//state to state mappings based on event_id
	std::map <int, std::string> eventIdStateTrigger;
	//the next state to run after this state ends
	std::string next_state_name;
	//if next_state_name is no longer valid
	std::string default_next_state_name;
	//if triggered then state manager will transition to next state, can only be triggered if state is active
	bool triggered;
	//if this state is currently cycling with state manager
	bool isActive;
	//pointer to the state_manager for this state
	State_Manager* manager;
};


////////////////////////////////////////////////////////////
/// \brief A NULL State.
///
////////////////////////////////////////////////////////////
class SFML_UTIL_API NULLState : public State
{
public:
	NULLState() : State("")
	{
		;
	}
	~NULLState()
	{
		;
	}
	int Update (const double time)
	{
		return 0;
	}
	int Events ()
	{
		return 0;
	}
	int Draw (Image& canvas)
	{
		return 0;
	}
	int CleanUp ()
	{
		return 0;
	}
	int Init ()
	{
		return 0;
	}
protected:
	;
};
} // namespace sf

#endif // SFML_STATE_HPP

////////////////////////////////////////////////////////////
/// \class sf::State
/// \ingroup util
///
/// sf::State represents a single state within a finite state machine. This is an abstract class.
///
/// Example:
/// \code
///
///class IntroState : public sf::State
///{
///public:
///	std::string name;
///
///	IntroState(std::string n) : sf::State(n)
///	{
///		name = n;
///	}
///
///	~IntroState()
///	{
///		;
///	}
///
///	int Update (const double time)
///	{
///		std::cout << "Intro_Update_" << name << std::endl;
///		return 0;
///	}
///
///	int Events ()
///	{
///		std::cout << "Intro_Events_" << name << std::endl;
///		return 0;
///	}
///
///	int Draw (sf::Image& canvas)
///	{
///		std::cout << "Intro_Draw_" << name << std::endl;
///		return 0;
///	}
///
///	int CleanUp ()
///	{
///		std::cout << "Intro_Cleanup_" << name << std::endl;
///		return 0;
///	}
///
///	int Init ()
///	{
///		std::cout << "Intro_Init_" << name << std::endl;
///		return 0;
///	}
///};
///
/// \endcode
///
/// \see sf::State
///
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
/// \class sf::NULLState
/// \ingroup util
///
/// sf::NULLState represents a single state within no real functionality.
///
/// Example:
/// \code
///
/// \endcode
///
/// \see sf::NULLState
///
////////////////////////////////////////////////////////////






state_event.hpp
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2012 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
//	you must not claim that you wrote the original software.
//	If you use this software in a product, an acknowledgment
//	in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
//	and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_STATE_EVENT_HPP
#define SFML_STATE_EVENT_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include "Export.hpp"
#include <iostream>
#include <string>
#include <vector>

namespace sf
{
class State;
////////////////////////////////////////////////////////////
/// \brief The Util State_Event class.
///
////////////////////////////////////////////////////////////
class SFML_UTIL_API State_Event
{
public :
	////////////////////////////////////////////////////////////
	/// \brief constructor
	///
	/// \param int id
	///
	////////////////////////////////////////////////////////////
	State_Event(int id);

	////////////////////////////////////////////////////////////
	/// \brief destructor
	///
	////////////////////////////////////////////////////////////
	~State_Event();

	////////////////////////////////////////////////////////////
	/// \brief trigger this event, and trigger all registered states
	///
	/// \return bool true on success
	////////////////////////////////////////////////////////////
	bool trigger();

	////////////////////////////////////////////////////////////
	/// \brief obtain this event's ID
	///
	/// \return int Event ID
	////////////////////////////////////////////////////////////
	int getId();
	////////////////////////////////////////////////////////////
	/// \brief change this event's ID
	///
	/// \param int id
	///
	////////////////////////////////////////////////////////////
	void setId(int id);

	////////////////////////////////////////////////////////////
	/// \brief register a state with this event
	///
	/// \param State*
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void registerState(State*);

	////////////////////////////////////////////////////////////
	/// \brief unregister a state from this event
	///
	/// \param State*
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void unRegisterState(State*);
protected :
	int event_id;
	static std::vector<State*> registered_states;
	std::vector<State*>::iterator it;
};
} // namespace sf

#endif // SFML_STATE_EVENT_HPP

////////////////////////////////////////////////////////////
/// \class sf::State_Event
/// \ingroup util
///
/// sf::State_Event represents an event that can register with multiple states.
///
/// Example:
/// \code
/// //create new state_event and register it to a State
/// sf::State_Event* enter_key_press_event = new sf::State_Event(0);
/// state_pntr->registerEvent(enter_key_press_event);
///
/// //map event to other state
/// state_pntr->registerStateChange(enter_key_press_event->getId(), state_pntr_2->getName());
///
/// //trigger an event, activates the mapping made earlier
/// enter_key_press_event->trigger();
/// \endcode
///
/// \see sf::State_Event
///
////////////////////////////////////////////////////////////







state_manager.hpp
////////////////////////////////////////////////////////////
//
// SFML - Simple and Fast Multimedia Library
// Copyright (C) 2007-2012 Laurent Gomila (laurent.gom@gmail.com)
//
// This software is provided 'as-is', without any express or implied warranty.
// In no event will the authors be held liable for any damages arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it freely,
// subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented;
//	you must not claim that you wrote the original software.
//	If you use this software in a product, an acknowledgment
//	in the product documentation would be appreciated but is not required.
//
// 2. Altered source versions must be plainly marked as such,
//	and must not be misrepresented as being the original software.
//
// 3. This notice may not be removed or altered from any source distribution.
//
////////////////////////////////////////////////////////////
#ifndef SFML_STATE_MANAGER_HPP
#define SFML_STATE_MANAGER_HPP
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include "Export.hpp"
#include <iostream>
#include <string>
#include <map>
#include <vector>

namespace sf
{

class State;
class Image;
class Clock;
class Time;
class NULLState;
////////////////////////////////////////////////////////////
/// \brief The Util State_Manager class.
///
////////////////////////////////////////////////////////////
class SFML_UTIL_API State_Manager
{
public :
	////////////////////////////////////////////////////////////
	/// \brief constructor
	///
	////////////////////////////////////////////////////////////
	State_Manager();
	////////////////////////////////////////////////////////////
	/// \brief destructor
	///
	////////////////////////////////////////////////////////////
	~State_Manager();
	////////////////////////////////////////////////////////////
	/// \brief register a state with this state manager
	///
	/// \param State*
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void registerState(State*);
	////////////////////////////////////////////////////////////
	/// \brief unregister a state with this state manager
	///
	/// \param State*
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void unRegisterState(State*);
	////////////////////////////////////////////////////////////
	/// \brief cycle one itteration, checks all states and runs them for one pass
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void Cycle();
	////////////////////////////////////////////////////////////
	/// \brief ends state machine, and cleans everyhting up
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void Halt();
	////////////////////////////////////////////////////////////
	/// \brief set current state
	///
	/// \param State*
	///
	/// \return void
	////////////////////////////////////////////////////////////
	void setCurrentState(State*);

	////////////////////////////////////////////////////////////
	/// \brief get current state
	///
	/// \return State*
	////////////////////////////////////////////////////////////
	State* getCurrentState();

protected :
	std::vector<State*> states;
	std::string currentState;
	std::string nextState;
	std::string previousState;
	std::vector<State*>::iterator it;
	State* baseState;
	Image* backBuffer;
	Clock* clock;
	Time* time;
};
} // namespace sf

#endif // SFML_STATE_MANAGER_HPP

////////////////////////////////////////////////////////////
/// \class sf::State_Manager
/// \ingroup util
///
/// sf::State_Manager manages a collection of states
///
/// Example:
/// \code
///
///class IntroState : public sf::State
///{
///public:
///	std::string name;
///
///	IntroState(std::string n) : sf::State(n)
///	{
///		name = n;
///	}
///
///	~IntroState()
///	{
///		;
///	}
///
///	int Update (const double time)
///	{
///		std::cout << "Intro_Update_" << name << std::endl;
///		return 0;
///	}
///
///	int Events ()
///	{
///		std::cout << "Intro_Events_" << name << std::endl;
///		return 0;
///	}
///
///	int Draw (sf::Image& canvas)
///	{
///		std::cout << "Intro_Draw_" << name << std::endl;
///		return 0;
///	}
///
///	int CleanUp ()
///	{
///		std::cout << "Intro_Cleanup_" << name << std::endl;
///		return 0;
///	}
///
///	int Init ()
///	{
///		std::cout << "Intro_Init_" << name << std::endl;
///		return 0;
///	}
///};
///
///
///
///int main() {
///
///	sf::State* sp;
///	sf::State* sp2;
///
///
///	//create two new classes from abstract type State
///	IntroState *sptr = new IntroState("Intro");
///	IntroState *nsptr = new IntroState("Level1");
///
///
///	//create event trigger
///	sf::State_Event* enter_key_press_event = new sf::State_Event(0);
///
///
///	//access Parent attributes by using State pointer
///	sp = sptr;
///	sp2 = nsptr;
///
///	sp->registerEvent(enter_key_press_event);
///	sp->registerStateChange(enter_key_press_event->getId(), sp2->getName());
///
///	//assign states to state manager
///	sf::State_Manager* sm = new sf::State_Manager();
///	sm->registerState(sp);
///	sm->registerState(sp2);
///	sm->setCurrentState(sp);
///
///
///	while(true)
///	{
///	 //detect action for trigger
///	 if(sf::Keyboard::isKeyPressed(sf::Keyboard::LControl))
///	 {
///	  enter_key_press_event->trigger();
///	 }
///
///	 sm->Cycle();//state cycle
///
///
///	 //exit game loop
///	 if(sm->getCurrentState()->getName() == nsptr->getName())
///	 {
///	  sm->Halt();
///	  break;
///	 }
///	}
///
///	//clean up
///	sptr->unRegisterEvent(enter_key_press_event);
///	enter_key_press_event->unRegisterState(sptr);
///	delete enter_key_press_event;
///	delete sptr;
///	delete nsptr;
///	delete sm;
///	return 0;
///}
///
/// \endcode
///
/// \see sf::State_Manager
///
////////////////////////////////////////////////////////////

Edited by kd7tck, 11 November 2012 - 03:36 PM.


#6 BaneTrapper   Members   -  Reputation: 1200

Like
4Likes
Like

Posted 11 November 2012 - 06:10 AM

First of all, what kind of game is it, 2D or 3D?
case 2D: "Should check if the character is just near the ( Code entering device ) and/or if he is facing it too"
switch user to entering text mode, and allow him to go back from it, if he cant figure out the code.
In text entering mode, just populate a std::string with any key pressed.
if(MyEnteredCode >= 4)
{
    if(MyEnteredCode == TheCorrectCodeString)
    {
	    //Open the doors
    }
    else
    {
	    //Notify the user code is invalid
	    //Empty the string
    }
}
//Link to sdl TextInput tutorial they have.
http://wiki.libsdl.org/moin.cgi/Tutorials/TextInput
From LazyFoo tutorials i used to follow and it tought me well
http://lazyfoo.net/SDL_tutorials/lesson23/index.php

Edited by BaneTrapper, 11 November 2012 - 06:10 AM.

Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


#7 selve   Members   -  Reputation: 145

Like
0Likes
Like

Posted 11 November 2012 - 09:07 AM

Thanks ;)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS