Avoid VLAs

'VLAs are bad because stack overflows' yeah yeah yeah except this
doesn't involve the stack at all.
This commit is contained in:
Clownacy 2019-02-13 16:49:45 +00:00
parent d7813fd900
commit c844bc9f49
2 changed files with 25 additions and 29 deletions

View file

@ -187,21 +187,21 @@ static void FlushSurface(int surf_no)
int pitch; int pitch;
SDL_LockTexture(surf[surf_no].texture, NULL, (void**)&raw_pixels, &pitch); SDL_LockTexture(surf[surf_no].texture, NULL, (void**)&raw_pixels, &pitch);
unsigned char (*src_pixels)[surf[surf_no].surface->pitch / 4][4] = (unsigned char (*)[surf[surf_no].surface->pitch/ 4][4])surf[surf_no].surface->pixels;
unsigned char (*dst_pixels)[pitch / 4][4] = (unsigned char (*)[pitch/ 4][4])raw_pixels;
for (int h = 0; h < surf[surf_no].surface->h; ++h) for (int h = 0; h < surf[surf_no].surface->h; ++h)
{ {
for (int w = 0; w < surf[surf_no].surface->w; ++w) for (int w = 0; w < surf[surf_no].surface->w; ++w)
{ {
dst_pixels[h][w][0] = src_pixels[h][w][0]; unsigned char *src_pixel = (unsigned char*)surf[surf_no].surface->pixels + (h * surf[surf_no].surface->pitch) + (w * 4);
dst_pixels[h][w][1] = src_pixels[h][w][1]; unsigned char *dst_pixel = (unsigned char*)raw_pixels + (h * pitch) + (w * 4);
dst_pixels[h][w][2] = src_pixels[h][w][2];
if (src_pixels[h][w][0] || src_pixels[h][w][1] || src_pixels[h][w][2]) // Colour-key dst_pixel[0] = src_pixel[0];
dst_pixels[h][w][3] = 0xFF; dst_pixel[1] = src_pixel[1];
dst_pixel[2] = src_pixel[2];
if (src_pixel[0] || src_pixel[1] || src_pixel[2]) // Colour-key
dst_pixel[3] = 0xFF;
else else
dst_pixels[h][w][3] = 0; dst_pixel[3] = 0;
} }
} }
@ -254,24 +254,24 @@ static bool LoadBitmap(SDL_RWops *fp, int surf_no, bool create_surface)
else else
{ {
// Upscale the bitmap to the game's native resolution (SDL_BlitScaled is buggy, so we have to do it on our own) // Upscale the bitmap to the game's native resolution (SDL_BlitScaled is buggy, so we have to do it on our own)
const unsigned char (*src_pixels)[converted_surface->pitch] = (unsigned char(*)[converted_surface->pitch])converted_surface->pixels;
unsigned char (*dst_pixels)[surf[surf_no].surface->pitch] = (unsigned char(*)[surf[surf_no].surface->pitch])surf[surf_no].surface->pixels;
for (int h = 0; h < converted_surface->h; ++h) for (int h = 0; h < converted_surface->h; ++h)
{ {
const unsigned long *src_row = (unsigned long*)src_pixels[h]; const unsigned char *src_row = (unsigned char*)converted_surface->pixels + h * converted_surface->pitch;
unsigned long *dst_row = (unsigned long*)dst_pixels[h * magnification]; unsigned char *dst_row = (unsigned char*)surf[surf_no].surface->pixels + h * surf[surf_no].surface->pitch * magnification;
const unsigned long *src_ptr = (unsigned long*)src_row;
unsigned long *dst_ptr = (unsigned long*)dst_row;
for (int w = 0; w < converted_surface->w; ++w) for (int w = 0; w < converted_surface->w; ++w)
{ {
const unsigned long src_pixel = *src_row++; const unsigned long src_pixel = *src_ptr++;
for (int i = 0; i < magnification; ++i) for (int i = 0; i < magnification; ++i)
*dst_row++ = src_pixel; *dst_ptr++ = src_pixel;
} }
for (int i = 1; i < magnification; ++i) for (int i = 1; i < magnification; ++i)
memcpy(dst_pixels[(h * magnification) + i], dst_pixels[h * magnification], surf[surf_no].surface->w * sizeof(unsigned long)); memcpy(dst_row + i * surf[surf_no].surface->pitch, dst_row, surf[surf_no].surface->w * sizeof(unsigned long));
} }
SDL_FreeSurface(converted_surface); SDL_FreeSurface(converted_surface);

View file

@ -181,8 +181,6 @@ void DrawText(FontObject *font_object, SDL_Surface *surface, int x, int y, unsig
{ {
const unsigned char colours[3] = {(unsigned char)(colour >> 16), (unsigned char)(colour >> 8), (unsigned char)colour}; const unsigned char colours[3] = {(unsigned char)(colour >> 16), (unsigned char)(colour >> 8), (unsigned char)colour};
unsigned char (*surface_buffer)[surface->pitch / 4][4] = (unsigned char (*)[surface->pitch / 4][4])surface->pixels;
FT_Face face = font_object->face; FT_Face face = font_object->face;
unsigned int pen_x = 0; unsigned int pen_x = 0;
@ -236,10 +234,8 @@ void DrawText(FontObject *font_object, SDL_Surface *surface, int x, int y, unsig
{ {
for (int ix = MAX(-letter_x, 0); letter_x + ix < MIN(letter_x + (int)converted.width / 3, surface->w); ++ix) for (int ix = MAX(-letter_x, 0); letter_x + ix < MIN(letter_x + (int)converted.width / 3, surface->w); ++ix)
{ {
const unsigned char (*font_buffer)[converted.pitch / 3][3] = (unsigned char (*)[converted.pitch / 3][3])converted.buffer; const unsigned char *font_pixel = converted.buffer + iy * converted.pitch + ix * 3;
unsigned char *surface_pixel = (unsigned char*)surface->pixels + (letter_y + iy) * surface->pitch + (letter_x + ix) * 4;
const unsigned char *font_pixel = font_buffer[iy][ix];
unsigned char *surface_pixel = surface_buffer[letter_y + iy][letter_x + ix];
if (font_pixel[0] || font_pixel[1] || font_pixel[2]) if (font_pixel[0] || font_pixel[1] || font_pixel[2])
{ {
@ -257,11 +253,11 @@ void DrawText(FontObject *font_object, SDL_Surface *surface, int x, int y, unsig
{ {
for (int ix = MAX(-letter_x, 0); letter_x + ix < MIN(letter_x + (int)converted.width, surface->w); ++ix) for (int ix = MAX(-letter_x, 0); letter_x + ix < MIN(letter_x + (int)converted.width, surface->w); ++ix)
{ {
unsigned char (*font_buffer)[converted.pitch] = (unsigned char (*)[converted.pitch])converted.buffer; const unsigned char font_pixel = converted.buffer[iy * converted.pitch + ix];
const double alpha = pow((double)font_buffer[iy][ix] / (converted.num_grays - 1), 1.0 / 1.8); // Gamma-corrected const double alpha = pow((double)font_pixel / (converted.num_grays - 1), 1.0 / 1.8); // Gamma-corrected
unsigned char *surface_pixel = surface_buffer[letter_y + iy][letter_x + ix]; unsigned char *surface_pixel = (unsigned char*)surface->pixels + (letter_y + iy) * surface->pitch + (letter_x + ix) * 4;
if (alpha) if (alpha)
{ {
@ -276,11 +272,11 @@ void DrawText(FontObject *font_object, SDL_Surface *surface, int x, int y, unsig
{ {
for (int ix = MAX(-letter_x, 0); letter_x + ix < MIN(letter_x + (int)converted.width, surface->w); ++ix) for (int ix = MAX(-letter_x, 0); letter_x + ix < MIN(letter_x + (int)converted.width, surface->w); ++ix)
{ {
unsigned char (*font_buffer)[converted.pitch] = (unsigned char (*)[converted.pitch])converted.buffer; const unsigned char font_pixel = converted.buffer[iy * converted.pitch + ix];
unsigned char *surface_pixel = surface_buffer[letter_y + iy][letter_x + ix]; unsigned char *surface_pixel = (unsigned char*)surface->pixels + (letter_y + iy) * surface->pitch + (letter_x + ix) * 4;
if (font_buffer[iy][ix]) if (font_pixel)
{ {
for (unsigned int j = 0; j < 3; ++j) for (unsigned int j = 0; j < 3; ++j)
surface_pixel[j] = colours[j]; surface_pixel[j] = colours[j];