Implement dynamic scaling for the camera (#55)

master
rexim 2018-01-16 23:01:07 +07:00
parent cfd9fb3154
commit 9e164f37f9
2 changed files with 10 additions and 18 deletions

View File

@ -9,7 +9,6 @@
struct camera_t {
int debug_mode;
point_t position;
float scale;
};
camera_t *create_camera(point_t position)
@ -23,7 +22,6 @@ camera_t *create_camera(point_t position)
camera->position = position;
camera->debug_mode = 0;
camera->scale = 2.0f;
return camera;
}
@ -47,15 +45,21 @@ int camera_fill_rect(const camera_t *camera,
SDL_RenderGetViewport(render, &view_port);
const float khooy_x = 16.0f;
const float khooy_y = (float) view_port.h / ((float) view_port.w / khooy_x);
const float scale_x = (float) view_port.w / (khooy_x * 50.0f);
const float scale_y = (float) view_port.h / (khooy_y * 50.0f);
SDL_Rect sdl_rect;
/* TODO(#55): make the scale depend on the view port.
*
* So the game looks the same regardless of the size of the window */
sdl_rect.x = (int) roundf((rect->x - camera->position.x) * camera->scale + (float) view_port.w * 0.5f);
sdl_rect.y = (int) roundf((rect->y - camera->position.y) * camera->scale + (float) view_port.h * 0.5f);
sdl_rect.w = (int) roundf(rect->w * camera->scale);
sdl_rect.h = (int) roundf(rect->h * camera->scale);
sdl_rect.x = (int) roundf((rect->x - camera->position.x) * scale_x + (float) view_port.w * 0.5f);
sdl_rect.y = (int) roundf((rect->y - camera->position.y) * scale_y + (float) view_port.h * 0.5f);
sdl_rect.w = (int) roundf(rect->w * scale_x);
sdl_rect.h = (int) roundf(rect->h * scale_y);
if (camera->debug_mode) {
if (SDL_RenderDrawRect(render, &sdl_rect) < 0) {
@ -83,13 +87,3 @@ void camera_toggle_debug_mode(camera_t *camera)
assert(camera);
camera->debug_mode = !camera->debug_mode;
}
void camera_zoom_in(camera_t * camera)
{
camera->scale = fminf(camera->scale + 0.1f, 2.0f);
}
void camera_zoom_out(camera_t * camera)
{
camera->scale = fmaxf(camera->scale - 0.1f, 0.1f);
}

View File

@ -16,7 +16,5 @@ int camera_fill_rect(const camera_t *camera,
void camera_center_at(camera_t *camera, point_t position);
void camera_toggle_debug_mode(camera_t *camera);
void camera_zoom_in(camera_t * camera);
void camera_zoom_out(camera_t * camera);
#endif // CAMERA_H_