• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
selve

[SDL/C++] Asking player for text

6 posts in this topic

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?
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
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.
1

Share this post


Link to post
Share on other sites
So if I would like to make asking for name should I do states for all characters? How it will look?
0

Share this post


Link to post
Share on other sites
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
[CODE]
////////////////////////////////////////////////////////////
//
// 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
///
////////////////////////////////////////////////////////////
[/CODE]






state_event.hpp
[CODE]
////////////////////////////////////////////////////////////
//
// 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
///
////////////////////////////////////////////////////////////
[/CODE]







state_manager.hpp
[CODE]
////////////////////////////////////////////////////////////
//
// 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
///
////////////////////////////////////////////////////////////
[/CODE] Edited by kd7tck
-2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0