added right mouse button drag to move image on surface
This commit is contained in:
+31
-12
@@ -95,7 +95,7 @@ SDL_Surface* create_image_surface(SDL_Surface* surface, Image* image){
|
|||||||
return image_surface;
|
return image_surface;
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw_image(SDL_Window* window, SDL_Surface* image_surface, Image* image, float pixel_size_delta){
|
void draw_image(SDL_Window* window, SDL_Surface* image_surface, Image* image, float pixel_size_delta, int image_start_x, int image_start_y){
|
||||||
SDL_Surface* surface = SDL_GetWindowSurface(window);
|
SDL_Surface* surface = SDL_GetWindowSurface(window);
|
||||||
|
|
||||||
float pixel_size = pixel_size_delta;
|
float pixel_size = pixel_size_delta;
|
||||||
@@ -105,7 +105,7 @@ void draw_image(SDL_Window* window, SDL_Surface* image_surface, Image* image, fl
|
|||||||
|
|
||||||
Uint64 start = SDL_GetTicksNS();
|
Uint64 start = SDL_GetTicksNS();
|
||||||
|
|
||||||
SDL_Rect dst = { center_x - (image->x * pixel_size / 2), center_y - (image->y * pixel_size / 2), image->x * pixel_size, image->y * pixel_size };
|
SDL_Rect dst = { center_x - (image->x * pixel_size / 2) + image_start_x, center_y - (image->y * pixel_size / 2) + image_start_y, image->x * pixel_size, image->y * pixel_size };
|
||||||
SDL_ClearSurface(surface, 0, 0, 0, 255);
|
SDL_ClearSurface(surface, 0, 0, 0, 255);
|
||||||
SDL_BlitSurfaceScaled(image_surface, NULL, surface, &dst, SDL_SCALEMODE_LINEAR);
|
SDL_BlitSurfaceScaled(image_surface, NULL, surface, &dst, SDL_SCALEMODE_LINEAR);
|
||||||
|
|
||||||
@@ -153,7 +153,8 @@ void on_wheel(SDL_Event* event, SDL_Window* window){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loop(SDL_Window* window, SDL_Surface* image_surface, Image* image){
|
void loop(SDL_Window* window, SDL_Surface* image_surface, Image* image){
|
||||||
bool drag_mouse = false;
|
bool drag_left_mouse = false;
|
||||||
|
bool drag_right_mouse = false;
|
||||||
int last_pointer_x = 0;
|
int last_pointer_x = 0;
|
||||||
int last_pointer_y = 0;
|
int last_pointer_y = 0;
|
||||||
|
|
||||||
@@ -161,6 +162,8 @@ void loop(SDL_Window* window, SDL_Surface* image_surface, Image* image){
|
|||||||
bool run = true;
|
bool run = true;
|
||||||
|
|
||||||
float pixel_size_delta = 1.0f;
|
float pixel_size_delta = 1.0f;
|
||||||
|
int current_image_start_x = 0, current_image_start_y = 0;
|
||||||
|
int image_start_at_drag_x = 0, image_start_at_drag_y = 0;
|
||||||
|
|
||||||
bool redraw = false;
|
bool redraw = false;
|
||||||
|
|
||||||
@@ -176,16 +179,32 @@ void loop(SDL_Window* window, SDL_Surface* image_surface, Image* image){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(event.type == SDL_EVENT_QUIT || event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED) run = false;
|
if(event.type == SDL_EVENT_QUIT || event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED) run = false;
|
||||||
if(event.type == SDL_EVENT_MOUSE_BUTTON_DOWN && event.button.button == SDL_BUTTON_LEFT){
|
if(event.type == SDL_EVENT_MOUSE_BUTTON_DOWN){
|
||||||
drag_mouse = true;
|
if(event.button.button == SDL_BUTTON_LEFT) drag_left_mouse = true;
|
||||||
|
if(event.button.button == SDL_BUTTON_RIGHT) drag_right_mouse = true;
|
||||||
|
|
||||||
last_pointer_x = (int)event.button.x;
|
last_pointer_x = (int)event.button.x;
|
||||||
last_pointer_y = (int)event.button.y;
|
last_pointer_y = (int)event.button.y;
|
||||||
|
image_start_at_drag_x = current_image_start_x;
|
||||||
|
image_start_at_drag_y = current_image_start_y;
|
||||||
}
|
}
|
||||||
if(event.type == SDL_EVENT_MOUSE_BUTTON_UP && event.button.button == SDL_BUTTON_LEFT) drag_mouse = false;
|
if(event.type == SDL_EVENT_MOUSE_BUTTON_UP && event.button.button == SDL_BUTTON_LEFT) drag_left_mouse = false;
|
||||||
if(event.type == SDL_EVENT_MOUSE_MOTION && drag_mouse){
|
if(event.type == SDL_EVENT_MOUSE_BUTTON_UP && event.button.button == SDL_BUTTON_RIGHT) drag_right_mouse = false;
|
||||||
int last_x, last_y;
|
if(event.type == SDL_EVENT_MOUSE_MOTION){
|
||||||
SDL_GetWindowPosition(window, &last_x, &last_y);
|
if(drag_left_mouse){
|
||||||
SDL_SetWindowPosition(window, last_x + (int)event.motion.x - last_pointer_x, last_y + (int)event.motion.y - last_pointer_y);
|
int last_x, last_y;
|
||||||
|
SDL_GetWindowPosition(window, &last_x, &last_y);
|
||||||
|
SDL_SetWindowPosition(window, last_x + (int)event.motion.x - last_pointer_x, last_y + (int)event.motion.y - last_pointer_y);
|
||||||
|
}
|
||||||
|
if(drag_right_mouse){
|
||||||
|
int image_start_x = (int)event.motion.x - last_pointer_x + image_start_at_drag_x;
|
||||||
|
int image_start_y = (int)event.motion.y - last_pointer_y + image_start_at_drag_y;
|
||||||
|
|
||||||
|
current_image_start_x = image_start_x;
|
||||||
|
current_image_start_y = image_start_y;
|
||||||
|
|
||||||
|
redraw = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if(event.type == SDL_EVENT_MOUSE_WHEEL){
|
if(event.type == SDL_EVENT_MOUSE_WHEEL){
|
||||||
float last_pixel_size_delta = pixel_size_delta;
|
float last_pixel_size_delta = pixel_size_delta;
|
||||||
@@ -202,7 +221,7 @@ void loop(SDL_Window* window, SDL_Surface* image_surface, Image* image){
|
|||||||
|
|
||||||
if(redraw){
|
if(redraw){
|
||||||
redraw = false;
|
redraw = false;
|
||||||
draw_image(window, image_surface, image, pixel_size_delta);
|
draw_image(window, image_surface, image, pixel_size_delta, current_image_start_x, current_image_start_y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -241,7 +260,7 @@ int main(int argc, char** argv){
|
|||||||
SDL_Surface* image_surface = create_image_surface(surface, &image);
|
SDL_Surface* image_surface = create_image_surface(surface, &image);
|
||||||
free_image_buffer(&image);
|
free_image_buffer(&image);
|
||||||
|
|
||||||
draw_image(window, image_surface, &image, 1.0f);
|
draw_image(window, image_surface, &image, 1.0f, 0, 0);
|
||||||
|
|
||||||
SDL_UpdateWindowSurface(window);
|
SDL_UpdateWindowSurface(window);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user