You haven't really explained what you are trying to do, so we would have to guess.
In the code you posted, mx and my are only used as arguments to SDL_GetMouseState(), so whether they are being updated or not is almost irrelevant as the values do not appear to be used. You might be able to use your debugger to confirm that they are being updated.
Sometimes it can be easier to create a small test program, rather than trying to debug such issues in the context of a bigger program. Here is a simple program demonstrating how SDL_GetMouseState can be used:
#include <iostream>
#include <cstdlib>
#include "SDL.h"
int main(int, char**) {
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cerr << "Failed to initialise SDL: " << SDL_GetError() << std::endl;
return 1;
}
std::atexit(&SDL_Quit);
SDL_Surface *screen = SDL_SetVideoMode(400, 400, 0, SDL_SWSURFACE);
if(!screen) {
std::cerr << "Failed to set video mode: " << SDL_GetError() << std::endl;
return 1;
}
SDL_ShowCursor(SDL_DISABLE);
bool running = true;
while(running) {
SDL_Event event;
while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT) {
running = false;
} else if(event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE) {
running = false;
}
}
int x, y;
SDL_GetMouseState(&x, &y);
Uint32 black = SDL_MapRGB(screen->format, 0, 0, 0);
SDL_FillRect(screen, nullptr, black);
int size = 10;
SDL_Rect mouse = { x, y, size, size };
Uint32 red = SDL_MapRGB(screen->format, 0xff, 0, 0);
SDL_FillRect(screen, &mouse, red);
SDL_Flip(screen);
}
return 0;
}
This uses the older version of SDL, it has been a while since I tried SDL 2.
On a stylistic note, when you have an array like that, consider using loops rather than copying and pasting code. For example:
const int NUM_BALLS = 4;
SDL_Rect BallRect[NUM_BALLS];
int ball_velx[NUM_BALLS];
int ball_vely[NUM_BALLS];
for(int i = 0 ; i < NUM_BALLS ; ++i)
{
BallRect[i].x += (int)ball_velx[i];
BallRect[i].y += (int)ball_vely[i];
if(BallRect[i].x < 0 || BallRect[i].x + BallRect[i].w > MAX_WIDTH)
{
BallRect[i].x -= (int)ball_velx[i];
ball_velx[i] *= -1;
}
if(BallRect[i].y < 0 || BallRect[i].y + BallRect[i].h > MAX_HEIGHT)
{
BallRect[i].y -= (int)ball_vely[i];
ball_vely[i] *= -1;
}
}
// Later
SDL_FillRect(GameSurf,NULL,SDL_MapRGB(GameSurf->format,0,0,0));
for(int i = 0 ; i < NUM_BALLS ; ++i)
{
SDL_BlitSurface(BallImg[i],NULL,GameSurf,&BallRect[i]);
}
for(int i = 0 ; i < NUM_SCORES ; ++i)
{
SDL_BlitSurface(ScoreText[i],NULL,GameSurf,&ScoreTextRect[i]);
}
for(int i = 0 ; i < NUM_MOUSE_TEXTS ; ++i)
{
SDL_BlitSurface(MouseText[i],NULL,DebugSurf,&MouseTextRect[i]);
}
Also, when you have multiple arrays of the same size about the same objects (e.g. ball "rect", ball velocity x, ball velocity y), consider using a struct or class:
const int NUM_BALLS = 4;
const int BALL_WIDTH = 10;
const int BALL_HEIGHT = BALL_WIDTH;
struct Ball {
int positionX;
int positionY;
int velocityX;
int velocityY;
SDL_Surface *image;
};
Ball balls[NUM_BALLS];
for(int i = 0 ; i < NUM_BALLS ; ++i)
{
balls[i].positionX += balls[i].velocityX;
balls[i].positionY += balls[i].velocityY;
if(balls[i].positionX < 0 || balls[i].positionX + BALL_WIDTH > MAX_WIDTH)
{
balls[i].positionX -= balls[i].velocityX;
balls[i].velocityX *= -1;
}
if(balls[i].positionY < 0 || balls[i].positionY + BALL_HEIGHT > MAX_HEIGHT)
{
balls[i].positionY -= balls[i].velocityY;
balls[i].velocityY *= -1;
}
}
// Later
for(int i = 0 ; i < NUM_BALLS ; ++i)
{
SDL_Rect ballLocation = { balls[i].positionX, balls[i].positionY, BALL_WIDTH, BALL_HEIGHT };
SDL_BlitSurface(balls[i].image, NULL, GameSurf, &ballLocation);
}
We can also use a C++ feature called "references" to remove some of the redundancy from these expressions:
for(int i = 0 ; i < NUM_BALLS ; ++i)
{
Ball &ball = balls[i];
ball.positionX += ball.velocityX;
ball.positionY += ball.velocityY;
if(ball.positionX < 0 || ball.positionX + BALL_WIDTH > MAX_WIDTH)
{
ball.positionX -= ball.velocityX;
ball.velocityX *= -1;
}
if(ball.positionY < 0 || ball.positionY + BALL_HEIGHT > MAX_HEIGHT)
{
ball.positionY -= ball.velocityY;
ball.velocityY *= -1;
}
}
// Later
for(int i = 0 ; i < NUM_BALLS ; ++i)
{
const Ball &ball = balls[i];
SDL_Rect ballLocation = { ball.positionX, ball.positionY, BALL_WIDTH, BALL_HEIGHT };
SDL_BlitSurface(ball.image, NULL, GameSurf, &ballLocation);
}
Note I haven't tested the last few examples so check them carefully if you want to use them