• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
LeftyGuitar

Variables Not Updating

7 posts in this topic

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.

0

Share this post


Link to post
Share on other sites

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.

-1

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites
You haven't really explained what you are trying to do, so we would have to guess.
 
In the code you posted, [i]mx[/i] and [i]my[/i] 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
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0