鼠标在SDL/C 中不起作用
Mouse Over in SDL/C++ not working
我有一个简单的脚本,当鼠标悬停在第一个初始按钮上时,我希望该脚本加载第二个按钮。但是它不会返回true,所以我似乎无法使它起作用。
这是我的班级检查情况:
class Button
{
private:
int m_x, m_y;
int m_width, m_height;
public:
Button(int x, int y, int width, int height)
{
m_x = x;
m_y = y;
m_width = width;
m_height = height;
}
bool IsIn( int mouseX, int mouseY )
{
if (((mouseX > m_x) && (mouseX < m_x + m_width))
&& ((mouseY > m_y) && (mouseY < m_y + m_height ) ) ) {
return true;
} else {
return false;
}
}
void Render(SDL_Surface *source,SDL_Surface *destination)
{
SDL_Rect offset;
offset.x = m_x;
offset.y = m_y;
offset.w = m_width;
offset.h = m_height;
source = IMG_Load("button.png");
SDL_BlitSurface( source, NULL, destination, &offset );
}
};
它是我试图开始工作的IsIn
功能...在我的主循环中,我有:
while(!quit){
while( SDL_PollEvent( &event ) )
switch( event.type ){
case SDL_QUIT: quit = true; break;
case SDL_MOUSEMOTION: mouseX = event.motion.x; mouseY = event.motion.y; break;
}
Button btn_quit(screen->w/2,screen->h/2,0,0);
btn_quit.Render(menu,screen);
if(btn_quit.IsIn(mouseX,mouseY)){
Button btn_settings(screen->w/2,screen->h/2+70,0,0);
btn_settings.Render(menu,screen);
}
SDL_Quit
工作正常,但是当我将鼠标悬停在 btn_quit
按钮上时,我似乎无法在案例语句之后返回true。有什么想法可能是?
,因为 btn_quit
没有宽度或高度,因此您永远无法在其内部。
Button btn_quit(screen->w/2,screen->h/2,0,0);
您的检查将失败,因为您的鼠标位置永远不会是>x && <x+0
或>y && <y+0
。
也许是一种更好的方法是定义按钮的位置,并允许从已加载的图像中获取尺寸?
class Button
{
public:
// Construct the button with an image name, and position on screen.
// This is important, your existing code loaded the image each render.
// That is not necessary, load it once when you construct the class.
Button(std::string name, int x, int y) : img(IMG_Load(name))
{
if(img) // If the image didn't load, NULL is returned.
{
// Set the dimensions using the image width and height.
offset.x = x; offset.y = y;
offset.w = img.w; offset.h = img.h;
}
else { throw; } // Handle the fact that the image didn't load somehow.
}
~Button() { SDL_FreeSurface(img); } // Make sure you free your resources!
void Render(SDL_Surface *destination)
{
// Simplified render call.
SDL_BlitSurface(img, NULL, destination, &offset );
}
bool Contains(int x, int y)
{
if((x>offset.x) && (x<offset.x+offset.w)
&& (y>offset.y) && (y<offset.y+offset.h))
return true; // If your button contains this point.
return false; // Otherwise false.
}
private:
SDL_Rect offset; // Dimensions of the button.
SDL_Surface* img; // The button image.
}
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- SDL-使用SDL_TEXTURE的std ::向量不起作用,数组正常工作
- 鼠标在SDL/C 中不起作用
- SDL 2.0 - SDL_GameControllerGetButton不起作用
- C++ + SDL + OpenGL 3.3 在 Mac OS X 上不起作用?