Jump to content

  • Log In with Google      Sign In   
  • Create Account


Variables Not Updating


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 LeftyGuitar   Members   -  Reputation: 163

Like
0Likes
Like

Posted 15 September 2013 - 01:02 PM

Hello,

 

I am having a issue where the value of my variable is not updating. I have it set to show when the mouse position of x and y is to be shown on the screen. However, neither the x nor the y value is updating, it stays at zero. I am wondering why it is not updating. I'll post the code where it does that. I can post more code if it is needed.

SDL_Surface* MouseDis[2];
SDL_Rect MouseDisRect[2];

int mx = 0;
int my = 0;

stringstream mousepos[2];
	mousepos[0] << mx;
	mousepos[1] << my;

MouseDis[0] = TTF_RenderText_Solid(Font,mousepos[0].str().c_str(),textColor);
	MouseDis[1] = TTF_RenderText_Solid(Font,mousepos[1].str().c_str(),textColor);
	MouseDisRect[0].x = 1;
	MouseDisRect[0].y = 30;
	MouseDisRect[0].w = 0;
	MouseDisRect[0].h = 0;
	MouseDisRect[1].x = 1;
	MouseDisRect[1].y = 60;
	MouseDisRect[1].w = 0;
	MouseDisRect[1].h = 0;

if(GameEvent.type == SDL_MOUSEMOTION || GameEvent.type == SDL_MOUSEBUTTONDOWN || GameEvent.type == SDL_MOUSEBUTTONUP)
			{
				SDL_GetMouseState(&mx,&my);
			}

SDL_BlitSurface(MouseDis[0],NULL,GameSurf,&MouseDisRect[0]);
			SDL_BlitSurface(MouseDis[1],NULL,GameSurf,&MouseDisRect[1]);

Again this is only part of the code. I can post if it is needed.



Sponsor:

#2 rip-off   Moderators   -  Reputation: 6876

Like
4Likes
Like

Posted 15 September 2013 - 05:25 PM

Here is what the code you posted does:

  • You initialise mx and my to zero
  • You append these values into the stringstreams
  • You render these stringstreams to SDL_Surfaces
  • If a mouse event occurred, you update the mx and my variables using SDL_GetMouseState()
  • You blit the surfaces to the game surface

Note that the order of operations is very important. You need to update the variables before appending them to the stringstream.


Edited by rip-off, 15 September 2013 - 05:26 PM.


#3 LeftyGuitar   Members   -  Reputation: 163

Like
-1Likes
Like

Posted 15 September 2013 - 06:48 PM

Thanks for the tip, however I think it is because I did not set the SDL_Rect x and y variables to the mouse x and y variables.



#4 ApochPiQ   Moderators   -  Reputation: 12394

Like
0Likes
Like

Posted 15 September 2013 - 07:31 PM

rip-off is correct.

#5 rip-off   Moderators   -  Reputation: 6876

Like
1Likes
Like

Posted 16 September 2013 - 04:43 AM

If your intended behaviour was to have the text follow the mouse like a cursor sprite, then yes you would want to update the destination rectangle's position too. You also might want to unconditionally call SDL_GetMouseState(), otherwise your cursor will snap back to the origin unless there has been mouse activity during that frame

 

On another note, you appear to be handling only a single event per frame. Your application should consume all available events every frame, or else it will feel unresponsive during more frantic action. You should consider making a local event variable only used in the loop that processes events. A basic example:

void processEvents(Game &game) {
    SDL_Event event;
    while(SDL_PollEvent(&event)) {
        if(event.type == SDL_QUIT) {
            game.exit();
        } else {
            // other events ...
        }
    }
}
 
int main(/* ... */) {
    Game game;
    while(game.isRunning()) {
        processEvents(game);
        runGameLogic(game);
        render(game);
    }
}


#6 LeftyGuitar   Members   -  Reputation: 163

Like
0Likes
Like

Posted 16 September 2013 - 04:51 PM

Well its not getting the x and y position of the mouse, so I'm not sure what I'm doing wrong. I got it to work in SDL 1, but with 2, it seems to be a bit more complicated.

Here's the revised code.

if(GameEvent.type == SDL_MOUSEMOTION || SDL_MOUSEBUTTONDOWN)
			{
				mState = SDL_GetMouseState(&mx,&my);
			}

		Update();
		Draw();

if(gamescreen == true)
	{
		mState = SDL_GetMouseState(&mx,&my);

		BallRect[0].x += (int)ball_velx[0];
		BallRect[0].y += (int)ball_vely[0];

		BallRect[1].x += (int)ball_velx[1];
		BallRect[1].y += (int)ball_vely[1];

		BallRect[2].x += (int)ball_velx[2];
		BallRect[2].y += (int)ball_vely[2];

		BallRect[3].x += (int)ball_velx[3];
		BallRect[3].y += (int)ball_vely[3];

		if(BallRect[0].x < 0 || BallRect[0].x + BallRect[0].w > MAX_WIDTH)
		{
			BallRect[0].x -= (int)ball_velx[0];
			ball_velx[0] *= -1;
		}

		if(BallRect[0].y < 0 || BallRect[0].y + BallRect[0].h > MAX_HEIGHT)
		{
			BallRect[0].y -= (int)ball_vely[0];
			ball_vely[0] *= -1;
		}

		if(BallRect[1].x < 0 || BallRect[1].x + BallRect[1].w > MAX_WIDTH)
		{
			BallRect[1].x -= (int)ball_velx[1];
			ball_velx[1] *= -1;
		}

		if(BallRect[1].y < 0 || BallRect[1].y + BallRect[1].h > MAX_HEIGHT)
		{
			BallRect[1].y -= (int)ball_vely[1];
			ball_vely[1] *= -1;
		}

		if(BallRect[2].x < 0 || BallRect[2].x + BallRect[2].w > MAX_WIDTH)
		{
			BallRect[2].x -= (int)ball_velx[2];
			ball_velx[2] *= -1;
		}

		if(BallRect[2].y < 0 || BallRect[2].y + BallRect[2].h > MAX_HEIGHT)
		{
			BallRect[2].y -= (int)ball_vely[2];
			ball_vely[2] *= -1;
		}

		if(BallRect[3].x < 0 || BallRect[3].x + BallRect[3].w > MAX_WIDTH)
		{
			BallRect[3].x -= (int)ball_velx[3];
			ball_velx[3] *= -1;
		}

		if(BallRect[3].y < 0 || BallRect[3].y + BallRect[3].h > MAX_HEIGHT)
		{
			BallRect[3].y -= (int)ball_vely[3];
			ball_vely[3] *= -1;
		}
	}
}

void Draw()
{
	if(titlescreen)
	{
		SDL_BlitSurface(Title,NULL,GameSurf,&TitleRect);
	}

	if(gamescreen == true)
	{
		SDL_FillRect(GameSurf,NULL,SDL_MapRGB(GameSurf->format,0,0,0));

		SDL_BlitSurface(BallImg[0],NULL,GameSurf,&BallRect[0]);
		SDL_BlitSurface(BallImg[1],NULL,GameSurf,&BallRect[1]);
		SDL_BlitSurface(BallImg[2],NULL,GameSurf,&BallRect[2]);
		SDL_BlitSurface(BallImg[3],NULL,GameSurf,&BallRect[3]);

		SDL_BlitSurface(ScoreText[1],NULL,GameSurf,&ScoreTextRect[1]);
		SDL_BlitSurface(ScoreText[0],NULL,GameSurf,&ScoreTextRect[0]);

		SDL_BlitSurface(MouseText[0],NULL,DebugSurf,&MouseTextRect[0]);
		SDL_BlitSurface(MouseText[1],NULL,DebugSurf,&MouseTextRect[1]);
		SDL_BlitSurface(MouseText[2],NULL,DebugSurf,&MouseTextRect[2]);
		SDL_BlitSurface(MouseText[3],NULL,DebugSurf,&MouseTextRect[3]);
	}
}


Sorry for posting a bit much, this isn't all the code, but it should be enough to help. I'm assuming its because I'm not updating it in a correct way, if I could figure out where I need to update it, that would help.



#7 rip-off   Moderators   -  Reputation: 6876

Like
0Likes
Like

Posted 17 September 2013 - 05:06 AM

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


#8 LeftyGuitar   Members   -  Reputation: 163

Like
0Likes
Like

Posted 17 September 2013 - 12:18 PM

Thanks for the help. The stylisitic code will help make the code look more organized. Also what I'm trying to do is display and get the coords of the mouse's x and y values. mx and my are set to zero. I want the position of x and y to contiunally update. The object is to shoo the balls with the mouse.

 

EDIT: I also forgot to note that I am trying to show the value of mx and my using stringstreams.

stringstream sMouse[2];

sMouse[0] << mx;
	sMouse[1] << my;

	MouseText[0] = TTF_RenderText_Solid(Font,"MouseX:",textColor);
	MouseTextRect[0].x = 1;
	MouseTextRect[0].y = 1;
	MouseTextRect[0].w = 0;
	MouseTextRect[0].h = 0;
	MouseText[1] = TTF_RenderText_Solid(Font,"MouseY:",textColor);
	MouseTextRect[1].x = 1;
	MouseTextRect[1].y = 20;
	MouseTextRect[1].w = 0;
	MouseTextRect[1].h = 0;
	MouseText[2] = TTF_RenderText_Solid(Font,sMouse[0].str().c_str(),textColor);
	MouseTextRect[2].x = 80;
	MouseTextRect[2].y = 1;
	MouseTextRect[2].w = 0;
	MouseTextRect[2].h = 0;
	MouseText[3] = TTF_RenderText_Solid(Font,sMouse[1].str().c_str(),textColor);
	MouseTextRect[3].x = 80;
	MouseTextRect[3].y = 20;
	MouseTextRect[3].w = 0;
	MouseTextRect[3].h = 0;

Not sure, but perhaps I am doing something wrong with this code?


Edited by LeftyGuitar, 18 September 2013 - 02:44 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS