Skip to content

Commit

Permalink
Support resolution independence
Browse files Browse the repository at this point in the history
  • Loading branch information
karjonas committed Feb 4, 2023
1 parent 425045c commit e0b8395
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
8 changes: 4 additions & 4 deletions src/Drawing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ void Drawing::draw_user(int w, int h, User& user)
else
points = calc_user_rectangle_points(user.user_x, h/2, false, user.rect_size);

points.insert(points.begin(), Point(100,h/2));
points.push_back(Point(w-100,h/2));
points.insert(points.begin(), Point(0,h/2));
points.push_back(Point(w,h/2));

const size_t num_pts = points.size();

Expand Down Expand Up @@ -139,7 +139,7 @@ void Drawing::draw_all(int w, int h, User& user, const std::vector<Opponent>& op
});

std::vector<LineColor> lcs;
auto last_pt = Point(100,h/2);
auto last_pt = Point(0,h/2);

for (auto& sp : sps)
{
Expand All @@ -154,7 +154,7 @@ void Drawing::draw_all(int w, int h, User& user, const std::vector<Opponent>& op
last_pt = sp.points.back();
}

lcs.emplace_back(last_pt, Point(w-100, h/2), ColorScheme::color0());
lcs.emplace_back(last_pt, Point(w, h/2), ColorScheme::color0());

for (auto& lc : lcs)
{
Expand Down
39 changes: 29 additions & 10 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void init(void)
ALLEGRO_DISPLAY_MODE disp_data;
al_get_display_mode(0, &disp_data);

al_set_new_display_flags(ALLEGRO_WINDOWED);
al_set_new_display_flags(ALLEGRO_WINDOWED | ALLEGRO_RESIZABLE);
//display = al_create_display(disp_data.width, disp_data.height);
display = al_create_display(800, 480);
if (!display)
Expand Down Expand Up @@ -92,8 +92,8 @@ void game_loop(void)
bool redraw = true;
al_start_timer(timer);

int windowWidth = al_get_display_width(display);
int windowHeight = al_get_display_height(display);
constexpr int windowWidth = 600;
constexpr int windowHeight = 288;

User user{};
user.user_x = windowWidth/2;
Expand All @@ -111,7 +111,8 @@ void game_loop(void)

bool death = false;
int finished_level = 5;
int num_kills = 0;
int num_kills = 0;

while (!done)
{
auto& opponents = level.opponents;
Expand All @@ -134,7 +135,7 @@ void game_loop(void)
if (!opponent.active && elapsed_time >= opponent.time)
{
opponent.active = true;
opponent.x = opponent.direction == Direction::LEFT ? windowWidth - 100 : 100;
opponent.x = opponent.direction == Direction::LEFT ? windowWidth : 0;
}
else if (opponent.active)
{
Expand Down Expand Up @@ -175,8 +176,8 @@ void game_loop(void)
user.user_x = user.user_x + user.user_speed*dt;
}

user.user_x = std::max(100.0 + 2*user.rect_size, user.user_x);
user.user_x = std::min(static_cast<double>(windowWidth) - 100.0 -(2*user.rect_size), user.user_x);
user.user_x = std::max<double>(2*user.rect_size, user.user_x);
user.user_x = std::min<double>(windowWidth -(2*user.rect_size), user.user_x);

time_last = time_curr;
}
Expand All @@ -188,27 +189,45 @@ void game_loop(void)
//main_loop.key_pressed(event.keyboard.keycode);
}
}
else if (event.type == ALLEGRO_EVENT_DISPLAY_RESIZE) {
al_acknowledge_resize(display);
int w = al_get_display_width(display);
int h = al_get_display_height(display);
const int scale = std::max(1, std::min(w/windowWidth, h/windowHeight));
w = w/scale;
h = h/scale;
ALLEGRO_TRANSFORM trans;
al_identity_transform(&trans);
al_translate_transform(&trans, (w - windowWidth)/2, (h - windowHeight)/2);
al_scale_transform(&trans, scale, scale);
al_use_transform(&trans);
}
else if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) {
done = true;
}

if (redraw && al_is_event_queue_empty(event_queue)) {
redraw = false;
al_clear_to_color(ColorScheme::color_bg());

// draw viewport
//al_draw_rectangle(0, 0, windowWidth, windowHeight,
//al_map_rgb(0x9b,0xd7,0xd5), 1);


Drawing::draw_all(windowWidth, windowHeight, user, opponents);

if (level.first_level)
{
Drawing::draw_tutorial_texts(font, ColorScheme::color1(), windowWidth/2 , windowHeight/5, elapsed_time);
Drawing::draw_tutorial_texts(font, ColorScheme::color1(), windowWidth/2 , 0, elapsed_time);
}
else if (level_nr != finished_level)
{
Drawing::draw_level_texts(font, ColorScheme::color1(), windowWidth/2, windowHeight/5, level_nr, elapsed_time);
Drawing::draw_level_texts(font, ColorScheme::color1(), windowWidth/2, 0, level_nr, elapsed_time);
}
else
{
Drawing::draw_credits(font, ColorScheme::color1(), windowWidth/2, windowHeight/5);
Drawing::draw_credits(font, ColorScheme::color1(), windowWidth/2, 0);
}
al_flip_display();
}
Expand Down

0 comments on commit e0b8395

Please sign in to comment.