i want to be able to drag an image file, drop it on the window and have it rendered exactly where the cursor is. problem is, SDL_GetMouseState() stops recording mouse events when I’m dragging the desired file, so it gets rendered on the last recorded cursor position (which is on the edge my cursor went out the window of to fetch the file). is using the Windows API the only solution ? i’d rather not deal with OS compatibility issues right now.
vector<pair<SDL_Texture*, SDL_Rect>> texturePairs;
pair < SDL_Texture*, SDL_Rect> loadTexture(const char* path, int rectX, int rectY) {
bool success = true;
SDL_Texture* loadedTexture = IMG_LoadTexture(gRenderer, path);
if (loadedTexture == NULL) {
cout << "Could not load texture :" << IMG_GetError() << endl;
success = false;
return { NULL, {} };
}
else {
SDL_Rect destRect = { 0, 0, 0, 0 };
SDL_QueryTexture(loadedTexture, NULL, NULL, &destRect.w, &destRect.h);
destRect.x = rectX;
destRect.y = rectY;
cout << "rect x,y:" << rectX << "," << rectY << endl;
texturePairs.push_back(make_pair(loadedTexture, destRect));
return { loadedTexture, destRect };
}
}
// game loop in main
while (gameRunning) {
while (SDL_PollEvent(&event)) {
int mouseX, mouseY;
SDL_GetMouseState(&mouseX, &mouseY);
switch (event.type) {
case SDL_QUIT:
gameRunning = false;
cout << "User requested exit." << endl;
break;
case SDL_MOUSEMOTION:
lastMouseX = event.motion.x;
lastMouseY = event.motion.y;
cout << "last x,y:" << lastMouseX << "," << lastMouseY << endl;
break;
case SDL_DROPFILE: {
char* dropped_filedir = event.drop.file;
loadTexture(dropped_filedir, mouseX, mouseY);
currentState = RESIZING_IMAGE;
SDL_free(dropped_filedir);
break;
}
on the console, i get printed out last x,y as the last known cursor position after it goes beyond the window bounds (so it’s 0,xxx if it goes out the left side), then after i drop the wanted file it prints out rect x,y identical to the first one which is no longer valid, the image gets rendered there, then it prints out a last x,y with the actual valid position.
user25584104 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.