Custom Mouse Cursor and OpenGL/SDL

Hello, I am wondering how do I go about making a custom mouse cursor
work in SDL/OpenGL? As of now I am guessing disable mouse cursor, switch
to glOrtho() mode, render a fullscreen image with glRasterPos2i() to
position the mouse cursor at mouseX, mouseY?

Thanks!

Why fullscreen? And why bother with glRasterPos2i() when in ortho mode? :slight_smile:

All you have to do is pass the view size in pixels to glOrtho(), and handle
the “wrong way” Y axis in one way or another. In this case, it’s probably best
to just swap the 0 and height to glOrtho() to get (0, 0) in the top-left
corner, as it usually is with other APIs, SDL included.On Tuesday 02 November 2010, at 05.15.55, “Mars_999” wrote:

Hello, I am wondering how do I go about making a custom mouse cursor
work in SDL/OpenGL? As of now I am guessing disable mouse cursor, switch
to glOrtho() mode, render a fullscreen image with glRasterPos2i() to
position the mouse cursor at mouseX, mouseY?


//David Olofson - Consultant, Developer, Artist, Open Source Advocate

.— Games, examples, libraries, scripting, sound, music, graphics —.
| http://olofson.net http://olofsonarcade.com http://kobodeluxe.com |
’---------------------------------------------------------------------’

I figured I might as well throw a mouse cursor into Kobo II, as I’ll need one
sooner or later anyway, so here goes:

Matrix setup (origo at bottom-left, OpenGL style):
–8<-----------------------------------------------------------------
MatrixMode(PROJECTION);
LoadIdentity();
Ortho(0, screen.w, 0, screen.h, 0, 10);
MatrixMode(MODELVIEW);
LoadIdentity();
–>8-----------------------------------------------------------------

In the SDL event handling switch:
–8<-----------------------------------------------------------------
case SDL.MOUSEMOTION
{
mousex = ev.x;
mousey = screen.h - ev.y;
}
–>8-----------------------------------------------------------------

Rendering:
–8<-----------------------------------------------------------------
local cursorsize = 48;
Enable(BLEND);
BlendFunc(SRC_ALPHA, ONE);
Disable(TEXTURE_2D);
Begin(TRIANGLES);
Color(1, 1, 1, 1);
Vertex(mousex, mousey);
Color(1, 1, 0, 0);
Vertex(mousex + cursorsize * sin(PI/4),
mousey - cursorsize * sin(PI/4));
Vertex(mousex, mousey - cursorsize);
End();
–>8-----------------------------------------------------------------

(No, that’s not C. It’s EEL - but it’s still SDL and OpenGL. ;-)On Tuesday 02 November 2010, at 12.45.29, David Olofson <@David_Olofson> wrote:

On Tuesday 02 November 2010, at 05.15.55, “Mars_999” wrote:

Hello, I am wondering how do I go about making a custom mouse cursor
work in SDL/OpenGL? As of now I am guessing disable mouse cursor, switch
to glOrtho() mode, render a fullscreen image with glRasterPos2i() to
position the mouse cursor at mouseX, mouseY?

Why fullscreen? And why bother with glRasterPos2i() when in ortho mode? :slight_smile:

All you have to do is pass the view size in pixels to glOrtho(), and handle
the “wrong way” Y axis in one way or another. In this case, it’s probably
best to just swap the 0 and height to glOrtho() to get (0, 0) in the
top-left corner, as it usually is with other APIs, SDL included.


//David Olofson - Consultant, Developer, Artist, Open Source Advocate

.— Games, examples, libraries, scripting, sound, music, graphics —.
| http://olofson.net http://olofsonarcade.com http://kobodeluxe.com |
’---------------------------------------------------------------------’