Skip to content

Commit

Permalink
output: fix crash when using console on legal.pcx
Browse files Browse the repository at this point in the history
  • Loading branch information
rr- committed Jan 29, 2025
1 parent 763eae7 commit 39fd9f8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
23 changes: 16 additions & 7 deletions src/libtrx/game/output/textures.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@
#include "game/shell.h"

static int32_t m_ObjectTextureCount = 0;
static OBJECT_TEXTURE *m_ObjectTextures = NULL;
static SPRITE_TEXTURE *m_SpriteTextures = NULL;
static ANIMATED_TEXTURE_RANGE *m_AnimTextureRanges = NULL;
static OBJECT_TEXTURE *m_ObjectTextures = nullptr;
static SPRITE_TEXTURE *m_SpriteTextures = nullptr;
static ANIMATED_TEXTURE_RANGE *m_AnimTextureRanges = nullptr;

void Output_InitialiseObjectTextures(const int32_t num_textures)
{
m_ObjectTextureCount = num_textures;
m_ObjectTextures = num_textures == 0
? NULL
? nullptr
: GameBuf_Alloc(
sizeof(OBJECT_TEXTURE) * num_textures, GBUF_OBJECT_TEXTURES);
}

void Output_InitialiseSpriteTextures(const int32_t num_textures)
{
m_SpriteTextures = num_textures == 0
? NULL
? nullptr
: GameBuf_Alloc(
sizeof(SPRITE_TEXTURE) * num_textures, GBUF_SPRITE_TEXTURES);
}

void Output_InitialiseAnimatedTextures(const int32_t num_ranges)
{
m_AnimTextureRanges = num_ranges == 0
? NULL
? nullptr
: GameBuf_Alloc(
sizeof(ANIMATED_TEXTURE_RANGE) * num_ranges,
GBUF_ANIMATED_TEXTURE_RANGES);
Expand All @@ -42,23 +42,32 @@ int32_t Output_GetObjectTextureCount(void)

OBJECT_TEXTURE *Output_GetObjectTexture(const int32_t texture_idx)
{
if (m_ObjectTextures == nullptr) {
return nullptr;
}
return &m_ObjectTextures[texture_idx];
}

SPRITE_TEXTURE *Output_GetSpriteTexture(const int32_t texture_idx)
{
if (m_SpriteTextures == nullptr) {
return nullptr;
}
return &m_SpriteTextures[texture_idx];
}

ANIMATED_TEXTURE_RANGE *Output_GetAnimatedTextureRange(const int32_t range_idx)
{
if (m_AnimTextureRanges == nullptr) {
return nullptr;
}
return &m_AnimTextureRanges[range_idx];
}

void Output_CycleAnimatedTextures(void)
{
const ANIMATED_TEXTURE_RANGE *range = m_AnimTextureRanges;
for (; range != NULL; range = range->next_range) {
for (; range != nullptr; range = range->next_range) {
int32_t i = 0;
const OBJECT_TEXTURE temp = m_ObjectTextures[range->textures[i]];
for (; i < range->num_textures - 1; i++) {
Expand Down
6 changes: 5 additions & 1 deletion src/tr1/game/text.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ void Text_DrawText(TEXTSTRING *const text)
return;
}

const OBJECT *const obj = Object_GetObject(O_ALPHABET);
if (!obj->loaded) {
return;
}

if (text->flags.flash) {
text->flash.count -= Clock_GetFrameAdvance();
if (text->flash.count <= -text->flash.rate) {
Expand Down Expand Up @@ -159,7 +164,6 @@ void Text_DrawText(TEXTSTRING *const text)
int32_t sv;
const int32_t start_x = x;

const OBJECT *const obj = Object_GetObject(O_ALPHABET);
const GLYPH_INFO **glyph_ptr = text->glyphs;
while (*glyph_ptr != NULL) {
const GLYPH_INFO *glyph = *glyph_ptr;
Expand Down
7 changes: 7 additions & 0 deletions src/tr2/game/output.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "global/vars.h"

#include <libtrx/config.h>
#include <libtrx/debug.h>
#include <libtrx/game/math.h>
#include <libtrx/game/matrix.h>
#include <libtrx/log.h>
Expand Down Expand Up @@ -285,6 +286,7 @@ static void M_DrawRoomSprites(const ROOM_MESH *const mesh)

const SPRITE_TEXTURE *const sprite =
Output_GetSpriteTexture(room_sprite->texture);
ASSERT(sprite != nullptr);
const double persp = (double)(vbuf->zv / g_PhdPersp);
const double x0 =
g_PhdWinCenterX + (vbuf->xv + (sprite->x0 << W2V_SHIFT)) / persp;
Expand Down Expand Up @@ -498,6 +500,7 @@ void Output_DrawSprite(
}

const SPRITE_TEXTURE *const sprite = Output_GetSpriteTexture(sprite_idx);
ASSERT(sprite != nullptr);
int32_t x0 = sprite->x0;
int32_t y0 = sprite->y0;
int32_t x1 = sprite->x1;
Expand Down Expand Up @@ -557,6 +560,7 @@ void Output_DrawPickup(
const int16_t sprite_idx, const int16_t shade)
{
const SPRITE_TEXTURE *const sprite = Output_GetSpriteTexture(sprite_idx);
ASSERT(sprite != nullptr);
const int32_t x0 = sx + ((sprite->x0 * scale) / PHD_ONE);
const int32_t y0 = sy + ((sprite->y0 * scale) / PHD_ONE);
const int32_t x1 = sx + ((sprite->x1 * scale) / PHD_ONE);
Expand All @@ -572,6 +576,7 @@ void Output_DrawScreenSprite2D(
const uint16_t flags)
{
const SPRITE_TEXTURE *const sprite = Output_GetSpriteTexture(sprite_idx);
ASSERT(sprite != nullptr);
const int32_t x0 = sx + ((sprite->x0 * scale_h) / PHD_ONE);
const int32_t y0 = sy + ((sprite->y0 * scale_v) / PHD_ONE);
const int32_t x1 = sx + ((sprite->x1 * scale_h) / PHD_ONE);
Expand All @@ -588,6 +593,7 @@ void Output_DrawScreenSprite(
const uint16_t flags)
{
const SPRITE_TEXTURE *const sprite = Output_GetSpriteTexture(sprite_idx);
ASSERT(sprite != nullptr);
const int32_t x0 = sx + (((sprite->x0 / 8) * scale_h) / PHD_ONE);
const int32_t x1 = sx + (((sprite->x1 / 8) * scale_h) / PHD_ONE);
const int32_t y0 = sy + (((sprite->y0 / 8) * scale_v) / PHD_ONE);
Expand Down Expand Up @@ -649,6 +655,7 @@ void Output_LoadBackgroundFromObject(void)

const int32_t texture_idx = mesh->tex_face4s[0].texture_idx;
const OBJECT_TEXTURE *const texture = Output_GetObjectTexture(texture_idx);
ASSERT(texture != nullptr);
Render_LoadBackgroundFromTexture(texture, 8, 6);
m_BackgroundType = BK_OBJECT;
return;
Expand Down
8 changes: 6 additions & 2 deletions src/tr2/game/text.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ void Text_DrawText(TEXTSTRING *const text)
return;
}

const OBJECT *const obj = Object_GetObject(O_ALPHABET);
if (!obj->loaded) {
return;
}

int32_t box_w = 0;
int32_t box_h = 0;
const int32_t scale_h = M_Scale(text->scale.h);
Expand Down Expand Up @@ -128,8 +133,7 @@ void Text_DrawText(TEXTSTRING *const text)
if (x >= 0 && x < g_PhdWinWidth && y >= 0 && y < g_PhdWinHeight) {
Output_DrawScreenSprite2D(
x, y, z, scale_h, scale_v,
g_Objects[O_ALPHABET].mesh_idx + (*glyph_ptr)->mesh_idx, 4096,
0);
obj->mesh_idx + (*glyph_ptr)->mesh_idx, 4096, 0);
}

if ((*glyph_ptr)->role != GLYPH_COMBINING) {
Expand Down

0 comments on commit 39fd9f8

Please sign in to comment.