Skip to content

Commit

Permalink
8x8 font term resizing
Browse files Browse the repository at this point in the history
  • Loading branch information
goblinhack committed Jan 15, 2025
1 parent 17a0e69 commit 88f807b
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 64 deletions.
Binary file modified data/fonts/PressStart.xcf
Binary file not shown.
4 changes: 2 additions & 2 deletions src/ascii.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -731,8 +731,8 @@ static void ascii_blit(void)
float mx = sdl.mouse_x;
float my = sdl.mouse_y;
#endif
const auto dw = game_ascii_gl_width_get(game);
const auto dh = game_ascii_gl_height_get(game);
const auto dw = game_ascii_pix_width_get(game);
const auto dh = game_ascii_pix_height_get(game);

tile_y = 0;
for (y = 0; y < TERM_HEIGHT; y++) {
Expand Down
28 changes: 14 additions & 14 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ using Config = class Config_
//
float aspect_ratio = {};

int ui_gfx_term_height = {};
int ui_gfx_term_width = {};
int ui_term_height = {};
int ui_term_width = {};

bool debug_mode = {};
bool fps_counter = {};
Expand Down Expand Up @@ -64,8 +64,8 @@ using Config = class Config_
int tiles_visible_across;
int tiles_visible_down;

int ascii_gl_height = {};
int ascii_gl_width = {};
int ascii_pix_height = {};
int ascii_pix_width = {};
int music_volume = {};
int sdl_delay = {};
int sound_volume = {};
Expand Down Expand Up @@ -250,8 +250,8 @@ void Config::reset(void)
gfx_vsync_enable = true;
mouse_wheel_lr_negated = false;
mouse_wheel_ud_negated = false;
ui_gfx_term_height = {TERM_GFX_HEIGHT_DEF};
ui_gfx_term_width = {TERM_GFX_WIDTH_DEF};
ui_term_height = {TERM_HEIGHT_DEF};
ui_term_width = {TERM_WIDTH_DEF};
version = "" MYVER "";
aspect_ratio = {};
window_pix_height = {};
Expand Down Expand Up @@ -545,11 +545,11 @@ void game_serialized_size_set(class Game *g, int val) { g->serialized_size = val
float game_aspect_ratio_get(class Game *g) { return g->config.aspect_ratio; }
void game_aspect_ratio_set(class Game *g, float val) { g->config.aspect_ratio = val; }

int game_ui_gfx_term_height_get(class Game *g) { return g->config.ui_gfx_term_height; }
void game_ui_gfx_term_height_set(class Game *g, int val) { g->config.ui_gfx_term_height = val; }
int game_ui_term_height_get(class Game *g) { return g->config.ui_term_height; }
void game_ui_term_height_set(class Game *g, int val) { g->config.ui_term_height = val; }

int game_ui_gfx_term_width_get(class Game *g) { return g->config.ui_gfx_term_width; }
void game_ui_gfx_term_width_set(class Game *g, int val) { g->config.ui_gfx_term_width = val; }
int game_ui_term_width_get(class Game *g) { return g->config.ui_term_width; }
void game_ui_term_width_set(class Game *g, int val) { g->config.ui_term_width = val; }

bool game_debug_mode_get(class Game *g) { return g->config.debug_mode; }
void game_debug_mode_set(class Game *g, bool val) { g->config.debug_mode = val; }
Expand Down Expand Up @@ -605,11 +605,11 @@ void game_window_pix_height_set(class Game *g, int val) { g->config.window_pix_h
int game_window_pix_width_get(class Game *g) { return g->config.window_pix_width; }
void game_window_pix_width_set(class Game *g, int val) { g->config.window_pix_width = val; }

int game_ascii_gl_height_get(class Game *g) { return g->config.ascii_gl_height; }
void game_ascii_gl_height_set(class Game *g, int val) { g->config.ascii_gl_height = val; }
int game_ascii_pix_height_get(class Game *g) { return g->config.ascii_pix_height; }
void game_ascii_pix_height_set(class Game *g, int val) { g->config.ascii_pix_height = val; }

int game_ascii_gl_width_get(class Game *g) { return g->config.ascii_gl_width; }
void game_ascii_gl_width_set(class Game *g, int val) { g->config.ascii_gl_width = val; }
int game_ascii_pix_width_get(class Game *g) { return g->config.ascii_pix_width; }
void game_ascii_pix_width_set(class Game *g, int val) { g->config.ascii_pix_width = val; }

int game_music_volume_get(class Game *g) { return g->config.music_volume; }
void game_music_volume_set(class Game *g, int val) { g->config.music_volume = val; }
Expand Down
20 changes: 10 additions & 10 deletions src/game_load.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ std::istream &operator>>(std::istream &in, Bits< Config & > my)
return in;
}

in >> bits(my.t.ascii_gl_height);
in >> bits(my.t.ascii_gl_width);
in >> bits(my.t.ascii_pix_height);
in >> bits(my.t.ascii_pix_width);
in >> bits(my.t.aspect_ratio);
in >> bits(my.t.config_pix_height);
in >> bits(my.t.config_pix_width);
Expand Down Expand Up @@ -89,13 +89,13 @@ std::istream &operator>>(std::istream &in, Bits< Config & > my)
in >> bits(my.t.sound_volume);
in >> bits(my.t.tiles_visible_across);
in >> bits(my.t.tiles_visible_down);
in >> bits(my.t.ui_gfx_term_height);
in >> bits(my.t.ui_gfx_term_width);
in >> bits(my.t.ui_term_height);
in >> bits(my.t.ui_term_width);
in >> bits(my.t.window_pix_height);
in >> bits(my.t.window_pix_width);

LOG("Read config: ascii_gl_height = %d", my.t.ascii_gl_height);
LOG("Read config: ascii_gl_width = %d", my.t.ascii_gl_width);
LOG("Read config: ascii_pix_height = %d", my.t.ascii_pix_height);
LOG("Read config: ascii_pix_width = %d", my.t.ascii_pix_width);
LOG("Read config: aspect_ratio = %f", my.t.aspect_ratio);
LOG("Read config: config_pix_height = %d", my.t.config_pix_height);
LOG("Read config: config_pix_width = %d", my.t.config_pix_width);
Expand Down Expand Up @@ -148,12 +148,12 @@ std::istream &operator>>(std::istream &in, Bits< Config & > my)
game_load_error += "end of config marker not found";
return in;
}
if (my.t.ascii_gl_height < 0) {
game_load_error += "ascii_gl_height is invalid";
if (my.t.ascii_pix_height < 0) {
game_load_error += "ascii_pix_height is invalid";
return in;
}
if (my.t.ascii_gl_width < 0) {
game_load_error += "ascii_gl_width is invalid";
if (my.t.ascii_pix_width < 0) {
game_load_error += "ascii_pix_width is invalid";
return in;
}
if (my.t.config_pix_height < 0) {
Expand Down
8 changes: 4 additions & 4 deletions src/game_save.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ std::ostream &operator<<(std::ostream &out, Bits< const Config & > const my)
uint32_t serialized_size = sizeof(Config);
out << bits(serialized_size);

out << bits(my.t.ascii_gl_height);
out << bits(my.t.ascii_gl_width);
out << bits(my.t.ascii_pix_height);
out << bits(my.t.ascii_pix_width);
out << bits(my.t.aspect_ratio);
out << bits(my.t.config_pix_height);
out << bits(my.t.config_pix_width);
Expand Down Expand Up @@ -74,8 +74,8 @@ std::ostream &operator<<(std::ostream &out, Bits< const Config & > const my)
out << bits(my.t.sound_volume);
out << bits(my.t.tiles_visible_across);
out << bits(my.t.tiles_visible_down);
out << bits(my.t.ui_gfx_term_height);
out << bits(my.t.ui_gfx_term_width);
out << bits(my.t.ui_term_height);
out << bits(my.t.ui_term_width);
out << bits(my.t.window_pix_height);
out << bits(my.t.window_pix_width);

Expand Down
18 changes: 18 additions & 0 deletions src/math.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Copyright Neil McGill, goblinhack@gmail.com
//

#include "my_math.hpp"

uint32_t nextpoweroftwo(uint32_t val)
{
val--;
val = (val >> 1) | val;
val = (val >> 2) | val;
val = (val >> 4) | val;
val = (val >> 8) | val;
val = (val >> 16) | val;
val++; // Val is now the next highest power of 2

return (val);
}
11 changes: 7 additions & 4 deletions src/my_ascii.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
#include "my_tile.hpp"
#include "my_ui.hpp"

#define TERM_GFX_WIDTH_DEF 132
#define TERM_GFX_HEIGHT_DEF 60
#define TERM_WIDTH_DEF 144
#define TERM_HEIGHT_DEF 80

#define TERM_WIDTH_MAX TERM_GFX_WIDTH_DEF
#define TERM_HEIGHT_MAX TERM_GFX_HEIGHT_DEF
#define TERM_WIDTH_MIN 100
#define TERM_HEIGHT_MIN 60

#define TERM_WIDTH_MAX (TERM_WIDTH_DEF * 2)
#define TERM_HEIGHT_MAX (TERM_HEIGHT_DEF * 2)

class AsciiCell
{
Expand Down
16 changes: 8 additions & 8 deletions src/my_game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ void game_serialized_size_set(class Game *, int);
float game_aspect_ratio_get(class Game *);
void game_aspect_ratio_set(class Game *, float);

int game_ui_gfx_term_height_get(class Game *);
void game_ui_gfx_term_height_set(class Game *, int);
int game_ui_term_height_get(class Game *);
void game_ui_term_height_set(class Game *, int);

int game_ui_gfx_term_width_get(class Game *);
void game_ui_gfx_term_width_set(class Game *, int);
int game_ui_term_width_get(class Game *);
void game_ui_term_width_set(class Game *, int);

bool game_debug_mode_get(class Game *);
void game_debug_mode_set(class Game *, bool);
Expand Down Expand Up @@ -109,11 +109,11 @@ void game_window_pix_height_set(class Game *, int);
int game_window_pix_width_get(class Game *);
void game_window_pix_width_set(class Game *, int);

int game_ascii_gl_height_get(class Game *);
void game_ascii_gl_height_set(class Game *, int);
int game_ascii_pix_height_get(class Game *);
void game_ascii_pix_height_set(class Game *, int);

int game_ascii_gl_width_get(class Game *);
void game_ascii_gl_width_set(class Game *, int);
int game_ascii_pix_width_get(class Game *);
void game_ascii_pix_width_set(class Game *, int);

int game_music_volume_get(class Game *);
void game_music_volume_set(class Game *, int);
Expand Down
6 changes: 6 additions & 0 deletions src/my_math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
//

#pragma once

#include <stdlib.h>

#ifndef _MY_MATH_UTIL_HPP_
#define _MY_MATH_UTIL_HPP_

Expand Down Expand Up @@ -32,4 +35,7 @@ double gaussrand(double mean, double stddev);
#define RAD_180 3.14159265358979323846f // pi
#define RAD_360 (2.0f * 3.14159265358979323846f) // 2pi
#define RAD_270 (RAD_180 + RAD_90)

uint32_t nextpoweroftwo(uint32_t val);

#endif
88 changes: 68 additions & 20 deletions src/sdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "my_command.hpp"
#include "my_game.hpp"
#include "my_gl.hpp"
#include "my_math.hpp"
#include "my_sdl_event.hpp"
#include "my_sdl_proto.hpp"
#include "my_ui.hpp"
Expand Down Expand Up @@ -686,8 +687,8 @@ void config_game_gfx_update(void)
//
game_aspect_ratio_set(game, (double) game_window_pix_width_get(game) / (double) game_window_pix_height_get(game));

TERM_WIDTH = game_ui_gfx_term_width_get(game);
TERM_HEIGHT = game_ui_gfx_term_height_get(game);
TERM_WIDTH = game_ui_term_width_get(game);
TERM_HEIGHT = game_ui_term_height_get(game);

if (! TERM_WIDTH) {
ERR("TERM_WIDTH is zero");
Expand All @@ -708,26 +709,76 @@ void config_game_gfx_update(void)
TERM_HEIGHT = TERM_HEIGHT_MAX;
}

float font_width = game_window_pix_width_get(game) / TERM_WIDTH;
float font_height = game_window_pix_height_get(game) / TERM_HEIGHT;
int font_width = game_window_pix_width_get(game) / TERM_WIDTH;
int font_height = game_window_pix_height_get(game) / TERM_HEIGHT;

game_ascii_gl_width_set(game, font_width);
game_ascii_gl_height_set(game, font_height);
game_ascii_pix_width_set(game, font_width);
game_ascii_pix_height_set(game, font_height);

LOG("SDL: Window:");
LOG("SDL: - window pixel size : %dx%d", game_window_pix_width_get(game), game_window_pix_height_get(game));
LOG("SDL: - aspect ratio : %f", game_aspect_ratio_get(game));
LOG("SDL: Initial terminal");
LOG("SDL: - term size : %dx%d", TERM_WIDTH, TERM_HEIGHT);
LOG("SDL: - ascii gl size : %ux%u", game_ascii_pix_width_get(game), game_ascii_pix_height_get(game));

//
// If the font has an odd number, it will look bad. Try the next power.
//
font_width = nextpoweroftwo(font_width);
font_height = nextpoweroftwo(font_height);
game_ascii_pix_width_set(game, font_width);
game_ascii_pix_height_set(game, font_height);
LOG("SDL: - ascii gl size (pow2) : %ux%u", game_ascii_pix_width_get(game), game_ascii_pix_height_get(game));

//
// If we overflow the screen, try to cut a few rows and columns off
//
while (game_ascii_gl_width_get(game) * TERM_WIDTH > game_window_pix_width_get(game)) {
while (font_width * TERM_WIDTH > game_window_pix_width_get(game)) {
TERM_WIDTH--;
LOG("SDL: - font width %u exceeded terminal pixel width %u, try term width: %d", game_ascii_gl_width_get(game),
game_window_pix_width_get(game), TERM_WIDTH);
LOG("SDL: - term overflow, font width %u, try width: %d", font_width, TERM_WIDTH);
}
while (game_ascii_gl_height_get(game) * TERM_HEIGHT > game_window_pix_height_get(game)) {

while (font_height * TERM_HEIGHT > game_window_pix_height_get(game)) {
TERM_HEIGHT--;
LOG("SDL: - font height %u exceeded terminal pixel height %u, try term height: %d",
game_ascii_gl_height_get(game), game_window_pix_height_get(game), TERM_HEIGHT);
LOG("SDL: - term overflow, font height %u, try height: %d", font_height, TERM_HEIGHT);
}

//
// If we've gone too low, try a smaller power.
//
if ((TERM_WIDTH < TERM_WIDTH_MIN) || (TERM_HEIGHT < TERM_HEIGHT_MIN)) {
font_width /= 2;
font_height /= 2;
game_ascii_pix_width_set(game, font_width);
game_ascii_pix_height_set(game, font_height);
LOG("SDL: - ascii gl size (prev) : %ux%u", game_ascii_pix_width_get(game), game_ascii_pix_height_get(game));
}

//
// Try to grow the terminal.
//
while (font_width * TERM_WIDTH < game_window_pix_width_get(game) - font_width - 1) {
TERM_WIDTH++;
LOG("SDL: - term underflow, font width %u, try width: %d", font_width, TERM_WIDTH);
if (TERM_WIDTH >= TERM_WIDTH_MAX) {
LOG("SDL: - term width max, font width %u, term width: %d", font_width, TERM_WIDTH);
break;
}
}

while (font_height * TERM_HEIGHT < game_window_pix_height_get(game) - font_height - 1) {
TERM_HEIGHT++;
LOG("SDL: - term underflow, font height %u, try height: %d", font_height, TERM_HEIGHT);
if (TERM_HEIGHT >= TERM_HEIGHT_MAX) {
LOG("SDL: - term height max, font height %u term height: %d", font_height, TERM_HEIGHT);
break;
}
}

game_ascii_pix_width_set(game, font_width);
game_ascii_pix_height_set(game, font_height);

//
// Work out the size of the game map
//
Expand Down Expand Up @@ -756,9 +807,9 @@ void config_game_gfx_update(void)
// Compensate for any additional pixels the rounding of the terminal cells loses
//
double pixel_rounding_w_fixup
= (game_ascii_gl_width_get(game) * TERM_WIDTH) / (double) game_window_pix_width_get(game);
= (game_ascii_pix_width_get(game) * TERM_WIDTH) / (double) game_window_pix_width_get(game);
double pixel_rounding_h_fixup
= (game_ascii_gl_height_get(game) * TERM_HEIGHT) / (double) game_window_pix_height_get(game);
= (game_ascii_pix_height_get(game) * TERM_HEIGHT) / (double) game_window_pix_height_get(game);
visible_game_map_w *= pixel_rounding_w_fixup;
visible_game_map_h *= pixel_rounding_h_fixup;

Expand Down Expand Up @@ -786,14 +837,11 @@ void config_game_gfx_update(void)
game_tiles_visible_across_set(game, tiles_across);
game_tiles_visible_down_set(game, tiles_down);

LOG("SDL: Window:");
LOG("SDL: - window pixel size : %dx%d", game_window_pix_width_get(game), game_window_pix_height_get(game));
LOG("SDL: - game map pixel size : %dx%d", game_pix_width_get(game), game_pix_height_get(game));
LOG("SDL: - visible map pixel size : %dx%d", game_map_pix_width_get(game), game_map_pix_height_get(game));
LOG("SDL: Terminal");
LOG("SDL: - ascii gl size : %ux%u", game_ascii_gl_width_get(game), game_ascii_gl_height_get(game));
LOG("SDL: - term size : %dx%d", TERM_WIDTH, TERM_HEIGHT);
LOG("SDL: - aspect ratio : %f", game_aspect_ratio_get(game));
LOG("SDL: Buffers:");
LOG("SDL: - game map pixel sz : %dx%d", game_pix_width_get(game), game_pix_height_get(game));
LOG("SDL: - visible map pixel sz : %dx%d", game_map_pix_width_get(game), game_map_pix_height_get(game));
LOG("SDL: Map");
LOG("SDL: - size : %dx%d", MAP_WIDTH, MAP_HEIGHT);
}
4 changes: 2 additions & 2 deletions src/sdl_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ void sdl_display(class Game *g)
if (g) {
auto level = game_level_get(g);
if (level) {
auto w = game_ascii_gl_width_get(game);
auto h = game_ascii_gl_height_get(game);
auto w = game_ascii_pix_width_get(game);
auto h = game_ascii_pix_height_get(game);

int visible_map_tl_x = w * UI_LEFTBAR_WIDTH;
int visible_map_tl_y = h * UI_TOPCON_HEIGHT;
Expand Down

0 comments on commit 88f807b

Please sign in to comment.