C++ sdl基于鼠标位置绘制动态矩形

weixin_38060336 2019-09-12 04:12:57

对许多人来说,这看起来很明显,但我被卡住了,我找不到任何帮助我的教程,因此在这里找到答案会很好。 我试图制作一个矩形,就像你在RTS游戏中所做的那样,可以说是一个动态选择框,但它不能正常工作。 这也将是足够的人列出了要点这对于创建像一个盒子的每一步该 (不要怀疑我真的很新的C++) Mouse.h: #ifndef MOUSE_H_ #define MOUSE_H_ class Mouse { public: bool leftButtonDown = false; bool mouseMoves = false; struct MouseCoords { int x = -1; int y = -1; }; MouseCoords start_coords; MouseCoords move_coords; Mouse(){} void setState(Uint32 eventType, SDL_Event event){ if (eventType == SDL_MOUSEBUTTONDOWN) { this->leftButtonDown = true; SDL_GetMouseState(&this->start_coords.x, &this->start_coords.y); } if (eventType == SDL_MOUSEBUTTONUP) { this->leftButtonDown = false; } if (eventType == SDL_MOUSEMOTION) { this->mouseMoves = true; this->move_coords.x = event.motion.x; this->move_coords.y = event.motion.y; } else { this->mouseMoves = false; } } /** * Provides coordinates when mousebutton down */ MouseCoords getCoordinates(){ return move_coords; } /** * Refresh the coordinates. MUST be called after Mouse::clearCoordinates(); */ void clearCoordinates(){ this->move_coords.x = -1; this->move_coords.y = -1; } /** * Creates selector rect, call this in the render loop */ void createSelectBox(SDL_Renderer *renderer){ if(this->leftButtonDown) { Block rect( this->start_coords.x, this->move_coords.y, this->start_coords.y -this->move_coords.y , this->move_coords.x - this->start_coords.x ); rect.draw(renderer); this->clearCoordinates(); } } }; #endif Block.h #ifndef BLOCK_H_ #define BLOCK_H_ /** * Standard Class which can be used to build a player, enemy, or something similar */ class Block{ SDL_Rect rect; public: Block(int x=10, int y=10, int w=10, int h=10, int filled=true){ rect.x = x; rect.y = y; rect.w = w; rect.h = h; } /** * draw with the given rendererer */ void draw(SDL_Renderer *renderer){ SDL_SetRenderDrawColor(renderer , 200 , 155 , 255 , 255); SDL_RenderDrawRect(renderer, &rect); SDL_RenderPresent(renderer); } bool framed(){ return 0; } }; #endif 然后在我的main.cpp SDL_Window *window; SDL_Renderer *renderer; Mouse mouse; Block rect; void renderWindow(){ window = SDL_CreateWindow("commanding rects", 100, 100, 700, 600, 0); renderer = SDL_CreateRenderer(window, -1, 0); } void renderGame(Mouse mouse){ SDL_RenderSetLogicalSize(renderer, 400, 300); SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); SDL_RenderClear(renderer); SDL_RenderPresent(renderer); mouse.createSelectBox(renderer); } void gameLoop(){ bool game_run = true; while (game_run) { SDL_Event event; while(SDL_PollEvent(&event)){ if (event.type == SDL_QUIT) { game_run = false; } mouse.setState(event.type, event); } renderGame(mouse); SDL_Delay(15); } } int main(){ renderWindow(); gameLoop(); return 0; }








...全文
68 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
weixin_38077132 2019-09-12
  • 打赏
  • 举报
回复

我终于发现我做错了什么。 我正在检索绘制矩形的错误起点。 当触发鼠标按下我得的event.button.x代替GetMouseState 这里是更新Mouse.h #ifndef MOUSE_H_ #define MOUSE_H_ class Mouse { public: bool leftButtonDown = false; bool mouseMoves = false; struct MouseCoords { int x = -1; int y = -1; }; MouseCoords start_coords; MouseCoords move_coords; Mouse(){} void setState(Uint32 eventType, SDL_Event event){ if (eventType == SDL_MOUSEBUTTONDOWN) { this->leftButtonDown = true; this->start_coords.x = event.button.x; this->start_coords.y = event.button.y; } if (eventType == SDL_MOUSEBUTTONUP) { this->leftButtonDown = false; } if (eventType == SDL_MOUSEMOTION) { this->move_coords.x = event.motion.x; this->move_coords.y = event.motion.y; } } /** * Provides coordinates when mousebutton down */ MouseCoords getCoordinates(){ return move_coords; } /** * Creates selector rect, call this in the render loop */ void createSelectBox(SDL_Renderer *renderer){ if(this->leftButtonDown) { float width = this->move_coords.x - this->start_coords.x; float height = this->start_coords.y - this->move_coords.y; float x = this->start_coords.x ; float y = this->move_coords.y; Block rect(x, y,width, height); rect.draw(renderer); } } }; #endif
weixin_38085456 2019-09-12
  • 打赏
  • 举报
回复

我在这里发现了一个问题: void createSelectBox(SDL_Renderer *renderer){ if(this->leftButtonDown) { Block rect(this->mousedown_coords.x, this->mousemove_coords.y, this->mousedown_coords.y - this->mousemove_coords.y, this->mousemove_coords.x - this->mousedown_coords.x); 在这一段代码,你矩形的宽度和高度,不管是什么鼠标坐标的值都设置为0。由于您正在绘制边界框,因此我想您需要存储两组x,y坐标,一个用于按下鼠标按钮,另一个用于释放时。

433

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧