3 Replies - 1440 Views - Last Post: 12 December 2012 - 11:25 PM Rate Topic: -----

#1 primecodez  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-December 12

Placement of SDL_Surface not going where I want it to go

Posted 11 December 2012 - 10:53 AM

Attached File  ScreenShotOfProgram.pdf (265.18K)
Number of downloads: 49

So I tried making a simple program that has a button on screen and when the user hovers there mouse over the button it will change color... I put the button location in a SDL_Rect and the blitted it to the screen. Now I try to use an if statement to test whether the mouse is within the button barriers... I ran the program and the button was on screen but when I hovered my mouse over it the button didn't change color. I moved my mouse over to a part of the screen to the left of the button and the button changed colors!

I did a test, putting the cords of my mouse in a string and blitting it as a message and when I put my cursor to the part of the screen that changes the color. The location is at where I wanted the button to go; however, the button is about 100 pixels down and 100 to the right of that location. Why is it that SDL is putting the surface not where i want it?

Here is the code:

#include "stdafx.h"

//screen dimensions
const int screen_width = 800;
const int screen_height = 600;
const int screen_bpp = 32;

//button location
SDL_Rect buttonLocation;

//surfaces
SDL_Surface* background = NULL;
SDL_Surface* paddle = NULL;
SDL_Surface* button = NULL;
SDL_Surface* button2 = NULL;
SDL_Surface* ball = NULL;
SDL_Surface* message = NULL;


SDL_Event event;

TTF_Font* font = NULL;

SDL_Color textColor = {255,255,255};


int main(int argc, char *args[])
{

	//quit for the event loop and running for the timer
	bool quit = false;
	bool running = false;
	bool mouseMoving = false;

	//ints to hold the x and y cords of mouse
	int xCord= 0, yCord= 0;

	buttonLocation.x = 200;
	buttonLocation.y = 200;

	// initialize sdl and ttf
	if (sdl_initWithScreenDimensions(screen_width, screen_height, screen_bpp) == false)
	{
		return 1;
	}

	//load the font
	font = TTF_OpenFont("newfont.ttf", 22);

	//load the surfaces
	background = load_image("background.png");
	button = load_image("button.png");
	button2 = load_image("button2.png");


	//apply the initial surfaces
	apply_surface(0, 0, background, screen);
	apply_surface(buttonLocation.x, buttonLocation.y, button, screen);

	//get the Timer
	Timer myTimer;

	//start the clock
	myTimer.start();
	while(quit == false)
	{
		while (SDL_PollEvent(&event))
		{
			if (event.type == SDL_QUIT)
			{
				quit = true;
			}
			if (event.type == SDL_MOUSEBUTTONDOWN)
			{
				mouseMoving = true;
	
			}
			if (event.type == SDL_MOUSEMOTION)
			{
				int x, y;
				mouseMoving == true;
				x = event.motion.x;
				y = event.motion.y;
				//if x cord of mouse is within x cords of button and y cords
				if ((x >= 200 && (x <= 200+(button->w)))&& (y >=200 && (y <= 200+(button->h))))
				{
					//do this
					apply_surface(0, 0, background, screen);
					apply_surface((screen_width - button->w)/2, (screen_height - button->h)/2, button2, screen);
				}
				else
				{
					//do this
					apply_surface(0, 0, background, screen);
					apply_surface((screen_width - button->w)/2, (screen_height - button->h)/2, button, screen);
				}

			}

		}

		if (mouseMoving== true)
		{
			std::stringstream mouseCords;
			xCord = event.motion.x;
			yCord = event.motion.y;
			mouseCords<< "Mouse Location: ("<< xCord << ","<< yCord << ")";
			message = TTF_RenderText_Solid(font, mouseCords.str().c_str(), textColor);

			apply_surface(10, 10, message, screen);


			SDL_FreeSurface(message);
		}

		if (SDL_Flip(screen) == -1)
		{
			return 1;
		}
	}

	return 0;

}


just a note:
There is an attachment of the photo at top of screen.
apply_surface() takes the x coords and then the source surface and the destination surface...

Is This A Good Question/Topic? 0
  • +

Replies To: Placement of SDL_Surface not going where I want it to go

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1369
  • View blog
  • Posts: 4,737
  • Joined: 19-February 09

Re: Placement of SDL_Surface not going where I want it to go

Posted 12 December 2012 - 11:07 PM

This is a different position.

089             apply_surface((screen_width - button->w)/2, (screen_height - button->h)/2, button2, screen);


Was This Post Helpful? 0
  • +
  • -

#3 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 810
  • View blog
  • Posts: 2,405
  • Joined: 20-March 10

Re: Placement of SDL_Surface not going where I want it to go

Posted 12 December 2012 - 11:10 PM

already answered here

http://www.cplusplus...beginner/87912/

sorry #define

regards

Snoopy.
Was This Post Helpful? 1
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1369
  • View blog
  • Posts: 4,737
  • Joined: 19-February 09

Re: Placement of SDL_Surface not going where I want it to go

Posted 12 December 2012 - 11:25 PM

No problem, it wasn't a long answer anyway. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1