The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Simple use of SDL_GetRelativeMouseState
gmen


Joined: 14 Sep 2014
Posts: 2
Hello all!
In the following program, which compiles without errors, I constantly get an output of x/y 0 0. Why?
Code:
#include <iostream>
using namespace std;

#include "SDL.h"
#undef main

int x,y;

int main()
{

   SDL_GetRelativeMouseState(&x,&y);
   while(1)
   {
      SDL_PumpEvents();
      SDL_GetRelativeMouseState(&x,&y);
      cout<<"x/y : "<<x<<" "<<y<<endl;
      cout<<"Press any key to continue...";
      cin.get();
      cout<<endl;
   }
   return 0;
}
gmen


Joined: 14 Sep 2014
Posts: 2
I really thought this would be replied quickly due to being simple, but it seems its not the case.

Let me re-phrase my question then: Can somebody give me a program, preferably short, that takes raw mouse input and prints it on the screen in some way?
Naith


Joined: 03 Jul 2014
Posts: 158
I've made a small program which prints out the mouse position in the console.
Hope it works the way you want. Smile

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;
}