[SDL] Simple use of SDL_GetRelativeMouseState

Naith Danielane85 at hotmail.com
Sat Sep 20 14:22:42 PDT 2014


I've made a small program which prints out the mouse position in the console.
Hope it works the way you want. :)

Code:


Code:

#include "SDL.h"

#include <iostream>

SDL_Window* m_pWindow = NULL;
SDL_Renderer* m_pRenderer = NULL;

SDL_Event m_Event;

bool m_Running = true;

int m_MouseX = 0;
int m_MouseY = 0;

int main(int argv, char* args[])
{
	// Initialize SDL
	if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
	{
		// If SDL for some reason fails to initialize
		printf("Error: failed to initialize SDL\n");
	}

	else
	{
		// Create the SDL window
		m_pWindow = SDL_CreateWindow("Mouse Position", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN);

		// If the SDL window has been successfully created
		if(m_pWindow)
		{
			// Create the SDL renderer	
			m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, SDL_RENDERER_ACCELERATED);
		}
	}
	
	while(m_Running)
	{
		while(SDL_PollEvent(&m_Event))
		{
			switch(m_Event.type)
			{
			case SDL_QUIT:
				{
					m_Running = false;
					break;
				}

			case SDL_MOUSEMOTION:
				{
					m_MouseX = m_Event.motion.x;
					m_MouseY = m_Event.motion.y;
					break;
				}
			}
		}

		// Print out the mouse position in the console
		printf("MouseX: %i, MouseY: %i\n", m_MouseX, m_MouseY);
	}

	// Destroy the SDL renderer
	if(m_pRenderer)
		SDL_DestroyRenderer(m_pRenderer);

	// Destroy the SDL window
	if(m_pWindow)
		SDL_DestroyWindow(m_pWindow);

	// Quit SDL
	SDL_Quit();

	return 0;
}








-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20140920/b7af29a4/attachment-0002.htm>


More information about the SDL mailing list