SDL_Surface not going where I place it on screen...

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?

CODE:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#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;

}


NOTE:
apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL);

That is the prototype for apply_surface :) kk ty!
You are drawing the button in the middle of the screen but when you check if the pointer is inside the button you assume the top left corner of the button is at position (200,200).

On line 81 you are using == when it should be =.
Last edited on
ty! lol
Topic archived. No new replies allowed.