Skip to content

Commit

Permalink
Finished the camera system
Browse files Browse the repository at this point in the history
  • Loading branch information
gorbit99 committed Oct 26, 2019
1 parent c46b418 commit a1f4dcf
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
26 changes: 10 additions & 16 deletions Camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,19 @@ static const float minZoom = 0.1f;
static const float maxZoom = 1;

void camera_zoom(Camera *camera, float zoomLevels, Point zoomPos) {
float multiplier = powf(0.9f, -zoomLevels);
float multiplier = powf(0.9f, -zoomLevels);

SDL_DisplayMode DM;
SDL_GetCurrentDisplayMode(0, &DM);
if (camera->zoom * multiplier < minZoom)
multiplier = minZoom / camera->zoom;
else if (camera->zoom * multiplier > maxZoom)
multiplier = maxZoom / camera->zoom;
camera->zoom *= multiplier;

camera->zoom *= multiplier;
if (camera->zoom < minZoom) {
camera->zoom = minZoom;
}
else if (camera->zoom > maxZoom) {
camera->zoom = maxZoom;
}
else {
camera->position.x -= (zoomPos.x - zoomPos.x * multiplier) / camera->zoom;
camera->position.y -= (zoomPos.y - zoomPos.y * multiplier) / camera->zoom;
}
camera->position.x -= (zoomPos.x - zoomPos.x * multiplier) / camera->zoom;
camera->position.y -= (zoomPos.y - zoomPos.y * multiplier) / camera->zoom;
}

void camera_move(Camera *camera, Vec movement) {
camera->position.x -= movement.x / camera->zoom;
camera->position.y -= movement.y / camera->zoom;
camera->position.x -= movement.x / camera->zoom;
camera->position.y -= movement.y / camera->zoom;
}
4 changes: 4 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ int main(int argc, char **argv) {

SDL_Cursor *cursor = SDL_GetCursor();
Camera camera;
camera.position = (Point){0, 0};
camera.zoom = 0.5f;

bool quit = false;
SDL_Event e;
Expand All @@ -90,6 +92,8 @@ int main(int argc, char **argv) {
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);

SDL_SetRenderDrawColor(renderer,255,0, 0, 255);

if (input_get_mouse_button(SDL_BUTTON_MIDDLE).isHeld)
camera_move(&camera, (Vec){input_get_mouse_delta_x(), input_get_mouse_delta_y()});

Expand Down

0 comments on commit a1f4dcf

Please sign in to comment.