Patrick’s development blog

Getting mouse state in SDL

Posted in SDL / OpenGL by Patrick on June 23, 2009

I’ve decided to finally start a programming project after a very long break. I found the need to use mouse states when creating a Level editor for my RPG because regular events doesn’t really do what I want.

I want to add tiles fast and be able to hold the mouse key down, instead of clicking every time I want to add a tile.

Mouse states

int cx,cy;
Uint8 ms = SDL_GetMouseState(&cx, &cy);
// the left mouse key is pressed
} else if (ms & SDL_BUTTON(SDL_BUTTON_RIGHT)) {
// the right mouse key is pressed

generic stringstream function in c++

Posted in C++ by Patrick on February 9, 2008

Something i’ve found useful while programming in C++, is the use of std::stringstream to convert different datatypes to std::string. Fortunately, the process is the same for all types. That’s why a generic helper-function is preferable and will simplify the process considerably.

In order to use stringstreams, the library <sstream> has to be included. The following will create a reusable stringstream helper-function using templates.

template <typename T> std::string to_string(const T& value) {
std::stringstream os;
os << value;
return os.str();

The parameter contains the value that will be converted into a string, so you only have to assign a string to the function.

// assign a new string to the return value of to_string and show the contents
std::string str = to_string(5.12);
std::cout << str;