Hi,
I’m new to SDL and I am loving it up until today. I am preparing to
develop an application that uses a touchscreen (hence why the mouse
pointer needs to be off for this app). I was developing on a normal PC
screen (not at the touchscreen) and all was working great. I have a toy
app that opens fullscreen and prints where I click on the screen. I
obviously enabled the mouse pointer on my normal screen so that I
could…ummm…see where I was clicking
However, when I suppress the mouse pointer, a weird things happens.
Instead of the origin of the screen being in the upper left corner, it
seems to move towards the center. Any click in the upper left of the
screen registers as (0,0). If I enable the pointer in fullscreen mode,
all works. If I disable the pointer but create it in windowed mode -
all works. It’s when both are done is when the weird problem occurs. I
even tried enabling and disabling in the code to see if tha w ould fix
it. Although by doing this I can see my mouse pointer blink and thus can
test it, I still get the same problem.
Below is the code. Can anyway assist? FYI- I am developing on Windows
XP using SDL 1.2.11 and MinGW 5.0.3.
- Damian----------------
#include <iostream.h>
#include <stdio.h>
#include <SDL.h>
#include <SDL_ttf.h>
#include <sge.h>
char* renderText(int, int, SDL_Surface*, char*);
int main(int argc, char *argv[])
{
SDL_Surface *screen = NULL;
SDL_Event event;
int quit = 0;
char buffer[50];
Uint32 fgcolor = sge_MapAlpha(255, 255, 255, 255);
Uint32 bgcolor = sge_MapAlpha(0, 0, 0, 0);
cout << "Initializing SDL.\n";
/* Initialize defaults */
if((SDL_Init(SDL_INIT_VIDEO) < 0))
{
cout << "Could not initialize SDL:" << SDL_GetError();
return 1;
}
cout << "SDL initialized.\n";
/* Clean up on exit */
atexit(SDL_Quit);
atexit(TTF_Quit);
/*
* Initialize the display to the desktop resolution (0,0 for
* first two parms to SDL_SetVideoMode), requesting a software
surface
*/
screen = SDL_SetVideoMode(0, 0, 0,
SDL_SWSURFACE|SDL_ANYFORMAT)|SDL_FULLSCREEN);
if (screen == NULL)
{
cout << "Couldn't set video mode: " << SDL_GetError();
return 1;
}
sprintf(buffer, "Screen set to resolution %d, %d\n", screen->w,
screen->h);
cout << buffer;
// hide the mouse pointer
SDL_ShowCursor(SDL_DISABLE);
while (quit == 0)
{
//Event Processing
while( SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
cout << SDL_GetKeyName(event.key.keysym.sym)
<< " key down is pressed\n";
break;
case SDL_KEYUP:
cout << SDL_GetKeyName(event.key.keysym.sym)
<< " key up is pressed\n";
if (strcmp(SDL_GetKeyName(event.key.keysym.sym),
“q”) == 0)
quit = 1;
break;
case SDL_QUIT:
quit = 1;
break;
case SDL_MOUSEBUTTONDOWN:
cout << "mouse click at " << event.button.x
<< ", " << event.button.y << “\n”;
sprintf(buffer, "%d, %d ", event.button.x,
event.button.y);
cout << renderText(0, 100, screen, buffer);
break;
} // switch
} // while EVENT Processing loop
} // while quit = 0
return 0;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface*
destination )
{
//Make a temporary rectangle to hold the offsets
SDL_Rect offset;
//Give the offsets to the rectangle
offset.x = x;
offset.y = y;
//Blit the surface
SDL_BlitSurface( source, NULL, destination, &offset );
}
char* renderText(int x, int y, SDL_Surface* screen, char* text)
{
SDL_Surface *message = NULL;
//The font that's going to be used
TTF_Font *font;
//The color of the font
// SDL_Color textColor = { 255, 255, 255 };
SDL_Color textColor = { 128, 128, 255 };
SDL_Color bgColor = { 0, 0, 0 };
//Initialize SDL_ttf
if( TTF_Init() == -1 )
{
return "error in TTF Init\n";
}
//Open the font
font = TTF_OpenFont( "font.ttf", 14 );
//If there was an error in loading the font
if( font == NULL )
{
return TTF_GetError();
}
message = TTF_RenderText_Shaded(font, text, textColor, bgColor);
//Apply to the screen
apply_surface( x, y, message, screen );
SDL_FreeSurface(message);
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return "Error in Flip\n";
}
return "Success -> renderText\n";
}