Skip to content

Commit 6cb4bbf

Browse files
committed
increase buffer size for chunks being loaded; speed up terrain optimizer; minor code tweaking
1 parent 52098a7 commit 6cb4bbf

8 files changed

+134
-157
lines changed

draw_png.cpp

+56-54
Original file line numberDiff line numberDiff line change
@@ -65,31 +65,31 @@ namespace
6565
png_structp pngPtrCurrent = NULL; // This will be either the same as above, or a temp image when using disk caching
6666
FILE *gPngPartialFileHandle = NULL;
6767

68-
inline void blend(uint8_t *destination, const uint8_t *source);
69-
inline void modColor(uint8_t *color, const int mod);
70-
inline void addColor(uint8_t *color, uint8_t *add);
68+
inline void blend(uint8_t * const destination, const uint8_t * const source);
69+
inline void modColor(uint8_t * const color, const int mod);
70+
inline void addColor(uint8_t * const color, const uint8_t * const add);
7171

7272
// Split them up so setPixel won't be one hell of a mess
73-
void setSnow(const size_t x, const size_t y, const uint8_t *color);
74-
void setTorch(const size_t x, const size_t y, const uint8_t *color);
75-
void setFlower(const size_t x, const size_t y, const uint8_t *color);
76-
void setRedwire(const size_t x, const size_t y, const uint8_t *color);
77-
void setFire(const size_t x, const size_t y, uint8_t *color, uint8_t *light, uint8_t *dark);
78-
void setGrass(const size_t x, const size_t y, const uint8_t *color, const uint8_t *light, const uint8_t *dark, const int &sub);
79-
void setFence(const size_t x, const size_t y, const uint8_t *color);
80-
void setStep(const size_t x, const size_t y, const uint8_t *color, const uint8_t *light, const uint8_t *dark);
73+
void setSnow(const size_t x, const size_t y, const uint8_t * const color);
74+
void setTorch(const size_t x, const size_t y, const uint8_t * const color);
75+
void setFlower(const size_t x, const size_t y, const uint8_t * const color);
76+
void setRedwire(const size_t x, const size_t y, const uint8_t * const color);
77+
void setFire(const size_t x, const size_t y, const uint8_t * const color, const uint8_t * const light, const uint8_t * const dark);
78+
void setGrass(const size_t x, const size_t y, const uint8_t * const color, const uint8_t * const light, const uint8_t * const dark, const int sub);
79+
void setFence(const size_t x, const size_t y, const uint8_t * const color);
80+
void setStep(const size_t x, const size_t y, const uint8_t * const color, const uint8_t * const light, const uint8_t * const dark);
8181
# define setRailroad setSnowBA
8282

8383
// Then make duplicate copies so it is one hell of a mess
8484
// ..but hey, its for speeeeeeeeed!
85-
void setSnowBA(const size_t x, const size_t y, const uint8_t *color);
86-
void setTorchBA(const size_t x, const size_t y, const uint8_t *color);
87-
void setFlowerBA(const size_t x, const size_t y, const uint8_t *color);
88-
void setGrassBA(const size_t x, const size_t y, const uint8_t *color, const uint8_t *light, const uint8_t *dark, const int &sub);
89-
void setStepBA(const size_t x, const size_t y, const uint8_t *color, const uint8_t *light, const uint8_t *dark);
85+
void setSnowBA(const size_t x, const size_t y, const uint8_t * const color);
86+
void setTorchBA(const size_t x, const size_t y, const uint8_t * const color);
87+
void setFlowerBA(const size_t x, const size_t y, const uint8_t * const color);
88+
void setGrassBA(const size_t x, const size_t y, const uint8_t * const color, const uint8_t * const light, const uint8_t * const dark, const int sub);
89+
void setStepBA(const size_t x, const size_t y, const uint8_t * const color, const uint8_t * const light, const uint8_t * const dark);
9090
}
9191

92-
void createImageBuffer(size_t width, size_t height, bool splitUp)
92+
void createImageBuffer(const size_t width, const size_t height, const bool splitUp)
9393
{
9494
gPngLocalWidth = gPngWidth = (int)width;
9595
gPngLocalHeight = gPngHeight = (int)height;
@@ -102,7 +102,7 @@ void createImageBuffer(size_t width, size_t height, bool splitUp)
102102
}
103103
}
104104

105-
bool createImage(FILE *fh, size_t width, size_t height, bool splitUp)
105+
bool createImage(FILE *fh, const size_t width, const size_t height, const bool splitUp)
106106
{
107107
gPngLocalWidth = gPngWidth = (int)width;
108108
gPngLocalHeight = gPngHeight = (int)height;
@@ -293,7 +293,7 @@ bool saveImage()
293293
/**
294294
* @return 0 = OK, -1 = Error, 1 = Zero/Negative size
295295
*/
296-
int loadImagePart(int startx, int starty, int width, int height)
296+
int loadImagePart(const int startx, const int starty, const int width, const int height)
297297
{
298298
// These are set to NULL in saveImahePartPng to make sure the two functions are called in turn
299299
if (pngPtrCurrent != NULL || gPngPartialFileHandle != NULL) {
@@ -303,29 +303,31 @@ int loadImagePart(int startx, int starty, int width, int height)
303303
// In case the image needs to be cropped the offsets will be negative
304304
gOffsetX = MIN(startx, 0);
305305
gOffsetY = MIN(starty, 0);
306-
// Also modify width and height in these cases
307-
if (startx < 0) {
308-
width += startx;
309-
startx = 0;
306+
gPngLocalWidth = width;
307+
gPngLocalHeight = height;
308+
int localX = startx;
309+
int localY = starty;
310+
// Also modify gPngLocalWidth and gPngLocalHeight in these cases
311+
if (localX < 0) {
312+
gPngLocalWidth += localX;
313+
localX = 0;
310314
}
311-
if (starty < 0) {
312-
height += starty;
313-
starty = 0;
315+
if (localY < 0) {
316+
gPngLocalHeight += localY;
317+
localY = 0;
314318
}
315-
if (startx + width > gPngWidth) {
316-
width = gPngWidth - startx;
319+
if (localX + gPngLocalWidth > gPngWidth) {
320+
gPngLocalWidth = gPngWidth - localX;
317321
}
318-
if (starty + height > gPngHeight) {
319-
height = gPngHeight - starty;
322+
if (localY + gPngLocalHeight > gPngHeight) {
323+
gPngLocalHeight = gPngHeight - localY;
320324
}
321-
if (width < 1 || height < 1) return 1;
325+
if (gPngLocalWidth < 1 || gPngLocalHeight < 1) return 1;
322326
char name[200];
323-
snprintf(name, 200, "cache/%d.%d.%d.%d.%d.png", startx, starty, width, height, (int)time(NULL));
324-
ImagePart *img = new ImagePart(name, startx, starty, width, height);
327+
snprintf(name, 200, "cache/%d.%d.%d.%d.%d.png", localX, localY, gPngLocalWidth, gPngLocalHeight, (int)time(NULL));
328+
ImagePart *img = new ImagePart(name, localX, localY, gPngLocalWidth, gPngLocalHeight);
325329
partialImages.push_back(img);
326330
// alloc mem for image and open tempfile
327-
gPngLocalWidth = width;
328-
gPngLocalHeight = height;
329331
gPngLocalLineWidthChans = gPngLocalWidth * CHANSPERPIXEL;
330332
uint64_t size = (uint64_t)gPngLocalLineWidthChans * (uint64_t)gPngLocalHeight;
331333
printf("Creating temporary image: %dx%d, 32bpp, %.2fMiB\n", gPngLocalWidth, gPngLocalHeight, float(size / float(1024 * 1024)));
@@ -599,14 +601,14 @@ bool composeFinalImage()
599601
return true;
600602
}
601603

602-
uint64_t calcImageSize(int mapChunksX, int mapChunksZ, size_t mapHeight, int &pixelsX, int &pixelsY, bool tight)
604+
uint64_t calcImageSize(const int mapChunksX, const int mapChunksZ, const size_t mapHeight, int &pixelsX, int &pixelsY, const bool tight)
603605
{
604606
pixelsX = (mapChunksX * CHUNKSIZE_X + mapChunksZ * CHUNKSIZE_Z) * 2 + (tight ? 3 : 10);
605607
pixelsY = (mapChunksX * CHUNKSIZE_X + mapChunksZ * CHUNKSIZE_Z + int(mapHeight) * g_OffsetY) + (tight ? 3 : 10);
606608
return uint64_t(pixelsX) * BYTESPERPIXEL * uint64_t(pixelsY);
607609
}
608610

609-
void setPixel(size_t x, size_t y, uint8_t color, float fsub)
611+
void setPixel(const size_t x, const size_t y, const uint8_t color, const float fsub)
610612
{
611613
// Sets pixels around x,y where A is the anchor
612614
// T = given color, D = darker, L = lighter
@@ -791,7 +793,7 @@ void setPixel(size_t x, size_t y, uint8_t color, float fsub)
791793
// The above two branches are almost the same, maybe one could just create a function pointer and...
792794
}
793795

794-
void blendPixel(size_t x, size_t y, uint8_t color, float fsub)
796+
void blendPixel(const size_t x, const size_t y, const uint8_t color, const float fsub)
795797
{
796798
// This one is used for cave overlay
797799
// Sets pixels around x,y where A is the anchor
@@ -835,7 +837,7 @@ void blendPixel(size_t x, size_t y, uint8_t color, float fsub)
835837
namespace
836838
{
837839

838-
inline void blend(uint8_t *destination, const uint8_t *source)
840+
inline void blend(uint8_t * const destination, const uint8_t * const source)
839841
{
840842
if (destination[PALPHA] == 0 || source[PALPHA] == 255) {
841843
memcpy(destination, source, BYTESPERPIXEL);
@@ -848,14 +850,14 @@ namespace
848850
destination[PALPHA] += (size_t(source[PALPHA]) * size_t(255 - destination[PALPHA])) / 255;
849851
}
850852

851-
inline void modColor(uint8_t *color, const int mod)
853+
inline void modColor(uint8_t * const color, const int mod)
852854
{
853855
color[0] = clamp(color[0] + mod);
854856
color[1] = clamp(color[1] + mod);
855857
color[2] = clamp(color[2] + mod);
856858
}
857859

858-
inline void addColor(uint8_t *color, uint8_t *add)
860+
inline void addColor(uint8_t * const color, const uint8_t * const add)
859861
{
860862
const float v2 = (float(add[PALPHA]) / 255.0f);
861863
const float v1 = (1.0f - (v2 * .2f));
@@ -864,7 +866,7 @@ namespace
864866
color[2] = clamp(uint16_t(float(color[2]) * v1 + float(add[2]) * v2));
865867
}
866868

867-
void setSnow(const size_t x, const size_t y, const uint8_t *color)
869+
void setSnow(const size_t x, const size_t y, const uint8_t * const color)
868870
{
869871
// Top row (second row)
870872
uint8_t *pos = &PIXEL(x, y+1);
@@ -873,7 +875,7 @@ namespace
873875
}
874876
}
875877

876-
void setTorch(const size_t x, const size_t y, const uint8_t *color)
878+
void setTorch(const size_t x, const size_t y, const uint8_t * const color)
877879
{
878880
// Maybe the orientation should be considered when drawing, but it probably isn't worth the efford
879881
uint8_t *pos = &PIXEL(x+2, y+1);
@@ -882,7 +884,7 @@ namespace
882884
memcpy(pos, color, BYTESPERPIXEL);
883885
}
884886

885-
void setFlower(const size_t x, const size_t y, const uint8_t *color)
887+
void setFlower(const size_t x, const size_t y, const uint8_t * const color)
886888
{
887889
uint8_t *pos = &PIXEL(x, y+1);
888890
memcpy(pos+(CHANSPERPIXEL), color, BYTESPERPIXEL);
@@ -893,7 +895,7 @@ namespace
893895
memcpy(pos, color, BYTESPERPIXEL);
894896
}
895897

896-
void setFire(const size_t x, const size_t y, uint8_t *color, uint8_t *light, uint8_t *dark)
898+
void setFire(const size_t x, const size_t y, const uint8_t * const color, const uint8_t * const light, const uint8_t * const dark)
897899
{
898900
// This basically just leaves out a few pixels
899901
// Top row
@@ -913,7 +915,7 @@ namespace
913915
blend(pos+(CHANSPERPIXEL*2), light);
914916
}
915917

916-
void setGrass(const size_t x, const size_t y, const uint8_t *color, const uint8_t *light, const uint8_t *dark, const int &sub)
918+
void setGrass(const size_t x, const size_t y, const uint8_t * const color, const uint8_t * const light, const uint8_t * const dark, const int sub)
917919
{
918920
// this will make grass look like dirt from the side
919921
uint8_t L[CHANSPERPIXEL], D[CHANSPERPIXEL];
@@ -954,7 +956,7 @@ namespace
954956
memcpy(pos+CHANSPERPIXEL*3, L, BYTESPERPIXEL);
955957
}
956958

957-
void setFence(const size_t x, const size_t y, const uint8_t *color)
959+
void setFence(const size_t x, const size_t y, const uint8_t * const color)
958960
{
959961
// First row
960962
uint8_t *pos = &PIXEL(x, y);
@@ -969,7 +971,7 @@ namespace
969971
blend(pos+CHANSPERPIXEL*2, color);
970972
}
971973

972-
void setStep(const size_t x, const size_t y, const uint8_t *color, const uint8_t *light, const uint8_t *dark)
974+
void setStep(const size_t x, const size_t y, const uint8_t * const color, const uint8_t * const light, const uint8_t * const dark)
973975
{
974976
uint8_t *pos = &PIXEL(x, y+1);
975977
for (size_t i = 0; i < 4; ++i, pos += CHANSPERPIXEL) {
@@ -981,7 +983,7 @@ namespace
981983
}
982984
}
983985

984-
void setRedwire(const size_t x, const size_t y, const uint8_t *color)
986+
void setRedwire(const size_t x, const size_t y, const uint8_t * const color)
985987
{
986988
uint8_t *pos = &PIXEL(x+1, y+2);
987989
blend(pos, color);
@@ -990,7 +992,7 @@ namespace
990992

991993
// The g_BlendAll versions of the block set functions
992994
//
993-
void setSnowBA(const size_t x, const size_t y, const uint8_t *color)
995+
void setSnowBA(const size_t x, const size_t y, const uint8_t * const color)
994996
{
995997
// Top row (second row)
996998
uint8_t *pos = &PIXEL(x, y+1);
@@ -999,7 +1001,7 @@ namespace
9991001
}
10001002
}
10011003

1002-
void setTorchBA(const size_t x, const size_t y, const uint8_t *color)
1004+
void setTorchBA(const size_t x, const size_t y, const uint8_t * const color)
10031005
{
10041006
// Maybe the orientation should be considered when drawing, but it probably isn't worth the effort
10051007
uint8_t *pos = &PIXEL(x+2, y+1);
@@ -1008,7 +1010,7 @@ namespace
10081010
blend(pos, color);
10091011
}
10101012

1011-
void setFlowerBA(const size_t x, const size_t y, const uint8_t *color)
1013+
void setFlowerBA(const size_t x, const size_t y, const uint8_t * const color)
10121014
{
10131015
uint8_t *pos = &PIXEL(x, y+1);
10141016
blend(pos+CHANSPERPIXEL, color);
@@ -1019,7 +1021,7 @@ namespace
10191021
blend(pos, color);
10201022
}
10211023

1022-
void setGrassBA(const size_t x, const size_t y, const uint8_t *color, const uint8_t *light, const uint8_t *dark, const int &sub)
1024+
void setGrassBA(const size_t x, const size_t y, const uint8_t * const color, const uint8_t * const light, const uint8_t * const dark, const int sub)
10231025
{
10241026
// this will make grass look like dirt from the side
10251027
uint8_t L[CHANSPERPIXEL], D[CHANSPERPIXEL];
@@ -1060,7 +1062,7 @@ namespace
10601062
blend(pos+CHANSPERPIXEL*3, L);
10611063
}
10621064

1063-
void setStepBA(const size_t x, const size_t y, const uint8_t *color, const uint8_t *light, const uint8_t *dark)
1065+
void setStepBA(const size_t x, const size_t y, const uint8_t * const color, const uint8_t * const light, const uint8_t * const dark)
10641066
{
10651067
uint8_t *pos = &PIXEL(x, y+1);
10661068
for (size_t i = 0; i < 3; ++i, pos += CHANSPERPIXEL) {

draw_png.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88

99
#include "helper.h"
1010

11-
void createImageBuffer(size_t width, size_t height, bool splitUp);
12-
bool createImage(FILE *fh, size_t width, size_t height, bool splitUp);
11+
void createImageBuffer(const size_t width, const size_t height, const bool splitUp);
12+
bool createImage(FILE *fh, const size_t width, const size_t height, const bool splitUp);
1313
bool saveImage();
14-
int loadImagePart(int startx, int starty, int width, int height);
15-
void setPixel(size_t x, size_t y, uint8_t color, float fsub);
16-
void blendPixel(size_t x, size_t y, uint8_t color, float fsub);
14+
int loadImagePart(const int startx, const int starty, const int width, const int height);
15+
void setPixel(const size_t x, const size_t y, const uint8_t color, const float fsub);
16+
void blendPixel(const size_t x, const size_t y, const uint8_t color, const float fsub);
1717
bool saveImagePart();
1818
bool discardImagePart();
1919
bool composeFinalImage();
20-
uint64_t calcImageSize(int mapChunksX, int mapChunksZ, size_t mapHeight, int &pixelsX, int &pixelsY, bool tight = false);
20+
uint64_t calcImageSize(const int mapChunksX, const int mapChunksZ, const size_t mapHeight, int &pixelsX, int &pixelsY, const bool tight = false);
2121

2222
#endif

globals.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#ifndef _GLOBALS_H_
22
#define _GLOBALS_H_
33

4-
#define VERSION "2.0c"
4+
#define VERSION "2.0d"
55

66
#include <stdint.h>
77
#include <cstdlib>

0 commit comments

Comments
 (0)