shithub: cstory

Download patch

ref: c844bc9f49ea91d313704db6f162a94ba798bfef
parent: d7813fd900763c7580bdf407b4ce585acdc848c6
author: Clownacy <[email protected]>
date: Wed Feb 13 11:49:45 EST 2019

Avoid VLAs

'VLAs are bad because stack overflows' yeah yeah yeah except this
doesn't involve the stack at all.

--- a/src/Draw.cpp
+++ b/src/Draw.cpp
@@ -187,21 +187,21 @@
 	int 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 w = 0; w < surf[surf_no].surface->w; ++w)
 		{
-			dst_pixels[h][w][0] = src_pixels[h][w][0];
-			dst_pixels[h][w][1] = src_pixels[h][w][1];
-			dst_pixels[h][w][2] = src_pixels[h][w][2];
+			unsigned char *src_pixel = (unsigned char*)surf[surf_no].surface->pixels + (h * surf[surf_no].surface->pitch) + (w * 4);
+			unsigned char *dst_pixel = (unsigned char*)raw_pixels + (h * pitch) + (w * 4);
 
-			if (src_pixels[h][w][0] || src_pixels[h][w][1] || src_pixels[h][w][2])	// Colour-key
-				dst_pixels[h][w][3] = 0xFF;
+			dst_pixel[0] = src_pixel[0];
+			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
-				dst_pixels[h][w][3] = 0;
+				dst_pixel[3] = 0;
 		}
 	}
 
@@ -254,24 +254,24 @@
 						else
 						{
 							// 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)
 							{
-								const unsigned long *src_row = (unsigned long*)src_pixels[h];
-								unsigned long *dst_row = (unsigned long*)dst_pixels[h * magnification];
+								const unsigned char *src_row = (unsigned char*)converted_surface->pixels + h * converted_surface->pitch;
+								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)
 								{
-									const unsigned long src_pixel = *src_row++;
+									const unsigned long src_pixel = *src_ptr++;
 
 									for (int i = 0; i < magnification; ++i)
-										*dst_row++ = src_pixel;
+										*dst_ptr++ = src_pixel;
 								}
 
 								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);
--- a/src/Font.cpp
+++ b/src/Font.cpp
@@ -181,8 +181,6 @@
 	{
 		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;
 
 		unsigned int pen_x = 0;
@@ -236,11 +234,9 @@
 				{
 					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])
 						{
 							for (unsigned int j = 0; j < 3; ++j)
@@ -257,11 +253,11 @@
 				{
 					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)
 						{
@@ -276,11 +272,11 @@
 				{
 					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)
 								surface_pixel[j] = colours[j];